From 28713789ad3585d7f15df6dd18c68cbfd5346d7e Mon Sep 17 00:00:00 2001 From: Konstantin Seurer Date: Sun, 4 May 2025 16:17:21 +0200 Subject: [PATCH] vulkan: Replace get_*_key with get_build_config It is a cleaner API and gives more control about the build to the driver. Reviewed-by: Natalie Vock Reviewed-by: Connor Abbott Part-of: --- src/amd/vulkan/radv_acceleration_structure.c | 22 +++++++--- .../vulkan/tu_acceleration_structure.cc | 43 +++++++++---------- .../lavapipe/lvp_acceleration_structure.c | 8 ---- .../vulkan/genX_acceleration_structure.c | 18 +++----- .../runtime/vk_acceleration_structure.c | 8 +--- .../runtime/vk_acceleration_structure.h | 7 ++- 6 files changed, 46 insertions(+), 60 deletions(-) diff --git a/src/amd/vulkan/radv_acceleration_structure.c b/src/amd/vulkan/radv_acceleration_structure.c index 36b02bc20a1..3298f831bd7 100644 --- a/src/amd/vulkan/radv_acceleration_structure.c +++ b/src/amd/vulkan/radv_acceleration_structure.c @@ -399,8 +399,7 @@ radv_get_update_scratch_size(struct vk_device *vk_device, const VkAccelerationSt } static uint32_t -radv_get_encode_key(struct vk_device *vk_device, VkAccelerationStructureTypeKHR type, - VkBuildAccelerationStructureFlagBitsKHR flags) +radv_get_encode_key(struct vk_device *vk_device, const VkAccelerationStructureBuildGeometryInfoKHR *build_info) { struct radv_device *device = container_of(vk_device, struct radv_device, vk); struct radv_physical_device *pdev = radv_device_physical(device); @@ -408,7 +407,7 @@ radv_get_encode_key(struct vk_device *vk_device, VkAccelerationStructureTypeKHR if (radv_use_bvh8(pdev)) return RADV_ENCODE_KEY_COMPACT; - if (flags & VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_COMPACTION_BIT_KHR) + if (build_info->flags & VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_COMPACTION_BIT_KHR) return RADV_ENCODE_KEY_COMPACT; return 0; @@ -420,6 +419,19 @@ radv_get_update_key(struct vk_device *vk_device, bool in_place) return in_place ? RADV_BUILD_FLAG_UPDATE_IN_PLACE : 0; } +static void +radv_get_build_config(struct vk_device *vk_device, struct vk_build_config *config, + const VkAccelerationStructureBuildGeometryInfoKHR *build_info) +{ + uint32_t encode_key = radv_get_encode_key(vk_device, build_info); + config->encode_key[0] = encode_key; + config->encode_key[1] = encode_key; + + uint32_t update_key = + radv_get_update_key(vk_device, build_info->srcAccelerationStructure == build_info->dstAccelerationStructure); + config->update_key[0] = update_key; +} + static void radv_bvh_build_bind_pipeline(VkCommandBuffer commandBuffer, enum radv_meta_object_key_type type, const uint32_t *spirv, uint32_t spirv_size, uint32_t push_constants_size, uint32_t flags) @@ -947,14 +959,12 @@ radv_device_init_accel_struct_build_state(struct radv_device *device) device->meta_state.accel_struct_build.build_ops = (struct vk_acceleration_structure_build_ops){ .begin_debug_marker = vk_accel_struct_cmd_begin_debug_marker, .end_debug_marker = vk_accel_struct_cmd_end_debug_marker, + .get_build_config = radv_get_build_config, .get_as_size = radv_get_as_size, .get_update_scratch_size = radv_get_update_scratch_size, - .get_encode_key[0] = radv_get_encode_key, - .get_encode_key[1] = radv_get_encode_key, .encode_bind_pipeline[1] = radv_init_header_bind_pipeline, .encode_as[1] = radv_init_header, .init_update_scratch = radv_init_update_scratch, - .get_update_key[0] = radv_get_update_key, .update_bind_pipeline[0] = radv_update_bind_pipeline, }; diff --git a/src/freedreno/vulkan/tu_acceleration_structure.cc b/src/freedreno/vulkan/tu_acceleration_structure.cc index 1061341f7e6..0b270e4a54c 100644 --- a/src/freedreno/vulkan/tu_acceleration_structure.cc +++ b/src/freedreno/vulkan/tu_acceleration_structure.cc @@ -153,13 +153,26 @@ VkDeviceSize get_bvh_size(VkDevice device, return layout.size; } -static uint32_t -encode_key(struct vk_device *device, VkAccelerationStructureTypeKHR type, - VkBuildAccelerationStructureFlagBitsKHR flags) -{ - return 0; -} +/* Don't bother copying over the compacted size using a compute shader if + * compaction is never going to happen. + */ +enum tu_header_key { + HEADER_NO_DISPATCH, + HEADER_USE_DISPATCH, +}; +static void +tu_get_build_config( + struct vk_device *device, + struct vk_build_config *config, + const VkAccelerationStructureBuildGeometryInfoKHR *build_info) +{ + config->encode_key[1] = + (build_info->flags & + VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_COMPACTION_BIT_KHR) + ? HEADER_USE_DISPATCH + : HEADER_NO_DISPATCH; +} static VkResult encode_bind_pipeline(VkCommandBuffer commandBuffer, uint32_t key) @@ -224,22 +237,6 @@ encode(VkCommandBuffer commandBuffer, } -/* Don't bother copying over the compacted size using a compute shader if - * compaction is never going to happen. - */ -enum tu_header_key { - HEADER_NO_DISPATCH, - HEADER_USE_DISPATCH -}; - -static uint32_t -header_key(struct vk_device *device, VkAccelerationStructureTypeKHR type, - VkBuildAccelerationStructureFlagBitsKHR flags) -{ - return (flags & VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_COMPACTION_BIT_KHR) ? - HEADER_USE_DISPATCH : HEADER_NO_DISPATCH; -} - static VkResult header_bind_pipeline(VkCommandBuffer commandBuffer, uint32_t key) { @@ -348,8 +345,8 @@ header(VkCommandBuffer commandBuffer, } const struct vk_acceleration_structure_build_ops tu_as_build_ops = { + .get_build_config = tu_get_build_config, .get_as_size = get_bvh_size, - .get_encode_key = { encode_key, header_key }, .encode_bind_pipeline = { encode_bind_pipeline, header_bind_pipeline }, .encode_as = { encode, header }, }; diff --git a/src/gallium/frontends/lavapipe/lvp_acceleration_structure.c b/src/gallium/frontends/lavapipe/lvp_acceleration_structure.c index 043b9c181b3..2ed8b7c0b63 100644 --- a/src/gallium/frontends/lavapipe/lvp_acceleration_structure.c +++ b/src/gallium/frontends/lavapipe/lvp_acceleration_structure.c @@ -645,13 +645,6 @@ lvp_get_as_size(VkDevice device, return sizeof(struct lvp_bvh_header) + nodes_size; } -static uint32_t -lvp_get_encode_key(struct vk_device *device, VkAccelerationStructureTypeKHR type, - VkBuildAccelerationStructureFlagBitsKHR flags) -{ - return 0; -} - static VkResult lvp_encode_bind_pipeline(VkCommandBuffer cmd_buffer, uint32_t key) @@ -661,7 +654,6 @@ lvp_encode_bind_pipeline(VkCommandBuffer cmd_buffer, const struct vk_acceleration_structure_build_ops accel_struct_ops = { .get_as_size = lvp_get_as_size, - .get_encode_key[0] = lvp_get_encode_key, .encode_bind_pipeline[0] = lvp_encode_bind_pipeline, .encode_as[0] = lvp_enqueue_encode_as, }; diff --git a/src/intel/vulkan/genX_acceleration_structure.c b/src/intel/vulkan/genX_acceleration_structure.c index 7e43ef6ab38..7bb58bd7ad0 100644 --- a/src/intel/vulkan/genX_acceleration_structure.c +++ b/src/intel/vulkan/genX_acceleration_structure.c @@ -356,11 +356,11 @@ anv_get_as_size(VkDevice device, return layout.size; } -static uint32_t -anv_get_encode_key(struct vk_device *device, VkAccelerationStructureTypeKHR type, - VkBuildAccelerationStructureFlagBitsKHR flags) +static void +anv_get_build_config(struct vk_device *device, struct vk_build_config *config, + const VkAccelerationStructureBuildGeometryInfoKHR *build_info) { - return 0; + config->encode_key[1] = (build_info->flags & VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_COMPACTION_BIT_KHR) ? 1 : 0; } static VkResult @@ -443,14 +443,6 @@ anv_encode_as(VkCommandBuffer commandBuffer, (cmd_buffer, indirect_addr, true /* is_unaligned_size_x */); } -static uint32_t -anv_get_header_key(struct vk_device *device, VkAccelerationStructureTypeKHR type, - VkBuildAccelerationStructureFlagBitsKHR flags) -{ - return (flags & VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_COMPACTION_BIT_KHR) ? - 1 : 0; -} - static VkResult anv_init_header_bind_pipeline(VkCommandBuffer commandBuffer, uint32_t key) { @@ -590,7 +582,7 @@ static const struct vk_acceleration_structure_build_ops anv_build_ops = { .begin_debug_marker = begin_debug_marker, .end_debug_marker = end_debug_marker, .get_as_size = anv_get_as_size, - .get_encode_key = { anv_get_encode_key, anv_get_header_key }, + .get_build_config = anv_get_build_config, .encode_bind_pipeline = { anv_encode_bind_pipeline, anv_init_header_bind_pipeline }, .encode_as = { anv_encode_as, anv_init_header }, diff --git a/src/vulkan/runtime/vk_acceleration_structure.c b/src/vulkan/runtime/vk_acceleration_structure.c index 3205f4360c4..eaaf2416d95 100644 --- a/src/vulkan/runtime/vk_acceleration_structure.c +++ b/src/vulkan/runtime/vk_acceleration_structure.c @@ -157,12 +157,8 @@ build_config(struct vk_device *device, uint32_t leaf_count, ops->update_as[0]) config.updateable = true; - for (unsigned i = 0; i < MAX_ENCODE_PASSES; i++) { - if (ops->get_encode_key[i]) - config.encode_key[i] = ops->get_encode_key[i](device, leaf_count, build_info->flags); - if (ops->get_update_key[i]) - config.update_key[i] = ops->get_update_key[i](device, build_info->srcAccelerationStructure == build_info->dstAccelerationStructure); - } + if (ops->get_build_config) + ops->get_build_config(device, &config, build_info); return config; } diff --git a/src/vulkan/runtime/vk_acceleration_structure.h b/src/vulkan/runtime/vk_acceleration_structure.h index 2247050f9be..ecebe448367 100644 --- a/src/vulkan/runtime/vk_acceleration_structure.h +++ b/src/vulkan/runtime/vk_acceleration_structure.h @@ -93,10 +93,9 @@ struct vk_acceleration_structure_build_ops { VkDeviceSize (*get_update_scratch_size)(struct vk_device *device, const VkAccelerationStructureBuildGeometryInfoKHR *build_info, uint32_t leaf_count); - uint32_t (*get_encode_key[MAX_ENCODE_PASSES])(struct vk_device *device, - VkAccelerationStructureTypeKHR type, - VkBuildAccelerationStructureFlagBitsKHR flags); - uint32_t (*get_update_key[MAX_ENCODE_PASSES])(struct vk_device *device, bool in_place); + void (*get_build_config)(struct vk_device *device, + struct vk_build_config *config, + const VkAccelerationStructureBuildGeometryInfoKHR *build_info); VkResult (*encode_bind_pipeline[MAX_ENCODE_PASSES])(VkCommandBuffer cmd_buffer, uint32_t key); void (*encode_as[MAX_ENCODE_PASSES])(VkCommandBuffer cmd_buffer,