Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,29 @@ else()
endif()
add_test(NAME xg_bc64_tiling_reference COMMAND xg_swizzle_select_check --bc64-tiling)
set_tests_properties(xg_bc64_tiling_reference PROPERTIES TIMEOUT 30 SKIP_RETURN_CODE 77)
# 4 KiB allocation answers for small 2D textures (src/startup_stubs/small_texture_4k.h): literal sizes and
# refused descriptions without a device; the mutated run is the negative control; the addrlib run compares
# every accepted shape with addrlib's GFX10 4 KiB layout and skips (77) without addrlib.
add_executable(startup_small_texture_4k_check tests/startup_small_texture_4k_check.cpp)
target_compile_features(startup_small_texture_4k_check PRIVATE cxx_std_17)
target_include_directories(startup_small_texture_4k_check PRIVATE src)
if(XBOX2WINDOWS_ADDRLIB_DIR)
target_link_libraries(startup_small_texture_4k_check PRIVATE xbw_addrlib)
target_compile_definitions(startup_small_texture_4k_check PRIVATE XBOX2WINDOWS_HAVE_ADDRLIB)
endif()
add_test(NAME startup_small_texture_4k COMMAND startup_small_texture_4k_check)
add_test(NAME startup_small_texture_4k_whole_width_tail_control COMMAND startup_small_texture_4k_check --mutate whole-width-tail)
set_tests_properties(startup_small_texture_4k_whole_width_tail_control PROPERTIES WILL_FAIL TRUE)
add_test(NAME startup_small_texture_4k_addrlib COMMAND startup_small_texture_4k_check --addrlib)
set_tests_properties(startup_small_texture_4k_addrlib PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77)
# The same answers through the device wrapper: allocation-info slots 25 and 108, with controls.
add_executable(startup_small_texture_4k_device_check tests/startup_small_texture_4k_device_check.cpp)
target_compile_features(startup_small_texture_4k_device_check PRIVATE cxx_std_17)
target_link_libraries(startup_small_texture_4k_device_check PRIVATE d3d12_xs_shim d3d12 dxgi)
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_small_texture_4k_device_${adapter} COMMAND startup_small_texture_4k_device_check ${adapter})
endforeach()
set_tests_properties(startup_small_texture_4k_device_hardware PROPERTIES SKIP_RETURN_CODE 77)
# Which descriptions take the 64 KiB standard BC chain (src/startup_stubs/bc64_chain.h), without addrlib
# or a device. The mutated run expects layout UNKNOWN to be accepted and is the negative control.
add_executable(startup_bc64_supports_check tests/startup_bc64_supports_check.cpp)
Expand Down Expand Up @@ -2748,6 +2771,15 @@ if(EXISTS "${INTAKE_PYTHON}")
"$<TARGET_FILE:offscreen_check>" ${adapter} $<$<BOOL:${STARTUP_FXC}>:${STARTUP_FXC}>)
set_tests_properties(startup_gfx2hlsl_shift64_${adapter} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77)
endforeach()
# XG slots 2 and 8 for a texture given the 4 KiB allocation answer (src/startup_stubs/small_texture_4k.h):
# the copy must stay in front of a guard page; the mutated run is not limited and must fail.
add_test(NAME startup_small_texture_4k_xg COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_small_texture_4k_xg_check.py" "$<TARGET_FILE:startup_stub_xg_xs>")
add_test(NAME startup_small_texture_4k_xg_no_limit_control COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_small_texture_4k_xg_check.py" "$<TARGET_FILE:startup_stub_xg_xs>" --mutate no-limit)
set_tests_properties(startup_small_texture_4k_xg startup_small_texture_4k_xg_no_limit_control
PROPERTIES TIMEOUT 30 ENVIRONMENT "X2W_D3D12_PERMISSIVE=1")
set_tests_properties(startup_small_texture_4k_xg_no_limit_control PROPERTIES WILL_FAIL TRUE)
add_test(NAME startup_xg_bc_tiling COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_xg_bc_tiling_check.py" "$<TARGET_FILE:startup_stub_xg_xs>")
set_tests_properties(startup_xg_bc_tiling PROPERTIES TIMEOUT 30 ENVIRONMENT "X2W_D3D12_PERMISSIVE=1")
Expand Down Expand Up @@ -2851,6 +2883,12 @@ if(EXISTS "${INTAKE_PYTHON}")
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_xg_bc7_draw_check.py"
"$<TARGET_FILE:startup_stream_consumer>" "${DXC_EXECUTABLE}" "${backend}")
set_tests_properties(startup_xg_bc7_draw_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77)
# A two-mip texture given the 4 KiB allocation answer: XG slot 8 into exactly the reported size before a
# guard page, placed, drawn and read back pixel for pixel (src/startup_stubs/small_texture_4k.h).
add_test(NAME startup_small_texture_4k_draw_${backend} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_small_texture_4k_draw_check.py"
"$<TARGET_FILE:startup_stream_consumer>" "${DXC_EXECUTABLE}" "${backend}")
set_tests_properties(startup_small_texture_4k_draw_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77)
add_test(NAME startup_slot57_blend_draw_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}"
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_slot57_blend_draw_check.py"
"$<TARGET_FILE:startup_stream_consumer>" "${backend}")
Expand Down
75 changes: 75 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,81 @@ R24G8 or the added colour families.
desktop texture that does not alias the address, so this shows the answered size is accepted by
the creation path, not that the console layout fits in it.

## 4 KiB allocation answers for small textures

A console executable asks the allocation-info entries (slots 25 and 108) with `Alignment` 0x1000 for
a texture that is not a render or depth target and whose top level, padded to 4 KiB blocks, holds at
most 0x80007 bits. It keeps the answer only when the returned alignment is 0x1000, and otherwise asks
again with alignment 0. The stand-in used to drop the 4 KiB request (`sanitized()`) and forward the
desktop answer, 64 KiB at 64 KiB alignment. The executable then recorded 16 pages for a texture the
console sizes at one, and its streaming read for that texture ran 64 KiB through the following
resources' compressed chunks. It ended inside one of them, which the executable's own check rejects
before the request is submitted. The storage stand-in then refused the request as destination
capacity exceeded.

For a description in scope the stand-in now answers the console size at 4 KiB alignment
(`src/startup_stubs/small_texture_4k.h`): the GFX10 4 KiB thin-block layout as AMD addrlib computes it.
Each level is padded to whole blocks (128 x 64 texels at 4 bits a texel, 64 x 64 at 8, 64 x 32 at 16,
32 x 32 at 32, 32 x 16 at 64, 16 x 16 at 128; block-compressed formats count 4 x 4 blocks) until the
first level no wider than half a block and no taller than a block. That level and every smaller one
share one block. (addrlib also caps a tail at 8 levels; a 4 KiB thin tail never holds more than 7.) The first eight uses are logged as
`x2w-d3d12 allocation info: 4 KiB answer ...`. Desktop D3D12 asked with a 4 KiB alignment is not a
substitute: on the adapter measured it answered 0x2000 for a 128 x 4 R16G16B16A16 texture, where the
4 KiB layout takes 0x4000.

Scope: a 2D texture with layout UNKNOWN, `Alignment` 0x1000, one sample, one array slice, power-of-two
width and height up to 16384, `MipLevels` 0 (full chain) up to the full chain, no render-target or
depth flag, a single-plane format of 4, 8, 16, 32, 64 or 128 bits a texel (`small_texture_4k_bits`;
depth with stencil is not answered), the executable's 0x80007-bit bound, and a desktop answer that is
not a failure. Any other description, arrays, other layouts and 1D or 3D textures included, keeps the
desktop answer.

The executable's memory for such a texture is then only the answered size. The desktop linear footprint
(256-byte rows, 512-byte level placement) can be larger: a 32 x 32 R8G8B8A8 texture takes 0x1f80 bytes.
So for a description that got the 4 KiB answer (`Alignment` 0x1000 and in scope) the stand-in uses one
compact linear plan (`small_texture_4k_plan`) in every call that touches that memory:

- XG slot 2 reports the answered size at 4 KiB alignment and, per level, the plan's offset, row bytes and
size;
- XG slot 8 writes every row of a level there, or nothing: a row pitch shorter than a row or a level past
the last one fails without writing or announcing a write;
- texture upload (`upload_texture`) reads the same rows from there.

In the plan, every row has its exact byte width (a row of texels, or of 4 x 4 blocks), and the levels
follow each other from the top level with no padding. Its size never exceeds the answer:
`startup_small_texture_4k` checks this for every accepted shape. The texels are linear, not the console's
4 KiB swizzle. Data the executable streams in its own 4 KiB layout is therefore read in the wrong order,
as it already was with the desktop layout. A GFX10 4 KiB tiling plan, such as `Gfx10Texture` in #1469,
can replace the compact plan in all three calls at once. At alignment 0 nothing changes: the desktop
answer and the desktop footprint.

Measured on one console executable under the startup debugger: 18 such reads failed during
first-run setup in every run, each with the executable's check firing once. With this answer both
counts were 0 to the same screen. Over the executable's archives, every resource of the family it
places in 4 KiB blocks starts exactly the size above after the previous one.

- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k(_whole_width_tail_control)?$' -V`
checks literal sizes and the refused descriptions without a device; the control expects a tail one
block wide and must fail.
- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_addrlib$' -V` compares every
accepted power-of-two shape and mip count, for one format of each bit depth, with addrlib's
`ADDR_SW_4KB_S` size (Mesa 25.1.0, `tools/fetch_addrlib.py`); it skips (77) without addrlib.
- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_device_' -V` goes through the
wrapper: slots 25 and 108 must give the 4 KiB answer, and controls the desktop answer.
- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_xg' -V` covers a two-mip 32 x 32
R8G8B8A8 texture at `Alignment` 0x1000:
- slot 2's size, alignment and level records;
- slot 8 into exactly the answered size in front of a no-access guard page, with distinct rows and a
different sentinel per level, checked byte for byte;
- refused copies that leave memory unchanged;
- alignment 0 unchanged: slot 2 reports 0x10000 and slot 8 writes all 32 rows.

The `_no_limit_control` run asks at an explicit 64 KiB alignment, which gets no compact plan, and must
fail.
- `ctest --preset release --no-tests=error -R '^startup_small_texture_4k_draw_' -V` writes the same
texture through slot 8 in front of a guard page, places it (slot 133), draws both levels through the
translated path on WARP and on the hardware adapter, and compares all 48 x 32 read-back pixels.

## Physical pages in shared sections

`XMemAllocatePhysicalPages` pages are 64 KiB slots of 256 MiB pagefile-backed sections. With
Expand Down
30 changes: 26 additions & 4 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1070,8 +1070,19 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
if (!plan.bytes(i) || plan.rows[i] != console_layout->rows(i) ||
plan.row_bytes[i] != u64{console_layout->columns(i)} * console_layout->bytes_per_element) return false;
}
// A texture given the 4 KiB allocation answer is read in the compact plan XG describes and writes for it
// (small_texture_4k.h); its rows must be the footprint's rows, level by level. Address-only textures
// (single-element, or a console layout named in Layout) are tiled and keep their own paths.
x2w_startup::SmallTexturePlan compact;
const bool is_compact = original && !tiled && x2w_startup::small_texture_4k_plan(original->console, compact);
if (is_compact) {
bool rows_match = compact.levels == plan.count;
for (UINT i = 0; rows_match && i < plan.count; ++i)
rows_match = compact.level[i].rows == plan.rows[i] && compact.level[i].row_bytes == plan.row_bytes[i];
if (!rows_match) return true;
}
const u64 total = tiled_chain ? x2w_startup::bc64::Chain(original->console).bytes() : console ? console_layout->bytes()
: tiled && !single ? surface.size() : stride * (slices - 1) + slice_total;
: tiled && !single ? surface.size() : is_compact ? compact.bytes : stride * (slices - 1) + slice_total;
const bool announced = state.generation != generation;
const UINT elapsed = static_cast<UINT>(frame_number) - static_cast<UINT>(state.checked_frame);
// A single texel (at most 16 bytes) is rehashed on every bind, so a write between two draws is seen.
Expand Down Expand Up @@ -1166,7 +1177,17 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
const auto& fp = plan.placed[i];
const u64 bytes = plan.bytes(i);
u64 offset = 0;
if (!ring_copy(reinterpret_cast<u64>(memory) + stride * s + fp.Offset, static_cast<std::size_t>(bytes), &offset)) {
if (is_compact) {
auto* const rows = reserve_upload_ring(static_cast<std::size_t>(bytes), &offset);
if (!rows) {
state.done = false;
return true;
}
const auto& level = compact.level[i];
for (u64 y = 0; y < level.rows; ++y)
std::memcpy(rows + y * fp.Footprint.RowPitch, memory + level.offset + y * level.row_bytes,
static_cast<std::size_t>(level.row_bytes));
} else if (!ring_copy(reinterpret_cast<u64>(memory) + stride * s + fp.Offset, static_cast<std::size_t>(bytes), &offset)) {
state.done = false;
return true;
}
Expand All @@ -1182,8 +1203,9 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
InterlockedExchangeAdd(&texture_bytes_uploaded, static_cast<LONG>(total >> 10));
static LONG shown;
if (first_calls(shown, 12))
log("x2w-draw texture upload %p %llux%u x%u mips %u format %d from %p (%llu KiB)%s", static_cast<void*>(texture), desc.Width,
desc.Height, slices, mips, desc.Format, static_cast<const void*>(memory), total >> 10, announced ? " (announced write)" : "");
log("x2w-draw texture upload %p %llux%u x%u mips %u format %d from %p (%llu KiB)%s%s", static_cast<void*>(texture), desc.Width,
desc.Height, slices, mips, desc.Format, static_cast<const void*>(memory), total >> 10, announced ? " (announced write)" : "",
is_compact ? " (compact plan of the 4 KiB allocation answer)" : "");
return true;
}

