Color legend method
The GPU half of the library: the any-hit walk of the scene, for compute and pixel shaders, and the buffers it reads.
An HLSL header, copied to your build output at Content/Effects/Lib/: #include "Lib/RayTraversal.fxh". It is a line-for-line port of Tlas.IntersectAny and MeshBlas.IntersectAny, declares the buffers Bind sets, and needs nothing that only compute shaders have. The GPU page shows it in context, with code.
Names are colored by what they are: a method; values and fields keep the plain color.
Functions
RtNodeHit() →
bool RtNodeHit(GpuBvhNode node, float3 origin, float3 inverseDirection, float maxDistance) · Whether the ray enters the node’s box closer than maxDistance: the slab test, as Bvh.HitsNode does it. A step of the walk.
| Parameter | Type | Description |
|---|---|---|
node |
GpuBvhNode |
The node. |
origin |
float3 |
Where the ray starts. |
inverseDirection |
float3 |
One over each component of its direction. |
maxDistance |
float |
Where the ray ends. |
RtSlab() →
void RtSlab(float boundsMin, float boundsMax, float origin, float inverseDirection, inout float tMin, inout float tMax) · Narrows the stretch of the ray inside a box, from tMin to tMax, to the part between the box’s two planes on one axis. Written with comparisons rather than min and max, so a NaN, from a ray running exactly along a plane, leaves the stretch as it is: HLSL doesn’t say what its min and max do with one.
| Parameter | Type | Description |
|---|---|---|
boundsMin |
float |
The box’s low plane on the axis. |
boundsMax |
float |
Its high plane. |
origin |
float |
The ray’s origin on the axis. |
inverseDirection |
float |
One over its direction on the axis. |
tMin |
inout float |
Where the stretch starts, raised to where the ray enters the slab. |
tMax |
inout float |
Where it ends, lowered to where the ray leaves the slab. |
RtTraceAny() →
bool RtTraceAny(float3 origin, float3 direction, float maxDistance, int requiredFlags) · Whether any instance with every flag in requiredFlags blocks the ray closer than maxDistance: the GPU’s Tlas.IntersectAny. The ray is in the space the instances were declared in, and its direction need not be of length 1. A maxDistance past RT_MAX_DISTANCE is taken as it, hits closer than RT_MIN_DISTANCE are ignored, and with an empty scene nothing blocks the ray.
| Parameter | Type | Description |
|---|---|---|
origin |
float3 |
Where the ray starts: off the surface it leaves, along its normal. |
direction |
float3 |
Its direction. |
maxDistance |
float |
Hits at or beyond this ray parameter don’t count. |
requiredFlags |
int |
RT_FLAG_CASTS_SHADOW or RT_FLAG_VISIBLE_TO_GI, or the two combined; 0 for every instance. |
RtTraceAnyBlas() →
bool RtTraceAnyBlas(int firstNode, int endNode, float3 origin, float3 direction, float maxDistance, inout uint steps) · Whether any triangle of one BLAS blocks the ray, in the mesh’s space: the walk over BlasNodes from firstNode up to endNode, an instance’s BlasNodeOffset and BlasNodeEnd. Adds every box and triangle it tests to steps. The bottom half of RtTraceAnyCounted.
| Parameter | Type | Description |
|---|---|---|
firstNode |
int |
The BLAS’s root in BlasNodes. |
endNode |
int |
One past its last node. |
origin |
float3 |
The ray’s origin, in the mesh’s space. |
direction |
float3 |
Its direction, in the mesh’s space, not normalized. |
maxDistance |
float |
Hits at or beyond this ray parameter don’t count. |
steps |
inout uint |
Added to, once per box and once per triangle tested. |
RtTraceAnyCounted() →
bool RtTraceAnyCounted(float3 origin, float3 direction, float maxDistance, int requiredFlags, inout uint steps) · RtTraceAny, adding one to steps for every box and every triangle it tests: how far the ray had to walk, for a heat map or a measure of cost. RtTraceAny is this with a count nothing reads, which compiles away; a kernel that reads it pays a register and an add per step, so keep counting kernels apart from the real ones.
| Parameter | Type | Description |
|---|---|---|
origin |
float3 |
Where the ray starts. |
direction |
float3 |
Its direction. |
maxDistance |
float |
Hits at or beyond this ray parameter don’t count. |
requiredFlags |
int |
The flags an instance needs to block the ray; 0 for every instance. |
steps |
inout uint |
Added to; start it at 0. |
RtTriangleHit() →
bool RtTriangleHit(GpuTriangle tri, float3 origin, float3 direction, float maxDistance) · Whether the ray hits the triangle, from either side, further along than RT_MIN_DISTANCE and closer than maxDistance: Möller–Trumbore, as on the CPU. A value below 10−12 counts as a ray along the triangle’s plane, or a triangle with no area.
| Parameter | Type | Description |
|---|---|---|
tri |
GpuTriangle |
The triangle, a corner and two edges. |
origin |
float3 |
Where the ray starts. |
direction |
float3 |
Its direction. |
maxDistance |
float |
Where the ray ends. |
Defines
RT_FLAG_CASTS_SHADOW →
1 · RayTracingFlags.CastsShadow, for requiredFlags: instances that block shadow rays.
RT_FLAG_VISIBLE_TO_GI →
2 · RayTracingFlags.VisibleToGI, for requiredFlags: instances that take part in indirect light.
RT_MAX_DISTANCE →
3.402823466e+38 · float’s largest finite value, the longest a ray may be. A longer maxDistance, infinity included, is taken as it, as on the CPU, so the two walks test every box alike.
RT_MIN_DISTANCE →
1e-4 · Hits closer than this along a ray are ignored, as on the CPU. A backstop against a surface shadowing itself: still start rays off the surface, see starting off a surface.
Buffers
BlasNodes →
StructuredBuffer<GpuBvhNode> · Every BLAS’s nodes, one after the other. Set by Bind.
Instances →
StructuredBuffer<GpuInstance> · One record per instance, in the order of the scene tree’s leaves. Set by Bind.
TlasNodeCount →
int · How many nodes the scene’s tree has, where its walk stops; 0 for an empty scene, which nothing is traced against. Set by Bind.
TlasNodes →
StructuredBuffer<GpuBvhNode> · The scene’s tree. Set by Bind.
Triangles →
StructuredBuffer<GpuTriangle> · Every BLAS’s triangles, one after the other. Set by Bind.
Structs
GpuBvhNode →
float3 Min; int SkipOrFirst; float3 Max; int PrimitiveCount; · The twin of BvhNode, 32 bytes. A leaf when PrimitiveCount is above 0, its first triangle or instance in SkipOrFirst; otherwise an interior node, its first child the next node and its skip in SkipOrFirst.
GpuInstance →
float3 WorldInverseX; int BlasNodeOffset; float3 WorldInverseY; int BlasNodeEnd; float3 WorldInverseZ; int Flags; float3 WorldInverseW; int Pad; · The twin of GpuInstance, 64 bytes.
GpuTriangle →
float3 V0; float3 Edge1; float3 Edge2; · The twin of GpuTriangle, 36 bytes: a structured buffer packs float3s tightly.