Skip to content
Open
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
10 changes: 8 additions & 2 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3917,10 +3917,16 @@ struct DeviceState {
return bvh_refuse(bvh_vertices, t.vertex_va, vertex_bytes), false;
t.vertex_va = out;
if (t.index_type && t.index_va) {
// The builder reads R16 indices as the aligned 4-byte words that hold them (xdxr_build.comp
// index_of), so their range is checked rounded out to whole words: an index buffer at 2 mod 4,
// or an odd index count, reaches 2 bytes further on either side.
const std::uint64_t index_bytes = 3 * triangles * (t.index_type == 1 ? 2 : 4);
if (!bvh_va(t.index_va, index_bytes, false, out))
const std::uint64_t lead = t.index_type == 1 ? t.index_va & 3 : 0;
const std::uint64_t words = t.index_type == 1 ? (lead + index_bytes + 3) & ~std::uint64_t(3)
: index_bytes;
if (!bvh_va(t.index_va - lead, words, false, out))
return bvh_refuse(bvh_indices, t.index_va, index_bytes), false;
t.index_va = out;
t.index_va = out + lead;
}
if (t.transform_va) {
if (!bvh_va(t.transform_va, 48, false, out))
Expand Down
9 changes: 7 additions & 2 deletions runtime/src/xdxr_build.comp
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,13 @@ vec3 vertex(uint v) {
uint index_of(uint i) {
if (pc.arg2 == 0u) return i;
if (pc.arg2 == 2u) return load(pc.src1, 4u * i);
uint word = load(pc.src1, 4u * (i >> 1)); // u16 indices: D3D12 requires 2-byte alignment; the word is aligned
return (i & 1u) != 0u ? word >> 16 : word & 0xffffu;
// u16 indices: D3D12 requires only 2-byte alignment, so index i is in the low or high half of the aligned word
// that holds it, by its own address (Words is 4-byte aligned; aligning down never borrows from the high word).
// That word may reach 2 bytes past the last index: the layer checks the index range rounded out to whole words
// (d3d12_vulkan_device.cpp, resolve_xdxr_build).
uvec2 a = add(pc.src1, 2u * i);
uint word = Words(uvec2(a.x & ~3u, a.y)).w[0];
return (a.x & 2u) != 0u ? word >> 16 : word & 0xffffu;
}
vec3 transformed(vec3 p) {
if (is_null(pc.src2)) return p;
Expand Down
29 changes: 25 additions & 4 deletions runtime/src/xdxr_build.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ struct XdxrTriangles {
std::uint64_t vertices = 0; // device address
std::uint32_t vertex_stride = 12;
bool xy_only = false; // DXGI_FORMAT_R32G32_FLOAT (z = 0); otherwise R32G32B32_FLOAT
std::uint64_t indices = 0; // device address, 0 = none
// Device address, 0 = none. R16_UINT indices need only 2-byte alignment, but the builder reads them as the
// aligned 4-byte words that hold them (xdxr_build.comp index_of: no 16-bit storage access is assumed), so the
// index range rounded out to whole words must be readable: up to 2 bytes before the first index and after the
// last. The layer checks that range (d3d12_vulkan_device.cpp resolve_xdxr_build) and refuses a build without it.
std::uint64_t indices = 0;
std::uint32_t index_type = 0; // 0 none, 1 R16_UINT, 2 R32_UINT
std::uint32_t triangle_count = 0;
std::uint64_t transform = 0; // device address of a 3x4 row-major float matrix, 0 = none
Expand Down Expand Up @@ -397,11 +401,27 @@ class XdxrBuilder {
dispatch(cb, h, 1, false);
}

public:
// Vulkan guarantees 65535 workgroups along X (maxComputeWorkGroupCount[0]), and some devices (MoltenVK) allow
// no more. A step with more threads (a copy or refit moves one word a thread, so any structure over 16 MiB; a
// build of over 4M elements) is split into dispatches of at most this many groups.
static constexpr std::uint32_t max_groups = 65535;

private:
// One dispatch, then a compute-to-compute dependency so the next step sees its writes. The last step leaves the
// dependency on its results to the caller's barrier (the application's UAV or AS barrier).
// dependency on its results to the caller's barrier (the application's UAV or AS barrier). Past max_groups the
// dispatch is split with vkCmdDispatchBase, whose base offsets gl_WorkGroupID and gl_GlobalInvocationID, so every
// mode sees the ids one dispatch would give it.
void dispatch(VkCommandBuffer cb, const Push& p, std::uint32_t threads, bool barrier = true) {
vk_.vkCmdPushConstants(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(Push), &p);
vk_.vkCmdDispatch(cb, std::uint32_t((std::uint64_t(threads) + 63u) / 64u), 1, 1);
const std::uint64_t groups = (std::uint64_t(threads) + 63u) / 64u;
if (groups <= max_groups) {
vk_.vkCmdDispatch(cb, std::uint32_t(groups), 1, 1);
} else {
for (std::uint64_t first = 0; first < groups; first += max_groups)
vk_.vkCmdDispatchBase(cb, std::uint32_t(first), 0, 0,
std::uint32_t(std::min<std::uint64_t>(max_groups, groups - first)), 1, 1);
}
if (!barrier) return;
VkMemoryBarrier b{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
b.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
Expand All @@ -414,7 +434,7 @@ class XdxrBuilder {
#define LEONIDA_XDXR_FUNCTIONS(X) \
X(vkCreateShaderModule) X(vkDestroyShaderModule) X(vkCreatePipelineLayout) X(vkDestroyPipelineLayout) \
X(vkCreateComputePipelines) X(vkDestroyPipeline) X(vkCmdBindPipeline) X(vkCmdPushConstants) \
X(vkCmdDispatch) X(vkCmdPipelineBarrier)
X(vkCmdDispatch) X(vkCmdDispatchBase) X(vkCmdPipelineBarrier)
#define LEONIDA_XDXR_MEMBER(name) PFN_##name name = nullptr;
LEONIDA_XDXR_FUNCTIONS(LEONIDA_XDXR_MEMBER)
#undef LEONIDA_XDXR_MEMBER
Expand Down Expand Up @@ -445,6 +465,7 @@ class XdxrBuilder {
const aftermath::ShaderName aftermath_name("(runtime) xdxr_build.comp");
if (vk_.vkCreateShaderModule(device_, &module_info, nullptr, &module) != VK_SUCCESS) return false;
VkComputePipelineCreateInfo info{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO};
info.flags = VK_PIPELINE_CREATE_DISPATCH_BASE_BIT; // dispatch() splits large steps with vkCmdDispatchBase
info.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, nullptr, 0, VK_SHADER_STAGE_COMPUTE_BIT,
module, "main", nullptr};
info.layout = layout_;
Expand Down
Loading
Loading