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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,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)
Expand Down
4 changes: 2 additions & 2 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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", "startup_present_readback_check",
"xmem_shim_test_controls", "startup_stub_xcrdapi", "startup_stub_xcrdapi_break_window_control",
"xmem_run_break_readers_check", "xmem_run_break_readers_control", "xmem_cross_call_merge_check",
Expand All @@ -48,7 +48,7 @@
"displayName": "Windows pull request smoke tests",
"filter": {
"include": {
"name": "^(bcpack_all_formats(_golden_control)?|startup_bcpack_storage|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|startup_present_readback|xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first)(_[a-z_]+)?|startup_dstorage_break_window(_[a-z_]+)?|xmem_page_runs_device(_join)?_warp|linear_buffer_backing_share_warp|startup_stage_package)$"
"name": "^(bcpack_all_formats(_golden_control)?|startup_bcpack_storage|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|startup_present_readback|xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first)(_[a-z_]+)?|startup_dstorage_break_window(_[a-z_]+)?|xmem_page_runs_device(_join)?_warp|linear_buffer_backing_share_warp|startup_stage_package)$"
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -11547,6 +11547,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`, `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_)'
```


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
80 changes: 64 additions & 16 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7748,29 +7748,77 @@ 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)
{
if (build.SourceAccelerationStructureData ||
(build.Inputs.Flags & D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE))
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<std::size_t>(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<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 (const Cause usable = x2w_startup::gpu_range_cause(gpu, offset); usable != Cause::ok) return {usable, 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.
// 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, update ? (std::max<UINT64>)(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) {
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;
}

Expand Down
Loading
Loading