From 50bb0d6427728f4a73eb28779cd67525f313d8bc Mon Sep 17 00:00:00 2001 From: Bas Nieuwenhuizen Date: Wed, 21 Sep 2022 01:37:04 +0200 Subject: [PATCH] radv: Use GLSL matrices for instance transforms in BVH. Reviewed-by: Konstantin Seurer Part-of: --- src/amd/vulkan/bvh/bvh.h | 10 ++++++++-- src/amd/vulkan/bvh/leaf.comp | 20 +++++++------------- src/amd/vulkan/radv_rra.c | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/amd/vulkan/bvh/bvh.h b/src/amd/vulkan/bvh/bvh.h index 5f6953b..46305e5 100644 --- a/src/amd/vulkan/bvh/bvh.h +++ b/src/amd/vulkan/bvh/bvh.h @@ -33,6 +33,11 @@ #define VK_UUID_SIZE 16 #else #include + +typedef struct { + float values[3][4]; +} mat3x4; + #endif struct radv_accel_struct_serialization_header { @@ -94,12 +99,13 @@ struct radv_bvh_instance_node { /* lower 24 bits are the sbt offset, upper 8 bits are VkGeometryInstanceFlagsKHR */ uint32_t sbt_offset_and_flags; - float wto_matrix[3][4]; + mat3x4 wto_matrix; + uint32_t instance_id; uint32_t reserved[3]; /* Object to world matrix transposed from the initial transform. */ - float otw_matrix[3][4]; + mat3x4 otw_matrix; }; struct radv_bvh_box16_node { diff --git a/src/amd/vulkan/bvh/leaf.comp b/src/amd/vulkan/bvh/leaf.comp index bcfd318..252c474 100644 --- a/src/amd/vulkan/bvh/leaf.comp +++ b/src/amd/vulkan/bvh/leaf.comp @@ -174,7 +174,7 @@ load_vertices(VOID_REF vertices, triangle_indices indices, uint32_t vertex_forma /* A GLSL-adapted copy of VkAccelerationStructureInstanceKHR. */ struct AccelerationStructureInstance { - float transform[12]; + mat3x4 transform; uint32_t custom_instance_and_mask; uint32_t sbt_offset_and_flags; uint64_t accelerationStructureReference; @@ -191,19 +191,13 @@ build_instance(inout AABB bounds, VOID_REF src_ptr, VOID_REF dst_ptr, uint32_t g if (instance.accelerationStructureReference == 0) return false; - mat4 transform = mat4(1.0); - for (uint32_t col = 0; col < 4; col++) - for (uint32_t row = 0; row < 3; row++) - transform[col][row] = instance.transform[col + row * 4]; + mat4 transform = mat4(instance.transform); - mat4 inv_transform = inverse(transform); - for (uint32_t row = 0; row < 3; row++) - for (uint32_t col = 0; col < 4; col++) - DEREF(node).wto_matrix[row][col] = inv_transform[col][row]; - - for (uint32_t col = 0; col < 4; col++) - for (uint32_t row = 0; row < 3; row++) - DEREF(node).otw_matrix[row][col] = transform[col][row]; + /* We store everything as mat3x4 for layout reasons but the conceptual matrix + * is really a mat4x3. So transpose it temporarily for the invertion. */ + mat4 inv_transform = transpose(inverse(transpose(transform))); + DEREF(node).wto_matrix = mat3x4(inv_transform); + DEREF(node).otw_matrix = mat3x4(transform); radv_accel_struct_header instance_header = DEREF(REF(radv_accel_struct_header)(instance.accelerationStructureReference)); diff --git a/src/amd/vulkan/radv_rra.c b/src/amd/vulkan/radv_rra.c index 96c3a01..92933a8 100644 --- a/src/amd/vulkan/radv_rra.c +++ b/src/amd/vulkan/radv_rra.c @@ -510,8 +510,8 @@ rra_transcode_instance_node(struct rra_instance_node *dst, const struct radv_bvh dst->instance_id = src->instance_id; dst->blas_metadata_size = sizeof(struct rra_accel_struct_metadata); - memcpy(dst->wto_matrix, src->wto_matrix, sizeof(dst->wto_matrix)); - memcpy(dst->otw_matrix, src->otw_matrix, sizeof(dst->otw_matrix)); + memcpy(dst->wto_matrix, src->wto_matrix.values, sizeof(dst->wto_matrix)); + memcpy(dst->otw_matrix, src->otw_matrix.values, sizeof(dst->otw_matrix)); } static void -- 2.7.4