From 6cae6e8708158f69461bb1505a492367bfaa5f12 Mon Sep 17 00:00:00 2001 From: Konstantin Seurer Date: Fri, 2 May 2025 14:46:15 +0200 Subject: [PATCH] vulkan: Allow reserving scratch memory for encode passes Reviewed-by: Natalie Vock Reviewed-by: Connor Abbott Part-of: --- src/vulkan/runtime/vk_acceleration_structure.c | 14 ++++++++++++++ src/vulkan/runtime/vk_acceleration_structure.h | 1 + 2 files changed, 15 insertions(+) diff --git a/src/vulkan/runtime/vk_acceleration_structure.c b/src/vulkan/runtime/vk_acceleration_structure.c index eb82ad35a10..35237abeda7 100644 --- a/src/vulkan/runtime/vk_acceleration_structure.c +++ b/src/vulkan/runtime/vk_acceleration_structure.c @@ -224,9 +224,20 @@ get_scratch_layout(struct vk_device *device, else lbvh_node_space = sizeof(struct lbvh_node_info) * internal_count; + uint32_t encode_scratch_size = 0; + if (device->as_build_ops->get_encode_scratch_size) { + for (uint32_t i = 0; i < MAX_ENCODE_PASSES; i++) { + uint32_t tmp_size = device->as_build_ops->get_encode_scratch_size(device, config.encode_key[i], leaf_count); + encode_scratch_size = MAX2(encode_scratch_size, tmp_size); + } + } + scratch->header_offset = offset; offset += sizeof(struct vk_ir_header); + /* The encode passes should not need node sorting state. Reuse the space reserved for node sorting. */ + uint32_t encode_scratch_end = offset + encode_scratch_size; + scratch->sort_buffer_offset[0] = offset; offset += requirements.keyvals_size; @@ -240,6 +251,9 @@ get_scratch_layout(struct vk_device *device, scratch->lbvh_node_offset = offset; offset += MAX3(requirements.internal_size, ploc_scratch_space, lbvh_node_space); + /* Make sure encode scratch space does not overlap the BVH. */ + offset = MAX2(offset, encode_scratch_end); + scratch->ir_offset = offset; offset += ir_leaf_size * leaf_count; diff --git a/src/vulkan/runtime/vk_acceleration_structure.h b/src/vulkan/runtime/vk_acceleration_structure.h index 11e8e16d780..dd1705a68a2 100644 --- a/src/vulkan/runtime/vk_acceleration_structure.h +++ b/src/vulkan/runtime/vk_acceleration_structure.h @@ -76,6 +76,7 @@ struct vk_acceleration_structure_build_ops { VkDeviceSize (*get_as_size)(VkDevice device, const VkAccelerationStructureBuildGeometryInfoKHR *pBuildInfo, uint32_t leaf_count); + VkDeviceSize (*get_encode_scratch_size)(struct vk_device *device, uint32_t key, uint32_t leaf_count); VkDeviceSize (*get_update_scratch_size)(struct vk_device *device, const VkAccelerationStructureBuildGeometryInfoKHR *build_info, uint32_t leaf_count);