diff --git a/CMakeLists.txt b/CMakeLists.txt index ccf282518c..73cc5523f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2101,6 +2101,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) @@ -2748,6 +2771,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") @@ -2851,6 +2883,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 a34410adee..7e48042d7f 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -2004,6 +2004,81 @@ 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. 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. + +Scope: a 2D texture with layout UNKNOWN, `Alignment` 0x1000, one sample, one array slice, power-of-two +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 +(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 +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. +- `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 `XMemAllocatePhysicalPages` pages are 64 KiB slots of 256 MiB pagefile-backed sections. With diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 44bcaf7bba..ff97d32973 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1070,8 +1070,19 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) if (!plan.bytes(i) || plan.rows[i] != console_layout->rows(i) || plan.row_bytes[i] != u64{console_layout->columns(i)} * console_layout->bytes_per_element) return false; } + // 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. Address-only textures + // (single-element, or a console layout named in Layout) are tiled and keep their own paths. + 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 true; + } const u64 total = tiled_chain ? x2w_startup::bc64::Chain(original->console).bytes() : console ? console_layout->bytes() - : tiled && !single ? surface.size() : stride * (slices - 1) + slice_total; + : tiled && !single ? surface.size() : 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); // A single texel (at most 16 bytes) is rehashed on every bind, so a write between two draws is seen. @@ -1166,7 +1177,17 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) const auto& fp = plan.placed[i]; const u64 bytes = plan.bytes(i); u64 offset = 0; - if (!ring_copy(reinterpret_cast(memory) + stride * s + fp.Offset, static_cast(bytes), &offset)) { + if (is_compact) { + auto* const rows = reserve_upload_ring(static_cast(bytes), &offset); + if (!rows) { + state.done = false; + return true; + } + 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 true; } @@ -1182,8 +1203,9 @@ bool 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)" : "", + is_compact ? " (compact plan of the 4 KiB allocation answer)" : ""); return true; } diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 9a33dcfe5d..52e25dd850 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -44,6 +44,7 @@ #include "command_signature_filter.h" #include "console_plane_size.h" #include "descriptor_heap_type.h" +#include "small_texture_4k.h" #include "null_binding_cause.h" #include "null_texture_view.h" #include "packed_target_format.h" @@ -886,6 +887,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) { @@ -896,6 +908,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)) @@ -1022,6 +1035,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..09fd133ff4 --- /dev/null +++ b/src/startup_stubs/small_texture_4k.h @@ -0,0 +1,154 @@ +// 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; 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 +// 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, 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 + +#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 + +// 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) +{ + const unsigned f = format; + if (f >= 1 && f <= 4) return 128; // R32G32B32A32 + 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 + 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) return (blocks + 1) * 0x1000; + blocks += (w + b.width - 1) / b.width * ((h + b.height - 1) / b.height); + } + return blocks * 0x1000; +} + +// 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.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 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 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 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 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 ? small_texture_4k_shape(d) : 0; +} + +// 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 (!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 3e8ea8cb70..bc308a701a 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 @@ -151,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); @@ -159,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); @@ -174,16 +229,17 @@ long __stdcall computer_layout(Computer* self, unsigned char* layout) if (!valid) slice_total = 0x10000; const u64 stride = (slice_total + 0xFFFF) & ~u64{0xFFFF}; const u64 total = stride * slices; + const u64 alignment = 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 +269,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; } @@ -302,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; diff --git a/tests/startup_small_texture_4k_check.cpp b/tests/startup_small_texture_4k_check.cpp new file mode 100644 index 0000000000..c83b2541d3 --- /dev/null +++ b/tests/startup_small_texture_4k_check.cpp @@ -0,0 +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 +#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_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; +} 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 new file mode 100644 index 0000000000..8ef2bf3f57 --- /dev/null +++ b/tests/startup_small_texture_4k_xg_check.py @@ -0,0 +1,157 @@ +"""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 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 (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. + 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 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 +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 +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 +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 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: + 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) diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 70379871bd..a6ceb46783 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -248,8 +248,11 @@ int main(int argc, char** argv) bc7 = {argv[6], static_cast(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 1531cdf072..2f89f37397 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); @@ -248,13 +288,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,