Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/asahi/vulkan/bvh/agx3_batch_build_leaf.comp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "vk_bvh_defines.h"
#define VK_USED_BUILD_FLAGS VK_LEAF_BUILD_FLAGS
#define VK_BATCH_LEAVES 1
/* An M5 image starts at the reference rounded up to its 1 KiB alignment
* (hk_as_native_va()): the reference itself is the structure's buffer
* address, which may be 256-byte aligned. */
#define VK_BVH_AS_ADDRESS(reference) (((reference) + 1023ul) & ~1023ul)
uvec3 batch_global_id;
#define gl_GlobalInvocationID batch_global_id
#define main build_leaf_body
Expand Down
9 changes: 6 additions & 3 deletions src/asahi/vulkan/hk_m5_acceleration_structure.c
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,13 @@ VKAPI_ATTR VkDeviceAddress VKAPI_CALL
hk_m5_backend_GetAccelerationStructureDeviceAddressKHR(
VkDevice _device, const VkAccelerationStructureDeviceAddressInfoKHR *info)
{
VK_FROM_HANDLE(hk_device, dev, _device);
VK_FROM_HANDLE(vk_acceleration_structure, as, info->accelerationStructure);
return dev->m5_native_as ? hk_as_native_va(as)
: vk_acceleration_structure_get_va(as);
/* The address of the structure's storage, as for every other driver:
* applications (vkd3d-proton for D3D12) place structures at 256-byte
* aligned buffer addresses and use those addresses directly. Everything
* that reads the native image from an address rounds it up to the image
* alignment (hk_as_native_va()). */
return vk_acceleration_structure_get_va(as);
}

/* ---------------------------------------------------------------- */
Expand Down
6 changes: 4 additions & 2 deletions src/asahi/vulkan/hk_nir_lower_m5_ray_queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* it works in every shader stage.
*
* Image layout read here (offsets relative to the 1 KiB-aligned image start,
* which is the acceleration structure's device address):
* which is the acceleration structure's device address rounded up to 1 KiB):
*
* - slot 0 (bytes 0..63): the root node; slots 1 and 2: the header, with
* the readiness word at 0xa0 (zero for an empty structure).
Expand Down Expand Up @@ -992,7 +992,9 @@ static void
lower_rq_initialize(nir_builder *b, nir_def *index, nir_intrinsic_instr *instr,
struct m5_rq_vars *vars)
{
nir_def *as = instr->src[1].ssa;
/* A descriptor holds the image address already; an address converted
* from an integer is the structure's device address. */
nir_def *as = nir_iand_imm(b, nir_iadd_imm(b, instr->src[1].ssa, 1023), ~UINT64_C(1023));
rq_store(b, index, vars->root, as);
rq_store(b, index, vars->flags, instr->src[2].ssa);
rq_store(b, index, vars->cull_mask, nir_iand_imm(b, instr->src[3].ssa, 0xff));
Expand Down
16 changes: 12 additions & 4 deletions src/vulkan/runtime/bvh/leaf.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ calculate_instance_node_bounds(vk_aabb blas_aabb, mat3x4 otw_matrix)
return aabb;
}

/* The address of the structure an instance references, from its
* accelerationStructureReference. A driver whose structures do not start at
* the address the API reports defines this. */
#ifndef VK_BVH_AS_ADDRESS
#define VK_BVH_AS_ADDRESS(reference) (reference)
#endif

bool
build_instance(inout vk_aabb bounds, VOID_REF src_ptr, VOID_REF dst_ptr, uint32_t global_id)
{
Expand All @@ -171,12 +178,13 @@ build_instance(inout vk_aabb bounds, VOID_REF src_ptr, VOID_REF dst_ptr, uint32_
(!VK_TEST_BUILD_FLAG_ALWAYS_ACTIVE && instance.custom_instance_and_mask < (1u << 24u)))
return false;

DEREF(node).base_ptr = instance.accelerationStructureReference;
const uint64_t blas = VK_BVH_AS_ADDRESS(instance.accelerationStructureReference);
DEREF(node).base_ptr = blas;

mat4 transform = mat4(instance.transform);
DEREF(node).otw_matrix = mat3x4(transform);

vk_aabb blas_aabb = DEREF(REF(vk_aabb)(instance.accelerationStructureReference + BVH_BOUNDS_OFFSET));
vk_aabb blas_aabb = DEREF(REF(vk_aabb)(blas + BVH_BOUNDS_OFFSET));

bounds = calculate_instance_node_bounds(blas_aabb, mat3x4(transform));

Expand All @@ -194,7 +202,7 @@ build_instance(inout vk_aabb bounds, VOID_REF src_ptr, VOID_REF dst_ptr, uint32_
* wouldn't be worth it.
*/
if (bounds_volume > 1.4f * blas_aabb_volume)
bounds = CALCULATE_FINE_INSTANCE_NODE_BOUNDS(instance.accelerationStructureReference, mat3x4(transform));
bounds = CALCULATE_FINE_INSTANCE_NODE_BOUNDS(blas, mat3x4(transform));
#endif

if (any(isnan(bounds.min)) || any(isnan(bounds.max))) {
Expand Down Expand Up @@ -222,7 +230,7 @@ build_instance(inout vk_aabb bounds, VOID_REF src_ptr, VOID_REF dst_ptr, uint32_
else if ((instance.sbt_offset_and_flags & (VK_GEOMETRY_INSTANCE_FORCE_NO_OPAQUE_BIT_KHR << 24)) != 0)
root_flags = VK_BVH_BOX_FLAG_NO_OPAQUE;
else
root_flags = DEREF(REF(uint32_t)(instance.accelerationStructureReference + ROOT_FLAGS_OFFSET));
root_flags = DEREF(REF(uint32_t)(blas + ROOT_FLAGS_OFFSET));
DEREF(node).root_flags = root_flags | ((~(instance.custom_instance_and_mask >> 24)) << VK_BVH_BOX_FLAGS_INV_CULL_MASK_SHIFT);

return true;
Expand Down