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
5 changes: 4 additions & 1 deletion docs/bvh.md
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,10 @@ elements, so with N elements there are at most (3N - 2) / 4 nodes. `XdxrLayout`
sizes for that many when the build is recorded, and the same numbers answer
`GetRaytracingAccelerationStructurePrebuildInfo`. The header's `totalSizeIn64BNodes` counts only the nodes the build
wrote.
A triangle with a NaN vertex gets an empty box (D3D12 treats it as inactive). So do an instance with mask 0, one with
A triangle with a NaN vertex gets an empty box (D3D12 treats it as inactive). So does a triangle with a vertex
index at or past the geometry's `VertexCount` (every triangle when it is 0): the builder reads no vertex for it, as
the index would address past the vertex buffer, possibly by gigabytes (`vk_xdxr_build_test` case
`blas_vertex_count`). So do an instance with mask 0, one with
no BLAS and one with a singular transform. An instance record holds its BLAS address in 256-byte units in 32 bits,
so BLAS addresses must be below 2^40, as console GPU addresses are. Where device addresses are wider (llvmpipe's are
host pointers), the builder takes a bias that is added to the addresses read from instance descs.
Expand Down
4 changes: 2 additions & 2 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3086,8 +3086,8 @@ struct DeviceState {
for (auto& t : b.triangles) {
const std::uint64_t triangles = (t.index_type ? t.index_count : t.vertex_count) / 3;
if (!triangles) continue;
// Unindexed: three vertices a triangle. Indexed: the VertexCount the title declared (indices past
// it read past the buffer, as on the console).
// Unindexed: three vertices a triangle. Indexed: the VertexCount the title declared (an index past
// it is not read: xdxr_build.comp vertex() makes its triangle inactive).
const std::uint64_t vertices = t.index_type ? t.vertex_count : 3 * triangles;
const std::uint64_t element = t.vertex_format == VK_FORMAT_R32G32_SFLOAT ? 8 : 12;
const std::uint64_t vertex_bytes = vertices ? (vertices - 1) * t.vertex_stride + element : 0;
Expand Down
1 change: 1 addition & 0 deletions runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,7 @@ class VulkanContext {
XdxrTriangles g;
g.vertices = t.vertex_va ? base + t.vertex_va : 0;
g.vertex_stride = t.vertex_stride;
g.vertex_count = t.vertex_count;
g.xy_only = t.vertex_format == VK_FORMAT_R32G32_SFLOAT;
g.indices = t.index_type && t.index_va ? base + t.index_va : 0;
g.index_type = t.index_type;
Expand Down
51 changes: 49 additions & 2 deletions runtime/src/xdxr_build.comp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ layout(buffer_reference, std430, buffer_reference_align = 4) buffer Words { uint
const uint MODE_INIT = 0u, MODE_TRIANGLES = 1u, MODE_INSTANCES = 2u, MODE_MORTON = 3u, MODE_KARRAS = 4u,
MODE_NODES = 5u, MODE_HEADER = 6u, MODE_RADIX_COUNT = 7u, MODE_RADIX_SCAN = 8u, MODE_RADIX_SCATTER = 9u,
MODE_SMALL_SORT = 10u, MODE_COPY_AS = 12u, MODE_POSTBUILD = 13u, MODE_KEPT = 14u,
MODE_KEPT_SCAN = 15u, MODE_KEPT_INDEX = 16u, MODE_PROPAGATE = 17u, MODE_CLEAR = 18u;
MODE_KEPT_SCAN = 15u, MODE_KEPT_INDEX = 16u, MODE_PROPAGATE = 17u, MODE_CLEAR = 18u, MODE_CHECK = 19u;
const uint SMALL_SORT_MAX = 1024u; // xdxr_build.h XdxrLayout::small_sort_max
const uint RADIX_BITS = 4u, RADIX_BINS = 16u, RADIX_PASSES = 8u;
const uint RADIX_TILE = 256u; // elements per workgroup in count and scatter: 4 chunks of 64
Expand All @@ -85,6 +85,9 @@ const uint TRI0 = 0u, TRI1 = 1u, BOX16 = 4u, BOX32 = 5u, INSTANCE = 6u;
const uint LEAF = 0x80000000u; // a binary child that is a sorted element, not an inner node
const uint REMAP_IDENTITY = 0x09u;
const uint FLAG_TLAS = 1u, FLAG_BOX16 = 2u, FLAG_PAIR = 4u, FLAG_TRANSLATE = 8u; // pc.flags; xdxr_build.h flag_*
// An update records both tails: FLAG_IF_REFIT steps run only when MODE_CHECK found the source a structure of this
// layout (scratch word SCRATCH_REFIT = 1), FLAG_IF_BUILD steps (a full build) only when it did not.
const uint FLAG_IF_REFIT = 16u, FLAG_IF_BUILD = 32u;
const uint FLAG_BOTTOM_LEVEL = 0x80000000u, VERSION_2403 = 6u, INST_IDENTITY_ROTATION = 0x80000000u,
INST_FLAGS_MASK = 0xc0000000u;

Expand All @@ -111,6 +114,7 @@ layout(push_constant) uniform Push {
uint flags; // FLAG_TLAS: elements are 128-byte instances; FLAG_BOX16: inner nodes are 64-byte BOX16;
// FLAG_PAIR: pair sibling triangles that share an edge into TRI0/TRI1 leaves;
// FLAG_TRANSLATE (instances): BLAS addresses go through blas_address
uint vertex_count; // triangles: the desc's VertexCount; a vertex index at or past it reads nothing
} pc;

// ---- 64-bit addresses as uvec2 ----
Expand Down Expand Up @@ -141,6 +145,7 @@ uvec2 sub64(uvec2 a, uvec2 b) {
// counts, their exclusive prefix sums, element boxes, a box accumulator per node, the binary tree ----
const uint SCRATCH_KEYS = 64u;
const uint SCRATCH_NODE_COUNT = 24u;
const uint SCRATCH_REFIT = 28u; // MODE_CHECK's verdict for an update
uint radix_tiles() { return (pc.sort_size + RADIX_TILE - 1u) / RADIX_TILE; }
uint keys_off() { return SCRATCH_KEYS; }
uint vals_off() { return SCRATCH_KEYS + 4u * pc.sort_size; }
Expand Down Expand Up @@ -201,6 +206,21 @@ uint element_ptr(uint e) { return ((pc.elements_off + e * element_size()) >> 6)
uint node_ptr(uint n) { return ((pc.nodes_off + n * node_size()) >> 6) << 3 | (is_box16() ? BOX16 : BOX32); }
uint ptr_unit(uint p) { return p >> 3; }
bool ptr_is_node(uint p) { return (p & 7u) == BOX16 || (p & 7u) == BOX32; }
// A pointer read from the structure (an update's source is the title's, possibly garbage) that names one of this
// layout's nodes: the layout's node type, on a node boundary, below the node count. Nothing else is followed.
bool layout_node(uint p) {
if ((p & 7u) != (is_box16() ? BOX16 : BOX32)) return false;
uint o = ptr_unit(p) * 64u;
if (ptr_unit(p) >= (1u << 26) || o < pc.nodes_off) return false;
uint rel = o - pc.nodes_off;
return rel % node_size() == 0u && rel / node_size() < pc.arg0;
}
// The parent-table slot of a record pointer, or INVALID when its unit is outside the body the table covers.
uint parent_slot(uint p) {
uint first = pc.elements_off / 64u, unit = ptr_unit(p);
if (unit < first || unit >= pc.total_size / 64u) return INVALID;
return pc.parents_off + 4u * (unit - first);
}

// ---- BOX16 bounds: six IEEE halves in three words (min.x | min.y << 16, min.z | max.x << 16, max.y | max.z << 16;
// docs/bvh.md 3.1, XdxrDecompressBox16) ----
Expand Down Expand Up @@ -232,7 +252,10 @@ void store_box16(uint o, vec3 lo, vec3 hi) {
}

// ---- MODE_TRIANGLES ----
// An index at or past VertexCount (garbage in the index buffer, or VertexCount 0) would read past the vertex buffer,
// possibly gigabytes past it: the vertex is NaN instead, which makes its triangle inactive (build_triangle).
vec3 vertex(uint v) {
if (v >= pc.vertex_count) return vec3(uintBitsToFloat(0x7fc00000u));
uvec2 a = add(pc.src0, v * pc.arg0);
float x = uintBitsToFloat(load(a, 0u)), y = uintBitsToFloat(load(a, 4u));
float z = pc.arg1 == 1u ? 0.0 : uintBitsToFloat(load(a, 8u));
Expand Down Expand Up @@ -732,7 +755,13 @@ void build_node(uint i) {
}

// ---- MODE_PROPAGATE ----
uint parent_of(uint p) { return load(pc.dst, pc.parents_off + 4u * (ptr_unit(p) - pc.elements_off / 64u)); }
// INVALID (the walk ends) for anything but one of the layout's nodes: an update reads the source's parent table.
uint parent_of(uint p) {
uint slot = parent_slot(p);
if (slot == INVALID) return INVALID;
uint parent = load(pc.dst, slot);
return layout_node(parent) ? parent : INVALID;
}
bool ptr_box16(uint p) { return (p & 7u) == BOX16; }
uint node_index(uint p) { return (ptr_unit(p) * 64u - pc.nodes_off) / (ptr_box16(p) ? 64u : 128u); }
void accum_box(uint node, out vec3 lo, out vec3 hi) {
Expand Down Expand Up @@ -802,8 +831,26 @@ void build_header() {
store(pc.dst, 60u, 0u); // compressionParams: uncompressed
}

// ---- MODE_CHECK: is the structure an update refits (already copied to dst) one this layout's build wrote? The
// header words build_header writes that the layout fixes, and a size within the layout. A title may pass a source it
// never built (D3D12 leaves that undefined); then the update is a full build instead and reads nothing of it. ----
bool refit_source_ok() {
uint size = (load(pc.dst, 4u) + 1u) * 64u;
return load(pc.dst, 0u) == ((is_tlas() ? 0u : FLAG_BOTTOM_LEVEL) | VERSION_2403) &&
load(pc.dst, 12u) == pc.elements && load(pc.dst, 40u) == pc.nodes_off && load(pc.dst, 48u) == node_ptr(0u) &&
load(pc.dst, 52u) == pc.elements_off && load(pc.dst, 56u) == pc.parents_off && load(pc.dst, 4u) < (1u << 26) &&
size > pc.nodes_off && size <= pc.total_size;
}

void main() {
uint i = gl_GlobalInvocationID.x;
// uniform across the dispatch, so a workgroup that uses barriers leaves as a whole
if ((pc.flags & (FLAG_IF_REFIT | FLAG_IF_BUILD)) != 0u &&
(load(pc.scratch, SCRATCH_REFIT) == 1u) != ((pc.flags & FLAG_IF_REFIT) != 0u)) return;
if (pc.mode == MODE_CHECK) {
if (i == 0u) store(pc.scratch, SCRATCH_REFIT, refit_source_ok() ? 1u : 0u);
return;
}
// the radix and kept modes use shared memory and barriers: every invocation of a workgroup takes part
if (pc.mode == MODE_RADIX_COUNT) { radix_count(gl_WorkGroupID.x, gl_LocalInvocationID.x); return; }
if (pc.mode == MODE_RADIX_SCAN) {
Expand Down
59 changes: 45 additions & 14 deletions runtime/src/xdxr_build.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ struct XdxrTriangles {
std::uint32_t triangle_count = 0;
std::uint64_t transform = 0; // device address of a 3x4 row-major float matrix, 0 = none
std::uint32_t flags = 0; // D3D12_RAYTRACING_GEOMETRY_FLAGS (bit 0 opaque)
// VertexCount: a triangle with a vertex index at or past it is inactive and reads no vertex (0: every triangle);
// the default bounds nothing, for callers with no desc.
std::uint32_t vertex_count = 0xffffffffu;
};

// How a top-level build reaches the BLAS addresses its instance descs hold (xdxr_build.comp blas_address). They are
Expand Down Expand Up @@ -172,11 +175,12 @@ class XdxrBuilder {
t.arg0 = geo.vertex_stride;
t.arg1 = geo.xy_only ? 1u : 0u;
t.arg2 = geo.index_type;
t.vertex_count = geo.vertex_count;
t.geometry = std::uint32_t(g) << 8 | (geo.flags & 0xffu);
dispatch(cb, t, t.count);
first += geo.triangle_count;
}
if (update_from) refit_finish(cb, layout, p); else finish(cb, layout, p);
if (update_from) update_finish(cb, layout, p); else finish(cb, layout, p);
return true;
}

Expand Down Expand Up @@ -204,7 +208,7 @@ class XdxrBuilder {
}
dispatch(cb, t, t.count);
}
if (update_from) refit_finish(cb, layout, p); else finish(cb, layout, p);
if (update_from) update_finish(cb, layout, p); else finish(cb, layout, p);
return true;
}

Expand Down Expand Up @@ -246,16 +250,19 @@ class XdxrBuilder {
mode_init = 0, mode_triangles = 1, mode_instances = 2, mode_morton = 3, mode_karras = 4, mode_nodes = 5,
mode_header = 6, mode_radix_count = 7, mode_radix_scan = 8, mode_radix_scatter = 9, mode_small_sort = 10,
mode_copy_as = 12, mode_postbuild = 13, mode_kept = 14, mode_kept_scan = 15,
mode_kept_index = 16, mode_propagate = 17, mode_clear = 18
mode_kept_index = 16, mode_propagate = 17, mode_clear = 18, mode_check = 19
};
struct Addr { std::uint32_t lo = 0, hi = 0; };
struct Push { // xdxr_build.comp `Push`, std430 push-constant layout
Addr dst, scratch, src0, src1, src2, bias;
std::uint32_t mode = 0, count = 0, first = 0, arg0 = 0, arg1 = 0, arg2 = 0, geometry = 0, elements = 0,
sort_size = 0, elements_off = 0, nodes_off = 0, parents_off = 0, total_size = 0, flags = 0;
sort_size = 0, elements_off = 0, nodes_off = 0, parents_off = 0, total_size = 0, flags = 0,
vertex_count = 0;
};
enum : std::uint32_t { flag_tlas = 1, flag_box16 = 2, flag_pair = 4, flag_translate = 8 }; // xdxr_build.comp FLAG_*
static_assert(sizeof(Push) == 104, "push constants must match xdxr_build.comp");
enum : std::uint32_t { // xdxr_build.comp FLAG_*
flag_tlas = 1, flag_box16 = 2, flag_pair = 4, flag_translate = 8, flag_if_refit = 16, flag_if_build = 32
};
static_assert(sizeof(Push) == 108, "push constants must match xdxr_build.comp");

static Addr split(std::uint64_t a) { return {std::uint32_t(a), std::uint32_t(a >> 32)}; }

Expand Down Expand Up @@ -284,19 +291,43 @@ class XdxrBuilder {

// An update: the source's tree is copied unless it is the destination, as many bytes as its header says (a
// compacted source is shorter than the layout). Its parent table stays as it is, so there is no MODE_INIT.
//
// Then MODE_CHECK: the source may not be a structure of this layout at all (a title passing one it never built,
// which D3D12 leaves undefined), and its pointers would send the refit anywhere. The full build's MODE_INIT runs
// only when the check fails (flag_if_build), and refit_finish / finish are both recorded with the flag that picks
// one of them (update_finish).
void refit_begin(VkCommandBuffer cb, const XdxrLayout& l, const Push& p, std::uint64_t source) {
vk_.vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_);
const std::uint64_t dst = std::uint64_t(p.dst.hi) << 32 | p.dst.lo;
if (source == dst) return;
Push c = p;
c.mode = mode_copy_as;
c.count = l.size / 4;
c.src0 = split(source);
dispatch(cb, c, c.count);
if (source != dst) {
Push c = p;
c.mode = mode_copy_as;
c.count = l.size / 4;
c.src0 = split(source);
dispatch(cb, c, c.count);
}
Push k = p;
k.mode = mode_check;
k.count = 1;
dispatch(cb, k, 1);
Push init = p;
init.flags |= flag_if_build;
init.mode = mode_init;
init.count = l.parent_entries > 16 ? l.parent_entries : 16;
dispatch(cb, init, init.count);
}

void update_finish(VkCommandBuffer cb, const XdxrLayout& l, const Push& p) {
Push r = p;
r.flags |= flag_if_refit;
refit_finish(cb, l, r, true);
Push b = p;
b.flags |= flag_if_build;
finish(cb, l, b);
}

// Boxes bottom-up through the existing nodes, then the header (its bounds change; nothing else does).
void refit_finish(VkCommandBuffer cb, const XdxrLayout& l, const Push& p) {
void refit_finish(VkCommandBuffer cb, const XdxrLayout& l, const Push& p, bool barrier_after = false) {
Push c = p;
c.mode = mode_clear;
c.count = l.nodes;
Expand All @@ -306,7 +337,7 @@ class XdxrBuilder {
h.mode = mode_header;
h.count = 1;
h.arg2 = 1; // an update: the bounds only
dispatch(cb, h, 1, false);
dispatch(cb, h, 1, barrier_after);
}

// `update`: the elements' pointers are their own (an update never pairs), not the ones MODE_NODES recorded.
Expand Down
Loading
Loading