From 0e1e570e3ac2006952e2175758c93e53a69f80a8 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 19:50:10 +0100 Subject: [PATCH] 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 335f829a3a..1e41430a7c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,6 +104,13 @@ foreach(scenario IN ITEMS controls lifetime) add_test(NAME era_physical_${scenario} COMMAND era_physical_check ${scenario}) endforeach() +# 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 605a9b7eed..0113519c70 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -7452,6 +7452,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 8c96001c34..7793c6f914 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -3495,27 +3495,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; +}