From 72f4a33935da9ed1f814caecdf7b7e5fa63532a3 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Tue, 24 Nov 2026 23:39:34 +0100 Subject: [PATCH 1/5] startup: answer 4 KiB allocation-info requests for small 2D textures A console executable asks slots 25 and 108 with Alignment 0x1000 for a texture that is not a render or depth target and whose top level, padded to 4 KiB blocks, holds at most 0x80007 bits, and keeps the answer only when its alignment is 0x1000. sanitized() dropped the request and the desktop answer, 64 KiB, made the executable size such a texture at 16 pages and stream 64 KiB of archive data into it, ending inside a following compressed chunk. For a single 2D texture with layout UNKNOWN, one sample, one slice, power-of-two dimensions, a format of 4 to 128 bits a texel and the executable's bound, both slots now answer 4 KiB alignment and the GFX10 4 KiB thin-block size as addrlib computes it. Anything else keeps the desktop answer. Tests: literal sizes and refused descriptions, a whole-width-tail mutation that must fail, every accepted shape against Mesa 25.1.0 addrlib SW_4KB_S (skips without it), and the wrapper's two slots. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 23 ++ docs/startup-runtime.md | 42 ++++ src/startup_stubs/d3d12_xs_device.cpp | 14 ++ src/startup_stubs/small_texture_4k.h | 92 ++++++++ tests/startup_small_texture_4k_check.cpp | 199 ++++++++++++++++++ .../startup_small_texture_4k_device_check.cpp | 102 +++++++++ 6 files changed, 472 insertions(+) create mode 100644 src/startup_stubs/small_texture_4k.h create mode 100644 tests/startup_small_texture_4k_check.cpp create mode 100644 tests/startup_small_texture_4k_device_check.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ae6c655cf3..1b65ba33a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1702,6 +1702,29 @@ else() endif() add_test(NAME xg_bc64_tiling_reference COMMAND xg_swizzle_select_check --bc64-tiling) set_tests_properties(xg_bc64_tiling_reference PROPERTIES TIMEOUT 30 SKIP_RETURN_CODE 77) +# 4 KiB allocation answers for small 2D textures (src/startup_stubs/small_texture_4k.h): literal sizes and +# refused descriptions without a device; the mutated run is the negative control; the addrlib run compares +# every accepted shape with addrlib's GFX10 4 KiB layout and skips (77) without addrlib. +add_executable(startup_small_texture_4k_check tests/startup_small_texture_4k_check.cpp) +target_compile_features(startup_small_texture_4k_check PRIVATE cxx_std_17) +target_include_directories(startup_small_texture_4k_check PRIVATE src) +if(XBOX2WINDOWS_ADDRLIB_DIR) + target_link_libraries(startup_small_texture_4k_check PRIVATE xbw_addrlib) + target_compile_definitions(startup_small_texture_4k_check PRIVATE XBOX2WINDOWS_HAVE_ADDRLIB) +endif() +add_test(NAME startup_small_texture_4k COMMAND startup_small_texture_4k_check) +add_test(NAME startup_small_texture_4k_whole_width_tail_control COMMAND startup_small_texture_4k_check --mutate whole-width-tail) +set_tests_properties(startup_small_texture_4k_whole_width_tail_control PROPERTIES WILL_FAIL TRUE) +add_test(NAME startup_small_texture_4k_addrlib COMMAND startup_small_texture_4k_check --addrlib) +set_tests_properties(startup_small_texture_4k_addrlib PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77) +# The same answers through the device wrapper: allocation-info slots 25 and 108, with controls. +add_executable(startup_small_texture_4k_device_check tests/startup_small_texture_4k_device_check.cpp) +target_compile_features(startup_small_texture_4k_device_check PRIVATE cxx_std_17) +target_link_libraries(startup_small_texture_4k_device_check PRIVATE d3d12_xs_shim d3d12 dxgi) +foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_small_texture_4k_device_${adapter} COMMAND startup_small_texture_4k_device_check ${adapter}) +endforeach() +set_tests_properties(startup_small_texture_4k_device_hardware PROPERTIES SKIP_RETURN_CODE 77) # Which descriptions take the 64 KiB standard BC chain (src/startup_stubs/bc64_chain.h), without addrlib # or a device. The mutated run expects layout UNKNOWN to be accepted and is the negative control. add_executable(startup_bc64_supports_check tests/startup_bc64_supports_check.cpp) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index f4da9a2a75..f05cd427d9 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1302,6 +1302,48 @@ R24G8 or the added colour families. desktop texture that does not alias the address, so this shows the answered size is accepted by the creation path, not that the console layout fits in it. +## 4 KiB allocation answers for small textures + +A console executable asks the allocation-info entries (slots 25 and 108) with `Alignment` 0x1000 for +a texture that is not a render or depth target and whose top level, padded to 4 KiB blocks, holds at +most 0x80007 bits. It keeps the answer only when the returned alignment is 0x1000, and otherwise asks +again with alignment 0. The stand-in used to drop the 4 KiB request (`sanitized()`) and forward the +desktop answer, 64 KiB at 64 KiB alignment. The executable then recorded 16 pages for a texture the +console sizes at one, and its streaming read for that texture ran 64 KiB through the following +resources' compressed chunks. It ended inside one of them, which the executable's own check rejects +before the request is submitted. The storage stand-in then refused the request as destination +capacity exceeded. + +For a description in scope the stand-in now answers the console size at 4 KiB alignment +(`src/startup_stubs/small_texture_4k.h`): the GFX10 4 KiB thin-block layout as AMD addrlib computes it. +Each level is padded to whole blocks (128 x 64 texels at 4 bits a texel, 64 x 64 at 8, 64 x 32 at 16, +32 x 32 at 32, 32 x 16 at 64, 16 x 16 at 128; block-compressed formats count 4 x 4 blocks) until the +first level no wider than half a block and no taller than a block, while at most 8 levels remain. +That level and every smaller one share one block. The first eight uses are logged as +`x2w-d3d12 allocation info: 4 KiB answer ...`. Desktop D3D12 asked with a 4 KiB alignment is not a +substitute: on the adapter measured it answered 0x2000 for a 128 x 4 R16G16B16A16 texture, where the +4 KiB layout takes 0x4000. + +Scope: a 2D texture with layout UNKNOWN, `Alignment` 0x1000, one sample, one array slice, power-of-two +width and height, `MipLevels` 0 (full chain) up to the full chain, no render-target or depth flag, a +format of 4, 8, 16, 32, 64 or 128 bits a texel (`small_texture_4k_bits`), and the executable's +0x80007-bit bound. Any other description, arrays, other layouts and 1D or 3D textures included, keeps +the desktop answer. + +Measured on one console executable under the startup debugger: 18 such reads failed during +first-run setup in every run, each with the executable's check firing once. With this answer both +counts were 0 to the same screen. Over the executable's archives, every resource of the family it +places in 4 KiB blocks starts exactly the size above after the previous one. + +- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k(_whole_width_tail_control)?$' -V` + checks literal sizes and the refused descriptions without a device; the control expects a tail one + block wide and must fail. +- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_addrlib$' -V` compares every + accepted power-of-two shape and mip count, for one format of each bit depth, with addrlib's + `ADDR_SW_4KB_S` size (Mesa 25.1.0, `tools/fetch_addrlib.py`); it skips (77) without addrlib. +- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_device_' -V` goes through the + wrapper: slots 25 and 108 must give the 4 KiB answer, and controls the desktop answer. + ## Physical pages in shared sections `XMemAllocatePhysicalPages` pages are 64 KiB slots of 256 MiB pagefile-backed sections. With diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 902309418d..e17a40c7fa 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -33,6 +33,7 @@ #include #include "command_list_slot_names.h" #include "console_plane_size.h" +#include "small_texture_4k.h" #include "null_binding_cause.h" #include "null_texture_view.h" #include "packed_target_format.h" @@ -873,6 +874,17 @@ void note_console_size(const D3D12_RESOURCE_DESC& desc, const D3D12_RESOURCE_ALL info.SizeInBytes, desc.Width, desc.Height, desc.Format, desc.SampleDesc.Count); } +// A single small 2D texture asked with a 4 KiB alignment gets the console's 4 KiB answer (small_texture_4k.h). +bool small_texture_answer(UINT count, const D3D12_RESOURCE_DESC* descs, D3D12_RESOURCE_ALLOCATION_INFO& info) +{ + if (count != 1 || !descs || !x2w_startup::small_texture_4k(descs[0], info)) return false; + static LONG calls; + if (first_calls(calls, 8)) + log("x2w-d3d12 allocation info: 4 KiB answer size 0x%llx for %llux%u mips %u format %d", info.SizeInBytes, descs[0].Width, + descs[0].Height, descs[0].MipLevels, descs[0].Format); + return true; +} + // Device slot 25, desktop GetResourceAllocationInfo(visibleMask, count, descs), returned by value. void* __stdcall device_resource_allocation_info(void* a, void* b, UINT mask, UINT count, const D3D12_RESOURCE_DESC* descs) { @@ -883,6 +895,7 @@ void* __stdcall device_resource_allocation_info(void* a, void* b, UINT mask, UIN D3D12_RESOURCE_ALLOCATION_INFO info = device_of(self)->GetResourceAllocationInfo(mask, count, copies.data()); // A single description of a family that is larger on desktop gets its console size (console_plane_size.h). if (count == 1 && x2w_startup::console_plane_size(descs[0], info)) note_console_size(descs[0], info); + small_texture_answer(count, descs, info); *static_cast(result) = info; static LONG calls, invalid; if (first_calls(calls)) @@ -972,6 +985,7 @@ void* __stdcall device_resource_allocation_info1(void* a, void* b, UINT mask, UI if (info1) *info1 = {0, info.Alignment, info.SizeInBytes}; note_console_size(descs[0], info); } + if (small_texture_answer(count, descs, info) && info1) *info1 = {0, info.Alignment, info.SizeInBytes}; *static_cast(result) = info; static LONG calls, invalid; if (first_calls(calls)) diff --git a/src/startup_stubs/small_texture_4k.h b/src/startup_stubs/small_texture_4k.h new file mode 100644 index 0000000000..64eb0de1da --- /dev/null +++ b/src/startup_stubs/small_texture_4k.h @@ -0,0 +1,92 @@ +// Console allocation answer for a small 2D texture asked with a 4 KiB alignment (docs/startup-runtime.md). +// +// A console executable asks the allocation-info entries with Alignment 0x1000 for a texture that is not a +// render or depth target and whose top level, padded to 4 KiB blocks, holds at most 0x80007 bits over all +// array slices. It keeps the answer only when the returned alignment is 0x1000 and otherwise asks again +// with alignment 0. Desktop D3D12 answers such a texture with 64 KiB; the console sizes it in 4 KiB blocks. +// +// The size is the GFX10 4 KiB thin-block layout as AMD addrlib computes it (Gfx10Lib, Mesa 25.1.0): each +// level is padded to whole blocks until the first level that fits a tail of half a block's width and a +// block's height while at most 8 levels remain; that level and every smaller one share one block. +// +// Scope, any other description keeps the desktop answer: a 2D texture with layout UNKNOWN, Alignment +// 0x1000, one sample, one array slice, power-of-two width and height (so block-compressed mip sizes are the +// same in texels and in blocks), MipLevels 0 (full chain) to the full chain, no render-target or depth +// flag, a format whose bits per texel are 4, 8, 16, 32, 64 or 128 (small_texture_4k_bits), and the top +// level within the executable's 0x80007-bit bound. +#pragma once + +#include + +#include + +namespace x2w_startup { +namespace small_texture_4k_detail { +// 4 KiB thin block in texels for a bit depth; {0, 0} for other depths. Block-compressed formats count +// their 4x4 texel blocks (8 or 16 bytes) as elements. +struct Block { std::uint64_t width, height; }; +inline Block block(unsigned bits) +{ + switch (bits) { + case 4: return {128, 64}; // 8-byte blocks, 32 x 16 elements + case 8: return {64, 64}; // 8-bit texels, or 16-byte blocks at 16 x 16 elements + case 16: return {64, 32}; + case 32: return {32, 32}; + case 64: return {32, 16}; + case 128: return {16, 16}; + default: return {0, 0}; + } +} +inline bool power_of_two(std::uint64_t v) { return v && !(v & (v - 1)); } +} // namespace small_texture_4k_detail + +// Bits per texel of the formats answered; 0 for every other format. +inline unsigned small_texture_4k_bits(unsigned format) +{ + const unsigned f = format; + if (f >= 1 && f <= 4) return 128; // R32G32B32A32 + if (f >= 9 && f <= 22) return 64; // R16G16B16A16, R32G32, R32G8X24 + if ((f >= 23 && f <= 47) || f == 67 || (f >= 87 && f <= 93)) return 32; // R10G10B10A2 .. R24G8, R9G9B9E5, B8G8R8A8 + if ((f >= 48 && f <= 59) || f == 85 || f == 86 || f == 115) return 16; // R8G8, R16, B5G6R5, B5G5R5A1, B4G4R4A4 + if (f >= 60 && f <= 65) return 8; // R8, A8 + if ((f >= 70 && f <= 72) || (f >= 79 && f <= 81)) return 4; // BC1, BC4 + if ((f >= 73 && f <= 78) || (f >= 82 && f <= 84) || (f >= 94 && f <= 99)) return 8; // BC2, BC3, BC5, BC6H, BC7 + return 0; +} + +// Bytes of the 4 KiB-block layout of a single-slice 2D texture (no scope checks). +inline std::uint64_t small_texture_4k_bytes(std::uint64_t width, std::uint64_t height, unsigned mips, unsigned bits) +{ + const auto b = small_texture_4k_detail::block(bits); + std::uint64_t blocks = 0; + for (unsigned m = 0; m < mips; ++m) { + const std::uint64_t w = (width >> m) ? (width >> m) : 1, h = (height >> m) ? (height >> m) : 1; + if (mips > 1 && w <= b.width / 2 && h <= b.height && mips - m <= 8) return (blocks + 1) * 0x1000; + blocks += (w + b.width - 1) / b.width * ((h + b.height - 1) / b.height); + } + return blocks * 0x1000; +} + +// Replaces info with the console answer and returns true for a description in scope; false leaves it. +inline bool small_texture_4k(const D3D12_RESOURCE_DESC& d, D3D12_RESOURCE_ALLOCATION_INFO& info) +{ + using namespace small_texture_4k_detail; + if (d.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || d.Alignment != 0x1000 || d.Layout != D3D12_TEXTURE_LAYOUT_UNKNOWN || + d.SampleDesc.Count != 1 || d.DepthOrArraySize != 1 || !power_of_two(d.Width) || !power_of_two(d.Height)) + return false; + if (d.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) return false; + const unsigned bits = small_texture_4k_bits(d.Format); + const Block b = block(bits); + if (!b.width) return false; + unsigned full = 0; + for (std::uint64_t e = d.Width > d.Height ? d.Width : d.Height; e; e >>= 1) ++full; + const unsigned mips = d.MipLevels ? d.MipLevels : full; + if (mips > full) return false; + // The executable's bound: the top level padded to whole blocks, in bits, at most 0x80007. + const std::uint64_t padded = (d.Width + b.width - 1) / b.width * b.width * ((d.Height + b.height - 1) / b.height * b.height); + if (padded * bits > 0x80007) return false; + info = {small_texture_4k_bytes(d.Width, d.Height, mips, bits), 0x1000}; + return true; +} + +} // namespace x2w_startup diff --git a/tests/startup_small_texture_4k_check.cpp b/tests/startup_small_texture_4k_check.cpp new file mode 100644 index 0000000000..0c12c74d66 --- /dev/null +++ b/tests/startup_small_texture_4k_check.cpp @@ -0,0 +1,199 @@ +// Checks src/startup_stubs/small_texture_4k.h without a device: the 4 KiB answers for small 2D textures +// asked with a 4 KiB alignment, and the descriptions it leaves alone. +// +// startup_small_texture_4k_check literal sizes and refused descriptions +// startup_small_texture_4k_check --mutate whole-width-tail expectations of a rule whose mip tail is a +// whole block wide (negative control: fails) +// startup_small_texture_4k_check --addrlib every accepted shape against AMD addrlib's +// GFX10 4 KiB layout; 77 when built without it +// +// The literal sizes are worked by hand from the 4 KiB blocks (128x64 texels at 4 bits, 64x64 at 8, 64x32 at +// 16, 32x32 at 32, 32x16 at 64, 16x16 at 128) and the tail rule; the --addrlib run reproduces all of them. +// addrlib: Mesa 25.1.0 (5c142e46f3f6e752ed745fd48912ebb8fad67145) src/amd/addrlib, MIT, fetched by +// tools/fetch_addrlib.py; nothing is vendored. +#include "startup_stubs/small_texture_4k.h" + +#include +#include + +namespace { +int failures; + +D3D12_RESOURCE_DESC texture(unsigned format, unsigned width, unsigned height, unsigned mips = 1, UINT64 alignment = 0x1000) +{ + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + desc.Alignment = alignment; + desc.Width = width; + desc.Height = height; + desc.DepthOrArraySize = 1; + desc.MipLevels = static_cast(mips); + desc.Format = static_cast(format); + desc.SampleDesc.Count = 1; + return desc; +} + +const D3D12_RESOURCE_ALLOCATION_INFO desktop{0x10000, 0x10000}; + +void answered(const char* name, const D3D12_RESOURCE_DESC& desc, unsigned long long expected) +{ + D3D12_RESOURCE_ALLOCATION_INFO info = desktop; + const bool changed = x2w_startup::small_texture_4k(desc, info); + if (!changed || info.SizeInBytes != expected || info.Alignment != 0x1000) { + std::printf("FAIL %s: changed %d size 0x%llx alignment 0x%llx, expected 0x%llx at 0x1000\n", name, changed, + static_cast(info.SizeInBytes), static_cast(info.Alignment), expected); + ++failures; + } +} + +void kept(const char* name, const D3D12_RESOURCE_DESC& desc) +{ + D3D12_RESOURCE_ALLOCATION_INFO info = desktop; + if (x2w_startup::small_texture_4k(desc, info) || info.SizeInBytes != desktop.SizeInBytes || info.Alignment != desktop.Alignment) { + std::printf("FAIL %s: the desktop answer was changed to 0x%llx at 0x%llx\n", name, + static_cast(info.SizeInBytes), static_cast(info.Alignment)); + ++failures; + } +} + +int literal(bool whole_width_tail) +{ + // whole_width_tail: what a tail one block wide (instead of half) would answer. + const auto rule = [whole_width_tail](unsigned long long real, unsigned long long mutated) { return whole_width_tail ? mutated : real; }; + answered("32x16 R8G8B8A8", texture(28, 32, 16), 0x1000); + answered("128x4 R16G16B16A16 (desktop at 4 KiB answers 0x2000)", texture(11, 128, 4), 0x4000); + answered("4x4 BC1", texture(71, 4, 4), 0x1000); + answered("128x128 R16 float", texture(54, 128, 128), 0x8000); + answered("256x256 BC7, the 64 KiB bound", texture(98, 256, 256), 0x10000); + answered("16x16 R32G32B32A32 float", texture(2, 16, 16), 0x1000); + answered("128x128 BC1 6 mips: 2 blocks + tail", texture(71, 128, 128, 6), 0x3000); + answered("64x128 BC1 5 mips: 2 blocks + tail", texture(71, 64, 128, 5), 0x3000); + answered("64x64 BC7 5 mips: 1 block + tail", texture(98, 64, 64, 5), rule(0x2000, 0x1000)); + answered("128x64 BC7 5 mips: 2 + 1 + tail", texture(98, 128, 64, 5), rule(0x4000, 0x3000)); + answered("256x64 BC1 5 mips: 2 + 1 + tail", texture(71, 256, 64, 5), rule(0x4000, 0x3000)); + answered("128x32 BC1 4 mips: 1 + tail", texture(71, 128, 32, 4), rule(0x2000, 0x1000)); + answered("128x128 R8G8B8A8 8 mips: 16 + 4 + 1 + tail", texture(28, 128, 128, 8), rule(0x16000, 0x15000)); + answered("1024x1 R8 full chain (MipLevels 0): 16+8+4+2+1 + tail", texture(61, 1024, 1, 0), 0x20000); + + // Refused: the desktop answer stays. + kept("alignment 0", texture(28, 32, 16, 1, 0)); + kept("alignment 64 KiB", texture(28, 32, 16, 1, 0x10000)); + D3D12_RESOURCE_DESC d = texture(28, 32, 16); + d.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + kept("render target", d); + d = texture(40, 32, 16); + d.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + kept("depth target", d); + kept("256x128 R8G8B8A8, over the 64 KiB bound", texture(28, 256, 128)); + kept("512x256 BC7, over the bound", texture(98, 512, 256)); + d = texture(28, 32, 16); + d.DepthOrArraySize = 2; + kept("array of 2", d); + kept("48x16 R8G8B8A8, width not a power of two", texture(28, 48, 16)); + kept("R32G32B32 float, 96 bits", texture(6, 32, 16)); + kept("32x16 with 7 mips, more than the chain", texture(28, 32, 16, 7)); + d = texture(28, 32, 16); + d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE; + kept("64 KiB layout", d); + d.Layout = static_cast(0x102); + kept("console layout code", d); + d = texture(28, 32, 16); + d.SampleDesc.Count = 4; + kept("4 samples", d); + d = texture(28, 32, 16); + d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D; + kept("3D texture", d); + if (failures) { + std::printf("startup_small_texture_4k: %d failure(s)\n", failures); + return 1; + } + std::printf("startup_small_texture_4k: PASS (14 sizes, 14 refused descriptions)\n"); + return 0; +} +} // namespace + +#ifndef XBOX2WINDOWS_HAVE_ADDRLIB +int addrlib_check() +{ + std::puts("startup_small_texture_4k --addrlib: SKIP (built without addrlib; run tools/fetch_addrlib.py and configure with " + "-DXBOX2WINDOWS_ADDRLIB_DIR=/src/amd/addrlib)"); + return 77; +} +#else +#include + +#include "addrinterface.h" + +namespace { +VOID* ADDR_API alloc_sys_mem(const ADDR_ALLOCSYSMEM_INPUT* in) { return std::malloc(in->sizeInBytes); } +ADDR_E_RETURNCODE ADDR_API free_sys_mem(const ADDR_FREESYSMEM_INPUT* in) +{ + std::free(in->pVirtAddr); + return ADDR_OK; +} +} // namespace + +// Every power-of-two shape and mip count the helper accepts, for one format of each bit depth, against +// Addr2ComputeSurfaceInfo with ADDR_SW_4KB_S on Mesa's "navi21" (gfx10.3) configuration, the one +// xg_swizzle_select_check uses: family 0x8F, revision 0x28, GB_ADDR_CONFIG 0x444. +int addrlib_check() +{ + ADDR_CREATE_INPUT create{}; + ADDR_CREATE_OUTPUT created{}; + create.size = sizeof create; + created.size = sizeof created; + create.chipEngine = 0x0000000D; // CIASICIDGFXENGINE_ARCTICISLAND + create.chipFamily = 0x8F; + create.chipRevision = 0x28; + create.callbacks.allocSysMem = alloc_sys_mem; + create.callbacks.freeSysMem = free_sys_mem; + create.regValue.gbAddrConfig = 0x40 | 4u | (4u << 8); + if (AddrCreate(&create, &created) != ADDR_OK) return 1; + struct { unsigned dxgi; AddrFormat addr; const char* name; } formats[] = { + {71, ADDR_FMT_BC1, "BC1"}, {98, ADDR_FMT_BC7, "BC7"}, {61, ADDR_FMT_8, "R8"}, {54, ADDR_FMT_16, "R16"}, + {28, ADDR_FMT_8_8_8_8, "R8G8B8A8"}, {11, ADDR_FMT_16_16_16_16, "R16G16B16A16"}, {2, ADDR_FMT_32_32_32_32, "R32G32B32A32"}}; + unsigned compared = 0, mismatches = 0; + for (const auto& f : formats) + for (unsigned w = 1; w <= 4096; w <<= 1) + for (unsigned h = 1; h <= 4096; h <<= 1) + for (unsigned mips = 1; mips <= 13; ++mips) { + D3D12_RESOURCE_ALLOCATION_INFO info = desktop; + if (!x2w_startup::small_texture_4k(texture(f.dxgi, w, h, mips), info)) continue; + ADDR2_COMPUTE_SURFACE_INFO_INPUT in{}; + ADDR2_COMPUTE_SURFACE_INFO_OUTPUT out{}; + in.size = sizeof in; + out.size = sizeof out; + in.flags.texture = 1; + in.swizzleMode = ADDR_SW_4KB_S; + in.resourceType = ADDR_RSRC_TEX_2D; + in.format = f.addr; + in.width = w; + in.height = h; + in.numSlices = 1; + in.numMipLevels = mips; + in.numSamples = 1; + in.numFrags = 1; + ++compared; + if (Addr2ComputeSurfaceInfo(created.hLib, &in, &out) != ADDR_OK || out.surfSize != info.SizeInBytes || + out.baseAlign != info.Alignment) { + if (++mismatches <= 12) + std::printf("FAIL %s %ux%u %u mips: helper 0x%llx at 0x%llx, addrlib 0x%llx at 0x%x\n", f.name, w, h, mips, + static_cast(info.SizeInBytes), + static_cast(info.Alignment), + static_cast(out.surfSize), out.baseAlign); + } + } + AddrDestroy(created.hLib); + std::printf("startup_small_texture_4k --addrlib: %u shapes compared, %u mismatches (Mesa 25.1.0 addrlib, SW_4KB_S)\n", compared, + mismatches); + return compared && !mismatches ? 0 : 1; +} +#endif + +int main(int argc, char** argv) +{ + if (argc > 1 && std::strcmp(argv[1], "--addrlib") == 0) return addrlib_check(); + const bool mutate = argc > 2 && std::strcmp(argv[1], "--mutate") == 0 && std::strcmp(argv[2], "whole-width-tail") == 0; + if (argc > 1 && !mutate) return 2; + return literal(mutate); +} diff --git a/tests/startup_small_texture_4k_device_check.cpp b/tests/startup_small_texture_4k_device_check.cpp new file mode 100644 index 0000000000..dfc988847c --- /dev/null +++ b/tests/startup_small_texture_4k_device_check.cpp @@ -0,0 +1,102 @@ +// Device check for src/startup_stubs/small_texture_4k.h: both allocation-info entries of the startup device +// wrapper (slots 25 and 108) give the 4 KiB answer for a small 2D texture asked with a 4 KiB alignment and +// the desktop answer otherwise. usage: startup_small_texture_4k_device_check warp|hardware +#include "xmem_test_device.h" +#include "../src/startup_stubs/small_texture_4k.h" + +#include + +extern "C" __declspec(dllimport) HRESULT WINAPI D3D12XboxCreateDevice(IUnknown*, const void*, REFIID, void**); + +namespace { +HRESULT create_wrapper(IUnknown* adapter, REFIID iid, void** out) +{ + unsigned char parameters[64]{}; + return D3D12XboxCreateDevice(adapter, parameters, iid, out); +} + +using Info25 = void* (*)(void*, D3D12_RESOURCE_ALLOCATION_INFO*, UINT, UINT, const D3D12_RESOURCE_DESC*); +using Info108 = void* (*)(void*, D3D12_RESOURCE_ALLOCATION_INFO*, UINT, UINT, const D3D12_RESOURCE_DESC*, D3D12_RESOURCE_ALLOCATION_INFO1*); + +template +F slot(ID3D12Device* device, int index) +{ + return reinterpret_cast((*reinterpret_cast(device))[index]); +} + +D3D12_RESOURCE_DESC texture(DXGI_FORMAT format, UINT width, UINT height, UINT16 mips, UINT64 alignment, D3D12_RESOURCE_FLAGS flags) +{ + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + desc.Alignment = alignment; + desc.Width = width; + desc.Height = height; + desc.DepthOrArraySize = 1; + desc.MipLevels = mips; + desc.Format = format; + desc.SampleDesc.Count = 1; + desc.Flags = flags; + return desc; +} + +// expect_4k: the wrapper must give the helper's 4 KiB answer; otherwise the desktop answer for the same +// description at the default alignment, which is what the wrapper forwards. +int check(const char* name, ID3D12Device* desktop, ID3D12Device* wrapper, const D3D12_RESOURCE_DESC& desc, bool expect_4k) +{ + D3D12_RESOURCE_DESC as_default = desc; + as_default.Alignment = 0; + D3D12_RESOURCE_ALLOCATION_INFO expected = desktop->GetResourceAllocationInfo(0, 1, &as_default); + if (x2w_startup::small_texture_4k(desc, expected) != expect_4k) { + std::printf("FAIL %s: the helper %s the description\n", name, expect_4k ? "refused" : "accepted"); + return 1; + } + D3D12_RESOURCE_ALLOCATION_INFO by25{1, 1}, by108{1, 1}; + D3D12_RESOURCE_ALLOCATION_INFO1 each{1, 1, 1}; + slot(wrapper, 25)(wrapper, &by25, 0, 1, &desc); + slot(wrapper, 108)(wrapper, &by108, 0, 1, &desc, &each); + std::printf("%s: slot 25 size 0x%llx alignment 0x%llx, slot 108 size 0x%llx alignment 0x%llx, expected 0x%llx at 0x%llx\n", name, + static_cast(by25.SizeInBytes), static_cast(by25.Alignment), + static_cast(by108.SizeInBytes), static_cast(by108.Alignment), + static_cast(expected.SizeInBytes), static_cast(expected.Alignment)); + const auto same = [&](const D3D12_RESOURCE_ALLOCATION_INFO& a) { + return a.SizeInBytes == expected.SizeInBytes && a.Alignment == expected.Alignment; + }; + if (!same(by25) || !same(by108)) { + std::printf("FAIL %s: slot 25 or slot 108 differs from the expected answer\n", name); + return 1; + } + if (expect_4k && (each.Offset != 0 || each.SizeInBytes != expected.SizeInBytes || each.Alignment != 0x1000)) { + std::printf("FAIL %s: slot 108 per-resource entry does not describe the 4 KiB answer\n", name); + return 1; + } + return 0; +} +} // namespace + +int main(int argc, char** argv) +{ + if (argc != 2) return 1; + const bool warp = std::strcmp(argv[1], "warp") == 0; + bool unmatched = false; + auto desktop = create_device(warp, unmatched); + if (!desktop) return warp || unmatched ? 1 : 77; + SetEnvironmentVariableW(L"X2W_D3D12_PERMISSIVE", L"1"); + auto wrapper = create_device(warp, unmatched, create_wrapper); + if (!wrapper) return 1; + + const auto none = D3D12_RESOURCE_FLAG_NONE; + int failures = 0; + failures += check("32x16 R8G8B8A8", desktop.Get(), wrapper.Get(), texture(DXGI_FORMAT_R8G8B8A8_UNORM, 32, 16, 1, 0x1000, none), true); + failures += check("128x4 R16G16B16A16", desktop.Get(), wrapper.Get(), + texture(DXGI_FORMAT_R16G16B16A16_UNORM, 128, 4, 1, 0x1000, none), true); + failures += check("128x128 BC1 6 mips", desktop.Get(), wrapper.Get(), texture(DXGI_FORMAT_BC1_UNORM, 128, 128, 6, 0x1000, none), true); + // Controls: the desktop answer stays. + failures += check("control alignment 0", desktop.Get(), wrapper.Get(), texture(DXGI_FORMAT_R8G8B8A8_UNORM, 32, 16, 1, 0, none), false); + failures += check("control render target", desktop.Get(), wrapper.Get(), + texture(DXGI_FORMAT_R8G8B8A8_UNORM, 32, 16, 1, 0x1000, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET), false); + failures += check("control 256x128 R8G8B8A8", desktop.Get(), wrapper.Get(), + texture(DXGI_FORMAT_R8G8B8A8_UNORM, 256, 128, 1, 0x1000, none), false); + if (failures) return 1; + std::printf("startup_small_texture_4k_device: PASS (3 descriptions answered with 4 KiB, 3 controls unchanged)\n"); + return 0; +} From 4d3c3199edd31d8dddd45950116e28e01b6ca685 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 00:19:29 +0100 Subject: [PATCH 2/5] startup: keep upload and XG accesses within the 4 KiB allocation answer A texture given the 4 KiB answer has only that many bytes of executable memory, while its desktop linear footprint can be larger (32x32 R8G8B8A8: 0x1f80). Texture upload now reads no further than the answer and zero-fills the rest of the footprint, XG slot 8 writes no rows past it, and XG slot 2 reports the answer's size and 4 KiB alignment. small_texture_4k_extent() applies the same shape test at alignment 0 or 0x1000. The helper also leaves depth with stencil (R32G8X24, R24G8: two planes) and widths or heights over 16384 alone, and keeps a desktop failure, as console_plane_size does. Tests: every format value's bits per texel against a literal DXGI table, the new refusals, the extent and span limits, and an XG slot 8 copy into exactly the answered size before a no-access guard page, with an explicit 64 KiB alignment as the negative control. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 9 + docs/startup-runtime.md | 24 +- src/startup_stubs/d3d12_draw.inl | 19 +- src/startup_stubs/small_texture_4k.h | 64 ++- src/startup_stubs/xg_xs.cpp | 38 +- tests/startup_small_texture_4k_check.cpp | 454 ++++++++++++--------- tests/startup_small_texture_4k_xg_check.py | 75 ++++ 7 files changed, 446 insertions(+), 237 deletions(-) create mode 100644 tests/startup_small_texture_4k_xg_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b65ba33a0..3ce879a936 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2178,6 +2178,15 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter} $<$:${STARTUP_FXC}>) set_tests_properties(startup_gfx2hlsl_shift64_${adapter} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77) endforeach() + # XG slots 2 and 8 for a texture given the 4 KiB allocation answer (src/startup_stubs/small_texture_4k.h): + # the copy must stay in front of a guard page; the mutated run is not limited and must fail. + add_test(NAME startup_small_texture_4k_xg COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_small_texture_4k_xg_check.py" "$") + add_test(NAME startup_small_texture_4k_xg_no_limit_control COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_small_texture_4k_xg_check.py" "$" --mutate no-limit) + set_tests_properties(startup_small_texture_4k_xg startup_small_texture_4k_xg_no_limit_control + PROPERTIES TIMEOUT 30 ENVIRONMENT "X2W_D3D12_PERMISSIVE=1") + set_tests_properties(startup_small_texture_4k_xg_no_limit_control PROPERTIES WILL_FAIL TRUE) add_test(NAME startup_xg_bc_tiling COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_xg_bc_tiling_check.py" "$") set_tests_properties(startup_xg_bc_tiling PROPERTIES TIMEOUT 30 ENVIRONMENT "X2W_D3D12_PERMISSIVE=1") diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index f05cd427d9..ed1be24c16 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1325,10 +1325,23 @@ substitute: on the adapter measured it answered 0x2000 for a 128 x 4 R16G16B16A1 4 KiB layout takes 0x4000. Scope: a 2D texture with layout UNKNOWN, `Alignment` 0x1000, one sample, one array slice, power-of-two -width and height, `MipLevels` 0 (full chain) up to the full chain, no render-target or depth flag, a -format of 4, 8, 16, 32, 64 or 128 bits a texel (`small_texture_4k_bits`), and the executable's -0x80007-bit bound. Any other description, arrays, other layouts and 1D or 3D textures included, keeps -the desktop answer. +width and height up to 16384, `MipLevels` 0 (full chain) up to the full chain, no render-target or +depth flag, a single-plane format of 4, 8, 16, 32, 64 or 128 bits a texel (`small_texture_4k_bits`; +depth with stencil is not answered), the executable's 0x80007-bit bound, and a desktop answer that is +not a failure. Any other description, arrays, other layouts and 1D or 3D textures included, keeps the +desktop answer. + +The executable's memory for such a texture is then only the answered size. The desktop linear footprint +can be larger: a 32 x 32 R8G8B8A8 texture takes 0x1f80 bytes at a 256-byte row pitch. So the stand-in's +own accesses stop at the answer for a description of that shape at alignment 0 or 0x1000 +(`small_texture_4k_extent`): +- texture upload (`upload_texture`) reads no further, and fills the rest of the footprint with zeros; +- XG slot 8 writes no rows past it; +- XG slot 2 reports the same size at 4 KiB alignment, though its mip records still describe the + linear footprint. + +Texel layout inside the answer is unchanged by this: these paths still treat the memory as the linear +footprint, not as the console's 4 KiB swizzle. Measured on one console executable under the startup debugger: 18 such reads failed during first-run setup in every run, each with the executable's check firing once. With this answer both @@ -1343,6 +1356,9 @@ places in 4 KiB blocks starts exactly the size above after the previous one. `ADDR_SW_4KB_S` size (Mesa 25.1.0, `tools/fetch_addrlib.py`); it skips (77) without addrlib. - `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_device_' -V` goes through the wrapper: slots 25 and 108 must give the 4 KiB answer, and controls the desktop answer. +- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_xg' -V` checks XG slot 2's size + and alignment, and a slot 8 copy into exactly the answered size in front of a no-access guard page. The + `_no_limit_control` run asks at an explicit 64 KiB alignment, which is not limited, and must fail. ## Physical pages in shared sections diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 4d5c972125..be241945d9 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -731,7 +731,12 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) } if (tiled && !tiled_chain && (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; + // A texture given the 4 KiB allocation answer has only that much executable memory behind it; the linear + // footprint can be larger (small_texture_4k.h), and nothing past the answer is read. + const u64 extent = original && !tiled ? x2w_startup::small_texture_4k_extent(original->console) : 0; + const u64 linear_total = stride * (slices - 1) + slice_total; + const u64 total = tiled_chain ? x2w_startup::bc64::Chain(original->console).bytes() : tiled ? surface.size() + : extent && extent < linear_total ? extent : linear_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 @@ -782,11 +787,16 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) for (UINT i = 0; i < plan.count; ++i) { const auto& fp = plan.placed[i]; const u64 bytes = plan.bytes(i); + const u64 at = stride * s + fp.Offset; + const u64 inside = x2w_startup::small_texture_4k_within(at, bytes, extent); u64 offset = 0; - if (!ring_copy(reinterpret_cast(memory) + stride * s + fp.Offset, static_cast(bytes), &offset)) { + auto* const rows = reserve_upload_ring(static_cast(bytes), &offset); + if (!rows) { state.done = false; return; } + std::memcpy(rows, memory + at, static_cast(inside)); + if (inside < bytes) std::memset(rows + inside, 0, static_cast(bytes - inside)); plan.upload(list, texture, upload_ring, mips, slices, s, i, offset); } } @@ -798,8 +808,9 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) InterlockedExchangeAdd(&texture_bytes_uploaded, static_cast(total >> 10)); static LONG shown; if (first_calls(shown, 12)) - log("x2w-draw texture upload %p %llux%u x%u mips %u format %d from %p (%llu KiB)%s", static_cast(texture), desc.Width, - desc.Height, slices, mips, desc.Format, static_cast(memory), total >> 10, announced ? " (announced write)" : ""); + log("x2w-draw texture upload %p %llux%u x%u mips %u format %d from %p (%llu KiB)%s%s", static_cast(texture), desc.Width, + desc.Height, slices, mips, desc.Format, static_cast(memory), total >> 10, announced ? " (announced write)" : "", + extent && extent < linear_total ? " (limited to the 4 KiB allocation answer)" : ""); } // Per-draw dumps (X2W_DRAW_DUMP_FRAME=, X2W_DRAW_DUMP_DIR=): during that frame, render diff --git a/src/startup_stubs/small_texture_4k.h b/src/startup_stubs/small_texture_4k.h index 64eb0de1da..dc8400c3c2 100644 --- a/src/startup_stubs/small_texture_4k.h +++ b/src/startup_stubs/small_texture_4k.h @@ -10,10 +10,15 @@ // block's height while at most 8 levels remain; that level and every smaller one share one block. // // Scope, any other description keeps the desktop answer: a 2D texture with layout UNKNOWN, Alignment -// 0x1000, one sample, one array slice, power-of-two width and height (so block-compressed mip sizes are the -// same in texels and in blocks), MipLevels 0 (full chain) to the full chain, no render-target or depth -// flag, a format whose bits per texel are 4, 8, 16, 32, 64 or 128 (small_texture_4k_bits), and the top -// level within the executable's 0x80007-bit bound. +// 0x1000, one sample, one array slice, power-of-two width and height up to 16384 (so block-compressed mip +// sizes are the same in texels and in blocks), MipLevels 0 (full chain) to the full chain, no render-target +// or depth flag, a single-plane format whose bits per texel are 4, 8, 16, 32, 64 or 128 +// (small_texture_4k_bits), the top level within the executable's 0x80007-bit bound, and a desktop answer +// that is not a failure. +// +// Once a description gets this answer, the executable's memory for it is that many bytes. The stand-in's +// own reads and writes of that memory (texture upload, XG copies) are limited to it with +// small_texture_4k_extent() and small_texture_4k_within(). #pragma once #include @@ -40,13 +45,14 @@ inline Block block(unsigned bits) inline bool power_of_two(std::uint64_t v) { return v && !(v & (v - 1)); } } // namespace small_texture_4k_detail -// Bits per texel of the formats answered; 0 for every other format. +// Bits per texel of the single-plane formats answered; 0 for every other format (depth with stencil in +// the R32G8X24 and R24G8 families has two planes on the console and is not answered here). inline unsigned small_texture_4k_bits(unsigned format) { const unsigned f = format; if (f >= 1 && f <= 4) return 128; // R32G32B32A32 - if (f >= 9 && f <= 22) return 64; // R16G16B16A16, R32G32, R32G8X24 - if ((f >= 23 && f <= 47) || f == 67 || (f >= 87 && f <= 93)) return 32; // R10G10B10A2 .. R24G8, R9G9B9E5, B8G8R8A8 + if (f >= 9 && f <= 18) return 64; // R16G16B16A16, R32G32 + if ((f >= 23 && f <= 43) || f == 67 || (f >= 87 && f <= 93)) return 32; // R10G10B10A2 .. R32, R9G9B9E5, B8G8R8A8 if ((f >= 48 && f <= 59) || f == 85 || f == 86 || f == 115) return 16; // R8G8, R16, B5G6R5, B5G5R5A1, B4G4R4A4 if (f >= 60 && f <= 65) return 8; // R8, A8 if ((f >= 70 && f <= 72) || (f >= 79 && f <= 81)) return 4; // BC1, BC4 @@ -67,26 +73,50 @@ inline std::uint64_t small_texture_4k_bytes(std::uint64_t width, std::uint64_t h return blocks * 0x1000; } -// Replaces info with the console answer and returns true for a description in scope; false leaves it. -inline bool small_texture_4k(const D3D12_RESOURCE_DESC& d, D3D12_RESOURCE_ALLOCATION_INFO& info) +// Bytes of the 4 KiB answer for a description in scope apart from its Alignment field; 0 otherwise. +inline std::uint64_t small_texture_4k_shape(const D3D12_RESOURCE_DESC& d) { using namespace small_texture_4k_detail; - if (d.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || d.Alignment != 0x1000 || d.Layout != D3D12_TEXTURE_LAYOUT_UNKNOWN || - d.SampleDesc.Count != 1 || d.DepthOrArraySize != 1 || !power_of_two(d.Width) || !power_of_two(d.Height)) - return false; - if (d.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) return false; + if (d.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || d.Layout != D3D12_TEXTURE_LAYOUT_UNKNOWN || d.SampleDesc.Count != 1 || + d.DepthOrArraySize != 1 || d.Width > 16384 || d.Height > 16384 || !power_of_two(d.Width) || !power_of_two(d.Height)) + return 0; + if (d.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) return 0; const unsigned bits = small_texture_4k_bits(d.Format); const Block b = block(bits); - if (!b.width) return false; + if (!b.width) return 0; unsigned full = 0; for (std::uint64_t e = d.Width > d.Height ? d.Width : d.Height; e; e >>= 1) ++full; const unsigned mips = d.MipLevels ? d.MipLevels : full; - if (mips > full) return false; + if (mips > full) return 0; // The executable's bound: the top level padded to whole blocks, in bits, at most 0x80007. const std::uint64_t padded = (d.Width + b.width - 1) / b.width * b.width * ((d.Height + b.height - 1) / b.height * b.height); - if (padded * bits > 0x80007) return false; - info = {small_texture_4k_bytes(d.Width, d.Height, mips, bits), 0x1000}; + if (padded * bits > 0x80007) return 0; + return small_texture_4k_bytes(d.Width, d.Height, mips, bits); +} + +// Replaces info with the console answer and returns true for a description in scope; false leaves it. +inline bool small_texture_4k(const D3D12_RESOURCE_DESC& d, D3D12_RESOURCE_ALLOCATION_INFO& info) +{ + if (d.Alignment != 0x1000 || info.SizeInBytes == ~0ull) return false; + const std::uint64_t bytes = small_texture_4k_shape(d); + if (!bytes) return false; + info = {bytes, 0x1000}; return true; } +// Bytes of executable memory behind a texture of this description, when the allocation-info entries give it +// the 4 KiB answer: Alignment 0x1000 or unspecified (0); 0 when the description is not limited. +inline std::uint64_t small_texture_4k_extent(const D3D12_RESOURCE_DESC& d) +{ + return d.Alignment == 0x1000 || d.Alignment == 0 ? small_texture_4k_shape(d) : 0; +} + +// Bytes of [offset, offset + bytes) that lie inside the first extent bytes; extent 0 means no limit. +inline std::uint64_t small_texture_4k_within(std::uint64_t offset, std::uint64_t bytes, std::uint64_t extent) +{ + if (!extent) return bytes; + if (offset >= extent) return 0; + return bytes < extent - offset ? bytes : extent - offset; +} + } // namespace x2w_startup diff --git a/src/startup_stubs/xg_xs.cpp b/src/startup_stubs/xg_xs.cpp index 581fc022df..96207ad140 100644 --- a/src/startup_stubs/xg_xs.cpp +++ b/src/startup_stubs/xg_xs.cpp @@ -12,6 +12,7 @@ #include #include "startup_stubs/bc64_chain.h" #include "startup_stubs/tile64_plan.h" +#include "startup_stubs/small_texture_4k.h" #include #include #include @@ -172,18 +173,22 @@ long __stdcall computer_layout(Computer* self, unsigned char* layout) UINT64 slice_total = plan.size; const bool valid = slice_total != ~0ull && slice_total != 0; if (!valid) slice_total = 0x10000; - const u64 stride = (slice_total + 0xFFFF) & ~u64{0xFFFF}; + // A description the allocation-info entries answer with 4 KiB gets the same size and alignment here + // (small_texture_4k.h); its mip records still describe the desktop linear footprint. + const u64 extent = x2w_startup::small_texture_4k_extent(self->desc); + const u64 stride = extent ? extent : (slice_total + 0xFFFF) & ~u64{0xFFFF}; const u64 total = stride * slices; + const u64 alignment = extent ? 0x1000 : 0x10000; put64(layout + 0x00, total); - put64(layout + 0x08, 0x10000); + put64(layout + 0x08, alignment); put32(layout + 0x10, mips); put32(layout + 0x14, plan.planes); for (unsigned p = 0; p < plan.planes; ++p) { unsigned char* plane = layout + layout_header + p * plane_bytes; put64(plane + 0x08, total); put64(plane + 0x10, slices > 1 ? stride : 0); - put64(plane + 0x18, 0x10000); + put64(plane + 0x18, alignment); for (unsigned m = 0; valid && m < mips; ++m) { const unsigned i = p * mips + m; unsigned char* mip = plane + mip_base + m * mip_bytes; @@ -213,9 +218,9 @@ long __stdcall computer_layout(Computer* self, unsigned char* layout) used += std::snprintf(sizes + used, sizeof sizes - used, " %llx@%llx", u64{footprints[i].Footprint.RowPitch} * rows[i] * footprints[i].Footprint.Depth, footprints[i].Offset); x2w_local::logf("x2w-xg TextureComputer::slot2(layout) dim=%d %llux%ux%u mips=%u fmt=%d(->%d) layout=0x%x -> %s " - "slice=0x%llx total=0x%llx mip0 pitch=%u mips:%s", self->desc.Dimension, self->desc.Width, self->desc.Height, + "slice=0x%llx total=0x%llx align=0x%llx mip0 pitch=%u mips:%s", self->desc.Dimension, self->desc.Width, self->desc.Height, self->desc.DepthOrArraySize, self->desc.MipLevels, self->desc.Format, desc.Format, self->desc.Layout, - valid ? "ok" : "DESKTOP REJECTED THE DESC", stride, total, valid ? footprints[0].Footprint.RowPitch : 0, sizes); + valid ? "ok" : "DESKTOP REJECTED THE DESC", stride, total, alignment, valid ? footprints[0].Footprint.RowPitch : 0, sizes); } return 0; } @@ -322,24 +327,31 @@ long __stdcall computer_copy_into(Computer* self, unsigned char* base, unsigned static_cast(base), plane, subresource, static_cast(linear), row_pitch, slice_pitch); return static_cast(0x80070057); } + // A description given the 4 KiB allocation answer has only that much memory at base: rows past it are + // not written (small_texture_4k.h). + const u64 extent = x2w_startup::small_texture_4k_extent(self->desc); const u64 stride = (slice_total + 0xFFFF) & ~u64{0xFFFF}; const auto& fp = footprints[i].Footprint; const u64 used = row_bytes[i] < row_pitch ? row_bytes[i] : row_pitch; - unsigned char* target = base + stride * slice + footprints[i].Offset; - const u64 target_bytes = plan.bytes(i); + const u64 start = stride * slice + footprints[i].Offset; + unsigned char* target = base + start; + const u64 target_bytes = x2w_startup::small_texture_4k_within(start, plan.bytes(i), extent); const u64 source_slice = slice_pitch ? slice_pitch : u64{row_pitch} * rows[i]; - const bool ok = x2w_local::writable(target, target_bytes) && + const bool ok = (!target_bytes || x2w_local::writable(target, target_bytes)) && x2w_local::readable(linear, source_slice * (fp.Depth - 1) + u64{row_pitch} * (rows[i] - 1) + used); if (ok) { for (unsigned z = 0; z < fp.Depth; ++z) - for (unsigned y = 0; y < rows[i]; ++y) - std::memcpy(target + (u64{z} * rows[i] + y) * fp.RowPitch, linear + z * source_slice + u64{y} * row_pitch, used); - notify_texture_write(base, stride * slices); + for (unsigned y = 0; y < rows[i]; ++y) { + const u64 row = (u64{z} * rows[i] + y) * fp.RowPitch; + const u64 inside = x2w_startup::small_texture_4k_within(start + row, used, extent); + if (inside) std::memcpy(target + row, linear + z * source_slice + u64{y} * row_pitch, inside); + } + notify_texture_write(base, extent ? extent : stride * slices); } if (x2w_local::first_calls(calls, 12) || !ok) x2w_local::logf("x2w-xg TextureComputer::slot8(copy into) base=%p subresource=%u (mip %u slice %u) %ux%u fmt=%d rows=%u " - "pitch %u->%u -> %s", static_cast(base), subresource, mip, slice, fp.Width, fp.Height, desc.Format, - rows[i], row_pitch, fp.RowPitch, ok ? "copied" : "MEMORY NOT ACCESSIBLE"); + "pitch %u->%u extent=0x%llx -> %s", static_cast(base), subresource, mip, slice, fp.Width, fp.Height, desc.Format, + rows[i], row_pitch, fp.RowPitch, extent, ok ? "copied" : "MEMORY NOT ACCESSIBLE"); return ok ? 0 : static_cast(0x80004005); } diff --git a/tests/startup_small_texture_4k_check.cpp b/tests/startup_small_texture_4k_check.cpp index 0c12c74d66..6776df4b8e 100644 --- a/tests/startup_small_texture_4k_check.cpp +++ b/tests/startup_small_texture_4k_check.cpp @@ -1,199 +1,255 @@ -// Checks src/startup_stubs/small_texture_4k.h without a device: the 4 KiB answers for small 2D textures -// asked with a 4 KiB alignment, and the descriptions it leaves alone. -// -// startup_small_texture_4k_check literal sizes and refused descriptions -// startup_small_texture_4k_check --mutate whole-width-tail expectations of a rule whose mip tail is a -// whole block wide (negative control: fails) -// startup_small_texture_4k_check --addrlib every accepted shape against AMD addrlib's -// GFX10 4 KiB layout; 77 when built without it -// -// The literal sizes are worked by hand from the 4 KiB blocks (128x64 texels at 4 bits, 64x64 at 8, 64x32 at -// 16, 32x32 at 32, 32x16 at 64, 16x16 at 128) and the tail rule; the --addrlib run reproduces all of them. -// addrlib: Mesa 25.1.0 (5c142e46f3f6e752ed745fd48912ebb8fad67145) src/amd/addrlib, MIT, fetched by -// tools/fetch_addrlib.py; nothing is vendored. -#include "startup_stubs/small_texture_4k.h" - -#include -#include - -namespace { -int failures; - -D3D12_RESOURCE_DESC texture(unsigned format, unsigned width, unsigned height, unsigned mips = 1, UINT64 alignment = 0x1000) -{ - D3D12_RESOURCE_DESC desc{}; - desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; - desc.Alignment = alignment; - desc.Width = width; - desc.Height = height; - desc.DepthOrArraySize = 1; - desc.MipLevels = static_cast(mips); - desc.Format = static_cast(format); - desc.SampleDesc.Count = 1; - return desc; -} - -const D3D12_RESOURCE_ALLOCATION_INFO desktop{0x10000, 0x10000}; - -void answered(const char* name, const D3D12_RESOURCE_DESC& desc, unsigned long long expected) -{ - D3D12_RESOURCE_ALLOCATION_INFO info = desktop; - const bool changed = x2w_startup::small_texture_4k(desc, info); - if (!changed || info.SizeInBytes != expected || info.Alignment != 0x1000) { - std::printf("FAIL %s: changed %d size 0x%llx alignment 0x%llx, expected 0x%llx at 0x1000\n", name, changed, - static_cast(info.SizeInBytes), static_cast(info.Alignment), expected); - ++failures; - } -} - -void kept(const char* name, const D3D12_RESOURCE_DESC& desc) -{ - D3D12_RESOURCE_ALLOCATION_INFO info = desktop; - if (x2w_startup::small_texture_4k(desc, info) || info.SizeInBytes != desktop.SizeInBytes || info.Alignment != desktop.Alignment) { - std::printf("FAIL %s: the desktop answer was changed to 0x%llx at 0x%llx\n", name, - static_cast(info.SizeInBytes), static_cast(info.Alignment)); - ++failures; - } -} - -int literal(bool whole_width_tail) -{ - // whole_width_tail: what a tail one block wide (instead of half) would answer. - const auto rule = [whole_width_tail](unsigned long long real, unsigned long long mutated) { return whole_width_tail ? mutated : real; }; - answered("32x16 R8G8B8A8", texture(28, 32, 16), 0x1000); - answered("128x4 R16G16B16A16 (desktop at 4 KiB answers 0x2000)", texture(11, 128, 4), 0x4000); - answered("4x4 BC1", texture(71, 4, 4), 0x1000); - answered("128x128 R16 float", texture(54, 128, 128), 0x8000); - answered("256x256 BC7, the 64 KiB bound", texture(98, 256, 256), 0x10000); - answered("16x16 R32G32B32A32 float", texture(2, 16, 16), 0x1000); - answered("128x128 BC1 6 mips: 2 blocks + tail", texture(71, 128, 128, 6), 0x3000); - answered("64x128 BC1 5 mips: 2 blocks + tail", texture(71, 64, 128, 5), 0x3000); - answered("64x64 BC7 5 mips: 1 block + tail", texture(98, 64, 64, 5), rule(0x2000, 0x1000)); - answered("128x64 BC7 5 mips: 2 + 1 + tail", texture(98, 128, 64, 5), rule(0x4000, 0x3000)); - answered("256x64 BC1 5 mips: 2 + 1 + tail", texture(71, 256, 64, 5), rule(0x4000, 0x3000)); - answered("128x32 BC1 4 mips: 1 + tail", texture(71, 128, 32, 4), rule(0x2000, 0x1000)); - answered("128x128 R8G8B8A8 8 mips: 16 + 4 + 1 + tail", texture(28, 128, 128, 8), rule(0x16000, 0x15000)); - answered("1024x1 R8 full chain (MipLevels 0): 16+8+4+2+1 + tail", texture(61, 1024, 1, 0), 0x20000); - - // Refused: the desktop answer stays. - kept("alignment 0", texture(28, 32, 16, 1, 0)); - kept("alignment 64 KiB", texture(28, 32, 16, 1, 0x10000)); - D3D12_RESOURCE_DESC d = texture(28, 32, 16); - d.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; - kept("render target", d); - d = texture(40, 32, 16); - d.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; - kept("depth target", d); - kept("256x128 R8G8B8A8, over the 64 KiB bound", texture(28, 256, 128)); - kept("512x256 BC7, over the bound", texture(98, 512, 256)); - d = texture(28, 32, 16); - d.DepthOrArraySize = 2; - kept("array of 2", d); - kept("48x16 R8G8B8A8, width not a power of two", texture(28, 48, 16)); - kept("R32G32B32 float, 96 bits", texture(6, 32, 16)); - kept("32x16 with 7 mips, more than the chain", texture(28, 32, 16, 7)); - d = texture(28, 32, 16); - d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE; - kept("64 KiB layout", d); - d.Layout = static_cast(0x102); - kept("console layout code", d); - d = texture(28, 32, 16); - d.SampleDesc.Count = 4; - kept("4 samples", d); - d = texture(28, 32, 16); - d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D; - kept("3D texture", d); - if (failures) { - std::printf("startup_small_texture_4k: %d failure(s)\n", failures); - return 1; - } - std::printf("startup_small_texture_4k: PASS (14 sizes, 14 refused descriptions)\n"); - return 0; -} -} // namespace - -#ifndef XBOX2WINDOWS_HAVE_ADDRLIB -int addrlib_check() -{ - std::puts("startup_small_texture_4k --addrlib: SKIP (built without addrlib; run tools/fetch_addrlib.py and configure with " - "-DXBOX2WINDOWS_ADDRLIB_DIR=/src/amd/addrlib)"); - return 77; -} -#else -#include - -#include "addrinterface.h" - -namespace { -VOID* ADDR_API alloc_sys_mem(const ADDR_ALLOCSYSMEM_INPUT* in) { return std::malloc(in->sizeInBytes); } -ADDR_E_RETURNCODE ADDR_API free_sys_mem(const ADDR_FREESYSMEM_INPUT* in) -{ - std::free(in->pVirtAddr); - return ADDR_OK; -} -} // namespace - -// Every power-of-two shape and mip count the helper accepts, for one format of each bit depth, against -// Addr2ComputeSurfaceInfo with ADDR_SW_4KB_S on Mesa's "navi21" (gfx10.3) configuration, the one -// xg_swizzle_select_check uses: family 0x8F, revision 0x28, GB_ADDR_CONFIG 0x444. -int addrlib_check() -{ - ADDR_CREATE_INPUT create{}; - ADDR_CREATE_OUTPUT created{}; - create.size = sizeof create; - created.size = sizeof created; - create.chipEngine = 0x0000000D; // CIASICIDGFXENGINE_ARCTICISLAND - create.chipFamily = 0x8F; - create.chipRevision = 0x28; - create.callbacks.allocSysMem = alloc_sys_mem; - create.callbacks.freeSysMem = free_sys_mem; - create.regValue.gbAddrConfig = 0x40 | 4u | (4u << 8); - if (AddrCreate(&create, &created) != ADDR_OK) return 1; - struct { unsigned dxgi; AddrFormat addr; const char* name; } formats[] = { - {71, ADDR_FMT_BC1, "BC1"}, {98, ADDR_FMT_BC7, "BC7"}, {61, ADDR_FMT_8, "R8"}, {54, ADDR_FMT_16, "R16"}, - {28, ADDR_FMT_8_8_8_8, "R8G8B8A8"}, {11, ADDR_FMT_16_16_16_16, "R16G16B16A16"}, {2, ADDR_FMT_32_32_32_32, "R32G32B32A32"}}; - unsigned compared = 0, mismatches = 0; - for (const auto& f : formats) - for (unsigned w = 1; w <= 4096; w <<= 1) - for (unsigned h = 1; h <= 4096; h <<= 1) - for (unsigned mips = 1; mips <= 13; ++mips) { - D3D12_RESOURCE_ALLOCATION_INFO info = desktop; - if (!x2w_startup::small_texture_4k(texture(f.dxgi, w, h, mips), info)) continue; - ADDR2_COMPUTE_SURFACE_INFO_INPUT in{}; - ADDR2_COMPUTE_SURFACE_INFO_OUTPUT out{}; - in.size = sizeof in; - out.size = sizeof out; - in.flags.texture = 1; - in.swizzleMode = ADDR_SW_4KB_S; - in.resourceType = ADDR_RSRC_TEX_2D; - in.format = f.addr; - in.width = w; - in.height = h; - in.numSlices = 1; - in.numMipLevels = mips; - in.numSamples = 1; - in.numFrags = 1; - ++compared; - if (Addr2ComputeSurfaceInfo(created.hLib, &in, &out) != ADDR_OK || out.surfSize != info.SizeInBytes || - out.baseAlign != info.Alignment) { - if (++mismatches <= 12) - std::printf("FAIL %s %ux%u %u mips: helper 0x%llx at 0x%llx, addrlib 0x%llx at 0x%x\n", f.name, w, h, mips, - static_cast(info.SizeInBytes), - static_cast(info.Alignment), - static_cast(out.surfSize), out.baseAlign); - } - } - AddrDestroy(created.hLib); - std::printf("startup_small_texture_4k --addrlib: %u shapes compared, %u mismatches (Mesa 25.1.0 addrlib, SW_4KB_S)\n", compared, - mismatches); - return compared && !mismatches ? 0 : 1; -} -#endif - -int main(int argc, char** argv) -{ - if (argc > 1 && std::strcmp(argv[1], "--addrlib") == 0) return addrlib_check(); - const bool mutate = argc > 2 && std::strcmp(argv[1], "--mutate") == 0 && std::strcmp(argv[2], "whole-width-tail") == 0; - if (argc > 1 && !mutate) return 2; - return literal(mutate); -} +// Checks src/startup_stubs/small_texture_4k.h without a device: the 4 KiB answers for small 2D textures +// asked with a 4 KiB alignment, and the descriptions it leaves alone. +// +// startup_small_texture_4k_check literal sizes and refused descriptions +// startup_small_texture_4k_check --mutate whole-width-tail expectations of a rule whose mip tail is a +// whole block wide (negative control: fails) +// startup_small_texture_4k_check --addrlib every accepted shape against AMD addrlib's +// GFX10 4 KiB layout; 77 when built without it +// +// The literal sizes are worked by hand from the 4 KiB blocks (128x64 texels at 4 bits, 64x64 at 8, 64x32 at +// 16, 32x32 at 32, 32x16 at 64, 16x16 at 128) and the tail rule; the --addrlib run reproduces all of them. +// addrlib: Mesa 25.1.0 (5c142e46f3f6e752ed745fd48912ebb8fad67145) src/amd/addrlib, MIT, fetched by +// tools/fetch_addrlib.py; nothing is vendored. +#include "startup_stubs/small_texture_4k.h" + +#include +#include + +namespace { +int failures; + +D3D12_RESOURCE_DESC texture(unsigned format, unsigned width, unsigned height, unsigned mips = 1, UINT64 alignment = 0x1000) +{ + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + desc.Alignment = alignment; + desc.Width = width; + desc.Height = height; + desc.DepthOrArraySize = 1; + desc.MipLevels = static_cast(mips); + desc.Format = static_cast(format); + desc.SampleDesc.Count = 1; + return desc; +} + +const D3D12_RESOURCE_ALLOCATION_INFO desktop{0x10000, 0x10000}; + +void answered(const char* name, const D3D12_RESOURCE_DESC& desc, unsigned long long expected) +{ + D3D12_RESOURCE_ALLOCATION_INFO info = desktop; + const bool changed = x2w_startup::small_texture_4k(desc, info); + if (!changed || info.SizeInBytes != expected || info.Alignment != 0x1000) { + std::printf("FAIL %s: changed %d size 0x%llx alignment 0x%llx, expected 0x%llx at 0x1000\n", name, changed, + static_cast(info.SizeInBytes), static_cast(info.Alignment), expected); + ++failures; + } +} + +void kept(const char* name, const D3D12_RESOURCE_DESC& desc) +{ + D3D12_RESOURCE_ALLOCATION_INFO info = desktop; + if (x2w_startup::small_texture_4k(desc, info) || info.SizeInBytes != desktop.SizeInBytes || info.Alignment != desktop.Alignment) { + std::printf("FAIL %s: the desktop answer was changed to 0x%llx at 0x%llx\n", name, + static_cast(info.SizeInBytes), static_cast(info.Alignment)); + ++failures; + } +} + +int literal(bool whole_width_tail) +{ + // whole_width_tail: what a tail one block wide (instead of half) would answer. + const auto rule = [whole_width_tail](unsigned long long real, unsigned long long mutated) { return whole_width_tail ? mutated : real; }; + answered("32x16 R8G8B8A8", texture(28, 32, 16), 0x1000); + answered("128x4 R16G16B16A16 (desktop at 4 KiB answers 0x2000)", texture(11, 128, 4), 0x4000); + answered("4x4 BC1", texture(71, 4, 4), 0x1000); + answered("128x128 R16 float", texture(54, 128, 128), 0x8000); + answered("256x256 BC7, the 64 KiB bound", texture(98, 256, 256), 0x10000); + answered("16x16 R32G32B32A32 float", texture(2, 16, 16), 0x1000); + answered("128x128 BC1 6 mips: 2 blocks + tail", texture(71, 128, 128, 6), 0x3000); + answered("64x128 BC1 5 mips: 2 blocks + tail", texture(71, 64, 128, 5), 0x3000); + answered("64x64 BC7 5 mips: 1 block + tail", texture(98, 64, 64, 5), rule(0x2000, 0x1000)); + answered("128x64 BC7 5 mips: 2 + 1 + tail", texture(98, 128, 64, 5), rule(0x4000, 0x3000)); + answered("256x64 BC1 5 mips: 2 + 1 + tail", texture(71, 256, 64, 5), rule(0x4000, 0x3000)); + answered("128x32 BC1 4 mips: 1 + tail", texture(71, 128, 32, 4), rule(0x2000, 0x1000)); + answered("128x128 R8G8B8A8 8 mips: 16 + 4 + 1 + tail", texture(28, 128, 128, 8), rule(0x16000, 0x15000)); + answered("1024x1 R8 full chain (MipLevels 0): 16+8+4+2+1 + tail", texture(61, 1024, 1, 0), 0x20000); + + // Refused: the desktop answer stays. + kept("alignment 0", texture(28, 32, 16, 1, 0)); + kept("alignment 64 KiB", texture(28, 32, 16, 1, 0x10000)); + D3D12_RESOURCE_DESC d = texture(28, 32, 16); + d.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + kept("render target", d); + d = texture(40, 32, 16); + d.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + kept("depth target", d); + kept("256x128 R8G8B8A8, over the 64 KiB bound", texture(28, 256, 128)); + kept("512x256 BC7, over the bound", texture(98, 512, 256)); + d = texture(28, 32, 16); + d.DepthOrArraySize = 2; + kept("array of 2", d); + kept("48x16 R8G8B8A8, width not a power of two", texture(28, 48, 16)); + kept("R32G32B32 float, 96 bits", texture(6, 32, 16)); + kept("32x16 with 7 mips, more than the chain", texture(28, 32, 16, 7)); + d = texture(28, 32, 16); + d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE; + kept("64 KiB layout", d); + d.Layout = static_cast(0x102); + kept("console layout code", d); + d = texture(28, 32, 16); + d.SampleDesc.Count = 4; + kept("4 samples", d); + d = texture(28, 32, 16); + d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D; + kept("3D texture", d); + kept("depth with stencil R32G8X24 (two planes)", texture(20, 32, 16)); + kept("depth with stencil R24G8 (two planes)", texture(45, 32, 16)); + d = texture(61, 1, 1); + d.Width = 1ull << 40; + d.Height = 1u << 24; + kept("2^40 x 2^24 R8, past the dimension limit", d); + kept("32768x1 R8, past 16384", texture(61, 32768, 1)); + { + D3D12_RESOURCE_ALLOCATION_INFO failed{~0ull, 0}; + if (x2w_startup::small_texture_4k(texture(71, 2, 2), failed) || failed.SizeInBytes != ~0ull) { + std::printf("FAIL 2x2 BC1: a desktop failure was replaced \n"); + ++failures; + } + } + + // Bits per texel for every format value, listed from the DXGI format table (not from the helper's ranges). + const unsigned bits128[] = {1, 2, 3, 4}; + const unsigned bits64[] = {9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; + const unsigned bits32[] = {23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, + 67, 87, 88, 89, 90, 91, 92, 93}; + const unsigned bits16[] = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 85, 86, 115}; + const unsigned bits8[] = {60, 61, 62, 63, 64, 65, 73, 74, 75, 76, 77, 78, 82, 83, 84, 94, 95, 96, 97, 98, 99}; + const unsigned bits4[] = {70, 71, 72, 79, 80, 81}; + unsigned expected_bits[256] = {}; + for (unsigned f : bits128) expected_bits[f] = 128; + for (unsigned f : bits64) expected_bits[f] = 64; + for (unsigned f : bits32) expected_bits[f] = 32; + for (unsigned f : bits16) expected_bits[f] = 16; + for (unsigned f : bits8) expected_bits[f] = 8; + for (unsigned f : bits4) expected_bits[f] = 4; + for (unsigned f = 0; f < 256; ++f) + if (x2w_startup::small_texture_4k_bits(f) != expected_bits[f]) { + std::printf("FAIL format %u: %u bits per texel, expected %u \n", f, x2w_startup::small_texture_4k_bits(f), expected_bits[f]); + ++failures; + } + + // The limit the stand-in's own reads and writes use: the 4 KiB answer whatever Alignment 0 or 0x1000 says. + const auto extent_is = [](const char* name, const D3D12_RESOURCE_DESC& desc, unsigned long long expected) { + const unsigned long long got = x2w_startup::small_texture_4k_extent(desc); + if (got != expected) { + std::printf("FAIL extent %s: 0x%llx, expected 0x%llx \n", name, got, expected); + ++failures; + } + }; + extent_is("32x32 R8G8B8A8 at alignment 0", texture(28, 32, 32, 1, 0), 0x1000); + extent_is("32x32 R8G8B8A8 at 4 KiB", texture(28, 32, 32), 0x1000); + extent_is("32x32 R8G8B8A8 at 64 KiB", texture(28, 32, 32, 1, 0x10000), 0); + extent_is("256x128 R8G8B8A8, over the bound", texture(28, 256, 128, 1, 0), 0); + struct { unsigned long long offset, bytes, extent, inside; } spans[] = { + {0, 0x1f80, 0x1000, 0x1000}, {0xf00, 0x80, 0x1000, 0x80}, {0xf80, 0x100, 0x1000, 0x80}, + {0x1000, 0x80, 0x1000, 0}, {0x2000, 0x80, 0x1000, 0}, {0x1800, 0x80, 0, 0x80}}; + for (const auto& c : spans) + if (x2w_startup::small_texture_4k_within(c.offset, c.bytes, c.extent) != c.inside) { + std::printf("FAIL within(0x%llx, 0x%llx, 0x%llx) != 0x%llx \n", c.offset, c.bytes, c.extent, c.inside); + ++failures; + } + if (failures) { + std::printf("startup_small_texture_4k: %d failure(s)\n", failures); + return 1; + } + std::printf("startup_small_texture_4k: PASS (14 sizes, 19 refused descriptions, 256 format values, extent and span limits)\n"); + return 0; +} +} // namespace + +#ifndef XBOX2WINDOWS_HAVE_ADDRLIB +int addrlib_check() +{ + std::puts("startup_small_texture_4k --addrlib: SKIP (built without addrlib; run tools/fetch_addrlib.py and configure with " + "-DXBOX2WINDOWS_ADDRLIB_DIR=/src/amd/addrlib)"); + return 77; +} +#else +#include + +#include "addrinterface.h" + +namespace { +VOID* ADDR_API alloc_sys_mem(const ADDR_ALLOCSYSMEM_INPUT* in) { return std::malloc(in->sizeInBytes); } +ADDR_E_RETURNCODE ADDR_API free_sys_mem(const ADDR_FREESYSMEM_INPUT* in) +{ + std::free(in->pVirtAddr); + return ADDR_OK; +} +} // namespace + +// Every power-of-two shape and mip count the helper accepts, for one format of each bit depth, against +// Addr2ComputeSurfaceInfo with ADDR_SW_4KB_S on Mesa's "navi21" (gfx10.3) configuration, the one +// xg_swizzle_select_check uses: family 0x8F, revision 0x28, GB_ADDR_CONFIG 0x444. +int addrlib_check() +{ + ADDR_CREATE_INPUT create{}; + ADDR_CREATE_OUTPUT created{}; + create.size = sizeof create; + created.size = sizeof created; + create.chipEngine = 0x0000000D; // CIASICIDGFXENGINE_ARCTICISLAND + create.chipFamily = 0x8F; + create.chipRevision = 0x28; + create.callbacks.allocSysMem = alloc_sys_mem; + create.callbacks.freeSysMem = free_sys_mem; + create.regValue.gbAddrConfig = 0x40 | 4u | (4u << 8); + if (AddrCreate(&create, &created) != ADDR_OK) return 1; + struct { unsigned dxgi; AddrFormat addr; const char* name; } formats[] = { + {71, ADDR_FMT_BC1, "BC1"}, {98, ADDR_FMT_BC7, "BC7"}, {61, ADDR_FMT_8, "R8"}, {54, ADDR_FMT_16, "R16"}, + {28, ADDR_FMT_8_8_8_8, "R8G8B8A8"}, {11, ADDR_FMT_16_16_16_16, "R16G16B16A16"}, {2, ADDR_FMT_32_32_32_32, "R32G32B32A32"}}; + unsigned compared = 0, mismatches = 0; + for (const auto& f : formats) + for (unsigned w = 1; w <= 4096; w <<= 1) + for (unsigned h = 1; h <= 4096; h <<= 1) + for (unsigned mips = 1; mips <= 13; ++mips) { + D3D12_RESOURCE_ALLOCATION_INFO info = desktop; + if (!x2w_startup::small_texture_4k(texture(f.dxgi, w, h, mips), info)) continue; + ADDR2_COMPUTE_SURFACE_INFO_INPUT in{}; + ADDR2_COMPUTE_SURFACE_INFO_OUTPUT out{}; + in.size = sizeof in; + out.size = sizeof out; + in.flags.texture = 1; + in.swizzleMode = ADDR_SW_4KB_S; + in.resourceType = ADDR_RSRC_TEX_2D; + in.format = f.addr; + in.width = w; + in.height = h; + in.numSlices = 1; + in.numMipLevels = mips; + in.numSamples = 1; + in.numFrags = 1; + ++compared; + if (Addr2ComputeSurfaceInfo(created.hLib, &in, &out) != ADDR_OK || out.surfSize != info.SizeInBytes || + out.baseAlign != info.Alignment) { + if (++mismatches <= 12) + std::printf("FAIL %s %ux%u %u mips: helper 0x%llx at 0x%llx, addrlib 0x%llx at 0x%x\n", f.name, w, h, mips, + static_cast(info.SizeInBytes), + static_cast(info.Alignment), + static_cast(out.surfSize), out.baseAlign); + } + } + AddrDestroy(created.hLib); + std::printf("startup_small_texture_4k --addrlib: %u shapes compared, %u mismatches (Mesa 25.1.0 addrlib, SW_4KB_S)\n", compared, + mismatches); + return compared && !mismatches ? 0 : 1; +} +#endif + +int main(int argc, char** argv) +{ + if (argc > 1 && std::strcmp(argv[1], "--addrlib") == 0) return addrlib_check(); + const bool mutate = argc > 2 && std::strcmp(argv[1], "--mutate") == 0 && std::strcmp(argv[2], "whole-width-tail") == 0; + if (argc > 1 && !mutate) return 2; + return literal(mutate); +} diff --git a/tests/startup_small_texture_4k_xg_check.py b/tests/startup_small_texture_4k_xg_check.py new file mode 100644 index 0000000000..371a4e7016 --- /dev/null +++ b/tests/startup_small_texture_4k_xg_check.py @@ -0,0 +1,75 @@ +"""XG slots 2 and 8 for a small texture given the 4 KiB allocation answer (src/startup_stubs/small_texture_4k.h). + +A 32x32 R8G8B8A8 texture with layout UNKNOWN gets 0x1000 bytes at 4 KiB alignment from the allocation-info +entries, while its desktop linear footprint is 0x1f80 bytes (32 rows at a 256-byte pitch). Slot 2 must +report 0x1000 at 0x1000, and slot 8 must copy into a 0x1000-byte allocation followed by a no-access guard +page without touching the guard: rows inside the allocation are written, rows past it are not. + +usage: startup_small_texture_4k_xg_check.py [--mutate no-limit] +--mutate no-limit asks with an explicit 64 KiB alignment, which the 4 KiB answer does not cover: slot 8 then +needs the whole footprint, cannot write it in front of the guard page, and the check fails (negative control). +""" +import ctypes as ct +import struct +import sys + +from startup_xg_bc7_layout_check import COPY_INTO, DESC, HRESULT, LAYOUT, LAYOUT_BYTES, PTR, RELEASE + +MEM_COMMIT, MEM_RESERVE, MEM_RELEASE = 0x1000, 0x2000, 0x8000 +PAGE_NOACCESS, PAGE_READWRITE = 0x01, 0x04 +WIDTH = HEIGHT = 32 +ROW_BYTES, DESKTOP_PITCH = WIDTH * 4, 256 + + +def main(path, mutate): + kernel = ct.WinDLL("kernel32", use_last_error=True) + kernel.VirtualAlloc.restype = PTR + kernel.VirtualAlloc.argtypes = (PTR, ct.c_size_t, ct.c_uint32, ct.c_uint32) + kernel.VirtualProtect.argtypes = (PTR, ct.c_size_t, ct.c_uint32, ct.POINTER(ct.c_uint32)) + kernel.VirtualFree.argtypes = (PTR, ct.c_size_t, ct.c_uint32) + create = ct.WinDLL(path).XGCreateTextureComputer + create.argtypes = (PTR, ct.POINTER(PTR), ct.c_uint64, ct.c_uint64) + create.restype = HRESULT + + alignment = 0x10000 if mutate else 0 + desc = ct.create_string_buffer(DESC.pack(3, alignment, WIDTH, HEIGHT, 1, 1, 28, 1, 0, 0, 0)) + computer = PTR() + assert create(desc, ct.byref(computer), 0, 0) == 0 and computer + slots = ct.cast(computer, ct.POINTER(ct.POINTER(PTR)))[0] + block = None + try: + layout = ct.create_string_buffer(LAYOUT_BYTES) + assert LAYOUT(slots[2])(computer, layout) == 0 + total, align = struct.unpack_from(" 1 and not mutate): + sys.exit("usage: startup_small_texture_4k_xg_check.py [--mutate no-limit]") + main(args[0], mutate) From 1fa1adb5b3d7cba8bfcae014c595b6a9a95937e3 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 00:40:21 +0100 Subject: [PATCH 3/5] startup: limit 4 KiB-answer accesses to Alignment 0x1000 descriptions small_texture_4k_extent() also took Alignment 0, which the allocation-info entries answer with the desktop size: a 32x32 R8G8B8A8 texture at alignment 0 then got a 0x1000 size from XG slot 2, half its rows from slot 8 and a zero-filled bottom half on upload. The limit now applies only where the 4 KiB answer was given. The XG check covers both: a copy before a guard page at 0x1000, and alignment 0 unchanged (slot 2 0x10000, all 32 rows written). Slot 2's mip records still describing the linear footprint is stated as a limit. Co-Authored-By: Claude Opus 5.5 --- docs/startup-runtime.md | 21 +++--- src/startup_stubs/small_texture_4k.h | 7 +- src/startup_stubs/xg_xs.cpp | 3 +- tests/startup_small_texture_4k_check.cpp | 6 +- tests/startup_small_texture_4k_xg_check.py | 77 ++++++++++++---------- 5 files changed, 65 insertions(+), 49 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 4f5e5e9464..4bfe6dcf61 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1411,16 +1411,17 @@ not a failure. Any other description, arrays, other layouts and 1D or 3D texture desktop answer. The executable's memory for such a texture is then only the answered size. The desktop linear footprint -can be larger: a 32 x 32 R8G8B8A8 texture takes 0x1f80 bytes at a 256-byte row pitch. So the stand-in's -own accesses stop at the answer for a description of that shape at alignment 0 or 0x1000 -(`small_texture_4k_extent`): +can be larger: a 32 x 32 R8G8B8A8 texture takes 0x1f80 bytes at a 256-byte row pitch. So for a description +that got the 4 KiB answer (in scope, `Alignment` 0x1000; `small_texture_4k_extent`) the stand-in's own +accesses stop at the answer. At alignment 0 the desktop answer and the full footprint are unchanged: - texture upload (`upload_texture`) reads no further, and fills the rest of the footprint with zeros; - XG slot 8 writes no rows past it; -- XG slot 2 reports the same size at 4 KiB alignment, though its mip records still describe the - linear footprint. +- XG slot 2 reports the same size at 4 KiB alignment. Limit: its mip records still describe the + linear footprint, which can reach past that size (32 x 32 R8G8B8A8: mip 0 at 0x2000 bytes, pitch 256). Texel layout inside the answer is unchanged by this: these paths still treat the memory as the linear -footprint, not as the console's 4 KiB swizzle. +footprint, not as the console's 4 KiB swizzle. A tightly packed linear layout for limited descriptions, +or the 4 KiB swizzle itself, would let the whole texture fit; neither is part of this change. Measured on one console executable under the startup debugger: 18 such reads failed during first-run setup in every run, each with the executable's check firing once. With this answer both @@ -1435,9 +1436,11 @@ places in 4 KiB blocks starts exactly the size above after the previous one. `ADDR_SW_4KB_S` size (Mesa 25.1.0, `tools/fetch_addrlib.py`); it skips (77) without addrlib. - `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_device_' -V` goes through the wrapper: slots 25 and 108 must give the 4 KiB answer, and controls the desktop answer. -- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_xg' -V` checks XG slot 2's size - and alignment, and a slot 8 copy into exactly the answered size in front of a no-access guard page. The - `_no_limit_control` run asks at an explicit 64 KiB alignment, which is not limited, and must fail. +- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_xg' -V` checks XG slots 2 and 8 + at `Alignment` 0x1000: size and alignment, then a copy into exactly the answered size in front of a + no-access guard page. At alignment 0 it checks that slot 2 still reports 0x10000 and slot 8 still + writes all 32 rows. The `_no_limit_control` run asks at an explicit 64 KiB alignment, which is not + limited, and must fail. ## Physical pages in shared sections diff --git a/src/startup_stubs/small_texture_4k.h b/src/startup_stubs/small_texture_4k.h index dc8400c3c2..af02539a21 100644 --- a/src/startup_stubs/small_texture_4k.h +++ b/src/startup_stubs/small_texture_4k.h @@ -104,11 +104,12 @@ inline bool small_texture_4k(const D3D12_RESOURCE_DESC& d, D3D12_RESOURCE_ALLOCA return true; } -// Bytes of executable memory behind a texture of this description, when the allocation-info entries give it -// the 4 KiB answer: Alignment 0x1000 or unspecified (0); 0 when the description is not limited. +// Bytes of executable memory behind a texture of this description when the allocation-info entries give it +// the 4 KiB answer (Alignment 0x1000 and in scope); 0 when the description is not limited. Alignment 0 keeps +// the desktop answer, and so the full footprint. inline std::uint64_t small_texture_4k_extent(const D3D12_RESOURCE_DESC& d) { - return d.Alignment == 0x1000 || d.Alignment == 0 ? small_texture_4k_shape(d) : 0; + return d.Alignment == 0x1000 ? small_texture_4k_shape(d) : 0; } // Bytes of [offset, offset + bytes) that lie inside the first extent bytes; extent 0 means no limit. diff --git a/src/startup_stubs/xg_xs.cpp b/src/startup_stubs/xg_xs.cpp index f3a27306e2..f433b64db1 100644 --- a/src/startup_stubs/xg_xs.cpp +++ b/src/startup_stubs/xg_xs.cpp @@ -174,7 +174,8 @@ long __stdcall computer_layout(Computer* self, unsigned char* layout) const bool valid = slice_total != ~0ull && slice_total != 0; if (!valid) slice_total = 0x10000; // A description the allocation-info entries answer with 4 KiB gets the same size and alignment here - // (small_texture_4k.h); its mip records still describe the desktop linear footprint. + // (small_texture_4k.h). Limit: its mip records still describe the desktop linear footprint, which can + // reach past that size; slot 8 writes only the part inside it. const u64 extent = x2w_startup::small_texture_4k_extent(self->desc); const u64 stride = extent ? extent : (slice_total + 0xFFFF) & ~u64{0xFFFF}; const u64 total = stride * slices; diff --git a/tests/startup_small_texture_4k_check.cpp b/tests/startup_small_texture_4k_check.cpp index 6776df4b8e..1c141da918 100644 --- a/tests/startup_small_texture_4k_check.cpp +++ b/tests/startup_small_texture_4k_check.cpp @@ -139,7 +139,7 @@ int literal(bool whole_width_tail) ++failures; } - // The limit the stand-in's own reads and writes use: the 4 KiB answer whatever Alignment 0 or 0x1000 says. + // The limit the stand-in's own reads and writes use: the 4 KiB answer at Alignment 0x1000 only. const auto extent_is = [](const char* name, const D3D12_RESOURCE_DESC& desc, unsigned long long expected) { const unsigned long long got = x2w_startup::small_texture_4k_extent(desc); if (got != expected) { @@ -147,10 +147,10 @@ int literal(bool whole_width_tail) ++failures; } }; - extent_is("32x32 R8G8B8A8 at alignment 0", texture(28, 32, 32, 1, 0), 0x1000); + extent_is("32x32 R8G8B8A8 at alignment 0 (desktop answer kept)", texture(28, 32, 32, 1, 0), 0); extent_is("32x32 R8G8B8A8 at 4 KiB", texture(28, 32, 32), 0x1000); extent_is("32x32 R8G8B8A8 at 64 KiB", texture(28, 32, 32, 1, 0x10000), 0); - extent_is("256x128 R8G8B8A8, over the bound", texture(28, 256, 128, 1, 0), 0); + extent_is("256x128 R8G8B8A8, over the bound", texture(28, 256, 128), 0); struct { unsigned long long offset, bytes, extent, inside; } spans[] = { {0, 0x1f80, 0x1000, 0x1000}, {0xf00, 0x80, 0x1000, 0x80}, {0xf80, 0x100, 0x1000, 0x80}, {0x1000, 0x80, 0x1000, 0}, {0x2000, 0x80, 0x1000, 0}, {0x1800, 0x80, 0, 0x80}}; diff --git a/tests/startup_small_texture_4k_xg_check.py b/tests/startup_small_texture_4k_xg_check.py index 371a4e7016..58af4c4a8c 100644 --- a/tests/startup_small_texture_4k_xg_check.py +++ b/tests/startup_small_texture_4k_xg_check.py @@ -1,13 +1,17 @@ """XG slots 2 and 8 for a small texture given the 4 KiB allocation answer (src/startup_stubs/small_texture_4k.h). -A 32x32 R8G8B8A8 texture with layout UNKNOWN gets 0x1000 bytes at 4 KiB alignment from the allocation-info -entries, while its desktop linear footprint is 0x1f80 bytes (32 rows at a 256-byte pitch). Slot 2 must -report 0x1000 at 0x1000, and slot 8 must copy into a 0x1000-byte allocation followed by a no-access guard -page without touching the guard: rows inside the allocation are written, rows past it are not. +A 32x32 R8G8B8A8 texture with layout UNKNOWN asked at Alignment 0x1000 gets 0x1000 bytes at 4 KiB alignment +from the allocation-info entries, while its desktop linear footprint is 0x1f80 bytes (32 rows at a 256-byte +pitch). Cases: + limited Alignment 0x1000: slot 2 reports 0x1000 at 0x1000, and slot 8 copies into a 0x1000-byte + allocation followed by a no-access guard page: the 16 rows inside are written, the guard is not + touched. + unchanged Alignment 0 (the allocation-info entries keep the desktop answer): slot 2 reports 0x10000 at + 0x10000, and slot 8 writes all 32 rows into a 64 KiB buffer, as before this change. usage: startup_small_texture_4k_xg_check.py [--mutate no-limit] ---mutate no-limit asks with an explicit 64 KiB alignment, which the 4 KiB answer does not cover: slot 8 then -needs the whole footprint, cannot write it in front of the guard page, and the check fails (negative control). +--mutate no-limit runs the limited case at an explicit 64 KiB alignment, which is not limited: slot 8 then needs +the whole footprint in front of the guard page and fails (negative control). """ import ctypes as ct import struct @@ -19,19 +23,16 @@ PAGE_NOACCESS, PAGE_READWRITE = 0x01, 0x04 WIDTH = HEIGHT = 32 ROW_BYTES, DESKTOP_PITCH = WIDTH * 4, 256 +LINEAR = bytes((y * 7 + x) & 0xFF for y in range(HEIGHT) for x in range(ROW_BYTES)) +kernel = ct.WinDLL("kernel32", use_last_error=True) +kernel.VirtualAlloc.restype = PTR +kernel.VirtualAlloc.argtypes = (PTR, ct.c_size_t, ct.c_uint32, ct.c_uint32) +kernel.VirtualProtect.argtypes = (PTR, ct.c_size_t, ct.c_uint32, ct.POINTER(ct.c_uint32)) +kernel.VirtualFree.argtypes = (PTR, ct.c_size_t, ct.c_uint32) -def main(path, mutate): - kernel = ct.WinDLL("kernel32", use_last_error=True) - kernel.VirtualAlloc.restype = PTR - kernel.VirtualAlloc.argtypes = (PTR, ct.c_size_t, ct.c_uint32, ct.c_uint32) - kernel.VirtualProtect.argtypes = (PTR, ct.c_size_t, ct.c_uint32, ct.POINTER(ct.c_uint32)) - kernel.VirtualFree.argtypes = (PTR, ct.c_size_t, ct.c_uint32) - create = ct.WinDLL(path).XGCreateTextureComputer - create.argtypes = (PTR, ct.POINTER(PTR), ct.c_uint64, ct.c_uint64) - create.restype = HRESULT - alignment = 0x10000 if mutate else 0 +def run(create, name, alignment, layout_expected, buffer_bytes, guard, rows_expected): desc = ct.create_string_buffer(DESC.pack(3, alignment, WIDTH, HEIGHT, 1, 1, 28, 1, 0, 0, 0)) computer = PTR() assert create(desc, ct.byref(computer), 0, 0) == 0 and computer @@ -41,32 +42,42 @@ def main(path, mutate): layout = ct.create_string_buffer(LAYOUT_BYTES) assert LAYOUT(slots[2])(computer, layout) == 0 total, align = struct.unpack_from(" Date: Wed, 25 Nov 2026 02:55:30 +0100 Subject: [PATCH 4/5] startup: one compact plan for XG slots 2 and 8 and texture upload of 4 KiB-answer textures The desktop linear footprint of a texture given the 4 KiB allocation answer can exceed it (32x32 R8G8B8A8: 0x1f80 bytes at a 256-byte pitch), so slot 8 wrote only part of the rows and upload filled the rest with zeros. All three calls now use one compact linear plan for such a description: every row at its exact byte width (texels or 4x4 blocks), levels back to back from the top level. Slot 2 reports the answer at 4 KiB alignment with the plan's level records, slot 8 writes every row of a level or nothing (short pitch, level past the last), and upload reads the same rows. The plan ends inside the answer for all 4,943 accepted shapes, checked in the device-free test. addrlib's 8-level tail cap is dropped from the size rule: a 4 KiB thin tail never holds more than 7 levels (checked over every accepted shape), so it never applied. Tests: the XG check writes two mips with distinct rows and per-mip sentinels into exactly the answered size before a guard page and compares bytes; refused copies leave memory unchanged; alignment 0 is unchanged. A new draw check places that texture, draws both mips on WARP and hardware and compares all 1,536 read-back pixels. The test file is LF again with no stray CR. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 6 + docs/startup-runtime.md | 50 +- src/startup_stubs/d3d12_draw.inl | 35 +- src/startup_stubs/small_texture_4k.h | 51 +- src/startup_stubs/xg_xs.cpp | 93 +++- tests/startup_small_texture_4k_check.cpp | 556 ++++++++++--------- tests/startup_small_texture_4k_draw_check.py | 98 ++++ tests/startup_small_texture_4k_xg_check.py | 156 ++++-- tests/startup_stream_consumer.cpp | 5 +- tests/startup_stream_draw.cpp | 44 +- tests/startup_stream_draw.h | 4 + 11 files changed, 736 insertions(+), 362 deletions(-) create mode 100644 tests/startup_small_texture_4k_draw_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 3690d13fa1..fe7f8ec5f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2473,6 +2473,12 @@ if(EXISTS "${INTAKE_PYTHON}") "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_xg_bc7_draw_check.py" "$" "${DXC_EXECUTABLE}" "${backend}") set_tests_properties(startup_xg_bc7_draw_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) + # A two-mip texture given the 4 KiB allocation answer: XG slot 8 into exactly the reported size before a + # guard page, placed, drawn and read back pixel for pixel (src/startup_stubs/small_texture_4k.h). + add_test(NAME startup_small_texture_4k_draw_${backend} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_small_texture_4k_draw_check.py" + "$" "${DXC_EXECUTABLE}" "${backend}") + set_tests_properties(startup_small_texture_4k_draw_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) add_test(NAME startup_slot57_blend_draw_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_slot57_blend_draw_check.py" "$" "${backend}") diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 9dfafb2891..1cecf01461 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1548,8 +1548,8 @@ For a description in scope the stand-in now answers the console size at 4 KiB al (`src/startup_stubs/small_texture_4k.h`): the GFX10 4 KiB thin-block layout as AMD addrlib computes it. Each level is padded to whole blocks (128 x 64 texels at 4 bits a texel, 64 x 64 at 8, 64 x 32 at 16, 32 x 32 at 32, 32 x 16 at 64, 16 x 16 at 128; block-compressed formats count 4 x 4 blocks) until the -first level no wider than half a block and no taller than a block, while at most 8 levels remain. -That level and every smaller one share one block. The first eight uses are logged as +first level no wider than half a block and no taller than a block. That level and every smaller one +share one block. (addrlib also caps a tail at 8 levels; a 4 KiB thin tail never holds more than 7.) The first eight uses are logged as `x2w-d3d12 allocation info: 4 KiB answer ...`. Desktop D3D12 asked with a 4 KiB alignment is not a substitute: on the adapter measured it answered 0x2000 for a 128 x 4 R16G16B16A16 texture, where the 4 KiB layout takes 0x4000. @@ -1562,17 +1562,23 @@ not a failure. Any other description, arrays, other layouts and 1D or 3D texture desktop answer. The executable's memory for such a texture is then only the answered size. The desktop linear footprint -can be larger: a 32 x 32 R8G8B8A8 texture takes 0x1f80 bytes at a 256-byte row pitch. So for a description -that got the 4 KiB answer (in scope, `Alignment` 0x1000; `small_texture_4k_extent`) the stand-in's own -accesses stop at the answer. At alignment 0 the desktop answer and the full footprint are unchanged: -- texture upload (`upload_texture`) reads no further, and fills the rest of the footprint with zeros; -- XG slot 8 writes no rows past it; -- XG slot 2 reports the same size at 4 KiB alignment. Limit: its mip records still describe the - linear footprint, which can reach past that size (32 x 32 R8G8B8A8: mip 0 at 0x2000 bytes, pitch 256). - -Texel layout inside the answer is unchanged by this: these paths still treat the memory as the linear -footprint, not as the console's 4 KiB swizzle. A tightly packed linear layout for limited descriptions, -or the 4 KiB swizzle itself, would let the whole texture fit; neither is part of this change. +(256-byte rows, 512-byte level placement) can be larger: a 32 x 32 R8G8B8A8 texture takes 0x1f80 bytes. +So for a description that got the 4 KiB answer (`Alignment` 0x1000 and in scope) the stand-in uses one +compact linear plan (`small_texture_4k_plan`) in every call that touches that memory: + +- XG slot 2 reports the answered size at 4 KiB alignment and, per level, the plan's offset, row bytes and + size; +- XG slot 8 writes every row of a level there, or nothing: a row pitch shorter than a row or a level past + the last one fails without writing or announcing a write; +- texture upload (`upload_texture`) reads the same rows from there. + +In the plan, every row has its exact byte width (a row of texels, or of 4 x 4 blocks), and the levels +follow each other from the top level with no padding. Its size never exceeds the answer: +`startup_small_texture_4k` checks this for every accepted shape. The texels are linear, not the console's +4 KiB swizzle. Data the executable streams in its own 4 KiB layout is therefore read in the wrong order, +as it already was with the desktop layout. A GFX10 4 KiB tiling plan, such as `Gfx10Texture` in #1469, +can replace the compact plan in all three calls at once. At alignment 0 nothing changes: the desktop +answer and the desktop footprint. Measured on one console executable under the startup debugger: 18 such reads failed during first-run setup in every run, each with the executable's check firing once. With this answer both @@ -1587,11 +1593,19 @@ places in 4 KiB blocks starts exactly the size above after the previous one. `ADDR_SW_4KB_S` size (Mesa 25.1.0, `tools/fetch_addrlib.py`); it skips (77) without addrlib. - `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_device_' -V` goes through the wrapper: slots 25 and 108 must give the 4 KiB answer, and controls the desktop answer. -- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_xg' -V` checks XG slots 2 and 8 - at `Alignment` 0x1000: size and alignment, then a copy into exactly the answered size in front of a - no-access guard page. At alignment 0 it checks that slot 2 still reports 0x10000 and slot 8 still - writes all 32 rows. The `_no_limit_control` run asks at an explicit 64 KiB alignment, which is not - limited, and must fail. +- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_xg' -V` covers a two-mip 32 x 32 + R8G8B8A8 texture at `Alignment` 0x1000: + - slot 2's size, alignment and level records; + - slot 8 into exactly the answered size in front of a no-access guard page, with distinct rows and a + different sentinel per level, checked byte for byte; + - refused copies that leave memory unchanged; + - alignment 0 unchanged: slot 2 reports 0x10000 and slot 8 writes all 32 rows. + + The `_no_limit_control` run asks at an explicit 64 KiB alignment, which gets no compact plan, and must + fail. +- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_draw_' -V` writes the same + texture through slot 8 in front of a guard page, places it (slot 133), draws both levels through the + translated path on WARP and on the hardware adapter, and compares all 48 x 32 read-back pixels. ## Physical pages in shared sections diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 62eac52d4d..7662eeb08d 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -731,12 +731,18 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) } if (tiled && !tiled_chain && (plan.count != 1 || plan.rows[0] != surface.rows || plan.row_bytes[0] != u64{surface.columns} * surface.block_bytes)) return; - // A texture given the 4 KiB allocation answer has only that much executable memory behind it; the linear - // footprint can be larger (small_texture_4k.h), and nothing past the answer is read. - const u64 extent = original && !tiled ? x2w_startup::small_texture_4k_extent(original->console) : 0; - const u64 linear_total = stride * (slices - 1) + slice_total; + // A texture given the 4 KiB allocation answer is read in the compact plan XG describes and writes for it + // (small_texture_4k.h); its rows must be the footprint's rows, level by level. + x2w_startup::SmallTexturePlan compact; + const bool is_compact = original && !tiled && x2w_startup::small_texture_4k_plan(original->console, compact); + if (is_compact) { + bool rows_match = compact.levels == plan.count; + for (UINT i = 0; rows_match && i < plan.count; ++i) + rows_match = compact.level[i].rows == plan.rows[i] && compact.level[i].row_bytes == plan.row_bytes[i]; + if (!rows_match) return; + } const u64 total = tiled_chain ? x2w_startup::bc64::Chain(original->console).bytes() : tiled ? surface.size() - : extent && extent < linear_total ? extent : linear_total; + : is_compact ? compact.bytes : 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 @@ -787,16 +793,21 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) for (UINT i = 0; i < plan.count; ++i) { const auto& fp = plan.placed[i]; const u64 bytes = plan.bytes(i); - const u64 at = stride * s + fp.Offset; - const u64 inside = x2w_startup::small_texture_4k_within(at, bytes, extent); u64 offset = 0; - auto* const rows = reserve_upload_ring(static_cast(bytes), &offset); - if (!rows) { + if (is_compact) { + auto* const rows = reserve_upload_ring(static_cast(bytes), &offset); + if (!rows) { + state.done = false; + return; + } + const auto& level = compact.level[i]; + for (u64 y = 0; y < level.rows; ++y) + std::memcpy(rows + y * fp.Footprint.RowPitch, memory + level.offset + y * level.row_bytes, + static_cast(level.row_bytes)); + } else if (!ring_copy(reinterpret_cast(memory) + stride * s + fp.Offset, static_cast(bytes), &offset)) { state.done = false; return; } - std::memcpy(rows, memory + at, static_cast(inside)); - if (inside < bytes) std::memset(rows + inside, 0, static_cast(bytes - inside)); plan.upload(list, texture, upload_ring, mips, slices, s, i, offset); } } @@ -810,7 +821,7 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) if (first_calls(shown, 12)) log("x2w-draw texture upload %p %llux%u x%u mips %u format %d from %p (%llu KiB)%s%s", static_cast(texture), desc.Width, desc.Height, slices, mips, desc.Format, static_cast(memory), total >> 10, announced ? " (announced write)" : "", - extent && extent < linear_total ? " (limited to the 4 KiB allocation answer)" : ""); + is_compact ? " (compact plan of the 4 KiB allocation answer)" : ""); } // Per-draw dumps (X2W_DRAW_DUMP_FRAME=, X2W_DRAW_DUMP_DIR=): during that frame, render diff --git a/src/startup_stubs/small_texture_4k.h b/src/startup_stubs/small_texture_4k.h index af02539a21..09fd133ff4 100644 --- a/src/startup_stubs/small_texture_4k.h +++ b/src/startup_stubs/small_texture_4k.h @@ -7,7 +7,8 @@ // // The size is the GFX10 4 KiB thin-block layout as AMD addrlib computes it (Gfx10Lib, Mesa 25.1.0): each // level is padded to whole blocks until the first level that fits a tail of half a block's width and a -// block's height while at most 8 levels remain; that level and every smaller one share one block. +// block's height; that level and every smaller one share one block. (addrlib also caps a tail at 8 levels; +// a 4 KiB thin tail never holds more than 7, so the cap never applies.) // // Scope, any other description keeps the desktop answer: a 2D texture with layout UNKNOWN, Alignment // 0x1000, one sample, one array slice, power-of-two width and height up to 16384 (so block-compressed mip @@ -16,9 +17,12 @@ // (small_texture_4k_bits), the top level within the executable's 0x80007-bit bound, and a desktop answer // that is not a failure. // -// Once a description gets this answer, the executable's memory for it is that many bytes. The stand-in's -// own reads and writes of that memory (texture upload, XG copies) are limited to it with -// small_texture_4k_extent() and small_texture_4k_within(). +// Once a description gets this answer, the executable's memory for it is that many bytes, which the desktop +// linear footprint (256-byte row pitch, 512-byte level placement) can exceed. XG slot 2 describes, XG slot 8 +// writes and texture upload reads such a texture in one compact linear plan instead (small_texture_4k_plan): +// every row at its exact byte width, levels back to back from the top level. It always fits inside the +// answer (tests/startup_small_texture_4k_check.cpp checks every accepted shape). The texels are linear, not +// the console's 4 KiB swizzle; a GFX10 4 KiB tiling plan (such as PR 1469's Gfx10Texture) can replace it. #pragma once #include @@ -45,6 +49,12 @@ inline Block block(unsigned bits) inline bool power_of_two(std::uint64_t v) { return v && !(v & (v - 1)); } } // namespace small_texture_4k_detail +// True for the block-compressed formats among those answered (4x4 texel blocks). +inline bool small_texture_4k_compressed(unsigned f) +{ + return (f >= 70 && f <= 84) || (f >= 94 && f <= 99); +} + // Bits per texel of the single-plane formats answered; 0 for every other format (depth with stencil in // the R32G8X24 and R24G8 families has two planes on the console and is not answered here). inline unsigned small_texture_4k_bits(unsigned format) @@ -67,7 +77,7 @@ inline std::uint64_t small_texture_4k_bytes(std::uint64_t width, std::uint64_t h std::uint64_t blocks = 0; for (unsigned m = 0; m < mips; ++m) { const std::uint64_t w = (width >> m) ? (width >> m) : 1, h = (height >> m) ? (height >> m) : 1; - if (mips > 1 && w <= b.width / 2 && h <= b.height && mips - m <= 8) return (blocks + 1) * 0x1000; + if (mips > 1 && w <= b.width / 2 && h <= b.height) return (blocks + 1) * 0x1000; blocks += (w + b.width - 1) / b.width * ((h + b.height - 1) / b.height); } return blocks * 0x1000; @@ -112,12 +122,33 @@ inline std::uint64_t small_texture_4k_extent(const D3D12_RESOURCE_DESC& d) return d.Alignment == 0x1000 ? small_texture_4k_shape(d) : 0; } -// Bytes of [offset, offset + bytes) that lie inside the first extent bytes; extent 0 means no limit. -inline std::uint64_t small_texture_4k_within(std::uint64_t offset, std::uint64_t bytes, std::uint64_t extent) +// The compact linear plan of a description with an extent: per level its offset, row bytes (a row of texels, +// or of 4x4 blocks), row count and bytes; levels follow each other with no padding. +struct SmallTexturePlan { + struct Level { std::uint64_t offset, row_bytes, rows, bytes; }; + unsigned levels = 0; + std::uint64_t bytes = 0; + Level level[15]{}; +}; +inline bool small_texture_4k_plan(const D3D12_RESOURCE_DESC& d, SmallTexturePlan& plan) { - if (!extent) return bytes; - if (offset >= extent) return 0; - return bytes < extent - offset ? bytes : extent - offset; + if (!small_texture_4k_extent(d)) return false; + unsigned full = 0; + for (std::uint64_t e = d.Width > d.Height ? d.Width : d.Height; e; e >>= 1) ++full; + const bool compressed = small_texture_4k_compressed(d.Format); + const unsigned bits = small_texture_4k_bits(d.Format); + plan = {}; + plan.levels = d.MipLevels ? d.MipLevels : full; + for (unsigned m = 0; m < plan.levels; ++m) { + const std::uint64_t w = (d.Width >> m) ? (d.Width >> m) : 1, h = (d.Height >> m) ? (d.Height >> m) : 1; + auto& l = plan.level[m]; + l.offset = plan.bytes; + l.row_bytes = compressed ? (w + 3) / 4 * bits * 2 : w * bits / 8; // a 4x4 block holds 16 texels + l.rows = compressed ? (h + 3) / 4 : h; + l.bytes = l.row_bytes * l.rows; + plan.bytes += l.bytes; + } + return true; } } // namespace x2w_startup diff --git a/src/startup_stubs/xg_xs.cpp b/src/startup_stubs/xg_xs.cpp index f433b64db1..bc308a701a 100644 --- a/src/startup_stubs/xg_xs.cpp +++ b/src/startup_stubs/xg_xs.cpp @@ -152,6 +152,56 @@ HRESULT copy_bc7_4k_s(const D3D12_RESOURCE_DESC& desc, unsigned char* dest, cons return S_OK; } +// A description given the 4 KiB allocation answer (small_texture_4k.h): slot 2 describes the compact plan +// that slot 8 writes and the texture upload reads, inside the answered size at 4 KiB alignment. +void write_compact_layout(const D3D12_RESOURCE_DESC& desc, const x2w_startup::SmallTexturePlan& compact, unsigned char* layout) +{ + const u64 total = x2w_startup::small_texture_4k_extent(desc); + const bool blocks = x2w_startup::small_texture_4k_compressed(desc.Format); + put64(layout + 0x00, total); + put64(layout + 0x08, 0x1000); + put32(layout + 0x10, compact.levels); + put32(layout + 0x14, 1); + unsigned char* plane = layout + layout_header; + put64(plane + 0x08, total); + put64(plane + 0x18, 0x1000); + for (unsigned m = 0; m < compact.levels; ++m) { + const auto& level = compact.level[m]; + const UINT width = static_cast((desc.Width >> m) ? (desc.Width >> m) : 1), height = (desc.Height >> m) ? (desc.Height >> m) : 1; + const UINT padded_width = blocks ? (width + 3) & ~3u : width, padded_height = blocks ? (height + 3) & ~3u : height; + unsigned char* mip = plane + mip_base + m * mip_bytes; + put64(mip + 0x00, level.bytes); + put64(mip + 0x08, level.offset); + put64(mip + 0x10, level.bytes); + put32(mip + 0x18, padded_width); + put32(mip + 0x1c, static_cast(level.row_bytes)); + put32(mip + 0x20, 1); + put32(mip + 0x24, padded_width); + put32(mip + 0x28, padded_height); + put32(mip + 0x2c, 1); + put32(mip + 0x30, width); + put32(mip + 0x34, height); + put32(mip + 0x38, 1); + put32(mip + 0x3c, 1); + } +} + +// Slot 8 for the compact plan: every row of one level, or nothing when the call cannot be served whole. +long copy_compact(const D3D12_RESOURCE_DESC& desc, const x2w_startup::SmallTexturePlan& compact, unsigned char* base, unsigned plane, + unsigned subresource, const unsigned char* linear, unsigned row_pitch) +{ + if (!base || !linear || plane || subresource >= compact.levels) return static_cast(0x80070057); + const auto& level = compact.level[subresource]; + if (row_pitch < level.row_bytes) return static_cast(0x80070057); + if (!x2w_local::writable(base + level.offset, level.bytes) || + !x2w_local::readable(linear, u64{row_pitch} * (level.rows - 1) + level.row_bytes)) + return static_cast(0x80004005); + for (u64 y = 0; y < level.rows; ++y) + std::memcpy(base + level.offset + y * level.row_bytes, linear + y * row_pitch, static_cast(level.row_bytes)); + notify_texture_write(base, x2w_startup::small_texture_4k_extent(desc)); + return 0; +} + long __stdcall computer_layout(Computer* self, unsigned char* layout) { if (!layout) return static_cast(0x80004003); @@ -160,6 +210,10 @@ long __stdcall computer_layout(Computer* self, unsigned char* layout) write_bc7_4k_s_layout(self->desc, layout); return S_OK; } + if (x2w_startup::SmallTexturePlan compact; x2w_startup::small_texture_4k_plan(self->desc, compact)) { + write_compact_layout(self->desc, compact, layout); + return S_OK; + } ID3D12Device* device = layout_device(); if (!device) return static_cast(0x80004005); const D3D12_RESOURCE_DESC desc = desktop_desc(self->desc); @@ -173,13 +227,9 @@ long __stdcall computer_layout(Computer* self, unsigned char* layout) UINT64 slice_total = plan.size; const bool valid = slice_total != ~0ull && slice_total != 0; if (!valid) slice_total = 0x10000; - // A description the allocation-info entries answer with 4 KiB gets the same size and alignment here - // (small_texture_4k.h). Limit: its mip records still describe the desktop linear footprint, which can - // reach past that size; slot 8 writes only the part inside it. - const u64 extent = x2w_startup::small_texture_4k_extent(self->desc); - const u64 stride = extent ? extent : (slice_total + 0xFFFF) & ~u64{0xFFFF}; + const u64 stride = (slice_total + 0xFFFF) & ~u64{0xFFFF}; const u64 total = stride * slices; - const u64 alignment = extent ? 0x1000 : 0x10000; + const u64 alignment = 0x10000; put64(layout + 0x00, total); put64(layout + 0x08, alignment); @@ -308,6 +358,14 @@ long __stdcall computer_copy_into(Computer* self, unsigned char* base, unsigned return hr; } static LONG calls, refused; + if (x2w_startup::SmallTexturePlan compact; x2w_startup::small_texture_4k_plan(self->desc, compact)) { + const long hr = copy_compact(self->desc, compact, base, plane, subresource, linear, row_pitch); + if (x2w_local::first_calls(calls, 12) || hr) + x2w_local::logf("x2w-xg TextureComputer::slot8(copy into) base=%p subresource=%u %llux%u fmt=%d pitch %u compact plan of " + "the 4 KiB answer -> 0x%08lx", static_cast(base), subresource, self->desc.Width, self->desc.Height, + self->desc.Format, row_pitch, static_cast(hr)); + return hr; + } ID3D12Device* device = layout_device(); const D3D12_RESOURCE_DESC desc = desktop_desc(self->desc); const bool volume = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D; @@ -328,31 +386,24 @@ long __stdcall computer_copy_into(Computer* self, unsigned char* base, unsigned static_cast(base), plane, subresource, static_cast(linear), row_pitch, slice_pitch); return static_cast(0x80070057); } - // A description given the 4 KiB allocation answer has only that much memory at base: rows past it are - // not written (small_texture_4k.h). - const u64 extent = x2w_startup::small_texture_4k_extent(self->desc); const u64 stride = (slice_total + 0xFFFF) & ~u64{0xFFFF}; const auto& fp = footprints[i].Footprint; const u64 used = row_bytes[i] < row_pitch ? row_bytes[i] : row_pitch; - const u64 start = stride * slice + footprints[i].Offset; - unsigned char* target = base + start; - const u64 target_bytes = x2w_startup::small_texture_4k_within(start, plan.bytes(i), extent); + unsigned char* target = base + stride * slice + footprints[i].Offset; + const u64 target_bytes = plan.bytes(i); const u64 source_slice = slice_pitch ? slice_pitch : u64{row_pitch} * rows[i]; - const bool ok = (!target_bytes || x2w_local::writable(target, target_bytes)) && + const bool ok = x2w_local::writable(target, target_bytes) && x2w_local::readable(linear, source_slice * (fp.Depth - 1) + u64{row_pitch} * (rows[i] - 1) + used); if (ok) { for (unsigned z = 0; z < fp.Depth; ++z) - for (unsigned y = 0; y < rows[i]; ++y) { - const u64 row = (u64{z} * rows[i] + y) * fp.RowPitch; - const u64 inside = x2w_startup::small_texture_4k_within(start + row, used, extent); - if (inside) std::memcpy(target + row, linear + z * source_slice + u64{y} * row_pitch, inside); - } - notify_texture_write(base, extent ? extent : stride * slices); + for (unsigned y = 0; y < rows[i]; ++y) + std::memcpy(target + (u64{z} * rows[i] + y) * fp.RowPitch, linear + z * source_slice + u64{y} * row_pitch, used); + notify_texture_write(base, stride * slices); } if (x2w_local::first_calls(calls, 12) || !ok) x2w_local::logf("x2w-xg TextureComputer::slot8(copy into) base=%p subresource=%u (mip %u slice %u) %ux%u fmt=%d rows=%u " - "pitch %u->%u extent=0x%llx -> %s", static_cast(base), subresource, mip, slice, fp.Width, fp.Height, desc.Format, - rows[i], row_pitch, fp.RowPitch, extent, ok ? "copied" : "MEMORY NOT ACCESSIBLE"); + "pitch %u->%u -> %s", static_cast(base), subresource, mip, slice, fp.Width, fp.Height, desc.Format, + rows[i], row_pitch, fp.RowPitch, ok ? "copied" : "MEMORY NOT ACCESSIBLE"); return ok ? 0 : static_cast(0x80004005); } diff --git a/tests/startup_small_texture_4k_check.cpp b/tests/startup_small_texture_4k_check.cpp index 1c141da918..c83b2541d3 100644 --- a/tests/startup_small_texture_4k_check.cpp +++ b/tests/startup_small_texture_4k_check.cpp @@ -1,255 +1,301 @@ -// Checks src/startup_stubs/small_texture_4k.h without a device: the 4 KiB answers for small 2D textures -// asked with a 4 KiB alignment, and the descriptions it leaves alone. -// -// startup_small_texture_4k_check literal sizes and refused descriptions -// startup_small_texture_4k_check --mutate whole-width-tail expectations of a rule whose mip tail is a -// whole block wide (negative control: fails) -// startup_small_texture_4k_check --addrlib every accepted shape against AMD addrlib's -// GFX10 4 KiB layout; 77 when built without it -// -// The literal sizes are worked by hand from the 4 KiB blocks (128x64 texels at 4 bits, 64x64 at 8, 64x32 at -// 16, 32x32 at 32, 32x16 at 64, 16x16 at 128) and the tail rule; the --addrlib run reproduces all of them. -// addrlib: Mesa 25.1.0 (5c142e46f3f6e752ed745fd48912ebb8fad67145) src/amd/addrlib, MIT, fetched by -// tools/fetch_addrlib.py; nothing is vendored. -#include "startup_stubs/small_texture_4k.h" - -#include -#include - -namespace { -int failures; - -D3D12_RESOURCE_DESC texture(unsigned format, unsigned width, unsigned height, unsigned mips = 1, UINT64 alignment = 0x1000) -{ - D3D12_RESOURCE_DESC desc{}; - desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; - desc.Alignment = alignment; - desc.Width = width; - desc.Height = height; - desc.DepthOrArraySize = 1; - desc.MipLevels = static_cast(mips); - desc.Format = static_cast(format); - desc.SampleDesc.Count = 1; - return desc; -} - -const D3D12_RESOURCE_ALLOCATION_INFO desktop{0x10000, 0x10000}; - -void answered(const char* name, const D3D12_RESOURCE_DESC& desc, unsigned long long expected) -{ - D3D12_RESOURCE_ALLOCATION_INFO info = desktop; - const bool changed = x2w_startup::small_texture_4k(desc, info); - if (!changed || info.SizeInBytes != expected || info.Alignment != 0x1000) { - std::printf("FAIL %s: changed %d size 0x%llx alignment 0x%llx, expected 0x%llx at 0x1000\n", name, changed, - static_cast(info.SizeInBytes), static_cast(info.Alignment), expected); - ++failures; - } -} - -void kept(const char* name, const D3D12_RESOURCE_DESC& desc) -{ - D3D12_RESOURCE_ALLOCATION_INFO info = desktop; - if (x2w_startup::small_texture_4k(desc, info) || info.SizeInBytes != desktop.SizeInBytes || info.Alignment != desktop.Alignment) { - std::printf("FAIL %s: the desktop answer was changed to 0x%llx at 0x%llx\n", name, - static_cast(info.SizeInBytes), static_cast(info.Alignment)); - ++failures; - } -} - -int literal(bool whole_width_tail) -{ - // whole_width_tail: what a tail one block wide (instead of half) would answer. - const auto rule = [whole_width_tail](unsigned long long real, unsigned long long mutated) { return whole_width_tail ? mutated : real; }; - answered("32x16 R8G8B8A8", texture(28, 32, 16), 0x1000); - answered("128x4 R16G16B16A16 (desktop at 4 KiB answers 0x2000)", texture(11, 128, 4), 0x4000); - answered("4x4 BC1", texture(71, 4, 4), 0x1000); - answered("128x128 R16 float", texture(54, 128, 128), 0x8000); - answered("256x256 BC7, the 64 KiB bound", texture(98, 256, 256), 0x10000); - answered("16x16 R32G32B32A32 float", texture(2, 16, 16), 0x1000); - answered("128x128 BC1 6 mips: 2 blocks + tail", texture(71, 128, 128, 6), 0x3000); - answered("64x128 BC1 5 mips: 2 blocks + tail", texture(71, 64, 128, 5), 0x3000); - answered("64x64 BC7 5 mips: 1 block + tail", texture(98, 64, 64, 5), rule(0x2000, 0x1000)); - answered("128x64 BC7 5 mips: 2 + 1 + tail", texture(98, 128, 64, 5), rule(0x4000, 0x3000)); - answered("256x64 BC1 5 mips: 2 + 1 + tail", texture(71, 256, 64, 5), rule(0x4000, 0x3000)); - answered("128x32 BC1 4 mips: 1 + tail", texture(71, 128, 32, 4), rule(0x2000, 0x1000)); - answered("128x128 R8G8B8A8 8 mips: 16 + 4 + 1 + tail", texture(28, 128, 128, 8), rule(0x16000, 0x15000)); - answered("1024x1 R8 full chain (MipLevels 0): 16+8+4+2+1 + tail", texture(61, 1024, 1, 0), 0x20000); - - // Refused: the desktop answer stays. - kept("alignment 0", texture(28, 32, 16, 1, 0)); - kept("alignment 64 KiB", texture(28, 32, 16, 1, 0x10000)); - D3D12_RESOURCE_DESC d = texture(28, 32, 16); - d.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; - kept("render target", d); - d = texture(40, 32, 16); - d.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; - kept("depth target", d); - kept("256x128 R8G8B8A8, over the 64 KiB bound", texture(28, 256, 128)); - kept("512x256 BC7, over the bound", texture(98, 512, 256)); - d = texture(28, 32, 16); - d.DepthOrArraySize = 2; - kept("array of 2", d); - kept("48x16 R8G8B8A8, width not a power of two", texture(28, 48, 16)); - kept("R32G32B32 float, 96 bits", texture(6, 32, 16)); - kept("32x16 with 7 mips, more than the chain", texture(28, 32, 16, 7)); - d = texture(28, 32, 16); - d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE; - kept("64 KiB layout", d); - d.Layout = static_cast(0x102); - kept("console layout code", d); - d = texture(28, 32, 16); - d.SampleDesc.Count = 4; - kept("4 samples", d); - d = texture(28, 32, 16); - d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D; - kept("3D texture", d); - kept("depth with stencil R32G8X24 (two planes)", texture(20, 32, 16)); - kept("depth with stencil R24G8 (two planes)", texture(45, 32, 16)); - d = texture(61, 1, 1); - d.Width = 1ull << 40; - d.Height = 1u << 24; - kept("2^40 x 2^24 R8, past the dimension limit", d); - kept("32768x1 R8, past 16384", texture(61, 32768, 1)); - { - D3D12_RESOURCE_ALLOCATION_INFO failed{~0ull, 0}; - if (x2w_startup::small_texture_4k(texture(71, 2, 2), failed) || failed.SizeInBytes != ~0ull) { - std::printf("FAIL 2x2 BC1: a desktop failure was replaced \n"); - ++failures; - } - } - - // Bits per texel for every format value, listed from the DXGI format table (not from the helper's ranges). - const unsigned bits128[] = {1, 2, 3, 4}; - const unsigned bits64[] = {9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; - const unsigned bits32[] = {23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, - 67, 87, 88, 89, 90, 91, 92, 93}; - const unsigned bits16[] = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 85, 86, 115}; - const unsigned bits8[] = {60, 61, 62, 63, 64, 65, 73, 74, 75, 76, 77, 78, 82, 83, 84, 94, 95, 96, 97, 98, 99}; - const unsigned bits4[] = {70, 71, 72, 79, 80, 81}; - unsigned expected_bits[256] = {}; - for (unsigned f : bits128) expected_bits[f] = 128; - for (unsigned f : bits64) expected_bits[f] = 64; - for (unsigned f : bits32) expected_bits[f] = 32; - for (unsigned f : bits16) expected_bits[f] = 16; - for (unsigned f : bits8) expected_bits[f] = 8; - for (unsigned f : bits4) expected_bits[f] = 4; - for (unsigned f = 0; f < 256; ++f) - if (x2w_startup::small_texture_4k_bits(f) != expected_bits[f]) { - std::printf("FAIL format %u: %u bits per texel, expected %u \n", f, x2w_startup::small_texture_4k_bits(f), expected_bits[f]); - ++failures; - } - - // The limit the stand-in's own reads and writes use: the 4 KiB answer at Alignment 0x1000 only. - const auto extent_is = [](const char* name, const D3D12_RESOURCE_DESC& desc, unsigned long long expected) { - const unsigned long long got = x2w_startup::small_texture_4k_extent(desc); - if (got != expected) { - std::printf("FAIL extent %s: 0x%llx, expected 0x%llx \n", name, got, expected); - ++failures; - } - }; - extent_is("32x32 R8G8B8A8 at alignment 0 (desktop answer kept)", texture(28, 32, 32, 1, 0), 0); - extent_is("32x32 R8G8B8A8 at 4 KiB", texture(28, 32, 32), 0x1000); - extent_is("32x32 R8G8B8A8 at 64 KiB", texture(28, 32, 32, 1, 0x10000), 0); - extent_is("256x128 R8G8B8A8, over the bound", texture(28, 256, 128), 0); - struct { unsigned long long offset, bytes, extent, inside; } spans[] = { - {0, 0x1f80, 0x1000, 0x1000}, {0xf00, 0x80, 0x1000, 0x80}, {0xf80, 0x100, 0x1000, 0x80}, - {0x1000, 0x80, 0x1000, 0}, {0x2000, 0x80, 0x1000, 0}, {0x1800, 0x80, 0, 0x80}}; - for (const auto& c : spans) - if (x2w_startup::small_texture_4k_within(c.offset, c.bytes, c.extent) != c.inside) { - std::printf("FAIL within(0x%llx, 0x%llx, 0x%llx) != 0x%llx \n", c.offset, c.bytes, c.extent, c.inside); - ++failures; - } - if (failures) { - std::printf("startup_small_texture_4k: %d failure(s)\n", failures); - return 1; - } - std::printf("startup_small_texture_4k: PASS (14 sizes, 19 refused descriptions, 256 format values, extent and span limits)\n"); - return 0; -} -} // namespace - -#ifndef XBOX2WINDOWS_HAVE_ADDRLIB -int addrlib_check() -{ - std::puts("startup_small_texture_4k --addrlib: SKIP (built without addrlib; run tools/fetch_addrlib.py and configure with " - "-DXBOX2WINDOWS_ADDRLIB_DIR=/src/amd/addrlib)"); - return 77; -} -#else -#include - -#include "addrinterface.h" - -namespace { -VOID* ADDR_API alloc_sys_mem(const ADDR_ALLOCSYSMEM_INPUT* in) { return std::malloc(in->sizeInBytes); } -ADDR_E_RETURNCODE ADDR_API free_sys_mem(const ADDR_FREESYSMEM_INPUT* in) -{ - std::free(in->pVirtAddr); - return ADDR_OK; -} -} // namespace - -// Every power-of-two shape and mip count the helper accepts, for one format of each bit depth, against -// Addr2ComputeSurfaceInfo with ADDR_SW_4KB_S on Mesa's "navi21" (gfx10.3) configuration, the one -// xg_swizzle_select_check uses: family 0x8F, revision 0x28, GB_ADDR_CONFIG 0x444. -int addrlib_check() -{ - ADDR_CREATE_INPUT create{}; - ADDR_CREATE_OUTPUT created{}; - create.size = sizeof create; - created.size = sizeof created; - create.chipEngine = 0x0000000D; // CIASICIDGFXENGINE_ARCTICISLAND - create.chipFamily = 0x8F; - create.chipRevision = 0x28; - create.callbacks.allocSysMem = alloc_sys_mem; - create.callbacks.freeSysMem = free_sys_mem; - create.regValue.gbAddrConfig = 0x40 | 4u | (4u << 8); - if (AddrCreate(&create, &created) != ADDR_OK) return 1; - struct { unsigned dxgi; AddrFormat addr; const char* name; } formats[] = { - {71, ADDR_FMT_BC1, "BC1"}, {98, ADDR_FMT_BC7, "BC7"}, {61, ADDR_FMT_8, "R8"}, {54, ADDR_FMT_16, "R16"}, - {28, ADDR_FMT_8_8_8_8, "R8G8B8A8"}, {11, ADDR_FMT_16_16_16_16, "R16G16B16A16"}, {2, ADDR_FMT_32_32_32_32, "R32G32B32A32"}}; - unsigned compared = 0, mismatches = 0; - for (const auto& f : formats) - for (unsigned w = 1; w <= 4096; w <<= 1) - for (unsigned h = 1; h <= 4096; h <<= 1) - for (unsigned mips = 1; mips <= 13; ++mips) { - D3D12_RESOURCE_ALLOCATION_INFO info = desktop; - if (!x2w_startup::small_texture_4k(texture(f.dxgi, w, h, mips), info)) continue; - ADDR2_COMPUTE_SURFACE_INFO_INPUT in{}; - ADDR2_COMPUTE_SURFACE_INFO_OUTPUT out{}; - in.size = sizeof in; - out.size = sizeof out; - in.flags.texture = 1; - in.swizzleMode = ADDR_SW_4KB_S; - in.resourceType = ADDR_RSRC_TEX_2D; - in.format = f.addr; - in.width = w; - in.height = h; - in.numSlices = 1; - in.numMipLevels = mips; - in.numSamples = 1; - in.numFrags = 1; - ++compared; - if (Addr2ComputeSurfaceInfo(created.hLib, &in, &out) != ADDR_OK || out.surfSize != info.SizeInBytes || - out.baseAlign != info.Alignment) { - if (++mismatches <= 12) - std::printf("FAIL %s %ux%u %u mips: helper 0x%llx at 0x%llx, addrlib 0x%llx at 0x%x\n", f.name, w, h, mips, - static_cast(info.SizeInBytes), - static_cast(info.Alignment), - static_cast(out.surfSize), out.baseAlign); - } - } - AddrDestroy(created.hLib); - std::printf("startup_small_texture_4k --addrlib: %u shapes compared, %u mismatches (Mesa 25.1.0 addrlib, SW_4KB_S)\n", compared, - mismatches); - return compared && !mismatches ? 0 : 1; -} -#endif - -int main(int argc, char** argv) -{ - if (argc > 1 && std::strcmp(argv[1], "--addrlib") == 0) return addrlib_check(); - const bool mutate = argc > 2 && std::strcmp(argv[1], "--mutate") == 0 && std::strcmp(argv[2], "whole-width-tail") == 0; - if (argc > 1 && !mutate) return 2; - return literal(mutate); -} +// Checks src/startup_stubs/small_texture_4k.h without a device: the 4 KiB answers for small 2D textures +// asked with a 4 KiB alignment, and the descriptions it leaves alone. +// +// startup_small_texture_4k_check literal sizes and refused descriptions +// startup_small_texture_4k_check --mutate whole-width-tail expectations of a rule whose mip tail is a +// whole block wide (negative control: fails) +// startup_small_texture_4k_check --addrlib every accepted shape against AMD addrlib's +// GFX10 4 KiB layout; 77 when built without it +// +// The literal sizes are worked by hand from the 4 KiB blocks (128x64 texels at 4 bits, 64x64 at 8, 64x32 at +// 16, 32x32 at 32, 32x16 at 64, 16x16 at 128) and the tail rule; the --addrlib run reproduces all of them. +// addrlib: Mesa 25.1.0 (5c142e46f3f6e752ed745fd48912ebb8fad67145) src/amd/addrlib, MIT, fetched by +// tools/fetch_addrlib.py; nothing is vendored. +#include "startup_stubs/small_texture_4k.h" + +#include +#include +#include + +namespace { +int failures; + +D3D12_RESOURCE_DESC texture(unsigned format, unsigned width, unsigned height, unsigned mips = 1, UINT64 alignment = 0x1000) +{ + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + desc.Alignment = alignment; + desc.Width = width; + desc.Height = height; + desc.DepthOrArraySize = 1; + desc.MipLevels = static_cast(mips); + desc.Format = static_cast(format); + desc.SampleDesc.Count = 1; + return desc; +} + +const D3D12_RESOURCE_ALLOCATION_INFO desktop{0x10000, 0x10000}; + +void answered(const char* name, const D3D12_RESOURCE_DESC& desc, unsigned long long expected) +{ + D3D12_RESOURCE_ALLOCATION_INFO info = desktop; + const bool changed = x2w_startup::small_texture_4k(desc, info); + if (!changed || info.SizeInBytes != expected || info.Alignment != 0x1000) { + std::printf("FAIL %s: changed %d size 0x%llx alignment 0x%llx, expected 0x%llx at 0x1000\n", name, changed, + static_cast(info.SizeInBytes), static_cast(info.Alignment), expected); + ++failures; + } +} + +void kept(const char* name, const D3D12_RESOURCE_DESC& desc) +{ + D3D12_RESOURCE_ALLOCATION_INFO info = desktop; + if (x2w_startup::small_texture_4k(desc, info) || info.SizeInBytes != desktop.SizeInBytes || info.Alignment != desktop.Alignment) { + std::printf("FAIL %s: the desktop answer was changed to 0x%llx at 0x%llx\n", name, + static_cast(info.SizeInBytes), static_cast(info.Alignment)); + ++failures; + } +} + +int literal(bool whole_width_tail) +{ + // whole_width_tail: what a tail one block wide (instead of half) would answer. + const auto rule = [whole_width_tail](unsigned long long real, unsigned long long mutated) { return whole_width_tail ? mutated : real; }; + answered("32x16 R8G8B8A8", texture(28, 32, 16), 0x1000); + answered("128x4 R16G16B16A16 (desktop at 4 KiB answers 0x2000)", texture(11, 128, 4), 0x4000); + answered("4x4 BC1", texture(71, 4, 4), 0x1000); + answered("128x128 R16 float", texture(54, 128, 128), 0x8000); + answered("256x256 BC7, the 64 KiB bound", texture(98, 256, 256), 0x10000); + answered("16x16 R32G32B32A32 float", texture(2, 16, 16), 0x1000); + answered("128x128 BC1 6 mips: 2 blocks + tail", texture(71, 128, 128, 6), 0x3000); + answered("64x128 BC1 5 mips: 2 blocks + tail", texture(71, 64, 128, 5), 0x3000); + answered("64x64 BC7 5 mips: 1 block + tail", texture(98, 64, 64, 5), rule(0x2000, 0x1000)); + answered("128x64 BC7 5 mips: 2 + 1 + tail", texture(98, 128, 64, 5), rule(0x4000, 0x3000)); + answered("256x64 BC1 5 mips: 2 + 1 + tail", texture(71, 256, 64, 5), rule(0x4000, 0x3000)); + answered("128x32 BC1 4 mips: 1 + tail", texture(71, 128, 32, 4), rule(0x2000, 0x1000)); + answered("128x128 R8G8B8A8 8 mips: 16 + 4 + 1 + tail", texture(28, 128, 128, 8), rule(0x16000, 0x15000)); + answered("1024x1 R8 full chain (MipLevels 0): 16+8+4+2+1 + tail", texture(61, 1024, 1, 0), 0x20000); + answered("64x64 BC1 full chain: all 7 levels in one tail block", texture(71, 64, 64, 7), 0x1000); + + // Refused: the desktop answer stays. + kept("alignment 0", texture(28, 32, 16, 1, 0)); + kept("alignment 64 KiB", texture(28, 32, 16, 1, 0x10000)); + D3D12_RESOURCE_DESC d = texture(28, 32, 16); + d.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + kept("render target", d); + d = texture(40, 32, 16); + d.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + kept("depth target", d); + kept("256x128 R8G8B8A8, over the 64 KiB bound", texture(28, 256, 128)); + kept("512x256 BC7, over the bound", texture(98, 512, 256)); + d = texture(28, 32, 16); + d.DepthOrArraySize = 2; + kept("array of 2", d); + kept("48x16 R8G8B8A8, width not a power of two", texture(28, 48, 16)); + kept("R32G32B32 float, 96 bits", texture(6, 32, 16)); + kept("32x16 with 7 mips, more than the chain", texture(28, 32, 16, 7)); + d = texture(28, 32, 16); + d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE; + kept("64 KiB layout", d); + d.Layout = static_cast(0x102); + kept("console layout code", d); + d = texture(28, 32, 16); + d.SampleDesc.Count = 4; + kept("4 samples", d); + d = texture(28, 32, 16); + d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D; + kept("3D texture", d); + kept("depth with stencil R32G8X24 (two planes)", texture(20, 32, 16)); + kept("depth with stencil R24G8 (two planes)", texture(45, 32, 16)); + d = texture(61, 1, 1); + d.Width = 1ull << 40; + d.Height = 1u << 24; + kept("2^40 x 2^24 R8, past the dimension limit", d); + kept("32768x1 R8, past 16384", texture(61, 32768, 1)); + { + D3D12_RESOURCE_ALLOCATION_INFO failed{~0ull, 0}; + if (x2w_startup::small_texture_4k(texture(71, 2, 2), failed) || failed.SizeInBytes != ~0ull) { + std::printf("FAIL 2x2 BC1: a desktop failure was replaced\n"); + ++failures; + } + } + + // Bits per texel for every format value, listed from the DXGI format table (not from the helper's ranges). + const unsigned bits128[] = {1, 2, 3, 4}; + const unsigned bits64[] = {9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; + const unsigned bits32[] = {23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, + 67, 87, 88, 89, 90, 91, 92, 93}; + const unsigned bits16[] = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 85, 86, 115}; + const unsigned bits8[] = {60, 61, 62, 63, 64, 65, 73, 74, 75, 76, 77, 78, 82, 83, 84, 94, 95, 96, 97, 98, 99}; + const unsigned bits4[] = {70, 71, 72, 79, 80, 81}; + unsigned expected_bits[256] = {}; + for (unsigned f : bits128) expected_bits[f] = 128; + for (unsigned f : bits64) expected_bits[f] = 64; + for (unsigned f : bits32) expected_bits[f] = 32; + for (unsigned f : bits16) expected_bits[f] = 16; + for (unsigned f : bits8) expected_bits[f] = 8; + for (unsigned f : bits4) expected_bits[f] = 4; + for (unsigned f = 0; f < 256; ++f) + if (x2w_startup::small_texture_4k_bits(f) != expected_bits[f]) { + std::printf("FAIL format %u: %u bits per texel, expected %u\n", f, x2w_startup::small_texture_4k_bits(f), expected_bits[f]); + ++failures; + } + + // The limit the stand-in's own reads and writes use: the 4 KiB answer at Alignment 0x1000 only. + const auto extent_is = [](const char* name, const D3D12_RESOURCE_DESC& desc, unsigned long long expected) { + const unsigned long long got = x2w_startup::small_texture_4k_extent(desc); + if (got != expected) { + std::printf("FAIL extent %s: 0x%llx, expected 0x%llx\n", name, got, expected); + ++failures; + } + }; + extent_is("32x32 R8G8B8A8 at alignment 0 (desktop answer kept)", texture(28, 32, 32, 1, 0), 0); + extent_is("32x32 R8G8B8A8 at 4 KiB", texture(28, 32, 32), 0x1000); + extent_is("32x32 R8G8B8A8 at 64 KiB", texture(28, 32, 32, 1, 0x10000), 0); + extent_is("256x128 R8G8B8A8, over the bound", texture(28, 256, 128), 0); + + // The compact plan slot 2 describes, slot 8 writes and upload reads: levels back to back at exact row bytes. + struct Level { unsigned long long offset, row_bytes, rows; }; + const auto plan_is = [](const char* name, const D3D12_RESOURCE_DESC& desc, std::initializer_list levels) { + x2w_startup::SmallTexturePlan plan; + bool ok = x2w_startup::small_texture_4k_plan(desc, plan) && plan.levels == levels.size(); + unsigned i = 0; + for (const Level& l : levels) { + ok = ok && plan.level[i].offset == l.offset && plan.level[i].row_bytes == l.row_bytes && plan.level[i].rows == l.rows && + plan.level[i].bytes == l.row_bytes * l.rows; + ++i; + } + if (!ok) { + std::printf("FAIL plan %s\n", name); + ++failures; + } + }; + plan_is("32x32 R8G8B8A8 2 mips", texture(28, 32, 32, 2), {{0, 128, 32}, {0x1000, 64, 16}}); + plan_is("128x128 BC1 3 mips", texture(71, 128, 128, 3), {{0, 256, 32}, {0x2000, 128, 16}, {0x2800, 64, 8}}); + plan_is("4x4 BC7 full chain", texture(98, 4, 4, 0), {{0, 16, 1}, {16, 16, 1}, {32, 16, 1}}); + { + x2w_startup::SmallTexturePlan plan; + if (x2w_startup::small_texture_4k_plan(texture(28, 32, 32, 1, 0), plan)) { + std::printf("FAIL plan at alignment 0: a description without the 4 KiB answer got a compact plan\n"); + ++failures; + } + } + + // Every accepted shape (one format of each bit depth and both block sizes, power-of-two sides up to 4096, + // every mip count): the compact plan ends inside the answer, and no tail holds more than 7 levels, so + // addrlib's 8-level tail limit never applies to 4 KiB thin blocks. + const unsigned formats[] = {71, 98, 61, 54, 28, 11, 2}; + unsigned shapes = 0, overflow = 0, long_tails = 0; + for (unsigned f : formats) + for (unsigned w = 1; w <= 4096; w <<= 1) + for (unsigned h = 1; h <= 4096; h <<= 1) + for (unsigned mips = 1; mips <= 13; ++mips) { + const D3D12_RESOURCE_DESC desc = texture(f, w, h, mips); + D3D12_RESOURCE_ALLOCATION_INFO info = desktop; + x2w_startup::SmallTexturePlan plan; + if (!x2w_startup::small_texture_4k(desc, info) || !x2w_startup::small_texture_4k_plan(desc, plan)) continue; + ++shapes; + if (plan.bytes > info.SizeInBytes) ++overflow; + const auto block = x2w_startup::small_texture_4k_detail::block(x2w_startup::small_texture_4k_bits(f)); + for (unsigned m = 0; m < mips; ++m) + if (((w >> m) ? (w >> m) : 1u) <= block.width / 2 && ((h >> m) ? (h >> m) : 1u) <= block.height) { + if (mips - m > 7) ++long_tails; + break; + } + } + std::printf("compact plan: %u accepted shapes, %u past the answer, %u tails over 7 levels\n", shapes, overflow, long_tails); + if (shapes != 4943 || overflow || long_tails) ++failures; + if (failures) { + std::printf("startup_small_texture_4k: %d failure(s)\n", failures); + return 1; + } + std::printf("startup_small_texture_4k: PASS (15 sizes, 19 refused descriptions, 256 format values, extents, compact plans)\n"); + return 0; +} +} // namespace + +#ifndef XBOX2WINDOWS_HAVE_ADDRLIB +int addrlib_check() +{ + std::puts("startup_small_texture_4k --addrlib: SKIP (built without addrlib; run tools/fetch_addrlib.py and configure with " + "-DXBOX2WINDOWS_ADDRLIB_DIR=/src/amd/addrlib)"); + return 77; +} +#else +#include + +#include "addrinterface.h" + +namespace { +VOID* ADDR_API alloc_sys_mem(const ADDR_ALLOCSYSMEM_INPUT* in) { return std::malloc(in->sizeInBytes); } +ADDR_E_RETURNCODE ADDR_API free_sys_mem(const ADDR_FREESYSMEM_INPUT* in) +{ + std::free(in->pVirtAddr); + return ADDR_OK; +} +} // namespace + +// Every power-of-two shape and mip count the helper accepts, for one format of each bit depth, against +// Addr2ComputeSurfaceInfo with ADDR_SW_4KB_S on Mesa's "navi21" (gfx10.3) configuration, the one +// xg_swizzle_select_check uses: family 0x8F, revision 0x28, GB_ADDR_CONFIG 0x444. +int addrlib_check() +{ + ADDR_CREATE_INPUT create{}; + ADDR_CREATE_OUTPUT created{}; + create.size = sizeof create; + created.size = sizeof created; + create.chipEngine = 0x0000000D; // CIASICIDGFXENGINE_ARCTICISLAND + create.chipFamily = 0x8F; + create.chipRevision = 0x28; + create.callbacks.allocSysMem = alloc_sys_mem; + create.callbacks.freeSysMem = free_sys_mem; + create.regValue.gbAddrConfig = 0x40 | 4u | (4u << 8); + if (AddrCreate(&create, &created) != ADDR_OK) return 1; + struct { unsigned dxgi; AddrFormat addr; const char* name; } formats[] = { + {71, ADDR_FMT_BC1, "BC1"}, {98, ADDR_FMT_BC7, "BC7"}, {61, ADDR_FMT_8, "R8"}, {54, ADDR_FMT_16, "R16"}, + {28, ADDR_FMT_8_8_8_8, "R8G8B8A8"}, {11, ADDR_FMT_16_16_16_16, "R16G16B16A16"}, {2, ADDR_FMT_32_32_32_32, "R32G32B32A32"}}; + unsigned compared = 0, mismatches = 0; + for (const auto& f : formats) + for (unsigned w = 1; w <= 4096; w <<= 1) + for (unsigned h = 1; h <= 4096; h <<= 1) + for (unsigned mips = 1; mips <= 13; ++mips) { + D3D12_RESOURCE_ALLOCATION_INFO info = desktop; + if (!x2w_startup::small_texture_4k(texture(f.dxgi, w, h, mips), info)) continue; + ADDR2_COMPUTE_SURFACE_INFO_INPUT in{}; + ADDR2_COMPUTE_SURFACE_INFO_OUTPUT out{}; + in.size = sizeof in; + out.size = sizeof out; + in.flags.texture = 1; + in.swizzleMode = ADDR_SW_4KB_S; + in.resourceType = ADDR_RSRC_TEX_2D; + in.format = f.addr; + in.width = w; + in.height = h; + in.numSlices = 1; + in.numMipLevels = mips; + in.numSamples = 1; + in.numFrags = 1; + ++compared; + if (Addr2ComputeSurfaceInfo(created.hLib, &in, &out) != ADDR_OK || out.surfSize != info.SizeInBytes || + out.baseAlign != info.Alignment) { + if (++mismatches <= 12) + std::printf("FAIL %s %ux%u %u mips: helper 0x%llx at 0x%llx, addrlib 0x%llx at 0x%x\n", f.name, w, h, mips, + static_cast(info.SizeInBytes), + static_cast(info.Alignment), + static_cast(out.surfSize), out.baseAlign); + } + } + AddrDestroy(created.hLib); + std::printf("startup_small_texture_4k --addrlib: %u shapes compared, %u mismatches (Mesa 25.1.0 addrlib, SW_4KB_S)\n", compared, + mismatches); + return compared && !mismatches ? 0 : 1; +} +#endif + +int main(int argc, char** argv) +{ + if (argc > 1 && std::strcmp(argv[1], "--addrlib") == 0) return addrlib_check(); + const bool mutate = argc > 2 && std::strcmp(argv[1], "--mutate") == 0 && std::strcmp(argv[2], "whole-width-tail") == 0; + if (argc > 1 && !mutate) return 2; + return literal(mutate); +} diff --git a/tests/startup_small_texture_4k_draw_check.py b/tests/startup_small_texture_4k_draw_check.py new file mode 100644 index 0000000000..6362e3f372 --- /dev/null +++ b/tests/startup_small_texture_4k_draw_check.py @@ -0,0 +1,98 @@ +"""Draw a small texture given the 4 KiB allocation answer and read the pixels back (src/startup_stubs/small_texture_4k.h). + +A 32x32 R8G8B8A8 texture with two mips at Alignment 0x1000 is written through XG slot 8 into memory of exactly the +size XG slot 2 reports (0x2000), followed by a no-access guard page, placed through device slot 133 and sampled by a +translated draw into a 48x32 target: mip 0 at x < 32, mip 1 at x >= 32 and y < 16, black elsewhere. Every texel +holds its own x and y and a per-mip sentinel, so a missing, zero-filled or shifted row, or a mip read from the +wrong place, changes pixels. + +usage: startup_small_texture_4k_draw_check.py warp|hardware +""" +import os +from pathlib import Path +import subprocess +import sys +import tempfile + +from startup_stream_check import container, gfx2hlsl + +WIDTH, HEIGHT = 48, 32 + + +def texel(x, y, mip): + return bytes((x, y, (0x11, 0xEE)[mip], 0x5C)) + + +def fixture(): + mip0 = b"".join(texel(x, y, 0) for y in range(32) for x in range(32)) + mip1 = b"".join(texel(x, y, 1) for y in range(16) for x in range(16)) + reference = bytearray(WIDTH * HEIGHT * 4) + for y in range(HEIGHT): + for x in range(WIDTH): + if x < 32: + value = texel(x, y, 0) + elif y < 16: + value = texel(x - 32, y, 1) + else: + value = bytes(4) + reference[(y * WIDTH + x) * 4:(y * WIDTH + x + 1) * 4] = value + return mip0 + mip1, bytes(reference) + + +def check(work, consumer, dxc, backend): + cache = work / "cache" + cache.mkdir() + vs, ps, root, image, upload = (work / name for name in ("vs", "ps", "root", "pixels", "texels")) + vs.write_bytes(container(1)) + ps.write_bytes(container(0)) + env = dict(os.environ, X2W_D3D12_PERMISSIVE="1", X2W_D3D12_LIST_NOOP="1", X2W_D3D12_RENDER="1", + X2W_SHADER_CACHE=str(cache)) + texels, reference = fixture() + upload.write_bytes(texels) + base = [consumer, vs, ps, root, backend, image, upload, "32", "32", "small4k"] + bootstrap = subprocess.run(base, env=dict(env, X2W_TEST_TRUNCATED_ONLY="1")) + if bootstrap.returncode == 77: + print(f"{backend}: D3D12 feature level 12 unavailable") + return 77 + bootstrap.check_returncode() + key = f"{gfx2hlsl.pipeline_key(vs.read_bytes(), ps.read_bytes(), root.read_bytes()):016x}" + (cache / f"{key}.txt").write_text("udsrc 0 rect\nsrv 0 tex2d table 0 0\n", encoding="ascii") + sources = { + "vs": """float4 main(uint id : SV_VertexID) : SV_Position { + return float4(id == 1 ? 3.0 : -1.0, id == 2 ? -3.0 : 1.0, 0.0, 1.0); + }""", + "ps": """Texture2D tex : register(t0); + float4 main(float4 position : SV_Position) : SV_Target { + int2 p = int2(position.xy); + if (p.x < 32) return tex.Load(int3(p, 0)); + if (p.y < 16) return tex.Load(int3(p.x - 32, p.y, 1)); + return float4(0.0, 0.0, 0.0, 0.0); + }""", + } + for stage, source in sources.items(): + shader = work / f"{stage}.hlsl" + shader.write_text(source, encoding="ascii") + subprocess.run([dxc, "-T", f"{stage}_6_0", "-E", "main", "-Fo", cache / f"{key}.{stage}.cso", shader], check=True) + + result = subprocess.run(base, env=env, capture_output=True, text=True) + print(result.stdout, end="") + for line in result.stderr.splitlines(): + if "debug layer:" in line or "x2w-d3d12-debug " in line or "compact plan" in line: + print(line) + if result.returncode or "-> drawn;" not in result.stderr: + raise RuntimeError(f"{backend}: small4k draw failed ({result.returncode})\n{result.stderr[-4000:]}") + actual = image.read_bytes() + assert len(actual) == len(reference), f"read {len(actual)} bytes, expected {len(reference)}" + if actual != reference: + first = next(i for i in range(0, len(reference), 4) if actual[i:i + 4] != reference[i:i + 4]) + x, y = first // 4 % WIDTH, first // 4 // WIDTH + bad = sum(actual[i:i + 4] != reference[i:i + 4] for i in range(0, len(reference), 4)) + raise AssertionError(f"{backend}: {bad} pixels differ; first ({x}, {y}) read {actual[first:first + 4].hex()}, " + f"expected {reference[first:first + 4].hex()}") + print(f"{backend}: small4k draw matched all {WIDTH * HEIGHT} reference pixels (32 + 16 distinct rows, two mips)") + return 0 + + +if __name__ == "__main__": + with tempfile.TemporaryDirectory(dir=Path.cwd()) as temporary: + sys.exit(check(Path(temporary), *sys.argv[1:])) diff --git a/tests/startup_small_texture_4k_xg_check.py b/tests/startup_small_texture_4k_xg_check.py index 58af4c4a8c..6799794933 100644 --- a/tests/startup_small_texture_4k_xg_check.py +++ b/tests/startup_small_texture_4k_xg_check.py @@ -1,17 +1,21 @@ """XG slots 2 and 8 for a small texture given the 4 KiB allocation answer (src/startup_stubs/small_texture_4k.h). -A 32x32 R8G8B8A8 texture with layout UNKNOWN asked at Alignment 0x1000 gets 0x1000 bytes at 4 KiB alignment -from the allocation-info entries, while its desktop linear footprint is 0x1f80 bytes (32 rows at a 256-byte -pitch). Cases: - limited Alignment 0x1000: slot 2 reports 0x1000 at 0x1000, and slot 8 copies into a 0x1000-byte - allocation followed by a no-access guard page: the 16 rows inside are written, the guard is not - touched. - unchanged Alignment 0 (the allocation-info entries keep the desktop answer): slot 2 reports 0x10000 at - 0x10000, and slot 8 writes all 32 rows into a 64 KiB buffer, as before this change. +A 32x32 R8G8B8A8 texture with two mips, layout UNKNOWN, asked at Alignment 0x1000 gets 0x2000 bytes at 4 KiB +alignment. Its desktop linear footprint (256-byte rows) would not fit; the stand-in uses one compact plan +instead: mip 0 at offset 0 in 128-byte rows, mip 1 at 0x1000 in 64-byte rows. +Cases: + limited Slot 2 reports 0x2000 at 0x1000 and exactly those mip records. Slot 8 writes both mips into a + 0x2000-byte allocation followed by a no-access guard page. Every row is distinct (row index and + mip in each texel); mip 0 and mip 1 carry different sentinels. Memory must equal the plan + byte for byte, the bytes after the plan keep their fill, and the guard is not touched. + unsupported A row pitch shorter than a row, and a subresource past the last mip: slot 8 fails and leaves + memory unchanged. + unchanged Alignment 0 (the allocation-info entries keep the desktop answer): slot 2 reports 0x10000 at + 0x10000 and slot 8 writes all 32 rows at the desktop 256-byte pitch, as before this change. usage: startup_small_texture_4k_xg_check.py [--mutate no-limit] ---mutate no-limit runs the limited case at an explicit 64 KiB alignment, which is not limited: slot 8 then needs -the whole footprint in front of the guard page and fails (negative control). +--mutate no-limit runs the limited case at an explicit 64 KiB alignment, which gets no compact plan: slot 8 then +needs the desktop footprint in front of the guard page and fails (negative control). """ import ctypes as ct import struct @@ -21,9 +25,9 @@ MEM_COMMIT, MEM_RESERVE, MEM_RELEASE = 0x1000, 0x2000, 0x8000 PAGE_NOACCESS, PAGE_READWRITE = 0x01, 0x04 -WIDTH = HEIGHT = 32 -ROW_BYTES, DESKTOP_PITCH = WIDTH * 4, 256 -LINEAR = bytes((y * 7 + x) & 0xFF for y in range(HEIGHT) for x in range(ROW_BYTES)) +FILL = 0xA5 +MIP_RECORD = 0x18 + 0x38 # first mip record: after the layout header and the plane header +MIP_BYTES = 0x70 kernel = ct.WinDLL("kernel32", use_last_error=True) kernel.VirtualAlloc.restype = PTR @@ -32,38 +36,102 @@ kernel.VirtualFree.argtypes = (PTR, ct.c_size_t, ct.c_uint32) -def run(create, name, alignment, layout_expected, buffer_bytes, guard, rows_expected): - desc = ct.create_string_buffer(DESC.pack(3, alignment, WIDTH, HEIGHT, 1, 1, 28, 1, 0, 0, 0)) - computer = PTR() - assert create(desc, ct.byref(computer), 0, 0) == 0 and computer - slots = ct.cast(computer, ct.POINTER(ct.POINTER(PTR)))[0] +def texels(mip, width, height): + """Distinct rows: each texel is (x, y, mip sentinel, 0x5c).""" + sentinel = (0x11, 0xEE)[mip] + return bytes(b for y in range(height) for x in range(width) for b in (x, y, sentinel, 0x5C)) + + +class Computer: + def __init__(self, create, alignment, mips): + desc = ct.create_string_buffer(DESC.pack(3, alignment, 32, 32, 1, mips, 28, 1, 0, 0, 0)) + self.handle = PTR() + assert create(desc, ct.byref(self.handle), 0, 0) == 0 and self.handle + self.slots = ct.cast(self.handle, ct.POINTER(ct.POINTER(PTR)))[0] + + def layout(self): + buffer = ct.create_string_buffer(LAYOUT_BYTES) + assert LAYOUT(self.slots[2])(self.handle, buffer) == 0 + return buffer.raw + + def copy(self, base, subresource, data, pitch): + source = ct.create_string_buffer(data, len(data)) + return COPY_INTO(self.slots[8])(self.handle, base, 0, subresource, source, pitch, 0) & 0xFFFFFFFF + + def release(self): + RELEASE(self.slots[1])(self.handle) + + +def guarded(size): + block = kernel.VirtualAlloc(None, size + 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) + assert block + old = ct.c_uint32() + assert kernel.VirtualProtect(block + size, 0x1000, PAGE_NOACCESS, ct.byref(old)) + ct.memset(block, FILL, size) + return block + + +def limited(create, alignment): + computer = Computer(create, alignment, 2) + block = None + try: + raw = computer.layout() + total, align = struct.unpack_from(" 0x{hr0:08x}, mip 1 -> 0x{hr1:08x} (0x{size:x} bytes before a guard page)") + assert (hr0, hr1) == (0, 0), (hex(hr0), hex(hr1)) + memory = ct.string_at(block, size) + expected = mip0 + mip1 + bytes([FILL]) * (size - len(mip0) - len(mip1)) + if memory != expected: + first = next(i for i in range(size) if memory[i] != expected[i]) + raise AssertionError(f"limited: first difference at byte 0x{first:x}") + rows = {memory[y * 128:(y + 1) * 128] for y in range(32)} | {memory[0x1000 + y * 64:0x1000 + (y + 1) * 64] for y in range(16)} + assert len(rows) == 48, len(rows) + print("limited: 32 + 16 distinct rows in both halves of mip 0 and in mip 1, plan bytes exact, rest untouched") + + before = ct.string_at(block, size) + short = computer.copy(block, 0, mip0, 64) + past = computer.copy(block, 2, mip1, 64) + print(f"unsupported: short pitch -> 0x{short:08x}, subresource 2 -> 0x{past:08x}") + assert short != 0 and past != 0 and ct.string_at(block, size) == before + finally: + if block: + kernel.VirtualFree(block, 0, MEM_RELEASE) + computer.release() + + +def unchanged(create): + computer = Computer(create, 0, 1) block = None try: - layout = ct.create_string_buffer(LAYOUT_BYTES) - assert LAYOUT(slots[2])(computer, layout) == 0 - total, align = struct.unpack_from("(std::strtoul(argv[7], nullptr, 10)), static_cast(std::strtoul(argv[8], nullptr, 10)), std::strcmp(argv[9], "tiled") == 0}; - if ((bc7.width != 64 || bc7.height != 64) && (bc7.width != 81 || bc7.height != 69)) return 2; + bc7.small4k = std::strcmp(argv[9], "small4k") == 0; + if (bc7.small4k ? bc7.width != 32 || bc7.height != 32 + : (bc7.width != 64 || bc7.height != 64) && (bc7.width != 81 || bc7.height != 69)) return 2; if (std::strncmp(argv[9], "packed:", 7) == 0) bc7.packed = argv[9] + 7; + else if (bc7.small4k) {} else if (!bc7.tiled && std::strcmp(argv[9], "linear") != 0) return 2; } ComPtr adapter; diff --git a/tests/startup_stream_draw.cpp b/tests/startup_stream_draw.cpp index 007adb8b90..f80b66be0e 100644 --- a/tests/startup_stream_draw.cpp +++ b/tests/startup_stream_draw.cpp @@ -190,6 +190,8 @@ int bind_packed(ID3D12Device* desktop, void* guest, const StartupBc7Input& input return binding.table ? 0 : 41; } +bool bind_table(ID3D12Device* desktop, void* guest, void* texture, Bc7Binding& binding); + bool bind_bc7(ID3D12Device* desktop, void* guest, const StartupBc7Input& input, Bc7Binding& binding) { std::ifstream file(input.blocks, std::ios::binary); @@ -217,7 +219,45 @@ bool bind_bc7(ID3D12Device* desktop, void* guest, const StartupBc7Input& input, void* texture = nullptr; if (FAILED(static_cast(invoke(guest, 133, arg(binding.backing.data()), arg(&desc), D3D12_RESOURCE_STATE_COMMON, 0, arg(&__uuidof(ID3D12Resource)), arg(&texture))))) return false; + return bind_table(desktop, guest, texture, binding); +} +bool bind_small4k(ID3D12Device* desktop, void* guest, const StartupBc7Input& input, Bc7Binding& binding) +{ + std::ifstream file(input.blocks, std::ios::binary); + const std::vector texels{std::istreambuf_iterator(file), std::istreambuf_iterator{}}; + if (texels.size() != 32 * 32 * 4 + 16 * 16 * 4) return false; + const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0x1000, 32, 32, 1, 2, + DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE}; + void** computer = nullptr; + if (FAILED(XGCreateTextureComputer(&desc, &computer, 0, 0)) || !computer) return false; + const auto table = static_cast(computer[0]); + using Query = HRESULT(*)(void*, unsigned char*); + using Write = HRESULT(*)(void*, unsigned char*, UINT, UINT, const unsigned char*, UINT, UINT); + std::array layout{}; + UINT64 total = 0, alignment = 0; + HRESULT hr = reinterpret_cast(table[2])(computer, layout.data()); + std::memcpy(&total, layout.data(), sizeof total); + std::memcpy(&alignment, layout.data() + 8, sizeof alignment); + std::printf("small4k: slot 2 total 0x%llx alignment 0x%llx\n", static_cast(total), + static_cast(alignment)); + if (SUCCEEDED(hr) && (total != 0x2000 || alignment != 0x1000)) hr = E_FAIL; + auto* memory = SUCCEEDED(hr) ? static_cast(VirtualAlloc(nullptr, 0x3000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)) : nullptr; + DWORD old = 0; + if (!memory || !VirtualProtect(memory + 0x2000, 0x1000, PAGE_NOACCESS, &old)) hr = E_FAIL; + if (SUCCEEDED(hr)) hr = reinterpret_cast(table[8])(computer, memory, 0, 0, texels.data(), 128, 0); + if (SUCCEEDED(hr)) hr = reinterpret_cast(table[8])(computer, memory, 0, 1, texels.data() + 32 * 32 * 4, 64, 0); + reinterpret_cast(table[1])(computer); + if (FAILED(hr)) return false; + void* texture = nullptr; + if (FAILED(static_cast(invoke(guest, 133, arg(memory), arg(&desc), + D3D12_RESOURCE_STATE_COMMON, 0, arg(&__uuidof(ID3D12Resource)), arg(&texture))))) return false; + return bind_table(desktop, guest, texture, binding); +} + +// A shader-visible heap whose first entry is a view of every mip of texture; binding.table is its GPU handle. +bool bind_table(ID3D12Device* desktop, void* guest, void* texture, Bc7Binding& binding) +{ const UINT increment = desktop->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); const UINT entries = (32 + increment - 1) / increment; binding.views.resize(static_cast(entries) * increment); @@ -246,13 +286,15 @@ int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* // The translated MRT formats draw one pixel; the other formats keep the 8x4 target. const bool single = integer || format == DXGI_FORMAT_R32G32B32A32_FLOAT; const bool legacy = !single; - const UINT width = bc_chain ? 384 : bc7 ? bc7->width : bc ? 4 * 128 : single ? 1 : 8; + const UINT width = bc_chain ? 384 : bc7 ? (bc7->small4k ? 48 : bc7->width) : bc ? 4 * 128 : single ? 1 : 8; const UINT height = bc_chain ? 32 : bc7 ? bc7->height : bc ? 128 : single ? 1 : 4; ComPtr desktop; if (FAILED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&desktop)))) return 20; Bc7Binding binding; if (bc7 && bc7->packed) { if (const int rc = bind_packed(desktop.Get(), guest, *bc7, binding)) return rc; + } else if (bc7 && bc7->small4k) { + if (!bind_small4k(desktop.Get(), guest, *bc7, binding)) return 35; } else if (bc7 && !bind_bc7(desktop.Get(), guest, *bc7, binding)) return 34; StreamTarget frame; if (two_targets) frame.count = 2; // a second RGBA8 target of the same size, written after the first diff --git a/tests/startup_stream_draw.h b/tests/startup_stream_draw.h index e8fd303f20..4e42a9e9bb 100644 --- a/tests/startup_stream_draw.h +++ b/tests/startup_stream_draw.h @@ -13,6 +13,10 @@ struct StartupBc7Input { // a texture of that description is created through slot 133, cleared through a render-target // view of the view format and sampled through a shader-resource view of the view format. const char* packed = nullptr; + // A 32x32 R8G8B8A8 texture with two mips at Alignment 0x1000 (src/startup_stubs/small_texture_4k.h): blocks + // holds mip 0 then mip 1 as tight rows; both go through XG slot 8 into memory of exactly the size XG slot 2 + // reports, followed by a no-access guard page. The target is 48x32: mip 0 at the left, mip 1 top right. + bool small4k = false; }; int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* pixels, From a6b880045ad9414e8e9d5ee175b45128149b2366 Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 03:00:49 +0100 Subject: [PATCH 5/5] startup: copy mip 0 from a padded source pitch in the 4 KiB XG check A tight source pitch let slot 8 place rows at the caller's pitch unnoticed; 160-byte source rows must still land as 128-byte plan rows. Co-Authored-By: Claude Opus 5.5 --- tests/startup_small_texture_4k_xg_check.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/startup_small_texture_4k_xg_check.py b/tests/startup_small_texture_4k_xg_check.py index 6799794933..8ef2bf3f57 100644 --- a/tests/startup_small_texture_4k_xg_check.py +++ b/tests/startup_small_texture_4k_xg_check.py @@ -4,7 +4,8 @@ alignment. Its desktop linear footprint (256-byte rows) would not fit; the stand-in uses one compact plan instead: mip 0 at offset 0 in 128-byte rows, mip 1 at 0x1000 in 64-byte rows. Cases: - limited Slot 2 reports 0x2000 at 0x1000 and exactly those mip records. Slot 8 writes both mips into a + limited Slot 2 reports 0x2000 at 0x1000 and exactly those mip records. Slot 8 writes both mips (mip 0 from + a source with padded 160-byte rows) into a 0x2000-byte allocation followed by a no-access guard page. Every row is distinct (row index and mip in each texel); mip 0 and mip 1 carry different sentinels. Memory must equal the plan byte for byte, the bytes after the plan keep their fill, and the guard is not touched. @@ -89,7 +90,9 @@ def limited(create, alignment): size = total block = guarded(size) mip0, mip1 = texels(0, 32, 32), texels(1, 16, 16) - hr0 = computer.copy(block, 0, mip0, 128) + # Mip 0 comes from a source with 160-byte rows (32 bytes of padding each): the plan's rows stay 128 bytes. + padded = b"".join(mip0[y * 128:(y + 1) * 128] + b"\x77" * 32 for y in range(32)) + hr0 = computer.copy(block, 0, padded, 160) hr1 = computer.copy(block, 1, mip1, 64) print(f"limited: slot 8 mip 0 -> 0x{hr0:08x}, mip 1 -> 0x{hr1:08x} (0x{size:x} bytes before a guard page)") assert (hr0, hr1) == (0, 0), (hex(hr0), hex(hr1))