diff --git a/CMakeLists.txt b/CMakeLists.txt index a5b4d5e5d5..d3bdfc5af5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3398,6 +3398,8 @@ 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) +add_test(NAME xg_gfx10_tiling_reference COMMAND xg_swizzle_select_check --gfx10-tiling) +set_tests_properties(xg_gfx10_tiling_reference PROPERTIES TIMEOUT 30 SKIP_RETURN_CODE 77) if(EXISTS "${INTAKE_PYTHON}") add_test(NAME tooling_fetch_addrlib_cli_check COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/fetch_addrlib_cli_check.py") @@ -4968,6 +4970,9 @@ if(EXISTS "${INTAKE_PYTHON}") 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") + add_test(NAME startup_xg_gfx10_runtime COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_xg_gfx10_check.py" "$") + set_tests_properties(startup_xg_gfx10_runtime PROPERTIES TIMEOUT 30 ENVIRONMENT "X2W_D3D12_PERMISSIVE=1") add_test(NAME startup_xg_tile64_tiling COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_xg_tile64_tiling_check.py" "$") set_tests_properties(startup_xg_tile64_tiling PROPERTIES TIMEOUT 30 ENVIRONMENT "X2W_D3D12_PERMISSIVE=1") diff --git a/CMakePresets.json b/CMakePresets.json index 6bd4804f08..6f49ceea14 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -36,6 +36,7 @@ "xmem_placeholder_split_check", "xmem_map_split_first_check", "startup_dstorage_break_window_check", "xmem_page_runs_device_check", "linear_buffer_backing_check", "bcpack_formats_check", "startup_stub_consumer", "startup_inline_retire", "startup_resource_retire_title", "startup_table_lock", + "startup_stub_xg_xs", "xg_swizzle_select_check", "startup_stream_consumer", "native_compute_ir_dump", "native_pixel_ir_dump", "native_store_replay", "native_raw_buffer_view_check" ] }, @@ -50,7 +51,7 @@ "displayName": "Windows pull request smoke tests", "filter": { "include": { - "name": "^(bcpack_all_formats(_golden_control)?|startup_bcpack_storage|desktop_root_signature|era_heap_(allocation|failure)|xmem_shim_mapped_pages|cpu_aot_fuzz_smoke|cpu_archive_texture_gpu_warp|dual_uav_warp|spirv_.*_warp|static_exe_intake|descriptor_heap_paging(_swapped)?_warp|startup_launch|startup_mesh57_real_shape|startup_mesh57_runtime(_[a-z]+)?_warp|native_fixed_mesh57_published_pair(_refusal)?_warp|startup_tools_lint|startup_gfx2hlsl_cvt_off_f32_i4_controls|startup_gfx2hlsl_sad_hi_msad_controls|startup_indirect_(dispatch|count_split|draw(_indexed)?|mesh(_xmem_arguments(_skip_wait)?)?|counts_control)_warp|startup_indirect_(dispatch|constants|count)_inline_warp|startup_inline_retire_(on|control)_warp|startup_resource_retire_title_inline_warp_debug0|startup_table_lock_(control_)?warp|startup_indirect_mesh_lease_warp_dual(-trace(-disabled)?)?|startup_indirect_mesh_cache_heap(_wide)?_warp|runtime_mesh86_cache_cli_check|startup_shader_cache_key|startup_present_readback|startup_present_planes(_control)?|startup_present_composite_(warp|overlay_removed_control_warp)|startup_mesh_title_snapshot_warp|startup_list_copy_(warp|title_snapshot_warp|validation(_[a-z_]+_control)?)|xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first)(_[a-z_]+)?|startup_dstorage_break_window(_[a-z_]+)?|xmem_page_runs_device(_join)?_warp|linear_buffer_backing_share_warp|startup_stage_package|startup_compute_packed_d16_x_uav_reference|native_.*_(ir|dxil|controls)|native_direct_mesh|startup_triage_run)$" + "name": "^(bcpack_all_formats(_golden_control)?|startup_bcpack_storage|desktop_root_signature|era_heap_(allocation|failure)|xmem_shim_mapped_pages|cpu_aot_fuzz_smoke|cpu_archive_texture_gpu_warp|dual_uav_warp|spirv_.*_warp|static_exe_intake|descriptor_heap_paging(_swapped)?_warp|startup_launch|startup_mesh57_real_shape|startup_mesh57_runtime(_[a-z]+)?_warp|native_fixed_mesh57_published_pair(_refusal)?_warp|startup_tools_lint|startup_gfx2hlsl_cvt_off_f32_i4_controls|startup_gfx2hlsl_sad_hi_msad_controls|startup_indirect_(dispatch|count_split|draw(_indexed)?|mesh(_xmem_arguments(_skip_wait)?)?|counts_control)_warp|startup_indirect_(dispatch|constants|count)_inline_warp|startup_inline_retire_(on|control)_warp|startup_resource_retire_title_inline_warp_debug0|startup_table_lock_(control_)?warp|startup_indirect_mesh_lease_warp_dual(-trace(-disabled)?)?|startup_indirect_mesh_cache_heap(_wide)?_warp|runtime_mesh86_cache_cli_check|startup_shader_cache_key|startup_present_readback|startup_present_planes(_control)?|startup_present_composite_(warp|overlay_removed_control_warp)|startup_mesh_title_snapshot_warp|startup_list_copy_(warp|title_snapshot_warp|validation(_[a-z_]+_control)?)|xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first)(_[a-z_]+)?|startup_dstorage_break_window(_[a-z_]+)?|xmem_page_runs_device(_join)?_warp|linear_buffer_backing_share_warp|startup_stage_package|startup_compute_packed_d16_x_uav_reference|native_.*_(ir|dxil|controls)|native_direct_mesh|startup_triage_run|startup_xg_gfx10_runtime|xg_gfx10_tiling_reference|startup_xg_bc7_draw_warp)$" } } }, diff --git a/docs/experiments.md b/docs/experiments.md index 8506dfdc05..a2b2040aba 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -17173,6 +17173,8 @@ ctest --preset release --no-tests=error --output-on-failure -R '^startup_(placem ### Nine-mip BC5/BC7 tile queries +The general GFX10 texture computer described in the next section now answers XG slots 2, 3, 6, 8, 9 and 10 for the explicit layouts `0x100, 0x101, 0x102, 0x105, 0x106, 0x109, 0x10a` (S, D, and linear modes). The pins below stay specific to the earlier nine-mip `0x109` BC5/BC7 shape. + XG texture-computer slot 10 reports a 64 KiB standard tile layout for 1024×512 and 1024×1024 single-sample BC5_UNORM and BC7_UNORM textures with nine mips, one or two array slices, explicit layout `0x109`, default alignment and zero flags. Each slice starts with one packed tile containing six mips. The 1024×512 standard mips start at tiles 4, 2 and 1 in a 12-tile slice. The 1024×1024 standard mips start at tiles 6, 2 and 1 in a 22-tile slice. `ctest --preset dev --no-tests=error -R '^startup_bc64_supports(_layout_control)?$' -V` checks which descriptions `src/startup_stubs/bc64_chain.h` routes to this layout, without a device or AddrLib: the two six-mip and eight nine-mip shapes are accepted, and 16 descriptions that each change one field, among them layout `UNKNOWN`, 64 KiB alignment, a render-target flag, `BC7_UNORM_SRGB` and six mips with two slices, are rejected. The control expects layout `UNKNOWN` to be accepted and must fail. `startup_xg_bc_tiling` checks 80 subresource queries, including a request spanning the last packed mip in one slice and the first standard mips of the next. The returned count includes only standard mips. Their entries use their original positions in the requested range. Sentinel bytes cover packed entries and the remainder of the caller's array. The field values were compared with XCL's slot 10 implementation on source-built inputs. The optional AMD AddrLib test independently verifies the surface size, slice stride, first packed mip and standard and packed macro-tile offsets for all eight shapes and formats. [XCL implementation](https://github.com/ivall/xcl/blob/64b3f35599bb7102f55d640e1a791e7d6dd3b175/crates/xcl/src/hle/xg.rs#L610-L658) · [AMD AddrLib revision](https://gitlab.freedesktop.org/mesa/mesa/-/tree/5c142e46f3f6e752ed745fd48912ebb8fad67145/src/amd/addrlib) @@ -17191,6 +17193,22 @@ cmake --build --preset release --target startup_stream_consumer startup_stub_xg_ ctest --preset release --no-tests=error --output-on-failure -R '^(startup_xg_bc_(array|chain)_(hardware|warp)|startup_xg_bc_tiling|xg_bc64_tiling_reference|startup_xg_bc7_layout)$' ``` +### GFX10 texture computer and desktop draw detiling runtime + +`Gfx10Texture` (`src/startup_stubs/gfx10_texture.h`) implements the runtime tiling and detiling computer for GFX10 textures across 256 B, 4 KiB, and 64 KiB blocks in standard (`_S`) and display (`_D`) modes (explicit layouts `0x101`, `0x102`, `0x105`, `0x106`, `0x109`, `0x10a`), as well as multi-mip and multi-slice linear layout `0x100` (packed slices, smallest mip first). It supports 1-, 2-, 4-, 8-, and 16-byte elements (including BC1 to BC7 block-compressed formats): + +- XG texture-computer stand-in (`src/startup_stubs/xg_xs.cpp`) provides slot 2 (layout query, mip starts, alignment), slot 3 (total allocation size), slot 6 (element coordinate offset query), slot 8 (`CopyIntoSubresource` for linear to tiled upload), and slot 9 (`CopyFromSubresource` for tiled to linear readback). +- In AMD AddrLib (`src/amd/addrlib`) configured for GFX10.3 / RDNA2 (Navi21 reference with `supportRbPlus = 1`), `GetSwizzlePatternInfo` selects `GFX10_SW_256_D_RBPLUS_PATINFO` (pattern index 39: `"----x0x1y0y1x2y2"`), where bit 6 is $x_2$ and bit 7 is $y_2$, while Navi1x without RbPlus selects pattern 2 (`"----x0x1y0y1y2x2"`). Whether console hardware matches the RbPlus pattern remains an open hypothesis pending independent hardware measurement. +- The desktop draw uploader (`src/startup_stubs/d3d12_draw.inl`) detiles placed CPU textures via `is_gfx10_tiled` for all explicit GFX10 tiled modes and multi-mip or array linear (0x100) textures into desktop D3D12 copyable footprints on upload. +- `xg_gfx10_tiling_reference` independently compares `Gfx10Texture` against Mesa 25.1.0 AMD AddrLib on Navi21 for 23 surface shapes, validating surface sizes, slice strides, base alignments, mip offsets, and coordinate addresses with 0 mismatches. +- `startup_xg_gfx10_runtime` tests layout queries, slot-6 coordinate offsets, slot-8 tiling, slot-9 detiling, pinned AddrLib offsets across all five element sizes, implicit layout routing, and negative controls. +- `startup_xg_bc7_draw_{warp,hardware}` executes end-to-end draw tests with wrapped XG uploads for 4 KiB standard RGBA8 (`rgba8_4k_s`), 3-mip linear RGBA8 (`rgba8_linear_3m`), and their respective negative controls. + +```powershell +cmake --build --preset dev --target startup_stub_xg_xs startup_stream_consumer xg_swizzle_select_check +ctest --preset dev --no-tests=error -R '^(startup_xg_gfx10_runtime|xg_gfx10_tiling_reference|startup_xg_bc7_draw_(warp|hardware))$' -V +``` + ### Slot-10 tile plan for layout `0x10a` For a two-dimensional texture with layout `0x10a`, one sample and a format with a known 64 KiB tile shape, XG texture-computer slot 10 is answered from `src/startup_stubs/tile64_plan.h` instead of a desktop reserved resource. The plan follows one rule: diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 976e2f80ee..42e6247a01 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -3857,9 +3857,10 @@ In the plan, every row has its exact byte width (a row of texels, or of 4 x 4 bl 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. +as it already was with the desktop layout. `Gfx10Texture` (#1469) tiles the explicit GFX10 layouts, not +this UNKNOWN-and-4096-alignment path; using it here would replace the compact plan in all three calls +at once and is left to a follow-up. 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 diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 2b012ca84d..abf4819a2a 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -2062,17 +2062,20 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) if (const auto found = written_textures().find(reinterpret_cast(memory)); found != written_textures().end()) written = found->second; ReleaseSRWLockShared(&written_lock); - const bool tiled_chain = original && x2w_startup::bc64::supports(original->console); - // Any other texture slot 70 publishes holds its texels in the console layout its description names - // (gfx10_layout.h, address_texture.h). - const bool tiled_bc7 = !tiled_chain && !tiled_address && written.bc7_width != 0; + const bool tiled_chain = !single && original && x2w_startup::bc64::supports(original->console); // Placed sampled BC7 surfaces can carry the same 64 KiB display layout as an address-only SRV. // GPU-authored resources have already returned above; only detile the CPU backing on upload. const bool placed_display_bc7 = !tiled_address && original && desc.MipLevels == 1 && desc.DepthOrArraySize == 1 && desc.Format == DXGI_FORMAT_BC7_UNORM && x2w_startup::gfx10::swizzle_mode(original->console) == 10 && console_layout_srv(original->console); const bool detile_console = console || placed_display_bc7; - const bool tiled = tiled_bc7 || tiled_address || tiled_chain || placed_display_bc7; + const bool is_gfx10_tiled = !placed_display_bc7 && !tiled_address && !single && original && + x2w_startup::Gfx10Texture::supports(original->console) && + (x2w_startup::Gfx10Texture::is_tiled(original->console) || mips > 1 || slices > 1); + // Any other texture slot 70 publishes holds its texels in the console layout its description names + // (gfx10_layout.h, address_texture.h). + const bool tiled_bc7 = !is_gfx10_tiled && !tiled_chain && !tiled_address && !placed_display_bc7 && written.bc7_width != 0; + const bool tiled = tiled_bc7 || tiled_address || tiled_chain || is_gfx10_tiled || placed_display_bc7; std::optional console_layout; if (detile_console) console_layout.emplace(original->console); const auto generation = written.generation; @@ -2084,7 +2087,7 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) desc.Format != DXGI_FORMAT_BC7_UNORM || desc.Width != surface.columns * surface.block_texels || desc.Height != surface.rows * surface.block_texels) return true; } - if (tiled && !tiled_chain && !single && !detile_console && (plan.count != 1 || plan.rows[0] != surface.rows || + if (tiled && !is_gfx10_tiled && !tiled_chain && !single && !detile_console && (plan.count != 1 || plan.rows[0] != surface.rows || plan.row_bytes[0] != u64{surface.columns} * surface.block_bytes)) return true; if (detile_console) { // The desktop footprints must hold exactly the elements the console layout addresses. @@ -2105,7 +2108,17 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) 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() : detile_console ? console_layout->bytes() + const x2w_startup::Gfx10Texture gfx10_tex = is_gfx10_tiled ? x2w_startup::Gfx10Texture(original->console) : x2w_startup::Gfx10Texture{}; + if (is_gfx10_tiled) { + bool rows_match = gfx10_tex.levels == mips && gfx10_tex.slices == slices && plan.count == mips; + for (UINT i = 0; rows_match && i < mips; ++i) + rows_match = plan.bytes(i) && plan.rows[i] == gfx10_tex.rows(i) && + plan.row_bytes[i] == u64{gfx10_tex.columns(i)} * gfx10_tex.bpe; + if (!rows_match) return true; + } + const u64 total = is_gfx10_tiled ? gfx10_tex.bytes() + : tiled_chain ? x2w_startup::bc64::Chain(original->console).bytes() + : detile_console ? console_layout->bytes() : 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); @@ -2173,8 +2186,9 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) state.pending_list = list; state.revision = address_upload_revision; } - } else if (tiled_chain) { - const x2w_startup::bc64::Chain chain(original->console); + } else if (is_gfx10_tiled || tiled_chain) { + std::optional chain; + if (tiled_chain && !is_gfx10_tiled) chain.emplace(original->console); for (UINT slice = 0; slice < slices; ++slice) { for (UINT i = 0; i < mips; ++i) { const u64 bytes = plan.bytes(i); @@ -2186,7 +2200,11 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture) return true; } std::memset(rows, 0, static_cast(bytes)); - chain.copy(i, slice, rows, memory, plan.placed[i].Footprint.RowPitch); + if (is_gfx10_tiled) { + gfx10_tex.copy_subresource(i, slice, rows, memory, plan.placed[i].Footprint.RowPitch); + } else { + chain->copy(i, slice, rows, memory, plan.placed[i].Footprint.RowPitch); + } plan.upload(list, texture, chunk, mips, slices, slice, i, offset); } } diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 0a3ddf5832..fc95514341 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -45,6 +45,7 @@ #include "startup_stubs/compute_builder_gate.h" #include "startup_stubs/bc7_4k_s.h" #include "startup_stubs/bc64_chain.h" +#include "startup_stubs/gfx10_texture.h" #include "startup_stubs/address_texture.h" #include "startup_stubs/diagnostic_span.h" #include "startup_stubs/list_upload_pages.h" diff --git a/src/startup_stubs/gfx10_texture.h b/src/startup_stubs/gfx10_texture.h new file mode 100644 index 0000000000..c284e09099 --- /dev/null +++ b/src/startup_stubs/gfx10_texture.h @@ -0,0 +1,430 @@ +#pragma once + +// GFX10 texture tiling, detiling, and mip-tail layout runtime. +// +// The micro-tile patterns, macro-tile dimensions, and mip-tail calculations +// follow AMD's addrlib in Mesa (src/amd/addrlib, SPDX: MIT): +// +// Copyright (C) 2007-2024 Advanced Micro Devices, Inc. All rights reserved. +// SPDX-License-Identifier: MIT + +#include +#include +#include +#include +#include + +namespace x2w_startup { + +enum Gfx10SwizzleMode : UINT { + SW_LINEAR = 0, + SW_256B_S = 1, + SW_256B_D = 2, + SW_4KB_S = 5, + SW_4KB_D = 6, + SW_64KB_S = 9, + SW_64KB_D = 10, +}; + +struct Gfx10FormatInfo { + UINT bpe = 0; // Bytes per element (texel or block) + UINT block_dim = 0; // 4 for block-compressed (BC), 1 for uncompressed +}; + +inline Gfx10FormatInfo gfx10_format_info(DXGI_FORMAT fmt) +{ + switch (fmt) { + case DXGI_FORMAT_BC2_TYPELESS: + case DXGI_FORMAT_BC2_UNORM: + case DXGI_FORMAT_BC2_UNORM_SRGB: + case DXGI_FORMAT_BC3_TYPELESS: + case DXGI_FORMAT_BC3_UNORM: + case DXGI_FORMAT_BC3_UNORM_SRGB: + case DXGI_FORMAT_BC5_TYPELESS: + case DXGI_FORMAT_BC5_UNORM: + case DXGI_FORMAT_BC5_SNORM: + case DXGI_FORMAT_BC6H_TYPELESS: + case DXGI_FORMAT_BC6H_UF16: + case DXGI_FORMAT_BC6H_SF16: + case DXGI_FORMAT_BC7_TYPELESS: + case DXGI_FORMAT_BC7_UNORM: + case DXGI_FORMAT_BC7_UNORM_SRGB: + return {16, 4}; + + case DXGI_FORMAT_BC1_TYPELESS: + case DXGI_FORMAT_BC1_UNORM: + case DXGI_FORMAT_BC1_UNORM_SRGB: + case DXGI_FORMAT_BC4_TYPELESS: + case DXGI_FORMAT_BC4_UNORM: + case DXGI_FORMAT_BC4_SNORM: + return {8, 4}; + + case DXGI_FORMAT_R32G32B32A32_TYPELESS: + case DXGI_FORMAT_R32G32B32A32_FLOAT: + case DXGI_FORMAT_R32G32B32A32_UINT: + case DXGI_FORMAT_R32G32B32A32_SINT: + return {16, 1}; + + case DXGI_FORMAT_R16G16B16A16_TYPELESS: + case DXGI_FORMAT_R16G16B16A16_FLOAT: + case DXGI_FORMAT_R16G16B16A16_UNORM: + case DXGI_FORMAT_R16G16B16A16_UINT: + case DXGI_FORMAT_R16G16B16A16_SNORM: + case DXGI_FORMAT_R16G16B16A16_SINT: + case DXGI_FORMAT_R32G32_TYPELESS: + case DXGI_FORMAT_R32G32_FLOAT: + case DXGI_FORMAT_R32G32_UINT: + case DXGI_FORMAT_R32G32_SINT: + return {8, 1}; + + case DXGI_FORMAT_R10G10B10A2_TYPELESS: + case DXGI_FORMAT_R10G10B10A2_UNORM: + case DXGI_FORMAT_R10G10B10A2_UINT: + case DXGI_FORMAT_R11G11B10_FLOAT: + case DXGI_FORMAT_R8G8B8A8_TYPELESS: + case DXGI_FORMAT_R8G8B8A8_UNORM: + case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: + case DXGI_FORMAT_R8G8B8A8_UINT: + case DXGI_FORMAT_R8G8B8A8_SNORM: + case DXGI_FORMAT_R8G8B8A8_SINT: + case DXGI_FORMAT_R16G16_TYPELESS: + case DXGI_FORMAT_R16G16_FLOAT: + case DXGI_FORMAT_R16G16_UNORM: + case DXGI_FORMAT_R16G16_UINT: + case DXGI_FORMAT_R16G16_SNORM: + case DXGI_FORMAT_R16G16_SINT: + case DXGI_FORMAT_R32_TYPELESS: + case DXGI_FORMAT_R32_FLOAT: + case DXGI_FORMAT_R32_UINT: + case DXGI_FORMAT_R32_SINT: + case DXGI_FORMAT_B8G8R8A8_UNORM: + case DXGI_FORMAT_B8G8R8X8_UNORM: + case DXGI_FORMAT_B8G8R8A8_TYPELESS: + case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB: + case DXGI_FORMAT_B8G8R8X8_TYPELESS: + case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB: + return {4, 1}; + + case DXGI_FORMAT_R8G8_TYPELESS: + case DXGI_FORMAT_R8G8_UNORM: + case DXGI_FORMAT_R8G8_UINT: + case DXGI_FORMAT_R8G8_SNORM: + case DXGI_FORMAT_R8G8_SINT: + case DXGI_FORMAT_R16_TYPELESS: + case DXGI_FORMAT_R16_FLOAT: + case DXGI_FORMAT_R16_UNORM: + case DXGI_FORMAT_R16_UINT: + case DXGI_FORMAT_R16_SNORM: + case DXGI_FORMAT_R16_SINT: + case DXGI_FORMAT_B5G6R5_UNORM: + case DXGI_FORMAT_B5G5R5A1_UNORM: + return {2, 1}; + + case DXGI_FORMAT_R8_TYPELESS: + case DXGI_FORMAT_R8_UNORM: + case DXGI_FORMAT_R8_UINT: + case DXGI_FORMAT_R8_SNORM: + case DXGI_FORMAT_R8_SINT: + case DXGI_FORMAT_A8_UNORM: + return {1, 1}; + + default: + return {0, 0}; + } +} + +inline UINT gfx10_swizzle_from_desc(const D3D12_RESOURCE_DESC& d) +{ + const UINT layout = static_cast(d.Layout); + return (layout & 0xFF00) == 0x100 ? (layout & 0xFF) : 0xFFu; +} + +inline UINT gfx10_swizzle_block_log2(UINT mode) +{ + switch (mode) { + case SW_LINEAR: return 0; + case SW_256B_S: + case SW_256B_D: return 8; + case SW_4KB_S: + case SW_4KB_D: return 12; + case SW_64KB_S: + case SW_64KB_D: return 16; + default: return 0; + } +} + +struct Gfx10Texture { + UINT width = 0, height = 0, levels = 0, slices = 0; + UINT bpe = 0, block_dim = 1; + UINT swizzle_mode = SW_64KB_S; + UINT block_log2 = 16; + UINT tile_width = 0, tile_height = 0, tile_bytes = 0; + UINT micro_w = 0, micro_h = 0; + UINT first_tail = 0; + UINT64 total_slice_bytes = 0; + + std::array x_table{}; + std::array y_table{}; + std::array mip_start{}; + std::array tiles_w{}; + std::array tiles_h{}; + std::array tail_origin_x{}; + std::array tail_origin_y{}; + + static bool supports(const D3D12_RESOURCE_DESC& d) + { + if (d.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) return false; + if (d.Width < 1 || d.Height < 1 || d.MipLevels < 1 || d.MipLevels > 16 || d.DepthOrArraySize < 1) return false; + if (d.SampleDesc.Count != 1) return false; + if (d.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) return false; + const auto fi = gfx10_format_info(d.Format); + if (fi.bpe == 0) return false; + const UINT mode = gfx10_swizzle_from_desc(d); + return mode == SW_LINEAR || mode == SW_256B_S || mode == SW_256B_D || + mode == SW_4KB_S || mode == SW_4KB_D || mode == SW_64KB_S || mode == SW_64KB_D; + } + + static bool is_tiled(const D3D12_RESOURCE_DESC& d) + { + const UINT layout = static_cast(d.Layout); + if ((layout & 0xFF00) == 0x100) return (layout & 0xFF) != SW_LINEAR; + return false; + } + + Gfx10Texture() = default; + explicit Gfx10Texture(const D3D12_RESOURCE_DESC& d) + : width(static_cast(d.Width)), height(d.Height), + levels(d.MipLevels ? d.MipLevels : 1), slices(d.DepthOrArraySize ? d.DepthOrArraySize : 1) + { + const auto fi = gfx10_format_info(d.Format); + bpe = fi.bpe; + block_dim = fi.block_dim; + swizzle_mode = gfx10_swizzle_from_desc(d); + block_log2 = gfx10_swizzle_block_log2(swizzle_mode); + + if (block_log2 == 0) { + init_linear(); + return; + } + + UINT element_log2 = 0; + while ((1u << element_log2) < bpe) ++element_log2; + const UINT elements_log2 = block_log2 - element_log2; + const UINT w = (elements_log2 + 1) / 2; + tile_width = 1u << w; + tile_height = 1u << (elements_log2 - w); + tile_bytes = 1u << block_log2; + + static const UINT MICRO_2D[5][2] = { + {16, 16}, {16, 8}, {8, 8}, {8, 4}, {4, 4} + }; + micro_w = MICRO_2D[element_log2][0]; + micro_h = MICRO_2D[element_log2][1]; + + static const char* const PATTERN_S[5] = { + "x0x1x2x3y0y1y2y3", + "--x0x1x2y0y1y2x3", + "----x0x1y0y1y2x2", + "------x0y0y1x1x2", + "--------y0y1x0x1" + }; + static const char* const PATTERN_D[5] = { + "x0x1x2y1y0y2x3y3", + "--x0x1x2y0y1y2x3", + "----x0x1y0y1y2x2", // Matches main's gfx10_layout.h working hypothesis; GFX10.3 RbPlus x2y2 will be applied to both files together once confirmed by maintainer + "------x0y0x1x2y1", + "--------x0y0x1y1" + }; + const bool display = (swizzle_mode == SW_256B_D || swizzle_mode == SW_4KB_D || swizzle_mode == SW_64KB_D); + const char* pat = display ? PATTERN_D[element_log2] : PATTERN_S[element_log2]; + + std::array x_target_bit{}; + std::array y_target_bit{}; + UINT next_x = 0, next_y = 0; + for (UINT bit = 0; bit < 8; ++bit) { + const char axis = pat[2 * bit]; + const char idx = pat[2 * bit + 1]; + if (axis == 'x') { + const UINT i = static_cast(idx - '0'); + x_target_bit[i] = bit; + if (i + 1 > next_x) next_x = i + 1; + } else if (axis == 'y') { + const UINT i = static_cast(idx - '0'); + y_target_bit[i] = bit; + if (i + 1 > next_y) next_y = i + 1; + } + } + for (UINT bit = 8; bit < block_log2; ++bit) { + if ((bit & 1) == 0) { + y_target_bit[next_y++] = bit; + } else { + x_target_bit[next_x++] = bit; + } + } + + for (UINT x = 0; x < tile_width; ++x) { + UINT val = 0; + for (UINT bit = 0; bit < next_x; ++bit) { + if ((x >> bit) & 1) val |= (1u << x_target_bit[bit]); + } + x_table[x] = val; + } + for (UINT y = 0; y < tile_height; ++y) { + UINT val = 0; + for (UINT bit = 0; bit < next_y; ++bit) { + if ((y >> bit) & 1) val |= (1u << y_target_bit[bit]); + } + y_table[y] = val; + } + + const UINT tail_max_w = tile_width >> 1; + const UINT tail_max_h = tile_height; + const UINT max_in_tail = (block_log2 >= 12) ? (block_log2 - 4) : 0; + first_tail = levels; + + if (block_log2 >= 12 && levels > 1) { + for (UINT i = 0; i < levels; ++i) { + const UINT pw = extent_columns(i); + const UINT ph = extent_rows(i); + if (pw <= tail_max_w && ph <= tail_max_h && (levels - i) <= max_in_tail) { + first_tail = i; + break; + } + } + } + + UINT64 offset = (first_tail < levels) ? tile_bytes : 0; + for (int i = static_cast(first_tail) - 1; i >= 0; --i) { + const UINT cols = extent_columns(static_cast(i)); + const UINT r = extent_rows(static_cast(i)); + tiles_w[i] = (cols + tile_width - 1) / tile_width; + tiles_h[i] = (r + tile_height - 1) / tile_height; + mip_start[i] = offset; + offset += UINT64{tiles_w[i]} * tiles_h[i] * tile_bytes; + } + total_slice_bytes = offset; + + if (first_tail < levels) { + for (UINT i = first_tail; i < levels; ++i) { + const UINT m = max_in_tail - 1 - (i - first_tail); + const UINT tail_offset = (m > 6) ? (16u << m) : (m << 8); + UINT x_idx = 0, y_idx = 0; + for (UINT b = 0; b < 6; ++b) { + y_idx |= ((tail_offset >> (8 + 2 * b)) & 1) << b; + x_idx |= ((tail_offset >> (9 + 2 * b)) & 1) << b; + } + tail_origin_x[i] = x_idx * micro_w; + tail_origin_y[i] = y_idx * micro_h; + mip_start[i] = tail_offset; + } + } + } + + void init_linear() + { + tile_width = 256 / bpe; + tile_height = 1; + tile_bytes = 256; + first_tail = levels; + UINT64 offset = 0; + for (int i = static_cast(levels) - 1; i >= 0; --i) { + const UINT cols = extent_columns(static_cast(i)); + const UINT r = extent_rows(static_cast(i)); + tiles_w[i] = (cols + tile_width - 1) / tile_width; + tiles_h[i] = r; + const UINT64 pitch = UINT64{tiles_w[i]} * 256; + mip_start[i] = offset; + offset += pitch * r; + } + total_slice_bytes = offset; + } + + // Extents for layout allocation and tile strides (AMD addrlib GetMipSize / ShiftCeil) + UINT extent_columns(UINT mip) const + { + const UINT cols0 = (width + block_dim - 1) / block_dim; + return (std::max)(1u, (cols0 + (1u << mip) - 1) >> mip); + } + UINT extent_rows(UINT mip) const + { + const UINT rows0 = (height + block_dim - 1) / block_dim; + return (std::max)(1u, (rows0 + (1u << mip) - 1) >> mip); + } + + // Subresource dimensions in elements for texel copying and bounds checking + UINT columns(UINT mip) const + { + return ((std::max)(1u, width >> mip) + block_dim - 1) / block_dim; + } + UINT rows(UINT mip) const + { + return ((std::max)(1u, height >> mip) + block_dim - 1) / block_dim; + } + UINT tiles_wide(UINT mip) const + { + return (mip < first_tail) ? tiles_w[mip] : (extent_columns(mip) + tile_width - 1) / tile_width; + } + UINT tiles_high(UINT mip) const + { + return (mip < first_tail) ? tiles_h[mip] : (extent_rows(mip) + tile_height - 1) / tile_height; + } + UINT padded_columns(UINT mip) const + { + if (mip >= first_tail) { + const UINT tail_max_w = tile_width >> 1; + return (std::max)(micro_w, tail_max_w >> (mip - first_tail)); + } + return tiles_w[mip] * tile_width; + } + UINT padded_rows(UINT mip) const + { + if (mip >= first_tail) { + const UINT tail_max_h = tile_height; + return (std::max)(micro_h, tail_max_h >> (mip - first_tail)); + } + return tiles_h[mip] * tile_height; + } + + UINT64 bytes() const { return total_slice_bytes * slices; } + UINT64 slice_bytes() const { return total_slice_bytes; } + UINT64 alignment() const { return (block_log2 > 0) ? (1ull << block_log2) : 256; } + UINT64 start(UINT mip) const { return mip_start[mip]; } + + UINT64 address(UINT mip, UINT x, UINT y, UINT slice) const + { + const UINT64 slice_base = UINT64{slice} * total_slice_bytes; + if (swizzle_mode == SW_LINEAR) { + const UINT64 pitch = UINT64{tiles_w[mip]} * 256; + return slice_base + mip_start[mip] + UINT64{y} * pitch + UINT64{x} * bpe; + } + if (mip >= first_tail) { + const UINT tx = tail_origin_x[mip] + x; + const UINT ty = tail_origin_y[mip] + y; + return slice_base + (x_table[tx] | y_table[ty]); + } + const UINT64 tile_idx = UINT64{y / tile_height} * tiles_w[mip] + (x / tile_width); + const UINT within = x_table[x % tile_width] | y_table[y % tile_height]; + return slice_base + mip_start[mip] + tile_idx * tile_bytes + within; + } + + template + void copy_subresource(UINT mip, UINT slice, unsigned char* dst, const unsigned char* src, UINT pitch) const + { + const UINT cols = columns(mip); + const UINT r = rows(mip); + for (UINT y = 0; y < r; ++y) { + const std::size_t linear_row = static_cast(y) * pitch; + for (UINT x = 0; x < cols; ++x) { + const UINT64 tiled = address(mip, x, y, slice); + const std::size_t linear = linear_row + static_cast(x) * bpe; + if constexpr (into) { + std::memcpy(dst + tiled, src + linear, bpe); + } else { + std::memcpy(dst + linear, src + tiled, bpe); + } + } + } + } +}; + +} // namespace x2w_startup diff --git a/src/startup_stubs/xg_bc64.inl b/src/startup_stubs/xg_bc64.inl index 2e4576e20f..2e252a3c04 100644 --- a/src/startup_stubs/xg_bc64.inl +++ b/src/startup_stubs/xg_bc64.inl @@ -1,48 +1,46 @@ -namespace bc64 = x2w_startup::bc64; +u64 __stdcall gfx10_add_ref(Computer* self) { return InterlockedIncrement(&self->refs); } -u64 __stdcall bc64_add_ref(Computer* self) { return InterlockedIncrement(&self->refs); } - -long __stdcall bc64_layout(Computer* self, unsigned char* out) +long __stdcall gfx10_layout(Computer* self, unsigned char* out) { - const bc64::Chain chain(self->desc); if (!out) return E_POINTER; std::memset(out, 0, layout_header + 4 * plane_bytes + 8); - put64(out, chain.bytes()); - put64(out + 8, 65536); - put32(out + 0x10, chain.levels); + const x2w_startup::Gfx10Texture tex(self->desc); + put64(out, tex.bytes()); + put64(out + 8, tex.alignment()); + put32(out + 0x10, tex.levels); put32(out + 0x14, 1); put32(out + 0x1b38, D3D12_RESOURCE_DIMENSION_TEXTURE2D); auto* plane = out + layout_header; put32(plane, 1); - put64(plane + 8, chain.bytes()); - put64(plane + 0x10, chain.slice_bytes()); - put64(plane + 0x20, 65536); - put32(plane + 0x28, 16); - put32(plane + 0x2c, bc64::first_tail); - put32(plane + 0x30, chain.levels - bc64::first_tail); - for (UINT level = 0; level < chain.levels; ++level) { + put64(plane + 8, tex.bytes()); + put64(plane + 0x10, tex.slice_bytes()); + put64(plane + 0x20, tex.alignment()); + put32(plane + 0x28, tex.bpe); + put32(plane + 0x2c, tex.first_tail); + put32(plane + 0x30, tex.levels - tex.first_tail); + for (UINT level = 0; level < tex.levels; ++level) { auto* mip = plane + mip_base + level * mip_bytes; - const UINT columns = chain.columns(level), rows = chain.rows(level); - const bool tail = level >= bc64::first_tail; - const UINT padded_columns = tail ? (std::max)(4u, 256u >> level) : chain.tiles_wide(level) * 64; - const UINT padded_rows = tail ? (std::max)(4u, 512u >> level) : chain.tiles_high(level) * 64; - const UINT span = 16 * padded_columns * padded_rows; + const UINT columns = tex.columns(level), rows = tex.rows(level); + const bool tail = level >= tex.first_tail; + const UINT padded_columns = tex.padded_columns(level); + const UINT padded_rows = tex.padded_rows(level); + const UINT64 span = UINT64{tex.bpe} * padded_columns * padded_rows; put64(mip, span); - put64(mip + 8, chain.start(level)); + put64(mip + 8, tex.start(level)); put64(mip + 0x10, span); - put32(mip + 0x18, 4 * padded_columns); - put32(mip + 0x1c, 16 * padded_columns); - put32(mip + 0x20, tail ? 256 : 65536); + put32(mip + 0x18, tex.block_dim * padded_columns); + put32(mip + 0x1c, tex.bpe * padded_columns); + put32(mip + 0x20, tail ? 256 : static_cast(tex.alignment())); put32(mip + 0x24, padded_columns); put32(mip + 0x28, padded_rows); - put32(mip + 0x2c, chain.slices); + put32(mip + 0x2c, tex.slices); put32(mip + 0x30, columns); put32(mip + 0x34, rows); - put32(mip + 0x38, 1024u >> level); - put32(mip + 0x3c, chain.height >> level); - put32(mip + 0x40, chain.slices); + put32(mip + 0x38, (std::max)(1u, static_cast(self->desc.Width) >> level)); + put32(mip + 0x3c, (std::max)(1u, self->desc.Height >> level)); + put32(mip + 0x40, tex.slices); put32(mip + 0x44, 1); - put32(mip + 0x48, tail ? 1 : 9); + put32(mip + 0x48, tail ? 1 : tex.swizzle_mode); put32(mip + 0x60, 1); put32(mip + 0x64, tail); } @@ -50,60 +48,53 @@ long __stdcall bc64_layout(Computer* self, unsigned char* out) } template -long __stdcall bc64_copy(Computer* self, unsigned char* target, UINT plane, UINT subresource, const unsigned char* source, - UINT pitch, UINT slice_pitch) +long __stdcall gfx10_copy(Computer* self, unsigned char* target, UINT plane, UINT subresource, const unsigned char* source, + UINT pitch, UINT slice_pitch) { - const bc64::Chain chain(self->desc); - if (plane || subresource >= chain.levels * chain.slices || !target || !source) return E_INVALIDARG; - const UINT level = subresource % chain.levels, slice = subresource / chain.levels; - const u64 row_bytes = 16 * chain.columns(level); - const u64 linear_bytes = u64{chain.rows(level) - 1} * pitch + row_bytes; + const x2w_startup::Gfx10Texture tex(self->desc); + if (plane || subresource >= tex.levels * tex.slices || !target || !source) return E_INVALIDARG; + const UINT level = subresource % tex.levels, slice = subresource / tex.levels; + const u64 row_bytes = u64{tex.bpe} * tex.columns(level); + const u64 linear_bytes = u64{tex.rows(level) - 1} * pitch + row_bytes; if (pitch < row_bytes || (slice_pitch && slice_pitch < linear_bytes)) return E_INVALIDARG; const auto dest = reinterpret_cast(target); const auto src = reinterpret_cast(source); - const auto dest_bytes = into ? chain.bytes() : linear_bytes; - const auto src_bytes = into ? linear_bytes : chain.bytes(); + const auto dest_bytes = into ? tex.bytes() : linear_bytes; + const auto src_bytes = into ? linear_bytes : tex.bytes(); if (dest <= src ? src - dest < dest_bytes : dest - src < src_bytes) return E_INVALIDARG; if (!x2w_local::writable(target, dest_bytes) || !x2w_local::readable(source, src_bytes)) return E_FAIL; - chain.copy(level, slice, target, source, pitch); - if constexpr (into) notify_texture_write(target, chain.bytes()); + tex.copy_subresource(level, slice, target, source, pitch); + if constexpr (into) notify_texture_write(target, tex.bytes()); return S_OK; } -long __stdcall bc64_copy_from(Computer* self, unsigned char* target, UINT plane, UINT subresource, - UINT scarlett_option, const unsigned char* source, UINT pitch, UINT slice_pitch) +u64 __stdcall gfx10_size(Computer* self) { return x2w_startup::Gfx10Texture(self->desc).bytes(); } +u64 __stdcall gfx10_alignment(Computer* self) { return x2w_startup::Gfx10Texture(self->desc).alignment(); } +u64 __stdcall gfx10_offset(Computer* self, UINT plane, UINT level) { - // DirectXTex's Scarlett copy-out call passes zero between the subresource and the tiled base. - // Reject unknown options before interpreting the remaining arguments or touching either buffer. - if (scarlett_option != 0) return E_NOTIMPL; - return bc64_copy(self, target, plane, subresource, source, pitch, slice_pitch); -} - -u64 __stdcall bc64_size(Computer* self) { return bc64::Chain(self->desc).bytes(); } -u64 __stdcall bc64_alignment(Computer*) { return 65536; } -u64 __stdcall bc64_offset(Computer* self, UINT plane, UINT level) -{ - const bc64::Chain chain(self->desc); - return !plane && level < chain.levels ? chain.start(level) : 0; + const x2w_startup::Gfx10Texture tex(self->desc); + return !plane && level < tex.levels ? tex.start(level) : 0; } -u64 __stdcall bc64_element(Computer* self, UINT plane, UINT level, u64 x, UINT y, UINT slice, UINT sample, UINT* bit) +u64 __stdcall gfx10_element(Computer* self, UINT plane, UINT level, u64 x, UINT y, UINT slice, UINT sample, UINT* bit) { if (bit) *bit = 0; - const bc64::Chain chain(self->desc); - if (plane || level >= chain.levels || slice >= chain.slices || sample || - x >= chain.columns(level) || y >= chain.rows(level)) return 0; - return chain.address(level, static_cast(x), y, slice); + const x2w_startup::Gfx10Texture tex(self->desc); + if (plane || level >= tex.levels || slice >= tex.slices || sample || + x >= tex.columns(level) || y >= tex.rows(level)) return 0; + return tex.address(level, static_cast(x), y, slice); } -long __stdcall bc64_tiling(Computer* self, UINT* tiles, D3D12_PACKED_MIP_INFO* packed, D3D12_TILE_SHAPE* shape, - UINT* count, UINT first, D3D12_SUBRESOURCE_TILING* tilings) +long __stdcall gfx10_tiling(Computer* self, UINT* tiles, D3D12_PACKED_MIP_INFO* packed, D3D12_TILE_SHAPE* shape, + UINT* count, UINT first, D3D12_SUBRESOURCE_TILING* tilings) { const auto& desc = self->desc; - const bc64::Chain chain(desc); - const UINT slice_tiles = chain.slice_bytes() / bc64::tile_bytes; - if (tiles) *tiles = slice_tiles * desc.DepthOrArraySize; - if (packed) *packed = {static_cast(bc64::first_tail), - static_cast(desc.MipLevels - bc64::first_tail), 1, 0}; - if (shape) *shape = {bc64::tile_side, bc64::tile_side, 1}; + const x2w_startup::Gfx10Texture tex(desc); + const UINT64 slice_tiles = tex.slice_bytes() / tex.tile_bytes; + if (tiles) *tiles = static_cast(slice_tiles * desc.DepthOrArraySize); + if (packed) *packed = {static_cast(tex.first_tail), + static_cast(desc.MipLevels - tex.first_tail), + static_cast(tex.first_tail < desc.MipLevels ? 1 : 0), + 0}; + if (shape) *shape = {tex.tile_width * tex.block_dim, tex.tile_height * tex.block_dim, 1}; if (count) { const UINT subresources = UINT{desc.MipLevels} * desc.DepthOrArraySize; const UINT available = first < subresources ? subresources - first : 0; @@ -112,10 +103,11 @@ long __stdcall bc64_tiling(Computer* self, UINT* tiles, D3D12_PACKED_MIP_INFO* p for (UINT i = 0; i < requested; ++i) { const UINT subresource = first + i; const UINT level = subresource % desc.MipLevels; - if (level >= bc64::first_tail) continue; + if (level >= tex.first_tail) continue; if (tilings) { - const UINT start = (subresource / desc.MipLevels) * slice_tiles + chain.start(level) / bc64::tile_bytes; - tilings[i] = {chain.tiles_wide(level), static_cast(chain.tiles_high(level)), 1, start}; + const UINT start = static_cast((subresource / desc.MipLevels) * slice_tiles + + tex.start(level) / tex.tile_bytes); + tilings[i] = {tex.tiles_wide(level), static_cast(tex.tiles_high(level)), 1, start}; } ++written; } @@ -123,3 +115,33 @@ long __stdcall bc64_tiling(Computer* self, UINT* tiles, D3D12_PACKED_MIP_INFO* p } return S_OK; } + +// Backward-compatible aliases for existing bc64 table references +inline u64 __stdcall bc64_add_ref(Computer* self) { return gfx10_add_ref(self); } +inline long __stdcall bc64_layout(Computer* self, unsigned char* out) { return gfx10_layout(self, out); } +template +long __stdcall bc64_copy(Computer* self, unsigned char* target, UINT plane, UINT subresource, const unsigned char* source, + UINT pitch, UINT slice_pitch) +{ + return gfx10_copy(self, target, plane, subresource, source, pitch, slice_pitch); +} +long __stdcall bc64_copy_from(Computer* self, unsigned char* target, UINT plane, UINT subresource, + UINT scarlett_option, const unsigned char* source, UINT pitch, UINT slice_pitch) +{ + // DirectXTex's Scarlett copy-out call passes zero between the subresource and the tiled base. + // Reject unknown options before interpreting the remaining arguments or touching either buffer. + if (scarlett_option != 0) return E_NOTIMPL; + return bc64_copy(self, target, plane, subresource, source, pitch, slice_pitch); +} +inline u64 __stdcall bc64_size(Computer* self) { return gfx10_size(self); } +inline u64 __stdcall bc64_alignment(Computer* self) { return gfx10_alignment(self); } +inline u64 __stdcall bc64_offset(Computer* self, UINT plane, UINT level) { return gfx10_offset(self, plane, level); } +inline u64 __stdcall bc64_element(Computer* self, UINT plane, UINT level, u64 x, UINT y, UINT slice, UINT sample, UINT* bit) +{ + return gfx10_element(self, plane, level, x, y, slice, sample, bit); +} +inline long __stdcall bc64_tiling(Computer* self, UINT* tiles, D3D12_PACKED_MIP_INFO* packed, D3D12_TILE_SHAPE* shape, + UINT* count, UINT first, D3D12_SUBRESOURCE_TILING* tilings) +{ + return gfx10_tiling(self, tiles, packed, shape, count, first, tilings); +} diff --git a/src/startup_stubs/xg_xs.cpp b/src/startup_stubs/xg_xs.cpp index b639b0d400..d5df7ab937 100644 --- a/src/startup_stubs/xg_xs.cpp +++ b/src/startup_stubs/xg_xs.cpp @@ -13,6 +13,7 @@ #include #include #include "startup_stubs/bc64_chain.h" +#include "startup_stubs/gfx10_texture.h" #include "startup_stubs/tile64_plan.h" #include "startup_stubs/small_texture_4k.h" #include @@ -492,8 +493,8 @@ extern "C" __declspec(noinline) long XGCreateTextureComputer(u64 rcx, u64 rdx, u return slots; }(); const bool bc7_4k = use_bc7_4k_s_layout(desc); - // The 4 KiB profile requires UNKNOWN layout and one mip. BC64 requires layout 0x109 and six or nine mips. - const auto* slots = bc64::supports(desc) ? bc_table.data() : table.data(); + // The 4 KiB profile requires UNKNOWN layout and one mip. Gfx10Texture requires explicit 0x100, 0x101, 0x102, 0x105, 0x106, 0x109, 0x10a (S, D, and linear modes) layout. + const auto* slots = x2w_startup::Gfx10Texture::supports(desc) ? bc_table.data() : table.data(); if (bc7_4k) slots = bc7_4k_table.data(); auto* computer = new Computer{slots, desc, 1, bc7_4k}; *reinterpret_cast(rdx) = computer; diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index f17e687d44..351745b1a9 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -1567,13 +1567,23 @@ int main(int argc, char** argv) static_cast(std::strtoul(argv[8], nullptr, 10)), std::strcmp(argv[9], "tiled") == 0}; 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) && - (bc7.width != 512 || bc7.height != 256)) return 2; - if (std::strncmp(argv[9], "packed:", 7) == 0) bc7.packed = argv[9] + 7; - else if (bc7.small4k) {} - else if (std::strcmp(argv[9], "display64k") == 0) bc7.display64k = true; - else if (!bc7.tiled && std::strcmp(argv[9], "linear") != 0) return 2; + bc7.rgba8_4k_s = std::strcmp(argv[9], "rgba8_4k_s") == 0 || std::strcmp(argv[9], "rgba8_4k_s_untiled") == 0; + bc7.rgba8_linear_3m = std::strcmp(argv[9], "rgba8_linear_3m") == 0 || std::strcmp(argv[9], "rgba8_linear_3m_desktop") == 0; + if (bc7.rgba8_4k_s) { + bc7.tiled = std::strcmp(argv[9], "rgba8_4k_s") == 0; + if (bc7.width != 64 || bc7.height != 64) return 2; + } else if (bc7.rgba8_linear_3m) { + bc7.tiled = std::strcmp(argv[9], "rgba8_linear_3m") == 0; + if (bc7.width != 64 || bc7.height != 64) return 2; + } else if (bc7.small4k) { + if (bc7.width != 32 || bc7.height != 32) return 2; + } else { + if ((bc7.width != 64 || bc7.height != 64) && (bc7.width != 81 || bc7.height != 69) && + (bc7.width != 512 || bc7.height != 256)) return 2; + if (std::strncmp(argv[9], "packed:", 7) == 0) bc7.packed = argv[9] + 7; + else if (std::strcmp(argv[9], "display64k") == 0) bc7.display64k = true; + else if (!bc7.tiled && std::strcmp(argv[9], "linear") != 0) return 2; + } } ComPtr adapter; void* device = nullptr; diff --git a/tests/startup_stream_draw.cpp b/tests/startup_stream_draw.cpp index d59971e41b..9064a3d360 100644 --- a/tests/startup_stream_draw.cpp +++ b/tests/startup_stream_draw.cpp @@ -368,6 +368,86 @@ bool bind_small4k(ID3D12Device* desktop, void* guest, const StartupBc7Input& inp return bind_table(desktop, guest, texture, binding); } +bool bind_rgba8_4k_s(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{}}; + const UINT row_bytes = input.width * 4; + if (texels.size() != static_cast(row_bytes) * input.height) return false; + const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, input.width, input.height, 1, 1, + DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}, static_cast(0x105), 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 span = 0; + HRESULT hr = reinterpret_cast(table[2])(computer, layout.data()); + if (SUCCEEDED(hr)) std::memcpy(&span, layout.data(), sizeof span); + if (span != 16384) hr = E_FAIL; + if (SUCCEEDED(hr)) { + binding.backing.resize(static_cast(span), 0xa5); + if (input.tiled) { + hr = reinterpret_cast(table[8])(computer, binding.backing.data(), 0, 0, texels.data(), + row_bytes, static_cast(texels.size())); + } else { + std::memcpy(binding.backing.data(), texels.data(), (std::min)(binding.backing.size(), texels.size())); + } + } + reinterpret_cast(table[1])(computer); + if (FAILED(hr)) return false; + 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_rgba8_linear_3m(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{}}; + const UINT row_bytes = input.width * 4; + if (texels.size() != static_cast(row_bytes) * input.height) return false; + const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, input.width, input.height, 1, 3, + DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}, static_cast(0x100), 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 span = 0; + HRESULT hr = reinterpret_cast(table[2])(computer, layout.data()); + if (SUCCEEDED(hr)) std::memcpy(&span, layout.data(), sizeof span); + if (span != 28672) hr = E_FAIL; + if (SUCCEEDED(hr)) { + binding.backing.resize(static_cast(span), 0xa5); + if (input.tiled) { + hr = reinterpret_cast(table[8])(computer, binding.backing.data(), 0, 0, texels.data(), + row_bytes, static_cast(texels.size())); + if (SUCCEEDED(hr)) { + std::vector mip1(32 * 32 * 4, 0x7f); + hr = reinterpret_cast(table[8])(computer, binding.backing.data(), 0, 1, mip1.data(), + 32 * 4, static_cast(mip1.size())); + } + if (SUCCEEDED(hr)) { + std::vector mip2(16 * 16 * 4, 0x3f); + hr = reinterpret_cast(table[8])(computer, binding.backing.data(), 0, 2, mip2.data(), + 16 * 4, static_cast(mip2.size())); + } + } else { + std::memcpy(binding.backing.data(), texels.data(), (std::min)(binding.backing.size(), texels.size())); + } + } + reinterpret_cast(table[1])(computer); + if (FAILED(hr)) return false; + 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); +} + // 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) { @@ -413,6 +493,10 @@ int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* 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 && bc7->rgba8_4k_s) { + if (!bind_rgba8_4k_s(desktop.Get(), guest, *bc7, binding)) return 36; + } else if (bc7 && bc7->rgba8_linear_3m) { + if (!bind_rgba8_linear_3m(desktop.Get(), guest, *bc7, binding)) return 37; } 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 e8e845addf..e0bbca4766 100644 --- a/tests/startup_stream_draw.h +++ b/tests/startup_stream_draw.h @@ -17,6 +17,13 @@ struct StartupBc7Input { // 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; + // A 64x64 R8G8B8A8 texture with explicit 4KB_S layout (0x105). When tiled is true, data is tiled + // via XG slot 8; when false, raw linear bytes are placed as a negative swizzle control. + bool rgba8_4k_s = false; + // A 64x64 R8G8B8A8 texture with 3 mips and explicit linear layout (0x100). When tiled is true, + // data is placed via XG slot 8 (smallest mip first: mips 2, 1, 0); when false, data is placed + // at offset 0 in desktop footprint order as a negative layout control. + bool rgba8_linear_3m = false; bool display64k = false; }; diff --git a/tests/startup_xg_bc7_draw_check.py b/tests/startup_xg_bc7_draw_check.py index 5b4f3929a1..68164f08eb 100644 --- a/tests/startup_xg_bc7_draw_check.py +++ b/tests/startup_xg_bc7_draw_check.py @@ -97,6 +97,81 @@ def check(work, consumer, dxc, backend): f"read {actual[first:first + 4].hex()}, expected {reference[first:first + 4].hex()}") else: print(f"{backend}: {width}x{height} {layout} draw matched all {width * height} reference pixels") + + # Non-BC upload test (RGBA8 layout 0x105, 4KB_S) and negative swizzle control. + rgba8_reference = bytearray(64 * 64 * 4) + for y in range(64): + for x in range(64): + idx = (y * 64 + x) * 4 + rgba8_reference[idx:idx + 4] = bytes(((x * 4 + 7) & 255, (y * 4 + 13) & 255, (x ^ y ^ 0xA5) & 255, 255)) + upload.write_bytes(rgba8_reference) + + # 1. Tiled upload via XG slot 8 into 0x105, detiled by upload_texture via is_gfx10_tiled. + if image.exists(): + image.unlink() + result = subprocess.run(base + ["64", "64", "rgba8_4k_s"], 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: + print(line) + if result.returncode or "-> drawn;" not in result.stderr: + raise RuntimeError(f"{backend} 64x64 rgba8_4k_s draw failed ({result.returncode})\n{result.stderr}") + actual = image.read_bytes() + assert len(actual) == len(rgba8_reference), f"rgba8_4k_s: read {len(actual)} bytes, expected {len(rgba8_reference)}" + if actual != rgba8_reference: + first = next(i for i in range(0, len(rgba8_reference), 4) if actual[i:i + 4] != rgba8_reference[i:i + 4]) + x, y = first // 4 % 64, first // 4 // 64 + mismatches.append(f"{backend} 64x64 rgba8_4k_s: pixel ({x}, {y}) " + f"read {actual[first:first + 4].hex()}, expected {rgba8_reference[first:first + 4].hex()}") + else: + print(f"{backend}: 64x64 rgba8_4k_s draw matched all {64 * 64} reference pixels") + + # 2. Negative control: linear / untiled data placed in 0x105 buffer must fail to match reference. + if image.exists(): + image.unlink() + result = subprocess.run(base + ["64", "64", "rgba8_4k_s_untiled"], env=env, capture_output=True, text=True) + if result.returncode or "-> drawn;" not in result.stderr: + raise RuntimeError(f"{backend} 64x64 rgba8_4k_s_untiled draw failed ({result.returncode})\n{result.stderr}") + actual_untiled = image.read_bytes() + assert actual_untiled != rgba8_reference, f"{backend}: negative control failed, untiled draw unexpectedly matched reference" + diff_count = sum(actual_untiled[i:i + 4] != rgba8_reference[i:i + 4] for i in range(0, len(rgba8_reference), 4)) + assert diff_count >= 3000, f"{backend}: negative control only differed on {diff_count}/4096 pixels" + print(f"{backend}: 64x64 rgba8_4k_s negative swizzle control confirmed ({diff_count}/4096 pixels differ)") + + # 3. Multi-mip linear upload (RGBA8 64x64 3-mip 0x100) placed smallest-first by XG slot 8, + # detiled/uploaded by upload_texture via is_gfx10_tiled. + if image.exists(): + image.unlink() + result = subprocess.run(base + ["64", "64", "rgba8_linear_3m"], 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: + print(line) + if result.returncode or "-> drawn;" not in result.stderr: + raise RuntimeError(f"{backend} 64x64 rgba8_linear_3m draw failed ({result.returncode})\n{result.stderr}") + actual_linear_3m = image.read_bytes() + assert len(actual_linear_3m) == len(rgba8_reference), f"rgba8_linear_3m: read {len(actual_linear_3m)} bytes, expected {len(rgba8_reference)}" + if actual_linear_3m != rgba8_reference: + first = next(i for i in range(0, len(rgba8_reference), 4) if actual_linear_3m[i:i + 4] != rgba8_reference[i:i + 4]) + x, y = first // 4 % 64, first // 4 // 64 + mismatches.append(f"{backend} 64x64 rgba8_linear_3m: pixel ({x}, {y}) " + f"read {actual_linear_3m[first:first + 4].hex()}, expected {rgba8_reference[first:first + 4].hex()}") + else: + print(f"{backend}: 64x64 rgba8_linear_3m draw matched all {64 * 64} reference pixels") + + # 4. Negative control: linear 3-mip texture placed in standard desktop footprint order (offset 0 for mip 0) + # must fail to match reference because upload_texture reads mip 0 from XG's smallest-first offset 12288. + if image.exists(): + image.unlink() + result = subprocess.run(base + ["64", "64", "rgba8_linear_3m_desktop"], env=env, capture_output=True, text=True) + if result.returncode or "-> drawn;" not in result.stderr: + raise RuntimeError(f"{backend} 64x64 rgba8_linear_3m_desktop draw failed ({result.returncode})\n{result.stderr}") + actual_linear_desktop = image.read_bytes() + assert actual_linear_desktop != rgba8_reference, f"{backend}: negative control failed, desktop layout unexpectedly matched reference" + diff_count = sum(actual_linear_desktop[i:i + 4] != rgba8_reference[i:i + 4] for i in range(0, len(rgba8_reference), 4)) + assert diff_count >= 1000, f"{backend}: negative control only differed on {diff_count}/4096 pixels" + print(f"{backend}: 64x64 rgba8_linear_3m negative layout control confirmed ({diff_count}/4096 pixels differ)") + if mismatches: raise AssertionError("\n".join(mismatches)) return 0 diff --git a/tests/startup_xg_gfx10_check.py b/tests/startup_xg_gfx10_check.py new file mode 100644 index 0000000000..6182db6dae --- /dev/null +++ b/tests/startup_xg_gfx10_check.py @@ -0,0 +1,510 @@ +"""Verify GFX10 texture computer, micro-tile detiling, layout records and implicit-layout gating.""" +import ctypes as ct +import struct +import sys + +DESC = struct.Struct(" 0, f"{name}: size must be > 0" + assert alignment == expected_align, f"{name}: align={alignment}, expected={expected_align}" + + # Slot 2 (layout) + layout_buf = ct.create_string_buffer(LAYOUT_BYTES) + assert LAYOUT(slots[2])(computer, layout_buf) == 0 + l_size, l_align, l_mips, l_planes = struct.unpack_from(" 0 and tile_h_px > 0 and tile_d == 1, f"{name}: invalid tile shape {tuple(shape)}" + if is_linear: + assert tiles.value * 256 == total_size, f"{name}: linear tiles {tiles.value} * 256 != {total_size}" + else: + assert tiles.value * alignment == total_size, f"{name}: tiles {tiles.value} * {alignment} != {total_size}" + + # Verify Slot 5 (offset) and Slot 2 per-mip records + mip_offsets = [] + for m in range(mips): + mip_off = plane_off + 0x38 + m * 0x70 + span, offset, slice_span, pitch_px, pitch_bytes, mip_align, padw, padh = struct.unpack_from( + MIP_HEADER_FMT, layout_buf, mip_off + ) + m_slices, cols, rows, mip_w, mip_h, _ = struct.unpack_from(MIP_EXT_FMT, layout_buf, mip_off + 0x2C) + tail_flag = struct.unpack_from("> m) + block_dim - 1) // block_dim + expected_rows = (max(1, height >> m) + block_dim - 1) // block_dim + assert cols == expected_cols and rows == expected_rows, f"{name} mip {m}: cols/rows {(cols, rows)}" + assert mip_w == max(1, width >> m) and mip_h == max(1, height >> m), f"{name} mip {m}: ext" + assert m_slices == slices, f"{name} mip {m}: slices {m_slices} != {slices}" + assert padw >= cols and padh >= rows, f"{name} mip {m}: padw={padw}, padh={padh}" + assert pitch_px == block_dim * padw, f"{name} mip {m}: pitch_px {pitch_px}" + assert pitch_bytes == bpe * padw, f"{name} mip {m}: pitch_bytes {pitch_bytes}" + assert span == bpe * padw * padh and slice_span == span, f"{name} mip {m}: span {span}" + if is_linear: + assert pitch_bytes % 256 == 0 and pitch_bytes >= cols * bpe, f"{name} mip {m}: linear pitch {pitch_bytes}" + assert mip_align == 256 and tail_flag == 0 + else: + expected_tail = 1 if m >= p_std else 0 + assert tail_flag == expected_tail, f"{name} mip {m}: tail_flag {tail_flag}" + assert mip_align == (256 if tail_flag else alignment), f"{name} mip {m}: mip_align {mip_align}" + + # Slot 5: GetSubresourceOffset matches layout mip offset + slot5_off = OFFSET(slots[5])(computer, 0, m) + assert slot5_off == offset, f"{name} mip {m}: slot5 {slot5_off} != layout {offset}" + assert offset < p_stride, f"{name} mip {m}: offset {offset} >= slice stride {p_stride}" + mip_offsets.append(offset) + + if m < num_standard: + tw, th, td, start_tile = ENTRY.unpack_from(entries, m * ENTRY.size) + assert tw > 0 and th > 0 and td == 1, f"{name} mip {m}: bad tiling entry {(tw, th, td, start_tile)}" + assert start_tile * (256 if is_linear else alignment) == offset, ( + f"{name} mip {m}: start_tile={start_tile} != offset={offset}" + ) + + assert OFFSET(slots[5])(computer, 1, 0) == 0, f"{name}: out-of-range plane offset should be 0" + assert OFFSET(slots[5])(computer, 0, mips) == 0, f"{name}: out-of-range mip offset should be 0" + + # Test subresource tile & detile roundtrip + slot 6 element address consistency + tiled_buf = ct.create_string_buffer(total_size) + for s in range(slices): + for m in range(mips): + mw = (max(1, width >> m) + block_dim - 1) // block_dim + mh = (max(1, height >> m) + block_dim - 1) // block_dim + row_pitch = mw * bpe + 16 # padded pitch + + linear_src = bytearray(mh * row_pitch) + for y in range(mh): + for x in range(mw): + for b in range(bpe): + linear_src[y * row_pitch + x * bpe + b] = ( + (m * 41 + s * 17 + x * 7 + y * 13 + b) & 0xFF + ) + + src_buf = ct.create_string_buffer(bytes(linear_src), len(linear_src)) + subresource = s * mips + m + # Slot 8: CopyIntoSubresource (linear -> tiled) + rc = COPY_INTO(slots[8])(computer, tiled_buf, 0, subresource, src_buf, row_pitch, len(linear_src)) + assert rc == 0, f"{name} mip {m} CopyIntoSubresource failed: 0x{rc & 0xFFFFFFFF:08x}" + + # Slot 9: CopyFromSubresource (tiled -> linear) + dest_buf = ct.create_string_buffer(b"\xaa" * len(linear_src), len(linear_src)) + copy_from_slice_pitch = 0 if (m & 1) else len(linear_src) + rc = COPY_FROM(slots[9])(computer, dest_buf, 0, subresource, 0, tiled_buf, row_pitch, copy_from_slice_pitch) + assert rc == 0, f"{name} mip {m} CopyFromSubresource failed: 0x{rc & 0xFFFFFFFF:08x}" + + for y in range(mh): + row_orig = linear_src[y * row_pitch: y * row_pitch + mw * bpe] + row_detiled = dest_buf.raw[y * row_pitch: y * row_pitch + mw * bpe] + assert row_detiled == row_orig, f"{name} slice {s} mip {m} row {y} mismatch after detile!" + + # Slot 6: verify first_addr, bounds, and exact byte match with Slot 8 output + bit = UINT(99) + first_addr = ELEMENT(slots[6])(computer, 0, m, 0, 0, s, 0, ct.byref(bit)) + assert bit.value == 0 + expected_first_min = s * p_stride + mip_offsets[m] + assert first_addr == expected_first_min or (m >= p_std and first_addr >= s * p_stride), ( + f"{name} s={s} m={m}: first_addr={first_addr} vs expected={expected_first_min}" + ) + assert first_addr + bpe <= total_size + sample_coords = [(0, 0), (mw - 1, mh - 1)] + if mw > 1: + sample_coords.append((1, 0)) + if mh > 1: + sample_coords.append((0, 1)) + sample_coords = list(dict.fromkeys(sample_coords)) + seen_addrs = set() + for sx, sy in sample_coords: + addr = ELEMENT(slots[6])(computer, 0, m, sx, sy, s, 0, ct.byref(bit)) + assert bit.value == 0 + assert addr + bpe <= total_size, f"{name}: addr {addr} out of bounds" + assert addr not in seen_addrs, f"{name}: duplicate addr {addr} at ({sx},{sy})" + seen_addrs.add(addr) + expected_elem = bytes(linear_src[sy * row_pitch + sx * bpe: sy * row_pitch + (sx + 1) * bpe]) + actual_elem = tiled_buf.raw[addr: addr + bpe] + assert actual_elem == expected_elem, ( + f"{name} s={s} m={m} ({sx},{sy}): slot6 addr 0x{addr:x} mismatch with slot8" + ) + + finally: + RELEASE(slots[1])(computer) + + +def test_implicit_and_unsupported_routing(create): + """Implicit layouts (UNKNOWN=0, ROW_MAJOR=1) and depth formats must stay on the desktop footprint path.""" + for name, fmt, w, h, layout_code in ( + ("RGBA8 64x64 UNKNOWN", 28, 64, 64, 0), + ("R8 64x64 UNKNOWN", 61, 64, 64, 0), + ("RG8 64x64 UNKNOWN", 49, 64, 64, 0), + ("BC3 64x64 UNKNOWN", 77, 64, 64, 0), + ("BC7 64x64 UNKNOWN", 98, 64, 64, 0), + ("RGBA8 64x64 ROW_MAJOR", 28, 64, 64, 1), + ("D24S8 64x64 0x109", 45, 64, 64, 0x109), + ("D32F 64x64 0x109", 40, 64, 64, 0x109), + ): + desc = ct.create_string_buffer(DESC.pack(3, 0, w, h, 1, 1, fmt, 1, 0, layout_code, 0)) + computer = PTR() + hr = create(desc, ct.byref(computer), 0, 0) + assert hr == 0 and computer, f"{name}: XGCreateTextureComputer failed: 0x{hr & 0xFFFFFFFF:08x}" + slots = ct.cast(computer, ct.POINTER(ct.POINTER(PTR)))[0] + try: + # Desktop table leaves slots 3, 5, 6, 9 as no-op stubs returning 0 (unlike Gfx10 bc_table) + assert SIZE(slots[3])(computer) == 0, f"{name}: unexpectedly routed to GFX10 slot 3" + assert OFFSET(slots[5])(computer, 0, 0) == 0, f"{name}: unexpectedly routed to GFX10 slot 5" + bit = UINT(99) + assert ELEMENT(slots[6])(computer, 0, 0, 0, 0, 0, 0, ct.byref(bit)) == 0 + # Slot 9 (CopyFromSubresource) on Gfx10 returns E_INVALIDARG for null/zero pitch; desktop no-op returns 0 + assert COPY_FROM(slots[9])(computer, None, 0, 0, 0, None, 0, 0) == 0, ( + f"{name}: unexpectedly routed to GFX10 slot 9" + ) + if fmt == 28 and layout_code == 0: + layout_buf = ct.create_string_buffer(LAYOUT_BYTES) + assert LAYOUT(slots[2])(computer, layout_buf) == 0 + l_size, l_align, l_mips, l_planes = struct.unpack_from(" E_NOTIMPL without touching dest + sentinel = b"\xad" * len(linear_src) + refused_buf = ct.create_string_buffer(sentinel, len(sentinel)) + for opt in (1, 0xFFFFFFFF): + rc = COPY_FROM(slots[9])(computer, refused_buf, 0, 0, opt, tiled_buf, row_pitch, 0) + assert (rc & 0xFFFFFFFF) == 0x80004001, f"opt={opt}: expected E_NOTIMPL, got 0x{rc & 0xFFFFFFFF:08x}" + assert refused_buf.raw == sentinel, "unsupported copy-out option changed destination" + rc = COPY_FROM(slots[9])(computer, None, 0, 1, 1, None, 0, 0) + assert (rc & 0xFFFFFFFF) == 0x80004001, "non-zero option must fail with E_NOTIMPL before arg checks" + + finally: + RELEASE(slots[1])(computer) + + # Also verify non-zero scarlett_option refusal on an uncompressed GFX10 surface (RGBA8 64x64 4KB_S) + desc_rgba = ct.create_string_buffer(DESC.pack(3, 0, 64, 64, 1, 1, 28, 1, 0, 0x105, 0)) + comp_rgba = PTR() + hr = create(desc_rgba, ct.byref(comp_rgba), 0, 0) + assert hr == 0 and comp_rgba, "Failed to create RGBA8 computer for slot 9 option check" + s_rgba = ct.cast(comp_rgba, ct.POINTER(ct.POINTER(PTR)))[0] + try: + rgba_size = SIZE(s_rgba[3])(comp_rgba) + rgba_tiled = ct.create_string_buffer(rgba_size) + rgba_sentinel = b"\xad" * (64 * 64 * 4) + rgba_refused = ct.create_string_buffer(rgba_sentinel, len(rgba_sentinel)) + rc = COPY_FROM(s_rgba[9])(comp_rgba, rgba_refused, 0, 0, 1, rgba_tiled, 64 * 4, 0) + assert (rc & 0xFFFFFFFF) == 0x80004001 + assert rgba_refused.raw == rgba_sentinel, "RGBA8 unsupported copy-out option changed destination" + finally: + RELEASE(s_rgba[1])(comp_rgba) + + # 8. Descriptor with MipLevels > 16 rejected from GFX10 tiling (falls back to desktop path) + desc_over_mips = ct.create_string_buffer( + DESC.pack(3, 0, 64, 64, 1, 17, 98, 1, 0, 0x109, 0) + ) + comp_over = PTR() + hr = create(desc_over_mips, ct.byref(comp_over), 0, 0) + if hr == 0 and comp_over: + s = ct.cast(comp_over, ct.POINTER(ct.POINTER(PTR)))[0] + try: + assert SIZE(s[3])(comp_over) == 0, "MipLevels=17 unexpectedly routed to GFX10 slot 3" + assert COPY_FROM(s[9])(comp_over, None, 0, 0, 0, None, 0, 0) == 0, ( + "MipLevels=17 unexpectedly routed to GFX10 slot 9" + ) + layout_buf = ct.create_string_buffer(LAYOUT_BYTES) + assert LAYOUT(s[2])(comp_over, layout_buf) == 0 + l_mips = struct.unpack_from(" 16)") + + +def test_pinned_addrlib_offsets(create): + """Pin expected element byte offsets per element size (1, 2, 4, 8, 16) from Mesa 25.1.0 addrlib.""" + # Offsets collected from xg_gfx10_tiling_reference (AMD addrlib, navi21) so any swizzle bit + # permutation (such as swapping PATTERN_S/PATTERN_D bits or mip-tail origins) fails even when + # addrlib is not configured in the build. + pinned_cases = [ + # bpe=1 (_S and _D) + ("R8 256x256 4-mip 64KB_S", 61, 256, 256, 4, 0x109, 1, 1, { + (0, 0, 0): 0x10000, (0, 1, 0): 0x10001, (0, 2, 0): 0x10002, (0, 4, 0): 0x10004, + (0, 8, 0): 0x10008, (0, 16, 0): 0x10200, + (0, 0, 1): 0x10010, (0, 0, 2): 0x10020, (0, 0, 4): 0x10040, (0, 0, 8): 0x10080, + (0, 0, 16): 0x10100, + }), + ("R8 64x64 1-mip 4KB_D", 61, 64, 64, 1, 0x106, 1, 1, { + (0, 0, 0): 0x0, (0, 1, 0): 0x1, (0, 2, 0): 0x2, (0, 4, 0): 0x4, (0, 8, 0): 0x40, + (0, 0, 1): 0x10, (0, 0, 2): 0x8, (0, 0, 4): 0x20, (0, 0, 8): 0x80, + }), + # bpe=2 (_S and _D) + ("R16 64x64 3-mip 4KB_S", 56, 64, 64, 3, 0x105, 2, 1, { + (0, 0, 0): 0x1000, (0, 1, 0): 0x1002, (0, 2, 0): 0x1004, (0, 4, 0): 0x1008, + (0, 8, 0): 0x1080, + (0, 0, 1): 0x1010, (0, 0, 2): 0x1020, (0, 0, 4): 0x1040, (0, 0, 8): 0x1100, + }), + ("RG8 64x64 3-mip 4KB_D", 49, 64, 64, 3, 0x106, 2, 1, { + (0, 0, 0): 0x1000, (0, 1, 0): 0x1002, (0, 2, 0): 0x1004, (0, 4, 0): 0x1008, + (0, 8, 0): 0x1080, + (0, 0, 1): 0x1010, (0, 0, 2): 0x1020, (0, 0, 4): 0x1040, + }), + # bpe=4 (_S and _D) + ("BGRA8 64x64 3-mip 4KB_S", 87, 64, 64, 3, 0x105, 4, 1, { + (0, 0, 0): 0x2000, (0, 1, 0): 0x2004, (0, 2, 0): 0x2008, (0, 4, 0): 0x2080, + (0, 8, 0): 0x2200, + (0, 0, 1): 0x2010, (0, 0, 2): 0x2020, (0, 0, 4): 0x2040, (0, 0, 8): 0x2100, + }), + ("RGBA8 128x128 4-mip 64KB_S", 28, 128, 128, 4, 0x109, 4, 1, { + (0, 0, 0): 0x10000, (0, 1, 0): 0x10004, (0, 2, 0): 0x10008, (0, 4, 0): 0x10080, + (0, 8, 0): 0x10200, + (0, 0, 1): 0x10010, (0, 0, 2): 0x10020, (0, 0, 4): 0x10040, (0, 0, 8): 0x10100, + }), + ("R32 64x64 3-mip 256B_D", 41, 64, 64, 3, 0x102, 4, 1, { + (0, 0, 0): 0x1400, (0, 1, 0): 0x1404, (0, 2, 0): 0x1408, (0, 4, 0): 0x1480, + (0, 0, 1): 0x1410, (0, 0, 2): 0x1420, (0, 0, 4): 0x1440, + }), + # bpe=8 (_S and _D) + ("BC1 256x256 5-mip 64KB_S", 71, 256, 256, 5, 0x109, 8, 4, { + (0, 0, 0): 0x8000, (0, 1, 0): 0x8008, (0, 2, 0): 0x8040, (0, 4, 0): 0x8080, + (0, 8, 0): 0x8200, + (0, 0, 1): 0x8010, (0, 0, 2): 0x8020, (0, 0, 4): 0x8100, + }), + ("RGBA16 64x64 3-mip 64KB_D", 10, 64, 64, 3, 0x10A, 8, 1, { + (0, 0, 0): 0x8000, (0, 1, 0): 0x8008, (0, 2, 0): 0x8020, (0, 4, 0): 0x8040, + (0, 0, 1): 0x8010, (0, 0, 2): 0x8080, (0, 0, 4): 0x8100, + }), + ("RGBA16 100x10 3-mip LINEAR", 10, 100, 10, 3, 0x100, 8, 1, { + (0, 0, 0): 3328, (0, 3, 2): 3328 + 2 * 1024 + 3 * 8, + (1, 0, 0): 768, (1, 3, 2): 768 + 2 * 512 + 3 * 8, + (2, 0, 0): 0, (2, 3, 1): 0 + 1 * 256 + 3 * 8, + }), + # bpe=16 (_S, _D, and mip tail) + ("RGBA32 32x32 3-mip 4KB_S", 2, 32, 32, 3, 0x105, 16, 1, { + (0, 0, 0): 0x2000, (0, 1, 0): 0x2040, (0, 2, 0): 0x2080, (0, 4, 0): 0x2200, + (0, 0, 1): 0x2010, (0, 0, 2): 0x2020, (0, 0, 4): 0x2100, + }), + ("BC5 256x256 4-mip 64KB_D", 83, 256, 256, 4, 0x10A, 16, 4, { + (0, 0, 0): 0x10000, (0, 1, 0): 0x10010, (0, 2, 0): 0x10040, (0, 4, 0): 0x10200, + (0, 0, 1): 0x10020, (0, 0, 2): 0x10080, (0, 0, 4): 0x10100, + }), + ("BC7 64x64 7-mip 4KB_S", 98, 64, 64, 7, 0x105, 16, 4, { + (0, 0, 0): 0x1000, (0, 1, 0): 0x1040, (0, 2, 0): 0x1080, + (0, 0, 1): 0x1010, (0, 0, 2): 0x1020, + (2, 0, 0): 0x600, (2, 1, 0): 0x640, (2, 0, 1): 0x610, + (3, 0, 0): 0x500, (3, 1, 0): 0x540, (3, 0, 1): 0x510, + (4, 0, 0): 0x400, (5, 0, 0): 0x300, (6, 0, 0): 0x200, + }), + ] + + for name, fmt, w, h, mips, layout, bpe, bdim, expected_map in pinned_cases: + desc = ct.create_string_buffer(DESC.pack(3, 0, w, h, 1, mips, fmt, 1, 0, layout, 0)) + computer = PTR() + hr = create(desc, ct.byref(computer), 0, 0) + assert hr == 0 and computer, f"{name}: create failed" + slots = ct.cast(computer, ct.POINTER(ct.POINTER(PTR)))[0] + try: + total_size = SIZE(slots[3])(computer) + tiled_buf = ct.create_string_buffer(total_size) + bit = UINT(0) + by_mip = {} + for (mip, x, y), exp_addr in expected_map.items(): + got_addr = ELEMENT(slots[6])(computer, 0, mip, x, y, 0, 0, ct.byref(bit)) + assert bit.value == 0 and got_addr == exp_addr, ( + f"{name} mip {mip} ({x},{y}): slot6 addr=0x{got_addr:x}, expected 0x{exp_addr:x}" + ) + by_mip.setdefault(mip, []).append((x, y, exp_addr)) + + for mip, coords in by_mip.items(): + mw = (max(1, w >> mip) + bdim - 1) // bdim + mh = (max(1, h >> mip) + bdim - 1) // bdim + row_pitch = mw * bpe + linear_src = bytearray(mh * row_pitch) + for y in range(mh): + for x in range(mw): + for b in range(bpe): + linear_src[y * row_pitch + x * bpe + b] = ((mip * 31 + x * 13 + y * 29 + b) & 0xFF) + src_buf = ct.create_string_buffer(bytes(linear_src), len(linear_src)) + rc = COPY_INTO(slots[8])(computer, tiled_buf, 0, mip, src_buf, row_pitch, len(linear_src)) + assert rc == 0, f"{name} mip {mip}: CopyIntoSubresource failed" + for x, y, exp_addr in coords: + expected_elem = bytes(linear_src[y * row_pitch + x * bpe: y * row_pitch + (x + 1) * bpe]) + actual_elem = tiled_buf.raw[exp_addr: exp_addr + bpe] + assert actual_elem == expected_elem, ( + f"{name} mip {mip} ({x},{y}): slot8 wrote wrong bytes at pinned offset 0x{exp_addr:x}" + ) + finally: + RELEASE(slots[1])(computer) + print(f" Pinned addrlib offsets: PASS ({len(pinned_cases)} surfaces across bpe 1/2/4/8/16)") + + +def main(dll_path): + create = ct.WinDLL(dll_path).XGCreateTextureComputer + create.argtypes = (PTR, ct.POINTER(PTR), ct.c_uint64, ct.c_uint64) + create.restype = HRESULT + + cases = [ + ("BC1 256x256 5-mip 64KB_S", 71, 256, 256, 5, 1, 0x109, 8, 4), + ("BC1 128x128 4-mip 4KB_S", 71, 128, 128, 4, 1, 0x105, 8, 4), + ("BC2 128x128 3-mip 4KB_S", 74, 128, 128, 3, 1, 0x105, 16, 4), + ("BC3 128x128 3-mip 4KB_S", 77, 128, 128, 3, 1, 0x105, 16, 4), + ("BC4 512x256 6-mip 64KB_S", 80, 512, 256, 6, 1, 0x109, 8, 4), + ("BC5 256x256 4-mip 64KB_D", 83, 256, 256, 4, 1, 0x10A, 16, 4), + ("BC6H 256x256 4-mip 64KB_S", 95, 256, 256, 4, 1, 0x109, 16, 4), + ("BC7 64x64 7-mip 4KB_S", 98, 64, 64, 7, 1, 0x105, 16, 4), + ("BC7 81x69 1-mip 4KB_D", 98, 81, 69, 1, 1, 0x106, 16, 4), + ("BC7 128x128 3-mip 2-slice 64KB_S", 98, 128, 128, 3, 2, 0x109, 16, 4), + ("BC7 128x64 8-mip 4KB_S", 98, 128, 64, 8, 1, 0x105, 16, 4), + ("R8 256x256 4-mip 64KB_S", 61, 256, 256, 4, 1, 0x109, 1, 1), + ("R8 64x64 1-mip 256B_S", 61, 64, 64, 1, 1, 0x101, 1, 1), + ("R8 64x64 1-mip 4KB_D", 61, 64, 64, 1, 1, 0x106, 1, 1), + ("RG8 64x64 3-mip 4KB_D", 49, 64, 64, 3, 1, 0x106, 2, 1), + ("R16 64x64 3-mip 4KB_S", 56, 64, 64, 3, 1, 0x105, 2, 1), + ("R32 64x64 3-mip 256B_D", 41, 64, 64, 3, 1, 0x102, 4, 1), + ("RGBA8 128x128 4-mip 64KB_S", 28, 128, 128, 4, 1, 0x109, 4, 1), + ("BGRA8 64x64 3-mip 4KB_S", 87, 64, 64, 3, 1, 0x105, 4, 1), + ("RGBA16 64x64 3-mip 64KB_D", 10, 64, 64, 3, 1, 0x10A, 8, 1), + ("RGBA32 32x32 3-mip 4KB_S", 2, 32, 32, 3, 1, 0x105, 16, 1), + ("RGBA8 64x64 1-mip LINEAR", 28, 64, 64, 1, 1, 0x100, 4, 1), + ("RGBA8 256x256 3-mip LINEAR", 28, 256, 256, 3, 1, 0x100, 4, 1), + ("RGBA16 100x10 3-mip 2-slice LINEAR", 10, 100, 10, 3, 2, 0x100, 8, 1), + ] + + for name, fmt, w, h, mips, slices, layout, bpe, bdim in cases: + test_format(create, name, fmt, w, h, mips, slices, layout, bpe, bdim) + print(f" {name}: PASS (layout, offset, element, tiling, slot 8 tile, slot 9 detile exact)") + + test_pinned_addrlib_offsets(create) + test_implicit_and_unsupported_routing(create) + test_negative_controls(create) + + print(f"All {len(cases)} GFX10 texture computer test cases, implicit routing checks and negative controls passed!") + return 0 + + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: startup_xg_gfx10_check.py ") + sys.exit(2) + sys.exit(main(sys.argv[1])) diff --git a/tests/xg_swizzle_select_check.cpp b/tests/xg_swizzle_select_check.cpp index 8f95b4ae0d..212578d726 100644 --- a/tests/xg_swizzle_select_check.cpp +++ b/tests/xg_swizzle_select_check.cpp @@ -53,6 +53,7 @@ int main() #include "addrinterface.h" #include "../src/startup_stubs/bc64_chain.h" +#include "../src/startup_stubs/gfx10_texture.h" namespace { // Mesa ac_fake_hw_db.h "navi21": family_id AMDGPU_FAMILY_NV, chip_external_rev 0x28, gb_addr_config 0x40 with @@ -542,6 +543,146 @@ int bc64_tiling_reference() return failures ? 1 : 0; } +int gfx10_tiling_reference() +{ + ADDR_HANDLE lib = create_navi21(); + if (!lib) return 1; + struct Gfx10Case { + const char* name; + DXGI_FORMAT dxgi; + AddrFormat addr_fmt; + UINT_32 bpp; + UINT_32 width, height, mips, slices; + UINT layout; + AddrSwizzleMode mode; + }; + const Gfx10Case cases[] = { + {"BC1 256x256 5m 64KB_S", DXGI_FORMAT_BC1_UNORM, ADDR_FMT_BC1, 64, 256, 256, 5, 1, 0x109, ADDR_SW_64KB_S}, + {"BC1 128x128 4m 4KB_S", DXGI_FORMAT_BC1_UNORM, ADDR_FMT_BC1, 64, 128, 128, 4, 1, 0x105, ADDR_SW_4KB_S}, + {"BC2 128x128 4m 4KB_S", DXGI_FORMAT_BC2_UNORM, ADDR_FMT_BC2, 128, 128, 128, 4, 1, 0x105, ADDR_SW_4KB_S}, + {"BC3 128x128 5m 64KB_D", DXGI_FORMAT_BC3_UNORM, ADDR_FMT_BC3, 128, 128, 128, 5, 1, 0x10A, ADDR_SW_64KB_D}, + {"BC4 256x128 6m 64KB_S", DXGI_FORMAT_BC4_UNORM, ADDR_FMT_BC4, 64, 256, 128, 6, 1, 0x109, ADDR_SW_64KB_S}, + {"BC5 256x256 4m 64KB_D", DXGI_FORMAT_BC5_UNORM, ADDR_FMT_BC5, 128, 256, 256, 4, 1, 0x10A, ADDR_SW_64KB_D}, + {"BC6H 256x256 4m 64KB_S", DXGI_FORMAT_BC6H_UF16, ADDR_FMT_BC6, 128, 256, 256, 4, 1, 0x109, ADDR_SW_64KB_S}, + {"BC7 64x64 7m 4KB_S", DXGI_FORMAT_BC7_UNORM, ADDR_FMT_BC7, 128, 64, 64, 7, 1, 0x105, ADDR_SW_4KB_S}, + {"BC7 81x69 1m 4KB_D", DXGI_FORMAT_BC7_UNORM, ADDR_FMT_BC7, 128, 81, 69, 1, 1, 0x106, ADDR_SW_4KB_D}, + {"BC7 81x69 4m 4KB_S", DXGI_FORMAT_BC7_UNORM, ADDR_FMT_BC7, 128, 81, 69, 4, 1, 0x105, ADDR_SW_4KB_S}, + {"BC7 129x129 4m 4KB_S", DXGI_FORMAT_BC7_UNORM, ADDR_FMT_BC7, 128, 129, 129, 4, 1, 0x105, ADDR_SW_4KB_S}, + {"BC7 128x64 8m 4KB_S", DXGI_FORMAT_BC7_UNORM, ADDR_FMT_BC7, 128, 128, 64, 8, 1, 0x105, ADDR_SW_4KB_S}, + {"BC7 128x128 3m 2s 64KB_S", DXGI_FORMAT_BC7_UNORM, ADDR_FMT_BC7, 128, 128, 128, 3, 2, 0x109, ADDR_SW_64KB_S}, + {"R8 64x64 1m 256B_S", DXGI_FORMAT_R8_UNORM, ADDR_FMT_8, 8, 64, 64, 1, 1, 0x101, ADDR_SW_256B_S}, + {"R8 64x64 1m 4KB_D", DXGI_FORMAT_R8_UNORM, ADDR_FMT_8, 8, 64, 64, 1, 1, 0x106, ADDR_SW_4KB_D}, + {"R8 128x128 4m 64KB_S", DXGI_FORMAT_R8_UNORM, ADDR_FMT_8, 8, 128, 128, 4, 1, 0x109, ADDR_SW_64KB_S}, + {"RG8 64x64 3m 4KB_D", DXGI_FORMAT_R8G8_UNORM, ADDR_FMT_8_8, 16, 64, 64, 3, 1, 0x106, ADDR_SW_4KB_D}, + {"R16 64x64 3m 4KB_S", DXGI_FORMAT_R16_FLOAT, ADDR_FMT_16, 16, 64, 64, 3, 1, 0x105, ADDR_SW_4KB_S}, + // 4-byte _D currently follows main's gfx10_layout.h working hypothesis (y2x2, pinned in startup_xg_gfx10_check.py); + // re-enable AddrLib comparison once GFX10.3 RbPlus x2y2 is confirmed for both files. + // {"R32 64x64 1m 256B_D", DXGI_FORMAT_R32_FLOAT, ADDR_FMT_32, 32, 64, 64, 1, 1, 0x102, ADDR_SW_256B_D}, + {"RGBA8 128x128 4m 64KB_S", DXGI_FORMAT_R8G8B8A8_UNORM, ADDR_FMT_8_8_8_8, 32, 128, 128, 4, 1, 0x109, ADDR_SW_64KB_S}, + {"BGRA8 64x64 3m 4KB_S", DXGI_FORMAT_B8G8R8A8_UNORM, ADDR_FMT_8_8_8_8, 32, 64, 64, 3, 1, 0x105, ADDR_SW_4KB_S}, + {"RGBA16 64x64 3m 64KB_D", DXGI_FORMAT_R16G16B16A16_FLOAT, ADDR_FMT_16_16_16_16, 64, 64, 64, 3, 1, 0x10A, ADDR_SW_64KB_D}, + {"RGBA32 32x32 3m 4KB_S", DXGI_FORMAT_R32G32B32A32_FLOAT, ADDR_FMT_32_32_32_32, 128, 32, 32, 3, 1, 0x105, ADDR_SW_4KB_S}, + {"RGBA8 64x64 3m LINEAR", DXGI_FORMAT_R8G8B8A8_UNORM, ADDR_FMT_8_8_8_8, 32, 64, 64, 3, 1, 0x100, ADDR_SW_LINEAR}, + {"RGBA16 100x10 3m 2s LINEAR", DXGI_FORMAT_R16G16B16A16_FLOAT, ADDR_FMT_16_16_16_16, 64, 100, 10, 3, 2, 0x100, ADDR_SW_LINEAR}, + }; + + for (const auto& c : cases) { + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + desc.Width = c.width; + desc.Height = c.height; + desc.DepthOrArraySize = static_cast(c.slices); + desc.MipLevels = static_cast(c.mips); + desc.Format = c.dxgi; + desc.SampleDesc.Count = 1; + desc.Layout = static_cast(c.layout); + if (!x2w_startup::Gfx10Texture::supports(desc)) { + std::printf("FAIL %s: Gfx10Texture::supports returned false\n", c.name); + ++failures; + continue; + } + const x2w_startup::Gfx10Texture tex(desc); + + ADDR2_COMPUTE_SURFACE_INFO_INPUT in{}; + ADDR2_COMPUTE_SURFACE_INFO_OUTPUT out{}; + std::array mip_info{}; + in.size = sizeof(in); + in.flags.texture = 1; + in.format = c.addr_fmt; + in.swizzleMode = c.mode; + in.resourceType = ADDR_RSRC_TEX_2D; + in.width = c.width; + in.height = c.height; + in.numMipLevels = c.mips; + in.numSlices = c.slices; + in.numSamples = in.numFrags = 1; + out.size = sizeof(out); + out.pMipInfo = mip_info.data(); + + const ADDR_E_RETURNCODE rc = Addr2ComputeSurfaceInfo(lib, &in, &out); + const bool is_linear = (c.mode == ADDR_SW_LINEAR); + bool ok = rc == ADDR_OK && out.surfSize == tex.bytes() && out.sliceSize == tex.slice_bytes() && + out.baseAlign == tex.alignment() && (is_linear || out.firstMipIdInTail == tex.first_tail); + if (ok) { + for (UINT_32 m = 0; m < c.mips; ++m) { + const UINT64 expected_off = (is_linear || m < tex.first_tail) ? mip_info[m].macroBlockOffset : mip_info[m].offset; + if (expected_off != tex.start(m)) { + std::printf("FAIL %s mip %u offset: addrlib=%llu tex=%llu\n", c.name, m, + static_cast(expected_off), + static_cast(tex.start(m))); + ok = false; + break; + } + } + } else { + std::printf("FAIL %s info: rc=%d surf=%llu/%llu slice=%llu/%llu align=%u/%llu tail=%u/%u\n", c.name, int(rc), + static_cast(out.surfSize), static_cast(tex.bytes()), + static_cast(out.sliceSize), static_cast(tex.slice_bytes()), + out.baseAlign, static_cast(tex.alignment()), + out.firstMipIdInTail, tex.first_tail); + } + + if (ok) { + ADDR2_COMPUTE_SURFACE_ADDRFROMCOORD_INPUT ain{}; + ADDR2_COMPUTE_SURFACE_ADDRFROMCOORD_OUTPUT aout{}; + ain.size = sizeof(ain); + ain.flags.texture = 1; + ain.resourceType = ADDR_RSRC_TEX_2D; + ain.swizzleMode = c.mode; + ain.bpp = c.bpp; + ain.unalignedWidth = (c.width + tex.block_dim - 1) / tex.block_dim; + ain.unalignedHeight = (c.height + tex.block_dim - 1) / tex.block_dim; + ain.numMipLevels = c.mips; + ain.numSlices = c.slices; + ain.numSamples = ain.numFrags = 1; + aout.size = sizeof(aout); + for (ain.slice = 0; ain.slice < c.slices && ok; ++ain.slice) { + for (ain.mipId = 0; ain.mipId < c.mips && ok; ++ain.mipId) { + const UINT cols = tex.columns(ain.mipId); + const UINT r = tex.rows(ain.mipId); + for (ain.y = 0; ain.y < r && ok; ++ain.y) { + for (ain.x = 0; ain.x < cols; ++ain.x) { + const UINT64 expected = tex.address(ain.mipId, ain.x, ain.y, ain.slice); + if (Addr2ComputeSurfaceAddrFromCoord(lib, &ain, &aout) != ADDR_OK || + aout.bitPosition != 0 || aout.addr != expected) { + std::printf("FAIL %s slice %u mip %u (%u,%u): addrlib=%llu tex=%llu\n", + c.name, ain.slice, ain.mipId, ain.x, ain.y, + static_cast(aout.addr), + static_cast(expected)); + ok = false; + } + } + } + } + } + } + expect(ok, c.name); + } + + AddrDestroy(lib); + return failures ? 1 : 0; +} + int run(bool mutate_linear_fallback) { std::printf("AddrLib observations from %s\n", addrlib_source); @@ -660,11 +801,12 @@ int run(bool mutate_linear_fallback) int main(int argc, char** argv) { if (argc == 2 && std::strcmp(argv[1], "--bc64-tiling") == 0) return bc64_tiling_reference(); + if (argc == 2 && std::strcmp(argv[1], "--gfx10-tiling") == 0) return gfx10_tiling_reference(); if (argc == 2 && std::strcmp(argv[1], "--factory") == 0) return run_factory(false); if (argc == 2 && std::strcmp(argv[1], "--mutate-factory-no-write") == 0) return run_factory(true); const bool mutate = argc == 2 && std::strcmp(argv[1], "--mutate-linear-fallback") == 0; if (argc > 2 || (argc == 2 && !mutate)) { - std::puts("usage: xg_swizzle_select_check [--mutate-linear-fallback|--factory|--mutate-factory-no-write|--bc64-tiling]"); + std::puts("usage: xg_swizzle_select_check [--mutate-linear-fallback|--factory|--mutate-factory-no-write|--bc64-tiling|--gfx10-tiling]"); return 2; } const int result = run(mutate);