Expand Down
14 changes: 14 additions & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "command_signature_filter.h"
#include "console_plane_size.h"
#include "descriptor_heap_type.h"
#include "small_texture_4k.h"
#include "null_binding_cause.h"
#include "null_texture_view.h"
#include "packed_target_format.h"
Expand Down Expand Up @@ -886,6 +887,17 @@ void note_console_size(const D3D12_RESOURCE_DESC& desc, const D3D12_RESOURCE_ALL
info.SizeInBytes, desc.Width, desc.Height, desc.Format, desc.SampleDesc.Count);
}

// A single small 2D texture asked with a 4 KiB alignment gets the console's 4 KiB answer (small_texture_4k.h).
bool small_texture_answer(UINT count, const D3D12_RESOURCE_DESC* descs, D3D12_RESOURCE_ALLOCATION_INFO& info)
{
if (count != 1 || !descs || !x2w_startup::small_texture_4k(descs[0], info)) return false;
static LONG calls;
if (first_calls(calls, 8))
log("x2w-d3d12 allocation info: 4 KiB answer size 0x%llx for %llux%u mips %u format %d", info.SizeInBytes, descs[0].Width,
descs[0].Height, descs[0].MipLevels, descs[0].Format);
return true;
}

// Device slot 25, desktop GetResourceAllocationInfo(visibleMask, count, descs), returned by value.
void* __stdcall device_resource_allocation_info(void* a, void* b, UINT mask, UINT count, const D3D12_RESOURCE_DESC* descs)
{
Expand All @@ -896,6 +908,7 @@ void* __stdcall device_resource_allocation_info(void* a, void* b, UINT mask, UIN
D3D12_RESOURCE_ALLOCATION_INFO info = device_of(self)->GetResourceAllocationInfo(mask, count, copies.data());
// A single description of a family that is larger on desktop gets its console size (console_plane_size.h).
if (count == 1 && x2w_startup::console_plane_size(descs[0], info)) note_console_size(descs[0], info);
small_texture_answer(count, descs, info);
*static_cast<D3D12_RESOURCE_ALLOCATION_INFO*>(result) = info;
static LONG calls, invalid;
if (first_calls(calls))
Expand Down Expand Up @@ -1022,6 +1035,7 @@ void* __stdcall device_resource_allocation_info1(void* a, void* b, UINT mask, UI
if (info1) *info1 = {0, info.Alignment, info.SizeInBytes};
note_console_size(descs[0], info);
}
if (small_texture_answer(count, descs, info) && info1) *info1 = {0, info.Alignment, info.SizeInBytes};
*static_cast<D3D12_RESOURCE_ALLOCATION_INFO*>(result) = info;
static LONG calls, invalid;
if (first_calls(calls))
Expand Down
Loading
Loading