Module: Mesa
Branch: main
Commit: 9cbc609db3456d8d6e6f0e55294a02df6c0b2af3
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=9cbc609db3456d8d6e6f0e55294a02df6c0b2af3

Author: Konstantin Seurer <[email protected]>
Date:   Thu Sep  1 21:21:19 2022 +0200

radv: Deduplicate push constant structs

This patch adds a header that is shared between the accel struct build
kernels and the dispatch code.

Signed-off-by: Konstantin Seurer <[email protected]>
Reviewed-by: Bas Nieuwenhuizen <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18376>

---

 src/amd/vulkan/bvh/build_interface.h         | 68 +++++++++++++++++++
 src/amd/vulkan/bvh/internal.comp             | 10 +--
 src/amd/vulkan/bvh/leaf.comp                 | 21 +-----
 src/amd/vulkan/bvh/meson.build               |  1 +
 src/amd/vulkan/bvh/morton.comp               |  8 +--
 src/amd/vulkan/radv_acceleration_structure.c | 97 +++++++++-------------------
 6 files changed, 110 insertions(+), 95 deletions(-)

diff --git a/src/amd/vulkan/bvh/build_interface.h 
b/src/amd/vulkan/bvh/build_interface.h
new file mode 100644
index 00000000000..50ca01475d7
--- /dev/null
+++ b/src/amd/vulkan/bvh/build_interface.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright © 2022 Konstantin Seurer
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef BVH_BUILD_INTERFACE_H
+#define BVH_BUILD_INTERFACE_H
+
+#ifdef VULKAN
+#include "build_helpers.h"
+#else
+#include <stdint.h>
+#define REF(type) uint64_t
+#define VOID_REF  uint64_t
+#endif
+
+struct leaf_args {
+   VOID_REF bvh;
+   REF(AABB) bounds;
+   REF(key_id_pair) ids;
+
+   VOID_REF data;
+   VOID_REF indices;
+   VOID_REF transform;
+
+   uint32_t dst_offset;
+   uint32_t first_id;
+   uint32_t geometry_type;
+   uint32_t geometry_id;
+
+   uint32_t stride;
+   uint32_t vertex_format;
+   uint32_t index_format;
+};
+
+struct morton_args {
+   VOID_REF bvh;
+   REF(AABB) bounds;
+   REF(key_id_pair) ids;
+};
+
+struct internal_args {
+   VOID_REF bvh;
+   REF(key_id_pair) src_ids;
+   REF(key_id_pair) dst_ids;
+   uint32_t dst_offset;
+   uint32_t fill_count;
+};
+
+#endif
diff --git a/src/amd/vulkan/bvh/internal.comp b/src/amd/vulkan/bvh/internal.comp
index 8d54bfe556e..9b2653096bf 100644
--- a/src/amd/vulkan/bvh/internal.comp
+++ b/src/amd/vulkan/bvh/internal.comp
@@ -35,15 +35,11 @@
 
 layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
 
-#include "build_helpers.h"
+#include "build_interface.h"
 
 layout(push_constant) uniform CONSTS {
-   VOID_REF bvh;
-   REF(key_id_pair) src_ids;
-   REF(key_id_pair) dst_ids;
-   uint32_t dst_offset;
-   uint32_t fill_count;
-} args;
+   internal_args args;
+};
 
 void
 main(void)
diff --git a/src/amd/vulkan/bvh/leaf.comp b/src/amd/vulkan/bvh/leaf.comp
index 3badc1bd1ae..c7aeb0f24e3 100644
--- a/src/amd/vulkan/bvh/leaf.comp
+++ b/src/amd/vulkan/bvh/leaf.comp
@@ -36,26 +36,11 @@
 
 layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
 
-#include "build_helpers.h"
+#include "build_interface.h"
 
 layout(push_constant) uniform CONSTS {
-   VOID_REF bvh;
-   REF(AABB) bounds;
-   REF(key_id_pair) ids;
-
-   VOID_REF data;
-   VOID_REF indices;
-   VOID_REF transform;
-
-   uint32_t dst_offset;
-   uint32_t first_id;
-   uint32_t geometry_type;
-   uint32_t geometry_id;
-
-   uint32_t stride;
-   uint32_t vertex_format;
-   uint32_t index_format;
-} args;
+   leaf_args args;
+};
 
 /* Just a wrapper for 3 uints. */
 struct triangle_indices {
diff --git a/src/amd/vulkan/bvh/meson.build b/src/amd/vulkan/bvh/meson.build
index f4b968137ea..6b785bbc9c0 100644
--- a/src/amd/vulkan/bvh/meson.build
+++ b/src/amd/vulkan/bvh/meson.build
@@ -28,6 +28,7 @@ bvh_include_dir = meson.source_root() + '/src/amd/vulkan/bvh'
 
 bvh_includes = files(
   'build_helpers.h',
+  'build_interface.h',
   'bvh.h',
 )
 
diff --git a/src/amd/vulkan/bvh/morton.comp b/src/amd/vulkan/bvh/morton.comp
index 1f8c1c36609..db0a843d04e 100644
--- a/src/amd/vulkan/bvh/morton.comp
+++ b/src/amd/vulkan/bvh/morton.comp
@@ -35,13 +35,11 @@
 
 layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
 
-#include "build_helpers.h"
+#include "build_interface.h"
 
 layout(push_constant) uniform CONSTS {
-   VOID_REF bvh;
-   REF(AABB) bounds;
-   REF(key_id_pair) ids;
-} args;
+   morton_args args;
+};
 
 uint32_t
 morton_component(uint32_t x)
