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>
This commit is contained in:
devil8blade committed 2026-11-26 13:45:46 +00:00
1 parent ce5a0a6e43
commit f9ecde4e95
4 files changed
+324 -101

No files matched your search

+1 -1
View File
@@ -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.
+12 -3
View File
@@ -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;
+225 -95
View File
@@ -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<OnePlacement*>(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<uint64_t>(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, &copy_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<LayoutOut*>(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<uintptr_t>(left) + static_cast<uintptr_t>(left_lo);
const uintptr_t left_end = reinterpret_cast<uintptr_t>(left) + static_cast<uintptr_t>(left_hi);
const uintptr_t right_begin = reinterpret_cast<uintptr_t>(right) + static_cast<uintptr_t>(right_lo);
const uintptr_t right_end = reinterpret_cast<uintptr_t>(right) + static_cast<uintptr_t>(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<size_t>(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<size_t>(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<size_t>(src_hi - stage_lo);
auto* staged = static_cast<uint8_t*>(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;
}
+86 -2
View File
@@ -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();