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);
266 vk->FreeCommandBuffers(
s->hwctx->act_dev, pool->
cmd_buf_pool,
269 vk->DestroyCommandPool(
s->hwctx->act_dev, pool->
cmd_buf_pool,
s->hwctx->alloc);
271 vk->DestroyQueryPool(
s->hwctx->act_dev, pool->
query_pool,
s->hwctx->alloc);
280 int nb_queries, VkQueryType query_type,
int query_64bit,
281 const void *query_create_pnext)
288 VkCommandBufferAllocateInfo cbuf_create;
290 const VkQueryPoolVideoEncodeFeedbackCreateInfoKHR *ef =
NULL;
292 if (query_type == VK_QUERY_TYPE_VIDEO_ENCODE_FEEDBACK_KHR) {
294 VK_STRUCTURE_TYPE_QUERY_POOL_VIDEO_ENCODE_FEEDBACK_CREATE_INFO_KHR);
301 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
302 .flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT |
303 VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
308 if (
ret != VK_SUCCESS) {
323 cbuf_create = (VkCommandBufferAllocateInfo) {
324 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
325 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
327 .commandBufferCount = nb_contexts,
329 ret = vk->AllocateCommandBuffers(
s->hwctx->act_dev, &cbuf_create,
331 if (
ret != VK_SUCCESS) {
340 VkQueryPoolCreateInfo query_pool_info = {
341 .sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO,
342 .pNext = query_create_pnext,
343 .queryType = query_type,
344 .queryCount = nb_queries*nb_contexts,
346 ret = vk->CreateQueryPool(
s->hwctx->act_dev, &query_pool_info,
348 if (
ret != VK_SUCCESS) {
361 if (query_type == VK_QUERY_TYPE_VIDEO_ENCODE_FEEDBACK_KHR) {
362 int nb_results =
av_popcount(ef->encodeFeedbackFlags);
365 }
else if (query_type == VK_QUERY_TYPE_RESULT_STATUS_ONLY_KHR) {
392 VkFenceCreateInfo fence_create = {
393 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
394 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
403 ret = vk->CreateFence(
s->hwctx->act_dev, &fence_create,
s->hwctx->alloc,
405 if (
ret != VK_SUCCESS) {
436 void **
data, VkQueryResultFlagBits
flags)
440 VkQueryResultFlags qf =
flags & ~(VK_QUERY_RESULT_64_BIT |
441 VK_QUERY_RESULT_WITH_STATUS_BIT_KHR);
445 return VK_INCOMPLETE;
449 VK_QUERY_RESULT_64_BIT : 0x0;
451 VK_QUERY_RESULT_WITH_STATUS_BIT_KHR : 0x0;
456 return vk->GetQueryPoolResults(
s->hwctx->act_dev, pool->
query_pool,
465 uint32_t idx = pool->
idx++;
474 vk->WaitForFences(
s->hwctx->act_dev, 1, &e->
fence, VK_TRUE, UINT64_MAX);
485 VkCommandBufferBeginInfo cmd_start = {
486 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
487 .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
491 vk->WaitForFences(
s->hwctx->act_dev, 1, &e->
fence, VK_TRUE, UINT64_MAX);
495 vk->ResetFences(
s->hwctx->act_dev, 1, &e->
fence);
501 ret = vk->BeginCommandBuffer(e->
buf, &cmd_start);
502 if (
ret != VK_SUCCESS) {
527 vkfc->unlock_frame(hwfc, vkf);
553 for (
int i = 0;
i < nb_deps;
i++) {
565 #define ARR_REALLOC(str, arr, alloc_s, cnt) \
567 arr = av_fast_realloc(str->arr, alloc_s, (cnt + 1)*sizeof(*arr)); \
569 ff_vk_exec_discard_deps(s, e); \
570 return AVERROR(ENOMEM); \
587 vk->DestroySemaphore(
s->hwctx->act_dev, ts->
sem[
i],
s->hwctx->alloc);
593 VkSemaphore *sem,
int nb,
594 VkPipelineStageFlagBits2 stage,
606 for (
int i = 0;
i < nb;
i++) {
607 VkSemaphoreSubmitInfo *sem_sig;
611 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO,
620 buf_size =
sizeof(int) +
sizeof(VkSemaphore)*nb;
627 memcpy(ts->
sem, sem, nb*
sizeof(*sem));
643 for (
int i = 0;
i < nb;
i++) {
648 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO,
657 for (
int i = 0;
i < nb;
i++)
658 vk->DestroySemaphore(
s->hwctx->act_dev, sem[
i],
s->hwctx->alloc);
664 VkPipelineStageFlagBits2 wait_stage,
665 VkPipelineStageFlagBits2 signal_stage)
667 uint8_t *frame_locked;
668 uint8_t *frame_update;
670 VkImageLayout *layout_dst;
671 uint32_t *queue_family_dst;
672 VkAccessFlagBits *access_dst;
698 vkfc->lock_frame(hwfc, vkf);
703 for (
int i = 0;
i < nb_images;
i++) {
705 VkSemaphoreSubmitInfo *sem_sig;
706 uint64_t **sem_sig_val_dst;
713 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO,
714 .semaphore = vkf->sem[
i],
715 .value = vkf->sem_value[
i],
716 .stageMask = wait_stage,
720 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO,
721 .semaphore = vkf->sem[
i],
722 .value = vkf->sem_value[
i] + 1,
723 .stageMask = signal_stage,
734 VkImageMemoryBarrier2 *bar, uint32_t *nb_img_bar)
753 VkSemaphore *
dst, uint64_t *dst_val,
756 uint64_t **sem_sig_val_dst;
782 VkCommandBufferSubmitInfo cmd_buf_info = (VkCommandBufferSubmitInfo) {
783 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO,
784 .commandBuffer = e->
buf,
786 VkSubmitInfo2 submit_info = (VkSubmitInfo2) {
787 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2,
788 .pCommandBufferInfos = &cmd_buf_info,
789 .commandBufferInfoCount = 1,
792 .pSignalSemaphoreInfos = e->
sem_sig,
796 ret = vk->EndCommandBuffer(e->
buf);
797 if (
ret != VK_SUCCESS) {
804 s->hwctx->lock_queue(
s->device, e->
qf, e->
qi);
806 s->hwctx->unlock_queue(
s->device, e->
qf, e->
qi);
808 if (
ret != VK_SUCCESS) {
828 for (
int i = 0;
i < nb_images;
i++) {
834 vkfc->unlock_frame(hwfc, vkf);
845 VkMemoryPropertyFlagBits req_flags,
void *alloc_extension,
846 VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
852 VkMemoryAllocateInfo alloc_info = {
853 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
854 .pNext = alloc_extension,
858 if ((req_flags != UINT32_MAX) && req_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
859 req->size =
FFALIGN(req->size,
s->props.properties.limits.minMemoryMapAlignment);
861 alloc_info.allocationSize = req->size;
865 for (
int i = 0;
i <
s->mprops.memoryTypeCount;
i++) {
867 if (!(req->memoryTypeBits & (1 <<
i)))
871 if ((req_flags != UINT32_MAX) &&
872 ((
s->mprops.memoryTypes[
i].propertyFlags & req_flags) != req_flags))
886 alloc_info.memoryTypeIndex =
index;
888 ret = vk->AllocateMemory(
s->hwctx->act_dev, &alloc_info,
889 s->hwctx->alloc, mem);
890 if (
ret != VK_SUCCESS)
894 *mem_flags |=
s->mprops.memoryTypes[
index].propertyFlags;
900 void *pNext,
void *alloc_pNext,
901 VkBufferUsageFlags
usage, VkMemoryPropertyFlagBits
flags)
908 VkBufferCreateInfo buf_spawn = {
909 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
912 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
917 VkMemoryAllocateFlagsInfo alloc_flags = {
918 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO,
919 .flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT,
921 VkBufferMemoryRequirementsInfo2 req_desc = {
922 .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2,
924 VkMemoryDedicatedAllocateInfo ded_alloc = {
925 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
926 .pNext = alloc_pNext,
928 VkMemoryDedicatedRequirements ded_req = {
929 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
931 VkMemoryRequirements2 req = {
932 .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
936 ret = vk->CreateBuffer(
s->hwctx->act_dev, &buf_spawn,
s->hwctx->alloc, &buf->
buf);
937 if (
ret != VK_SUCCESS) {
943 req_desc.buffer = buf->
buf;
945 vk->GetBufferMemoryRequirements2(
s->hwctx->act_dev, &req_desc, &req);
948 use_ded_mem = ded_req.prefersDedicatedAllocation |
949 ded_req.requiresDedicatedAllocation;
951 ded_alloc.buffer = buf->
buf;
952 ded_alloc.pNext = alloc_pNext;
953 alloc_pNext = &ded_alloc;
956 if (
usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) {
957 alloc_flags.pNext = alloc_pNext;
958 alloc_pNext = &alloc_flags;
966 ret = vk->BindBufferMemory(
s->hwctx->act_dev, buf->
buf, buf->
mem, 0);
967 if (
ret != VK_SUCCESS) {
973 if (
usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) {
974 VkBufferDeviceAddressInfo address_info = {
975 .sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
978 buf->
address = vk->GetBufferDeviceAddress(
s->hwctx->act_dev, &address_info);
995 void *pNext,
void *alloc_pNext,
996 VkBufferUsageFlags
usage, VkMemoryPropertyFlagBits
flags)
1022 int nb_buffers,
int invalidate)
1026 VkMappedMemoryRange inval_list[64];
1027 int inval_count = 0;
1029 for (
int i = 0;
i < nb_buffers;
i++) {
1031 ret = vk->MapMemory(
s->hwctx->act_dev, buf[
i]->
mem, 0,
1032 VK_WHOLE_SIZE, 0, &
dst);
1033 if (
ret != VK_SUCCESS) {
1044 for (
int i = 0;
i < nb_buffers;
i++) {
1045 const VkMappedMemoryRange ival_buf = {
1046 .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
1047 .memory = buf[
i]->
mem,
1048 .size = VK_WHOLE_SIZE,
1050 if (buf[
i]->
flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
1052 inval_list[inval_count++] = ival_buf;
1056 ret = vk->InvalidateMappedMemoryRanges(
s->hwctx->act_dev, inval_count,
1058 if (
ret != VK_SUCCESS) {
1074 VkMappedMemoryRange flush_list[64];
1075 int flush_count = 0;
1078 for (
int i = 0;
i < nb_buffers;
i++) {
1079 const VkMappedMemoryRange flush_buf = {
1080 .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
1081 .memory = buf[
i]->
mem,
1082 .size = VK_WHOLE_SIZE,
1084 if (buf[
i]->
flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
1086 flush_list[flush_count++] = flush_buf;
1091 ret = vk->FlushMappedMemoryRanges(
s->hwctx->act_dev, flush_count,
1093 if (
ret != VK_SUCCESS) {
1100 for (
int i = 0;
i < nb_buffers;
i++)
1101 vk->UnmapMemory(
s->hwctx->act_dev, buf[
i]->
mem);
1110 if (!buf || !
s->hwctx)
1115 if (buf->
buf != VK_NULL_HANDLE)
1116 vk->DestroyBuffer(
s->hwctx->act_dev, buf->
buf,
s->hwctx->alloc);
1117 if (buf->
mem != VK_NULL_HANDLE)
1118 vk->FreeMemory(
s->hwctx->act_dev, buf->
mem,
s->hwctx->alloc);
1144 void *create_pNext,
size_t size,
1145 VkMemoryPropertyFlagBits mem_props)
1163 data->stage = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
1164 data->access = VK_ACCESS_2_NONE;
1183 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
1195 VkShaderStageFlagBits stage)
1197 VkPushConstantRange *pc;
1205 memset(pc, 0,
sizeof(*pc));
1207 pc->stageFlags = stage;
1215 int unnorm_coords, VkFilter
filt)
1220 VkSamplerCreateInfo sampler_info = {
1221 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
1223 .minFilter = sampler_info.magFilter,
1224 .mipmapMode = unnorm_coords ? VK_SAMPLER_MIPMAP_MODE_NEAREST :
1225 VK_SAMPLER_MIPMAP_MODE_LINEAR,
1226 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1227 .addressModeV = sampler_info.addressModeU,
1228 .addressModeW = sampler_info.addressModeU,
1229 .anisotropyEnable = VK_FALSE,
1230 .compareOp = VK_COMPARE_OP_NEVER,
1231 .borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK,
1232 .unnormalizedCoordinates = unnorm_coords,
1235 ret = vk->CreateSampler(
s->hwctx->act_dev, &sampler_info,
1236 s->hwctx->alloc, sampler);
1237 if (
ret != VK_SUCCESS) {
1262 const int high =
desc->comp[0].depth > 8;
1263 return high ?
"rgba16f" :
"rgba8";
1278 vk->DestroyImageView(
s->hwctx->act_dev, iv->
views[
i],
s->hwctx->alloc);
1297 const size_t buf_size =
sizeof(int) + nb_planes*
sizeof(VkImageView);
1302 for (
int i = 0;
i < nb_planes;
i++) {
1303 VkImageAspectFlags plane_aspect[] = { VK_IMAGE_ASPECT_COLOR_BIT,
1304 VK_IMAGE_ASPECT_PLANE_0_BIT,
1305 VK_IMAGE_ASPECT_PLANE_1_BIT,
1306 VK_IMAGE_ASPECT_PLANE_2_BIT, };
1308 VkImageViewCreateInfo view_create_info = {
1309 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1311 .image = vkf->
img[
FFMIN(
i, nb_images - 1)],
1312 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1313 .format = rep_fmts[
i],
1315 .subresourceRange = {
1316 .aspectMask = plane_aspect[(nb_planes != nb_images) +
1317 i*(nb_planes != nb_images)],
1323 ret = vk->CreateImageView(
s->hwctx->act_dev, &view_create_info,
1324 s->hwctx->alloc, &iv->
views[
i]);
1325 if (
ret != VK_SUCCESS) {
1346 memcpy(views, iv->views, nb_planes*
sizeof(*views));
1351 for (
int i = 0;
i < iv->nb_views;
i++)
1352 vk->DestroyImageView(
s->hwctx->act_dev, iv->views[
i],
s->hwctx->alloc);
1358 AVFrame *pic, VkImageMemoryBarrier2 *bar,
int *nb_bar,
1359 VkPipelineStageFlags src_stage,
1360 VkPipelineStageFlags dst_stage,
1361 VkAccessFlagBits new_access,
1362 VkImageLayout new_layout,
1375 for (
int i = 0;
i < nb_images;
i++) {
1376 bar[*nb_bar] = (VkImageMemoryBarrier2) {
1377 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
1379 .srcStageMask = src_stage,
1380 .dstStageMask = dst_stage,
1382 .dstAccessMask = new_access,
1384 .newLayout = new_layout,
1386 .dstQueueFamilyIndex = new_qf,
1387 .image = vkf->
img[
i],
1388 .subresourceRange = (VkImageSubresourceRange) {
1389 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1401 VkShaderStageFlags stage, uint32_t required_subgroup_size)
1405 shd->
shader.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1406 shd->
shader.stage = stage;
1408 if (required_subgroup_size) {
1409 shd->
shader.flags |= VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT;
1411 shd->
subgroup_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO;
1412 shd->
subgroup_info.requiredSubgroupSize = required_subgroup_size;
1418 GLSLC(0, #define IS_WITHIN(v1, v2) ((v1.x < v2.x) && (v1.y < v2.y)) );
1420 GLSLC(0, #extension GL_EXT_buffer_reference : require );
1421 GLSLC(0, #extension GL_EXT_buffer_reference2 : require );
1433 "local_size_y = %i, local_size_z = %i) in;\n\n",
1440 const char *p = shd->
src.str;
1441 const char *start = p;
1442 const size_t len = strlen(p);
1447 for (
int i = 0;
i <
len;
i++) {
1465 vk->DestroyShaderModule(
s->hwctx->act_dev, shd->
shader.module,
s->hwctx->alloc);
1469 uint8_t *spirv,
size_t spirv_size,
const char *entrypoint)
1473 VkShaderModuleCreateInfo shader_create;
1475 shd->
shader.pName = entrypoint;
1478 shd->
name, spirv_size);
1480 shader_create.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1481 shader_create.pNext =
NULL;
1482 shader_create.codeSize = spirv_size;
1483 shader_create.flags = 0;
1484 shader_create.pCode = (
void *)spirv;
1486 ret = vk->CreateShaderModule(
s->hwctx->act_dev, &shader_create,
NULL,
1488 if (
ret != VK_SUCCESS) {
1505 [VK_DESCRIPTOR_TYPE_SAMPLER] = {
sizeof(VkDescriptorImageInfo),
"sampler", 1, 0, 0, 0, },
1506 [VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE] = {
sizeof(VkDescriptorImageInfo),
"texture", 1, 0, 1, 0, },
1507 [VK_DESCRIPTOR_TYPE_STORAGE_IMAGE] = {
sizeof(VkDescriptorImageInfo),
"image", 1, 1, 1, 0, },
1508 [VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT] = {
sizeof(VkDescriptorImageInfo),
"subpassInput", 1, 0, 0, 0, },
1509 [VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER] = {
sizeof(VkDescriptorImageInfo),
"sampler", 1, 0, 1, 0, },
1510 [VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER] = {
sizeof(VkDescriptorBufferInfo),
NULL, 1, 0, 0, 1, },
1511 [VK_DESCRIPTOR_TYPE_STORAGE_BUFFER] = {
sizeof(VkDescriptorBufferInfo),
"buffer", 0, 1, 0, 1, },
1512 [VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC] = {
sizeof(VkDescriptorBufferInfo),
NULL, 1, 0, 0, 1, },
1513 [VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC] = {
sizeof(VkDescriptorBufferInfo),
"buffer", 0, 1, 0, 1, },
1514 [VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER] = {
sizeof(VkBufferView),
"samplerBuffer", 1, 0, 0, 0, },
1515 [VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER] = {
sizeof(VkBufferView),
"imageBuffer", 1, 0, 0, 0, },
1521 int singular,
int print_to_shader_only)
1523 int has_sampler = 0;
1526 if (print_to_shader_only)
1537 memset(
set, 0,
sizeof(*
set));
1544 if (!
set->binding_offset) {
1549 for (
int i = 0;
i < nb;
i++) {
1550 set->binding[
i].binding =
i;
1551 set->binding[
i].descriptorType =
desc[
i].type;
1553 set->binding[
i].stageFlags =
desc[
i].stages;
1554 set->binding[
i].pImmutableSamplers =
desc[
i].samplers;
1556 if (
desc[
i].
type == VK_DESCRIPTOR_TYPE_SAMPLER ||
1557 desc[
i].
type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
1561 set->usage = VK_BUFFER_USAGE_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT |
1562 VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
1564 set->usage |= VK_BUFFER_USAGE_SAMPLER_DESCRIPTOR_BUFFER_BIT_EXT;
1567 for (
int i = 0;
i < nb;
i++) {
1569 VkDescriptorPoolSize *desc_pool_size;
1575 sizeof(*desc_pool_size),
1577 if (!desc_pool_size)
1582 memset(&desc_pool_size[j], 0,
sizeof(VkDescriptorPoolSize));
1589 set->singular = singular;
1590 set->nb_bindings = nb;
1595 for (
int i = 0;
i < nb;
i++) {
1599 if (
desc[
i].mem_layout)
1619 else if (
desc[
i].elems > 0)
1654 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
1655 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1656 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
1664 pl->
desc_bind[
i] = (VkDescriptorBufferBindingInfoEXT) {
1665 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_BUFFER_BINDING_INFO_EXT,
1666 .usage =
set->usage,
1667 .address =
set->buf.address,
1675 VkDescriptorSetLayout *tmp_layouts;
1676 VkDescriptorSetAllocateInfo set_alloc_info;
1677 VkDescriptorPoolCreateInfo pool_create_info;
1682 pool_create_info = (VkDescriptorPoolCreateInfo) {
1683 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
1690 ret = vk->CreateDescriptorPool(
s->hwctx->act_dev, &pool_create_info,
1692 if (
ret != VK_SUCCESS) {
1698 tmp_layouts =
av_malloc_array(pool_create_info.maxSets,
sizeof(*tmp_layouts));
1707 set_alloc_info = (VkDescriptorSetAllocateInfo) {
1708 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
1710 .pSetLayouts = tmp_layouts,
1711 .descriptorSetCount = pool_create_info.maxSets,
1715 sizeof(*tmp_layouts));
1720 ret = vk->AllocateDescriptorSets(
s->hwctx->act_dev, &set_alloc_info,
1723 if (
ret != VK_SUCCESS) {
1738 int bind_idx,
int array_idx,
1739 VkDescriptorGetInfoEXT *desc_get_info,
1743 const size_t exec_offset =
set->singular ? 0 :
set->aligned_size*e->
idx;
1746 set->binding_offset[bind_idx] +
1747 array_idx*desc_size;
1749 vk->GetDescriptorEXT(
s->hwctx->act_dev, desc_get_info, desc_size,
desc);
1756 VkWriteDescriptorSet *write_info)
1762 vk->UpdateDescriptorSets(
s->hwctx->act_dev, 1, write_info, 0,
NULL);
1766 vk->CmdPushDescriptorSetKHR(e->
buf,
1773 vk->UpdateDescriptorSets(
s->hwctx->act_dev, 1, write_info, 0,
NULL);
1780 VkImageView view, VkImageLayout
layout,
1786 VkDescriptorGetInfoEXT desc_get_info = {
1787 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_GET_INFO_EXT,
1788 .type = desc_set->
binding[bind].descriptorType,
1790 VkDescriptorImageInfo desc_img_info = {
1797 switch (desc_get_info.type) {
1798 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1799 desc_get_info.
data.pSampledImage = &desc_img_info;
1800 desc_size =
s->desc_buf_props.sampledImageDescriptorSize;
1802 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1803 desc_get_info.data.pStorageImage = &desc_img_info;
1804 desc_size =
s->desc_buf_props.storageImageDescriptorSize;
1806 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1807 desc_get_info.data.pInputAttachmentImage = &desc_img_info;
1808 desc_size =
s->desc_buf_props.inputAttachmentDescriptorSize;
1810 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1811 desc_get_info.data.pCombinedImageSampler = &desc_img_info;
1812 desc_size =
s->desc_buf_props.combinedImageSamplerDescriptorSize;
1816 set, bind, desc_get_info.type);
1822 &desc_get_info, desc_size);
1824 VkDescriptorImageInfo desc_pool_write_info_img = {
1829 VkWriteDescriptorSet desc_pool_write_info = {
1830 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
1832 .descriptorCount = 1,
1833 .dstArrayElement = offs,
1834 .descriptorType = desc_set->
binding[bind].descriptorType,
1835 .pImageInfo = &desc_pool_write_info_img,
1851 VkDescriptorGetInfoEXT desc_get_info = {
1852 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_GET_INFO_EXT,
1853 .type = desc_set->
binding[bind].descriptorType,
1855 VkDescriptorAddressInfoEXT desc_buf_info = {
1856 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_ADDRESS_INFO_EXT,
1863 switch (desc_get_info.type) {
1864 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1865 desc_get_info.data.pUniformBuffer = &desc_buf_info;
1866 desc_size =
s->desc_buf_props.uniformBufferDescriptorSize;
1868 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1869 desc_get_info.data.pStorageBuffer = &desc_buf_info;
1870 desc_size =
s->desc_buf_props.storageBufferDescriptorSize;
1872 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1873 desc_get_info.data.pUniformTexelBuffer = &desc_buf_info;
1874 desc_size =
s->desc_buf_props.uniformTexelBufferDescriptorSize;
1876 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1877 desc_get_info.data.pStorageTexelBuffer = &desc_buf_info;
1878 desc_size =
s->desc_buf_props.storageTexelBufferDescriptorSize;
1882 set, bind, desc_get_info.type);
1889 VkDescriptorBufferInfo desc_pool_write_info_buf = {
1894 VkWriteDescriptorSet desc_pool_write_info = {
1895 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
1897 .descriptorCount = 1,
1898 .dstArrayElement = elem,
1899 .descriptorType = desc_set->
binding[bind].descriptorType,
1900 .pBufferInfo = &desc_pool_write_info_buf,
1910 VkImageView *views,
int set,
int binding,
1911 VkImageLayout
layout, VkSampler sampler)
1916 for (
int i = 0;
i < nb_planes;
i++)
1923 VkShaderStageFlagBits stage,
1942 int has_singular = 0;
1956 VkDescriptorSetLayoutCreateInfo desc_layout_create = {
1957 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1958 .bindingCount =
set->nb_bindings,
1959 .pBindings =
set->binding,
1961 VK_DESCRIPTOR_SET_LAYOUT_CREATE_DESCRIPTOR_BUFFER_BIT_EXT :
1963 VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR :
1967 ret = vk->CreateDescriptorSetLayout(
s->hwctx->act_dev,
1968 &desc_layout_create,
1971 if (
ret != VK_SUCCESS) {
1978 vk->GetDescriptorSetLayoutSizeEXT(
s->hwctx->act_dev, pl->
desc_layout[
i],
1982 s->desc_buf_props.descriptorBufferOffsetAlignment);
1984 for (
int j = 0; j <
set->nb_bindings; j++)
1985 vk->GetDescriptorSetLayoutBindingOffsetEXT(
s->hwctx->act_dev,
1988 &
set->binding_offset[j]);
1999 VkPipelineLayoutCreateInfo pipeline_layout_info;
2002 pipeline_layout_info = (VkPipelineLayoutCreateInfo) {
2003 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
2010 ret = vk->CreatePipelineLayout(
s->hwctx->act_dev, &pipeline_layout_info,
2012 if (
ret != VK_SUCCESS) {
2028 VkComputePipelineCreateInfo pipeline_create_info;
2038 pipeline_create_info = (VkComputePipelineCreateInfo) {
2039 .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
2041 VK_PIPELINE_CREATE_DESCRIPTOR_BUFFER_BIT_EXT : 0x0,
2046 ret = vk->CreateComputePipelines(
s->hwctx->act_dev, VK_NULL_HANDLE, 1,
2047 &pipeline_create_info,
2049 if (
ret != VK_SUCCESS) {
2055 pl->
bind_point = VK_PIPELINE_BIND_POINT_COMPUTE;
2097 vk->DestroyPipeline(
s->hwctx->act_dev, pl->
pipeline,
s->hwctx->alloc);
2113 vk->DestroyDescriptorSetLayout(
s->hwctx->act_dev, pl->
desc_layout[
i],
2117 vk->DestroyDescriptorPool(
s->hwctx->act_dev, pl->
desc_pool,
2146 static const AVClass vulkan_context_class = {
2152 memset(
s, 0,
sizeof(*
s));
2153 s->log_parent = log_parent;
2154 s->class = &vulkan_context_class;
2162 s->hwfc =
s->frames->hwctx;
2164 device_ref =
s->frames->device_ref;
2168 if (!
s->device_ref) {
2174 s->hwctx =
s->device->hwctx;
2177 s->hwctx->nb_enabled_dev_extensions);