28 .r = VK_COMPONENT_SWIZZLE_IDENTITY,
29 .g = VK_COMPONENT_SWIZZLE_IDENTITY,
30 .b = VK_COMPONENT_SWIZZLE_IDENTITY,
31 .a = VK_COMPONENT_SWIZZLE_IDENTITY,
37 #define CASE(VAL) case VAL: return #VAL
45 CASE(VK_ERROR_OUT_OF_HOST_MEMORY);
46 CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY);
47 CASE(VK_ERROR_INITIALIZATION_FAILED);
48 CASE(VK_ERROR_DEVICE_LOST);
49 CASE(VK_ERROR_MEMORY_MAP_FAILED);
50 CASE(VK_ERROR_LAYER_NOT_PRESENT);
51 CASE(VK_ERROR_EXTENSION_NOT_PRESENT);
52 CASE(VK_ERROR_FEATURE_NOT_PRESENT);
53 CASE(VK_ERROR_INCOMPATIBLE_DRIVER);
54 CASE(VK_ERROR_TOO_MANY_OBJECTS);
55 CASE(VK_ERROR_FORMAT_NOT_SUPPORTED);
56 CASE(VK_ERROR_FRAGMENTED_POOL);
57 CASE(VK_ERROR_UNKNOWN);
58 CASE(VK_ERROR_OUT_OF_POOL_MEMORY);
59 CASE(VK_ERROR_INVALID_EXTERNAL_HANDLE);
60 CASE(VK_ERROR_FRAGMENTATION);
61 CASE(VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS);
62 CASE(VK_PIPELINE_COMPILE_REQUIRED);
63 CASE(VK_ERROR_SURFACE_LOST_KHR);
64 CASE(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
65 CASE(VK_SUBOPTIMAL_KHR);
66 CASE(VK_ERROR_OUT_OF_DATE_KHR);
67 CASE(VK_ERROR_INCOMPATIBLE_DISPLAY_KHR);
68 CASE(VK_ERROR_VALIDATION_FAILED_EXT);
69 CASE(VK_ERROR_INVALID_SHADER_NV);
70 CASE(VK_ERROR_VIDEO_PICTURE_LAYOUT_NOT_SUPPORTED_KHR);
71 CASE(VK_ERROR_VIDEO_PROFILE_OPERATION_NOT_SUPPORTED_KHR);
72 CASE(VK_ERROR_VIDEO_PROFILE_FORMAT_NOT_SUPPORTED_KHR);
73 CASE(VK_ERROR_VIDEO_PROFILE_CODEC_NOT_SUPPORTED_KHR);
74 CASE(VK_ERROR_VIDEO_STD_VERSION_NOT_SUPPORTED_KHR);
75 CASE(VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT);
76 CASE(VK_ERROR_NOT_PERMITTED_KHR);
77 CASE(VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT);
78 CASE(VK_THREAD_IDLE_KHR);
79 CASE(VK_THREAD_DONE_KHR);
80 CASE(VK_OPERATION_DEFERRED_KHR);
81 CASE(VK_OPERATION_NOT_DEFERRED_KHR);
82 default:
return "Unknown error";
90 for (
int i = 0;
i <
s->hwctx->nb_qf;
i++) {
93 for (
int j = 0; j <
s->nb_qfs; j++) {
94 if (
s->qfs[j] ==
s->hwctx->qf[
i].idx) {
102 s->qfs[
s->nb_qfs++] =
s->hwctx->qf[
i].idx;
110 s->hprops = (VkPhysicalDeviceExternalMemoryHostPropertiesEXT) {
111 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT,
113 s->optical_flow_props = (VkPhysicalDeviceOpticalFlowPropertiesNV) {
114 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_OPTICAL_FLOW_PROPERTIES_NV,
117 s->coop_matrix_props = (VkPhysicalDeviceCooperativeMatrixPropertiesKHR) {
118 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_PROPERTIES_KHR,
119 .pNext = &
s->optical_flow_props,
121 s->subgroup_props = (VkPhysicalDeviceSubgroupSizeControlProperties) {
122 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES,
123 .pNext = &
s->coop_matrix_props,
125 s->desc_buf_props = (VkPhysicalDeviceDescriptorBufferPropertiesEXT) {
126 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_BUFFER_PROPERTIES_EXT,
127 .pNext = &
s->subgroup_props,
129 s->driver_props = (VkPhysicalDeviceDriverProperties) {
130 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES,
131 .pNext = &
s->desc_buf_props,
133 s->props = (VkPhysicalDeviceProperties2) {
134 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
135 .pNext = &
s->driver_props,
138 s->atomic_float_feats = (VkPhysicalDeviceShaderAtomicFloatFeaturesEXT) {
139 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_FLOAT_FEATURES_EXT,
141 s->feats_12 = (VkPhysicalDeviceVulkan12Features) {
142 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
143 .pNext = &
s->atomic_float_feats,
145 s->feats = (VkPhysicalDeviceFeatures2) {
146 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
147 .pNext = &
s->feats_12,
150 vk->GetPhysicalDeviceProperties2(
s->hwctx->phys_dev, &
s->props);
151 vk->GetPhysicalDeviceMemoryProperties(
s->hwctx->phys_dev, &
s->mprops);
152 vk->GetPhysicalDeviceFeatures2(
s->hwctx->phys_dev, &
s->feats);
159 vk->GetPhysicalDeviceQueueFamilyProperties2(
s->hwctx->phys_dev, &
s->tot_nb_qfs,
NULL);
161 s->qf_props =
av_calloc(
s->tot_nb_qfs,
sizeof(*
s->qf_props));
165 s->query_props =
av_calloc(
s->tot_nb_qfs,
sizeof(*
s->query_props));
171 s->video_props =
av_calloc(
s->tot_nb_qfs,
sizeof(*
s->video_props));
172 if (!
s->video_props) {
178 for (uint32_t
i = 0;
i <
s->tot_nb_qfs;
i++) {
179 s->query_props[
i] = (VkQueueFamilyQueryResultStatusPropertiesKHR) {
180 .sType = VK_STRUCTURE_TYPE_QUEUE_FAMILY_QUERY_RESULT_STATUS_PROPERTIES_KHR,
182 s->video_props[
i] = (VkQueueFamilyVideoPropertiesKHR) {
183 .sType = VK_STRUCTURE_TYPE_QUEUE_FAMILY_VIDEO_PROPERTIES_KHR,
184 .pNext = &
s->query_props[
i],
186 s->qf_props[
i] = (VkQueueFamilyProperties2) {
187 .sType = VK_STRUCTURE_TYPE_QUEUE_FAMILY_PROPERTIES_2,
188 .pNext = &
s->video_props[
i],
192 vk->GetPhysicalDeviceQueueFamilyProperties2(
s->hwctx->phys_dev, &
s->tot_nb_qfs,
s->qf_props);
195 vk->GetPhysicalDeviceCooperativeMatrixPropertiesKHR(
s->hwctx->phys_dev,
196 &
s->coop_mat_props_nb,
NULL);
198 if (
s->coop_mat_props_nb) {
200 sizeof(VkCooperativeMatrixPropertiesKHR));
201 for (
int i = 0;
i <
s->coop_mat_props_nb;
i++) {
202 s->coop_mat_props[
i] = (VkCooperativeMatrixPropertiesKHR) {
203 .sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR,
207 vk->GetPhysicalDeviceCooperativeMatrixPropertiesKHR(
s->hwctx->phys_dev,
208 &
s->coop_mat_props_nb,
218 for (
int i = 0;
i <
s->hwctx->nb_qf;
i++) {
219 if (
s->hwctx->qf[
i].flags & dev_family) {
220 *nb =
s->hwctx->qf[
i].num;
221 return s->hwctx->qf[
i].idx;
229 VkQueueFlagBits dev_family)
246 vk->WaitForFences(
s->hwctx->act_dev, 1, &e->
fence, VK_TRUE, UINT64_MAX);
247 vk->DestroyFence(
s->hwctx->act_dev, e->
fence,
s->hwctx->alloc);
279 vk->DestroyDescriptorPool(
s->hwctx->act_dev, sd->
desc_pool,
288 vk->FreeCommandBuffers(
s->hwctx->act_dev, pool->
cmd_buf_pool,
291 vk->DestroyCommandPool(
s->hwctx->act_dev, pool->
cmd_buf_pool,
s->hwctx->alloc);
293 vk->DestroyQueryPool(
s->hwctx->act_dev, pool->
query_pool,
s->hwctx->alloc);
302 int nb_queries, VkQueryType query_type,
int query_64bit,
303 const void *query_create_pnext)
310 VkCommandBufferAllocateInfo cbuf_create;
312 const VkQueryPoolVideoEncodeFeedbackCreateInfoKHR *ef =
NULL;
314 if (query_type == VK_QUERY_TYPE_VIDEO_ENCODE_FEEDBACK_KHR) {
316 VK_STRUCTURE_TYPE_QUERY_POOL_VIDEO_ENCODE_FEEDBACK_CREATE_INFO_KHR);
323 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
324 .flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT |
325 VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
330 if (
ret != VK_SUCCESS) {
345 cbuf_create = (VkCommandBufferAllocateInfo) {
346 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
347 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
349 .commandBufferCount = nb_contexts,
351 ret = vk->AllocateCommandBuffers(
s->hwctx->act_dev, &cbuf_create,
353 if (
ret != VK_SUCCESS) {
362 VkQueryPoolCreateInfo query_pool_info = {
363 .sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO,
364 .pNext = query_create_pnext,
365 .queryType = query_type,
366 .queryCount = nb_queries*nb_contexts,
368 ret = vk->CreateQueryPool(
s->hwctx->act_dev, &query_pool_info,
370 if (
ret != VK_SUCCESS) {
383 if (query_type == VK_QUERY_TYPE_VIDEO_ENCODE_FEEDBACK_KHR) {
384 int nb_results =
av_popcount(ef->encodeFeedbackFlags);
387 }
else if (query_type == VK_QUERY_TYPE_RESULT_STATUS_ONLY_KHR) {
414 VkFenceCreateInfo fence_create = {
415 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
416 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
425 ret = vk->CreateFence(
s->hwctx->act_dev, &fence_create,
s->hwctx->alloc,
427 if (
ret != VK_SUCCESS) {
458 void **
data, VkQueryResultFlagBits
flags)
462 VkQueryResultFlags qf =
flags & ~(VK_QUERY_RESULT_64_BIT |
463 VK_QUERY_RESULT_WITH_STATUS_BIT_KHR);
467 return VK_INCOMPLETE;
471 VK_QUERY_RESULT_64_BIT : 0x0;
473 VK_QUERY_RESULT_WITH_STATUS_BIT_KHR : 0x0;
478 return vk->GetQueryPoolResults(
s->hwctx->act_dev, pool->
query_pool,
492 if (vk->GetFenceStatus(
s->hwctx->act_dev, e->
fence) == VK_SUCCESS)
503 vk->WaitForFences(
s->hwctx->act_dev, 1, &e->
fence, VK_TRUE, UINT64_MAX);
514 VkCommandBufferBeginInfo cmd_start = {
515 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
516 .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
520 vk->WaitForFences(
s->hwctx->act_dev, 1, &e->
fence, VK_TRUE, UINT64_MAX);
524 vk->ResetFences(
s->hwctx->act_dev, 1, &e->
fence);
530 ret = vk->BeginCommandBuffer(e->
buf, &cmd_start);
531 if (
ret != VK_SUCCESS) {
556 vkfc->unlock_frame(hwfc, vkf);
582 for (
int i = 0;
i < nb_deps;
i++) {
594 #define ARR_REALLOC(str, arr, alloc_s, cnt) \
596 arr = av_fast_realloc(str->arr, alloc_s, (cnt + 1)*sizeof(*arr)); \
598 ff_vk_exec_discard_deps(s, e); \
599 return AVERROR(ENOMEM); \
616 vk->DestroySemaphore(
s->hwctx->act_dev, ts->
sem[
i],
s->hwctx->alloc);
622 VkSemaphore *sem,
int nb,
623 VkPipelineStageFlagBits2 stage,
635 for (
int i = 0;
i < nb;
i++) {
636 VkSemaphoreSubmitInfo *sem_sig;
640 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO,
649 buf_size =
sizeof(int) +
sizeof(VkSemaphore)*nb;
656 memcpy(ts->
sem, sem, nb*
sizeof(*sem));
672 for (
int i = 0;
i < nb;
i++) {
677 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO,
686 for (
int i = 0;
i < nb;
i++)
687 vk->DestroySemaphore(
s->hwctx->act_dev, sem[
i],
s->hwctx->alloc);
693 VkPipelineStageFlagBits2 wait_stage,
694 VkPipelineStageFlagBits2 signal_stage)
696 uint8_t *frame_locked;
697 uint8_t *frame_update;
699 VkImageLayout *layout_dst;
700 uint32_t *queue_family_dst;
701 VkAccessFlagBits *access_dst;
727 vkfc->lock_frame(hwfc, vkf);
732 for (
int i = 0;
i < nb_images;
i++) {
734 VkSemaphoreSubmitInfo *sem_sig;
735 uint64_t **sem_sig_val_dst;
742 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO,
743 .semaphore = vkf->sem[
i],
744 .value = vkf->sem_value[
i],
745 .stageMask = wait_stage,
749 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO,
750 .semaphore = vkf->sem[
i],
751 .value = vkf->sem_value[
i] + 1,
752 .stageMask = signal_stage,
763 VkImageMemoryBarrier2 *bar, uint32_t *nb_img_bar)
782 VkSemaphore *
dst, uint64_t *dst_val,
785 uint64_t **sem_sig_val_dst;
811 VkCommandBufferSubmitInfo cmd_buf_info = (VkCommandBufferSubmitInfo) {
812 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO,
813 .commandBuffer = e->
buf,
815 VkSubmitInfo2 submit_info = (VkSubmitInfo2) {
816 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2,
817 .pCommandBufferInfos = &cmd_buf_info,
818 .commandBufferInfoCount = 1,
821 .pSignalSemaphoreInfos = e->
sem_sig,
825 ret = vk->EndCommandBuffer(e->
buf);
826 if (
ret != VK_SUCCESS) {
833 s->hwctx->lock_queue(
s->device, e->
qf, e->
qi);
835 s->hwctx->unlock_queue(
s->device, e->
qf, e->
qi);
837 if (
ret != VK_SUCCESS) {
857 for (
int i = 0;
i < nb_images;
i++) {
863 vkfc->unlock_frame(hwfc, vkf);
874 VkMemoryPropertyFlagBits req_flags,
void *alloc_extension,
875 VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
881 VkMemoryAllocateInfo alloc_info = {
882 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
883 .pNext = alloc_extension,
887 if ((req_flags != UINT32_MAX) && req_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
888 req->size =
FFALIGN(req->size,
s->props.properties.limits.minMemoryMapAlignment);
890 alloc_info.allocationSize = req->size;
894 for (
int i = 0;
i <
s->mprops.memoryTypeCount;
i++) {
896 if (!(req->memoryTypeBits & (1 <<
i)))
900 if ((req_flags != UINT32_MAX) &&
901 ((
s->mprops.memoryTypes[
i].propertyFlags & req_flags) != req_flags))
915 alloc_info.memoryTypeIndex =
index;
917 ret = vk->AllocateMemory(
s->hwctx->act_dev, &alloc_info,
918 s->hwctx->alloc, mem);
919 if (
ret != VK_SUCCESS)
923 *mem_flags |=
s->mprops.memoryTypes[
index].propertyFlags;
929 void *pNext,
void *alloc_pNext,
930 VkBufferUsageFlags
usage, VkMemoryPropertyFlagBits
flags)
937 VkBufferCreateInfo buf_spawn = {
938 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
941 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
946 VkMemoryAllocateFlagsInfo alloc_flags = {
947 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO,
948 .flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT,
950 VkBufferMemoryRequirementsInfo2 req_desc = {
951 .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2,
953 VkMemoryDedicatedAllocateInfo ded_alloc = {
954 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
955 .pNext = alloc_pNext,
957 VkMemoryDedicatedRequirements ded_req = {
958 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
960 VkMemoryRequirements2 req = {
961 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
965 ret = vk->CreateBuffer(
s->hwctx->act_dev, &buf_spawn,
s->hwctx->alloc, &buf->
buf);
966 if (
ret != VK_SUCCESS) {
972 req_desc.buffer = buf->
buf;
974 vk->GetBufferMemoryRequirements2(
s->hwctx->act_dev, &req_desc, &req);
977 use_ded_mem = ded_req.prefersDedicatedAllocation |
978 ded_req.requiresDedicatedAllocation;
980 ded_alloc.buffer = buf->
buf;
981 ded_alloc.pNext = alloc_pNext;
982 alloc_pNext = &ded_alloc;
985 if (
usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) {
986 alloc_flags.pNext = alloc_pNext;
987 alloc_pNext = &alloc_flags;
995 ret = vk->BindBufferMemory(
s->hwctx->act_dev, buf->
buf, buf->
mem, 0);
996 if (
ret != VK_SUCCESS) {
1002 if (
usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) {
1003 VkBufferDeviceAddressInfo address_info = {
1004 .sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
1007 buf->
address = vk->GetBufferDeviceAddress(
s->hwctx->act_dev, &address_info);
1024 void *pNext,
void *alloc_pNext,
1025 VkBufferUsageFlags
usage, VkMemoryPropertyFlagBits
flags)
1051 int nb_buffers,
int invalidate)
1055 VkMappedMemoryRange inval_list[64];
1056 int inval_count = 0;
1058 for (
int i = 0;
i < nb_buffers;
i++) {
1060 ret = vk->MapMemory(
s->hwctx->act_dev, buf[
i]->
mem, 0,
1061 VK_WHOLE_SIZE, 0, &
dst);
1062 if (
ret != VK_SUCCESS) {
1073 for (
int i = 0;
i < nb_buffers;
i++) {
1074 const VkMappedMemoryRange ival_buf = {
1075 .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
1076 .memory = buf[
i]->
mem,
1077 .size = VK_WHOLE_SIZE,
1079 if (buf[
i]->
flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
1081 inval_list[inval_count++] = ival_buf;
1085 ret = vk->InvalidateMappedMemoryRanges(
s->hwctx->act_dev, inval_count,
1087 if (
ret != VK_SUCCESS) {
1103 VkMappedMemoryRange flush_list[64];
1104 int flush_count = 0;
1107 for (
int i = 0;
i < nb_buffers;
i++) {
1108 const VkMappedMemoryRange flush_buf = {
1109 .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
1110 .memory = buf[
i]->
mem,
1111 .size = VK_WHOLE_SIZE,
1113 if (buf[
i]->
flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
1115 flush_list[flush_count++] = flush_buf;
1120 ret = vk->FlushMappedMemoryRanges(
s->hwctx->act_dev, flush_count,
1122 if (
ret != VK_SUCCESS) {
1129 for (
int i = 0;
i < nb_buffers;
i++)
1130 vk->UnmapMemory(
s->hwctx->act_dev, buf[
i]->
mem);
1139 if (!buf || !
s->hwctx)
1144 if (buf->
buf != VK_NULL_HANDLE)
1145 vk->DestroyBuffer(
s->hwctx->act_dev, buf->
buf,
s->hwctx->alloc);
1146 if (buf->
mem != VK_NULL_HANDLE)
1147 vk->FreeMemory(
s->hwctx->act_dev, buf->
mem,
s->hwctx->alloc);
1173 void *create_pNext,
size_t size,
1174 VkMemoryPropertyFlagBits mem_props)
1192 data->stage = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
1193 data->access = VK_ACCESS_2_NONE;
1212 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
1224 VkShaderStageFlagBits stage)
1226 VkPushConstantRange *pc;
1235 memset(pc, 0,
sizeof(*pc));
1237 pc->stageFlags = stage;
1245 int unnorm_coords, VkFilter
filt)
1250 VkSamplerCreateInfo sampler_info = {
1251 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
1253 .minFilter = sampler_info.magFilter,
1254 .mipmapMode = unnorm_coords ? VK_SAMPLER_MIPMAP_MODE_NEAREST :
1255 VK_SAMPLER_MIPMAP_MODE_LINEAR,
1256 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1257 .addressModeV = sampler_info.addressModeU,
1258 .addressModeW = sampler_info.addressModeU,
1259 .anisotropyEnable = VK_FALSE,
1260 .compareOp = VK_COMPARE_OP_NEVER,
1261 .borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK,
1262 .unnormalizedCoordinates = unnorm_coords,
1265 ret = vk->CreateSampler(
s->hwctx->act_dev, &sampler_info,
1266 s->hwctx->alloc, sampler);
1267 if (
ret != VK_SUCCESS) {
1292 const int high =
desc->comp[0].depth > 8;
1293 return high ?
"rgba16f" :
"rgba8";
1308 vk->DestroyImageView(
s->hwctx->act_dev, iv->
views[
i],
s->hwctx->alloc);
1327 const size_t buf_size =
sizeof(int) + nb_planes*
sizeof(VkImageView);
1332 for (
int i = 0;
i < nb_planes;
i++) {
1333 VkImageAspectFlags plane_aspect[] = { VK_IMAGE_ASPECT_COLOR_BIT,
1334 VK_IMAGE_ASPECT_PLANE_0_BIT,
1335 VK_IMAGE_ASPECT_PLANE_1_BIT,
1336 VK_IMAGE_ASPECT_PLANE_2_BIT, };
1338 VkImageViewCreateInfo view_create_info = {
1339 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1341 .image = vkf->
img[
FFMIN(
i, nb_images - 1)],
1342 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1343 .format = rep_fmts[
i],
1345 .subresourceRange = {
1346 .aspectMask = plane_aspect[(nb_planes != nb_images) +
1347 i*(nb_planes != nb_images)],
1353 ret = vk->CreateImageView(
s->hwctx->act_dev, &view_create_info,
1354 s->hwctx->alloc, &iv->
views[
i]);
1355 if (
ret != VK_SUCCESS) {
1376 memcpy(views, iv->views, nb_planes*
sizeof(*views));
1381 for (
int i = 0;
i < iv->nb_views;
i++)
1382 vk->DestroyImageView(
s->hwctx->act_dev, iv->views[
i],
s->hwctx->alloc);
1388 AVFrame *pic, VkImageMemoryBarrier2 *bar,
int *nb_bar,
1389 VkPipelineStageFlags src_stage,
1390 VkPipelineStageFlags dst_stage,
1391 VkAccessFlagBits new_access,
1392 VkImageLayout new_layout,
1405 for (
int i = 0;
i < nb_images;
i++) {
1406 bar[*nb_bar] = (VkImageMemoryBarrier2) {
1407 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
1409 .srcStageMask = src_stage,
1410 .dstStageMask = dst_stage,
1412 .dstAccessMask = new_access,
1414 .newLayout = new_layout,
1416 .dstQueueFamilyIndex = new_qf,
1417 .image = vkf->
img[
i],
1418 .subresourceRange = (VkImageSubresourceRange) {
1419 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1431 VkPipelineStageFlags stage,
1432 const char *extensions[],
int nb_extensions,
1433 int lg_x,
int lg_y,
int lg_z,
1434 uint32_t required_subgroup_size)
1444 switch (shd->
stage) {
1445 case VK_SHADER_STAGE_ANY_HIT_BIT_KHR:
1446 case VK_SHADER_STAGE_CALLABLE_BIT_KHR:
1447 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR:
1448 case VK_SHADER_STAGE_INTERSECTION_BIT_KHR:
1449 case VK_SHADER_STAGE_MISS_BIT_KHR:
1450 case VK_SHADER_STAGE_RAYGEN_BIT_KHR:
1451 shd->
bind_point = VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR;
1453 case VK_SHADER_STAGE_COMPUTE_BIT:
1454 shd->
bind_point = VK_PIPELINE_BIND_POINT_COMPUTE;
1457 shd->
bind_point = VK_PIPELINE_BIND_POINT_GRAPHICS;
1461 if (required_subgroup_size) {
1462 shd->
subgroup_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO;
1463 shd->
subgroup_info.requiredSubgroupSize = required_subgroup_size;
1467 (stage == VK_SHADER_STAGE_TASK_BIT_EXT ||
1468 stage == VK_SHADER_STAGE_MESH_BIT_EXT) ?
1470 (shd->
bind_point == VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR) ?
1472 (shd->
bind_point == VK_PIPELINE_BIND_POINT_COMPUTE) ?
1473 "Compute" :
"Graphics",
1479 GLSLC(0, #define IS_WITHIN(v1, v2) ((v1.x < v2.x) && (v1.y < v2.y)) );
1481 GLSLC(0, #extension GL_EXT_scalar_block_layout : require );
1484 GLSLC(0, #extension GL_EXT_debug_printf : require );
1486 if (stage == VK_SHADER_STAGE_TASK_BIT_EXT ||
1487 stage == VK_SHADER_STAGE_MESH_BIT_EXT)
1488 GLSLC(0, #extension GL_EXT_mesh_shader : require );
1490 for (
int i = 0;
i < nb_extensions;
i++)
1491 GLSLF(0, #extension %
s : %
s ,extensions[
i],
"require");
1494 GLSLF(0,
layout (local_size_x = %
i, local_size_y = %
i, local_size_z = %
i) in;
1504 const char *p = shd->
src.str;
1505 const char *start = p;
1506 const size_t len = strlen(p);
1511 for (
int i = 0;
i <
len;
i++) {
1527 VkPipelineLayoutCreateInfo pipeline_layout_info;
1530 pipeline_layout_info = (VkPipelineLayoutCreateInfo) {
1531 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1538 ret = vk->CreatePipelineLayout(
s->hwctx->act_dev, &pipeline_layout_info,
1540 if (
ret != VK_SUCCESS) {
1550 VkShaderModule *
mod,
1551 uint8_t *spirv,
size_t spirv_len)
1556 VkShaderModuleCreateInfo shader_module_info = {
1557 .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
1560 .pCode = (
void *)spirv,
1561 .codeSize = spirv_len,
1564 ret = vk->CreateShaderModule(
s->hwctx->act_dev, &shader_module_info,
1565 s->hwctx->alloc,
mod);
1566 if (
ret != VK_SUCCESS) {
1576 VkShaderModule
mod,
const char *entrypoint)
1581 VkComputePipelineCreateInfo pipeline_create_info = {
1582 .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
1584 VK_PIPELINE_CREATE_DESCRIPTOR_BUFFER_BIT_EXT : 0x0,
1586 .stage = (VkPipelineShaderStageCreateInfo) {
1587 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
1590 .pName = entrypoint,
1592 VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT : 0x0,
1593 .stage = shd->
stage,
1598 ret = vk->CreateComputePipelines(
s->hwctx->act_dev, VK_NULL_HANDLE, 1,
1599 &pipeline_create_info,
1601 if (
ret != VK_SUCCESS) {
1611 uint8_t *spirv,
size_t spirv_len,
1612 const char *entrypoint)
1616 size_t shader_size = 0;
1618 VkShaderCreateInfoEXT shader_obj_create = {
1619 .sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO_EXT,
1621 VK_SHADER_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT : 0x0,
1622 .stage = shd->
stage,
1624 .codeType = VK_SHADER_CODE_TYPE_SPIRV_EXT,
1626 .codeSize = spirv_len,
1627 .pName = entrypoint,
1632 .pSpecializationInfo =
NULL,
1635 ret = vk->CreateShadersEXT(
s->hwctx->act_dev, 1, &shader_obj_create,
1636 s->hwctx->alloc, &shd->
object);
1637 if (
ret != VK_SUCCESS) {
1643 if (vk->GetShaderBinaryDataEXT(
s->hwctx->act_dev, shd->
object,
1644 &shader_size,
NULL) == VK_SUCCESS)
1646 shd->
name, shader_size, spirv_len);
1662 int has_singular = 0;
1676 VkDescriptorSetLayoutCreateInfo desc_layout_create = {
1677 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1678 .bindingCount =
set->nb_bindings,
1679 .pBindings =
set->binding,
1681 VK_DESCRIPTOR_SET_LAYOUT_CREATE_DESCRIPTOR_BUFFER_BIT_EXT :
1683 VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR :
1687 ret = vk->CreateDescriptorSetLayout(
s->hwctx->act_dev,
1688 &desc_layout_create,
1691 if (
ret != VK_SUCCESS) {
1698 vk->GetDescriptorSetLayoutSizeEXT(
s->hwctx->act_dev, shd->
desc_layout[
i],
1702 s->desc_buf_props.descriptorBufferOffsetAlignment);
1704 for (
int j = 0; j <
set->nb_bindings; j++)
1705 vk->GetDescriptorSetLayoutBindingOffsetEXT(
s->hwctx->act_dev,
1708 &
set->binding_offset[j]);
1716 uint8_t *spirv,
size_t spirv_len,
1717 const char *entrypoint)
1751 case VK_PIPELINE_BIND_POINT_COMPUTE:
1761 vk->DestroyShaderModule(
s->hwctx->act_dev,
mod,
s->hwctx->alloc);
1777 [VK_DESCRIPTOR_TYPE_SAMPLER] = {
sizeof(VkDescriptorImageInfo),
"sampler", 1, 0, 0, 0, },
1778 [VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE] = {
sizeof(VkDescriptorImageInfo),
"texture", 1, 0, 1, 0, },
1779 [VK_DESCRIPTOR_TYPE_STORAGE_IMAGE] = {
sizeof(VkDescriptorImageInfo),
"image", 1, 1, 1, 0, },
1780 [VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT] = {
sizeof(VkDescriptorImageInfo),
"subpassInput", 1, 0, 0, 0, },
1781 [VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER] = {
sizeof(VkDescriptorImageInfo),
"sampler", 1, 0, 1, 0, },
1782 [VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER] = {
sizeof(VkDescriptorBufferInfo),
NULL, 1, 0, 0, 1, },
1783 [VK_DESCRIPTOR_TYPE_STORAGE_BUFFER] = {
sizeof(VkDescriptorBufferInfo),
"buffer", 0, 1, 0, 1, },
1784 [VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC] = {
sizeof(VkDescriptorBufferInfo),
NULL, 1, 0, 0, 1, },
1785 [VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC] = {
sizeof(VkDescriptorBufferInfo),
"buffer", 0, 1, 0, 1, },
1786 [VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER] = {
sizeof(VkBufferView),
"samplerBuffer", 1, 0, 0, 0, },
1787 [VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER] = {
sizeof(VkBufferView),
"imageBuffer", 1, 0, 0, 0, },
1792 int singular,
int print_to_shader_only)
1794 int has_sampler = 0;
1797 if (print_to_shader_only)
1809 memset(
set, 0,
sizeof(*
set));
1816 if (!
set->binding_offset) {
1821 for (
int i = 0;
i < nb;
i++) {
1822 set->binding[
i].binding =
i;
1823 set->binding[
i].descriptorType =
desc[
i].type;
1825 set->binding[
i].stageFlags =
desc[
i].stages;
1826 set->binding[
i].pImmutableSamplers =
desc[
i].samplers;
1828 if (
desc[
i].
type == VK_DESCRIPTOR_TYPE_SAMPLER ||
1829 desc[
i].
type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
1833 set->usage = VK_BUFFER_USAGE_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT |
1834 VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
1836 set->usage |= VK_BUFFER_USAGE_SAMPLER_DESCRIPTOR_BUFFER_BIT_EXT;
1839 for (
int i = 0;
i < nb;
i++) {
1841 VkDescriptorPoolSize *desc_pool_size;
1847 sizeof(*desc_pool_size),
1849 if (!desc_pool_size)
1854 memset(&desc_pool_size[j], 0,
sizeof(VkDescriptorPoolSize));
1861 set->singular = singular;
1862 set->nb_bindings = nb;
1867 for (
int i = 0;
i < nb;
i++) {
1871 if (
desc[
i].mem_layout)
1891 else if (
desc[
i].elems > 0)
1919 memset(sd, 0,
sizeof(*sd));
1939 set->aligned_size*nb,
1941 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
1942 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1943 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
1951 sd->
desc_bind[
i] = (VkDescriptorBufferBindingInfoEXT) {
1952 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_BUFFER_BINDING_INFO_EXT,
1953 .usage =
set->usage,
1960 VkDescriptorSetLayout *tmp_layouts;
1961 VkDescriptorSetAllocateInfo set_alloc_info;
1962 VkDescriptorPoolCreateInfo pool_create_info;
1967 pool_create_info = (VkDescriptorPoolCreateInfo) {
1968 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
1975 ret = vk->CreateDescriptorPool(
s->hwctx->act_dev, &pool_create_info,
1977 if (
ret != VK_SUCCESS) {
1983 tmp_layouts =
av_malloc_array(pool_create_info.maxSets,
sizeof(*tmp_layouts));
1992 set_alloc_info = (VkDescriptorSetAllocateInfo) {
1993 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
1995 .pSetLayouts = tmp_layouts,
1996 .descriptorSetCount = pool_create_info.maxSets,
2000 sizeof(*tmp_layouts));
2005 ret = vk->AllocateDescriptorSets(
s->hwctx->act_dev, &set_alloc_info,
2008 if (
ret != VK_SUCCESS) {
2030 int bind_idx,
int array_idx,
2031 VkDescriptorGetInfoEXT *desc_get_info,
2042 array_idx*desc_size;
2044 vk->GetDescriptorEXT(
s->hwctx->act_dev, desc_get_info, desc_size,
desc);
2049 VkWriteDescriptorSet *write_info)
2058 vk->UpdateDescriptorSets(
s->hwctx->act_dev, 1, write_info, 0,
NULL);
2062 vk->CmdPushDescriptorSetKHR(e->
buf,
2069 vk->UpdateDescriptorSets(
s->hwctx->act_dev, 1, write_info, 0,
NULL);
2076 VkImageView view, VkImageLayout
layout,
2082 VkDescriptorGetInfoEXT desc_get_info = {
2083 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_GET_INFO_EXT,
2084 .type = desc_set->
binding[bind].descriptorType,
2086 VkDescriptorImageInfo desc_img_info = {
2093 switch (desc_get_info.type) {
2094 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2095 desc_get_info.
data.pSampledImage = &desc_img_info;
2096 desc_size =
s->desc_buf_props.sampledImageDescriptorSize;
2098 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
2099 desc_get_info.data.pStorageImage = &desc_img_info;
2100 desc_size =
s->desc_buf_props.storageImageDescriptorSize;
2102 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2103 desc_get_info.data.pInputAttachmentImage = &desc_img_info;
2104 desc_size =
s->desc_buf_props.inputAttachmentDescriptorSize;
2106 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
2107 desc_get_info.data.pCombinedImageSampler = &desc_img_info;
2108 desc_size =
s->desc_buf_props.combinedImageSamplerDescriptorSize;
2112 set, bind, desc_get_info.type);
2118 &desc_get_info, desc_size);
2120 VkDescriptorImageInfo desc_pool_write_info_img = {
2125 VkWriteDescriptorSet desc_pool_write_info = {
2126 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
2128 .descriptorCount = 1,
2129 .dstArrayElement = offs,
2130 .descriptorType = desc_set->
binding[bind].descriptorType,
2131 .pImageInfo = &desc_pool_write_info_img,
2141 int set,
int bind,
int elem,
2148 VkDescriptorGetInfoEXT desc_get_info = {
2149 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_GET_INFO_EXT,
2150 .type = desc_set->
binding[bind].descriptorType,
2152 VkDescriptorAddressInfoEXT desc_buf_info = {
2153 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_ADDRESS_INFO_EXT,
2160 switch (desc_get_info.type) {
2161 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2162 desc_get_info.data.pUniformBuffer = &desc_buf_info;
2163 desc_size =
s->desc_buf_props.uniformBufferDescriptorSize;
2165 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2166 desc_get_info.data.pStorageBuffer = &desc_buf_info;
2167 desc_size =
s->desc_buf_props.storageBufferDescriptorSize;
2169 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2170 desc_get_info.data.pUniformTexelBuffer = &desc_buf_info;
2171 desc_size =
s->desc_buf_props.uniformTexelBufferDescriptorSize;
2173 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2174 desc_get_info.data.pStorageTexelBuffer = &desc_buf_info;
2175 desc_size =
s->desc_buf_props.storageTexelBufferDescriptorSize;
2179 set, bind, desc_get_info.type);
2186 VkDescriptorBufferInfo desc_pool_write_info_buf = {
2191 VkWriteDescriptorSet desc_pool_write_info = {
2192 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
2194 .descriptorCount = 1,
2195 .dstArrayElement = elem,
2196 .descriptorType = desc_set->
binding[bind].descriptorType,
2197 .pBufferInfo = &desc_pool_write_info_buf,
2207 VkImageView *views,
int set,
int binding,
2208 VkImageLayout
layout, VkSampler sampler)
2213 for (
int i = 0;
i < nb_planes;
i++)
2220 VkShaderStageFlagBits stage,
2236 VkShaderStageFlagBits stages = shd->
stage;
2237 vk->CmdBindShadersEXT(e->
buf, 1, &stages, &shd->
object);
2269 if (shd->shader.module)
2270 vk->DestroyShaderModule(
s->hwctx->act_dev, shd->shader.module,
2275 vk->DestroyShaderEXT(
s->hwctx->act_dev, shd->
object,
s->hwctx->alloc);
2277 vk->DestroyPipeline(
s->hwctx->act_dev, shd->
pipeline,
s->hwctx->alloc);
2290 vk->DestroyDescriptorSetLayout(
s->hwctx->act_dev, shd->
desc_layout[
i],
2317 static const AVClass vulkan_context_class = {
2323 memset(
s, 0,
sizeof(*
s));
2324 s->log_parent = log_parent;
2325 s->class = &vulkan_context_class;
2333 s->hwfc =
s->frames->hwctx;
2335 device_ref =
s->frames->device_ref;
2339 if (!
s->device_ref) {
2345 s->hwctx =
s->device->hwctx;
2348 s->hwctx->nb_enabled_dev_extensions);
2350 s->hwctx->nb_enabled_inst_extensions);