Skip to content

Commit 23eca97

Browse files
committed
[SPIR-V] Add descriptor heap RaytracingAccelerationStructure support
Extends the SPV_EXT_descriptor_heap native heap lowering to cover RaytracingAccelerationStructure resources loaded from ResourceDescriptorHeap. Acceleration structure descriptors are accessed via OpUntypedAccessChainKHR into a runtime array of OpTypeAccelerationStructureKHR, consistent with the image and sampler paths added in the previous commit.
1 parent bd79339 commit 23eca97

4 files changed

Lines changed: 63 additions & 0 deletions

File tree

tools/clang/include/clang/SPIRV/AstTypeProbe.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ bool isBuffer(QualType type);
250250
/// \brief Returns true if the given type is the HLSL RWBuffer type.
251251
bool isRWBuffer(QualType type);
252252

253+
/// \brief Returns true if the given type is the HLSL
254+
/// RaytracingAccelerationStructure type.
255+
bool isRaytracingAccelerationStructure(QualType type);
256+
253257
/// \brief Returns true if the given type is an HLSL Texture type.
254258
bool isTexture(QualType);
255259

tools/clang/lib/SPIRV/AstTypeProbe.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,13 @@ bool isBuffer(QualType type) {
917917
return false;
918918
}
919919

920+
bool isRaytracingAccelerationStructure(QualType type) {
921+
if (const auto *rt = type->getAs<RecordType>()) {
922+
return rt->getDecl()->getName() == "RaytracingAccelerationStructure";
923+
}
924+
return false;
925+
}
926+
920927
bool isRWTexture(QualType type) {
921928
if (const auto *rt = type->getAs<RecordType>()) {
922929
const auto name = rt->getDecl()->getName();

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,16 @@ bool SpirvEmitter::tryToCreateDescriptorHeapAlias(const VarDecl *decl,
20392039
return true;
20402040
}
20412041

2042+
if (isRaytracingAccelerationStructure(decl->getType())) {
2043+
if (auto *initVal = loadIfGLValue(init))
2044+
declIdMapper.registerFnVarAlias(decl, initVal);
2045+
else
2046+
emitError("cannot create descriptor heap acceleration structure alias "
2047+
"from initializer",
2048+
init->getExprLoc());
2049+
return true;
2050+
}
2051+
20422052
return false;
20432053
}
20442054

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// RUN: %dxc -T lib_6_6 -fspv-use-descriptor-heap -fspv-target-env=vulkan1.3 -fvk-resource-heap-stride 64 -fvk-sampler-heap-stride 32 -fspv-extension=SPV_KHR_ray_tracing -fspv-extension=SPV_EXT_descriptor_heap -fspv-extension=SPV_KHR_untyped_pointers -spirv %s | FileCheck %s
2+
3+
// Verifies: ResourceDescriptorHeap of a RaytracingAccelerationStructure
4+
// lowers to an acceleration-structure runtime array, loads the accel
5+
// handle from the heap, and emits OpTraceRayKHR under the ray-tracing
6+
// capability/extension.
7+
8+
// CHECK: OpCapability RayTracingKHR
9+
// CHECK: OpExtension "SPV_KHR_ray_tracing"
10+
11+
// CHECK-DAG: %[[UntypedUniformConstant:[a-zA-Z0-9_]+]] = OpTypeUntypedPointerKHR UniformConstant
12+
// CHECK-DAG: %[[Accel:[a-zA-Z0-9_]+]] = OpTypeAccelerationStructureKHR
13+
// CHECK-DAG: %[[ASArray:[a-zA-Z0-9_]+]] = OpTypeRuntimeArray %[[Accel]]
14+
// CHECK: %[[ResourceHeap:[a-zA-Z0-9_]+]] = OpUntypedVariableKHR %[[UntypedUniformConstant]] UniformConstant
15+
16+
struct Payload {
17+
float4 color;
18+
};
19+
20+
struct Attribute {
21+
float2 bary;
22+
};
23+
24+
[shader("closesthit")]
25+
void main(inout Payload payload, in Attribute attr) {
26+
RaytracingAccelerationStructure scene = ResourceDescriptorHeap[3];
27+
28+
RayDesc ray;
29+
ray.Origin = float3(0.0f, 0.0f, 0.0f);
30+
ray.Direction = float3(0.0f, 0.0f, -1.0f);
31+
ray.TMin = 0.0f;
32+
ray.TMax = 1000.0f;
33+
34+
Payload childPayload = { float4(attr.bary, 0.0f, 1.0f) };
35+
36+
// CHECK: %[[Desc:[a-zA-Z0-9_]+]] = OpUntypedAccessChainKHR %[[UntypedUniformConstant]] %[[ASArray]] %[[ResourceHeap]] %uint_3
37+
// CHECK: %[[Scene:[a-zA-Z0-9_]+]] = OpLoad %[[Accel]] %[[Desc]]
38+
// CHECK: OpTraceRayKHR %[[Scene]]
39+
TraceRay(scene, 0x0, 0xff, 0, 1, 0, ray, childPayload);
40+
41+
payload.color = childPayload.color;
42+
}

0 commit comments

Comments
 (0)