From da888e466253b21f6e616b2d12da0478ae6c0065 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Tue, 24 Nov 2026 21:21:01 +0100 Subject: [PATCH 1/7] startup: publish single-element 2D and 3D textures by address in device slot 70 A 1x1 2D or 1x1x1 3D texture with one mip, one sample and a plain colour format keeps its only texel at offset 0 for layout UNKNOWN, linear and the swizzle modes without pipe/bank XOR, so the memory SRV call publishes it like the address-only tiled views and the draw uploads that texel through the footprint path, with the same change detection. XOR layouts, larger shapes, mip chains, arrays, multisampling, block-compressed formats, R8 (placed path) and GPU-written flags stay refused. startup_single_element_srv checks the descriptions without a device (an XOR-layout mutation is the negative control); startup_address_single_element_{warp,hardware} draws eight columns per dimension, including a texel rewritten and published again and ten refused descriptions. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 12 +++ docs/experiments.md | 11 ++ src/startup_stubs/d3d12_draw.inl | 10 +- src/startup_stubs/d3d12_memory_srv.inl | 12 ++- src/startup_stubs/d3d12_xs_device.cpp | 1 + src/startup_stubs/single_element_srv.h | 55 ++++++++++ tests/startup_address_srv.cpp | 71 ++++++++++++ tests/startup_single_element_srv_check.cpp | 119 +++++++++++++++++++++ tests/startup_single_element_srv_check.py | 57 ++++++++++ tests/startup_stream_draw.cpp | 3 + 10 files changed, 346 insertions(+), 5 deletions(-) create mode 100644 src/startup_stubs/single_element_srv.h create mode 100644 tests/startup_single_element_srv_check.cpp create mode 100644 tests/startup_single_element_srv_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d4f424a67..6804efbf01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1538,6 +1538,14 @@ target_include_directories(startup_bc64_supports_check PRIVATE src) add_test(NAME startup_bc64_supports COMMAND startup_bc64_supports_check) add_test(NAME startup_bc64_supports_layout_control COMMAND startup_bc64_supports_check --mutate layout) set_tests_properties(startup_bc64_supports_layout_control PROPERTIES WILL_FAIL TRUE) +# Which descriptions take the single-element memory SRV path (src/startup_stubs/single_element_srv.h), without +# a device. The mutated run expects an XOR layout to be accepted and is the negative control. +add_executable(startup_single_element_srv_check tests/startup_single_element_srv_check.cpp) +target_compile_features(startup_single_element_srv_check PRIVATE cxx_std_17) +target_include_directories(startup_single_element_srv_check PRIVATE src) +add_test(NAME startup_single_element_srv COMMAND startup_single_element_srv_check) +add_test(NAME startup_single_element_srv_xor_control COMMAND startup_single_element_srv_check --mutate xor) +set_tests_properties(startup_single_element_srv_xor_control PROPERTIES WILL_FAIL TRUE) # Slot-10 tile plan for layout 0x10a (src/startup_stubs/tile64_plan.h) without a device: tile shapes, # accepted descriptions and hand-worked plans. The mutated runs are negative controls. add_executable(startup_tile64_plan_check tests/startup_tile64_plan_check.cpp) @@ -2051,6 +2059,10 @@ if(EXISTS "${INTAKE_PYTHON}") "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_bc_view_check.py" "$" "${backend}") set_tests_properties(startup_address_bc_view_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) + add_test(NAME startup_address_single_element_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" + "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_single_element_srv_check.py" + "$" "${backend}") + set_tests_properties(startup_address_single_element_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) add_test(NAME startup_xg_bc_chain_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_xg_bc_chain_check.py" "$" "$" "${backend}") diff --git a/docs/experiments.md b/docs/experiments.md index 8b6fe1c158..e3cd24edff 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11006,6 +11006,17 @@ The fixture checks the XG layout, tile spans, block offsets, copy in and out, an ctest --preset release --no-tests=error --output-on-failure -R '^startup_xg_bc_chain_' ``` +### Single-element 2D and 3D textures published by address + +Device slot 70 publishes a 1×1 2D or 1×1×1 3D texture by address, without a resource object. It must have one mip and one sample, and a plain colour format whose element is one texel of 1 to 16 bytes. R8_UNORM keeps its placed-texture path. The view must name the whole texture with the default mapping. The only element is read from offset 0 of the guest memory. That holds for layout UNKNOWN, linear (`0x100`) and the swizzle modes without pipe/bank XOR (`0x101`–`0x10B`). A swizzle address is a block offset plus a bit pattern of the coordinates, both zero at the origin. AMD addrlib places a single mip at offset 0 (`Gfx10Lib::ComputeSurfaceInfoMacroTiled` at the pinned [revision](https://gitlab.freedesktop.org/mesa/mesa/-/tree/5c142e46f3f6e752ed745fd48912ebb8fad67145/src/amd/addrlib)). XOR modes (`0x110` and up) can move the origin and stay refused, as do larger shapes, mip chains, arrays, multisampling, block-compressed formats and render-target, depth or UAV flags. The translated draw uploads the texel through the footprint path. It re-reads the texel when the view is published again and once per frame, like the other address-only views. + +`startup_single_element_srv` checks the accepted and refused descriptions without a device; its `xor_control` expects layout `0x115` to be accepted and must fail. `startup_address_single_element_{warp,hardware}` draws eight columns per dimension from a translated shader that loads texel zero. The columns are RGBA8 at layouts 0 and `0x105`, the first texel rewritten and published again, B8G8R8A8 and R16G16B16A16_FLOAT. Ten refused descriptions and a zero address must leave the previous view in place. On Windows 11 build 26300 with the debug layer off, RTX 5090 (32.0.16.2135) and WARP (10.0.26100.10068) matched all 32 RGBA pixels per dimension. With the runtime change reverted, column 0 reads zero. An upload that skips the texel after its first copy fails column 2, which then reads the old texel. + +```powershell +cmake --build --preset release --target startup_stream_consumer startup_single_element_srv_check d3d12_xs_shim +ctest --preset release --no-tests=error --output-on-failure -R '^startup_(single_element_srv|address_single_element_)' +``` + ### Placed vertex buffers consumed by a translated draw `startup_placed_vertex_{hardware,warp}` opens two real slot-133 buffers on CPU-visible allocations with bases separated by 1 TiB. Slot 48 supplies the full guest address and byte length to a translated vertex-stream binding. The shader can read from the registered GPU buffer directly when the resource aliases the guest memory, the length covers whole DWORDs and the start is aligned to 16 bytes. Views crossing a registered boundary or overlapping multiple live placements on the list's device produce a null descriptor before staging. Final wrapper release removes the placement from binding lookup. diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 7879f94eb3..fad9c2f70b 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -643,7 +643,9 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) auto* const memory = static_cast(resource->own().title_memory); auto* list = recording(self); const D3D12_RESOURCE_DESC desc = texture->GetDesc(); - const bool uploadable = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D || + // An address-only single-element texture (2D or 3D) uploads its one texel through the footprint path. + const bool single = resource->own().tiled_address_srv && x2w_startup::single_element_bytes(desc) != 0; + const bool uploadable = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D || single || (desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D && desc.DepthOrArraySize == 1 && desc.Height == 1); { static LONG reasons; @@ -684,9 +686,9 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) desc.Format != DXGI_FORMAT_BC7_UNORM || desc.Width != surface.columns * surface.block_texels || desc.Height != surface.rows * surface.block_texels) return; } - if (tiled && !tiled_chain && (plan.count != 1 || plan.rows[0] != surface.rows || + if (tiled && !tiled_chain && !single && (plan.count != 1 || plan.rows[0] != surface.rows || plan.row_bytes[0] != u64{surface.columns} * surface.block_bytes)) return; - const u64 total = tiled_chain ? x2w_startup::bc64::Chain(original->console).bytes() : tiled ? surface.size() : stride * (slices - 1) + slice_total; + const u64 total = tiled_chain ? x2w_startup::bc64::Chain(original->console).bytes() : tiled && !single ? surface.size() : stride * (slices - 1) + slice_total; const bool announced = state.generation != generation; const UINT elapsed = static_cast(frame_number) - static_cast(state.checked_frame); const bool recheck = (tiled_address || tiled_chain) ? elapsed != 0 @@ -721,7 +723,7 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) plan.upload(list, texture, upload_ring, mips, slices, slice, i, offset); } } - } else if (tiled) { + } else if (tiled && !single) { u64 offset = 0; const auto bytes = plan.bytes(0); auto* rows = reserve_upload_ring(static_cast(bytes), &offset); diff --git a/src/startup_stubs/d3d12_memory_srv.inl b/src/startup_stubs/d3d12_memory_srv.inl index a0c7eca7ef..8bc8dd0b4f 100644 --- a/src/startup_stubs/d3d12_memory_srv.inl +++ b/src/startup_stubs/d3d12_memory_srv.inl @@ -41,6 +41,7 @@ bool supports_memory_srv(const D3D12_RESOURCE_DESC& r, const D3D12_SHADER_RESOUR v.Texture2D.MipLevels <= r.MipLevels - v.Texture2D.MostDetailedMip && !v.Texture2D.PlaneSlice && v.Texture2D.ResourceMinLODClamp == 0; } + if (x2w_startup::supports_single_element_srv(r, v)) return true; return r.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D && r.Width && r.Height && r.DepthOrArraySize == 1 && r.MipLevels == 1 && r.SampleDesc.Count == 1 && r.SampleDesc.Quality == 0 && (r.Format == DXGI_FORMAT_R8_UNORM || tiled_bc_srv(r)) && v.Format == r.Format && !(r.Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE) && @@ -135,7 +136,8 @@ std::unordered_multimap tiled_srv_addresses; Wrapper* publish_tiled_texture(Wrapper* device, u64 address, const D3D12_RESOURCE_DESC& desc) { - const auto bytes = x2w_startup::bc64::supports(desc) ? x2w_startup::bc64::Chain(desc).bytes() : x2w_startup::Bc16Surface(desc).size(); + const UINT element = x2w_startup::single_element_bytes(desc); + const auto bytes = element ? u64{element} : x2w_startup::bc64::supports(desc) ? x2w_startup::bc64::Chain(desc).bytes() : x2w_startup::Bc16Surface(desc).size(); // The transient upload ring accepts individual copies up to 64 MiB. if (bytes > (64ull << 20) || !x2w_local::readable(reinterpret_cast(address), bytes)) return nullptr; std::lock_guard lock(tiled_srv_mutex); @@ -213,6 +215,14 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou if (first_calls(refused, 8)) log_refused_memory_srv("unsupported memory SRV arguments", reason, resource, input, destination); return; } + // A single-element texture is published by address like a tiled view: its texel is read at offset 0. + if (x2w_startup::supports_single_element_srv(*resource, input->view)) { + if (Wrapper* texture = publish_tiled_texture(self, input->address, *resource)) + view_slot<18, 2>(self, reinterpret_cast(texture), reinterpret_cast(&input->view), destination, 0); + else if (first_calls(refused, 8)) + log_refused_memory_srv("unavailable texture at the supplied base and layout", "texture_unavailable", resource, input, destination); + return; + } if (resource->Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) { const u64 elem_bytes = x2w_startup::buffer_srv_element_bytes(input->view); const u64 span_bytes = (input->view.Buffer.FirstElement + input->view.Buffer.NumElements) * elem_bytes; diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index ce5d96ed41..6f463aa447 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -27,6 +27,7 @@ #include "startup_stubs/placed_build.h" #include "startup_stubs/memory_srv_report.h" #include "startup_stubs/memory_srv_buffer.h" +#include "startup_stubs/single_element_srv.h" #include "startup_stubs/slice_footprints.h" diff --git a/src/startup_stubs/single_element_srv.h b/src/startup_stubs/single_element_srv.h new file mode 100644 index 0000000000..7d4a4a2a5d --- /dev/null +++ b/src/startup_stubs/single_element_srv.h @@ -0,0 +1,55 @@ +#pragma once + +#include + +namespace x2w_startup { +// One texel, one mip, one sample, 2D or 3D. In every layout accepted here the only element sits at offset 0 +// of the title memory: UNKNOWN, linear (0x100) and the swizzle modes without pipe/bank XOR (0x101..0x10B; a +// swizzle address is block index * block size + a bit pattern of the coordinates, both zero at the origin). +// XOR modes (0x110 and up) can move the origin by the surface's pipe/bank XOR and are refused. Formats are +// plain colour formats whose element is one texel with the same bytes on desktop; R8_UNORM keeps its placed- +// texture path, and block-compressed, planar, depth and packed console formats are refused. Returns the +// element size in bytes, or 0 when the description is not such a texture. +inline UINT single_element_bytes(const D3D12_RESOURCE_DESC& r) +{ + const UINT layout = static_cast(r.Layout); + if ((r.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D && r.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE3D) || + r.Width != 1 || r.Height != 1 || r.DepthOrArraySize != 1 || r.MipLevels != 1 || r.SampleDesc.Count != 1 || + r.SampleDesc.Quality != 0 || (layout != 0 && (layout < 0x100 || layout > 0x10B))) + return 0; + // GPU-written textures are never uploaded from title memory. + if (r.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | + D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE)) + return 0; + switch (r.Format) { + case DXGI_FORMAT_R32G32B32A32_FLOAT: + return 16; + case DXGI_FORMAT_R16G16B16A16_FLOAT: case DXGI_FORMAT_R16G16B16A16_UNORM: + return 8; + case DXGI_FORMAT_R10G10B10A2_UNORM: case DXGI_FORMAT_R11G11B10_FLOAT: case DXGI_FORMAT_R8G8B8A8_UNORM: + case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: case DXGI_FORMAT_R8G8B8A8_UINT: case DXGI_FORMAT_R8G8B8A8_SNORM: + case DXGI_FORMAT_R8G8B8A8_SINT: case DXGI_FORMAT_R16G16_FLOAT: case DXGI_FORMAT_R16G16_UNORM: + case DXGI_FORMAT_R32_FLOAT: case DXGI_FORMAT_B8G8R8A8_UNORM: case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB: + return 4; + case DXGI_FORMAT_R8G8_UNORM: case DXGI_FORMAT_R16_FLOAT: case DXGI_FORMAT_R16_UNORM: + return 2; + case DXGI_FORMAT_A8_UNORM: + return 1; + default: + return 0; + } +} + +// The view must name the whole texture with its own format, dimension and the default mapping. +inline bool supports_single_element_srv(const D3D12_RESOURCE_DESC& r, const D3D12_SHADER_RESOURCE_VIEW_DESC& v) +{ + if (!single_element_bytes(r) || v.Format != r.Format || + v.Shader4ComponentMapping != D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING) + return false; + if (r.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D) + return v.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE3D && v.Texture3D.MostDetailedMip == 0 && + v.Texture3D.MipLevels == 1 && v.Texture3D.ResourceMinLODClamp == 0.0f; + return v.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE2D && v.Texture2D.MostDetailedMip == 0 && + v.Texture2D.MipLevels == 1 && v.Texture2D.PlaneSlice == 0 && v.Texture2D.ResourceMinLODClamp == 0.0f; +} +} // namespace x2w_startup diff --git a/tests/startup_address_srv.cpp b/tests/startup_address_srv.cpp index 5ebce1c274..e6b6b484b7 100644 --- a/tests/startup_address_srv.cpp +++ b/tests/startup_address_srv.cpp @@ -364,3 +364,74 @@ int draw_address_buffer_srvs(ID3D12Device* desktop, void* guest, void* list) draw(7); return 0; } + +// One-texel textures published by address (X2W_TEST_SINGLE_ELEMENT=2d or 3d), one 1x4 column per draw. The texel +// sits at offset 0 of its memory whatever the layout field says. Column 3 follows ten refused descriptions +// and must still show the view published before them. +int draw_single_element_srvs(void* guest, void* list) +{ + wchar_t mode[4]{}; + GetEnvironmentVariableW(L"X2W_TEST_SINGLE_ELEMENT", mode, 4); + const bool volume = mode[0] == L'3'; + alignas(256) static unsigned char memory[4][256]; + const unsigned char a[] = {0x11, 0x22, 0x33, 0xff}, b[] = {0xc0, 0x40, 0x80, 0xff}, c[] = {0x5a, 0xa5, 0x3c, 0xff}; + const unsigned char bgra[] = {0x10, 0x20, 0xf0, 0xff}; + const std::uint16_t half[] = {0x3400, 0x3a00, 0x3c00, 0x3c00}; // 0.25, 0.75, 1, 1 + std::memcpy(memory[0], a, 4); + std::memcpy(memory[1], b, 4); + std::memcpy(memory[2], bgra, 4); + std::memcpy(memory[3], half, 8); + Word cpu = 0; + if (!bind_view_table(guest, list, cpu)) return 42; + const auto texel = [&](DXGI_FORMAT format, UINT layout) { + return D3D12_RESOURCE_DESC{volume ? D3D12_RESOURCE_DIMENSION_TEXTURE3D : D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, 1, 1, 1, 1, + format, {1, 0}, static_cast(layout), static_cast(0x300000)}; + }; + const auto view_of = [&](const D3D12_RESOURCE_DESC& desc, Word address) { + ViewAt v{}; + v.view.Format = desc.Format; + v.view.ViewDimension = volume ? D3D12_SRV_DIMENSION_TEXTURE3D : D3D12_SRV_DIMENSION_TEXTURE2D; + v.view.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + v.view.Texture2D.MipLevels = 1; // Texture3D.MipLevels is at the same offset + v.address = address; + return v; + }; + const auto publish = [&](const D3D12_RESOURCE_DESC& desc, const ViewAt& v) { invoke(guest, 70, ptr(&desc), ptr(&v), cpu); }; + const auto draw = [&](LONG x) { + const D3D12_RECT strip{x, 0, x + 1, 4}; + invoke(list, 22, 1, ptr(&strip)); + invoke(list, 12, 3, 1); + }; + const auto rgba = texel(DXGI_FORMAT_R8G8B8A8_UNORM, 0); + publish(rgba, view_of(rgba, ptr(memory[0]))); draw(0); + const auto swizzled = texel(DXGI_FORMAT_R8G8B8A8_UNORM, 0x105); + publish(swizzled, view_of(swizzled, ptr(memory[1]))); draw(1); + std::memcpy(memory[0], c, 4); // rewritten, then published again at the same address + publish(rgba, view_of(rgba, ptr(memory[0]))); draw(2); + const auto refuse = [&](auto edit) { + auto desc = rgba; + auto v = view_of(desc, ptr(memory[0])); + edit(desc, v); + publish(desc, v); + }; + refuse([](auto& d, auto&) { d.Width = 2; }); + refuse([](auto& d, auto&) { d.MipLevels = 2; }); + refuse([](auto& d, auto&) { d.DepthOrArraySize = 2; }); + refuse([](auto& d, auto&) { d.SampleDesc.Count = 4; }); + refuse([](auto& d, auto&) { d.Layout = static_cast(0x115); }); // 4 KiB S with XOR + refuse([](auto& d, auto& v) { d.Format = v.view.Format = DXGI_FORMAT_BC1_UNORM; }); + refuse([](auto& d, auto& v) { d.Format = v.view.Format = DXGI_FORMAT_R8_UNORM; }); // placed-texture path + refuse([](auto& d, auto&) { d.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; }); + refuse([&](auto&, auto& v) { + v.view.ViewDimension = volume ? D3D12_SRV_DIMENSION_TEXTURE2D : D3D12_SRV_DIMENSION_TEXTURE3D; + }); + refuse([](auto&, auto& v) { v.view.Texture2D.MipLevels = 2; }); + draw(3); + const auto bgra_desc = texel(DXGI_FORMAT_B8G8R8A8_UNORM, 0x100); + publish(bgra_desc, view_of(bgra_desc, ptr(memory[2]))); draw(4); + const auto half_desc = texel(DXGI_FORMAT_R16G16B16A16_FLOAT, 0x105); + publish(half_desc, view_of(half_desc, ptr(memory[3]))); draw(5); + publish(rgba, view_of(rgba, 0)); draw(6); // zero address: refused, column 5's view stays + publish(rgba, view_of(rgba, ptr(memory[0]))); draw(7); + return 0; +} diff --git a/tests/startup_single_element_srv_check.cpp b/tests/startup_single_element_srv_check.cpp new file mode 100644 index 0000000000..46f301240e --- /dev/null +++ b/tests/startup_single_element_srv_check.cpp @@ -0,0 +1,119 @@ +// x2w_startup::single_element_bytes and supports_single_element_srv (src/startup_stubs/single_element_srv.h) +// without a device. Accepted: 1x1 2D and 1x1x1 3D textures with one mip and one sample, layout UNKNOWN, linear +// or a swizzle mode without XOR, a plain colour format and a view naming the whole texture. Each refused case +// differs from an accepted one in a single field. +// --mutate xor: expect the XOR layout 0x115 to be accepted (a negative control). +#include "startup_stubs/single_element_srv.h" + +#include +#include + +namespace { +D3D12_RESOURCE_DESC texel(D3D12_RESOURCE_DIMENSION dimension, DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM, UINT layout = 0) +{ + return {dimension, 0, 1, 1, 1, 1, format, {1, 0}, static_cast(layout), + static_cast(0x300000)}; +} + +D3D12_SHADER_RESOURCE_VIEW_DESC view_of(const D3D12_RESOURCE_DESC& r) +{ + D3D12_SHADER_RESOURCE_VIEW_DESC v{}; + v.Format = r.Format; + v.ViewDimension = r.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? D3D12_SRV_DIMENSION_TEXTURE3D : D3D12_SRV_DIMENSION_TEXTURE2D; + v.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + v.Texture2D.MipLevels = 1; + return v; +} + +struct Case { + const char* name; + D3D12_RESOURCE_DESC desc; + D3D12_SHADER_RESOURCE_VIEW_DESC view; + UINT bytes; // expected single_element_bytes + bool accepted; +}; + +template Case edited(const char* name, D3D12_RESOURCE_DIMENSION dimension, UINT bytes, bool accepted, Edit edit) +{ + Case c{name, texel(dimension), {}, bytes, accepted}; + c.view = view_of(c.desc); + edit(c.desc, c.view); + return c; +} +} // namespace + +int main(int argc, char** argv) +{ + const bool xor_mutation = argc == 3 && !std::strcmp(argv[1], "--mutate") && !std::strcmp(argv[2], "xor"); + if (argc != 1 && !xor_mutation) { + std::printf("usage: startup_single_element_srv_check [--mutate xor]\n"); + return 2; + } + constexpr auto d2 = D3D12_RESOURCE_DIMENSION_TEXTURE2D, d3 = D3D12_RESOURCE_DIMENSION_TEXTURE3D; + const auto none = [](auto&, auto&) {}; + const Case cases[] = { + edited("2D RGBA8 layout UNKNOWN", d2, 4, true, none), + edited("3D RGBA8 layout UNKNOWN", d3, 4, true, none), + edited("3D RGBA8 linear 0x100", d3, 4, true, [](auto& d, auto&) { d.Layout = static_cast(0x100); }), + edited("2D RGBA8 4 KiB S 0x105", d2, 4, true, [](auto& d, auto&) { d.Layout = static_cast(0x105); }), + edited("3D RGBA8 64 KiB R 0x10b", d3, 4, true, [](auto& d, auto&) { d.Layout = static_cast(0x10b); }), + edited("3D R16G16B16A16_FLOAT", d3, 8, true, [](auto& d, auto& v) { d.Format = v.Format = DXGI_FORMAT_R16G16B16A16_FLOAT; }), + edited("2D R32G32B32A32_FLOAT", d2, 16, true, [](auto& d, auto& v) { d.Format = v.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; }), + edited("2D R16_FLOAT", d2, 2, true, [](auto& d, auto& v) { d.Format = v.Format = DXGI_FORMAT_R16_FLOAT; }), + edited("2D A8_UNORM", d2, 1, true, [](auto& d, auto& v) { d.Format = v.Format = DXGI_FORMAT_A8_UNORM; }), + edited("2D zero flags", d2, 4, true, [](auto& d, auto&) { d.Flags = D3D12_RESOURCE_FLAG_NONE; }), + // Shape. + edited("2x1", d2, 0, false, [](auto& d, auto&) { d.Width = 2; }), + edited("1x2", d3, 0, false, [](auto& d, auto&) { d.Height = 2; }), + edited("1x1x2 volume", d3, 0, false, [](auto& d, auto&) { d.DepthOrArraySize = 2; }), + edited("two array slices", d2, 0, false, [](auto& d, auto&) { d.DepthOrArraySize = 2; }), + edited("two mips", d2, 0, false, [](auto& d, auto&) { d.MipLevels = 2; }), + edited("zero mips", d3, 0, false, [](auto& d, auto&) { d.MipLevels = 0; }), + edited("four samples", d2, 0, false, [](auto& d, auto&) { d.SampleDesc.Count = 4; }), + edited("sample quality 1", d2, 0, false, [](auto& d, auto&) { d.SampleDesc.Quality = 1; }), + edited("1D", d2, 0, false, [](auto& d, auto&) { d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE1D; }), + // Layout: XOR modes and desktop layouts. + edited("4 KiB S with XOR 0x115", d2, xor_mutation ? 4 : 0, xor_mutation, + [](auto& d, auto&) { d.Layout = static_cast(0x115); }), + edited("64 KiB Z T 0x110", d3, 0, false, [](auto& d, auto&) { d.Layout = static_cast(0x110); }), + edited("reserved mode 0x10c", d3, 0, false, [](auto& d, auto&) { d.Layout = static_cast(0x10c); }), + edited("ROW_MAJOR", d2, 0, false, [](auto& d, auto&) { d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; }), + // Flags and formats. + edited("render-target flag", d2, 0, false, [](auto& d, auto&) { d.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; }), + edited("UAV flag", d3, 0, false, [](auto& d, auto&) { d.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; }), + edited("deny shader resource", d2, 0, false, [](auto& d, auto&) { + d.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE; }), + edited("R8_UNORM (placed path)", d2, 0, false, [](auto& d, auto& v) { d.Format = v.Format = DXGI_FORMAT_R8_UNORM; }), + edited("BC1_UNORM", d2, 0, false, [](auto& d, auto& v) { d.Format = v.Format = DXGI_FORMAT_BC1_UNORM; }), + edited("R8G8B8A8_TYPELESS", d2, 0, false, [](auto& d, auto& v) { d.Format = v.Format = DXGI_FORMAT_R8G8B8A8_TYPELESS; }), + edited("D32_FLOAT", d2, 0, false, [](auto& d, auto& v) { d.Format = v.Format = DXGI_FORMAT_D32_FLOAT; }), + edited("NV12", d2, 0, false, [](auto& d, auto& v) { d.Format = v.Format = DXGI_FORMAT_NV12; }), + edited("console format 116", d2, 0, false, + [](auto& d, auto& v) { d.Format = v.Format = static_cast(116); }), + // The view (single_element_bytes accepts the description; the view check refuses). + edited("view format SRGB", d2, 4, false, [](auto&, auto& v) { v.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; }), + edited("2D view of a volume", d3, 4, false, [](auto&, auto& v) { v.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; }), + edited("3D view of a 2D texture", d2, 4, false, [](auto&, auto& v) { v.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE3D; }), + edited("array view", d2, 4, false, [](auto&, auto& v) { v.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY; }), + edited("two-mip view", d3, 4, false, [](auto&, auto& v) { v.Texture3D.MipLevels = 2; }), + edited("most detailed mip 1", d2, 4, false, [](auto&, auto& v) { v.Texture2D.MostDetailedMip = 1; }), + edited("plane slice 1", d2, 4, false, [](auto&, auto& v) { v.Texture2D.PlaneSlice = 1; }), + edited("LOD clamp 1", d3, 4, false, [](auto&, auto& v) { v.Texture3D.ResourceMinLODClamp = 1.0f; }), + edited("swizzled mapping", d2, 4, false, [](auto&, auto& v) { + v.Shader4ComponentMapping = D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(2, 1, 0, 3); }), + }; + int failures = 0, accepted = 0; + for (const Case& c : cases) { + const UINT bytes = x2w_startup::single_element_bytes(c.desc); + const bool actual = x2w_startup::supports_single_element_srv(c.desc, c.view); + accepted += actual; + if (bytes != c.bytes || actual != c.accepted) { + std::printf("FAIL %s: bytes %u accepted %d, expected bytes %u accepted %d\n", c.name, bytes, actual, c.bytes, + c.accepted); + ++failures; + } + } + const int total = static_cast(sizeof(cases) / sizeof(cases[0])); + std::printf("%d cases, %d accepted, %d refused, %d mismatches\n", total, accepted, total - accepted, failures); + return failures ? 1 : 0; +} diff --git a/tests/startup_single_element_srv_check.py b/tests/startup_single_element_srv_check.py new file mode 100644 index 0000000000..7e9a1ef519 --- /dev/null +++ b/tests/startup_single_element_srv_check.py @@ -0,0 +1,57 @@ +"""Draw one-texel 2D and 3D textures published by address (device slot 70) and check refused descriptions. + +Each run draws eight 1x4 columns from a translated pixel shader that loads texel (0, 0, 0): + 0 RGBA8, layout UNKNOWN 1 RGBA8, layout 0x105 (another address) + 2 the column-0 texel rewritten and published again; it must read the new value + 3 after ten refused descriptions (2x1, two mips, two slices, four samples, XOR layout 0x115, BC1, R8, + a UAV flag, the other view dimension, a two-mip view): the column-2 view must be unchanged + 4 B8G8R8A8, layout 0x100 5 R16G16B16A16_FLOAT, layout 0x105 + 6 a zero address after column 5: refused, column 5's view stays + 7 the column-0 address and description again +Usage: startup_single_element_srv_check.py +Without the single-element path every column except 3 and 6 reads a null view (zero). +""" +from pathlib import Path +import sys +import tempfile + +from startup_address_srv_check import draw_texture + +A = (0x11, 0x22, 0x33, 0xff) +B = (0xc0, 0x40, 0x80, 0xff) +C = (0x5a, 0xa5, 0x3c, 0xff) +BGRA = (0xf0, 0x20, 0x10, 0xff) +HALF = (64, 191, 255, 255) # 0.25, 0.75, 1, 1 as UNORM8 + + +def expected(): + columns = [A, B, C, C, BGRA, HALF, HALF, C] + return bytes(c for _ in range(4) for column in columns for c in column) + + +def check(directory, consumer, backend, dimension): + texture, load = ("Texture3D", "int4(0, 0, 0, 0)") if dimension == "3d" else ("Texture2D", "int3(0, 0, 0)") + hlsl = (f"{texture} t : register(t0);\n" + "float4 main(float4 p : SV_Position) : SV_Target {\n" + f" return t.Load({load});\n}}\n") + kind = "tex3d" if dimension == "3d" else "tex2d" + actual = draw_texture(directory, consumer, backend, hlsl, {"X2W_TEST_SINGLE_ELEMENT": dimension}, kind) + if actual is None: + return 77 + want = expected() + if actual != want: + for i in range(0, len(want), 4): + if actual[i:i + 4] != want[i:i + 4]: + raise AssertionError(f"{dimension} column {(i // 4) % 8} row {i // 32}: actual {actual[i:i + 4].hex()} " + f"expected {want[i:i + 4].hex()}") + raise AssertionError(f"{dimension}: readback length {len(actual)}, expected {len(want)}") + print(f"{backend} {dimension}: 8 columns of one-texel views exact, ten refused descriptions kept the previous view") + return 0 + + +if __name__ == "__main__": + for dimension in ("2d", "3d"): + with tempfile.TemporaryDirectory() as temporary: + result = check(Path(temporary), *sys.argv[1:], dimension) + if result: + sys.exit(result) diff --git a/tests/startup_stream_draw.cpp b/tests/startup_stream_draw.cpp index 8b89c00211..ac9f4391e4 100644 --- a/tests/startup_stream_draw.cpp +++ b/tests/startup_stream_draw.cpp @@ -281,6 +281,9 @@ int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* } else if (GetEnvironmentVariableW(L"X2W_TEST_ADDRESS_SRV", nullptr, 0)) { extern int draw_address_srvs(ID3D12Device*, void*, void*); if (const int result = draw_address_srvs(desktop.Get(), guest, list)) return result; + } else if (GetEnvironmentVariableW(L"X2W_TEST_SINGLE_ELEMENT", nullptr, 0)) { + extern int draw_single_element_srvs(void*, void*); + if (const int result = draw_single_element_srvs(guest, list)) return result; } else if (GetEnvironmentVariableW(L"X2W_TEST_ADDRESS_BUFFER_SRV", nullptr, 0)) { extern int draw_address_buffer_srvs(ID3D12Device*, void*, void*); if (const int result = draw_address_buffer_srvs(desktop.Get(), guest, list)) return result; From efe5473297a4c09e51a1d2ec33902d6c120400b6 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Tue, 24 Nov 2026 23:03:57 +0100 Subject: [PATCH 2/7] startup: keep single-element views over GPU-written resources on the resource lookup A live render-target, depth or UAV resource at the view's address is written on the desktop side, so a flagless single-element view there must not read title memory; it keeps the existing lookup, which needs the same description and otherwise refuses as before. The fixture adds that refused call. Co-Authored-By: Claude Opus 5.5 --- docs/experiments.md | 4 ++-- src/startup_stubs/d3d12_memory_srv.inl | 22 +++++++++++++++++++++- tests/startup_address_srv.cpp | 13 ++++++++++--- tests/startup_single_element_srv_check.py | 7 ++++--- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index e3cd24edff..f2332943d2 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11008,9 +11008,9 @@ ctest --preset release --no-tests=error --output-on-failure -R '^startup_xg_bc_c ### Single-element 2D and 3D textures published by address -Device slot 70 publishes a 1×1 2D or 1×1×1 3D texture by address, without a resource object. It must have one mip and one sample, and a plain colour format whose element is one texel of 1 to 16 bytes. R8_UNORM keeps its placed-texture path. The view must name the whole texture with the default mapping. The only element is read from offset 0 of the guest memory. That holds for layout UNKNOWN, linear (`0x100`) and the swizzle modes without pipe/bank XOR (`0x101`–`0x10B`). A swizzle address is a block offset plus a bit pattern of the coordinates, both zero at the origin. AMD addrlib places a single mip at offset 0 (`Gfx10Lib::ComputeSurfaceInfoMacroTiled` at the pinned [revision](https://gitlab.freedesktop.org/mesa/mesa/-/tree/5c142e46f3f6e752ed745fd48912ebb8fad67145/src/amd/addrlib)). XOR modes (`0x110` and up) can move the origin and stay refused, as do larger shapes, mip chains, arrays, multisampling, block-compressed formats and render-target, depth or UAV flags. The translated draw uploads the texel through the footprint path. It re-reads the texel when the view is published again and once per frame, like the other address-only views. +Device slot 70 publishes a 1×1 2D or 1×1×1 3D texture by address, without a resource object. It must have one mip and one sample, and a plain colour format whose element is one texel of 1 to 16 bytes. R8_UNORM keeps its placed-texture path. The view must name the whole texture with the default mapping. The only element is read from offset 0 of the guest memory. That holds for layout UNKNOWN, the value seen on such views, and for linear (`0x100`). It is inferred, not run, for the swizzle modes without pipe/bank XOR (`0x101`–`0x10B`). A swizzle address is a block offset plus a bit pattern of the coordinates, both zero at the origin. AMD addrlib places a single mip at offset 0 (`Gfx10Lib::ComputeSurfaceInfoMacroTiled` at the pinned [revision](https://gitlab.freedesktop.org/mesa/mesa/-/tree/5c142e46f3f6e752ed745fd48912ebb8fad67145/src/amd/addrlib)). XOR modes (`0x110` and up) can move the origin and stay refused, as do larger shapes, mip chains, arrays, multisampling, block-compressed formats and render-target, depth or UAV flags. A live render-target, depth or UAV resource at the same address is written on the desktop side, so such a view keeps the existing lookup, which needs the same description. The translated draw uploads the texel through the footprint path, with the change detection of the other address-only views: a new publication of the view re-reads it (tested), and by code reading so does the next frame's first bind (not tested here, since the fixture records one frame). -`startup_single_element_srv` checks the accepted and refused descriptions without a device; its `xor_control` expects layout `0x115` to be accepted and must fail. `startup_address_single_element_{warp,hardware}` draws eight columns per dimension from a translated shader that loads texel zero. The columns are RGBA8 at layouts 0 and `0x105`, the first texel rewritten and published again, B8G8R8A8 and R16G16B16A16_FLOAT. Ten refused descriptions and a zero address must leave the previous view in place. On Windows 11 build 26300 with the debug layer off, RTX 5090 (32.0.16.2135) and WARP (10.0.26100.10068) matched all 32 RGBA pixels per dimension. With the runtime change reverted, column 0 reads zero. An upload that skips the texel after its first copy fails column 2, which then reads the old texel. +`startup_single_element_srv` checks the accepted and refused descriptions without a device; its `xor_control` expects layout `0x115` to be accepted and must fail. `startup_address_single_element_{warp,hardware}` draws eight columns per dimension from a translated shader that loads texel zero. The columns are RGBA8 at layouts 0 and `0x105`, the first texel rewritten and published again, B8G8R8A8 and R16G16B16A16_FLOAT. Ten refused descriptions, a flagless view over a placed UAV texture and a zero address must leave the previous view in place. On Windows 11 build 26300 with the debug layer off, RTX 5090 (32.0.16.2135) and WARP (10.0.26100.10068) matched all 32 RGBA pixels per dimension. With the runtime change reverted, column 0 reads zero. An upload that skips the texel after its first copy fails column 2, which then reads the old texel. ```powershell cmake --build --preset release --target startup_stream_consumer startup_single_element_srv_check d3d12_xs_shim diff --git a/src/startup_stubs/d3d12_memory_srv.inl b/src/startup_stubs/d3d12_memory_srv.inl index 8bc8dd0b4f..3f992d2ce4 100644 --- a/src/startup_stubs/d3d12_memory_srv.inl +++ b/src/startup_stubs/d3d12_memory_srv.inl @@ -86,6 +86,25 @@ Wrapper* existing_memory_texture(u64 address, const D3D12_RESOURCE_DESC& desc) return match; } +// A live render-target, depth or UAV resource at the address is written on the desktop side, so title memory +// there does not hold its texels. +bool gpu_written_at(u64 address) +{ + constexpr UINT gpu_written = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | + D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; + bool found = false; + AcquireSRWLockShared(¬e_lock); + for (const auto& [wrapper, info] : resource_infos()) { + if (reinterpret_cast(wrapper->own().title_memory) == address && (static_cast(info.console.Flags) & gpu_written) && + InterlockedCompareExchange(&wrapper->own().refs, 0, 0) > 0) { + found = true; + break; + } + } + ReleaseSRWLockShared(¬e_lock); + return found; +} + Wrapper* existing_memory_buffer(Wrapper* device, u64 address, u64 bytes) { if (!address || !bytes) return nullptr; @@ -216,7 +235,8 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou return; } // A single-element texture is published by address like a tiled view: its texel is read at offset 0. - if (x2w_startup::supports_single_element_srv(*resource, input->view)) { + // Over a GPU-written resource it takes the lookup below, which needs the same description. + if (x2w_startup::supports_single_element_srv(*resource, input->view) && !gpu_written_at(input->address)) { if (Wrapper* texture = publish_tiled_texture(self, input->address, *resource)) view_slot<18, 2>(self, reinterpret_cast(texture), reinterpret_cast(&input->view), destination, 0); else if (first_calls(refused, 8)) diff --git a/tests/startup_address_srv.cpp b/tests/startup_address_srv.cpp index e6b6b484b7..61dea5aaaa 100644 --- a/tests/startup_address_srv.cpp +++ b/tests/startup_address_srv.cpp @@ -366,14 +366,14 @@ int draw_address_buffer_srvs(ID3D12Device* desktop, void* guest, void* list) } // One-texel textures published by address (X2W_TEST_SINGLE_ELEMENT=2d or 3d), one 1x4 column per draw. The texel -// sits at offset 0 of its memory whatever the layout field says. Column 3 follows ten refused descriptions -// and must still show the view published before them. +// sits at offset 0 of its memory whatever the layout field says. Column 3 follows eleven refused descriptions +// and must still show the view published before them; the last is a flagless view over a GPU-written resource. int draw_single_element_srvs(void* guest, void* list) { wchar_t mode[4]{}; GetEnvironmentVariableW(L"X2W_TEST_SINGLE_ELEMENT", mode, 4); const bool volume = mode[0] == L'3'; - alignas(256) static unsigned char memory[4][256]; + alignas(256) static unsigned char memory[5][256]; const unsigned char a[] = {0x11, 0x22, 0x33, 0xff}, b[] = {0xc0, 0x40, 0x80, 0xff}, c[] = {0x5a, 0xa5, 0x3c, 0xff}; const unsigned char bgra[] = {0x10, 0x20, 0xf0, 0xff}; const std::uint16_t half[] = {0x3400, 0x3a00, 0x3c00, 0x3c00}; // 0.25, 0.75, 1, 1 @@ -426,6 +426,13 @@ int draw_single_element_srvs(void* guest, void* list) v.view.ViewDimension = volume ? D3D12_SRV_DIMENSION_TEXTURE2D : D3D12_SRV_DIMENSION_TEXTURE3D; }); refuse([](auto&, auto& v) { v.view.Texture2D.MipLevels = 2; }); + // A placed UAV texture at an address is written on the desktop side; a flagless view there stays refused. + auto written = rgba; + written.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; + void* uav = nullptr; + if (FAILED(static_cast(invoke(guest, 133, ptr(memory[4]), ptr(&written), D3D12_RESOURCE_STATE_COMMON, 0, + ptr(&__uuidof(ID3D12Resource)), ptr(&uav))))) return 48; + publish(rgba, view_of(rgba, ptr(memory[4]))); draw(3); const auto bgra_desc = texel(DXGI_FORMAT_B8G8R8A8_UNORM, 0x100); publish(bgra_desc, view_of(bgra_desc, ptr(memory[2]))); draw(4); diff --git a/tests/startup_single_element_srv_check.py b/tests/startup_single_element_srv_check.py index 7e9a1ef519..bd119fc191 100644 --- a/tests/startup_single_element_srv_check.py +++ b/tests/startup_single_element_srv_check.py @@ -3,8 +3,9 @@ Each run draws eight 1x4 columns from a translated pixel shader that loads texel (0, 0, 0): 0 RGBA8, layout UNKNOWN 1 RGBA8, layout 0x105 (another address) 2 the column-0 texel rewritten and published again; it must read the new value - 3 after ten refused descriptions (2x1, two mips, two slices, four samples, XOR layout 0x115, BC1, R8, - a UAV flag, the other view dimension, a two-mip view): the column-2 view must be unchanged + 3 after eleven refused calls (2x1, two mips, two slices, four samples, XOR layout 0x115, BC1, R8, + a UAV flag, the other view dimension, a two-mip view, and a flagless view over a placed UAV texture, + whose texels live on the desktop side): the column-2 view must be unchanged 4 B8G8R8A8, layout 0x100 5 R16G16B16A16_FLOAT, layout 0x105 6 a zero address after column 5: refused, column 5's view stays 7 the column-0 address and description again @@ -45,7 +46,7 @@ def check(directory, consumer, backend, dimension): raise AssertionError(f"{dimension} column {(i // 4) % 8} row {i // 32}: actual {actual[i:i + 4].hex()} " f"expected {want[i:i + 4].hex()}") raise AssertionError(f"{dimension}: readback length {len(actual)}, expected {len(want)}") - print(f"{backend} {dimension}: 8 columns of one-texel views exact, ten refused descriptions kept the previous view") + print(f"{backend} {dimension}: 8 columns of one-texel views exact, eleven refused calls kept the previous view") return 0 From 4e652285062e01578eeab26c2f6024d23c8dc6b8 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Tue, 24 Nov 2026 23:42:01 +0100 Subject: [PATCH 3/7] tests: register the single-element address-view test next to startup_address_srv Keeps its CMake hunk away from the neighbouring BC address-view timeout line. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c2bf692ec4..9385c59641 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2340,6 +2340,10 @@ if(EXISTS "${INTAKE_PYTHON}") "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_srv_check.py" "$" "${backend}") set_tests_properties(startup_address_srv_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) + add_test(NAME startup_address_single_element_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" + "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_single_element_srv_check.py" + "$" "${backend}") + set_tests_properties(startup_address_single_element_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) add_test(NAME startup_address_buffer_srv_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_buffer_srv_check.py" "$" "${backend}") @@ -2356,10 +2360,6 @@ if(EXISTS "${INTAKE_PYTHON}") "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_bc_view_check.py" "$" "${backend}") set_tests_properties(startup_address_bc_view_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) - add_test(NAME startup_address_single_element_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" - "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_single_element_srv_check.py" - "$" "${backend}") - set_tests_properties(startup_address_single_element_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) add_test(NAME startup_xg_bc_chain_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_xg_bc_chain_check.py" "$" "$" "${backend}") From 323ccf6febae47fdaa0dc48135b92ff8d1e066d3 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 00:34:33 +0100 Subject: [PATCH 4/7] startup: tighten single-element memory SRVs after review Accept only the layouts AMD addrlib defines (0, 0x100, 0x101, 0x102, 0x105, 0x106, 0x109, 0x10A) and default or 64 KiB alignment; refuse a view over a GPU-written resource that covers the address (base, or inside a known allocation size) with its own reason, gpu_written_resource; rehash the texel on every bind. The draw fixture points its refusals at memory holding another texel and reads a same-frame rewrite. Co-Authored-By: Claude Opus 5.5 --- src/startup_stubs/d3d12_draw.inl | 5 +-- src/startup_stubs/d3d12_memory_srv.inl | 19 +++++++---- src/startup_stubs/single_element_srv.h | 16 +++++++--- tests/startup_address_srv.cpp | 37 ++++++++++++++++------ tests/startup_single_element_srv_check.cpp | 14 ++++++-- tests/startup_single_element_srv_check.py | 18 ++++++----- tests/startup_triage_run_check.py | 1 + tools/startup/triage_run.py | 1 + 8 files changed, 79 insertions(+), 32 deletions(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index dfe22f5ea8..4afe116c32 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -736,8 +736,9 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) const u64 total = tiled_chain ? x2w_startup::bc64::Chain(original->console).bytes() : tiled && !single ? surface.size() : stride * (slices - 1) + slice_total; const bool announced = state.generation != generation; const UINT elapsed = static_cast(frame_number) - static_cast(state.checked_frame); - const bool recheck = (tiled_address || tiled_chain) ? elapsed != 0 - : x2w_startup::recheck_cpu_texture(desc, total, elapsed); + // A single texel (at most 16 bytes) is rehashed on every bind, so a write between two draws is seen. + const bool recheck = single || ((tiled_address || tiled_chain) ? elapsed != 0 + : x2w_startup::recheck_cpu_texture(desc, total, elapsed)); if (state.done && !announced && !recheck) return; if (readable_window(reinterpret_cast(memory), static_cast(total)) < total) { if (!state.done) InterlockedIncrement(&textures_unreadable); diff --git a/src/startup_stubs/d3d12_memory_srv.inl b/src/startup_stubs/d3d12_memory_srv.inl index 3f992d2ce4..241ea10b53 100644 --- a/src/startup_stubs/d3d12_memory_srv.inl +++ b/src/startup_stubs/d3d12_memory_srv.inl @@ -86,8 +86,9 @@ Wrapper* existing_memory_texture(u64 address, const D3D12_RESOURCE_DESC& desc) return match; } -// A live render-target, depth or UAV resource at the address is written on the desktop side, so title memory -// there does not hold its texels. +// A live render-target, depth or UAV resource covering the address is written on the desktop side, so title +// memory there does not hold its texels. A resource covers its base address, and the bytes after it when its +// allocation size (or a buffer's width) is known. bool gpu_written_at(u64 address) { constexpr UINT gpu_written = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | @@ -95,8 +96,10 @@ bool gpu_written_at(u64 address) bool found = false; AcquireSRWLockShared(¬e_lock); for (const auto& [wrapper, info] : resource_infos()) { - if (reinterpret_cast(wrapper->own().title_memory) == address && (static_cast(info.console.Flags) & gpu_written) && - InterlockedCompareExchange(&wrapper->own().refs, 0, 0) > 0) { + const u64 base = reinterpret_cast(wrapper->own().title_memory); + const u64 bytes = info.bytes ? info.bytes : info.console.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER ? info.console.Width : 0; + if (base && (address == base || (address > base && address - base < bytes)) && + (static_cast(info.console.Flags) & gpu_written) && InterlockedCompareExchange(&wrapper->own().refs, 0, 0) > 0) { found = true; break; } @@ -235,9 +238,11 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou return; } // A single-element texture is published by address like a tiled view: its texel is read at offset 0. - // Over a GPU-written resource it takes the lookup below, which needs the same description. - if (x2w_startup::supports_single_element_srv(*resource, input->view) && !gpu_written_at(input->address)) { - if (Wrapper* texture = publish_tiled_texture(self, input->address, *resource)) + if (x2w_startup::supports_single_element_srv(*resource, input->view)) { + if (gpu_written_at(input->address)) { + if (first_calls(refused, 8)) + log_refused_memory_srv("GPU-written resource at the supplied address", "gpu_written_resource", resource, input, destination); + } else if (Wrapper* texture = publish_tiled_texture(self, input->address, *resource)) view_slot<18, 2>(self, reinterpret_cast(texture), reinterpret_cast(&input->view), destination, 0); else if (first_calls(refused, 8)) log_refused_memory_srv("unavailable texture at the supplied base and layout", "texture_unavailable", resource, input, destination); diff --git a/src/startup_stubs/single_element_srv.h b/src/startup_stubs/single_element_srv.h index 7d4a4a2a5d..a1c0972a62 100644 --- a/src/startup_stubs/single_element_srv.h +++ b/src/startup_stubs/single_element_srv.h @@ -4,9 +4,11 @@ namespace x2w_startup { // One texel, one mip, one sample, 2D or 3D. In every layout accepted here the only element sits at offset 0 -// of the title memory: UNKNOWN, linear (0x100) and the swizzle modes without pipe/bank XOR (0x101..0x10B; a -// swizzle address is block index * block size + a bit pattern of the coordinates, both zero at the origin). -// XOR modes (0x110 and up) can move the origin by the surface's pipe/bank XOR and are refused. Formats are +// of the title memory: UNKNOWN, linear (0x100) and the GFX10 swizzle modes without pipe/bank XOR that AMD addrlib +// defines (256B_S/D 0x101/0x102, 4KB_S/D 0x105/0x106, 64KB_S/D 0x109/0x10A; a swizzle address is block index * +// block size + a bit pattern of the coordinates, both zero at the origin). Reserved modes (0x103, 0x104, 0x107, +// 0x108, 0x10B) and XOR modes (0x110 and up, which can move the origin by the surface's pipe/bank XOR) are +// refused, as is an alignment other than the default or 64 KiB, which the desktop texture cannot take. Formats are // plain colour formats whose element is one texel with the same bytes on desktop; R8_UNORM keeps its placed- // texture path, and block-compressed, planar, depth and packed console formats are refused. Returns the // element size in bytes, or 0 when the description is not such a texture. @@ -15,8 +17,14 @@ inline UINT single_element_bytes(const D3D12_RESOURCE_DESC& r) const UINT layout = static_cast(r.Layout); if ((r.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D && r.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE3D) || r.Width != 1 || r.Height != 1 || r.DepthOrArraySize != 1 || r.MipLevels != 1 || r.SampleDesc.Count != 1 || - r.SampleDesc.Quality != 0 || (layout != 0 && (layout < 0x100 || layout > 0x10B))) + r.SampleDesc.Quality != 0 || (r.Alignment != 0 && r.Alignment != 0x10000)) return 0; + switch (layout) { + case 0: case 0x100: case 0x101: case 0x102: case 0x105: case 0x106: case 0x109: case 0x10A: + break; + default: + return 0; + } // GPU-written textures are never uploaded from title memory. if (r.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE)) diff --git a/tests/startup_address_srv.cpp b/tests/startup_address_srv.cpp index 61dea5aaaa..0f92d11402 100644 --- a/tests/startup_address_srv.cpp +++ b/tests/startup_address_srv.cpp @@ -366,21 +366,24 @@ int draw_address_buffer_srvs(ID3D12Device* desktop, void* guest, void* list) } // One-texel textures published by address (X2W_TEST_SINGLE_ELEMENT=2d or 3d), one 1x4 column per draw. The texel -// sits at offset 0 of its memory whatever the layout field says. Column 3 follows eleven refused descriptions -// and must still show the view published before them; the last is a flagless view over a GPU-written resource. +// sits at offset 0 of its memory whatever the layout field says. Before column 3 the column-2 texel is rewritten +// without a new publication, then fifteen calls are refused; each names memory holding another texel, so any of +// them taking the descriptor would change column 3. int draw_single_element_srvs(void* guest, void* list) { wchar_t mode[4]{}; GetEnvironmentVariableW(L"X2W_TEST_SINGLE_ELEMENT", mode, 4); const bool volume = mode[0] == L'3'; - alignas(256) static unsigned char memory[5][256]; + alignas(256) static unsigned char memory[8][256]; const unsigned char a[] = {0x11, 0x22, 0x33, 0xff}, b[] = {0xc0, 0x40, 0x80, 0xff}, c[] = {0x5a, 0xa5, 0x3c, 0xff}; + const unsigned char d[] = {0x81, 0x18, 0xe7, 0xff}, other[] = {0xde, 0xad, 0xbe, 0xef}; const unsigned char bgra[] = {0x10, 0x20, 0xf0, 0xff}; const std::uint16_t half[] = {0x3400, 0x3a00, 0x3c00, 0x3c00}; // 0.25, 0.75, 1, 1 std::memcpy(memory[0], a, 4); std::memcpy(memory[1], b, 4); std::memcpy(memory[2], bgra, 4); std::memcpy(memory[3], half, 8); + for (int row : {4, 5, 7}) std::memcpy(memory[row], other, 4); Word cpu = 0; if (!bind_view_table(guest, list, cpu)) return 42; const auto texel = [&](DXGI_FORMAT format, UINT layout) { @@ -408,9 +411,10 @@ int draw_single_element_srvs(void* guest, void* list) publish(swizzled, view_of(swizzled, ptr(memory[1]))); draw(1); std::memcpy(memory[0], c, 4); // rewritten, then published again at the same address publish(rgba, view_of(rgba, ptr(memory[0]))); draw(2); + std::memcpy(memory[0], d, 4); // rewritten without a new publication: column 3 must read it const auto refuse = [&](auto edit) { auto desc = rgba; - auto v = view_of(desc, ptr(memory[0])); + auto v = view_of(desc, ptr(memory[5])); edit(desc, v); publish(desc, v); }; @@ -419,6 +423,8 @@ int draw_single_element_srvs(void* guest, void* list) refuse([](auto& d, auto&) { d.DepthOrArraySize = 2; }); refuse([](auto& d, auto&) { d.SampleDesc.Count = 4; }); refuse([](auto& d, auto&) { d.Layout = static_cast(0x115); }); // 4 KiB S with XOR + refuse([](auto& d, auto&) { d.Layout = static_cast(0x10b); }); // reserved mode + refuse([](auto& d, auto&) { d.Alignment = 0x400000; }); refuse([](auto& d, auto& v) { d.Format = v.view.Format = DXGI_FORMAT_BC1_UNORM; }); refuse([](auto& d, auto& v) { d.Format = v.view.Format = DXGI_FORMAT_R8_UNORM; }); // placed-texture path refuse([](auto& d, auto&) { d.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; }); @@ -426,19 +432,32 @@ int draw_single_element_srvs(void* guest, void* list) v.view.ViewDimension = volume ? D3D12_SRV_DIMENSION_TEXTURE2D : D3D12_SRV_DIMENSION_TEXTURE3D; }); refuse([](auto&, auto& v) { v.view.Texture2D.MipLevels = 2; }); - // A placed UAV texture at an address is written on the desktop side; a flagless view there stays refused. + refuse([](auto&, auto& v) { v.address = 0; }); + // Placed UAV resources are written on the desktop side. A flagless view at the base of a 1x1 UAV texture, + // and one 256 bytes into a 512-byte UAV buffer, stay refused. auto written = rgba; written.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; - void* uav = nullptr; + D3D12_RESOURCE_DESC written_buffer{}; + written_buffer.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + written_buffer.Width = 512; + written_buffer.Height = written_buffer.DepthOrArraySize = written_buffer.MipLevels = written_buffer.SampleDesc.Count = 1; + written_buffer.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + written_buffer.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; + void* uav[2]{}; if (FAILED(static_cast(invoke(guest, 133, ptr(memory[4]), ptr(&written), D3D12_RESOURCE_STATE_COMMON, 0, - ptr(&__uuidof(ID3D12Resource)), ptr(&uav))))) return 48; + ptr(&__uuidof(ID3D12Resource)), ptr(&uav[0])))) || + FAILED(static_cast(invoke(guest, 133, ptr(memory[6]), ptr(&written_buffer), D3D12_RESOURCE_STATE_COMMON, 0, + ptr(&__uuidof(ID3D12Resource)), ptr(&uav[1]))))) return 48; publish(rgba, view_of(rgba, ptr(memory[4]))); + publish(rgba, view_of(rgba, ptr(memory[7]))); draw(3); const auto bgra_desc = texel(DXGI_FORMAT_B8G8R8A8_UNORM, 0x100); publish(bgra_desc, view_of(bgra_desc, ptr(memory[2]))); draw(4); const auto half_desc = texel(DXGI_FORMAT_R16G16B16A16_FLOAT, 0x105); publish(half_desc, view_of(half_desc, ptr(memory[3]))); draw(5); - publish(rgba, view_of(rgba, 0)); draw(6); // zero address: refused, column 5's view stays - publish(rgba, view_of(rgba, ptr(memory[0]))); draw(7); + draw(6); // the same view again + auto aligned = rgba; + aligned.Alignment = 0x10000; + publish(aligned, view_of(aligned, ptr(memory[0]))); draw(7); return 0; } diff --git a/tests/startup_single_element_srv_check.cpp b/tests/startup_single_element_srv_check.cpp index 46f301240e..353e1981cc 100644 --- a/tests/startup_single_element_srv_check.cpp +++ b/tests/startup_single_element_srv_check.cpp @@ -1,6 +1,7 @@ // x2w_startup::single_element_bytes and supports_single_element_srv (src/startup_stubs/single_element_srv.h) // without a device. Accepted: 1x1 2D and 1x1x1 3D textures with one mip and one sample, layout UNKNOWN, linear -// or a swizzle mode without XOR, a plain colour format and a view naming the whole texture. Each refused case +// or a defined GFX10 swizzle mode without XOR, default or 64 KiB alignment, a plain colour format and a view naming +// the whole texture. Each refused case // differs from an accepted one in a single field. // --mutate xor: expect the XOR layout 0x115 to be accepted (a negative control). #include "startup_stubs/single_element_srv.h" @@ -56,7 +57,9 @@ int main(int argc, char** argv) edited("3D RGBA8 layout UNKNOWN", d3, 4, true, none), edited("3D RGBA8 linear 0x100", d3, 4, true, [](auto& d, auto&) { d.Layout = static_cast(0x100); }), edited("2D RGBA8 4 KiB S 0x105", d2, 4, true, [](auto& d, auto&) { d.Layout = static_cast(0x105); }), - edited("3D RGBA8 64 KiB R 0x10b", d3, 4, true, [](auto& d, auto&) { d.Layout = static_cast(0x10b); }), + edited("3D RGBA8 64 KiB D 0x10a", d3, 4, true, [](auto& d, auto&) { d.Layout = static_cast(0x10a); }), + edited("2D RGBA8 256 B S 0x101", d2, 4, true, [](auto& d, auto&) { d.Layout = static_cast(0x101); }), + edited("3D 64 KiB alignment", d3, 4, true, [](auto& d, auto&) { d.Alignment = 0x10000; }), edited("3D R16G16B16A16_FLOAT", d3, 8, true, [](auto& d, auto& v) { d.Format = v.Format = DXGI_FORMAT_R16G16B16A16_FLOAT; }), edited("2D R32G32B32A32_FLOAT", d2, 16, true, [](auto& d, auto& v) { d.Format = v.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; }), edited("2D R16_FLOAT", d2, 2, true, [](auto& d, auto& v) { d.Format = v.Format = DXGI_FORMAT_R16_FLOAT; }), @@ -77,6 +80,13 @@ int main(int argc, char** argv) [](auto& d, auto&) { d.Layout = static_cast(0x115); }), edited("64 KiB Z T 0x110", d3, 0, false, [](auto& d, auto&) { d.Layout = static_cast(0x110); }), edited("reserved mode 0x10c", d3, 0, false, [](auto& d, auto&) { d.Layout = static_cast(0x10c); }), + edited("reserved mode 0x103", d2, 0, false, [](auto& d, auto&) { d.Layout = static_cast(0x103); }), + edited("reserved mode 0x104", d3, 0, false, [](auto& d, auto&) { d.Layout = static_cast(0x104); }), + edited("reserved mode 0x107", d2, 0, false, [](auto& d, auto&) { d.Layout = static_cast(0x107); }), + edited("reserved mode 0x108", d3, 0, false, [](auto& d, auto&) { d.Layout = static_cast(0x108); }), + edited("reserved mode 0x10b", d3, 0, false, [](auto& d, auto&) { d.Layout = static_cast(0x10b); }), + edited("4 KiB alignment", d2, 0, false, [](auto& d, auto&) { d.Alignment = 0x1000; }), + edited("4 MiB alignment", d3, 0, false, [](auto& d, auto&) { d.Alignment = 0x400000; }), edited("ROW_MAJOR", d2, 0, false, [](auto& d, auto&) { d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; }), // Flags and formats. edited("render-target flag", d2, 0, false, [](auto& d, auto&) { d.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; }), diff --git a/tests/startup_single_element_srv_check.py b/tests/startup_single_element_srv_check.py index bd119fc191..762ce1121d 100644 --- a/tests/startup_single_element_srv_check.py +++ b/tests/startup_single_element_srv_check.py @@ -2,13 +2,14 @@ Each run draws eight 1x4 columns from a translated pixel shader that loads texel (0, 0, 0): 0 RGBA8, layout UNKNOWN 1 RGBA8, layout 0x105 (another address) - 2 the column-0 texel rewritten and published again; it must read the new value - 3 after eleven refused calls (2x1, two mips, two slices, four samples, XOR layout 0x115, BC1, R8, - a UAV flag, the other view dimension, a two-mip view, and a flagless view over a placed UAV texture, - whose texels live on the desktop side): the column-2 view must be unchanged + 2 the column-0 texel rewritten and published again + 3 the same texel rewritten again without a new publication (it must read the new value, in the same frame), + after fifteen refused calls that each name memory holding another texel: 2x1, two mips, two slices, four + samples, XOR layout 0x115, reserved layout 0x10b, 4 MiB alignment, BC1, R8, a UAV flag, the other view + dimension, a two-mip view, a zero address, and flagless views at the base of a placed UAV texture and + 256 bytes into a placed UAV buffer 4 B8G8R8A8, layout 0x100 5 R16G16B16A16_FLOAT, layout 0x105 - 6 a zero address after column 5: refused, column 5's view stays - 7 the column-0 address and description again + 6 column 5's view drawn again 7 the column-0 address with 64 KiB alignment Usage: startup_single_element_srv_check.py Without the single-element path every column except 3 and 6 reads a null view (zero). """ @@ -21,12 +22,13 @@ A = (0x11, 0x22, 0x33, 0xff) B = (0xc0, 0x40, 0x80, 0xff) C = (0x5a, 0xa5, 0x3c, 0xff) +D = (0x81, 0x18, 0xe7, 0xff) BGRA = (0xf0, 0x20, 0x10, 0xff) HALF = (64, 191, 255, 255) # 0.25, 0.75, 1, 1 as UNORM8 def expected(): - columns = [A, B, C, C, BGRA, HALF, HALF, C] + columns = [A, B, C, D, BGRA, HALF, HALF, D] return bytes(c for _ in range(4) for column in columns for c in column) @@ -46,7 +48,7 @@ def check(directory, consumer, backend, dimension): raise AssertionError(f"{dimension} column {(i // 4) % 8} row {i // 32}: actual {actual[i:i + 4].hex()} " f"expected {want[i:i + 4].hex()}") raise AssertionError(f"{dimension}: readback length {len(actual)}, expected {len(want)}") - print(f"{backend} {dimension}: 8 columns of one-texel views exact, eleven refused calls kept the previous view") + print(f"{backend} {dimension}: 8 columns of one-texel views exact, fifteen refused calls kept the previous view") return 0 diff --git a/tests/startup_triage_run_check.py b/tests/startup_triage_run_check.py index 13915f377e..135b7c396e 100644 --- a/tests/startup_triage_run_check.py +++ b/tests/startup_triage_run_check.py @@ -198,6 +198,7 @@ def check_every_slot70_reason_is_reported(failures: list[str]) -> None: ("unsupported memory SRV arguments", "null_address"), ("unsupported memory SRV arguments", "unsupported_description"), ("unsupported memory SRV arguments", "destination_outside_view_heaps"), + ("GPU-written resource at the supplied address", "gpu_written_resource"), ("unavailable buffer at the supplied address", "buffer_unavailable"), ("unavailable texture at the supplied base and layout", "texture_unavailable"), ] diff --git a/tools/startup/triage_run.py b/tools/startup/triage_run.py index ba6b9d4236..9f91488a69 100644 --- a/tools/startup/triage_run.py +++ b/tools/startup/triage_run.py @@ -42,6 +42,7 @@ "null_address", "unsupported_description", "destination_outside_view_heaps", + "gpu_written_resource", "buffer_unavailable", "texture_unavailable", } From b1da87b0715d77cb58b548ca72b4e2f7a8d94185 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 00:42:41 +0100 Subject: [PATCH 5/7] docs: single-element memory SRVs after review: layouts, alignment, GPU-written refusal, limits Co-Authored-By: Claude Opus 5.5 --- docs/experiments.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index a7b100ea2b..1b4c07bfdb 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11486,9 +11486,24 @@ ctest --preset release --no-tests=error --output-on-failure -R '^startup_xg_bc_c ### Single-element 2D and 3D textures published by address -Device slot 70 publishes a 1×1 2D or 1×1×1 3D texture by address, without a resource object. It must have one mip and one sample, and a plain colour format whose element is one texel of 1 to 16 bytes. R8_UNORM keeps its placed-texture path. The view must name the whole texture with the default mapping. The only element is read from offset 0 of the guest memory. That holds for layout UNKNOWN, the value seen on such views, and for linear (`0x100`). It is inferred, not run, for the swizzle modes without pipe/bank XOR (`0x101`–`0x10B`). A swizzle address is a block offset plus a bit pattern of the coordinates, both zero at the origin. AMD addrlib places a single mip at offset 0 (`Gfx10Lib::ComputeSurfaceInfoMacroTiled` at the pinned [revision](https://gitlab.freedesktop.org/mesa/mesa/-/tree/5c142e46f3f6e752ed745fd48912ebb8fad67145/src/amd/addrlib)). XOR modes (`0x110` and up) can move the origin and stay refused, as do larger shapes, mip chains, arrays, multisampling, block-compressed formats and render-target, depth or UAV flags. A live render-target, depth or UAV resource at the same address is written on the desktop side, so such a view keeps the existing lookup, which needs the same description. The translated draw uploads the texel through the footprint path, with the change detection of the other address-only views: a new publication of the view re-reads it (tested), and by code reading so does the next frame's first bind (not tested here, since the fixture records one frame). +Device slot 70 publishes a 1×1 2D or 1×1×1 3D texture by address, without a resource object. It must have one mip, one sample, default or 64 KiB alignment, and a plain colour format whose element is one texel of 1 to 16 bytes. R8_UNORM keeps its placed-texture path. The view must name the whole texture with the default mapping. The only element is read from offset 0 of the guest memory. That holds for layout UNKNOWN, the value seen on such views, and for linear (`0x100`). For the swizzle modes AMD addrlib defines without pipe/bank XOR (`0x101`, `0x102`, `0x105`, `0x106`, `0x109`, `0x10A`) it is inferred from the source, not run: a swizzle address is a block offset plus a bit pattern of the coordinates, both zero at the origin, and a single mip sits at offset 0 (`Gfx10Lib::ComputeSurfaceInfoMacroTiled` at the pinned [revision](https://gitlab.freedesktop.org/mesa/mesa/-/tree/5c142e46f3f6e752ed745fd48912ebb8fad67145/src/amd/addrlib)). Reserved modes (`0x103`, `0x104`, `0x107`, `0x108`, `0x10B`) and XOR modes (`0x110` and up) stay refused. So do larger shapes, mip chains, arrays, multisampling, other alignments, block-compressed formats and render-target, depth or UAV flags. A view over a live render-target, depth or UAV resource that covers the address (its base, or inside its known allocation size) is refused with `reason=gpu_written_resource`, because that resource is written on the desktop side. The translated draw uploads the texel through the footprint path and rehashes it on every bind, so a write between two draws is seen. -`startup_single_element_srv` checks the accepted and refused descriptions without a device; its `xor_control` expects layout `0x115` to be accepted and must fail. `startup_address_single_element_{warp,hardware}` draws eight columns per dimension from a translated shader that loads texel zero. The columns are RGBA8 at layouts 0 and `0x105`, the first texel rewritten and published again, B8G8R8A8 and R16G16B16A16_FLOAT. Ten refused descriptions, a flagless view over a placed UAV texture and a zero address must leave the previous view in place. On Windows 11 build 26300 with the debug layer off, RTX 5090 (32.0.16.2135) and WARP (10.0.26100.10068) matched all 32 RGBA pixels per dimension. With the runtime change reverted, column 0 reads zero. An upload that skips the texel after its first copy fails column 2, which then reads the old texel. +Limits: the GPU-written check runs when the view is published, so a resource created at the address later is not seen, and a resource whose allocation size is unknown covers only its base. Published address-only views are never evicted (as for the BC views above). + +`startup_single_element_srv` checks 50 descriptions without a device; its `xor_control` expects layout `0x115` to be accepted and must fail. `startup_address_single_element_{warp,hardware}` draws eight columns per dimension from a translated shader that loads texel zero. The columns are: + +- RGBA8 at layouts 0 and `0x105`; +- the first texel rewritten and published again; +- the texel rewritten without a new publication, in the same frame; +- B8G8R8A8 and R16G16B16A16_FLOAT; +- 64 KiB alignment. + +Before the fourth column, fifteen refused calls name memory holding another texel; the previous view must stay. On Windows 11 build 26300 with the debug layer off, RTX 5090 (32.0.16.2135) and WARP (10.0.26100.10068) matched all 32 RGBA pixels per dimension. Each of these changes fails the WARP draw: + +- reverting the runtime change, which makes column 0 read zero; +- accepting 2x1, two mips or layout `0x115` (column 3 reads the other texel); +- dropping the per-bind rehash (column 3 reads the previous texel); +- checking only the base of a GPU-written resource (column 3 reads the other texel). ```powershell cmake --build --preset release --target startup_stream_consumer startup_single_element_srv_check d3d12_xs_shim From acdc50e84a8428eaabd2bd06b301a616f4b7e249 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 02:48:56 +0100 Subject: [PATCH 6/7] tests: assert refused single-element calls leave the destination descriptor unchanged The consumer compares the destination's 32 bytes after each of the fifteen refused calls and exits with 60 plus the call's position when one changed them. Accepting 2x1 now fails with 61. Co-Authored-By: Claude Opus 5.5 --- tests/startup_address_srv.cpp | 14 +++++++++++++- tests/startup_single_element_srv_check.py | 3 ++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/startup_address_srv.cpp b/tests/startup_address_srv.cpp index 0f92d11402..a763908984 100644 --- a/tests/startup_address_srv.cpp +++ b/tests/startup_address_srv.cpp @@ -368,7 +368,8 @@ int draw_address_buffer_srvs(ID3D12Device* desktop, void* guest, void* list) // One-texel textures published by address (X2W_TEST_SINGLE_ELEMENT=2d or 3d), one 1x4 column per draw. The texel // sits at offset 0 of its memory whatever the layout field says. Before column 3 the column-2 texel is rewritten // without a new publication, then fifteen calls are refused; each names memory holding another texel, so any of -// them taking the descriptor would change column 3. +// them taking the descriptor would change column 3. Each refused call must also leave the destination's 32 bytes +// (plain memory behind a view heap created without title memory) unchanged; the first that does not returns 60+. int draw_single_element_srvs(void* guest, void* list) { wchar_t mode[4]{}; @@ -412,11 +413,19 @@ int draw_single_element_srvs(void* guest, void* list) std::memcpy(memory[0], c, 4); // rewritten, then published again at the same address publish(rgba, view_of(rgba, ptr(memory[0]))); draw(2); std::memcpy(memory[0], d, 4); // rewritten without a new publication: column 3 must read it + unsigned char published[32]; + std::memcpy(published, reinterpret_cast(cpu), 32); + int refusals = 0, changed = 0; + const auto unchanged = [&] { + ++refusals; + if (!changed && std::memcmp(published, reinterpret_cast(cpu), 32)) changed = refusals; + }; const auto refuse = [&](auto edit) { auto desc = rgba; auto v = view_of(desc, ptr(memory[5])); edit(desc, v); publish(desc, v); + unchanged(); }; refuse([](auto& d, auto&) { d.Width = 2; }); refuse([](auto& d, auto&) { d.MipLevels = 2; }); @@ -449,7 +458,10 @@ int draw_single_element_srvs(void* guest, void* list) FAILED(static_cast(invoke(guest, 133, ptr(memory[6]), ptr(&written_buffer), D3D12_RESOURCE_STATE_COMMON, 0, ptr(&__uuidof(ID3D12Resource)), ptr(&uav[1]))))) return 48; publish(rgba, view_of(rgba, ptr(memory[4]))); + unchanged(); publish(rgba, view_of(rgba, ptr(memory[7]))); + unchanged(); + if (changed) return 60 + changed; draw(3); const auto bgra_desc = texel(DXGI_FORMAT_B8G8R8A8_UNORM, 0x100); publish(bgra_desc, view_of(bgra_desc, ptr(memory[2]))); draw(4); diff --git a/tests/startup_single_element_srv_check.py b/tests/startup_single_element_srv_check.py index 762ce1121d..d199a229e0 100644 --- a/tests/startup_single_element_srv_check.py +++ b/tests/startup_single_element_srv_check.py @@ -7,7 +7,8 @@ after fifteen refused calls that each name memory holding another texel: 2x1, two mips, two slices, four samples, XOR layout 0x115, reserved layout 0x10b, 4 MiB alignment, BC1, R8, a UAV flag, the other view dimension, a two-mip view, a zero address, and flagless views at the base of a placed UAV texture and - 256 bytes into a placed UAV buffer + 256 bytes into a placed UAV buffer; the consumer also checks that each refused call left the destination + descriptor's 32 bytes unchanged and exits with 60 + the call's position otherwise 4 B8G8R8A8, layout 0x100 5 R16G16B16A16_FLOAT, layout 0x105 6 column 5's view drawn again 7 the column-0 address with 64 KiB alignment Usage: startup_single_element_srv_check.py From 84c66897860d9aef3b2d879e89b773099905577b Mon Sep 17 00:00:00 2001 From: Royoo <3230853833@qq.com> Date: Wed, 25 Nov 2026 10:36:01 +0800 Subject: [PATCH 7/7] Reject an entire texel that overlaps live GPU-written memory --- docs/experiments.md | 2 +- src/startup_stubs/d3d12_memory_srv.inl | 12 ++++++------ tests/startup_address_srv.cpp | 6 ++++-- tests/startup_single_element_srv_check.py | 6 +++--- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 255fb57cc7..cc0b4322ec 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11754,7 +11754,7 @@ ctest --preset release --no-tests=error --output-on-failure -R '^startup_xg_bc_c Device slot 70 publishes a 1×1 2D or 1×1×1 3D texture by address, without a resource object. It must have one mip, one sample, default or 64 KiB alignment, and a plain colour format whose element is one texel of 1 to 16 bytes. R8_UNORM keeps its placed-texture path. The view must name the whole texture with the default mapping. The only element is read from offset 0 of the guest memory. That holds for layout UNKNOWN, the value seen on such views, and for linear (`0x100`). For the swizzle modes AMD addrlib defines without pipe/bank XOR (`0x101`, `0x102`, `0x105`, `0x106`, `0x109`, `0x10A`) it is inferred from the source, not run: a swizzle address is a block offset plus a bit pattern of the coordinates, both zero at the origin, and a single mip sits at offset 0 (`Gfx10Lib::ComputeSurfaceInfoMacroTiled` at the pinned [revision](https://gitlab.freedesktop.org/mesa/mesa/-/tree/5c142e46f3f6e752ed745fd48912ebb8fad67145/src/amd/addrlib)). Reserved modes (`0x103`, `0x104`, `0x107`, `0x108`, `0x10B`) and XOR modes (`0x110` and up) stay refused. So do larger shapes, mip chains, arrays, multisampling, other alignments, block-compressed formats and render-target, depth or UAV flags. A view over a live render-target, depth or UAV resource that covers the address (its base, or inside its known allocation size) is refused with `reason=gpu_written_resource`, because that resource is written on the desktop side. The translated draw uploads the texel through the footprint path and rehashes it on every bind, so a write between two draws is seen. -Limits: the GPU-written check runs when the view is published, so a resource created at the address later is not seen, and a resource whose allocation size is unknown covers only its base. Published address-only views are never evicted (as for the BC views above). +Limits: the GPU-written check runs when the view is published, so it does not see a resource created later. The check compares the entire texel with the live resource range (only its base byte if the allocation size is unknown). Published address-only views are never evicted (as for the BC views above). `startup_single_element_srv` checks 50 descriptions without a device; its `xor_control` expects layout `0x115` to be accepted and must fail. `startup_address_single_element_{warp,hardware}` draws eight columns per dimension from a translated shader that loads texel zero. The columns are: diff --git a/src/startup_stubs/d3d12_memory_srv.inl b/src/startup_stubs/d3d12_memory_srv.inl index 241ea10b53..06866ae732 100644 --- a/src/startup_stubs/d3d12_memory_srv.inl +++ b/src/startup_stubs/d3d12_memory_srv.inl @@ -86,10 +86,9 @@ Wrapper* existing_memory_texture(u64 address, const D3D12_RESOURCE_DESC& desc) return match; } -// A live render-target, depth or UAV resource covering the address is written on the desktop side, so title -// memory there does not hold its texels. A resource covers its base address, and the bytes after it when its -// allocation size (or a buffer's width) is known. -bool gpu_written_at(u64 address) +// A live render-target, depth or UAV resource overlapping the texel is written on the desktop side, so its +// title memory does not hold the texel. For a resource of unknown size, its base is the only known byte. +bool gpu_written_overlap(u64 address, UINT texel_bytes) { constexpr UINT gpu_written = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; @@ -98,7 +97,8 @@ bool gpu_written_at(u64 address) for (const auto& [wrapper, info] : resource_infos()) { const u64 base = reinterpret_cast(wrapper->own().title_memory); const u64 bytes = info.bytes ? info.bytes : info.console.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER ? info.console.Width : 0; - if (base && (address == base || (address > base && address - base < bytes)) && + const bool overlap = address <= base ? base - address < texel_bytes : address - base < bytes; + if (base && overlap && (static_cast(info.console.Flags) & gpu_written) && InterlockedCompareExchange(&wrapper->own().refs, 0, 0) > 0) { found = true; break; @@ -239,7 +239,7 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou } // A single-element texture is published by address like a tiled view: its texel is read at offset 0. if (x2w_startup::supports_single_element_srv(*resource, input->view)) { - if (gpu_written_at(input->address)) { + if (gpu_written_overlap(input->address, x2w_startup::single_element_bytes(*resource))) { if (first_calls(refused, 8)) log_refused_memory_srv("GPU-written resource at the supplied address", "gpu_written_resource", resource, input, destination); } else if (Wrapper* texture = publish_tiled_texture(self, input->address, *resource)) diff --git a/tests/startup_address_srv.cpp b/tests/startup_address_srv.cpp index a763908984..be44cc3c4d 100644 --- a/tests/startup_address_srv.cpp +++ b/tests/startup_address_srv.cpp @@ -367,7 +367,7 @@ int draw_address_buffer_srvs(ID3D12Device* desktop, void* guest, void* list) // One-texel textures published by address (X2W_TEST_SINGLE_ELEMENT=2d or 3d), one 1x4 column per draw. The texel // sits at offset 0 of its memory whatever the layout field says. Before column 3 the column-2 texel is rewritten -// without a new publication, then fifteen calls are refused; each names memory holding another texel, so any of +// without a new publication, then sixteen calls are refused; each names memory holding another texel, so any of // them taking the descriptor would change column 3. Each refused call must also leave the destination's 32 bytes // (plain memory behind a view heap created without title memory) unchanged; the first that does not returns 60+. int draw_single_element_srvs(void* guest, void* list) @@ -443,7 +443,7 @@ int draw_single_element_srvs(void* guest, void* list) refuse([](auto&, auto& v) { v.view.Texture2D.MipLevels = 2; }); refuse([](auto&, auto& v) { v.address = 0; }); // Placed UAV resources are written on the desktop side. A flagless view at the base of a 1x1 UAV texture, - // and one 256 bytes into a 512-byte UAV buffer, stay refused. + // one 256 bytes into a 512-byte UAV buffer, and one that straddles the buffer base stay refused. auto written = rgba; written.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; D3D12_RESOURCE_DESC written_buffer{}; @@ -461,6 +461,8 @@ int draw_single_element_srvs(void* guest, void* list) unchanged(); publish(rgba, view_of(rgba, ptr(memory[7]))); unchanged(); + publish(rgba, view_of(rgba, ptr(memory[5] + 254))); // two texel bytes precede the UAV buffer base + unchanged(); if (changed) return 60 + changed; draw(3); const auto bgra_desc = texel(DXGI_FORMAT_B8G8R8A8_UNORM, 0x100); diff --git a/tests/startup_single_element_srv_check.py b/tests/startup_single_element_srv_check.py index d199a229e0..dbb630ef39 100644 --- a/tests/startup_single_element_srv_check.py +++ b/tests/startup_single_element_srv_check.py @@ -4,10 +4,10 @@ 0 RGBA8, layout UNKNOWN 1 RGBA8, layout 0x105 (another address) 2 the column-0 texel rewritten and published again 3 the same texel rewritten again without a new publication (it must read the new value, in the same frame), - after fifteen refused calls that each name memory holding another texel: 2x1, two mips, two slices, four + after sixteen refused calls that each name memory holding another texel: 2x1, two mips, two slices, four samples, XOR layout 0x115, reserved layout 0x10b, 4 MiB alignment, BC1, R8, a UAV flag, the other view dimension, a two-mip view, a zero address, and flagless views at the base of a placed UAV texture and - 256 bytes into a placed UAV buffer; the consumer also checks that each refused call left the destination + 256 bytes into a placed UAV buffer, plus a texel straddling that buffer's base; the consumer also checks that each refused call left the destination descriptor's 32 bytes unchanged and exits with 60 + the call's position otherwise 4 B8G8R8A8, layout 0x100 5 R16G16B16A16_FLOAT, layout 0x105 6 column 5's view drawn again 7 the column-0 address with 64 KiB alignment @@ -49,7 +49,7 @@ def check(directory, consumer, backend, dimension): raise AssertionError(f"{dimension} column {(i // 4) % 8} row {i // 32}: actual {actual[i:i + 4].hex()} " f"expected {want[i:i + 4].hex()}") raise AssertionError(f"{dimension}: readback length {len(actual)}, expected {len(want)}") - print(f"{backend} {dimension}: 8 columns of one-texel views exact, fifteen refused calls kept the previous view") + print(f"{backend} {dimension}: 8 columns of one-texel views exact, sixteen refused calls kept the previous view") return 0