diff --git a/src/amd/vulkan/radv_acceleration_structure.c 
b/src/amd/vulkan/radv_acceleration_structure.c
index 78d6ae7bd07..76fbe6219c9 100644
--- a/src/amd/vulkan/radv_acceleration_structure.c
+++ b/src/amd/vulkan/radv_acceleration_structure.c
@@ -29,6 +29,8 @@
 
 #include "radix_sort/radv_radix_sort.h"
 
+#include "bvh/build_interface.h"
+
 static const uint32_t leaf_spv[] = {
 #include "bvh/leaf.comp.spv.h"
 };
@@ -206,39 +208,6 @@ create_accel_build_shader(struct radv_device *device, 
const char *name)
    return b;
 }
 
-struct leaf_constants {
-   uint64_t bvh_addr;
-   uint64_t bounds_addr;
-   uint64_t ids_addr;
-
-   uint64_t data_addr;
-   uint64_t indices_addr;
-   uint64_t transform_addr;
-
-   uint32_t dst_offset;
-   uint32_t first_id;
-   uint32_t geometry_type;
-   uint32_t geometry_id;
-
-   uint32_t stride;
-   uint32_t vertex_format;
-   uint32_t index_format;
-};
-
-struct morton_constants {
-   uint64_t bvh_addr;
-   uint64_t bounds_addr;
-   uint64_t ids_addr;
-};
-
-struct internal_constants {
-   uint64_t bvh_addr;
-   uint64_t src_ids_addr;
-   uint64_t dst_ids_addr;
-   uint32_t dst_offset;
-   uint32_t fill_count;
-};
-
 enum copy_mode {
    COPY_MODE_COPY,
    COPY_MODE_SERIALIZE,
@@ -609,15 +578,14 @@ radv_device_init_accel_struct_build_state(struct 
radv_device *device)
    VkResult result;
    nir_shader *copy_cs = build_copy_shader(device);
 
-   result =
-      create_build_pipeline_spv(device, leaf_spv, sizeof(leaf_spv), 
sizeof(struct leaf_constants),
-                                
&device->meta_state.accel_struct_build.leaf_pipeline,
-                                
&device->meta_state.accel_struct_build.leaf_p_layout);
+   result = create_build_pipeline_spv(device, leaf_spv, sizeof(leaf_spv), 
sizeof(struct leaf_args),
+                                      
&device->meta_state.accel_struct_build.leaf_pipeline,
+                                      
&device->meta_state.accel_struct_build.leaf_p_layout);
    if (result != VK_SUCCESS)
       return result;
 
    result = create_build_pipeline_spv(device, internal_spv, 
sizeof(internal_spv),
-                                      sizeof(struct internal_constants),
+                                      sizeof(struct internal_args),
                                       
&device->meta_state.accel_struct_build.internal_pipeline,
                                       
&device->meta_state.accel_struct_build.internal_p_layout);
    if (result != VK_SUCCESS)
@@ -630,10 +598,10 @@ radv_device_init_accel_struct_build_state(struct 
radv_device *device)
    if (result != VK_SUCCESS)
       return result;
 
-   result = create_build_pipeline_spv(device, morton_spv, sizeof(morton_spv),
-                                      sizeof(struct morton_constants),
-                                      
&device->meta_state.accel_struct_build.morton_pipeline,
-                                      
&device->meta_state.accel_struct_build.morton_p_layout);
+   result =
+      create_build_pipeline_spv(device, morton_spv, sizeof(morton_spv), 
sizeof(struct morton_args),
+                                
&device->meta_state.accel_struct_build.morton_pipeline,
+                                
&device->meta_state.accel_struct_build.morton_p_layout);
    if (result != VK_SUCCESS)
       return result;
 
@@ -700,10 +668,10 @@ radv_CmdBuildAccelerationStructuresKHR(
       RADV_FROM_HANDLE(radv_acceleration_structure, accel_struct,
                        pInfos[i].dstAccelerationStructure);
 
-      struct leaf_constants leaf_consts = {
-         .bvh_addr = radv_accel_struct_get_va(accel_struct),
-         .bounds_addr = pInfos[i].scratchData.deviceAddress,
-         .ids_addr = pInfos[i].scratchData.deviceAddress + 
SCRATCH_TOTAL_BOUNDS_SIZE,
+      struct leaf_args leaf_consts = {
+         .bvh = radv_accel_struct_get_va(accel_struct),
+         .bounds = pInfos[i].scratchData.deviceAddress,
+         .ids = pInfos[i].scratchData.deviceAddress + 
SCRATCH_TOTAL_BOUNDS_SIZE,
          .dst_offset =
             ALIGN(sizeof(struct radv_accel_struct_header), 64) + sizeof(struct 
radv_bvh_box32_node),
       };
@@ -725,19 +693,18 @@ radv_CmdBuildAccelerationStructuresKHR(
          case VK_GEOMETRY_TYPE_TRIANGLES_KHR:
             assert(pInfos[i].type == 
VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR);
 
-            leaf_consts.data_addr =
-               geom->geometry.triangles.vertexData.deviceAddress +
-               buildRangeInfo->firstVertex * 
geom->geometry.triangles.vertexStride;
-            leaf_consts.indices_addr = 
geom->geometry.triangles.indexData.deviceAddress;
+            leaf_consts.data = 
geom->geometry.triangles.vertexData.deviceAddress +
+                               buildRangeInfo->firstVertex * 
geom->geometry.triangles.vertexStride;
+            leaf_consts.indices = 
geom->geometry.triangles.indexData.deviceAddress;
 
             if (geom->geometry.triangles.indexType == VK_INDEX_TYPE_NONE_KHR)
-               leaf_consts.data_addr += buildRangeInfo->primitiveOffset;
+               leaf_consts.data += buildRangeInfo->primitiveOffset;
             else
-               leaf_consts.indices_addr += buildRangeInfo->primitiveOffset;
+               leaf_consts.indices += buildRangeInfo->primitiveOffset;
 
-            leaf_consts.transform_addr = 
geom->geometry.triangles.transformData.deviceAddress;
-            if (leaf_consts.transform_addr)
-               leaf_consts.transform_addr += buildRangeInfo->transformOffset;
+            leaf_consts.transform = 
geom->geometry.triangles.transformData.deviceAddress;
+            if (leaf_consts.transform)
+               leaf_consts.transform += buildRangeInfo->transformOffset;
 
             leaf_consts.stride = geom->geometry.triangles.vertexStride;
             leaf_consts.vertex_format = geom->geometry.triangles.vertexFormat;
@@ -748,7 +715,7 @@ radv_CmdBuildAccelerationStructuresKHR(
          case VK_GEOMETRY_TYPE_AABBS_KHR:
             assert(pInfos[i].type == 
VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR);
 
-            leaf_consts.data_addr =
+            leaf_consts.data =
                geom->geometry.aabbs.data.deviceAddress + 
buildRangeInfo->primitiveOffset;
             leaf_consts.stride = geom->geometry.aabbs.stride;
 
@@ -757,7 +724,7 @@ radv_CmdBuildAccelerationStructuresKHR(
          case VK_GEOMETRY_TYPE_INSTANCES_KHR:
             assert(pInfos[i].type == 
VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR);
 
-            leaf_consts.data_addr =
+            leaf_consts.data =
                geom->geometry.instances.data.deviceAddress + 
buildRangeInfo->primitiveOffset;
 
             if (geom->geometry.instances.arrayOfPointers)
@@ -793,10 +760,10 @@ radv_CmdBuildAccelerationStructuresKHR(
       RADV_FROM_HANDLE(radv_acceleration_structure, accel_struct,
                        pInfos[i].dstAccelerationStructure);
 
-      const struct morton_constants consts = {
-         .bvh_addr = radv_accel_struct_get_va(accel_struct),
-         .bounds_addr = pInfos[i].scratchData.deviceAddress,
-         .ids_addr = pInfos[i].scratchData.deviceAddress + 
SCRATCH_TOTAL_BOUNDS_SIZE,
+      const struct morton_args consts = {
+         .bvh = radv_accel_struct_get_va(accel_struct),
+         .bounds = pInfos[i].scratchData.deviceAddress,
+         .ids = pInfos[i].scratchData.deviceAddress + 
SCRATCH_TOTAL_BOUNDS_SIZE,
       };
 
       radv_CmdPushConstants(commandBuffer,
@@ -878,10 +845,10 @@ radv_CmdBuildAccelerationStructuresKHR(
          if (final_iter)
             dst_node_offset = ALIGN(sizeof(struct radv_accel_struct_header), 
64);
 
-         const struct internal_constants consts = {
-            .bvh_addr = radv_accel_struct_get_va(accel_struct),
-            .src_ids_addr = pInfos[i].scratchData.deviceAddress + 
src_scratch_offset,
-            .dst_ids_addr = pInfos[i].scratchData.deviceAddress + 
dst_scratch_offset,
+         const struct internal_args consts = {
+            .bvh = radv_accel_struct_get_va(accel_struct),
+            .src_ids = pInfos[i].scratchData.deviceAddress + 
src_scratch_offset,
+            .dst_ids = pInfos[i].scratchData.deviceAddress + 
dst_scratch_offset,
             .dst_offset = dst_node_offset,
             .fill_count = bvh_states[i].node_count | (final_iter ? 0x80000000U 
: 0),
          };

Reply via email to