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
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,27 @@ add_test(NAME startup_placed_buffer_range_region_only_control COMMAND startup_pl
add_test(NAME startup_placed_buffer_range_walk_only_control COMMAND startup_placed_buffer_range_check --mutate walk-only)
set_tests_properties(startup_placed_buffer_range_region_only_control startup_placed_buffer_range_walk_only_control
PROPERTIES SKIP_RETURN_CODE 77)
# CopyTextureRegion (list slot 16): the argument checks refuse what the desktop would reject
# (src/startup_stubs/list_copy_texture.h). The mutated run is the negative control.
add_executable(startup_list_copy_texture_check tests/startup_list_copy_texture_check.cpp)
target_compile_features(startup_list_copy_texture_check PRIVATE cxx_std_17)
target_include_directories(startup_list_copy_texture_check PRIVATE src)
add_test(NAME startup_list_copy_texture COMMAND startup_list_copy_texture_check)
add_test(NAME startup_list_copy_texture_overflow_control COMMAND startup_list_copy_texture_check --mutate overflow)
set_tests_properties(startup_list_copy_texture_overflow_control PROPERTIES WILL_FAIL TRUE)
add_test(NAME startup_list_copy_texture_slice_control COMMAND startup_list_copy_texture_check --mutate slice)
set_tests_properties(startup_list_copy_texture_slice_control PROPERTIES WILL_FAIL TRUE)

# The same call through the wrapped command list: two array slices round-trip, and an out-of-range subresource
# or a format mismatch is refused before any desktop copy.
add_executable(startup_list_copy_texture_warp tests/startup_list_copy_texture_warp.cpp)
target_compile_features(startup_list_copy_texture_warp PRIVATE cxx_std_17)
target_link_libraries(startup_list_copy_texture_warp PRIVATE d3d12_xs_shim d3d12 dxgi)
foreach(backend IN ITEMS warp hardware)
add_test(NAME startup_list_copy_texture_${backend} COMMAND startup_list_copy_texture_warp ${backend})
set_tests_properties(startup_list_copy_texture_${backend} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77
ENVIRONMENT "X2W_D3D12_PERMISSIVE=1;X2W_D3D12_LIST_NOOP=1;X2W_D3D12_RENDER=1;X2W_D3D12_DEBUG=1")
endforeach()
add_executable(startup_storage_file_range tests/startup_storage_file_range.cpp)
target_compile_features(startup_storage_file_range PRIVATE cxx_std_17)
target_include_directories(startup_storage_file_range PRIVATE src)
Expand Down
34 changes: 33 additions & 1 deletion docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ upload the stale title memory over the result; a list discarded by Reset or a fa
and block-compressed). A resolve format is exact against a fully typed endpoint and only family-compatible with a
typeless one; a typeless resolve format (or an unknown one over typeless endpoints) resolves as the family's typed
choice: half float for `R16G16B16A16`, normalised for the 8-bit and 10-bit families. Calls on a closed list are ignored.
These slots have not been seen in a title log yet. `CopyTextureRegion` (16) and `CopyTiles` (18) are not handled.
These slots have not been seen in a title log yet. Slot 16 is described in [Command list CopyTextureRegion](#command-list-copytextureregion-list-slot-16). Slot 18 (`CopyTiles`) is not handled.
`ctest -R '^startup_list_copy_validation'` runs the pure checks on any host; `startup_list_copy_{warp,hardware}` copies,
resolves and reads back through the `x2w_d3d12_list_copy_stats` export.

Expand Down Expand Up @@ -2680,3 +2680,35 @@ and console layouts. On Windows 11 Build 26100 with an AMD Radeon RX 6600M (driv
- `ctest --preset release --no-tests=error -R '^startup_memory_srv_(report|buffer|volume)$' -V`
- `ctest --preset release --no-tests=error -R '^startup_memory_srv_(buffer|volume)_.*_control$' -V`
- `ctest --preset release --no-tests=error -R '^startup_address_(srv|buffer_srv|volume_srv)_' -V`

## Command list CopyTextureRegion (list slot 16)

Command list slot 16, `CopyTextureRegion`, maps each location's resource to its desktop resource, checks the
call, and forwards it only when the desktop would accept it. A refused call is counted and logged
(`x2w-d3d12 CopyTextureRegion refused: <reason>`), not forwarded. Capability, measured on the portable
check only:

- Argument checks (`src/startup_stubs/list_copy_texture.h`): footprint on both sides, empty or inverted box,
source and destination bounds, row pitch (multiple of 256), offset (multiple of 512), footprint end inside
the buffer (includes the destination origin, the source box origin and the footprint-height slice stride),
block alignment, every multiply and add in the footprint arithmetic checked for wrapping.
- Subresource index: `mip + slice * mips + plane * mips * array_size` must lie inside the resource's
`mips * array_size * planes`; a mip-only modulo would read an out-of-range slice as mip 0
(`bad_subresource`). A planar format (more than one plane) is refused (`planar_unsupported`).
- The two sides must name the same format (`format_mismatch`) and the same sample count
(`sample_mismatch`). Multisampled and single-plane depth-stencil copies require equally sized whole
subresources, destination origin 0 and a null source box (`partial_multisample`, `partial_depth_stencil`).
Buffer footprints are single-sample, so a multisampled texture and a buffer are refused.
- Upload and readback footprint buffers keep their fixed heap states and get no barrier; an upload buffer as
destination or a readback buffer as source is refused (`bad_heap`).
- A source or destination texture is brought up to date from its guest backing (`upload_texture`) before the
copy, so a later first sample does not replace the copied texels. A copied (non-aliased) source buffer is
refused (`stale_source`).
- Two subresources of one resource are refused (`same_resource`): resource states are tracked per resource.

Wrapped-list readback (`tests/startup_list_copy_texture_warp.cpp`): two images round-trip through array
slices 1 and 2 of a 2-mip, 3-slice texture (upload buffer, texture, readback buffer, byte-for-byte), and a
list with an out-of-range subresource, an index of `0xffffffff` and a footprint format mismatch is refused
before any desktop copy, leaving a sentinel-filled readback buffer unchanged. Reproduce the portable check
with its negative controls and the WARP and hardware runs with
`ctest --preset dev --no-tests=error -R '^startup_list_copy_texture' -V`.
2 changes: 2 additions & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3170,6 +3170,7 @@ bool console_layout_srv(const D3D12_RESOURCE_DESC& desc);
#include "startup_stubs/d3d12_draw.inl"
#include "startup_stubs/mesh86_cache.inl"
#include "startup_stubs/d3d12_compute_binding_trace.inl"
#include "startup_stubs/list_copy_texture.inl"

// Queue slot 10, desktop ExecuteCommandLists(count, lists).
void __stdcall queue_execute(Wrapper* self, UINT count, void* const* lists)
Expand Down Expand Up @@ -4786,6 +4787,7 @@ void build_tables()
list_typed[139] = reinterpret_cast<ListTyped>(&list_reset);
list_typed[148] = reinterpret_cast<ListTyped>(&list_allocate_upload);
list_typed[12] = reinterpret_cast<ListTyped>(&list_draw);
list_typed[16] = reinterpret_cast<ListTyped>(&list_copy_texture);
list_typed[20] = reinterpret_cast<ListTyped>(&list_topology);
list_typed[21] = reinterpret_cast<ListTyped>(&list_viewports);
list_typed[22] = reinterpret_cast<ListTyped>(&list_scissors);
Expand Down
171 changes: 171 additions & 0 deletions src/startup_stubs/list_copy_texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Experimental (docs/startup-runtime.md, #870): the argument checks for command list slot 16,
// CopyTextureRegion(dst, x, y, z, src, box). Plain standard C++ so a Linux fixture can run it; the
// handler (list_copy_texture.inl) maps the title's resources and forwards only what passes here.
// A call the desktop runtime would reject is refused with a named reason instead.
#pragma once

#include <cstdint>
#include <initializer_list>

namespace x2w_list_copy_texture {

enum class Kind : std::uint32_t { subresource = 0, placed_footprint = 1 };

// What the check needs to know about one side of the copy. For a subresource side, width/height/depth are
// the subresource's size in blocks (texels for an uncompressed format). For a footprint side, the footprint
// size in blocks, the row pitch in bytes, the byte offset and the bytes per block are used, and
// resource_bytes is the size of the buffer.
struct Side {
Kind kind;
std::uint32_t width, height, depth;
std::uint64_t offset;
std::uint32_t row_pitch, bytes_per_block;
std::uint64_t resource_bytes;
std::uint32_t samples; // sample count; a footprint (buffer) side is always 1
std::uint32_t block_w, block_h; // texels per block (4 x 4 for block-compressed formats, else 1 x 1)
std::uint32_t format; // DXGI_FORMAT value of the subresource, or of the footprint
// A subresource side: its index and the resource's counts, for D3D12's
// index = mip + slice * mips + plane * mips * array_size.
std::uint32_t subresource, mips, array_size, planes;
bool depth_stencil = false;
};

struct Subresource {
std::uint32_t mip, slice, plane;
};

// Splits a subresource index by the desktop's math. False when the index is outside
// mips * array_size * planes (or a count is zero), so no mip, slice or plane aliases another.
inline bool decode_subresource(std::uint32_t index, std::uint32_t mips, std::uint32_t array_size, std::uint32_t planes,
Subresource& out) noexcept
{
if (!mips || !array_size || !planes) return false;
const std::uint64_t per_plane = std::uint64_t{mips} * array_size;
if (index >= per_plane * planes) return false;
out = {static_cast<std::uint32_t>(index % mips), static_cast<std::uint32_t>((index / mips) % array_size),
static_cast<std::uint32_t>(index / per_plane)};
return true;
}

struct Box {
std::uint32_t left, top, front, right, bottom, back;
};

enum class Verdict {
ok,
buffer_to_buffer, // a footprint on both sides: the desktop wants CopyBufferRegion
bad_box, // an empty or inverted source box
source_overflow, // the source box leaves the source subresource
dest_overflow, // the destination origin plus box size leaves the destination subresource
bad_pitch, // a footprint row pitch below one row, or not a multiple of 256
bad_offset, // a footprint offset that is not a multiple of 512
footprint_overflow, // the footprint reaches past the end of its buffer
sample_mismatch, // the two sides differ in sample count
bad_alignment, // a block-compressed copy that does not start on a block
bad_subresource, // an index outside the resource's mips * array slices * planes
format_mismatch, // the two sides name different formats
partial_multisample, // a multisampled copy that is not the whole subresource at the origin
partial_depth_stencil
};

inline const char* name(Verdict v) noexcept
{
switch (v) {
case Verdict::ok: return "ok";
case Verdict::buffer_to_buffer: return "buffer_to_buffer";
case Verdict::bad_box: return "bad_box";
case Verdict::source_overflow: return "source_overflow";
case Verdict::dest_overflow: return "dest_overflow";
case Verdict::bad_pitch: return "bad_pitch";
case Verdict::bad_offset: return "bad_offset";
case Verdict::footprint_overflow: return "footprint_overflow";
case Verdict::sample_mismatch: return "sample_mismatch";
case Verdict::bad_alignment: return "bad_alignment";
case Verdict::bad_subresource: return "bad_subresource";
case Verdict::format_mismatch: return "format_mismatch";
case Verdict::partial_multisample: return "partial_multisample";
case Verdict::partial_depth_stencil: return "partial_depth_stencil";
}
return "unknown";
}

// a * b and a + b with the overflow reported, so no step of the footprint arithmetic wraps.
inline bool checked_mul(std::uint64_t a, std::uint64_t b, std::uint64_t& out) noexcept
{
if (a && b > ~std::uint64_t{0} / a) return false;
out = a * b;
return true;
}
inline bool checked_add(std::uint64_t a, std::uint64_t b, std::uint64_t& out) noexcept
{
if (b > ~std::uint64_t{0} - a) return false;
out = a + b;
return true;
}

enum class End { ok, over_pitch, overflow };

// Bytes a footprint reaches for a w x h x d copy that starts at texel (ox, oy, oz) inside it, from its offset
// (the last row is not padded; slices are as tall as the footprint, not the copy). Every multiply and add is checked.
inline End footprint_end(const Side& s, std::uint32_t w, std::uint32_t h, std::uint32_t d, std::uint32_t ox, std::uint32_t oy,
std::uint32_t oz, std::uint64_t& end) noexcept
{
if (!w || !h || !d) { end = s.offset; return End::ok; }
const std::uint64_t blocks_w = (std::uint64_t{w} + s.block_w - 1) / s.block_w;
const std::uint64_t rows = (std::uint64_t{h} + s.block_h - 1) / s.block_h;
const std::uint64_t start_row = oy / s.block_h;
const std::uint64_t slice_rows = (std::uint64_t{s.height} + s.block_h - 1) / s.block_h;
std::uint64_t row = 0, start_x = 0, row_end = 0;
if (!checked_mul(blocks_w, s.bytes_per_block, row) || !checked_mul(ox / s.block_w, s.bytes_per_block, start_x) ||
!checked_add(start_x, row, row_end))
return End::overflow;
if (row_end > s.row_pitch) return End::over_pitch;
std::uint64_t last_row = 0, row_bytes = 0, slice = 0, slice_bytes = 0, slices = 0, slices_bytes = 0, total = 0;
if (!checked_add(start_row, rows - 1, last_row) || !checked_mul(s.row_pitch, last_row, row_bytes) ||
!checked_add(row_bytes, row_end, slice) || !checked_mul(s.row_pitch, slice_rows, slice_bytes) ||
!checked_add(oz, d - 1, slices) || !checked_mul(slice_bytes, slices, slices_bytes) ||
!checked_add(slices_bytes, slice, total) || !checked_add(s.offset, total, end))
return End::overflow;
return End::ok;
}

// box == nullptr copies the whole source (for a footprint source, the whole footprint).
inline Verdict check(const Side& dst, std::uint32_t x, std::uint32_t y, std::uint32_t z, const Side& src, const Box* box) noexcept
{
if (dst.kind == Kind::placed_footprint && src.kind == Kind::placed_footprint) return Verdict::buffer_to_buffer;
for (const Side* s : {&dst, &src}) {
Subresource unused{};
if (s->kind == Kind::subresource && !decode_subresource(s->subresource, s->mips, s->array_size, s->planes, unused))
return Verdict::bad_subresource;
}
if (dst.samples != src.samples) return Verdict::sample_mismatch;
if (dst.format != src.format) return Verdict::format_mismatch;
const Box whole{0, 0, 0, src.width, src.height, src.depth};
const Box use = box ? *box : whole;
if (use.left >= use.right || use.top >= use.bottom || use.front >= use.back) return Verdict::bad_box;
if (use.right > src.width || use.bottom > src.height || use.back > src.depth) return Verdict::source_overflow;
const Side& tex = dst.kind == Kind::subresource ? dst : src; // the side that knows the block size
if (x % tex.block_w || use.left % tex.block_w || y % tex.block_h || use.top % tex.block_h) return Verdict::bad_alignment;
const std::uint32_t w = use.right - use.left, h = use.bottom - use.top, d = use.back - use.front;
// D3D12 requires a null source box even if an explicit box would cover the whole subresource.
const bool depth_stencil = src.depth_stencil || dst.depth_stencil;
if ((src.samples > 1 || depth_stencil) &&
(box || x || y || z || w != dst.width || h != dst.height || d != dst.depth))
return src.samples > 1 ? Verdict::partial_multisample : Verdict::partial_depth_stencil;
if (std::uint64_t{x} + w > dst.width || std::uint64_t{y} + h > dst.height || std::uint64_t{z} + d > dst.depth)
return Verdict::dest_overflow;
for (const Side* s : {&dst, &src}) {
if (s->kind != Kind::placed_footprint) continue;
if (!s->row_pitch || s->row_pitch % 256) return Verdict::bad_pitch;
if (s->offset % 512) return Verdict::bad_offset;
std::uint64_t end = 0;
const bool is_dst = s == &dst;
const End reach = footprint_end(*s, w, h, d, is_dst ? x : use.left, is_dst ? y : use.top, is_dst ? z : use.front, end);
if (reach == End::over_pitch) return Verdict::bad_pitch;
if (reach == End::overflow) return Verdict::footprint_overflow;
if (end > s->resource_bytes) return Verdict::footprint_overflow;
}
return Verdict::ok;
}

} // namespace x2w_list_copy_texture
Loading
Loading