From 224d6fd509b491ef1e5d5e80da385c32ca566214 Mon Sep 17 00:00:00 2001 From: Nivek Leahy <360009221+potatoeking5150-commits@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:42:28 -0500 Subject: [PATCH] Add wave-based UAV atomic waterfall experiment --- CMakeLists.txt | 11 ++++- docs/experiments.md | 3 +- tests/desktop_gpu.h | 1 + tests/gpu_alias_atomic_waterfall.hlsl | 42 +++++++++++++++++++ tests/gpu_alias_lookup.cpp | 58 ++++++++++++++++++++++++++- tests/offscreen_check.cpp | 5 +++ 6 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 tests/gpu_alias_atomic_waterfall.hlsl diff --git a/CMakeLists.txt b/CMakeLists.txt index e30f4c68..16f472ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,6 +184,14 @@ add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/ProbeArrayAtomics.h" COMM "${CMAKE_CURRENT_SOURCE_DIR}/tests/gpu_alias_writes.hlsl" DEPENDS tests/gpu_alias_writes.hlsl src/title_va_lookup.hlsli BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/ProbeArrayAtomics.cso" VERBATIM) target_sources(offscreen_check PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/ProbeArrayAtomics.h") +add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/ProbeArrayAtomicsWaterfall.h" COMMAND "${DXC_EXECUTABLE}" -T cs_6_0 + -E ProbeArrayAtomicsWaterfall -Vn ProbeArrayAtomicsWaterfall + -Fh "${CMAKE_CURRENT_BINARY_DIR}/ProbeArrayAtomicsWaterfall.h" + -Fo "${CMAKE_CURRENT_BINARY_DIR}/ProbeArrayAtomicsWaterfall.cso" + "${CMAKE_CURRENT_SOURCE_DIR}/tests/gpu_alias_atomic_waterfall.hlsl" + DEPENDS tests/gpu_alias_atomic_waterfall.hlsl + BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/ProbeArrayAtomicsWaterfall.cso" VERBATIM) +target_sources(offscreen_check PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/ProbeArrayAtomicsWaterfall.h") target_include_directories(offscreen_check PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src") add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/AliasTransform.h" COMMAND "${DXC_EXECUTABLE}" -T cs_6_0 -E AliasTransform -Vn AliasTransform -Fh "${CMAKE_CURRENT_BINARY_DIR}/AliasTransform.h" @@ -326,9 +334,10 @@ foreach(adapter IN ITEMS hardware warp) add_test(NAME gpu_alias_${adapter} COMMAND offscreen_check ${adapter} --gpu-alias) add_test(NAME gpu_alias_lookup_${adapter} COMMAND offscreen_check ${adapter} --gpu-alias-lookup) add_test(NAME gpu_alias_lookup_writes_${adapter} COMMAND offscreen_check ${adapter} --gpu-alias-lookup-writes) + add_test(NAME gpu_alias_atomic_waterfall_${adapter} COMMAND offscreen_check ${adapter} --gpu-alias-atomic-waterfall) add_test(NAME gpu_alias_registry_${adapter} COMMAND offscreen_check ${adapter} --gpu-alias-registry) set_tests_properties(gpu_alias_${adapter} gpu_alias_lookup_${adapter} gpu_alias_lookup_writes_${adapter} - gpu_alias_registry_${adapter} PROPERTIES TIMEOUT 45 SKIP_RETURN_CODE 77) + gpu_alias_atomic_waterfall_${adapter} gpu_alias_registry_${adapter} PROPERTIES TIMEOUT 45 SKIP_RETURN_CODE 77) add_test(NAME bvh_root_roundtrip_${adapter} COMMAND offscreen_check ${adapter} --bvh-root-roundtrip) set_tests_properties(bvh_root_roundtrip_${adapter} PROPERTIES TIMEOUT 45) foreach(work IN ITEMS reads waves wave64 hits differential atomics numeric packed) diff --git a/docs/experiments.md b/docs/experiments.md index 8ccaf83e..3df8a8a3 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -2764,7 +2764,8 @@ Measured at commit `55cea41` on Windows 11 build 26300, with the debug layer una - At commit `65492448` on Windows 10 22H2 (build 19045.6456, MSVC 19.44.35230.0, Windows SDK 10.0.26100.0 DXC, debug layer enabled on both backends), a GTX 1060 6 GB (driver 32.0.15.6094) reads 64 in all four probe buffers in 3/3 runs, and the in-box WARP 10.0.19041.5794 on the same host reproduces the Windows 10 behavior above: each buffer reads 256 in 3/3 runs, with the fixture reporting the shared add and sub words as the only two mismatches. Both backends matched all 1,047 per-record results and the other 147,454 words, with zero debug errors or warnings ([report](https://github.com/R0Y00/Xbox2Windows/issues/369#issuecomment-7071770464)). - At commit `185b9d6c` on Windows 11 build 26200 with the debug layer enabled, an RDNA 3 adapter reproduces the same probe failure: an AMD Radeon RX 7900 XTX (device `744c`, driver 32.0.32029.2010) reads 256 in each of the four buffers, with the title-address fixture recording zero mismatches across all 1,047 records and 147,454 checked words and zero D3D12 debug errors or warnings ([report](https://github.com/R0Y00/Xbox2Windows/issues/369)). -## Offscreen rendering and readback +- At commit `a4ade3032fec08767ed411cc83834a3543c626f2`, an AMD Radeon RX 6500 (device `743f`, RDNA 2, driver 32.0.21045.29003, Windows 11 build 26300, debug layer enabled) reproduces the dynamic-index atomic probe failure: all four buffers read 256 instead of 64. The title-address lookup/write fixture still reports zero mismatches across its add/sub results, 1,047 per-record results and 147,454 other words, with zero debug errors or warnings. A separate experimental shader uses wave operations to group lanes by descriptor index and issues one `InterlockedAdd` per distinct index. It reads 64 in all four buffers in four consecutive manual runs and in the registered `gpu_alias_atomic_waterfall_hardware` CTest. The same experiment passes on in-box WARP 10.0.26100.10068 (`gpu_alias_atomic_waterfall_warp`). Both CTest runs report zero debug errors or warnings. In a combined CTest run, the original lookup/write test fails only its atomic probe while the waterfall test passes. This demonstrates a working alternative on this RX 6500, but does not establish the underlying driver or hardware cause. + ## Offscreen rendering and readback `offscreen_hardware` selects a high-performance adapter. `offscreen_warp` selects Microsoft's software rasterizer. Both draw the same DXC-compiled triangle to a 64×64 RGBA8 texture. After GPU completion the test reads the texture using its row pitch and compares colors and coverage pixel by pixel. Outputs are `triangle_hardware.bmp` and `triangle_warp.bmp` in the build directory. `ctest --preset dev -V` reports the adapter, driver, debug layer and pixel differences. Error or corruption messages from an available debug layer fail the test. diff --git a/tests/desktop_gpu.h b/tests/desktop_gpu.h index 9c9583df..d9795411 100644 --- a/tests/desktop_gpu.h +++ b/tests/desktop_gpu.h @@ -59,6 +59,7 @@ bool check_bvh_waves(ID3D12Device* device, bool force64); bool check_gpu_alias(ID3D12Device* device); bool check_gpu_alias_lookup(ID3D12Device* device); bool check_gpu_alias_lookup_writes(ID3D12Device* device); +bool check_gpu_alias_atomic_waterfall(ID3D12Device* device); bool check_gpu_alias_registry(ID3D12Device* device); bool check_wave_shuffle(ID3D12Device* device); bool check_dxr_rays(ID3D12Device* device, bool usePipeline); diff --git a/tests/gpu_alias_atomic_waterfall.hlsl b/tests/gpu_alias_atomic_waterfall.hlsl new file mode 100644 index 00000000..4ae08277 --- /dev/null +++ b/tests/gpu_alias_atomic_waterfall.hlsl @@ -0,0 +1,42 @@ +#define ProbeRoot "DescriptorTable(UAV(u0, space = 1, numDescriptors = 8)), RootConstants(num32BitConstants = 2, b0)" + +RWByteAddressBuffer Aliases[8] : register(u0, space1); + +cbuffer Counts : register(b0) +{ + uint rangeCount; + uint threadCount; +}; + +[RootSignature(ProbeRoot)] +[numthreads(64, 1, 1)] +void ProbeArrayAtomicsWaterfall(uint id : SV_DispatchThreadID) +{ + if (id >= threadCount) return; + + uint selected = id % rangeCount; + bool remaining = true; + + while (WaveActiveAnyTrue(remaining)) + { + uint leaderLane = WaveActiveMin( + remaining ? WaveGetLaneIndex() : 0xffffffff + ); + + uint groupIndex = WaveReadLaneAt(selected, leaderLane); + uint groupCount = WaveActiveCountBits( + remaining && selected == groupIndex + ); + + if (remaining && WaveGetLaneIndex() == leaderLane) + { + uint previous; + Aliases[NonUniformResourceIndex(groupIndex)].InterlockedAdd( + 0, groupCount, previous + ); + } + + if (remaining && selected == groupIndex) + remaining = false; + } +} diff --git a/tests/gpu_alias_lookup.cpp b/tests/gpu_alias_lookup.cpp index 0e1db7cf..eb6e0af3 100644 --- a/tests/gpu_alias_lookup.cpp +++ b/tests/gpu_alias_lookup.cpp @@ -10,6 +10,7 @@ #include "era_title_va.h" #include "LookupTitleAddresses.h" #include "ProbeArrayAtomics.h" +#include "ProbeArrayAtomicsWaterfall.h" #include "WriteTitleAddresses.h" #include @@ -246,8 +247,63 @@ bool array_atomics_exact(ID3D12Device* device) counts[0], counts[1], counts[2], counts[3]); return std::all_of(counts.begin(), counts.end(), [=](Word count) { return count == expected; }); } +bool array_atomics_waterfall_exact(ID3D12Device* device) +{ + constexpr UINT targets = 4, threads = 256; + Kernel kernel(device, {ProbeArrayAtomicsWaterfall, sizeof(ProbeArrayAtomicsWaterfall)}); + D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, descriptor_count, + D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; + ComPtr heap; + check(device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&heap)), "Create waterfall probe descriptor heap"); + const UINT stride = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + std::array, targets> buffers; + for (UINT slot = 0; slot < descriptor_count; ++slot) { + D3D12_UNORDERED_ACCESS_VIEW_DESC view{}; + view.Format = DXGI_FORMAT_R32_TYPELESS; + view.ViewDimension = D3D12_UAV_DIMENSION_BUFFER; + view.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW; + view.Buffer.NumElements = 1; + ID3D12Resource* resource = nullptr; + if (slot < targets) { + buffers[slot] = make_buffer(device, sizeof(Word), D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COMMON, + D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS); + resource = buffers[slot].Get(); + } + D3D12_CPU_DESCRIPTOR_HANDLE cpu = heap->GetCPUDescriptorHandleForHeapStart(); + cpu.ptr += static_cast(slot) * stride; + device->CreateUnorderedAccessView(resource, nullptr, &view, cpu); + } + auto zero = upload(device, std::vector(1, 0)); + auto back = make_buffer(device, targets * sizeof(Word), D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); + ComputeCommands cmd(device, kernel.pipeline.Get()); + auto* list = cmd.list.Get(); + for (auto& buffer : buffers) { + list->CopyBufferRegion(buffer.Get(), 0, zero.Get(), 0, sizeof(Word)); + transition(list, buffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + } + ID3D12DescriptorHeap* heaps[] = {heap.Get()}; + list->SetDescriptorHeaps(1, heaps); + list->SetComputeRootSignature(kernel.root.Get()); + list->SetComputeRootDescriptorTable(0, heap->GetGPUDescriptorHandleForHeapStart()); + list->SetComputeRoot32BitConstant(1, targets, 0); + list->SetComputeRoot32BitConstant(1, threads, 1); + list->Dispatch(threads / 64, 1, 1); + for (UINT i = 0; i < targets; ++i) { + transition(list, buffers[i].Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE); + list->CopyBufferRegion(back.Get(), i * sizeof(Word), buffers[i].Get(), 0, sizeof(Word)); + } + cmd.submit_and_wait(device); + const auto counts = read(back.Get(), targets); + const Word expected = threads / targets; + std::printf("array_atomics_waterfall threads=%u buffers=%u expected=%u actual=%u,%u,%u,%u\n", threads, + targets, expected, counts[0], counts[1], counts[2], counts[3]); + return std::all_of(counts.begin(), counts.end(), [=](Word count) { return count == expected; }); +} +} +bool check_gpu_alias_atomic_waterfall(ID3D12Device* device) +{ + return array_atomics_waterfall_exact(device); } - bool check_gpu_alias_lookup(ID3D12Device* device) { AliasFixture fixture; diff --git a/tests/offscreen_check.cpp b/tests/offscreen_check.cpp index 99ba82a7..8db3f0c3 100644 --- a/tests/offscreen_check.cpp +++ b/tests/offscreen_check.cpp @@ -793,6 +793,10 @@ OperationResult run_gpu_alias_lookup_writes(const OperationContext& ctx) return skip_if_false(check_gpu_alias_lookup_writes(ctx.device)); } +OperationResult run_gpu_alias_atomic_waterfall(const OperationContext& ctx) +{ + return skip_if_false(check_gpu_alias_atomic_waterfall(ctx.device)); +} OperationResult run_gpu_alias_registry(const OperationContext& ctx) { return skip_if_false(check_gpu_alias_registry(ctx.device)); @@ -905,6 +909,7 @@ const OperationSpec kOperations[] = { {"--gpu-alias", 3, 3, nullptr, kNoGate, run_gpu_alias}, {"--gpu-alias-lookup", 3, 3, nullptr, kNoGate, run_gpu_alias_lookup}, {"--gpu-alias-lookup-writes", 3, 3, nullptr, kNoGate, run_gpu_alias_lookup_writes}, + {"--gpu-alias-atomic-waterfall", 3, 3, nullptr, kNoGate, run_gpu_alias_atomic_waterfall}, {"--gpu-alias-registry", 3, 3, nullptr, kNoGate, run_gpu_alias_registry}, {"--buffer-ops", 3, 3, nullptr, kGateSm60BufferOps, run_buffer_ops}, {"--wave-shuffle", 3, 3, nullptr, kGateWaveShuffle, run_wave_shuffle},