From 9e8d04df6e3bc241f70fb9f08346d0b976186e04 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 19:50:10 +0100 Subject: [PATCH 01/10] startup: name the causes of a refused placed build and accept PERFORM_UPDATE with a source structure --- CMakeLists.txt | 7 ++ docs/experiments.md | 6 ++ src/startup_stubs/d3d12_xs_device.cpp | 53 +++++++++---- src/startup_stubs/placed_build.h | 90 ++++++++++++++++++++++ tests/dxr_placed_build.h | 55 ++++++++++++- tests/placed_build_causes_check.cpp | 106 ++++++++++++++++++++++++++ 6 files changed, 302 insertions(+), 15 deletions(-) create mode 100644 tests/placed_build_causes_check.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 98b7c61063..0aa0f68298 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,6 +143,13 @@ add_test(NAME era_physical_owns_last_page_control COMMAND era_physical_last_page set_tests_properties(era_physical_owns_last_page_control PROPERTIES PASS_REGULAR_EXPRESSION "FAIL: owns: last page") +# Device-free rules of the placed acceleration-structure build (#870): standard C++17 only. +add_executable(placed_build_causes_check tests/placed_build_causes_check.cpp) +target_include_directories(placed_build_causes_check PRIVATE src) +target_compile_features(placed_build_causes_check PRIVATE cxx_std_17) +add_test(NAME placed_build_causes COMMAND placed_build_causes_check) +set_tests_properties(placed_build_causes PROPERTIES TIMEOUT 30) + # Desktop xmem.dll for native and mapped callers. add_library(xmem_shim SHARED src/xmem_shim.cpp src/xmem_shim.def src/xmem_gpu_usage.cpp src/adapter_identity.cpp src/era_heap.cpp src/era_query.cpp src/era_physical.cpp src/era_title_gpu.cpp src/era_gpu_alias.cpp src/era_title_va.cpp src/xmem_mapped_pages.cpp) diff --git a/docs/experiments.md b/docs/experiments.md index be66c1b5fa..9a71757a01 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -7818,6 +7818,12 @@ cmake --build --preset release --target offscreen_check ctest --preset release --no-tests=error --output-on-failure -R '^dxr_placed_build_' ``` +The same resolver now names why it refused a build and accepts `PERFORM_UPDATE` with `SourceAccelerationStructureData` (#870, #1758). Each refusal still returns `E_INVALIDARG`, publishes no address and logs one line per cause for the first refusal of that kind: `x2w-dxr placed build refused: ( at title 0x...)`. The causes are `unaligned`, `not_placed`, `wrong_state` and `range` for the destination and scratch, and for the source `update_without_source`, `source_without_update`, `source_unaligned`, `source_not_placed`, `source_wrong_state`, `source_range` and `source_dest_partial_overlap`. An update takes its source through the same placement lookup as the destination (a live placed buffer created in the acceleration-structure state, 256-byte aligned), is sized by `UpdateScratchDataSizeInBytes` instead of `ScratchDataSizeInBytes`, and may run in place (source and destination the same address) or write another structure; a source that partly overlaps the destination is refused. The resolver cannot see whether the source was built with `ALLOW_UPDATE`, which the update needs (see the GTX 1060 note above). `placed_build_causes` checks the device-free rules (`src/startup_stubs/placed_build.h`) on any platform, and `dxr_placed_build_{hardware,warp}` now also resolves six refused updates, an in-place update and an out-of-place one. Limits: verified on WARP (Debug and /O2); the hardware variant was not run, and no update build has been recorded or dispatched. + +```powershell +ctest --preset release --no-tests=error --output-on-failure -R '^(placed_build_causes|dxr_placed_build_)' +``` + In this configuration all `bvh_*_warp`, `dxr_*_warp` and `bvh_differential_warp` tests pass, and the hardware wave64 check skips at its 32-lane range with SM 6.6 available. `bvh_wave64_warp` now runs and fails at field 25, `WaveReadLaneAt(v, (lane*7+3) % 64)`. In two groups, all 128 lanes received the value of lane m where `7m+3 ≡ lane (mod 64)`, which is the inverse of the requested shuffle. Field 24's shuffle, the mirror read and the native-width run pass. At WARP's native width of 8, `7n+3 ≡ 3-n (mod 8)` is self-inverse, so the default `bvh_waves_warp` cannot show the difference. `wave_shuffle_warp` narrows the cause. WARP 1.0.22 runs a forced wave as blocks of its native 8 lanes, and it picks the source block using the request of the wrong lane: the lane of the reader's block that sits at the requested position. For this shuffle that equals the inverse permutation. A rotation by one lane gets 16 of 64 reads wrong at widths 16, 32 and 64, and none of them match its inverse. Reads where every lane of a block requests the same source block are unaffected. [Open question](https://github.com/R0Y00/Xbox2Windows/issues/145) diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 902309418d..e3933a9a52 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -3765,27 +3765,52 @@ HRESULT x2w_startup::resolve_placed_build(void* guest_device, UINT64 dest, UINT6 const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO& sizes, D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& build) { - if (build.SourceAccelerationStructureData || - (build.Inputs.Flags & D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE)) + using Cause = x2w_startup::PlacedBuildCause; + // One log line per cause, for the first refusal of that kind in the process. + static LONG logged[static_cast(Cause::count)]; + const auto refuse = [&](Cause cause, const char* role, UINT64 address) { + if (first_calls(logged[static_cast(cause)], 1)) + log("x2w-dxr placed build refused: %s (%s at title 0x%llx)", x2w_startup::cause_name(cause), role, + static_cast(address)); return E_INVALIDARG; - constexpr UINT64 alignment = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT; - const auto translate = [&](UINT64 address, UINT64 bytes, D3D12_RESOURCE_STATES state) -> UINT64 { - if (address % alignment) return 0; + }; + const bool update = (build.Inputs.Flags & D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE) != 0; + const UINT64 source = build.SourceAccelerationStructureData; + if (const Cause shape = x2w_startup::check_update_shape(update, source, dest, sizes.ResultDataMaxSizeInBytes); + shape != Cause::ok) + return refuse(shape, "source", source); + constexpr UINT64 alignment = x2w_startup::build_alignment; + struct Translated { + Cause cause; + UINT64 gpu; + }; + const auto translate = [&](UINT64 address, UINT64 bytes, D3D12_RESOURCE_STATES state) -> Translated { + if (address % alignment) return {Cause::unaligned, 0}; const auto found = placed_buffer_range(static_cast(guest_device), address, bytes); - if (!found.owner || found.reject) return 0; + if (!found.owner || found.reject) return {Cause::not_placed, 0}; const auto* info = resource_info(found.owner); - if (!info || info->initial_state != state) return 0; + if (!info || info->initial_state != state) return {Cause::wrong_state, 0}; const auto base = reinterpret_cast(found.owner->own().title_memory); const auto gpu = static_cast(found.owner->own().real)->GetGPUVirtualAddress(); const auto offset = address - base; - if (!gpu || offset > UINT64_MAX - gpu || (gpu + offset) % alignment) return 0; - return gpu + offset; + if (!gpu || offset > UINT64_MAX - gpu || (gpu + offset) % alignment) return {Cause::range, 0}; + return {Cause::ok, gpu + offset}; }; - const auto out = translate(dest, sizes.ResultDataMaxSizeInBytes, D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE); - const auto temp = translate(scratch, sizes.ScratchDataSizeInBytes, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); - if (!out || !temp) return E_INVALIDARG; - build.DestAccelerationStructureData = out; - build.ScratchAccelerationStructureData = temp; + constexpr auto as_state = D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE; + const auto out = translate(dest, sizes.ResultDataMaxSizeInBytes, as_state); + if (out.cause != Cause::ok) return refuse(out.cause, "dest", dest); + // An update is sized by its own scratch requirement, a full build by the build one. + const auto temp = translate(scratch, update ? sizes.UpdateScratchDataSizeInBytes : sizes.ScratchDataSizeInBytes, + D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + if (temp.cause != Cause::ok) return refuse(temp.cause, "scratch", scratch); + Translated from{Cause::ok, 0}; + if (update) { + from = translate(source, sizes.ResultDataMaxSizeInBytes, as_state); + if (from.cause != Cause::ok) return refuse(x2w_startup::as_source_cause(from.cause), "source", source); + } + build.DestAccelerationStructureData = out.gpu; + build.ScratchAccelerationStructureData = temp.gpu; + if (update) build.SourceAccelerationStructureData = from.gpu; return S_OK; } diff --git a/src/startup_stubs/placed_build.h b/src/startup_stubs/placed_build.h index a8967cbe41..13f2fb11a8 100644 --- a/src/startup_stubs/placed_build.h +++ b/src/startup_stubs/placed_build.h @@ -1,5 +1,91 @@ #pragma once +#include +#include + +namespace x2w_startup { +// Why a placed acceleration-structure build was refused. The first part of this header is standard C++ only, so +// the rules below are checked on any platform; the resolver that applies them to a device follows it. +enum class PlacedBuildCause : unsigned { + ok, + unaligned, // destination or scratch is not 256-byte aligned + not_placed, // no live placed buffer covers the range + wrong_state, // the buffer was not created in the state its role needs + range, // the range overflows or its translated address is unusable + update_without_source, // PERFORM_UPDATE with no source structure + source_without_update, // a source structure without PERFORM_UPDATE + source_unaligned, + source_not_placed, + source_wrong_state, + source_range, + source_dest_partial_overlap, // the source and the destination overlap without being the same structure + count +}; + +constexpr const char* cause_name(PlacedBuildCause cause) +{ + switch (cause) { + case PlacedBuildCause::ok: return "ok"; + case PlacedBuildCause::unaligned: return "unaligned"; + case PlacedBuildCause::not_placed: return "not_placed"; + case PlacedBuildCause::wrong_state: return "wrong_state"; + case PlacedBuildCause::range: return "range"; + case PlacedBuildCause::update_without_source: return "update_without_source"; + case PlacedBuildCause::source_without_update: return "source_without_update"; + case PlacedBuildCause::source_unaligned: return "source_unaligned"; + case PlacedBuildCause::source_not_placed: return "source_not_placed"; + case PlacedBuildCause::source_wrong_state: return "source_wrong_state"; + case PlacedBuildCause::source_range: return "source_range"; + case PlacedBuildCause::source_dest_partial_overlap: return "source_dest_partial_overlap"; + case PlacedBuildCause::count: break; + } + return "unknown"; +} + +// The cause a lookup of the destination or scratch gives, as it reads for the source structure. +constexpr PlacedBuildCause as_source_cause(PlacedBuildCause cause) +{ + switch (cause) { + case PlacedBuildCause::unaligned: return PlacedBuildCause::source_unaligned; + case PlacedBuildCause::not_placed: return PlacedBuildCause::source_not_placed; + case PlacedBuildCause::wrong_state: return PlacedBuildCause::source_wrong_state; + case PlacedBuildCause::range: return PlacedBuildCause::source_range; + default: return cause; + } +} + +constexpr std::uint64_t build_alignment = 256; // D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT + +// The last byte of [start, start + bytes), clamped to the top of the address space. +constexpr std::uint64_t last_byte(std::uint64_t start, std::uint64_t bytes) +{ + return bytes - 1 > UINT64_MAX - start ? UINT64_MAX : start + (bytes - 1); +} + +// Whether [a, a + a_bytes) and [b, b + b_bytes) share a byte. An empty range shares none. +constexpr bool ranges_overlap(std::uint64_t a, std::uint64_t a_bytes, std::uint64_t b, std::uint64_t b_bytes) +{ + if (!a_bytes || !b_bytes) return false; + return a <= last_byte(b, b_bytes) && b <= last_byte(a, a_bytes); +} + +// The rules that need no device: how PERFORM_UPDATE, the source structure and the destination relate. An update +// may run in place (the source is the destination) or write a different structure, but the two may not partly +// overlap. result_bytes is the size of both structures. +constexpr PlacedBuildCause check_update_shape(bool perform_update, std::uint64_t source, std::uint64_t dest, + std::uint64_t result_bytes) +{ + if (perform_update && !source) return PlacedBuildCause::update_without_source; + if (!perform_update && source) return PlacedBuildCause::source_without_update; + if (!source) return PlacedBuildCause::ok; + if (source % build_alignment) return PlacedBuildCause::source_unaligned; + if (source != dest && ranges_overlap(source, result_bytes, dest, result_bytes)) + return PlacedBuildCause::source_dest_partial_overlap; + return PlacedBuildCause::ok; +} +} // namespace x2w_startup + +#if defined(_WIN32) #include namespace x2w_startup { @@ -8,7 +94,11 @@ void attach_device(ID3D12Device* device, void** wrapped); // Internal desktop build preparation. The caller retains the registered resources // through execution and supplies prebuild sizes for these exact BLAS/TLAS inputs. +// With PERFORM_UPDATE, build.SourceAccelerationStructureData names the structure to update, in place or not, +// and the scratch size is sizes.UpdateScratchDataSizeInBytes. A refusal returns E_INVALIDARG, leaves build +// untouched and logs its PlacedBuildCause once per cause. HRESULT resolve_placed_build(void* guest_device, UINT64 dest, UINT64 scratch, const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO& sizes, D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& build); } +#endif diff --git a/tests/dxr_placed_build.h b/tests/dxr_placed_build.h index 9e80ce341d..3117f417c8 100644 --- a/tests/dxr_placed_build.h +++ b/tests/dxr_placed_build.h @@ -4,6 +4,8 @@ #include "startup_stubs/placed_build.h" #include #include +#include +#include namespace dxr_fixture { // These are the existing slot-133 and IUnknown contracts, called on the @@ -15,7 +17,7 @@ class PlacedBuildScene { ~Buffer() { wrapper.Reset(); if (memory) VirtualFree(memory, 0, MEM_RELEASE); } }; Microsoft::WRL::ComPtr guest_; - std::array buffers_; + std::array buffers_; public: // Wrong-routing control (issue #874): also build a decoy BLAS from another triangle into its own placed @@ -79,6 +81,7 @@ class PlacedBuildScene { must_reject(unmapped, ts); must_reject(tlas, short_scratch); must_reject(ts, tlas); + check_updates(top, top_size, tlas, ts); check(x2w_startup::resolve_placed_build(guest_.Get(), blas, bs, bottom_size, bottom), "Resolve placed BLAS"); check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, ts, top_size, top), "Resolve placed TLAS"); const auto verify_offset = [&](const auto& build, UINT64 dest, UINT64 scratch, const auto& sizes) { @@ -96,6 +99,56 @@ class PlacedBuildScene { top_size.ResultDataMaxSizeInBytes, top_size.ScratchDataSizeInBytes); } + // PERFORM_UPDATE with SourceAccelerationStructureData, through the same resolver. The scratch of an update + // is sized by UpdateScratchDataSizeInBytes; the fixture sets it to the build scratch size so that the one + // scratch buffer placed for the build fits. + void check_updates(const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& top, + const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO& top_size, UINT64 tlas, UINT64 scratch) { + constexpr auto update_flag = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE; + constexpr UINT64 dest_offset = 512, unmapped = 0x1000; + auto sizes = top_size; + sizes.UpdateScratchDataSizeInBytes = top_size.ScratchDataSizeInBytes; + const auto request = [&](UINT64 source, bool update) { + auto build = top; + build.Inputs.Flags = update ? (build.Inputs.Flags | update_flag) : (build.Inputs.Flags & ~update_flag); + build.SourceAccelerationStructureData = source; + build.DestAccelerationStructureData = build.ScratchAccelerationStructureData = 0; + return build; + }; + // Each refusal returns E_INVALIDARG and publishes neither address, and keeps the source as the title gave it. + const auto must_reject = [&](UINT64 source, bool update, const char* what) { + auto build = request(source, update); + if (x2w_startup::resolve_placed_build(guest_.Get(), tlas, scratch, sizes, build) != E_INVALIDARG || + build.DestAccelerationStructureData || build.ScratchAccelerationStructureData || + build.SourceAccelerationStructureData != source) + throw std::runtime_error(std::string("Invalid DXR update was accepted or partially published: ") + what); + }; + must_reject(0, true, "update without a source"); + must_reject(tlas, false, "source without PERFORM_UPDATE"); + must_reject(tlas + 1, true, "unaligned source"); + must_reject(unmapped, true, "source outside the placed buffers"); + must_reject(scratch, true, "source in a buffer that is not an acceleration structure"); + must_reject(tlas + 256, true, "source partly overlapping the destination"); + + // In place: the source is the destination. + auto in_place = request(tlas, true); + check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, scratch, sizes, in_place), "Resolve in-place update"); + if (!in_place.DestAccelerationStructureData || in_place.SourceAccelerationStructureData != in_place.DestAccelerationStructureData) + throw std::runtime_error("An in-place update must translate source and destination to the same address"); + + // Out of place: another placed acceleration structure, its interior offset kept. + const auto other = place(7, dest_offset + top_size.ResultDataMaxSizeInBytes, + D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE) + dest_offset; + auto moved = request(other, true); + check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, scratch, sizes, moved), "Resolve out-of-place update"); + auto base = request(0, false); + check(x2w_startup::resolve_placed_build(guest_.Get(), other - dest_offset, scratch - 768, sizes, base), + "Resolve the source placement base"); + if (moved.SourceAccelerationStructureData != base.DestAccelerationStructureData + dest_offset || + moved.SourceAccelerationStructureData == moved.DestAccelerationStructureData) + throw std::runtime_error("An out-of-place update lost the source address or aliased the destination"); + } + // The decoy BLAS for the wrong-routing control, placed and resolved through the same slot-133 path. void prepare_decoy(const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO& bottom_size, D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& decoy) { diff --git a/tests/placed_build_causes_check.cpp b/tests/placed_build_causes_check.cpp new file mode 100644 index 0000000000..2c86051070 --- /dev/null +++ b/tests/placed_build_causes_check.cpp @@ -0,0 +1,106 @@ +// The device-free rules of the placed acceleration-structure build (src/startup_stubs/placed_build.h). Standard +// C++17 only. +#include +#include +#include +#include + +#include "startup_stubs/placed_build.h" + +namespace { +using x2w_startup::PlacedBuildCause; +using Cause = PlacedBuildCause; +using x2w_startup::check_update_shape; +using x2w_startup::ranges_overlap; + +#define CHECK(condition) \ + do { \ + if (!(condition)) { \ + std::fprintf(stderr, "%s:%d: check failed: %s\n", __FILE__, __LINE__, #condition); \ + std::exit(1); \ + } \ + } while (0) + +constexpr std::uint64_t size = 4096; +constexpr std::uint64_t base = 0x10000; + +void causes_have_distinct_names() +{ + for (unsigned a = 0; a < static_cast(Cause::count); ++a) { + const std::string_view name = x2w_startup::cause_name(static_cast(a)); + CHECK(name != "unknown"); + for (unsigned b = a + 1; b < static_cast(Cause::count); ++b) + CHECK(name != x2w_startup::cause_name(static_cast(b))); + } + CHECK(std::string_view(x2w_startup::cause_name(Cause::count)) == "unknown"); +} + +void a_plain_build_has_no_source() +{ + CHECK(check_update_shape(false, 0, base, size) == Cause::ok); + CHECK(check_update_shape(false, base, base + size, size) == Cause::source_without_update); + CHECK(check_update_shape(true, 0, base, size) == Cause::update_without_source); +} + +void an_update_runs_in_place_or_elsewhere() +{ + CHECK(check_update_shape(true, base, base, size) == Cause::ok); // in place + CHECK(check_update_shape(true, base, base + size, size) == Cause::ok); // next to it, touching + CHECK(check_update_shape(true, base + 4 * size, base, size) == Cause::ok); // far above + CHECK(check_update_shape(true, base, base + 4 * size, size) == Cause::ok); // far below +} + +void partial_overlap_is_refused() +{ + CHECK(check_update_shape(true, base + 256, base, size) == Cause::source_dest_partial_overlap); + CHECK(check_update_shape(true, base, base + 256, size) == Cause::source_dest_partial_overlap); + CHECK(check_update_shape(true, base + size - 256, base, size) == Cause::source_dest_partial_overlap); // last 256 bytes + CHECK(check_update_shape(true, base + size, base, size) == Cause::ok); // first byte past +} + +void the_source_must_be_aligned() +{ + CHECK(check_update_shape(true, base + 1, base + 8 * size, size) == Cause::source_unaligned); + CHECK(check_update_shape(true, base + 255, base + 8 * size, size) == Cause::source_unaligned); + CHECK(check_update_shape(true, base + 256, base + 8 * size, size) == Cause::ok); + // Alignment is named before overlap. + CHECK(check_update_shape(true, base + 1, base, size) == Cause::source_unaligned); +} + +void overlap_edges() +{ + CHECK(!ranges_overlap(base, 0, base, size)); + CHECK(!ranges_overlap(base, size, base, 0)); + CHECK(ranges_overlap(base, 1, base, 1)); + CHECK(!ranges_overlap(base, 1, base + 1, 1)); + CHECK(ranges_overlap(base, size, base + size - 1, 1)); + CHECK(!ranges_overlap(base, size, base + size, 1)); + // A range that would pass the top of the address space stops at it. + CHECK(ranges_overlap(UINT64_MAX - 15, 64, UINT64_MAX, 1)); + CHECK(!ranges_overlap(UINT64_MAX - 15, 64, 0, 16)); + CHECK(ranges_overlap(0, UINT64_MAX, UINT64_MAX - 1, 1)); +} + +void source_causes_read_as_the_source() +{ + CHECK(x2w_startup::as_source_cause(Cause::unaligned) == Cause::source_unaligned); + CHECK(x2w_startup::as_source_cause(Cause::not_placed) == Cause::source_not_placed); + CHECK(x2w_startup::as_source_cause(Cause::wrong_state) == Cause::source_wrong_state); + CHECK(x2w_startup::as_source_cause(Cause::range) == Cause::source_range); + CHECK(x2w_startup::as_source_cause(Cause::ok) == Cause::ok); + CHECK(x2w_startup::as_source_cause(Cause::source_unaligned) == Cause::source_unaligned); +} +} // namespace + +int main() +{ + causes_have_distinct_names(); + a_plain_build_has_no_source(); + an_update_runs_in_place_or_elsewhere(); + partial_overlap_is_refused(); + the_source_must_be_aligned(); + overlap_edges(); + source_causes_read_as_the_source(); + std::puts("placed build causes: all checks passed"); + return 0; +} From 8e402dcc99f3d15409ab1a019674246681279f7f Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 01:40:17 +0100 Subject: [PATCH 02/10] test: dispatch rays after a placed PERFORM_UPDATE and document the ALLOW_UPDATE precondition Part of #870, #1758. Build the placed BLAS and TLAS with ALLOW_UPDATE, record the in-place update of both through resolve_placed_build, and read back four rays: the hit IDs change and equal the direct-address update and a fresh placed build. --- docs/experiments.md | 2 +- tests/dxr_check.cpp | 39 +++++++++++++++++++++++++++++++++++++++ tests/dxr_placed_build.h | 27 +++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 9a71757a01..70a831572f 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -7818,7 +7818,7 @@ cmake --build --preset release --target offscreen_check ctest --preset release --no-tests=error --output-on-failure -R '^dxr_placed_build_' ``` -The same resolver now names why it refused a build and accepts `PERFORM_UPDATE` with `SourceAccelerationStructureData` (#870, #1758). Each refusal still returns `E_INVALIDARG`, publishes no address and logs one line per cause for the first refusal of that kind: `x2w-dxr placed build refused: ( at title 0x...)`. The causes are `unaligned`, `not_placed`, `wrong_state` and `range` for the destination and scratch, and for the source `update_without_source`, `source_without_update`, `source_unaligned`, `source_not_placed`, `source_wrong_state`, `source_range` and `source_dest_partial_overlap`. An update takes its source through the same placement lookup as the destination (a live placed buffer created in the acceleration-structure state, 256-byte aligned), is sized by `UpdateScratchDataSizeInBytes` instead of `ScratchDataSizeInBytes`, and may run in place (source and destination the same address) or write another structure; a source that partly overlaps the destination is refused. The resolver cannot see whether the source was built with `ALLOW_UPDATE`, which the update needs (see the GTX 1060 note above). `placed_build_causes` checks the device-free rules (`src/startup_stubs/placed_build.h`) on any platform, and `dxr_placed_build_{hardware,warp}` now also resolves six refused updates, an in-place update and an out-of-place one. Limits: verified on WARP (Debug and /O2); the hardware variant was not run, and no update build has been recorded or dispatched. +The same resolver now names why it refused a build and accepts `PERFORM_UPDATE` with `SourceAccelerationStructureData` (#870, #1758). Each refusal still returns `E_INVALIDARG`, publishes no address and logs one line per cause for the first refusal of that kind: `x2w-dxr placed build refused: ( at title 0x...)`. The causes are `unaligned`, `not_placed`, `wrong_state` and `range` for the destination and scratch, and for the source `update_without_source`, `source_without_update`, `source_unaligned`, `source_not_placed`, `source_wrong_state`, `source_range` and `source_dest_partial_overlap`. An update takes its source through the same placement lookup as the destination (a live placed buffer created in the acceleration-structure state, 256-byte aligned), is sized by `UpdateScratchDataSizeInBytes` instead of `ScratchDataSizeInBytes`, and may run in place (source and destination the same address) or write another structure; a source that partly overlaps the destination is refused. The resolver cannot see whether the source was built with `ALLOW_UPDATE`, which the update needs (see the GTX 1060 note above), and the shim does not track that provenance. Caller precondition: a source structure must have been built with `ALLOW_UPDATE` in the same flags, geometry layout and instance count as the update; otherwise the device rejects or removes on the update. `placed_build_causes` checks the device-free rules (`src/startup_stubs/placed_build.h`) on any platform, and `dxr_placed_build_{hardware,warp}` now also resolves six refused updates, an in-place update and an out-of-place one. `dxr_placed_build_{hardware,warp}` also builds a placed BLAS and TLAS with `ALLOW_UPDATE`, records an in-place `PERFORM_UPDATE` of both through the resolver, and reads back four rays after the normal UAV barriers and fence: the hit instance IDs change from 7, 11, 0, 0 to 0, 0, 7, 11, and the rows equal the same update on desktop addresses and a fresh placed build of the updated scene. Limits: out-of-place updates are resolved but not dispatched. ```powershell ctest --preset release --no-tests=error --output-on-failure -R '^(placed_build_causes|dxr_placed_build_)' diff --git a/tests/dxr_check.cpp b/tests/dxr_check.cpp index c6d72a92b6..e8d5aa54b5 100644 --- a/tests/dxr_check.cpp +++ b/tests/dxr_check.cpp @@ -371,6 +371,11 @@ static std::vector trace_scene(ID3D12Device* device, const st top.Inputs.Flags |= D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE; bottom.SourceAccelerationStructureData = bottom.DestAccelerationStructureData; top.SourceAccelerationStructureData = top.DestAccelerationStructureData; + if (placedScene) { + // The update goes through the placed resolver too, from the title addresses of the initial build. + placedScene->resolve_update(0, bottom); + placedScene->resolve_update(1, top); + } list->ResourceBarrier(1, &order); list->BuildRaytracingAccelerationStructure(&bottom, 0, nullptr); list->ResourceBarrier(1, &order); @@ -487,6 +492,39 @@ std::vector dxr_fixture::trace_placed_triangle(ID3D12Device* device, con nullptr, nullptr, nullptr, placement, resolved, counts); } +// The accepted PERFORM_UPDATE path (#870, #1758): build the placed BLAS and TLAS with ALLOW_UPDATE, record the update +// of both through the placed resolver, and read back rays whose result changed. The references are the same update on +// desktop addresses, and a fresh placed build of the updated scene. +static void check_placed_update(ID3D12Device* device) { + using namespace dxr_fixture; + const std::vector initial{{-1,-1,1}, {1,-1,1}, {0,1,1}}; + const std::vector instances{instance(7,1), instance(11,2,4)}; + const SceneUpdate update{{{-1,-1,3}, {1,-1,3}, {0,1,3}}, {instance(7,1,2), instance(11,4,-3)}}; + std::vector rays; + for (const auto& probe : {std::pair{0.f, 1u}, {4.f, 2u}, {2.f, 1u}, {-3.f, 4u}}) + rays.push_back({probe.first,0,0,0.01f,0,0,1,8,probe.second}); + const auto run = [&](const SceneUpdate* change, bool placed, const std::vector& vertices, + const std::vector& desc) { + PlacedBuildScene scene; + return trace_scene(device, vertices, desc, rays, Backend::pipeline, change, nullptr, nullptr, + placed ? &scene : nullptr); + }; + const auto updated = run(&update, true, initial, instances); + const auto direct = run(&update, false, initial, instances); + const auto fresh = run(nullptr, true, update.vertices, update.instances); + const auto before = run(nullptr, true, initial, instances); + if (updated != direct) throw std::runtime_error("Placed PERFORM_UPDATE differs from the direct-address update"); + if (updated != fresh) throw std::runtime_error("Placed PERFORM_UPDATE differs from a fresh placed build of the updated scene"); + if (updated == before) throw std::runtime_error("Placed PERFORM_UPDATE left every ray result unchanged"); + // Hit instance IDs per ray before and after: the moved instances swap which probe rays they catch. + constexpr UINT idsBefore[]{7,11,0,0}, idsAfter[]{0,0,7,11}; + for (std::size_t i = 0; i < rays.size(); ++i) + if (before[4 * i + 1] != idsBefore[i] || updated[4 * i + 1] != idsAfter[i]) + throw std::runtime_error("Placed PERFORM_UPDATE ray " + std::to_string(i) + " has the wrong instance ID"); + std::puts("Slot 133 placed PERFORM_UPDATE (ALLOW_UPDATE build, in-place BLAS and TLAS update): ray results changed " + "and equal the direct-address update and a fresh placed build"); +} + bool check_dxr_placed_build(ID3D12Device* device) { using namespace dxr_fixture; if (!supported(device, Backend::pipeline)) return false; @@ -526,6 +564,7 @@ bool check_dxr_placed_build(ID3D12Device* device) { throw std::runtime_error("Routing the instance to the decoy BLAS still matched the direct reference"); std::printf("Slot 133 placed BLAS/TLAS: hit7 (distance=0x%08x), miss, masked miss; all 12 words equal the " "direct-address reference, decoy routing differs\n", output[3]); + check_placed_update(device); return true; } diff --git a/tests/dxr_placed_build.h b/tests/dxr_placed_build.h index 3117f417c8..a5123bd9fd 100644 --- a/tests/dxr_placed_build.h +++ b/tests/dxr_placed_build.h @@ -2,6 +2,7 @@ #include "desktop_gpu.h" #include "startup_stubs/placed_build.h" +#include #include #include #include @@ -18,6 +19,11 @@ class PlacedBuildScene { }; Microsoft::WRL::ComPtr guest_; std::array buffers_; + struct Placed { + UINT64 dest{}, scratch{}; + D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO sizes{}; + }; + std::array titles_; // BLAS, TLAS as built by prepare() public: // Wrong-routing control (issue #874): also build a decoy BLAS from another triangle into its own placed @@ -66,10 +72,16 @@ class PlacedBuildScene { constexpr auto scratch_state = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; if (!bottom_size.ScratchDataSizeInBytes || !top_size.ScratchDataSizeInBytes) throw std::runtime_error("This scratch-boundary fixture requires nonzero BLAS and TLAS scratch sizes"); + // A scratch buffer serves the build and, later, the update, so it holds the larger of the two. + const auto scratch_bytes = [](const auto& sizes) { + return (std::max)(sizes.ScratchDataSizeInBytes, sizes.UpdateScratchDataSizeInBytes); + }; const auto blas = place(0, dest_offset + bottom_size.ResultDataMaxSizeInBytes, dest_state) + dest_offset; const auto tlas = place(1, dest_offset + top_size.ResultDataMaxSizeInBytes, dest_state) + dest_offset; - const auto bs = place(2, scratch_offset + bottom_size.ScratchDataSizeInBytes, scratch_state) + scratch_offset; - const auto ts = place(3, scratch_offset + top_size.ScratchDataSizeInBytes, scratch_state) + scratch_offset; + const auto bs = place(2, scratch_offset + scratch_bytes(bottom_size), scratch_state) + scratch_offset; + const auto ts = place(3, scratch_offset + scratch_bytes(top_size), scratch_state) + scratch_offset; + titles_[0] = {blas, bs, bottom_size}; + titles_[1] = {tlas, ts, top_size}; const auto short_scratch = place(4, scratch_offset + top_size.ScratchDataSizeInBytes - 1, scratch_state) + scratch_offset; constexpr UINT64 unmapped = 0x1000; // outside the user allocation range const auto must_reject = [&](UINT64 dest, UINT64 scratch) { @@ -149,6 +161,17 @@ class PlacedBuildScene { throw std::runtime_error("An out-of-place update lost the source address or aliased the destination"); } + // Re-resolve one placed structure (0 = BLAS, 1 = TLAS) as an in-place PERFORM_UPDATE of itself. The source is the + // title address the structure was built at, so the update runs through the same slot-133 placement lookup as the + // build. The caller keeps ALLOW_UPDATE from the initial build; the resolver cannot see it. + void resolve_update(unsigned which, D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& build) { + const auto& placed = titles_.at(which); + build.Inputs.Flags |= D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE; + build.SourceAccelerationStructureData = placed.dest; + check(x2w_startup::resolve_placed_build(guest_.Get(), placed.dest, placed.scratch, placed.sizes, build), + "Resolve placed in-place update"); + } + // The decoy BLAS for the wrong-routing control, placed and resolved through the same slot-133 path. void prepare_decoy(const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO& bottom_size, D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& decoy) { From 0d997a8b3f2882413fe0d9df9aff4b7f29882cdb Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 01:47:44 +0100 Subject: [PATCH 03/10] test: print placed update sizes --- tests/dxr_placed_build.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/dxr_placed_build.h b/tests/dxr_placed_build.h index a5123bd9fd..c7dc9f7870 100644 --- a/tests/dxr_placed_build.h +++ b/tests/dxr_placed_build.h @@ -168,6 +168,9 @@ class PlacedBuildScene { const auto& placed = titles_.at(which); build.Inputs.Flags |= D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE; build.SourceAccelerationStructureData = placed.dest; + std::printf("placed update %u: dest=%llx scratch=%llx result=%llu build_scratch=%llu update_scratch=%llu\n", which, + placed.dest, placed.scratch, placed.sizes.ResultDataMaxSizeInBytes, placed.sizes.ScratchDataSizeInBytes, + placed.sizes.UpdateScratchDataSizeInBytes); check(x2w_startup::resolve_placed_build(guest_.Get(), placed.dest, placed.scratch, placed.sizes, build), "Resolve placed in-place update"); } From 1c5de00a95102011a0ed02f4092df07c7929ec4e Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 01:56:43 +0100 Subject: [PATCH 04/10] fix: accept a placed update when the device reports no update scratch --- docs/experiments.md | 2 +- src/startup_stubs/d3d12_xs_device.cpp | 8 ++++++-- tests/dxr_placed_build.h | 17 ++++++++++++++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 70a831572f..303ea7dd1c 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -7818,7 +7818,7 @@ cmake --build --preset release --target offscreen_check ctest --preset release --no-tests=error --output-on-failure -R '^dxr_placed_build_' ``` -The same resolver now names why it refused a build and accepts `PERFORM_UPDATE` with `SourceAccelerationStructureData` (#870, #1758). Each refusal still returns `E_INVALIDARG`, publishes no address and logs one line per cause for the first refusal of that kind: `x2w-dxr placed build refused: ( at title 0x...)`. The causes are `unaligned`, `not_placed`, `wrong_state` and `range` for the destination and scratch, and for the source `update_without_source`, `source_without_update`, `source_unaligned`, `source_not_placed`, `source_wrong_state`, `source_range` and `source_dest_partial_overlap`. An update takes its source through the same placement lookup as the destination (a live placed buffer created in the acceleration-structure state, 256-byte aligned), is sized by `UpdateScratchDataSizeInBytes` instead of `ScratchDataSizeInBytes`, and may run in place (source and destination the same address) or write another structure; a source that partly overlaps the destination is refused. The resolver cannot see whether the source was built with `ALLOW_UPDATE`, which the update needs (see the GTX 1060 note above), and the shim does not track that provenance. Caller precondition: a source structure must have been built with `ALLOW_UPDATE` in the same flags, geometry layout and instance count as the update; otherwise the device rejects or removes on the update. `placed_build_causes` checks the device-free rules (`src/startup_stubs/placed_build.h`) on any platform, and `dxr_placed_build_{hardware,warp}` now also resolves six refused updates, an in-place update and an out-of-place one. `dxr_placed_build_{hardware,warp}` also builds a placed BLAS and TLAS with `ALLOW_UPDATE`, records an in-place `PERFORM_UPDATE` of both through the resolver, and reads back four rays after the normal UAV barriers and fence: the hit instance IDs change from 7, 11, 0, 0 to 0, 0, 7, 11, and the rows equal the same update on desktop addresses and a fresh placed build of the updated scene. Limits: out-of-place updates are resolved but not dispatched. +The same resolver now names why it refused a build and accepts `PERFORM_UPDATE` with `SourceAccelerationStructureData` (#870, #1758). Each refusal still returns `E_INVALIDARG`, publishes no address and logs one line per cause for the first refusal of that kind: `x2w-dxr placed build refused: ( at title 0x...)`. The causes are `unaligned`, `not_placed`, `wrong_state` and `range` for the destination and scratch, and for the source `update_without_source`, `source_without_update`, `source_unaligned`, `source_not_placed`, `source_wrong_state`, `source_range` and `source_dest_partial_overlap`. An update takes its source through the same placement lookup as the destination (a live placed buffer created in the acceleration-structure state, 256-byte aligned), is sized by `UpdateScratchDataSizeInBytes` instead of `ScratchDataSizeInBytes` (WARP reports 0 there; a null scratch is then accepted and a given one must still be a placed buffer), and may run in place (source and destination the same address) or write another structure; a source that partly overlaps the destination is refused. The resolver cannot see whether the source was built with `ALLOW_UPDATE`, which the update needs (see the GTX 1060 note above), and the shim does not track that provenance. Caller precondition: a source structure must have been built with `ALLOW_UPDATE` in the same flags, geometry layout and instance count as the update; otherwise the device rejects or removes on the update. `placed_build_causes` checks the device-free rules (`src/startup_stubs/placed_build.h`) on any platform, and `dxr_placed_build_{hardware,warp}` now also resolves six refused updates, an in-place update and an out-of-place one. `dxr_placed_build_{hardware,warp}` also builds a placed BLAS and TLAS with `ALLOW_UPDATE`, records an in-place `PERFORM_UPDATE` of both through the resolver, and reads back four rays after the normal UAV barriers and fence: the hit instance IDs change from 7, 11, 0, 0 to 0, 0, 7, 11, and the rows equal the same update on desktop addresses and a fresh placed build of the updated scene. Limits: out-of-place updates are resolved but not dispatched. ```powershell ctest --preset release --no-tests=error --output-on-failure -R '^(placed_build_causes|dxr_placed_build_)' diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index e3933a9a52..21deb8df97 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -3800,8 +3800,12 @@ HRESULT x2w_startup::resolve_placed_build(void* guest_device, UINT64 dest, UINT6 const auto out = translate(dest, sizes.ResultDataMaxSizeInBytes, as_state); if (out.cause != Cause::ok) return refuse(out.cause, "dest", dest); // An update is sized by its own scratch requirement, a full build by the build one. - const auto temp = translate(scratch, update ? sizes.UpdateScratchDataSizeInBytes : sizes.ScratchDataSizeInBytes, - D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + // A device may report no update scratch at all (WARP does). Then a null scratch is fine, and a scratch the title + // still passes must be a placed buffer, checked over at least one byte. + const UINT64 scratch_bytes = update ? sizes.UpdateScratchDataSizeInBytes : sizes.ScratchDataSizeInBytes; + const bool no_scratch = update && !scratch_bytes && !scratch; + const auto temp = no_scratch ? Translated{Cause::ok, 0} + : translate(scratch, (std::max)(scratch_bytes, 1), D3D12_RESOURCE_STATE_UNORDERED_ACCESS); if (temp.cause != Cause::ok) return refuse(temp.cause, "scratch", scratch); Translated from{Cause::ok, 0}; if (update) { diff --git a/tests/dxr_placed_build.h b/tests/dxr_placed_build.h index c7dc9f7870..077fa100d4 100644 --- a/tests/dxr_placed_build.h +++ b/tests/dxr_placed_build.h @@ -148,6 +148,20 @@ class PlacedBuildScene { if (!in_place.DestAccelerationStructureData || in_place.SourceAccelerationStructureData != in_place.DestAccelerationStructureData) throw std::runtime_error("An in-place update must translate source and destination to the same address"); + // A device that reports no update scratch (WARP): a placed scratch and a null scratch both resolve, and a + // scratch outside every placed buffer is still refused. + auto none = sizes; + none.UpdateScratchDataSizeInBytes = 0; + for (const UINT64 given : {scratch, UINT64{0}}) { + auto build = request(tlas, true); + check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, given, none, build), "Resolve update without scratch"); + if (!build.DestAccelerationStructureData || bool(build.ScratchAccelerationStructureData) != bool(given)) + throw std::runtime_error("A zero-size update scratch was not published as given"); + } + auto stray = request(tlas, true); + if (x2w_startup::resolve_placed_build(guest_.Get(), tlas, unmapped, none, stray) != E_INVALIDARG) + throw std::runtime_error("A stray scratch was accepted for a zero-size update scratch"); + // Out of place: another placed acceleration structure, its interior offset kept. const auto other = place(7, dest_offset + top_size.ResultDataMaxSizeInBytes, D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE) + dest_offset; @@ -168,9 +182,6 @@ class PlacedBuildScene { const auto& placed = titles_.at(which); build.Inputs.Flags |= D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE; build.SourceAccelerationStructureData = placed.dest; - std::printf("placed update %u: dest=%llx scratch=%llx result=%llu build_scratch=%llu update_scratch=%llu\n", which, - placed.dest, placed.scratch, placed.sizes.ResultDataMaxSizeInBytes, placed.sizes.ScratchDataSizeInBytes, - placed.sizes.UpdateScratchDataSizeInBytes); check(x2w_startup::resolve_placed_build(guest_.Get(), placed.dest, placed.scratch, placed.sizes, build), "Resolve placed in-place update"); } From 3bdbf41c585918fad3529679e058691856cf26d0 Mon Sep 17 00:00:00 2001 From: c1 Date: Thu, 26 Nov 2026 12:16:20 +0100 Subject: [PATCH 05/10] fix: record ALLOW_UPDATE per placed build and refuse an update whose source lacks it; name every cause in the device tests --- docs/experiments.md | 2 +- src/startup_stubs/d3d12_xs_device.cpp | 21 ++++++- src/startup_stubs/placed_build.h | 53 +++++++++++++++- tests/dxr_placed_build.h | 87 ++++++++++++++++++++------- tests/placed_build_causes_check.cpp | 48 +++++++++++++++ 5 files changed, 182 insertions(+), 29 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index aa2b9c27f4..7ead63d5cc 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11271,7 +11271,7 @@ cmake --build --preset release --target offscreen_check ctest --preset release --no-tests=error --output-on-failure -R '^dxr_placed_build_' ``` -The same resolver now names why it refused a build and accepts `PERFORM_UPDATE` with `SourceAccelerationStructureData` (#870, #1758). Each refusal still returns `E_INVALIDARG`, publishes no address and logs one line per cause for the first refusal of that kind: `x2w-dxr placed build refused: ( at title 0x...)`. The causes are `unaligned`, `not_placed`, `wrong_state` and `range` for the destination and scratch, and for the source `update_without_source`, `source_without_update`, `source_unaligned`, `source_not_placed`, `source_wrong_state`, `source_range` and `source_dest_partial_overlap`. An update takes its source through the same placement lookup as the destination (a live placed buffer created in the acceleration-structure state, 256-byte aligned), is sized by `UpdateScratchDataSizeInBytes` instead of `ScratchDataSizeInBytes` (WARP reports 0 there; a null scratch is then accepted and a given one must still be a placed buffer), and may run in place (source and destination the same address) or write another structure; a source that partly overlaps the destination is refused. The resolver cannot see whether the source was built with `ALLOW_UPDATE`, which the update needs (see the GTX 1060 note above), and the shim does not track that provenance. Caller precondition: a source structure must have been built with `ALLOW_UPDATE` in the same flags, geometry layout and instance count as the update; otherwise the device rejects or removes on the update. `placed_build_causes` checks the device-free rules (`src/startup_stubs/placed_build.h`) on any platform, and `dxr_placed_build_{hardware,warp}` now also resolves six refused updates, an in-place update and an out-of-place one. `dxr_placed_build_{hardware,warp}` also builds a placed BLAS and TLAS with `ALLOW_UPDATE`, records an in-place `PERFORM_UPDATE` of both through the resolver, and reads back four rays after the normal UAV barriers and fence: the hit instance IDs change from 7, 11, 0, 0 to 0, 0, 7, 11, and the rows equal the same update on desktop addresses and a fresh placed build of the updated scene. Limits: out-of-place updates are resolved but not dispatched. +The same resolver now names why it refused a build and accepts `PERFORM_UPDATE` with `SourceAccelerationStructureData` (#870, #1758). Each refusal still returns `E_INVALIDARG`, publishes no address and logs one line per cause for the first refusal of that kind: `x2w-dxr placed build refused: ( at title 0x...)`. The causes are `unaligned`, `not_placed`, `wrong_state` and `range` for the destination and scratch, and for the source `update_without_source`, `source_without_update`, `source_unaligned`, `source_not_placed`, `source_wrong_state`, `source_range`, `source_dest_partial_overlap`, `source_not_built` and `source_not_updatable`. An update takes its source through the same placement lookup as the destination (a live placed buffer created in the acceleration-structure state, 256-byte aligned), is sized by `UpdateScratchDataSizeInBytes` instead of `ScratchDataSizeInBytes` (WARP reports 0 there; a null scratch is then accepted and a given one must still be a placed buffer), and may run in place (source and destination the same address) or write another structure; a source that partly overlaps the destination is refused. An update needs a source built with `ALLOW_UPDATE` (see the GTX 1060 note above). The shim cannot read the flags back from the GPU, so the resolver records the `ALLOW_UPDATE` flag of every build it publishes, keyed by the structure's title address (a later build over the same memory replaces the record, and an update's destination is recorded with the update's flags), and refuses an update whose source was recorded without it (`source_not_updatable`) or that it never saw built (`source_not_built`; unknown provenance is refused). Nothing is recorded for a refusal. Caller precondition that stays: the source must also have the same geometry layout and instance count as the update; otherwise the device rejects or removes on the update. `placed_build_causes` checks the device-free rules (`src/startup_stubs/placed_build.h`: the update shape, the ledger of recorded builds and the translated-address rule behind `range` and `source_range`, which a real device cannot be made to break) on any platform, and `dxr_placed_build_{hardware,warp}` resolves every refusal that a device can trigger and requires the named cause (`resolve_placed_build` reports it through an optional out parameter): destination and scratch not placed, unaligned or in a buffer of the wrong state, and for an update the missing source, a source without the flag, an unaligned, unplaced or wrong-state source, a partial overlap, a source never built and a source built without `ALLOW_UPDATE`; then an in-place update and an out-of-place one. `dxr_placed_build_{hardware,warp}` also builds a placed BLAS and TLAS with `ALLOW_UPDATE`, records an in-place `PERFORM_UPDATE` of both through the resolver, and reads back four rays after the normal UAV barriers and fence: the hit instance IDs change from 7, 11, 0, 0 to 0, 0, 7, 11, and the rows equal the same update on desktop addresses and a fresh placed build of the updated scene. Limits: out-of-place updates are resolved but not dispatched. ```powershell ctest --preset release --no-tests=error --output-on-failure -R '^(placed_build_causes|dxr_placed_build_)' diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index c6c281da12..4b9d337882 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -7063,12 +7063,16 @@ void x2w_startup::attach_device(ID3D12Device* device, void** wrapped) HRESULT x2w_startup::resolve_placed_build(void* guest_device, UINT64 dest, UINT64 scratch, const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO& sizes, - D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& build) + D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& build, x2w_startup::PlacedBuildCause* reported) { using Cause = x2w_startup::PlacedBuildCause; // One log line per cause, for the first refusal of that kind in the process. static LONG logged[static_cast(Cause::count)]; + // What the resolver published for each placed build (flags per structure), see PlacedBuildLedger. + static SRWLOCK ledger_lock = SRWLOCK_INIT; + static x2w_startup::PlacedBuildLedger ledger; const auto refuse = [&](Cause cause, const char* role, UINT64 address) { + if (reported) *reported = cause; if (first_calls(logged[static_cast(cause)], 1)) log("x2w-dxr placed build refused: %s (%s at title 0x%llx)", x2w_startup::cause_name(cause), role, static_cast(address)); @@ -7093,7 +7097,7 @@ HRESULT x2w_startup::resolve_placed_build(void* guest_device, UINT64 dest, UINT6 const auto base = reinterpret_cast(found.owner->own().title_memory); const auto gpu = static_cast(found.owner->own().real)->GetGPUVirtualAddress(); const auto offset = address - base; - if (!gpu || offset > UINT64_MAX - gpu || (gpu + offset) % alignment) return {Cause::range, 0}; + if (const Cause usable = x2w_startup::gpu_range_cause(gpu, offset); usable != Cause::ok) return {usable, 0}; return {Cause::ok, gpu + offset}; }; constexpr auto as_state = D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE; @@ -7112,9 +7116,22 @@ HRESULT x2w_startup::resolve_placed_build(void* guest_device, UINT64 dest, UINT6 from = translate(source, sizes.ResultDataMaxSizeInBytes, as_state); if (from.cause != Cause::ok) return refuse(x2w_startup::as_source_cause(from.cause), "source", source); } + // The provenance of the source and the record of this build are one step under the lock: nothing is recorded for a refusal. + AcquireSRWLockExclusive(&ledger_lock); + if (update) { + if (const Cause built = ledger.check_source(source); built != Cause::ok) { + ReleaseSRWLockExclusive(&ledger_lock); + return refuse(built, "source", source); + } + } + // The structure written here keeps ALLOW_UPDATE only if this build asked for it (an update repeats the source's flags). + ledger.record(dest, sizes.ResultDataMaxSizeInBytes, + (build.Inputs.Flags & D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE) != 0); + ReleaseSRWLockExclusive(&ledger_lock); build.DestAccelerationStructureData = out.gpu; build.ScratchAccelerationStructureData = temp.gpu; if (update) build.SourceAccelerationStructureData = from.gpu; + if (reported) *reported = Cause::ok; return S_OK; } diff --git a/src/startup_stubs/placed_build.h b/src/startup_stubs/placed_build.h index 15ea65e341..de95d022b4 100644 --- a/src/startup_stubs/placed_build.h +++ b/src/startup_stubs/placed_build.h @@ -2,6 +2,8 @@ #include #include +#include +#include namespace x2w_startup { // Why a placed acceleration-structure build was refused. The first part of this header is standard C++ only, so @@ -19,6 +21,8 @@ enum class PlacedBuildCause : unsigned { source_wrong_state, source_range, source_dest_partial_overlap, // the source and the destination overlap without being the same structure + source_not_built, // no build through the resolver produced a structure at the source address + source_not_updatable, // the source was built without ALLOW_UPDATE count }; @@ -37,6 +41,8 @@ constexpr const char* cause_name(PlacedBuildCause cause) case PlacedBuildCause::source_wrong_state: return "source_wrong_state"; case PlacedBuildCause::source_range: return "source_range"; case PlacedBuildCause::source_dest_partial_overlap: return "source_dest_partial_overlap"; + case PlacedBuildCause::source_not_built: return "source_not_built"; + case PlacedBuildCause::source_not_updatable: return "source_not_updatable"; case PlacedBuildCause::count: break; } return "unknown"; @@ -56,6 +62,14 @@ constexpr PlacedBuildCause as_source_cause(PlacedBuildCause cause) constexpr std::uint64_t build_alignment = 256; // D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT +// Whether a placed buffer's translated address can be used: it exists, the offset does not wrap it, and the result keeps the +// 256-byte alignment a structure needs. A real device places buffers 64 KB aligned, so only a broken translation reaches this. +constexpr PlacedBuildCause gpu_range_cause(std::uint64_t gpu, std::uint64_t offset) +{ + if (!gpu || offset > UINT64_MAX - gpu || (gpu + offset) % build_alignment) return PlacedBuildCause::range; + return PlacedBuildCause::ok; +} + // The last byte of [start, start + bytes), clamped to the top of the address space. constexpr std::uint64_t last_byte(std::uint64_t start, std::uint64_t bytes) { @@ -83,6 +97,38 @@ constexpr PlacedBuildCause check_update_shape(bool perform_update, std::uint64_t return PlacedBuildCause::source_dest_partial_overlap; return PlacedBuildCause::ok; } + +// What the resolver published for each placed build, so an update can be checked against the flags its source was built with. +// An update needs a source built with ALLOW_UPDATE (D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE). The shim +// cannot read the flags back from the GPU, so it records them when it resolves the build. A source it never saw built is +// refused too: unknown provenance means refuse. Not thread safe; the resolver holds a lock around it. +class PlacedBuildLedger { +public: + // A build at [start, start + bytes) replaces whatever was recorded over that memory. + void record(std::uint64_t start, std::uint64_t bytes, bool allow_update) + { + for (auto it = entries_.begin(); it != entries_.end();) { + if (ranges_overlap(it->first, it->second.bytes, start, bytes)) it = entries_.erase(it); + else ++it; + } + entries_[start] = {bytes, allow_update}; + } + // The source of an update names the start of a recorded structure that was built with ALLOW_UPDATE. + PlacedBuildCause check_source(std::uint64_t source) const + { + const auto found = entries_.find(source); + if (found == entries_.end()) return PlacedBuildCause::source_not_built; + return found->second.allow_update ? PlacedBuildCause::ok : PlacedBuildCause::source_not_updatable; + } + std::size_t size() const { return entries_.size(); } + +private: + struct Entry { + std::uint64_t bytes{}; + bool allow_update{}; + }; + std::map entries_; +}; } // namespace x2w_startup #if defined(_WIN32) @@ -96,10 +142,11 @@ void attach_device(ID3D12Device* device, void** wrapped); // Internal desktop build preparation. The caller retains the registered resources // through execution and supplies prebuild sizes for these exact BLAS/TLAS inputs. // With PERFORM_UPDATE, build.SourceAccelerationStructureData names the structure to update, in place or not, -// and the scratch size is sizes.UpdateScratchDataSizeInBytes. A refusal returns E_INVALIDARG, leaves build -// untouched and logs its PlacedBuildCause once per cause. +// and the scratch size is sizes.UpdateScratchDataSizeInBytes. A successful build is recorded with its ALLOW_UPDATE flag, and an +// update whose source was not recorded as built with ALLOW_UPDATE is refused. A refusal returns E_INVALIDARG, leaves build +// untouched, logs its PlacedBuildCause once per cause and reports it in *cause when that is not null (ok on success). HRESULT resolve_placed_build(void* guest_device, UINT64 dest, UINT64 scratch, const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO& sizes, - D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& build); + D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& build, PlacedBuildCause* cause = nullptr); } #endif diff --git a/tests/dxr_placed_build.h b/tests/dxr_placed_build.h index 9287b9feba..428b7966c7 100644 --- a/tests/dxr_placed_build.h +++ b/tests/dxr_placed_build.h @@ -28,6 +28,7 @@ class PlacedBuildScene { std::array titles_; // BLAS, TLAS as built by prepare() public: + using Cause = x2w_startup::PlacedBuildCause; // Wrong-routing control (issue #874): also build a decoy BLAS from another triangle into its own placed // destination and scratch, and route the TLAS instance to the decoy instead of the BLAS under test. bool misroute = false; @@ -90,16 +91,24 @@ class PlacedBuildScene { titles_[1] = {tlas, ts, top_size}; const auto short_scratch = place(4, scratch_offset + top_size.ScratchDataSizeInBytes - 1, scratch_state) + scratch_offset; constexpr UINT64 unmapped = 0x1000; // outside the user allocation range - const auto must_reject = [&](UINT64 dest, UINT64 scratch) { + // Each refusal names its cause (not only E_INVALIDARG) and publishes nothing. + const auto must_reject = [&](UINT64 dest, UINT64 scratch, Cause expected, const char* what) { auto candidate = top; - if (x2w_startup::resolve_placed_build(guest_.Get(), dest, scratch, top_size, candidate) != E_INVALIDARG || - candidate.DestAccelerationStructureData || candidate.ScratchAccelerationStructureData) - throw std::runtime_error("Invalid DXR build was accepted or partially published"); + Cause cause = Cause::ok; + const HRESULT hr = x2w_startup::resolve_placed_build(guest_.Get(), dest, scratch, top_size, candidate, &cause); + if (hr != E_INVALIDARG || candidate.DestAccelerationStructureData || candidate.ScratchAccelerationStructureData) + throw std::runtime_error(std::string("Invalid DXR build was accepted or partially published: ") + what); + if (cause != expected) + throw std::runtime_error(std::string("DXR build refusal names the wrong cause (") + x2w_startup::cause_name(cause) + + " for " + what + ", expected " + x2w_startup::cause_name(expected) + ")"); }; - must_reject(unmapped, ts); - must_reject(tlas, short_scratch); - must_reject(ts, tlas); - check_updates(top, top_size, tlas, ts); + must_reject(unmapped, ts, Cause::not_placed, "destination outside the placed buffers"); + must_reject(tlas + 16, ts, Cause::unaligned, "unaligned destination"); + must_reject(ts, tlas, Cause::wrong_state, "destination in a buffer that is not an acceleration structure"); + must_reject(tlas, short_scratch, Cause::not_placed, "scratch shorter than the build needs"); + must_reject(tlas, unmapped, Cause::not_placed, "scratch outside the placed buffers"); + must_reject(tlas, ts + 16, Cause::unaligned, "unaligned scratch"); + must_reject(tlas, tlas + 512, Cause::wrong_state, "scratch in an acceleration-structure buffer"); check(x2w_startup::resolve_placed_build(guest_.Get(), blas, bs, bottom_size, bottom), "Resolve placed BLAS"); check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, ts, top_size, top), "Resolve placed TLAS"); tlas_title_ = tlas; @@ -114,6 +123,11 @@ class PlacedBuildScene { }; verify_offset(bottom, blas, bs, bottom_size); verify_offset(top, tlas, ts, top_size); + // The base resolves above are builds over the same memory, so the resolver's record of the real builds was replaced: + // resolve the real ones again, then run the update controls against them. + check(x2w_startup::resolve_placed_build(guest_.Get(), blas, bs, bottom_size, bottom), "Resolve placed BLAS again"); + check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, ts, top_size, top), "Resolve placed TLAS again"); + check_updates(top, top_size, tlas, ts); std::printf("Placed DXR bytes dest/scratch: BLAS=%llu/%llu TLAS=%llu/%llu; short scratch, missing dest and swapped states rejected before recording\n", bottom_size.ResultDataMaxSizeInBytes, bottom_size.ScratchDataSizeInBytes, top_size.ResultDataMaxSizeInBytes, top_size.ScratchDataSizeInBytes); @@ -121,10 +135,11 @@ class PlacedBuildScene { // PERFORM_UPDATE with SourceAccelerationStructureData, through the same resolver. The scratch of an update // is sized by UpdateScratchDataSizeInBytes; the fixture sets it to the build scratch size so that the one - // scratch buffer placed for the build fits. + // scratch buffer placed for the build fits. Needs the BLAS/TLAS builds of prepare() to be recorded already. void check_updates(const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& top, const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO& top_size, UINT64 tlas, UINT64 scratch) { constexpr auto update_flag = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE; + constexpr auto allow_flag = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE; constexpr UINT64 dest_offset = 512, unmapped = 0x1000; auto sizes = top_size; sizes.UpdateScratchDataSizeInBytes = top_size.ScratchDataSizeInBytes; @@ -135,20 +150,30 @@ class PlacedBuildScene { build.DestAccelerationStructureData = build.ScratchAccelerationStructureData = 0; return build; }; - // Each refusal returns E_INVALIDARG and publishes neither address, and keeps the source as the title gave it. - const auto must_reject = [&](UINT64 source, bool update, const char* what) { + // Each refusal returns E_INVALIDARG, names its cause, publishes neither address and keeps the source as the title gave it. + const auto must_reject = [&](UINT64 dest, UINT64 source, UINT64 scratch_address, const auto& use, bool update, + Cause expected, const char* what) { auto build = request(source, update); - if (x2w_startup::resolve_placed_build(guest_.Get(), tlas, scratch, sizes, build) != E_INVALIDARG || - build.DestAccelerationStructureData || build.ScratchAccelerationStructureData || + Cause cause = Cause::ok; + const HRESULT hr = x2w_startup::resolve_placed_build(guest_.Get(), dest, scratch_address, use, build, &cause); + if (hr != E_INVALIDARG || build.DestAccelerationStructureData || build.ScratchAccelerationStructureData || build.SourceAccelerationStructureData != source) throw std::runtime_error(std::string("Invalid DXR update was accepted or partially published: ") + what); + if (cause != expected) + throw std::runtime_error(std::string("DXR update refusal names the wrong cause (") + x2w_startup::cause_name(cause) + + " for " + what + ", expected " + x2w_startup::cause_name(expected) + ")"); }; - must_reject(0, true, "update without a source"); - must_reject(tlas, false, "source without PERFORM_UPDATE"); - must_reject(tlas + 1, true, "unaligned source"); - must_reject(unmapped, true, "source outside the placed buffers"); - must_reject(scratch, true, "source in a buffer that is not an acceleration structure"); - must_reject(tlas + 256, true, "source partly overlapping the destination"); + must_reject(tlas, 0, scratch, sizes, true, Cause::update_without_source, "update without a source"); + must_reject(tlas, tlas, scratch, sizes, false, Cause::source_without_update, "source without PERFORM_UPDATE"); + must_reject(tlas, tlas + 1, scratch, sizes, true, Cause::source_unaligned, "unaligned source"); + must_reject(tlas, unmapped, scratch, sizes, true, Cause::source_not_placed, "source outside the placed buffers"); + must_reject(tlas, scratch, scratch, sizes, true, Cause::source_wrong_state, + "source in a buffer that is not an acceleration structure"); + must_reject(tlas, tlas + 256, scratch, sizes, true, Cause::source_dest_partial_overlap, + "source partly overlapping the destination"); + // The destination and scratch rules apply to an update as to a build. + must_reject(tlas + 16, tlas, scratch, sizes, true, Cause::unaligned, "update with an unaligned destination"); + must_reject(tlas, tlas, tlas + 512, sizes, true, Cause::wrong_state, "update with a scratch in an acceleration-structure buffer"); // In place: the source is the destination. auto in_place = request(tlas, true); @@ -166,13 +191,26 @@ class PlacedBuildScene { if (!build.DestAccelerationStructureData || bool(build.ScratchAccelerationStructureData) != bool(given)) throw std::runtime_error("A zero-size update scratch was not published as given"); } - auto stray = request(tlas, true); - if (x2w_startup::resolve_placed_build(guest_.Get(), tlas, unmapped, none, stray) != E_INVALIDARG) - throw std::runtime_error("A stray scratch was accepted for a zero-size update scratch"); + must_reject(tlas, tlas, unmapped, none, true, Cause::not_placed, "a stray scratch for a zero-size update scratch"); - // Out of place: another placed acceleration structure, its interior offset kept. + // Provenance: an update needs a source that the resolver saw built with ALLOW_UPDATE. const auto other = place(7, dest_offset + top_size.ResultDataMaxSizeInBytes, D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE) + dest_offset; + must_reject(tlas, other, scratch, sizes, true, Cause::source_not_built, "source never built"); + const auto build_other = [&](bool allow_update) { + auto build = request(0, false); + build.Inputs.Flags = allow_update ? (build.Inputs.Flags | allow_flag) : (build.Inputs.Flags & ~allow_flag); + check(x2w_startup::resolve_placed_build(guest_.Get(), other, scratch, top_size, build), "Resolve a build of the other structure"); + }; + build_other(false); + must_reject(tlas, other, scratch, sizes, true, Cause::source_not_updatable, "source built without ALLOW_UPDATE"); + // A refused update records nothing: the in-place TLAS update still works, and the other source is still refused. + auto again = request(tlas, true); + check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, scratch, sizes, again), "Resolve in-place update after refusals"); + must_reject(tlas, other, scratch, sizes, true, Cause::source_not_updatable, "source built without ALLOW_UPDATE, again"); + + // Out of place: another placed acceleration structure built with ALLOW_UPDATE, its interior offset kept. + build_other(true); auto moved = request(other, true); check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, scratch, sizes, moved), "Resolve out-of-place update"); auto base = request(0, false); @@ -181,6 +219,9 @@ class PlacedBuildScene { if (moved.SourceAccelerationStructureData != base.DestAccelerationStructureData + dest_offset || moved.SourceAccelerationStructureData == moved.DestAccelerationStructureData) throw std::runtime_error("An out-of-place update lost the source address or aliased the destination"); + // The update's destination (the TLAS) is a recorded build again, with the flags of the update. + auto after = request(tlas, true); + check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, scratch, sizes, after), "Resolve in-place update of the updated structure"); } // Re-resolve one placed structure (0 = BLAS, 1 = TLAS) as an in-place PERFORM_UPDATE of itself. The source is the diff --git a/tests/placed_build_causes_check.cpp b/tests/placed_build_causes_check.cpp index 2c86051070..eeb06df7c5 100644 --- a/tests/placed_build_causes_check.cpp +++ b/tests/placed_build_causes_check.cpp @@ -12,6 +12,8 @@ using x2w_startup::PlacedBuildCause; using Cause = PlacedBuildCause; using x2w_startup::check_update_shape; using x2w_startup::ranges_overlap; +using x2w_startup::gpu_range_cause; +using x2w_startup::PlacedBuildLedger; #define CHECK(condition) \ do { \ @@ -90,6 +92,50 @@ void source_causes_read_as_the_source() CHECK(x2w_startup::as_source_cause(Cause::ok) == Cause::ok); CHECK(x2w_startup::as_source_cause(Cause::source_unaligned) == Cause::source_unaligned); } + +// The translated address of a placed buffer: this is the only rule behind `range` and `source_range` that a real device cannot +// be made to break (buffers are placed 64 KB aligned and never at address zero), so it is checked here by value. +void the_translated_address_must_be_usable() +{ + constexpr std::uint64_t gpu = 0x7000'0000'0000; + CHECK(gpu_range_cause(gpu, 0) == Cause::ok); + CHECK(gpu_range_cause(gpu, 512) == Cause::ok); + CHECK(gpu_range_cause(0, 0) == Cause::range); // no GPU address at all + CHECK(gpu_range_cause(gpu, 1) == Cause::range); // the offset breaks the 256-byte alignment + CHECK(gpu_range_cause(gpu + 128, 0) == Cause::range); // a misaligned base + CHECK(gpu_range_cause(UINT64_MAX - 255, 256) == Cause::range); // the offset wraps the address + CHECK(gpu_range_cause(UINT64_MAX - 255, 255) == Cause::range); // (no wrap, but the sum is misaligned) + CHECK(gpu_range_cause(UINT64_MAX - 511, 256) == Cause::ok); // the last aligned fit + CHECK(x2w_startup::as_source_cause(gpu_range_cause(0, 0)) == Cause::source_range); +} + +// An update needs a source built with ALLOW_UPDATE; the resolver records each build's flag and checks it here. +void an_update_source_needs_a_recorded_allow_update_build() +{ + PlacedBuildLedger ledger; + CHECK(ledger.check_source(base) == Cause::source_not_built); // never built + ledger.record(base, size, true); + CHECK(ledger.check_source(base) == Cause::ok); + CHECK(ledger.check_source(base + 256) == Cause::source_not_built); // only the start of a structure names it + CHECK(ledger.check_source(base - 256) == Cause::source_not_built); + ledger.record(base + 8 * size, size, false); // built without ALLOW_UPDATE + CHECK(ledger.check_source(base + 8 * size) == Cause::source_not_updatable); + CHECK(ledger.check_source(base) == Cause::ok); // the other structure is unaffected + // Building again over the same memory replaces the record, in either direction. + ledger.record(base + 8 * size, size, true); + CHECK(ledger.check_source(base + 8 * size) == Cause::ok); + ledger.record(base, size, false); + CHECK(ledger.check_source(base) == Cause::source_not_updatable); + // A build that overlaps another structure's memory (partly or wholly) drops that structure's record. + ledger.record(base, size, true); + ledger.record(base + 256, size, true); + CHECK(ledger.check_source(base) == Cause::source_not_built); + CHECK(ledger.check_source(base + 256) == Cause::ok); + // An adjacent build (first byte past) keeps it. + ledger.record(base + 256 + size, size, false); + CHECK(ledger.check_source(base + 256) == Cause::ok); + CHECK(ledger.size() == 3); +} } // namespace int main() @@ -101,6 +147,8 @@ int main() the_source_must_be_aligned(); overlap_edges(); source_causes_read_as_the_source(); + the_translated_address_must_be_usable(); + an_update_source_needs_a_recorded_allow_update_build(); std::puts("placed build causes: all checks passed"); return 0; } From 3e1cc746da06ee7104ea8ac8aa4b9dccf82d2ace Mon Sep 17 00:00:00 2001 From: c1 Date: Thu, 26 Nov 2026 12:18:02 +0100 Subject: [PATCH 06/10] test: use an acceleration-structure buffer big enough for the wrong-state scratch controls; drop device controls a small structure cannot reach --- docs/experiments.md | 2 +- tests/dxr_placed_build.h | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 7ead63d5cc..66bad82c67 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11271,7 +11271,7 @@ cmake --build --preset release --target offscreen_check ctest --preset release --no-tests=error --output-on-failure -R '^dxr_placed_build_' ``` -The same resolver now names why it refused a build and accepts `PERFORM_UPDATE` with `SourceAccelerationStructureData` (#870, #1758). Each refusal still returns `E_INVALIDARG`, publishes no address and logs one line per cause for the first refusal of that kind: `x2w-dxr placed build refused: ( at title 0x...)`. The causes are `unaligned`, `not_placed`, `wrong_state` and `range` for the destination and scratch, and for the source `update_without_source`, `source_without_update`, `source_unaligned`, `source_not_placed`, `source_wrong_state`, `source_range`, `source_dest_partial_overlap`, `source_not_built` and `source_not_updatable`. An update takes its source through the same placement lookup as the destination (a live placed buffer created in the acceleration-structure state, 256-byte aligned), is sized by `UpdateScratchDataSizeInBytes` instead of `ScratchDataSizeInBytes` (WARP reports 0 there; a null scratch is then accepted and a given one must still be a placed buffer), and may run in place (source and destination the same address) or write another structure; a source that partly overlaps the destination is refused. An update needs a source built with `ALLOW_UPDATE` (see the GTX 1060 note above). The shim cannot read the flags back from the GPU, so the resolver records the `ALLOW_UPDATE` flag of every build it publishes, keyed by the structure's title address (a later build over the same memory replaces the record, and an update's destination is recorded with the update's flags), and refuses an update whose source was recorded without it (`source_not_updatable`) or that it never saw built (`source_not_built`; unknown provenance is refused). Nothing is recorded for a refusal. Caller precondition that stays: the source must also have the same geometry layout and instance count as the update; otherwise the device rejects or removes on the update. `placed_build_causes` checks the device-free rules (`src/startup_stubs/placed_build.h`: the update shape, the ledger of recorded builds and the translated-address rule behind `range` and `source_range`, which a real device cannot be made to break) on any platform, and `dxr_placed_build_{hardware,warp}` resolves every refusal that a device can trigger and requires the named cause (`resolve_placed_build` reports it through an optional out parameter): destination and scratch not placed, unaligned or in a buffer of the wrong state, and for an update the missing source, a source without the flag, an unaligned, unplaced or wrong-state source, a partial overlap, a source never built and a source built without `ALLOW_UPDATE`; then an in-place update and an out-of-place one. `dxr_placed_build_{hardware,warp}` also builds a placed BLAS and TLAS with `ALLOW_UPDATE`, records an in-place `PERFORM_UPDATE` of both through the resolver, and reads back four rays after the normal UAV barriers and fence: the hit instance IDs change from 7, 11, 0, 0 to 0, 0, 7, 11, and the rows equal the same update on desktop addresses and a fresh placed build of the updated scene. Limits: out-of-place updates are resolved but not dispatched. +The same resolver now names why it refused a build and accepts `PERFORM_UPDATE` with `SourceAccelerationStructureData` (#870, #1758). Each refusal still returns `E_INVALIDARG`, publishes no address and logs one line per cause for the first refusal of that kind: `x2w-dxr placed build refused: ( at title 0x...)`. The causes are `unaligned`, `not_placed`, `wrong_state` and `range` for the destination and scratch, and for the source `update_without_source`, `source_without_update`, `source_unaligned`, `source_not_placed`, `source_wrong_state`, `source_range`, `source_dest_partial_overlap`, `source_not_built` and `source_not_updatable`. An update takes its source through the same placement lookup as the destination (a live placed buffer created in the acceleration-structure state, 256-byte aligned), is sized by `UpdateScratchDataSizeInBytes` instead of `ScratchDataSizeInBytes` (WARP reports 0 there; a null scratch is then accepted and a given one must still be a placed buffer), and may run in place (source and destination the same address) or write another structure; a source that partly overlaps the destination is refused. An update needs a source built with `ALLOW_UPDATE` (see the GTX 1060 note above). The shim cannot read the flags back from the GPU, so the resolver records the `ALLOW_UPDATE` flag of every build it publishes, keyed by the structure's title address (a later build over the same memory replaces the record, and an update's destination is recorded with the update's flags), and refuses an update whose source was recorded without it (`source_not_updatable`) or that it never saw built (`source_not_built`; unknown provenance is refused). Nothing is recorded for a refusal. Caller precondition that stays: the source must also have the same geometry layout and instance count as the update; otherwise the device rejects or removes on the update. `placed_build_causes` checks the device-free rules (`src/startup_stubs/placed_build.h`: the update shape including the partial overlap, which a structure under 256 bytes cannot reach on a device, the ledger of recorded builds and the translated-address rule behind `range` and `source_range`, which a real device cannot be made to break) on any platform, and `dxr_placed_build_{hardware,warp}` resolves every refusal that a device can trigger and requires the named cause (`resolve_placed_build` reports it through an optional out parameter): destination and scratch not placed, unaligned or in a buffer of the wrong state, and for an update the missing source, a source without the flag, an unaligned, unplaced or wrong-state source, a source never built and a source built without `ALLOW_UPDATE`; then an in-place update and an out-of-place one. `dxr_placed_build_{hardware,warp}` also builds a placed BLAS and TLAS with `ALLOW_UPDATE`, records an in-place `PERFORM_UPDATE` of both through the resolver, and reads back four rays after the normal UAV barriers and fence: the hit instance IDs change from 7, 11, 0, 0 to 0, 0, 7, 11, and the rows equal the same update on desktop addresses and a fresh placed build of the updated scene. Limits: out-of-place updates are resolved but not dispatched. ```powershell ctest --preset release --no-tests=error --output-on-failure -R '^(placed_build_causes|dxr_placed_build_)' diff --git a/tests/dxr_placed_build.h b/tests/dxr_placed_build.h index 428b7966c7..03e516c98d 100644 --- a/tests/dxr_placed_build.h +++ b/tests/dxr_placed_build.h @@ -19,7 +19,8 @@ class PlacedBuildScene { }; Microsoft::WRL::ComPtr guest_; Microsoft::WRL::ComPtr cpu_heap_; - std::array buffers_; + std::array buffers_; + UINT64 big_as_{}; // an acceleration-structure buffer larger than any scratch range the controls use UINT64 tlas_title_{}, scratch_title_{}; struct Placed { UINT64 dest{}, scratch{}; @@ -89,6 +90,7 @@ class PlacedBuildScene { const auto ts = place(3, scratch_offset + scratch_bytes(top_size), scratch_state) + scratch_offset; titles_[0] = {blas, bs, bottom_size}; titles_[1] = {tlas, ts, top_size}; + big_as_ = place(8, 4096, dest_state); const auto short_scratch = place(4, scratch_offset + top_size.ScratchDataSizeInBytes - 1, scratch_state) + scratch_offset; constexpr UINT64 unmapped = 0x1000; // outside the user allocation range // Each refusal names its cause (not only E_INVALIDARG) and publishes nothing. @@ -108,7 +110,7 @@ class PlacedBuildScene { must_reject(tlas, short_scratch, Cause::not_placed, "scratch shorter than the build needs"); must_reject(tlas, unmapped, Cause::not_placed, "scratch outside the placed buffers"); must_reject(tlas, ts + 16, Cause::unaligned, "unaligned scratch"); - must_reject(tlas, tlas + 512, Cause::wrong_state, "scratch in an acceleration-structure buffer"); + must_reject(tlas, big_as_, Cause::wrong_state, "scratch in an acceleration-structure buffer"); check(x2w_startup::resolve_placed_build(guest_.Get(), blas, bs, bottom_size, bottom), "Resolve placed BLAS"); check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, ts, top_size, top), "Resolve placed TLAS"); tlas_title_ = tlas; @@ -128,6 +130,9 @@ class PlacedBuildScene { check(x2w_startup::resolve_placed_build(guest_.Get(), blas, bs, bottom_size, bottom), "Resolve placed BLAS again"); check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, ts, top_size, top), "Resolve placed TLAS again"); check_updates(top, top_size, tlas, ts); + // check_updates records the TLAS as built with ALLOW_UPDATE whatever flags the caller gave it: record the builds as given. + check(x2w_startup::resolve_placed_build(guest_.Get(), blas, bs, bottom_size, bottom), "Resolve placed BLAS as given"); + check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, ts, top_size, top), "Resolve placed TLAS as given"); std::printf("Placed DXR bytes dest/scratch: BLAS=%llu/%llu TLAS=%llu/%llu; short scratch, missing dest and swapped states rejected before recording\n", bottom_size.ResultDataMaxSizeInBytes, bottom_size.ScratchDataSizeInBytes, top_size.ResultDataMaxSizeInBytes, top_size.ScratchDataSizeInBytes); @@ -169,11 +174,16 @@ class PlacedBuildScene { must_reject(tlas, unmapped, scratch, sizes, true, Cause::source_not_placed, "source outside the placed buffers"); must_reject(tlas, scratch, scratch, sizes, true, Cause::source_wrong_state, "source in a buffer that is not an acceleration structure"); - must_reject(tlas, tlas + 256, scratch, sizes, true, Cause::source_dest_partial_overlap, - "source partly overlapping the destination"); - // The destination and scratch rules apply to an update as to a build. - must_reject(tlas + 16, tlas, scratch, sizes, true, Cause::unaligned, "update with an unaligned destination"); - must_reject(tlas, tlas, tlas + 512, sizes, true, Cause::wrong_state, "update with a scratch in an acceleration-structure buffer"); + // An aligned source one stride past the destination is beyond the end of a structure this small (the partial-overlap + // rule needs a structure longer than 256 bytes and is checked by value in placed_build_causes). + must_reject(tlas, tlas + 256, scratch, sizes, true, Cause::source_not_placed, "source past the end of the destination's buffer"); + // The scratch rule applies to an update as to a build. + must_reject(tlas, tlas, big_as_, sizes, true, Cause::wrong_state, "update with a scratch in an acceleration-structure buffer"); + + // The update controls below need the TLAS recorded as built with ALLOW_UPDATE, whatever flags the caller built it with. + auto recorded = request(0, false); + recorded.Inputs.Flags |= allow_flag; + check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, scratch, top_size, recorded), "Record the TLAS with ALLOW_UPDATE"); // In place: the source is the destination. auto in_place = request(tlas, true); From ffb79b62e506c455b13ea0da4ecba4fadc697dcb Mon Sep 17 00:00:00 2001 From: c1 Date: Thu, 26 Nov 2026 12:18:43 +0100 Subject: [PATCH 07/10] test: an update request repeats ALLOW_UPDATE, as a valid update does --- tests/dxr_placed_build.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/dxr_placed_build.h b/tests/dxr_placed_build.h index 03e516c98d..4d520166f6 100644 --- a/tests/dxr_placed_build.h +++ b/tests/dxr_placed_build.h @@ -150,7 +150,8 @@ class PlacedBuildScene { sizes.UpdateScratchDataSizeInBytes = top_size.ScratchDataSizeInBytes; const auto request = [&](UINT64 source, bool update) { auto build = top; - build.Inputs.Flags = update ? (build.Inputs.Flags | update_flag) : (build.Inputs.Flags & ~update_flag); + // A valid update repeats the source's flags, ALLOW_UPDATE included (the structure it writes stays updatable). + build.Inputs.Flags = update ? (build.Inputs.Flags | update_flag | allow_flag) : (build.Inputs.Flags & ~update_flag); build.SourceAccelerationStructureData = source; build.DestAccelerationStructureData = build.ScratchAccelerationStructureData = 0; return build; From 5a79addf711b8934ee45a77906fd4be1be98f8ad Mon Sep 17 00:00:00 2001 From: c1 Date: Thu, 26 Nov 2026 12:19:20 +0100 Subject: [PATCH 08/10] test: the one-stride source control follows the structure size the device reports --- tests/dxr_placed_build.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/dxr_placed_build.h b/tests/dxr_placed_build.h index 4d520166f6..031828c66b 100644 --- a/tests/dxr_placed_build.h +++ b/tests/dxr_placed_build.h @@ -175,9 +175,14 @@ class PlacedBuildScene { must_reject(tlas, unmapped, scratch, sizes, true, Cause::source_not_placed, "source outside the placed buffers"); must_reject(tlas, scratch, scratch, sizes, true, Cause::source_wrong_state, "source in a buffer that is not an acceleration structure"); - // An aligned source one stride past the destination is beyond the end of a structure this small (the partial-overlap - // rule needs a structure longer than 256 bytes and is checked by value in placed_build_causes). - must_reject(tlas, tlas + 256, scratch, sizes, true, Cause::source_not_placed, "source past the end of the destination's buffer"); + // An aligned source one stride (256 bytes) past the destination: where the structure is longer than that the two + // partly overlap (the shape rule, checked by value in placed_build_causes names it); on a device that sizes it + // shorter, the source lies past the end of the destination's buffer. + const Cause stride_cause = x2w_startup::check_update_shape(true, tlas + 256, tlas, sizes.ResultDataMaxSizeInBytes); + must_reject(tlas, tlas + 256, scratch, sizes, true, + stride_cause != Cause::ok ? stride_cause : Cause::source_not_placed, "source one stride past the destination"); + if (stride_cause != Cause::ok && stride_cause != Cause::source_dest_partial_overlap) + throw std::runtime_error("The shape rule named an unexpected cause for a source one stride past the destination"); // The scratch rule applies to an update as to a build. must_reject(tlas, tlas, big_as_, sizes, true, Cause::wrong_state, "update with a scratch in an acceleration-structure buffer"); From b09ac9ddb7085b09d704d12a230ef7eca3172087 Mon Sep 17 00:00:00 2001 From: c1 Date: Thu, 26 Nov 2026 17:16:49 +0100 Subject: [PATCH 09/10] test: DXR placed-build fixture sizes its decoy buffers from the device sizes The controls that name a scratch buffer as a destination, use an acceleration-structure buffer as scratch, or read a 256-byte view at the scratch address sized those buffers to the minimum of one device (WARP). A GPU that reports a result size larger than the scratch size made the range check fail first, so the build named not_placed instead of wrong_state. The decoy buffers now cover the result size, the scratch size and the view. --- tests/dxr_placed_build.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/dxr_placed_build.h b/tests/dxr_placed_build.h index 031828c66b..dbe9cc3748 100644 --- a/tests/dxr_placed_build.h +++ b/tests/dxr_placed_build.h @@ -86,11 +86,19 @@ class PlacedBuildScene { }; const auto blas = place(0, dest_offset + bottom_size.ResultDataMaxSizeInBytes, dest_state) + dest_offset; const auto tlas = place(1, dest_offset + top_size.ResultDataMaxSizeInBytes, dest_state) + dest_offset; - const auto bs = place(2, scratch_offset + scratch_bytes(bottom_size), scratch_state) + scratch_offset; - const auto ts = place(3, scratch_offset + scratch_bytes(top_size), scratch_state) + scratch_offset; + // The controls also name a scratch buffer as a destination (a buffer that is not an acceleration structure) and + // read a 256-byte view at it, so a scratch buffer must cover the result size and that view as well. The sizes + // come from the device: a GPU with a larger result than scratch size must still see a placed buffer here, so the + // refusal under test is the state, not the range. + constexpr UINT64 view_bytes = 256; + const auto decoy_bytes = [&](const auto& sizes) { + return scratch_offset + (std::max)({scratch_bytes(sizes), sizes.ResultDataMaxSizeInBytes, view_bytes}); + }; + const auto bs = place(2, decoy_bytes(bottom_size), scratch_state) + scratch_offset; + const auto ts = place(3, decoy_bytes(top_size), scratch_state) + scratch_offset; titles_[0] = {blas, bs, bottom_size}; titles_[1] = {tlas, ts, top_size}; - big_as_ = place(8, 4096, dest_state); + big_as_ = place(8, (std::max)({UINT64(4096), scratch_bytes(bottom_size), scratch_bytes(top_size)}), dest_state); const auto short_scratch = place(4, scratch_offset + top_size.ScratchDataSizeInBytes - 1, scratch_state) + scratch_offset; constexpr UINT64 unmapped = 0x1000; // outside the user allocation range // Each refusal names its cause (not only E_INVALIDARG) and publishes nothing. From 0b6fe8de4aaa952dd7cbca0b5a6772be3a22a968 Mon Sep 17 00:00:00 2001 From: romanv9 <154753195+romanv9@users.noreply.github.com> Date: Sat, 28 Nov 2026 15:08:47 +0400 Subject: [PATCH 10/10] test: close placed-build review gaps Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- CMakePresets.json | 4 ++-- src/startup_stubs/d3d12_xs_device.cpp | 8 +++++--- tests/dxr_placed_build.h | 28 +++++++++++++++++---------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 19a646b004..f91dfc7766 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -28,7 +28,7 @@ "targets": [ "d3d12_sdk_check", "era_heap_check", "xmem_shim", "offscreen_check", "cpu_fuzz_lifted", "cpu_fuzz_lifted_o2", "pe_map", "intake_candidate", - "archive_gpu_reader", "archive_codec_gpu", "archive_texture_gpu_lifted", "startup_launch_probe", + "archive_gpu_reader", "archive_codec_gpu", "archive_texture_gpu_lifted", "placed_build_causes_check", "startup_launch_probe", "startup_indirect_dispatch", "startup_indirect_draw_indexed" ] }, @@ -42,7 +42,7 @@ "displayName": "Windows pull request smoke tests", "filter": { "include": { - "name": "^(desktop_root_signature|era_heap_(allocation|failure)|xmem_shim_mapped_pages|cpu_aot_fuzz_smoke|cpu_archive_texture_gpu_warp|dual_uav_warp|spirv_.*_warp|static_exe_intake|descriptor_heap_paging(_swapped)?_warp|startup_launch|startup_tools_lint|startup_gfx2hlsl_cvt_off_f32_i4_controls|startup_gfx2hlsl_sad_hi_msad_controls|startup_indirect_(dispatch|count_split|draw(_indexed)?|mesh(_xmem_arguments(_skip_wait)?)?|counts_control)_warp|startup_indirect_mesh_lease_warp_dual(-trace(-disabled)?)?|startup_shader_cache_key)$" + "name": "^(desktop_root_signature|era_heap_(allocation|failure)|xmem_shim_mapped_pages|cpu_aot_fuzz_smoke|cpu_archive_texture_gpu_warp|dual_uav_warp|spirv_.*_warp|static_exe_intake|descriptor_heap_paging(_swapped)?_warp|placed_build_causes|startup_launch|startup_tools_lint|startup_gfx2hlsl_cvt_off_f32_i4_controls|startup_gfx2hlsl_sad_hi_msad_controls|startup_indirect_(dispatch|count_split|draw(_indexed)?|mesh(_xmem_arguments(_skip_wait)?)?|counts_control)_warp|startup_indirect_mesh_lease_warp_dual(-trace(-disabled)?)?|startup_shader_cache_key)$" } } }, diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 4b9d337882..57f5780101 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -7104,12 +7104,14 @@ HRESULT x2w_startup::resolve_placed_build(void* guest_device, UINT64 dest, UINT6 const auto out = translate(dest, sizes.ResultDataMaxSizeInBytes, as_state); if (out.cause != Cause::ok) return refuse(out.cause, "dest", dest); // An update is sized by its own scratch requirement, a full build by the build one. - // A device may report no update scratch at all (WARP does). Then a null scratch is fine, and a scratch the title - // still passes must be a placed buffer, checked over at least one byte. + // A device may report no update scratch at all (WARP does). Then a null update scratch is fine, and a scratch the + // title still passes must be a placed buffer, checked over at least one byte. Keep zero-size full-build scratch + // behavior unchanged. const UINT64 scratch_bytes = update ? sizes.UpdateScratchDataSizeInBytes : sizes.ScratchDataSizeInBytes; const bool no_scratch = update && !scratch_bytes && !scratch; const auto temp = no_scratch ? Translated{Cause::ok, 0} - : translate(scratch, (std::max)(scratch_bytes, 1), D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + : translate(scratch, update ? (std::max)(scratch_bytes, 1) : scratch_bytes, + D3D12_RESOURCE_STATE_UNORDERED_ACCESS); if (temp.cause != Cause::ok) return refuse(temp.cause, "scratch", scratch); Translated from{Cause::ok, 0}; if (update) { diff --git a/tests/dxr_placed_build.h b/tests/dxr_placed_build.h index dbe9cc3748..10743fe970 100644 --- a/tests/dxr_placed_build.h +++ b/tests/dxr_placed_build.h @@ -85,7 +85,8 @@ class PlacedBuildScene { return (std::max)(sizes.ScratchDataSizeInBytes, sizes.UpdateScratchDataSizeInBytes); }; const auto blas = place(0, dest_offset + bottom_size.ResultDataMaxSizeInBytes, dest_state) + dest_offset; - const auto tlas = place(1, dest_offset + top_size.ResultDataMaxSizeInBytes, dest_state) + dest_offset; + const auto overlap_test_bytes = (std::max)(top_size.ResultDataMaxSizeInBytes, UINT64{512}); + const auto tlas = place(1, dest_offset + overlap_test_bytes + 256, dest_state) + dest_offset; // The controls also name a scratch buffer as a destination (a buffer that is not an acceleration structure) and // read a 256-byte view at it, so a scratch buffer must cover the result size and that view as well. The sizes // come from the device: a GPU with a larger result than scratch size must still see a placed buffer here, so the @@ -119,6 +120,15 @@ class PlacedBuildScene { must_reject(tlas, unmapped, Cause::not_placed, "scratch outside the placed buffers"); must_reject(tlas, ts + 16, Cause::unaligned, "unaligned scratch"); must_reject(tlas, big_as_, Cause::wrong_state, "scratch in an acceleration-structure buffer"); + auto no_build_scratch = top_size; + no_build_scratch.ScratchDataSizeInBytes = 0; + auto no_build_scratch_build = top; + Cause no_build_scratch_cause = Cause::ok; + const HRESULT no_build_scratch_hr = x2w_startup::resolve_placed_build( + guest_.Get(), tlas, ts, no_build_scratch, no_build_scratch_build, &no_build_scratch_cause); + if (no_build_scratch_hr != E_INVALIDARG || no_build_scratch_cause != Cause::not_placed || + no_build_scratch_build.DestAccelerationStructureData || no_build_scratch_build.ScratchAccelerationStructureData) + throw std::runtime_error("A full build with zero scratch size was not refused as not_placed"); check(x2w_startup::resolve_placed_build(guest_.Get(), blas, bs, bottom_size, bottom), "Resolve placed BLAS"); check(x2w_startup::resolve_placed_build(guest_.Get(), tlas, ts, top_size, top), "Resolve placed TLAS"); tlas_title_ = tlas; @@ -183,14 +193,12 @@ class PlacedBuildScene { must_reject(tlas, unmapped, scratch, sizes, true, Cause::source_not_placed, "source outside the placed buffers"); must_reject(tlas, scratch, scratch, sizes, true, Cause::source_wrong_state, "source in a buffer that is not an acceleration structure"); - // An aligned source one stride (256 bytes) past the destination: where the structure is longer than that the two - // partly overlap (the shape rule, checked by value in placed_build_causes names it); on a device that sizes it - // shorter, the source lies past the end of the destination's buffer. - const Cause stride_cause = x2w_startup::check_update_shape(true, tlas + 256, tlas, sizes.ResultDataMaxSizeInBytes); - must_reject(tlas, tlas + 256, scratch, sizes, true, - stride_cause != Cause::ok ? stride_cause : Cause::source_not_placed, "source one stride past the destination"); - if (stride_cause != Cause::ok && stride_cause != Cause::source_dest_partial_overlap) - throw std::runtime_error("The shape rule named an unexpected cause for a source one stride past the destination"); + // Make the source range fit even on devices with small structures, so this control specifically exercises the + // partial-overlap rule rather than a placement boundary. + auto overlap_sizes = sizes; + overlap_sizes.ResultDataMaxSizeInBytes = (std::max)(sizes.ResultDataMaxSizeInBytes, UINT64{512}); + must_reject(tlas, tlas + 256, scratch, overlap_sizes, true, Cause::source_dest_partial_overlap, + "source one stride past the destination"); // The scratch rule applies to an update as to a build. must_reject(tlas, tlas, big_as_, sizes, true, Cause::wrong_state, "update with a scratch in an acceleration-structure buffer"); @@ -250,7 +258,7 @@ class PlacedBuildScene { // Re-resolve one placed structure (0 = BLAS, 1 = TLAS) as an in-place PERFORM_UPDATE of itself. The source is the // title address the structure was built at, so the update runs through the same slot-133 placement lookup as the - // build. The caller keeps ALLOW_UPDATE from the initial build; the resolver cannot see it. + // build. The resolver also verifies that the initial build used ALLOW_UPDATE. void resolve_update(unsigned which, D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& build) { const auto& placed = titles_.at(which); build.Inputs.Flags |= D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE;