From d7cd15592488e12deb24add90c2268489e58f50f Mon Sep 17 00:00:00 2001 From: devil8blade <360741796+devil8blade@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:32:05 +0000 Subject: [PATCH 1/3] xg_xs: add linear 2D texture address computer Implement XGCreateTextureComputer and XGSuggestSwizzleMode for linear RGBA8 and BC1 through BC7 layouts. RDNA 2 SW_64KB_S/D/R modes return E_NOTIMPL and do not write addresses. Closes #415 Signed-off-by: devil8blade <360741796+devil8blade@users.noreply.github.com> --- CHANGELOG.md | 1 + shims/xg_xs/xg_texture.h | 177 ++++ shims/xg_xs/xg_xs.cpp | 911 +++++++++++++++++++- tests/conformance/test_xg_xs/CMakeLists.txt | 5 + tests/conformance/test_xg_xs/main.cpp | 544 ++++++++++++ 5 files changed, 1620 insertions(+), 18 deletions(-) create mode 100644 shims/xg_xs/xg_texture.h create mode 100644 tests/conformance/test_xg_xs/CMakeLists.txt create mode 100644 tests/conformance/test_xg_xs/main.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d09c9ef..5a2577a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Guest execution uses original, purpose-built test programs. Commercial Xbox game remains unsupported. ### Added +- `xg_xs.dll` computes linear 2D layouts for `R8G8B8A8` and BC1 through BC7 (`XGCreateTextureComputer`, `XGSuggestSwizzleMode`, and COM `IXGTextureAddressComputer`). Row pitch uses the public D3D12 256-byte alignment and each subresource is placed on the public 512-byte alignment. Sizes are overflow-checked, null pointers return `E_POINTER`, and copies stay inside the caller buffers. `SW_64KB_S`, `SW_64KB_D`, and `SW_64KB_R` return `E_NOTIMPL` and write no address. Synthetic `test_xg_xs.exe` checks hand-computed mip offsets, alignment, refcounts, and a byte-identical copy round trip. This is not an Xbox hardware result (#415). - `dxbc-accc` parses a synthetic FSPX section table inside DXBC `ACCC` chunks (section descriptors, section 1 stage configuration using the `bx_shader_stage_t` values, and section 6 RDNA 2 code bounds) and returns a parse error for truncated or out-of-range input instead of panicking. A table may declare at most 64 sections. A `DXIL` chunk is accepted only when it is a `DxilProgramHeader` from the public DirectXShaderCompiler layout. It exports `bx_shader_inspect`, `bx_shader_prepare_for_d3dmetal`, and `bx_shader_free_buffer` with the layouts in `abi/include/bx_shader_bridge.h`. `bx_shader_inspect` fills that struct without logging. `bx_shader_prepare_for_d3dmetal` copies the `container_size` prefix when that DXIL header is present, including when the container also has a malformed FSPX table. An empty `DXIL` chunk is rejected. ACCC-only input returns `BX_SHADER_ERR_UNSUPPORTED` and writes one stderr inspection line. The first FSPX table that declares a stage supplies it. A failed stderr write does not panic. RDNA 2 bytes are not translated to DXIL. The section layout is synthetic and is not a console container specification or an Xbox hardware result (#416). - Added a visual `Progress Tracker` section to `README.md` with an M0–M6 Mermaid milestone pipeline, a Track A–F live status board (`🟩`/`🟨`/`⬜`), and direct GitHub label links for open M1 issues. - Added a required `Zen` field to `.github/ISSUE_TEMPLATE/track-handoff.yml` so every track handoff issue states its core purpose briefly in plain English up front. diff --git a/shims/xg_xs/xg_texture.h b/shims/xg_xs/xg_texture.h new file mode 100644 index 00000000..38464710 --- /dev/null +++ b/shims/xg_xs/xg_texture.h @@ -0,0 +1,177 @@ +#ifndef BETTERXEN_XG_TEXTURE_H +#define BETTERXEN_XG_TEXTURE_H + +/* + * BetterXen M1 linear texture-address contract for xg_xs.dll. + * + * Field meanings follow the public D3D12 resource and footprint documents + * (D3D12_RESOURCE_DIMENSION, D3D12_SUBRESOURCE_FOOTPRINT, and + * D3D12_TEXTURE_DATA_PITCH_ALIGNMENT / D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT). + * Format numbers are the public DXGI_FORMAT values. This is not the Xbox + * GDK XG interface and it is not an Xbox hardware result. + */ + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Public D3D12_RESOURCE_DIMENSION values used by this shim. */ +#define BX_XG_DIMENSION_BUFFER 1u +#define BX_XG_DIMENSION_TEXTURE1D 2u +#define BX_XG_DIMENSION_TEXTURE2D 3u +#define BX_XG_DIMENSION_TEXTURE3D 4u + +/* Public D3D12 texture copy alignments, in bytes. */ +#define BX_XG_LINEAR_PITCH_ALIGNMENT 256u +#define BX_XG_LINEAR_PLACEMENT_ALIGNMENT 512u + +/* Public D3D12 texture size limits used as the acceptance cap. */ +#define BX_XG_MAX_TEXTURE_EDGE 16384u +#define BX_XG_MAX_ARRAY_SLICES 2048u + +/* + * Layout selectors for this shim. LINEAR is computed exactly. + * SW_64KB_S / D / R are named RDNA 2 tile modes reserved for M3. They are + * accepted by XGCreateTextureComputer and every layout or copy call on them + * returns E_NOTIMPL without writing an address. + * XGSuggestSwizzleMode never returns a tile mode. It returns LINEAR or NONE. + */ +typedef enum bx_xg_layout_mode { + BX_XG_LAYOUT_LINEAR = 0, + BX_XG_LAYOUT_SW_64KB_S = 1, + BX_XG_LAYOUT_SW_64KB_D = 2, + BX_XG_LAYOUT_SW_64KB_R = 3, + BX_XG_LAYOUT_NONE = 0xFFFFFFFFu +} bx_xg_layout_mode; + +typedef struct bx_xg_sample_desc { + uint32_t count; + uint32_t quality; +} bx_xg_sample_desc; + +typedef struct bx_xg_resource_desc { + uint32_t dimension; + uint32_t alignment; /* 0 or BX_XG_LINEAR_PLACEMENT_ALIGNMENT */ + uint64_t width; /* texels */ + uint32_t height; /* texels */ + uint16_t depth_or_array_size; + uint16_t mip_levels; /* 0 selects the full chain, down to 1x1 */ + uint32_t format; /* DXGI_FORMAT */ + bx_xg_sample_desc sample_desc; + uint32_t layout; /* bx_xg_layout_mode */ + uint32_t flags; /* must be 0 */ +} bx_xg_resource_desc; + +typedef struct bx_xg_subresource_footprint { + uint32_t format; + uint32_t width; /* texels */ + uint32_t height; /* texels */ + uint32_t depth; + uint32_t row_pitch; /* bytes, multiple of BX_XG_LINEAR_PITCH_ALIGNMENT */ +} bx_xg_subresource_footprint; + +typedef struct bx_xg_placed_footprint { + uint64_t offset; + bx_xg_subresource_footprint footprint; +} bx_xg_placed_footprint; + +/* {B7E4C8A1-6F2D-4C55-8A19-3D0E5F7B91C4} BetterXen IXGTextureAddressComputer */ +static const GUID BX_IID_IXGTextureAddressComputer = { + 0xB7E4C8A1u, 0x6F2Du, 0x4C55u, {0x8Au, 0x19u, 0x3Du, 0x0Eu, 0x5Fu, 0x7Bu, 0x91u, 0xC4u}}; + +/* Public COM IUnknown IID. */ +static const GUID BX_IID_IUnknown = { + 0x00000000u, 0x0000u, 0x0000u, {0xC0u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x46u}}; + +typedef struct bx_xg_texture_computer bx_xg_texture_computer; + +typedef struct bx_xg_texture_computer_vtbl { + HRESULT(WINAPI* QueryInterface)(bx_xg_texture_computer* self, const GUID* riid, void** object); + ULONG(WINAPI* AddRef)(bx_xg_texture_computer* self); + ULONG(WINAPI* Release)(bx_xg_texture_computer* self); + HRESULT(WINAPI* GetResourceLayout)( + bx_xg_texture_computer* self, + uint32_t first_subresource, + uint32_t subresource_count, + uint64_t base_offset, + bx_xg_placed_footprint* layouts, + uint32_t* row_counts, + uint64_t* row_sizes_in_bytes, + uint64_t* total_bytes); + HRESULT(WINAPI* GetResourceSizeBytes)(bx_xg_texture_computer* self, uint64_t* size_bytes); + HRESULT(WINAPI* GetResourceBaseAlignmentBytes)(bx_xg_texture_computer* self, uint64_t* alignment_bytes); + HRESULT(WINAPI* GetMipLevelOffsetBytes)( + bx_xg_texture_computer* self, + uint32_t array_slice, + uint32_t mip_level, + uint64_t* offset_bytes); + HRESULT(WINAPI* GetTexelElementOffsetBytes)( + bx_xg_texture_computer* self, + uint32_t array_slice, + uint32_t mip_level, + uint32_t x, + uint32_t y, + uint32_t z, + uint64_t* offset_bytes); + HRESULT(WINAPI* CopyIntoSubresource)( + bx_xg_texture_computer* self, + void* destination, + uint64_t destination_size, + uint32_t subresource, + const void* source, + uint64_t source_size, + uint32_t source_row_pitch, + uint32_t source_depth_pitch); + HRESULT(WINAPI* CopyFromSubresource)( + bx_xg_texture_computer* self, + void* destination, + uint64_t destination_size, + uint32_t destination_row_pitch, + uint32_t destination_depth_pitch, + const void* source, + uint64_t source_size, + uint32_t subresource); +} bx_xg_texture_computer_vtbl; + +struct bx_xg_texture_computer { + const bx_xg_texture_computer_vtbl* lpVtbl; +}; + +/* + * On success, *computer is an IXGTextureAddressComputer with a reference + * count of 1. On failure, *computer is NULL when computer itself is non-NULL. + * Subresource index is mip_level + array_slice * mip_levels. + * A row pitch of 0 on a copy selects the tight logical row size. + */ +HRESULT WINAPI XGCreateTextureComputer(const bx_xg_resource_desc* desc, void** computer); + +/* + * Returns BX_XG_LAYOUT_LINEAR when this shim can lay the texture out, and + * BX_XG_LAYOUT_NONE otherwise. The same arguments always return the same mode. + */ +uint32_t WINAPI XGSuggestSwizzleMode( + uint32_t resource_type, + uint32_t format, + uint32_t width, + uint32_t height, + uint32_t depth, + uint32_t mip_levels, + uint32_t sample_count, + uint32_t flags); + +#ifdef __cplusplus +} +#endif + +#endif /* BETTERXEN_XG_TEXTURE_H */ diff --git a/shims/xg_xs/xg_xs.cpp b/shims/xg_xs/xg_xs.cpp index f1389e11..66464ad1 100644 --- a/shims/xg_xs/xg_xs.cpp +++ b/shims/xg_xs/xg_xs.cpp @@ -1,32 +1,907 @@ -#include +#include "xg_texture.h" + +#include #include +#include #include "bx_log.h" -/* Track C (M1-C2): Scarlett RDNA 2 texture tiling / swizzle address computer. */ +/* File-local helpers. Exported entry points stay at the bottom with C linkage. */ -extern "C" { +/* + * Linear 2D layouts follow the public D3D12 copy footprint rules: + * mip size = max(1, base >> mip) + * block count = ceil(texels / block), at least 1 + * row bytes = block_columns * bytes_per_block + * row pitch = align_up(row_bytes, 256) + * subresource = row_pitch * block_rows + * next offset = align_up(offset + subresource, 512) + * Subresource index is mip + array_slice * mip_count. + * BC1 and BC4 store 8 bytes per 4x4 block. BC2, BC3, BC5, BC6H, and BC7 + * store 16. R8G8B8A8 stores 4 bytes per texel. + * SW_64KB_S, SW_64KB_D, and SW_64KB_R do not produce addresses. + */ -HRESULT WINAPI XGCreateTextureComputer(const void* /*desc*/, void** out_computer) { - BX_STUB_LOG("xg_xs.dll", "XGCreateTextureComputer"); - if (out_computer) { - *out_computer = nullptr; +constexpr uint32_t kPitchAlignment = BX_XG_LINEAR_PITCH_ALIGNMENT; +constexpr uint32_t kPlacementAlignment = BX_XG_LINEAR_PLACEMENT_ALIGNMENT; + +static_assert(static_cast(DXGI_FORMAT_R8G8B8A8_TYPELESS) == 27u, "DXGI R8G8B8A8_TYPELESS"); +static_assert(static_cast(DXGI_FORMAT_R8G8B8A8_UNORM) == 28u, "DXGI R8G8B8A8_UNORM"); +static_assert(static_cast(DXGI_FORMAT_R8G8B8A8_SINT) == 32u, "DXGI R8G8B8A8_SINT"); +static_assert(static_cast(DXGI_FORMAT_BC1_TYPELESS) == 70u, "DXGI BC1_TYPELESS"); +static_assert(static_cast(DXGI_FORMAT_BC1_UNORM) == 71u, "DXGI BC1_UNORM"); +static_assert(static_cast(DXGI_FORMAT_BC4_SNORM) == 81u, "DXGI BC4_SNORM"); +static_assert(static_cast(DXGI_FORMAT_BC5_TYPELESS) == 82u, "DXGI BC5_TYPELESS"); +static_assert(static_cast(DXGI_FORMAT_BC6H_TYPELESS) == 94u, "DXGI BC6H_TYPELESS"); +static_assert(static_cast(DXGI_FORMAT_BC6H_UF16) == 95u, "DXGI BC6H_UF16"); +static_assert(static_cast(DXGI_FORMAT_BC7_UNORM) == 98u, "DXGI BC7_UNORM"); +static_assert(static_cast(DXGI_FORMAT_BC7_UNORM_SRGB) == 99u, "DXGI BC7_UNORM_SRGB"); + +struct Resolved { + uint32_t width; + uint32_t height; + uint32_t mip_levels; + uint32_t array_size; + uint32_t subresource_count; + uint32_t format; + uint32_t block_w; + uint32_t block_h; + uint32_t block_bytes; + uint32_t layout; +}; + +struct TextureComputer { + bx_xg_texture_computer iface; + volatile LONG refcount; + Resolved resolved; +}; + +struct Placement { + uint64_t offset; + uint32_t width; + uint32_t height; + uint32_t depth; + uint32_t row_pitch; + uint32_t row_count; + uint64_t row_bytes; + uint32_t format; +}; + +struct Rdna2TileMode { + uint32_t mode; + uint32_t tile_bytes; + const char* name; +}; + +struct Rdna2TileQuery { + uint32_t mode; + uint32_t mip_width; + uint32_t mip_height; + uint32_t block_bytes; + uint32_t texel_x; + uint32_t texel_y; +}; + +/* Named 64 KiB tiles. The address equation is intentionally absent. */ +constexpr Rdna2TileMode kRdna2TileModes[] = { + {BX_XG_LAYOUT_SW_64KB_S, 64u * 1024u, "SW_64KB_S"}, + {BX_XG_LAYOUT_SW_64KB_D, 64u * 1024u, "SW_64KB_D"}, + {BX_XG_LAYOUT_SW_64KB_R, 64u * 1024u, "SW_64KB_R"}, +}; + +constexpr uint32_t kSlotGetResourceLayout = 3u; +constexpr uint32_t kSlotGetResourceSizeBytes = 4u; +constexpr uint32_t kSlotGetResourceBaseAlignmentBytes = 5u; +constexpr uint32_t kSlotGetMipLevelOffsetBytes = 6u; +constexpr uint32_t kSlotGetTexelElementOffsetBytes = 7u; +constexpr uint32_t kSlotCopyIntoSubresource = 8u; +constexpr uint32_t kSlotCopyFromSubresource = 9u; + +static bool mul_u64(uint64_t a, uint64_t b, uint64_t* out) { + if (a != 0 && b > UINT64_MAX / a) { + return false; + } + *out = a * b; + return true; +} + +static bool add_u64(uint64_t a, uint64_t b, uint64_t* out) { + if (a > UINT64_MAX - b) { + return false; + } + *out = a + b; + return true; +} + +static bool align_up_u64(uint64_t value, uint64_t alignment, uint64_t* out) { + if (alignment == 0 || (alignment & (alignment - 1u)) != 0) { + return false; + } + const uint64_t mask = alignment - 1u; + if (value > UINT64_MAX - mask) { + return false; + } + *out = (value + mask) & ~mask; + return true; +} + +static uint32_t div_up_u32(uint32_t value, uint32_t block) { + return (value + block - 1u) / block; +} + +static uint32_t mip_size(uint32_t base, uint32_t mip) { + if (mip >= 31u) { + return 1u; + } + const uint32_t scaled = base >> mip; + return scaled == 0u ? 1u : scaled; +} + +static uint32_t full_mip_count(uint32_t width, uint32_t height) { + uint32_t levels = 1u; + while (width > 1u || height > 1u) { + if (width > 1u) { + width >>= 1u; + } + if (height > 1u) { + height >>= 1u; + } + levels += 1u; + } + return levels; +} + +static bool guid_eq(const GUID* a, const GUID* b) { + return std::memcmp(a, b, sizeof(GUID)) == 0; +} + +static bool lookup_format(uint32_t format, uint32_t* block_w, uint32_t* block_h, uint32_t* block_bytes) { + switch (format) { + 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: + *block_w = 1u; + *block_h = 1u; + *block_bytes = 4u; + return true; + 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: + *block_w = 4u; + *block_h = 4u; + *block_bytes = 8u; + return true; + 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: + *block_w = 4u; + *block_h = 4u; + *block_bytes = 16u; + return true; + default: + return false; + } +} + +static bool layout_is_tiled(uint32_t layout) { + for (const Rdna2TileMode& mode : kRdna2TileModes) { + if (mode.mode == layout) { + return true; + } + } + return false; +} + +static HRESULT validate_desc(const bx_xg_resource_desc* desc, Resolved* out) { + if (desc->dimension == BX_XG_DIMENSION_TEXTURE1D || desc->dimension == BX_XG_DIMENSION_TEXTURE3D) { + return E_NOTIMPL; + } + if (desc->dimension != BX_XG_DIMENSION_TEXTURE2D) { + return E_INVALIDARG; + } + if (desc->flags != 0u) { + return E_INVALIDARG; + } + if (desc->alignment != 0u && desc->alignment != kPlacementAlignment) { + return E_INVALIDARG; + } + if (desc->width == 0 || desc->width > BX_XG_MAX_TEXTURE_EDGE || desc->height == 0u || + desc->height > BX_XG_MAX_TEXTURE_EDGE) { + return E_INVALIDARG; + } + if (desc->depth_or_array_size == 0u || desc->depth_or_array_size > BX_XG_MAX_ARRAY_SLICES) { + return E_INVALIDARG; + } + if (desc->sample_desc.count == 0u || desc->sample_desc.quality != 0u) { + return E_INVALIDARG; + } + if (desc->sample_desc.count != 1u) { + return E_NOTIMPL; + } + if (desc->layout != BX_XG_LAYOUT_LINEAR && !layout_is_tiled(desc->layout)) { + return E_INVALIDARG; + } + + uint32_t block_w = 0; + uint32_t block_h = 0; + uint32_t block_bytes = 0; + if (!lookup_format(desc->format, &block_w, &block_h, &block_bytes)) { + return E_INVALIDARG; + } + + const uint32_t width = static_cast(desc->width); + const uint32_t height = desc->height; + const uint32_t max_mips = full_mip_count(width, height); + const uint32_t mips = desc->mip_levels == 0u ? max_mips : desc->mip_levels; + if (mips == 0u || mips > max_mips) { + return E_INVALIDARG; + } + if (desc->depth_or_array_size > UINT32_MAX / mips) { + return E_INVALIDARG; + } + + out->width = width; + out->height = height; + out->mip_levels = mips; + out->array_size = desc->depth_or_array_size; + out->subresource_count = mips * desc->depth_or_array_size; + out->format = desc->format; + out->block_w = block_w; + out->block_h = block_h; + out->block_bytes = block_bytes; + out->layout = desc->layout; + return S_OK; +} + +static TextureComputer* impl_from(bx_xg_texture_computer* self) { + static_assert(offsetof(TextureComputer, iface) == 0, "COM object must start with its vtable pointer"); + return reinterpret_cast(self); +} + +/* M3 placeholder. Validates the tile request and returns no address. */ +static HRESULT rdna2_sw64kb_prepare(const Rdna2TileQuery* query) { + if (!query || query->mip_width == 0u || query->mip_height == 0u || query->block_bytes == 0u) { + return E_INVALIDARG; + } + const Rdna2TileMode* found = nullptr; + for (const Rdna2TileMode& mode : kRdna2TileModes) { + if (mode.mode == query->mode) { + found = &mode; + break; + } + } + if (!found || found->tile_bytes != (64u * 1024u) || found->name == nullptr) { + return E_INVALIDARG; } return E_NOTIMPL; } -uint32_t WINAPI XGSuggestSwizzleMode( - uint32_t /*resource_type*/, - uint32_t /*format*/, - uint32_t /*width*/, - uint32_t /*height*/, - uint32_t /*depth*/, - uint32_t /*mip_levels*/, - uint32_t /*sample_count*/, - uint32_t /*flags*/ +static HRESULT reject_if_tiled(const TextureComputer* obj, uint32_t slot, const char* method) { + if (obj->resolved.layout == BX_XG_LAYOUT_LINEAR) { + return S_OK; + } + Rdna2TileQuery query{}; + query.mode = obj->resolved.layout; + query.mip_width = obj->resolved.width; + query.mip_height = obj->resolved.height; + query.block_bytes = obj->resolved.block_bytes; + query.texel_x = 0u; + query.texel_y = 0u; + const HRESULT hr = rdna2_sw64kb_prepare(&query); + if (hr == E_NOTIMPL) { + BX_VTABLE_STUB_LOG("IXGTextureAddressComputer", slot, method); + } + return hr; +} + +static HRESULT measure_mip(const Resolved* resolved, uint32_t mip, Placement* place) { + const uint32_t width = mip_size(resolved->width, mip); + const uint32_t height = mip_size(resolved->height, mip); + const uint32_t blocks_w = div_up_u32(width, resolved->block_w); + const uint32_t blocks_h = div_up_u32(height, resolved->block_h); + uint64_t row_bytes = 0; + if (!mul_u64(blocks_w, resolved->block_bytes, &row_bytes) || row_bytes == 0u) { + return E_INVALIDARG; + } + uint64_t row_pitch = 0; + if (!align_up_u64(row_bytes, kPitchAlignment, &row_pitch) || row_pitch > UINT32_MAX) { + return E_INVALIDARG; + } + place->offset = 0; + place->width = width; + place->height = height; + place->depth = 1u; + place->row_pitch = static_cast(row_pitch); + place->row_count = blocks_h; + place->row_bytes = row_bytes; + place->format = resolved->format; + return S_OK; +} + +static HRESULT advance_cursor(uint64_t cursor, const Placement* place, uint64_t* next) { + uint64_t payload = 0; + uint64_t raw_end = 0; + if (!mul_u64(place->row_pitch, place->row_count, &payload) || !add_u64(cursor, payload, &raw_end)) { + return E_INVALIDARG; + } + if (!align_up_u64(raw_end, kPlacementAlignment, next)) { + return E_INVALIDARG; + } + return S_OK; +} + +using PlacementSink = void (*)(void* ctx, uint32_t slot, const Placement* place); + +static HRESULT walk_placements( + const Resolved* resolved, + uint64_t base_offset, + uint32_t first, + uint32_t last_exclusive, + PlacementSink sink, + void* sink_ctx, + uint64_t* aligned_end ) { - BX_STUB_LOG("xg_xs.dll", "XGSuggestSwizzleMode"); - return 0u; /* Linear fallback during M0/M1 bring-up */ + uint64_t cursor = base_offset; + uint64_t end = base_offset; + for (uint32_t index = 0; index < last_exclusive; ++index) { + Placement place{}; + const HRESULT hr = measure_mip(resolved, index % resolved->mip_levels, &place); + if (hr != S_OK) { + return hr; + } + place.offset = cursor; + uint64_t next = 0; + const HRESULT advanced = advance_cursor(cursor, &place, &next); + if (advanced != S_OK) { + return advanced; + } + if (sink && index >= first) { + sink(sink_ctx, index - first, &place); + } + end = next; + cursor = next; + } + *aligned_end = end; + return S_OK; +} + +struct OnePlacement { + Placement place; + bool written; +}; + +static void sink_one(void* ctx, uint32_t /*slot*/, const Placement* place) { + auto* out = static_cast(ctx); + out->place = *place; + out->written = true; +} + +static HRESULT placement_at(const Resolved* resolved, uint32_t index, Placement* out) { + if (index >= resolved->subresource_count) { + return E_INVALIDARG; + } + OnePlacement captured{}; + uint64_t end = 0; + const HRESULT hr = walk_placements(resolved, 0, index, index + 1u, sink_one, &captured, &end); + if (hr != S_OK) { + return hr; + } + if (!captured.written) { + return E_FAIL; + } + *out = captured.place; + return S_OK; +} + +struct LayoutOut { + bx_xg_placed_footprint* layouts; + uint32_t* row_counts; + uint64_t* row_sizes; +}; + +static void sink_layout(void* ctx, uint32_t slot, const Placement* place) { + auto* out = static_cast(ctx); + out->layouts[slot].offset = place->offset; + out->layouts[slot].footprint.format = place->format; + out->layouts[slot].footprint.width = place->width; + out->layouts[slot].footprint.height = place->height; + out->layouts[slot].footprint.depth = place->depth; + out->layouts[slot].footprint.row_pitch = place->row_pitch; + out->row_counts[slot] = place->row_count; + out->row_sizes[slot] = place->row_bytes; +} + +static bool row_bounds( + uint64_t origin, + uint64_t row_pitch, + uint64_t depth_pitch, + uint32_t z, + uint32_t y, + uint64_t row_bytes, + uint64_t size, + uint64_t* begin, + uint64_t* end +) { + uint64_t zoff = 0; + uint64_t yoff = 0; + uint64_t start = 0; + uint64_t stop = 0; + if (!mul_u64(z, depth_pitch, &zoff) || !mul_u64(y, row_pitch, &yoff)) { + return false; + } + if (!add_u64(origin, zoff, &start) || !add_u64(start, yoff, &start) || !add_u64(start, row_bytes, &stop)) { + return false; + } + if (stop < start || stop > size) { + return false; + } + *begin = start; + *end = stop; + return true; +} + +static HRESULT resolve_image_pitch(const Placement* place, uint32_t row_pitch_in, uint32_t depth_pitch_in, uint64_t* row_pitch, uint64_t* depth_pitch) { + const uint64_t row = row_pitch_in == 0u ? place->row_bytes : row_pitch_in; + if (row < place->row_bytes || row > UINT32_MAX) { + return E_INVALIDARG; + } + uint64_t depth = 0; + if (depth_pitch_in == 0u) { + if (!mul_u64(row, place->row_count, &depth)) { + return E_INVALIDARG; + } + } else { + depth = depth_pitch_in; + if (place->depth > 1u && place->row_count > 0u) { + uint64_t min_span = 0; + if (!mul_u64(place->row_count - 1u, row, &min_span) || !add_u64(min_span, place->row_bytes, &min_span)) { + return E_INVALIDARG; + } + if (depth < min_span) { + return E_INVALIDARG; + } + } + } + *row_pitch = row; + *depth_pitch = depth; + return S_OK; +} + +static HRESULT copy_rows( + const Placement* place, + uint8_t* dst, + uint64_t dst_size, + uint64_t dst_origin, + uint64_t dst_row_pitch, + uint64_t dst_depth_pitch, + const uint8_t* src, + uint64_t src_size, + uint64_t src_origin, + uint64_t src_row_pitch, + uint64_t src_depth_pitch +) { + if (!dst || !src || place->row_count == 0u || place->depth == 0u || place->row_bytes == 0u) { + return E_INVALIDARG; + } + if (dst_row_pitch < place->row_bytes || src_row_pitch < place->row_bytes) { + return E_INVALIDARG; + } + if (place->row_bytes > SIZE_MAX) { + return E_INVALIDARG; + } + + for (uint32_t z = 0; z < place->depth; ++z) { + for (uint32_t y = 0; y < place->row_count; ++y) { + uint64_t src_begin = 0; + uint64_t src_end = 0; + uint64_t dst_begin = 0; + uint64_t dst_end = 0; + if (!row_bounds(src_origin, src_row_pitch, src_depth_pitch, z, y, place->row_bytes, src_size, &src_begin, &src_end) || + !row_bounds(dst_origin, dst_row_pitch, dst_depth_pitch, z, y, place->row_bytes, dst_size, &dst_begin, &dst_end)) { + return E_INVALIDARG; + } + } + } + + for (uint32_t z = 0; z < place->depth; ++z) { + for (uint32_t y = 0; y < place->row_count; ++y) { + uint64_t src_begin = 0; + uint64_t src_end = 0; + uint64_t dst_begin = 0; + uint64_t dst_end = 0; + if (!row_bounds(src_origin, src_row_pitch, src_depth_pitch, z, y, place->row_bytes, src_size, &src_begin, &src_end) || + !row_bounds(dst_origin, dst_row_pitch, dst_depth_pitch, z, y, place->row_bytes, dst_size, &dst_begin, &dst_end)) { + return E_FAIL; + } + std::memcpy(dst + dst_begin, src + src_begin, static_cast(place->row_bytes)); + } + } + return S_OK; +} + +static HRESULT image_and_resource_pitch( + const Placement* place, + uint32_t image_row_pitch, + uint32_t image_depth_pitch, + uint64_t* image_row, + uint64_t* image_depth, + uint64_t* resource_depth +) { + const HRESULT pitch = resolve_image_pitch(place, image_row_pitch, image_depth_pitch, image_row, image_depth); + if (pitch != S_OK) { + return pitch; + } + if (!mul_u64(place->row_pitch, place->row_count, resource_depth)) { + return E_INVALIDARG; + } + return S_OK; +} + +static ULONG WINAPI computer_add_ref(bx_xg_texture_computer* self) { + if (!self) { + return 0u; + } + const LONG count = InterlockedIncrement(&impl_from(self)->refcount); + return static_cast(count); +} + +static ULONG WINAPI computer_release(bx_xg_texture_computer* self) { + if (!self) { + return 0u; + } + TextureComputer* obj = impl_from(self); + const LONG count = InterlockedDecrement(&obj->refcount); + if (count == 0) { + HeapFree(GetProcessHeap(), 0, obj); + return 0u; + } + if (count < 0) { + return 0u; + } + return static_cast(count); +} + +static HRESULT WINAPI computer_query_interface(bx_xg_texture_computer* self, const GUID* riid, void** object) { + if (!self || !riid || !object) { + return E_POINTER; + } + *object = nullptr; + if (!guid_eq(riid, &BX_IID_IUnknown) && !guid_eq(riid, &BX_IID_IXGTextureAddressComputer)) { + return E_NOINTERFACE; + } + *object = self; + computer_add_ref(self); + return S_OK; +} + +static HRESULT WINAPI computer_get_resource_layout( + bx_xg_texture_computer* self, + uint32_t first_subresource, + uint32_t subresource_count, + uint64_t base_offset, + bx_xg_placed_footprint* layouts, + uint32_t* row_counts, + uint64_t* row_sizes_in_bytes, + uint64_t* total_bytes +) { + if (!self || !layouts || !row_counts || !row_sizes_in_bytes || !total_bytes) { + return E_POINTER; + } + TextureComputer* obj = impl_from(self); + const HRESULT tiled = reject_if_tiled(obj, kSlotGetResourceLayout, "GetResourceLayout"); + if (tiled != S_OK) { + return tiled; + } + if (subresource_count == 0u || (base_offset % kPlacementAlignment) != 0u) { + return E_INVALIDARG; + } + if (first_subresource > obj->resolved.subresource_count || + subresource_count > obj->resolved.subresource_count - first_subresource) { + return E_INVALIDARG; + } + uint64_t end = 0; + const HRESULT checked = walk_placements( + &obj->resolved, base_offset, first_subresource, first_subresource + subresource_count, nullptr, nullptr, &end); + if (checked != S_OK) { + return checked; + } + if (end < base_offset) { + return E_FAIL; + } + LayoutOut out{layouts, row_counts, row_sizes_in_bytes}; + uint64_t end_again = 0; + const HRESULT written = walk_placements( + &obj->resolved, + base_offset, + first_subresource, + first_subresource + subresource_count, + sink_layout, + &out, + &end_again); + if (written != S_OK || end_again != end) { + return written != S_OK ? written : E_FAIL; + } + *total_bytes = end - base_offset; + return S_OK; +} + +static HRESULT WINAPI computer_get_resource_size_bytes(bx_xg_texture_computer* self, uint64_t* size_bytes) { + if (!self || !size_bytes) { + return E_POINTER; + } + TextureComputer* obj = impl_from(self); + const HRESULT tiled = reject_if_tiled(obj, kSlotGetResourceSizeBytes, "GetResourceSizeBytes"); + if (tiled != S_OK) { + return tiled; + } + uint64_t end = 0; + const HRESULT hr = walk_placements(&obj->resolved, 0, 0, obj->resolved.subresource_count, nullptr, nullptr, &end); + if (hr != S_OK) { + return hr; + } + *size_bytes = end; + return S_OK; +} + +static HRESULT WINAPI computer_get_resource_base_alignment_bytes(bx_xg_texture_computer* self, uint64_t* alignment_bytes) { + if (!self || !alignment_bytes) { + return E_POINTER; + } + TextureComputer* obj = impl_from(self); + const HRESULT tiled = reject_if_tiled(obj, kSlotGetResourceBaseAlignmentBytes, "GetResourceBaseAlignmentBytes"); + if (tiled != S_OK) { + return tiled; + } + *alignment_bytes = kPlacementAlignment; + return S_OK; +} + +static HRESULT WINAPI computer_get_mip_level_offset_bytes( + bx_xg_texture_computer* self, + uint32_t array_slice, + uint32_t mip_level, + uint64_t* offset_bytes +) { + if (!self || !offset_bytes) { + return E_POINTER; + } + TextureComputer* obj = impl_from(self); + const HRESULT tiled = reject_if_tiled(obj, kSlotGetMipLevelOffsetBytes, "GetMipLevelOffsetBytes"); + if (tiled != S_OK) { + return tiled; + } + if (array_slice >= obj->resolved.array_size || mip_level >= obj->resolved.mip_levels) { + return E_INVALIDARG; + } + if (array_slice > (UINT32_MAX - mip_level) / obj->resolved.mip_levels) { + return E_INVALIDARG; + } + const uint32_t index = mip_level + array_slice * obj->resolved.mip_levels; + Placement place{}; + const HRESULT hr = placement_at(&obj->resolved, index, &place); + if (hr != S_OK) { + return hr; + } + *offset_bytes = place.offset; + return S_OK; +} + +static HRESULT WINAPI computer_get_texel_element_offset_bytes( + bx_xg_texture_computer* self, + uint32_t array_slice, + uint32_t mip_level, + uint32_t x, + uint32_t y, + uint32_t z, + uint64_t* offset_bytes +) { + if (!self || !offset_bytes) { + return E_POINTER; + } + TextureComputer* obj = impl_from(self); + const HRESULT tiled = reject_if_tiled(obj, kSlotGetTexelElementOffsetBytes, "GetTexelElementOffsetBytes"); + if (tiled != S_OK) { + return tiled; + } + if (z != 0u || array_slice >= obj->resolved.array_size || mip_level >= obj->resolved.mip_levels) { + return E_INVALIDARG; + } + const uint32_t index = mip_level + array_slice * obj->resolved.mip_levels; + Placement place{}; + const HRESULT hr = placement_at(&obj->resolved, index, &place); + if (hr != S_OK) { + return hr; + } + if (x >= place.width || y >= place.height) { + return E_INVALIDARG; + } + const uint32_t block_x = x / obj->resolved.block_w; + const uint32_t block_y = y / obj->resolved.block_h; + uint64_t row_off = 0; + uint64_t element = 0; + uint64_t sum = 0; + if (!mul_u64(block_y, place.row_pitch, &row_off) || !mul_u64(block_x, obj->resolved.block_bytes, &element) || + !add_u64(place.offset, row_off, &sum) || !add_u64(sum, element, &sum)) { + return E_INVALIDARG; + } + *offset_bytes = sum; + return S_OK; +} + +static HRESULT WINAPI computer_copy_into_subresource( + bx_xg_texture_computer* self, + void* destination, + uint64_t destination_size, + uint32_t subresource, + const void* source, + uint64_t source_size, + uint32_t source_row_pitch, + uint32_t source_depth_pitch +) { + if (!self || !destination || !source) { + return E_POINTER; + } + TextureComputer* obj = impl_from(self); + const HRESULT tiled = reject_if_tiled(obj, kSlotCopyIntoSubresource, "CopyIntoSubresource"); + if (tiled != S_OK) { + return tiled; + } + Placement place{}; + const HRESULT placed = placement_at(&obj->resolved, subresource, &place); + if (placed != S_OK) { + return placed; + } + uint64_t image_row = 0; + uint64_t image_depth = 0; + uint64_t resource_depth = 0; + const HRESULT pitch = image_and_resource_pitch( + &place, source_row_pitch, source_depth_pitch, &image_row, &image_depth, &resource_depth); + if (pitch != S_OK) { + return pitch; + } + return copy_rows( + &place, + static_cast(destination), + destination_size, + place.offset, + place.row_pitch, + resource_depth, + static_cast(source), + source_size, + 0, + image_row, + image_depth); +} + +static HRESULT WINAPI computer_copy_from_subresource( + bx_xg_texture_computer* self, + void* destination, + uint64_t destination_size, + uint32_t destination_row_pitch, + uint32_t destination_depth_pitch, + const void* source, + uint64_t source_size, + uint32_t subresource +) { + if (!self || !destination || !source) { + return E_POINTER; + } + TextureComputer* obj = impl_from(self); + const HRESULT tiled = reject_if_tiled(obj, kSlotCopyFromSubresource, "CopyFromSubresource"); + if (tiled != S_OK) { + return tiled; + } + Placement place{}; + const HRESULT placed = placement_at(&obj->resolved, subresource, &place); + if (placed != S_OK) { + return placed; + } + uint64_t image_row = 0; + uint64_t image_depth = 0; + uint64_t resource_depth = 0; + const HRESULT pitch = image_and_resource_pitch( + &place, destination_row_pitch, destination_depth_pitch, &image_row, &image_depth, &resource_depth); + if (pitch != S_OK) { + return pitch; + } + return copy_rows( + &place, + static_cast(destination), + destination_size, + 0, + image_row, + image_depth, + static_cast(source), + source_size, + place.offset, + place.row_pitch, + resource_depth); +} + +static const bx_xg_texture_computer_vtbl kVtbl = { + computer_query_interface, + computer_add_ref, + computer_release, + computer_get_resource_layout, + computer_get_resource_size_bytes, + computer_get_resource_base_alignment_bytes, + computer_get_mip_level_offset_bytes, + computer_get_texel_element_offset_bytes, + computer_copy_into_subresource, + computer_copy_from_subresource, +}; + +extern "C" { + +HRESULT WINAPI XGCreateTextureComputer(const bx_xg_resource_desc* desc, void** computer) { + if (!computer) { + return E_POINTER; + } + *computer = nullptr; + if (!desc) { + return E_POINTER; + } + Resolved resolved{}; + const HRESULT hr = validate_desc(desc, &resolved); + if (hr != S_OK) { + return hr; + } + auto* obj = static_cast(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TextureComputer))); + if (!obj) { + return E_OUTOFMEMORY; + } + obj->iface.lpVtbl = &kVtbl; + obj->refcount = 1; + obj->resolved = resolved; + *computer = obj; + return S_OK; +} + +uint32_t WINAPI XGSuggestSwizzleMode( + uint32_t resource_type, + uint32_t format, + uint32_t width, + uint32_t height, + uint32_t depth, + uint32_t mip_levels, + uint32_t sample_count, + uint32_t flags +) { + bx_xg_resource_desc desc{}; + desc.dimension = resource_type; + desc.alignment = 0u; + desc.width = width; + desc.height = height; + desc.depth_or_array_size = depth > 0xFFFFu ? static_cast(0) : static_cast(depth); + desc.mip_levels = mip_levels > 0xFFFFu ? static_cast(0xFFFF) : static_cast(mip_levels); + desc.format = format; + desc.sample_desc.count = sample_count; + desc.sample_desc.quality = 0u; + desc.layout = BX_XG_LAYOUT_LINEAR; + desc.flags = flags; + Resolved resolved{}; + if (validate_desc(&desc, &resolved) != S_OK || resolved.layout != BX_XG_LAYOUT_LINEAR) { + return BX_XG_LAYOUT_NONE; + } + return BX_XG_LAYOUT_LINEAR; } } // extern "C" diff --git a/tests/conformance/test_xg_xs/CMakeLists.txt b/tests/conformance/test_xg_xs/CMakeLists.txt new file mode 100644 index 00000000..a75639f4 --- /dev/null +++ b/tests/conformance/test_xg_xs/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable(test_xg_xs main.cpp) +target_include_directories(test_xg_xs PRIVATE + "${BX_ABI_INCLUDE_DIR}" + "${CMAKE_CURRENT_SOURCE_DIR}/../../../shims/xg_xs") +target_link_libraries(test_xg_xs PRIVATE xg_xs kernel32) diff --git a/tests/conformance/test_xg_xs/main.cpp b/tests/conformance/test_xg_xs/main.cpp new file mode 100644 index 00000000..6b60ef49 --- /dev/null +++ b/tests/conformance/test_xg_xs/main.cpp @@ -0,0 +1,544 @@ +#include +#include +#include +#include +#include + +#include "xg_texture.h" + +/* + * Hand-checked layouts. A row is align_up(block_columns * bytes_per_block, 256). + * The next subresource starts at align_up(offset + row_pitch * block_rows, 512). + * These constants are not computed by xg_xs.dll. + */ + +static int g_failures = 0; +static int g_checks = 0; + +static void check(bool ok, const char* msg) { + g_checks += 1; + if (!ok) { + std::fprintf(stderr, "FAIL: %s\n", msg); + g_failures += 1; + } +} + +static void check_hr(const char* name, HRESULT hr, HRESULT want) { + g_checks += 1; + if (hr != want) { + std::fprintf(stderr, "FAIL: %s hr=0x%08lx want=0x%08lx\n", + name, + static_cast(static_cast(hr)), + static_cast(static_cast(want))); + g_failures += 1; + } +} + +static bx_xg_resource_desc desc_2d( + uint32_t format, + uint32_t width, + uint32_t height, + uint16_t arrays, + uint16_t mips, + uint32_t layout +) { + bx_xg_resource_desc desc{}; + desc.dimension = BX_XG_DIMENSION_TEXTURE2D; + desc.width = width; + desc.height = height; + desc.depth_or_array_size = arrays; + desc.mip_levels = mips; + desc.format = format; + desc.sample_desc.count = 1u; + desc.sample_desc.quality = 0u; + desc.layout = layout; + return desc; +} + +static bx_xg_texture_computer* create_ok(const bx_xg_resource_desc* desc, const char* name) { + void* object = nullptr; + const HRESULT hr = XGCreateTextureComputer(desc, &object); + check_hr(name, hr, S_OK); + check(object != nullptr, name); + return static_cast(object); +} + +static void release_computer(bx_xg_texture_computer* computer) { + if (computer) { + computer->lpVtbl->Release(computer); + } +} + +static bool bytes_are(const uint8_t* data, size_t size, uint8_t value) { + for (size_t i = 0; i < size; ++i) { + if (data[i] != value) { + return false; + } + } + return true; +} + +static void test_arguments() { + void* object = reinterpret_cast(static_cast(1)); + check_hr("null desc", XGCreateTextureComputer(nullptr, &object), E_POINTER); + check(object == nullptr, "null desc clears the output"); + + bx_xg_resource_desc desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 4, 4, 1, 1, BX_XG_LAYOUT_LINEAR); + check_hr("null computer", XGCreateTextureComputer(&desc, nullptr), E_POINTER); + + desc.format = 1u; + object = reinterpret_cast(static_cast(1)); + check_hr("unknown format", XGCreateTextureComputer(&desc, &object), E_INVALIDARG); + check(object == nullptr, "unknown format clears the output"); + + desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 0, 4, 1, 1, BX_XG_LAYOUT_LINEAR); + check_hr("zero width", XGCreateTextureComputer(&desc, &object), E_INVALIDARG); + + desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 16385, 1, 1, 1, BX_XG_LAYOUT_LINEAR); + check_hr("width above the cap", XGCreateTextureComputer(&desc, &object), E_INVALIDARG); + + desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 4, 4, 2049, 1, BX_XG_LAYOUT_LINEAR); + check_hr("array above the cap", XGCreateTextureComputer(&desc, &object), E_INVALIDARG); + + desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 8, 8, 1, 5, BX_XG_LAYOUT_LINEAR); + check_hr("too many mips", XGCreateTextureComputer(&desc, &object), E_INVALIDARG); + + desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 4, 4, 1, 1, BX_XG_LAYOUT_LINEAR); + desc.dimension = BX_XG_DIMENSION_TEXTURE3D; + check_hr("texture3d", XGCreateTextureComputer(&desc, &object), E_NOTIMPL); + + desc.dimension = 0u; + check_hr("unknown dimension", XGCreateTextureComputer(&desc, &object), E_INVALIDARG); + + desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 4, 4, 1, 1, BX_XG_LAYOUT_LINEAR); + desc.sample_desc.count = 4u; + check_hr("msaa", XGCreateTextureComputer(&desc, &object), E_NOTIMPL); + + desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 4, 4, 1, 1, 9u); + check_hr("unknown layout", XGCreateTextureComputer(&desc, &object), E_INVALIDARG); + + const uint32_t linear = XGSuggestSwizzleMode(BX_XG_DIMENSION_TEXTURE2D, DXGI_FORMAT_R8G8B8A8_UNORM, 64, 32, 1, 0, 1, 0); + const uint32_t linear_again = XGSuggestSwizzleMode(BX_XG_DIMENSION_TEXTURE2D, DXGI_FORMAT_R8G8B8A8_UNORM, 64, 32, 1, 0, 1, 0); + check(linear == BX_XG_LAYOUT_LINEAR, "suggest linear rgba8"); + check(linear_again == linear, "suggest is stable"); + check(XGSuggestSwizzleMode(BX_XG_DIMENSION_TEXTURE2D, DXGI_FORMAT_BC7_UNORM, 32, 32, 1, 0, 1, 0) == BX_XG_LAYOUT_LINEAR, + "suggest linear bc7"); + check(XGSuggestSwizzleMode(BX_XG_DIMENSION_TEXTURE2D, 1u, 64, 32, 1, 0, 1, 0) == BX_XG_LAYOUT_NONE, + "suggest rejects an unknown format"); + check(XGSuggestSwizzleMode(BX_XG_DIMENSION_TEXTURE2D, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 32, 1, 0, 1, 0) == BX_XG_LAYOUT_NONE, + "suggest rejects zero width"); + check(XGSuggestSwizzleMode(BX_XG_DIMENSION_TEXTURE3D, DXGI_FORMAT_R8G8B8A8_UNORM, 64, 32, 1, 0, 1, 0) == BX_XG_LAYOUT_NONE, + "suggest does not invent a 3D mode"); + check(XGSuggestSwizzleMode(BX_XG_DIMENSION_TEXTURE2D, DXGI_FORMAT_BC1_UNORM, 8, 8, 1, 0, 1, 1) == BX_XG_LAYOUT_NONE, + "suggest rejects flags"); +} + +static void test_hand_layouts() { + bx_xg_placed_footprint layouts[8]{}; + uint32_t rows[8]{}; + uint64_t row_bytes[8]{}; + uint64_t total = 0; + uint64_t size = 0; + uint64_t align = 0; + uint64_t offset = 0; + + bx_xg_resource_desc desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 8, 8, 1, 0, BX_XG_LAYOUT_LINEAR); + bx_xg_texture_computer* computer = create_ok(&desc, "create rgba 8x8"); + if (!computer) { + return; + } + check_hr("rgba full layout", + computer->lpVtbl->GetResourceLayout(computer, 0, 4, 0, layouts, rows, row_bytes, &total), + S_OK); + check_hr("rgba size", computer->lpVtbl->GetResourceSizeBytes(computer, &size), S_OK); + check_hr("rgba align", computer->lpVtbl->GetResourceBaseAlignmentBytes(computer, &align), S_OK); + check(size == total && total == 4096u, "rgba 8x8 chain is 4096 bytes"); + check(align == 512u, "linear base alignment is 512"); + /* mip0 8x8: 32 -> 256 pitch, 8 rows, 2048 payload, next 2048. */ + check(layouts[0].offset == 0u && rows[0] == 8u && row_bytes[0] == 32u && layouts[0].footprint.row_pitch == 256u && + layouts[0].footprint.width == 8u, + "rgba mip0"); + /* mip1 4x4: 16 -> 256, 4 rows, offset 2048. */ + check(layouts[1].offset == 2048u && rows[1] == 4u && row_bytes[1] == 16u && layouts[1].footprint.width == 4u, + "rgba mip1"); + /* mip2 2x2: 8 -> 256, 2 rows, offset 3072. */ + check(layouts[2].offset == 3072u && rows[2] == 2u && row_bytes[2] == 8u && layouts[2].footprint.width == 2u, + "rgba mip2"); + /* mip3 1x1: 4 -> 256, 1 row, offset 3584. total align(3840, 512) = 4096. */ + check(layouts[3].offset == 3584u && rows[3] == 1u && row_bytes[3] == 4u && layouts[3].footprint.width == 1u, + "rgba mip3"); + check_hr("rgba mip1 offset", computer->lpVtbl->GetMipLevelOffsetBytes(computer, 0, 1, &offset), S_OK); + check(offset == 2048u, "mip helper matches the footprint"); + check_hr("rgba texel", computer->lpVtbl->GetTexelElementOffsetBytes(computer, 0, 1, 3, 1, 0, &offset), S_OK); + check(offset == 2316u, "rgba mip1 texel (3,1) is 2316"); + uint64_t sentinel = 0x1122334455667788ull; + check_hr("texel x out of range", + computer->lpVtbl->GetTexelElementOffsetBytes(computer, 0, 0, 8, 0, 0, &sentinel), + E_INVALIDARG); + check(sentinel == 0x1122334455667788ull, "rejected texel leaves the output unchanged"); + check_hr("texel z", computer->lpVtbl->GetTexelElementOffsetBytes(computer, 0, 0, 0, 0, 1, &sentinel), E_INVALIDARG); + check_hr("mip out of range", computer->lpVtbl->GetMipLevelOffsetBytes(computer, 0, 4, &sentinel), E_INVALIDARG); + release_computer(computer); + + desc = desc_2d(DXGI_FORMAT_BC1_UNORM, 8, 8, 1, 2, BX_XG_LAYOUT_LINEAR); + computer = create_ok(&desc, "create bc1 8x8"); + if (!computer) { + return; + } + check_hr("bc1 layout", computer->lpVtbl->GetResourceLayout(computer, 0, 2, 0, layouts, rows, row_bytes, &total), S_OK); + /* 2x2 blocks of 8 bytes -> row 16, pitch 256, 2 rows, payload 512. mip1 one 8-byte block at 512. */ + check(layouts[0].offset == 0u && rows[0] == 2u && row_bytes[0] == 16u && total == 1024u, "bc1 mip0"); + check(layouts[1].offset == 512u && rows[1] == 1u && row_bytes[1] == 8u && layouts[1].footprint.width == 4u, "bc1 mip1"); + check_hr("bc1 texel", computer->lpVtbl->GetTexelElementOffsetBytes(computer, 0, 0, 4, 0, 0, &offset), S_OK); + check(offset == 8u, "bc1 texel x=4 is the second block"); + check_hr("bc1 texel corner", computer->lpVtbl->GetTexelElementOffsetBytes(computer, 0, 0, 7, 7, 0, &offset), S_OK); + check(offset == 264u, "bc1 texel (7,7) is block (1,1)"); + release_computer(computer); + + desc = desc_2d(DXGI_FORMAT_BC7_UNORM, 4, 4, 1, 1, BX_XG_LAYOUT_LINEAR); + computer = create_ok(&desc, "create bc7 4x4"); + if (!computer) { + return; + } + check_hr("bc7 layout", computer->lpVtbl->GetResourceLayout(computer, 0, 1, 0, layouts, rows, row_bytes, &total), S_OK); + check(row_bytes[0] == 16u && rows[0] == 1u && layouts[0].footprint.row_pitch == 256u && total == 512u, "bc7 4x4"); + release_computer(computer); + + desc = desc_2d(DXGI_FORMAT_BC1_UNORM, 4, 4, 1, 1, BX_XG_LAYOUT_LINEAR); + computer = create_ok(&desc, "create bc1 4x4"); + if (!computer) { + return; + } + check_hr("bc1 4x4 layout", computer->lpVtbl->GetResourceLayout(computer, 0, 1, 0, layouts, rows, row_bytes, &total), S_OK); + check(row_bytes[0] == 8u && total == 512u, "bc1 4x4 row is 8 bytes"); + release_computer(computer); + + desc = desc_2d(DXGI_FORMAT_BC1_UNORM, 128, 4, 1, 1, BX_XG_LAYOUT_LINEAR); + computer = create_ok(&desc, "create bc1 128"); + if (!computer) { + return; + } + check_hr("bc1 128 layout", computer->lpVtbl->GetResourceLayout(computer, 0, 1, 0, layouts, rows, row_bytes, &total), S_OK); + check(row_bytes[0] == 256u && layouts[0].footprint.row_pitch == 256u && total == 512u, "bc1 128x4 pitch"); + release_computer(computer); + + desc = desc_2d(DXGI_FORMAT_BC7_UNORM, 128, 4, 1, 1, BX_XG_LAYOUT_LINEAR); + computer = create_ok(&desc, "create bc7 128"); + if (!computer) { + return; + } + check_hr("bc7 128 layout", computer->lpVtbl->GetResourceLayout(computer, 0, 1, 0, layouts, rows, row_bytes, &total), S_OK); + check(row_bytes[0] == 512u && layouts[0].footprint.row_pitch == 512u && total == 512u, "bc7 128x4 pitch"); + release_computer(computer); + + desc = desc_2d(DXGI_FORMAT_BC1_UNORM, 5, 3, 1, 1, BX_XG_LAYOUT_LINEAR); + computer = create_ok(&desc, "create bc1 partial"); + if (!computer) { + return; + } + check_hr("bc1 partial layout", computer->lpVtbl->GetResourceLayout(computer, 0, 1, 0, layouts, rows, row_bytes, &total), S_OK); + /* ceil(5/4)=2 columns, ceil(3/4)=1 row, 16 byte row, payload 256, total 512. */ + check(rows[0] == 1u && row_bytes[0] == 16u && layouts[0].footprint.width == 5u && total == 512u, "bc1 5x3"); + release_computer(computer); + + desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 4, 4, 2, 1, BX_XG_LAYOUT_LINEAR); + computer = create_ok(&desc, "create array"); + if (!computer) { + return; + } + check_hr("array layout", computer->lpVtbl->GetResourceLayout(computer, 0, 2, 0, layouts, rows, row_bytes, &total), S_OK); + check(layouts[0].offset == 0u && layouts[1].offset == 1024u && total == 2048u, "array offsets"); + check_hr("array mip offset", computer->lpVtbl->GetMipLevelOffsetBytes(computer, 1, 0, &offset), S_OK); + check(offset == 1024u, "array slice 1 offset"); + check_hr("array texel", computer->lpVtbl->GetTexelElementOffsetBytes(computer, 1, 0, 0, 1, 0, &offset), S_OK); + check(offset == 1280u, "array slice 1 texel y=1"); + check_hr("partial range", computer->lpVtbl->GetResourceLayout(computer, 1, 1, 0, layouts, rows, row_bytes, &total), S_OK); + check(layouts[0].offset == 1024u && total == 2048u, "partial range keeps the absolute offset"); + + std::memset(layouts, 0x11, sizeof(layouts)); + total = 0x2222u; + check_hr("unaligned base", + computer->lpVtbl->GetResourceLayout(computer, 0, 1, 256, layouts, rows, row_bytes, &total), + E_INVALIDARG); + check(layouts[0].offset == 0x1111111111111111ull && total == 0x2222u, "failed layout does not write"); + check_hr("shifted base", computer->lpVtbl->GetResourceLayout(computer, 0, 1, 512, layouts, rows, row_bytes, &total), S_OK); + check(layouts[0].offset == 512u && total == 1024u, "base offset 512 shifts the footprint"); + release_computer(computer); + + const uint32_t rgba_formats[] = { + DXGI_FORMAT_R8G8B8A8_TYPELESS, + DXGI_FORMAT_R8G8B8A8_UNORM, + DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, + DXGI_FORMAT_R8G8B8A8_UINT, + DXGI_FORMAT_R8G8B8A8_SNORM, + DXGI_FORMAT_R8G8B8A8_SINT, + }; + for (uint32_t format : rgba_formats) { + desc = desc_2d(format, 1, 1, 1, 1, BX_XG_LAYOUT_LINEAR); + computer = create_ok(&desc, "rgba variant"); + if (!computer) { + return; + } + check_hr("rgba variant layout", + computer->lpVtbl->GetResourceLayout(computer, 0, 1, 0, layouts, rows, row_bytes, &total), + S_OK); + check(row_bytes[0] == 4u && total == 512u && layouts[0].footprint.format == format, "rgba variant size"); + release_computer(computer); + } + + desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 16384, 1, 1, 1, BX_XG_LAYOUT_LINEAR); + computer = create_ok(&desc, "create max edge"); + if (!computer) { + return; + } + check_hr("max edge layout", computer->lpVtbl->GetResourceLayout(computer, 0, 1, 0, layouts, rows, row_bytes, &total), S_OK); + check(row_bytes[0] == 65536u && layouts[0].footprint.row_pitch == 65536u && total == 65536u, "16384 rgba row"); + release_computer(computer); + + desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 1, 1, 2048, 1, BX_XG_LAYOUT_LINEAR); + computer = create_ok(&desc, "create max array"); + if (!computer) { + return; + } + check_hr("max array size", computer->lpVtbl->GetResourceSizeBytes(computer, &size), S_OK); + check(size == 1048576u, "2048 slices of 512 bytes"); + release_computer(computer); +} + +static void test_monotonic_chain(uint32_t format, uint32_t width, uint32_t height, const char* name) { + bx_xg_resource_desc desc = desc_2d(format, width, height, 1, 0, BX_XG_LAYOUT_LINEAR); + bx_xg_texture_computer* computer = create_ok(&desc, name); + if (!computer) { + return; + } + bx_xg_placed_footprint layouts[16]{}; + uint32_t rows[16]{}; + uint64_t row_bytes[16]{}; + uint64_t total = 0; + uint32_t count = 1u; + while (count < 16u) { + const HRESULT hr = computer->lpVtbl->GetMipLevelOffsetBytes(computer, 0, count, &total); + if (hr != S_OK) { + break; + } + count += 1u; + } + check(count >= 1u && count < 16u, "mip chain fits the stack buffer"); + check_hr(name, computer->lpVtbl->GetResourceLayout(computer, 0, count, 0, layouts, rows, row_bytes, &total), S_OK); + uint64_t reported = 0; + check_hr(name, computer->lpVtbl->GetResourceSizeBytes(computer, &reported), S_OK); + check(reported == total && (total % 512u) == 0u, "size matches the placed chain"); + for (uint32_t i = 0; i < count; ++i) { + check((layouts[i].offset % 512u) == 0u, "subresource offset is placement aligned"); + check((layouts[i].footprint.row_pitch % 256u) == 0u, "row pitch is 256 aligned"); + check(layouts[i].footprint.row_pitch >= row_bytes[i], "pitch covers the logical row"); + check(layouts[i].footprint.width >= 1u && layouts[i].footprint.height >= 1u, "mip is at least one texel"); + check(rows[i] >= 1u, "mip has a row"); + if (i > 0u) { + check(layouts[i].offset > layouts[i - 1u].offset, "subresource offsets increase"); + } + } + check(total > layouts[count - 1u].offset, "total extends past the last offset"); + release_computer(computer); +} + +static uint8_t* alloc_bytes(size_t size, uint8_t fill) { + uint8_t* data = static_cast(std::malloc(size)); + if (!data) { + check(false, "malloc"); + return nullptr; + } + std::memset(data, fill, size); + return data; +} + +static void test_round_trip( + uint32_t format, + uint32_t width, + uint32_t height, + uint32_t subresource, + size_t logical_row, + size_t logical_rows, + size_t resource_bytes, + uint64_t placed_row, + const char* name +) { + bx_xg_resource_desc desc = desc_2d(format, width, height, 1, 0, BX_XG_LAYOUT_LINEAR); + if (subresource > 0u) { + desc.mip_levels = static_cast(subresource + 1u); + } + bx_xg_texture_computer* computer = create_ok(&desc, name); + if (!computer) { + return; + } + + const size_t logical_bytes = logical_row * logical_rows; + const size_t pitched_row = logical_row + 8u; + const size_t pitched_bytes = pitched_row * logical_rows; + uint8_t* logical = alloc_bytes(logical_bytes, 0); + uint8_t* pitched = alloc_bytes(pitched_bytes, 0xFF); + uint8_t* resource = alloc_bytes(resource_bytes + 32u, 0); + uint8_t* readback = alloc_bytes(logical_bytes + 32u, 0xA5); + if (!logical || !pitched || !resource || !readback) { + std::free(logical); + std::free(pitched); + std::free(resource); + std::free(readback); + release_computer(computer); + return; + } + std::memset(resource + resource_bytes, 0xA5, 32u); + for (size_t i = 0; i < logical_bytes; ++i) { + logical[i] = static_cast((i * 13u + 7u) & 0xFFu); + } + for (size_t y = 0; y < logical_rows; ++y) { + std::memcpy(pitched + y * pitched_row, logical + y * logical_row, logical_row); + } + + check_hr(name, + computer->lpVtbl->CopyIntoSubresource( + computer, resource, resource_bytes, subresource, pitched, pitched_bytes, static_cast(pitched_row), 0), + S_OK); + check(bytes_are(resource + resource_bytes, 32u, 0xA5), "copy leaves the canary"); + check(resource[0] == (subresource == 0u ? logical[0] : 0u), "earlier bytes stay put when the mip is not zero"); + if (subresource == 0u && logical_rows > 1u) { + check(resource[placed_row] == logical[logical_row], "second row uses the placed pitch"); + check(resource[logical_row] == 0u, "padding past the logical row stays zero"); + } + + std::memset(readback, 0xA5, logical_bytes + 32u); + check_hr(name, + computer->lpVtbl->CopyFromSubresource( + computer, readback, logical_bytes, 0, 0, resource, resource_bytes, subresource), + S_OK); + check(std::memcmp(readback, logical, logical_bytes) == 0, "round trip is byte identical"); + check(bytes_are(readback + logical_bytes, 32u, 0xA5), "readback leaves its canary"); + + std::memset(resource, 0x5A, resource_bytes); + check_hr("short destination", + computer->lpVtbl->CopyIntoSubresource( + computer, resource, 1, subresource, logical, logical_bytes, 0, 0), + E_INVALIDARG); + check(bytes_are(resource, resource_bytes, 0x5A), "rejected copy writes nothing"); + + check_hr("null copy source", + computer->lpVtbl->CopyIntoSubresource(computer, resource, resource_bytes, subresource, nullptr, logical_bytes, 0, 0), + E_POINTER); + check(bytes_are(resource, resource_bytes, 0x5A), "null source writes nothing"); + + std::free(logical); + std::free(pitched); + std::free(resource); + std::free(readback); + release_computer(computer); +} + +static void test_copies() { + /* RGBA8 6x3: logical row 24, placed pitch 256, 3 rows, resource 1024. */ + test_round_trip(DXGI_FORMAT_R8G8B8A8_UNORM, 6, 3, 0, 24, 3, 1024, 256, "rgba round trip"); + /* BC7 6x5: 2 x 2 blocks, logical row 32, placed pitch 256, resource 512. */ + test_round_trip(DXGI_FORMAT_BC7_UNORM, 6, 5, 0, 32, 2, 512, 256, "bc7 round trip"); + /* BC1 5x3: one 16-byte block row, resource 512. */ + test_round_trip(DXGI_FORMAT_BC1_UNORM, 5, 3, 0, 16, 1, 512, 256, "bc1 round trip"); + /* BC1 8x8 mip1 is a single 8-byte block at offset 512. Resource total is 1024. */ + test_round_trip(DXGI_FORMAT_BC1_UNORM, 8, 8, 1, 8, 1, 1024, 256, "bc1 mip1 round trip"); +} + +static void test_swizzle_placeholder() { + const uint32_t modes[] = {BX_XG_LAYOUT_SW_64KB_S, BX_XG_LAYOUT_SW_64KB_D, BX_XG_LAYOUT_SW_64KB_R}; + for (uint32_t mode : modes) { + bx_xg_resource_desc desc = desc_2d(DXGI_FORMAT_BC7_UNORM, 32, 32, 1, 0, mode); + void* object = nullptr; + check_hr("create tiled", XGCreateTextureComputer(&desc, &object), S_OK); + auto* computer = static_cast(object); + if (!computer) { + continue; + } + bx_xg_placed_footprint layout{}; + std::memset(&layout, 0xEE, sizeof(layout)); + uint32_t rows = 0xEEEEEEEEu; + uint64_t row_bytes = 0xEEEEEEEEEEEEEEEEull; + uint64_t total = 0xEEEEEEEEEEEEEEEEull; + check_hr("tiled layout", + computer->lpVtbl->GetResourceLayout(computer, 0, 1, 0, &layout, &rows, &row_bytes, &total), + E_NOTIMPL); + check(layout.offset == 0xEEEEEEEEEEEEEEEEull && rows == 0xEEEEEEEEu && total == 0xEEEEEEEEEEEEEEEEull, + "tiled layout writes nothing"); + + uint64_t size = 0x0123456789ABCDEFull; + check_hr("tiled size", computer->lpVtbl->GetResourceSizeBytes(computer, &size), E_NOTIMPL); + check_hr("tiled align", computer->lpVtbl->GetResourceBaseAlignmentBytes(computer, &size), E_NOTIMPL); + check_hr("tiled mip", computer->lpVtbl->GetMipLevelOffsetBytes(computer, 0, 0, &size), E_NOTIMPL); + check_hr("tiled texel", computer->lpVtbl->GetTexelElementOffsetBytes(computer, 0, 0, 0, 0, 0, &size), E_NOTIMPL); + check(size == 0x0123456789ABCDEFull, "tiled queries write nothing"); + + uint8_t dest[16]; + uint8_t src[16]; + std::memset(dest, 0x3C, sizeof(dest)); + std::memset(src, 0x5A, sizeof(src)); + check_hr("tiled copy", + computer->lpVtbl->CopyIntoSubresource(computer, dest, sizeof(dest), 0, src, sizeof(src), 0, 0), + E_NOTIMPL); + check(bytes_are(dest, sizeof(dest), 0x3C), "tiled copy writes nothing"); + release_computer(computer); + } + check(XGSuggestSwizzleMode(BX_XG_DIMENSION_TEXTURE2D, DXGI_FORMAT_BC7_UNORM, 32, 32, 1, 0, 1, 0) == BX_XG_LAYOUT_LINEAR, + "suggest stays on the implemented mode"); +} + +static void test_refcount() { + bx_xg_resource_desc desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 4, 2, 1, 1, BX_XG_LAYOUT_LINEAR); + bx_xg_texture_computer* computer = create_ok(&desc, "create refcount"); + if (!computer) { + return; + } + check(computer->lpVtbl->AddRef(nullptr) == 0u, "AddRef null"); + check(computer->lpVtbl->Release(nullptr) == 0u, "Release null"); + void* missing = reinterpret_cast(static_cast(1)); + check_hr("QueryInterface null", computer->lpVtbl->QueryInterface(nullptr, &BX_IID_IUnknown, &missing), E_POINTER); + check(missing == reinterpret_cast(static_cast(1)), "null QueryInterface leaves its output"); + check(computer->lpVtbl->AddRef(computer) == 2u, "AddRef 2"); + check(computer->lpVtbl->AddRef(computer) == 3u, "AddRef 3"); + check(computer->lpVtbl->Release(computer) == 2u, "Release 2"); + + void* extra = nullptr; + check_hr("QI IUnknown", computer->lpVtbl->QueryInterface(computer, &BX_IID_IUnknown, &extra), S_OK); + check(extra == computer, "IUnknown is the object"); + check(computer->lpVtbl->Release(computer) == 2u, "release QI"); + + check_hr("QI computer", computer->lpVtbl->QueryInterface(computer, &BX_IID_IXGTextureAddressComputer, &extra), S_OK); + check(extra == computer, "computer IID is the object"); + check(computer->lpVtbl->Release(computer) == 2u, "release computer IID"); + + GUID other{}; + other.Data1 = 0x11111111u; + void* rejected = reinterpret_cast(static_cast(1)); + check_hr("QI unknown", computer->lpVtbl->QueryInterface(computer, &other, &rejected), E_NOINTERFACE); + check(rejected == nullptr, "unknown IID clears the output"); + + uint64_t offset = 0; + check_hr("texel after QI", computer->lpVtbl->GetTexelElementOffsetBytes(computer, 0, 0, 1, 1, 0, &offset), S_OK); + /* 4x2 rgba, pitch 256. (1,1) = 256 + 4. */ + check(offset == 260u, "rgba 4x2 texel (1,1)"); + + check(computer->lpVtbl->Release(computer) == 1u, "Release 1"); + check(computer->lpVtbl->Release(computer) == 0u, "Release 0"); +} + +int main() { + test_arguments(); + test_hand_layouts(); + test_monotonic_chain(DXGI_FORMAT_R8G8B8A8_UNORM, 64, 48, "rgba chain"); + test_monotonic_chain(DXGI_FORMAT_BC1_UNORM, 33, 17, "bc1 chain"); + test_monotonic_chain(DXGI_FORMAT_BC7_UNORM, 32, 32, "bc7 chain"); + test_copies(); + test_swizzle_placeholder(); + test_refcount(); + + if (g_failures != 0) { + std::fprintf(stderr, "FAIL: test_xg_xs (%d failures / %d checks)\n", g_failures, g_checks); + return 1; + } + std::printf("PASS: test_xg_xs (%d checks)\n", g_checks); + return 0; +} From ce5a0a6e43c78de29b61e8e1cc84a849843c6612 Mon Sep 17 00:00:00 2001 From: devil8blade <360741796+devil8blade@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:44:47 +0000 Subject: [PATCH 2/3] xg_xs: log unimplemented layouts and round BC footprints Name TEXTURE1D, TEXTURE3D, and MSAA in the unimplemented log, and include the SW_64KB mode in tiled vtable traces. Footprints now use D3D12 block-rounded dimensions, and tiled CopyFromSubresource is tested. Signed-off-by: devil8blade <360741796+devil8blade@users.noreply.github.com> --- shims/xg_xs/xg_texture.h | 13 ++++++++-- shims/xg_xs/xg_xs.cpp | 34 +++++++++++++++++++++++---- tests/conformance/test_xg_xs/main.cpp | 29 +++++++++++++++++++++-- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/shims/xg_xs/xg_texture.h b/shims/xg_xs/xg_texture.h index 38464710..6672ff72 100644 --- a/shims/xg_xs/xg_texture.h +++ b/shims/xg_xs/xg_texture.h @@ -7,6 +7,8 @@ * Field meanings follow the public D3D12 resource and footprint documents * (D3D12_RESOURCE_DIMENSION, D3D12_SUBRESOURCE_FOOTPRINT, and * D3D12_TEXTURE_DATA_PITCH_ALIGNMENT / D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT). + * Block-compressed footprint width and height are texels rounded up to the + * 4x4 block, the same rounding D3D12 uses for a copyable footprint. * Format numbers are the public DXGI_FORMAT values. This is not the Xbox * GDK XG interface and it is not an Xbox hardware result. */ @@ -75,8 +77,8 @@ typedef struct bx_xg_resource_desc { typedef struct bx_xg_subresource_footprint { uint32_t format; - uint32_t width; /* texels */ - uint32_t height; /* texels */ + uint32_t width; /* texels, rounded up to the format block width */ + uint32_t height; /* texels, rounded up to the format block height */ uint32_t depth; uint32_t row_pitch; /* bytes, multiple of BX_XG_LINEAR_PITCH_ALIGNMENT */ } bx_xg_subresource_footprint; @@ -94,6 +96,13 @@ static const GUID BX_IID_IXGTextureAddressComputer = { static const GUID BX_IID_IUnknown = { 0x00000000u, 0x0000u, 0x0000u, {0xC0u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x46u}}; +/* + * GetResourceLayout places every subresource at its absolute offset from + * base_offset, including subresources before first_subresource. total_bytes + * runs from base_offset through the aligned end of the last requested + * subresource, so a partial range still reports a total that covers the + * earlier subresources. + */ typedef struct bx_xg_texture_computer bx_xg_texture_computer; typedef struct bx_xg_texture_computer_vtbl { diff --git a/shims/xg_xs/xg_xs.cpp b/shims/xg_xs/xg_xs.cpp index 66464ad1..3bfc09c1 100644 --- a/shims/xg_xs/xg_xs.cpp +++ b/shims/xg_xs/xg_xs.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "bx_log.h" @@ -12,6 +13,7 @@ * Linear 2D layouts follow the public D3D12 copy footprint rules: * mip size = max(1, base >> mip) * block count = ceil(texels / block), at least 1 + * footprint = block count times the block size (D3D12 rounds texels up) * row bytes = block_columns * bytes_per_block * row pitch = align_up(row_bytes, 256) * subresource = row_pitch * block_rows @@ -58,8 +60,10 @@ struct TextureComputer { struct Placement { uint64_t offset; - uint32_t width; + uint32_t width; /* logical texels, used for element bounds */ uint32_t height; + uint32_t footprint_width; /* texels rounded up to the block */ + uint32_t footprint_height; uint32_t depth; uint32_t row_pitch; uint32_t row_count; @@ -211,7 +215,12 @@ static bool layout_is_tiled(uint32_t layout) { } static HRESULT validate_desc(const bx_xg_resource_desc* desc, Resolved* out) { - if (desc->dimension == BX_XG_DIMENSION_TEXTURE1D || desc->dimension == BX_XG_DIMENSION_TEXTURE3D) { + if (desc->dimension == BX_XG_DIMENSION_TEXTURE1D) { + BX_STUB_LOG("xg_xs.dll", "XGCreateTextureComputer TEXTURE1D"); + return E_NOTIMPL; + } + if (desc->dimension == BX_XG_DIMENSION_TEXTURE3D) { + BX_STUB_LOG("xg_xs.dll", "XGCreateTextureComputer TEXTURE3D"); return E_NOTIMPL; } if (desc->dimension != BX_XG_DIMENSION_TEXTURE2D) { @@ -234,6 +243,7 @@ static HRESULT validate_desc(const bx_xg_resource_desc* desc, Resolved* out) { return E_INVALIDARG; } if (desc->sample_desc.count != 1u) { + BX_STUB_LOG("xg_xs.dll", "XGCreateTextureComputer MSAA"); return E_NOTIMPL; } if (desc->layout != BX_XG_LAYOUT_LINEAR && !layout_is_tiled(desc->layout)) { @@ -307,7 +317,16 @@ static HRESULT reject_if_tiled(const TextureComputer* obj, uint32_t slot, const query.texel_y = 0u; const HRESULT hr = rdna2_sw64kb_prepare(&query); if (hr == E_NOTIMPL) { - BX_VTABLE_STUB_LOG("IXGTextureAddressComputer", slot, method); + const Rdna2TileMode* mode = nullptr; + for (const Rdna2TileMode& candidate : kRdna2TileModes) { + if (candidate.mode == obj->resolved.layout) { + mode = &candidate; + break; + } + } + char label[80]; + std::snprintf(label, sizeof(label), "%s %s", method, mode && mode->name ? mode->name : "SW_64KB"); + BX_VTABLE_STUB_LOG("IXGTextureAddressComputer", slot, label); } return hr; } @@ -325,9 +344,14 @@ static HRESULT measure_mip(const Resolved* resolved, uint32_t mip, Placement* pl if (!align_up_u64(row_bytes, kPitchAlignment, &row_pitch) || row_pitch > UINT32_MAX) { return E_INVALIDARG; } + if (blocks_w > UINT32_MAX / resolved->block_w || blocks_h > UINT32_MAX / resolved->block_h) { + return E_INVALIDARG; + } place->offset = 0; place->width = width; place->height = height; + place->footprint_width = blocks_w * resolved->block_w; + place->footprint_height = blocks_h * resolved->block_h; place->depth = 1u; place->row_pitch = static_cast(row_pitch); place->row_count = blocks_h; @@ -421,8 +445,8 @@ static void sink_layout(void* ctx, uint32_t slot, const Placement* place) { auto* out = static_cast(ctx); out->layouts[slot].offset = place->offset; out->layouts[slot].footprint.format = place->format; - out->layouts[slot].footprint.width = place->width; - out->layouts[slot].footprint.height = place->height; + out->layouts[slot].footprint.width = place->footprint_width; + out->layouts[slot].footprint.height = place->footprint_height; out->layouts[slot].footprint.depth = place->depth; out->layouts[slot].footprint.row_pitch = place->row_pitch; out->row_counts[slot] = place->row_count; diff --git a/tests/conformance/test_xg_xs/main.cpp b/tests/conformance/test_xg_xs/main.cpp index 6b60ef49..be2d3716 100644 --- a/tests/conformance/test_xg_xs/main.cpp +++ b/tests/conformance/test_xg_xs/main.cpp @@ -237,8 +237,27 @@ static void test_hand_layouts() { return; } check_hr("bc1 partial layout", computer->lpVtbl->GetResourceLayout(computer, 0, 1, 0, layouts, rows, row_bytes, &total), S_OK); - /* ceil(5/4)=2 columns, ceil(3/4)=1 row, 16 byte row, payload 256, total 512. */ - check(rows[0] == 1u && row_bytes[0] == 16u && layouts[0].footprint.width == 5u && total == 512u, "bc1 5x3"); + /* 5x3 texels round to an 8x4 footprint: 2 block columns, 1 block row, 16 byte row, total 512. */ + check(rows[0] == 1u && row_bytes[0] == 16u && layouts[0].footprint.width == 8u && + layouts[0].footprint.height == 4u && total == 512u, + "bc1 5x3"); + check_hr("bc1 5x3 texel", computer->lpVtbl->GetTexelElementOffsetBytes(computer, 0, 0, 4, 2, 0, &offset), S_OK); + check(offset == 8u, "bc1 5x3 texel (4,2) stays inside the logical 5x3"); + offset = 0x99u; + check_hr("bc1 past logical width", + computer->lpVtbl->GetTexelElementOffsetBytes(computer, 0, 0, 5, 0, 0, &offset), + E_INVALIDARG); + check(offset == 0x99u, "padding texels outside 5x3 are not addressable"); + release_computer(computer); + + desc = desc_2d(DXGI_FORMAT_BC7_UNORM, 6, 5, 1, 1, BX_XG_LAYOUT_LINEAR); + computer = create_ok(&desc, "create bc7 6x5"); + if (!computer) { + return; + } + check_hr("bc7 6x5 layout", computer->lpVtbl->GetResourceLayout(computer, 0, 1, 0, layouts, rows, row_bytes, &total), S_OK); + check(layouts[0].footprint.width == 8u && layouts[0].footprint.height == 8u && rows[0] == 2u && row_bytes[0] == 32u, + "bc7 6x5 rounds to 8x8"); release_computer(computer); desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 4, 4, 2, 1, BX_XG_LAYOUT_LINEAR); @@ -480,6 +499,12 @@ static void test_swizzle_placeholder() { computer->lpVtbl->CopyIntoSubresource(computer, dest, sizeof(dest), 0, src, sizeof(src), 0, 0), E_NOTIMPL); check(bytes_are(dest, sizeof(dest), 0x3C), "tiled copy writes nothing"); + std::memset(dest, 0x3C, sizeof(dest)); + check_hr("tiled copy from", + computer->lpVtbl->CopyFromSubresource(computer, dest, sizeof(dest), 0, 0, src, sizeof(src), 0), + E_NOTIMPL); + check(bytes_are(dest, sizeof(dest), 0x3C), "tiled copy from writes nothing"); + check(bytes_are(src, sizeof(src), 0x5A), "tiled copy from leaves the source"); release_computer(computer); } check(XGSuggestSwizzleMode(BX_XG_DIMENSION_TEXTURE2D, DXGI_FORMAT_BC7_UNORM, 32, 32, 1, 0, 1, 0) == BX_XG_LAYOUT_LINEAR, From f9ecde4e95ff0ff432cc768489e929f242df0de6 Mon Sep 17 00:00:00 2001 From: devil8blade <360741796+devil8blade@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:45:46 +0000 Subject: [PATCH 3/3] xg_xs: stage overlapping copies and split the size contract Stage a source span before an in-place pitch change so later rows stay intact. Report a D3D12 copy_end beside the aligned allocation span, and look up mip offsets from a table built at creation. Signed-off-by: devil8blade <360741796+devil8blade@users.noreply.github.com> --- CHANGELOG.md | 2 +- shims/xg_xs/xg_texture.h | 15 +- shims/xg_xs/xg_xs.cpp | 320 ++++++++++++++++++-------- tests/conformance/test_xg_xs/main.cpp | 88 ++++++- 4 files changed, 324 insertions(+), 101 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a2577a8..61686711 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ Guest execution uses original, purpose-built test programs. Commercial Xbox game remains unsupported. ### Added -- `xg_xs.dll` computes linear 2D layouts for `R8G8B8A8` and BC1 through BC7 (`XGCreateTextureComputer`, `XGSuggestSwizzleMode`, and COM `IXGTextureAddressComputer`). Row pitch uses the public D3D12 256-byte alignment and each subresource is placed on the public 512-byte alignment. Sizes are overflow-checked, null pointers return `E_POINTER`, and copies stay inside the caller buffers. `SW_64KB_S`, `SW_64KB_D`, and `SW_64KB_R` return `E_NOTIMPL` and write no address. Synthetic `test_xg_xs.exe` checks hand-computed mip offsets, alignment, refcounts, and a byte-identical copy round trip. This is not an Xbox hardware result (#415). +- `xg_xs.dll` computes linear 2D layouts for `R8G8B8A8` and BC1 through BC7 (`XGCreateTextureComputer`, `XGSuggestSwizzleMode`, and COM `IXGTextureAddressComputer`). Row pitch uses the public D3D12 256-byte alignment and each subresource is placed on the public 512-byte alignment. `GetResourceLayout` reports that aligned allocation span in `total_bytes` (512 for a 1x1 RGBA8 texture) and a separate D3D12-style `copy_end` on each footprint (4 for that same texture). Sizes are overflow-checked, null pointers return `E_POINTER`, and copies stay inside the caller buffers. `SW_64KB_S`, `SW_64KB_D`, and `SW_64KB_R` return `E_NOTIMPL` and write no address. Synthetic `test_xg_xs.exe` checks hand-computed mip offsets, alignment, refcounts, and a byte-identical copy round trip. This is not an Xbox hardware result (#415). - `dxbc-accc` parses a synthetic FSPX section table inside DXBC `ACCC` chunks (section descriptors, section 1 stage configuration using the `bx_shader_stage_t` values, and section 6 RDNA 2 code bounds) and returns a parse error for truncated or out-of-range input instead of panicking. A table may declare at most 64 sections. A `DXIL` chunk is accepted only when it is a `DxilProgramHeader` from the public DirectXShaderCompiler layout. It exports `bx_shader_inspect`, `bx_shader_prepare_for_d3dmetal`, and `bx_shader_free_buffer` with the layouts in `abi/include/bx_shader_bridge.h`. `bx_shader_inspect` fills that struct without logging. `bx_shader_prepare_for_d3dmetal` copies the `container_size` prefix when that DXIL header is present, including when the container also has a malformed FSPX table. An empty `DXIL` chunk is rejected. ACCC-only input returns `BX_SHADER_ERR_UNSUPPORTED` and writes one stderr inspection line. The first FSPX table that declares a stage supplies it. A failed stderr write does not panic. RDNA 2 bytes are not translated to DXIL. The section layout is synthetic and is not a console container specification or an Xbox hardware result (#416). - Added a visual `Progress Tracker` section to `README.md` with an M0–M6 Mermaid milestone pipeline, a Track A–F live status board (`🟩`/`🟨`/`⬜`), and direct GitHub label links for open M1 issues. - Added a required `Zen` field to `.github/ISSUE_TEMPLATE/track-handoff.yml` so every track handoff issue states its core purpose briefly in plain English up front. diff --git a/shims/xg_xs/xg_texture.h b/shims/xg_xs/xg_texture.h index 6672ff72..f9756d3d 100644 --- a/shims/xg_xs/xg_texture.h +++ b/shims/xg_xs/xg_texture.h @@ -86,6 +86,14 @@ typedef struct bx_xg_subresource_footprint { typedef struct bx_xg_placed_footprint { uint64_t offset; bx_xg_subresource_footprint footprint; + /* + * Absolute byte just past this subresource's last payload byte: + * offset + (row_count - 1) * row_pitch + row_bytes. + * That is the D3D12 GetCopyableFootprints end. It does not include the + * last row's pitch padding or the 512-byte tail. The copy span of a + * GetResourceLayout call is layouts[last].copy_end - base_offset. + */ + uint64_t copy_end; } bx_xg_placed_footprint; /* {B7E4C8A1-6F2D-4C55-8A19-3D0E5F7B91C4} BetterXen IXGTextureAddressComputer */ @@ -99,9 +107,10 @@ static const GUID BX_IID_IUnknown = { /* * GetResourceLayout places every subresource at its absolute offset from * base_offset, including subresources before first_subresource. total_bytes - * runs from base_offset through the aligned end of the last requested - * subresource, so a partial range still reports a total that covers the - * earlier subresources. + * is the BetterXen allocation span: the 512-aligned end of the last requested + * subresource, measured from base_offset, so a partial range still covers the + * earlier subresources and a 1x1 RGBA8 texture reports 512. copy_end on each + * footprint is the separate D3D12-style copy span end described above. */ typedef struct bx_xg_texture_computer bx_xg_texture_computer; diff --git a/shims/xg_xs/xg_xs.cpp b/shims/xg_xs/xg_xs.cpp index 3bfc09c1..77c2f41e 100644 --- a/shims/xg_xs/xg_xs.cpp +++ b/shims/xg_xs/xg_xs.cpp @@ -52,14 +52,32 @@ struct Resolved { uint32_t layout; }; +struct MipInfo { + uint64_t offset; /* byte offset of this mip inside one array slice */ + uint32_t logical_width; + uint32_t logical_height; + uint32_t footprint_width; + uint32_t footprint_height; + uint32_t row_pitch; + uint32_t row_count; + uint64_t row_bytes; +}; + +/* 16384 texels have 15 mips. One extra slot keeps the bound obvious. */ +constexpr uint32_t kMaxStoredMips = 16u; + struct TextureComputer { bx_xg_texture_computer iface; volatile LONG refcount; Resolved resolved; + MipInfo mips[kMaxStoredMips]; + uint32_t stored_mips; + uint64_t slice_stride; }; struct Placement { uint64_t offset; + uint64_t copy_end; uint32_t width; /* logical texels, used for element bounds */ uint32_t height; uint32_t footprint_width; /* texels rounded up to the block */ @@ -372,66 +390,87 @@ static HRESULT advance_cursor(uint64_t cursor, const Placement* place, uint64_t* return S_OK; } -using PlacementSink = void (*)(void* ctx, uint32_t slot, const Placement* place); - -static HRESULT walk_placements( - const Resolved* resolved, - uint64_t base_offset, - uint32_t first, - uint32_t last_exclusive, - PlacementSink sink, - void* sink_ctx, - uint64_t* aligned_end -) { - uint64_t cursor = base_offset; - uint64_t end = base_offset; - for (uint32_t index = 0; index < last_exclusive; ++index) { +static HRESULT build_linear_table(TextureComputer* obj) { + if (obj->resolved.layout != BX_XG_LAYOUT_LINEAR) { + return S_OK; + } + if (obj->resolved.mip_levels == 0u || obj->resolved.mip_levels > kMaxStoredMips) { + return E_INVALIDARG; + } + uint64_t cursor = 0; + for (uint32_t mip = 0; mip < obj->resolved.mip_levels; ++mip) { Placement place{}; - const HRESULT hr = measure_mip(resolved, index % resolved->mip_levels, &place); - if (hr != S_OK) { - return hr; + const HRESULT measured = measure_mip(&obj->resolved, mip, &place); + if (measured != S_OK) { + return measured; } - place.offset = cursor; + MipInfo* info = &obj->mips[mip]; + info->offset = cursor; + info->logical_width = place.width; + info->logical_height = place.height; + info->footprint_width = place.footprint_width; + info->footprint_height = place.footprint_height; + info->row_pitch = place.row_pitch; + info->row_count = place.row_count; + info->row_bytes = place.row_bytes; uint64_t next = 0; const HRESULT advanced = advance_cursor(cursor, &place, &next); if (advanced != S_OK) { return advanced; } - if (sink && index >= first) { - sink(sink_ctx, index - first, &place); - } - end = next; cursor = next; } - *aligned_end = end; + obj->slice_stride = cursor; + obj->stored_mips = obj->resolved.mip_levels; return S_OK; } -struct OnePlacement { - Placement place; - bool written; -}; - -static void sink_one(void* ctx, uint32_t /*slot*/, const Placement* place) { - auto* out = static_cast(ctx); - out->place = *place; - out->written = true; -} - -static HRESULT placement_at(const Resolved* resolved, uint32_t index, Placement* out) { - if (index >= resolved->subresource_count) { +static HRESULT allocation_end_relative(const TextureComputer* obj, uint32_t index, uint64_t* end) { + if (obj->stored_mips == 0u || index >= obj->resolved.subresource_count) { return E_INVALIDARG; } - OnePlacement captured{}; - uint64_t end = 0; - const HRESULT hr = walk_placements(resolved, 0, index, index + 1u, sink_one, &captured, &end); - if (hr != S_OK) { - return hr; + const uint32_t mip = index % obj->stored_mips; + const uint32_t slice = index / obj->stored_mips; + if (mip + 1u < obj->stored_mips) { + uint64_t slice_base = 0; + if (!mul_u64(slice, obj->slice_stride, &slice_base) || !add_u64(slice_base, obj->mips[mip + 1u].offset, end)) { + return E_INVALIDARG; + } + return S_OK; } - if (!captured.written) { - return E_FAIL; + return mul_u64(static_cast(slice) + 1u, obj->slice_stride, end) ? S_OK : E_INVALIDARG; +} + +static HRESULT placement_from_index(const TextureComputer* obj, uint32_t index, uint64_t base_offset, Placement* out) { + if (obj->stored_mips == 0u || index >= obj->resolved.subresource_count) { + return E_INVALIDARG; } - *out = captured.place; + const uint32_t mip = index % obj->stored_mips; + const uint32_t slice = index / obj->stored_mips; + const MipInfo* info = &obj->mips[mip]; + uint64_t relative = 0; + uint64_t absolute = 0; + uint64_t row_tail = 0; + uint64_t copy_end = 0; + if (!mul_u64(slice, obj->slice_stride, &relative) || !add_u64(relative, info->offset, &relative) || + !add_u64(relative, base_offset, &absolute)) { + return E_INVALIDARG; + } + if (info->row_count == 0u || !mul_u64(info->row_count - 1u, info->row_pitch, &row_tail) || + !add_u64(row_tail, info->row_bytes, &row_tail) || !add_u64(absolute, row_tail, ©_end)) { + return E_INVALIDARG; + } + out->offset = absolute; + out->copy_end = copy_end; + out->width = info->logical_width; + out->height = info->logical_height; + out->footprint_width = info->footprint_width; + out->footprint_height = info->footprint_height; + out->depth = 1u; + out->row_pitch = info->row_pitch; + out->row_count = info->row_count; + out->row_bytes = info->row_bytes; + out->format = obj->resolved.format; return S_OK; } @@ -441,14 +480,14 @@ struct LayoutOut { uint64_t* row_sizes; }; -static void sink_layout(void* ctx, uint32_t slot, const Placement* place) { - auto* out = static_cast(ctx); +static void write_layout(LayoutOut* out, uint32_t slot, const Placement* place) { out->layouts[slot].offset = place->offset; out->layouts[slot].footprint.format = place->format; out->layouts[slot].footprint.width = place->footprint_width; out->layouts[slot].footprint.height = place->footprint_height; out->layouts[slot].footprint.depth = place->depth; out->layouts[slot].footprint.row_pitch = place->row_pitch; + out->layouts[slot].copy_end = place->copy_end; out->row_counts[slot] = place->row_count; out->row_sizes[slot] = place->row_bytes; } @@ -509,6 +548,83 @@ static HRESULT resolve_image_pitch(const Placement* place, uint32_t row_pitch_in return S_OK; } +static bool touch_span( + uint64_t origin, + uint64_t row_pitch, + uint64_t depth_pitch, + uint32_t depth, + uint32_t row_count, + uint64_t row_bytes, + uint64_t size, + uint64_t* lo, + uint64_t* hi +) { + uint64_t low = UINT64_MAX; + uint64_t high = 0; + for (uint32_t z = 0; z < depth; ++z) { + for (uint32_t y = 0; y < row_count; ++y) { + uint64_t begin = 0; + uint64_t end = 0; + if (!row_bounds(origin, row_pitch, depth_pitch, z, y, row_bytes, size, &begin, &end)) { + return false; + } + if (begin < low) { + low = begin; + } + if (end > high) { + high = end; + } + } + } + *lo = low; + *hi = high; + return true; +} + +static bool byte_ranges_overlap( + const uint8_t* left, + uint64_t left_lo, + uint64_t left_hi, + const uint8_t* right, + uint64_t right_lo, + uint64_t right_hi +) { + const uintptr_t left_begin = reinterpret_cast(left) + static_cast(left_lo); + const uintptr_t left_end = reinterpret_cast(left) + static_cast(left_hi); + const uintptr_t right_begin = reinterpret_cast(right) + static_cast(right_lo); + const uintptr_t right_end = reinterpret_cast(right) + static_cast(right_hi); + return left_begin < right_end && right_begin < left_end; +} + +static HRESULT emit_rows( + const Placement* place, + uint8_t* dst, + uint64_t dst_size, + uint64_t dst_origin, + uint64_t dst_row_pitch, + uint64_t dst_depth_pitch, + const uint8_t* src, + uint64_t src_size, + uint64_t src_origin, + uint64_t src_row_pitch, + uint64_t src_depth_pitch +) { + for (uint32_t z = 0; z < place->depth; ++z) { + for (uint32_t y = 0; y < place->row_count; ++y) { + uint64_t src_begin = 0; + uint64_t src_end = 0; + uint64_t dst_begin = 0; + uint64_t dst_end = 0; + if (!row_bounds(src_origin, src_row_pitch, src_depth_pitch, z, y, place->row_bytes, src_size, &src_begin, &src_end) || + !row_bounds(dst_origin, dst_row_pitch, dst_depth_pitch, z, y, place->row_bytes, dst_size, &dst_begin, &dst_end)) { + return E_FAIL; + } + std::memcpy(dst + dst_begin, src + src_begin, static_cast(place->row_bytes)); + } + } + return S_OK; +} + static HRESULT copy_rows( const Placement* place, uint8_t* dst, @@ -532,33 +648,44 @@ static HRESULT copy_rows( return E_INVALIDARG; } - for (uint32_t z = 0; z < place->depth; ++z) { - for (uint32_t y = 0; y < place->row_count; ++y) { - uint64_t src_begin = 0; - uint64_t src_end = 0; - uint64_t dst_begin = 0; - uint64_t dst_end = 0; - if (!row_bounds(src_origin, src_row_pitch, src_depth_pitch, z, y, place->row_bytes, src_size, &src_begin, &src_end) || - !row_bounds(dst_origin, dst_row_pitch, dst_depth_pitch, z, y, place->row_bytes, dst_size, &dst_begin, &dst_end)) { - return E_INVALIDARG; - } - } + uint64_t src_lo = 0; + uint64_t src_hi = 0; + uint64_t dst_lo = 0; + uint64_t dst_hi = 0; + if (!touch_span(src_origin, src_row_pitch, src_depth_pitch, place->depth, place->row_count, place->row_bytes, src_size, &src_lo, &src_hi) || + !touch_span(dst_origin, dst_row_pitch, dst_depth_pitch, place->depth, place->row_count, place->row_bytes, dst_size, &dst_lo, &dst_hi)) { + return E_INVALIDARG; + } + if (!byte_ranges_overlap(src, src_lo, src_hi, dst, dst_lo, dst_hi)) { + return emit_rows( + place, dst, dst_size, dst_origin, dst_row_pitch, dst_depth_pitch, src, src_size, src_origin, src_row_pitch, src_depth_pitch); } - for (uint32_t z = 0; z < place->depth; ++z) { - for (uint32_t y = 0; y < place->row_count; ++y) { - uint64_t src_begin = 0; - uint64_t src_end = 0; - uint64_t dst_begin = 0; - uint64_t dst_end = 0; - if (!row_bounds(src_origin, src_row_pitch, src_depth_pitch, z, y, place->row_bytes, src_size, &src_begin, &src_end) || - !row_bounds(dst_origin, dst_row_pitch, dst_depth_pitch, z, y, place->row_bytes, dst_size, &dst_begin, &dst_end)) { - return E_FAIL; - } - std::memcpy(dst + dst_begin, src + src_begin, static_cast(place->row_bytes)); - } + /* Stage the whole source span first so a later row cannot clobber an unread one. */ + const uint64_t stage_lo = src_lo < src_origin ? src_lo : src_origin; + if (src_hi < stage_lo || src_hi - stage_lo > SIZE_MAX) { + return E_INVALIDARG; } - return S_OK; + const size_t staged_bytes = static_cast(src_hi - stage_lo); + auto* staged = static_cast(HeapAlloc(GetProcessHeap(), 0, staged_bytes)); + if (!staged) { + return E_OUTOFMEMORY; + } + std::memcpy(staged, src + stage_lo, staged_bytes); + const HRESULT hr = emit_rows( + place, + dst, + dst_size, + dst_origin, + dst_row_pitch, + dst_depth_pitch, + staged, + staged_bytes, + src_origin - stage_lo, + src_row_pitch, + src_depth_pitch); + HeapFree(GetProcessHeap(), 0, staged); + return hr; } static HRESULT image_and_resource_pitch( @@ -641,29 +768,24 @@ static HRESULT WINAPI computer_get_resource_layout( subresource_count > obj->resolved.subresource_count - first_subresource) { return E_INVALIDARG; } - uint64_t end = 0; - const HRESULT checked = walk_placements( - &obj->resolved, base_offset, first_subresource, first_subresource + subresource_count, nullptr, nullptr, &end); - if (checked != S_OK) { - return checked; - } - if (end < base_offset) { - return E_FAIL; + const uint32_t last_index = first_subresource + subresource_count - 1u; + uint64_t allocation_end = 0; + Placement probe{}; + const HRESULT ended = allocation_end_relative(obj, last_index, &allocation_end); + const HRESULT probed = placement_from_index(obj, last_index, base_offset, &probe); + if (ended != S_OK || probed != S_OK || probe.copy_end < probe.offset) { + return ended != S_OK ? ended : (probed != S_OK ? probed : E_FAIL); } LayoutOut out{layouts, row_counts, row_sizes_in_bytes}; - uint64_t end_again = 0; - const HRESULT written = walk_placements( - &obj->resolved, - base_offset, - first_subresource, - first_subresource + subresource_count, - sink_layout, - &out, - &end_again); - if (written != S_OK || end_again != end) { - return written != S_OK ? written : E_FAIL; + for (uint32_t slot = 0; slot < subresource_count; ++slot) { + Placement place{}; + const HRESULT hr = placement_from_index(obj, first_subresource + slot, base_offset, &place); + if (hr != S_OK) { + return hr; + } + write_layout(&out, slot, &place); } - *total_bytes = end - base_offset; + *total_bytes = allocation_end; return S_OK; } @@ -676,8 +798,11 @@ static HRESULT WINAPI computer_get_resource_size_bytes(bx_xg_texture_computer* s if (tiled != S_OK) { return tiled; } + if (obj->resolved.subresource_count == 0u) { + return E_INVALIDARG; + } uint64_t end = 0; - const HRESULT hr = walk_placements(&obj->resolved, 0, 0, obj->resolved.subresource_count, nullptr, nullptr, &end); + const HRESULT hr = allocation_end_relative(obj, obj->resolved.subresource_count - 1u, &end); if (hr != S_OK) { return hr; } @@ -720,7 +845,7 @@ static HRESULT WINAPI computer_get_mip_level_offset_bytes( } const uint32_t index = mip_level + array_slice * obj->resolved.mip_levels; Placement place{}; - const HRESULT hr = placement_at(&obj->resolved, index, &place); + const HRESULT hr = placement_from_index(obj, index, 0, &place); if (hr != S_OK) { return hr; } @@ -750,7 +875,7 @@ static HRESULT WINAPI computer_get_texel_element_offset_bytes( } const uint32_t index = mip_level + array_slice * obj->resolved.mip_levels; Placement place{}; - const HRESULT hr = placement_at(&obj->resolved, index, &place); + const HRESULT hr = placement_from_index(obj, index, 0, &place); if (hr != S_OK) { return hr; } @@ -789,7 +914,7 @@ static HRESULT WINAPI computer_copy_into_subresource( return tiled; } Placement place{}; - const HRESULT placed = placement_at(&obj->resolved, subresource, &place); + const HRESULT placed = placement_from_index(obj, subresource, 0, &place); if (placed != S_OK) { return placed; } @@ -834,7 +959,7 @@ static HRESULT WINAPI computer_copy_from_subresource( return tiled; } Placement place{}; - const HRESULT placed = placement_at(&obj->resolved, subresource, &place); + const HRESULT placed = placement_from_index(obj, subresource, 0, &place); if (placed != S_OK) { return placed; } @@ -895,6 +1020,11 @@ HRESULT WINAPI XGCreateTextureComputer(const bx_xg_resource_desc* desc, void** c obj->iface.lpVtbl = &kVtbl; obj->refcount = 1; obj->resolved = resolved; + const HRESULT built = build_linear_table(obj); + if (built != S_OK) { + HeapFree(GetProcessHeap(), 0, obj); + return built; + } *computer = obj; return S_OK; } diff --git a/tests/conformance/test_xg_xs/main.cpp b/tests/conformance/test_xg_xs/main.cpp index be2d3716..baad86e8 100644 --- a/tests/conformance/test_xg_xs/main.cpp +++ b/tests/conformance/test_xg_xs/main.cpp @@ -167,6 +167,8 @@ static void test_hand_layouts() { /* mip3 1x1: 4 -> 256, 1 row, offset 3584. total align(3840, 512) = 4096. */ check(layouts[3].offset == 3584u && rows[3] == 1u && row_bytes[3] == 4u && layouts[3].footprint.width == 1u, "rgba mip3"); + /* D3D12 copy end keeps the last row's payload and drops the 512-byte tail. */ + check(layouts[0].copy_end == 1824u && layouts[3].copy_end == 3588u, "rgba chain copy ends"); check_hr("rgba mip1 offset", computer->lpVtbl->GetMipLevelOffsetBytes(computer, 0, 1, &offset), S_OK); check(offset == 2048u, "mip helper matches the footprint"); check_hr("rgba texel", computer->lpVtbl->GetTexelElementOffsetBytes(computer, 0, 1, 3, 1, 0, &offset), S_OK); @@ -189,6 +191,7 @@ static void test_hand_layouts() { /* 2x2 blocks of 8 bytes -> row 16, pitch 256, 2 rows, payload 512. mip1 one 8-byte block at 512. */ check(layouts[0].offset == 0u && rows[0] == 2u && row_bytes[0] == 16u && total == 1024u, "bc1 mip0"); check(layouts[1].offset == 512u && rows[1] == 1u && row_bytes[1] == 8u && layouts[1].footprint.width == 4u, "bc1 mip1"); + check(layouts[0].copy_end == 272u && layouts[1].copy_end == 520u, "bc1 copy ends"); check_hr("bc1 texel", computer->lpVtbl->GetTexelElementOffsetBytes(computer, 0, 0, 4, 0, 0, &offset), S_OK); check(offset == 8u, "bc1 texel x=4 is the second block"); check_hr("bc1 texel corner", computer->lpVtbl->GetTexelElementOffsetBytes(computer, 0, 0, 7, 7, 0, &offset), S_OK); @@ -210,7 +213,7 @@ static void test_hand_layouts() { return; } check_hr("bc1 4x4 layout", computer->lpVtbl->GetResourceLayout(computer, 0, 1, 0, layouts, rows, row_bytes, &total), S_OK); - check(row_bytes[0] == 8u && total == 512u, "bc1 4x4 row is 8 bytes"); + check(row_bytes[0] == 8u && total == 512u && layouts[0].copy_end == 8u, "bc1 4x4 row is 8 bytes"); release_computer(computer); desc = desc_2d(DXGI_FORMAT_BC1_UNORM, 128, 4, 1, 1, BX_XG_LAYOUT_LINEAR); @@ -301,7 +304,8 @@ static void test_hand_layouts() { check_hr("rgba variant layout", computer->lpVtbl->GetResourceLayout(computer, 0, 1, 0, layouts, rows, row_bytes, &total), S_OK); - check(row_bytes[0] == 4u && total == 512u && layouts[0].footprint.format == format, "rgba variant size"); + check(row_bytes[0] == 4u && total == 512u && layouts[0].copy_end == 4u && layouts[0].footprint.format == format, + "rgba variant size"); release_computer(computer); } @@ -511,6 +515,84 @@ static void test_swizzle_placeholder() { "suggest stays on the implemented mode"); } +static bool row_is(const uint8_t* row, uint8_t value) { + return row[0] == value && row[1] == value && row[2] == value && row[3] == value; +} + +static void paint_row(uint8_t* row, uint8_t value) { + row[0] = value; + row[1] = value; + row[2] = value; + row[3] = value; +} + +static void test_overlapping_copies() { + bx_xg_resource_desc desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 1, 3, 1, 1, BX_XG_LAYOUT_LINEAR); + bx_xg_texture_computer* computer = create_ok(&desc, "create overlap"); + if (!computer) { + return; + } + uint8_t* buf = alloc_bytes(1024, 0); + if (!buf) { + release_computer(computer); + return; + } + paint_row(buf + 0, 1); + paint_row(buf + 128, 2); + paint_row(buf + 256, 3); + check_hr("expand pitch in place", + computer->lpVtbl->CopyIntoSubresource(computer, buf, 1024, 0, buf, 1024, 128, 0), + S_OK); + check(row_is(buf + 0, 1) && row_is(buf + 256, 2) && row_is(buf + 512, 3), "expanded rows stay 1, 2, 3"); + + std::memset(buf, 0, 1024); + paint_row(buf + 0, 1); + paint_row(buf + 256, 2); + paint_row(buf + 512, 3); + check_hr("contract pitch in place", + computer->lpVtbl->CopyFromSubresource(computer, buf, 1024, 128, 0, buf, 1024, 0), + S_OK); + check(row_is(buf + 0, 1) && row_is(buf + 128, 2) && row_is(buf + 256, 3), "contracted rows stay 1, 2, 3"); + std::free(buf); + release_computer(computer); +} + +static void test_large_array_offsets() { + /* 64x64 RGBA8 is 7 mips. One slice stride is 32768. + * mip6 sits at 32256 inside the slice. + * Slice 2047 therefore starts at 2047 * 32768 = 67076096, + * and its last mip is at 67076096 + 32256 = 67108352. + * The allocation is 2048 * 32768 = 67108864. + */ + bx_xg_resource_desc desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 64, 64, 2048, 7, BX_XG_LAYOUT_LINEAR); + bx_xg_texture_computer* computer = create_ok(&desc, "create 2048x7"); + if (!computer) { + return; + } + uint64_t offset = 0; + uint64_t size = 0; + check_hr("slice 0 mip6", computer->lpVtbl->GetMipLevelOffsetBytes(computer, 0, 6, &offset), S_OK); + check(offset == 32256u, "first slice last mip is 32256"); + check_hr("last slice mip0", computer->lpVtbl->GetMipLevelOffsetBytes(computer, 2047, 0, &offset), S_OK); + check(offset == 67076096ull, "last slice starts at 67076096"); + check_hr("last mip", computer->lpVtbl->GetMipLevelOffsetBytes(computer, 2047, 6, &offset), S_OK); + check(offset == 67108352ull, "last subresource offset is 67108352"); + check_hr("array allocation", computer->lpVtbl->GetResourceSizeBytes(computer, &size), S_OK); + check(size == 67108864ull, "2048 slices allocate 67108864 bytes"); + + bx_xg_placed_footprint layout{}; + uint32_t rows = 0; + uint64_t row_bytes = 0; + uint64_t total = 0; + const uint32_t last_index = 6u + 2047u * 7u; + check_hr("last layout", + computer->lpVtbl->GetResourceLayout(computer, last_index, 1, 0, &layout, &rows, &row_bytes, &total), + S_OK); + check(layout.offset == 67108352ull && layout.copy_end == 67108356ull && total == 67108864ull, + "last footprint copy end is 4 bytes past its offset"); + release_computer(computer); +} + static void test_refcount() { bx_xg_resource_desc desc = desc_2d(DXGI_FORMAT_R8G8B8A8_UNORM, 4, 2, 1, 1, BX_XG_LAYOUT_LINEAR); bx_xg_texture_computer* computer = create_ok(&desc, "create refcount"); @@ -557,6 +639,8 @@ int main() { test_monotonic_chain(DXGI_FORMAT_BC1_UNORM, 33, 17, "bc1 chain"); test_monotonic_chain(DXGI_FORMAT_BC7_UNORM, 32, 32, "bc7 chain"); test_copies(); + test_overlapping_copies(); + test_large_array_offsets(); test_swizzle_placeholder(); test_refcount();