Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <cause> (<dest|scratch|source> 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)

Expand Down
53 changes: 39 additions & 14 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t>(Cause::count)];
const auto refuse = [&](Cause cause, const char* role, UINT64 address) {
if (first_calls(logged[static_cast<std::size_t>(cause)], 1))
log("x2w-dxr placed build refused: %s (%s at title 0x%llx)", x2w_startup::cause_name(cause), role,
static_cast<unsigned long long>(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<const Wrapper*>(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<UINT64>(found.owner->own().title_memory);
const auto gpu = static_cast<ID3D12Resource*>(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;
}

Expand Down
90 changes: 90 additions & 0 deletions src/startup_stubs/placed_build.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,91 @@
#pragma once

#include <cstddef>
#include <cstdint>

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 <d3d12.h>

namespace x2w_startup {
Expand All @@ -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
55 changes: 54 additions & 1 deletion tests/dxr_placed_build.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "startup_stubs/placed_build.h"
#include <array>
#include <cstdio>
#include <stdexcept>
#include <string>

namespace dxr_fixture {
// These are the existing slot-133 and IUnknown contracts, called on the
Expand All @@ -15,7 +17,7 @@ class PlacedBuildScene {
~Buffer() { wrapper.Reset(); if (memory) VirtualFree(memory, 0, MEM_RELEASE); }
};
Microsoft::WRL::ComPtr<IUnknown> guest_;
std::array<Buffer, 7> buffers_;
std::array<Buffer, 8> buffers_;

public:
// Wrong-routing control (issue #874): also build a decoy BLAS from another triangle into its own placed
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Loading
Loading