diff --git a/CMakeLists.txt b/CMakeLists.txt index e9cd32bb57..4a99afe0ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 1beb90dda2..eb7cb26f22 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -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. @@ -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: `), 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`. diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 872f3bb3ca..cb962805ce 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -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) @@ -4786,6 +4787,7 @@ void build_tables() list_typed[139] = reinterpret_cast(&list_reset); list_typed[148] = reinterpret_cast(&list_allocate_upload); list_typed[12] = reinterpret_cast(&list_draw); + list_typed[16] = reinterpret_cast(&list_copy_texture); list_typed[20] = reinterpret_cast(&list_topology); list_typed[21] = reinterpret_cast(&list_viewports); list_typed[22] = reinterpret_cast(&list_scissors); diff --git a/src/startup_stubs/list_copy_texture.h b/src/startup_stubs/list_copy_texture.h new file mode 100644 index 0000000000..96603cf2be --- /dev/null +++ b/src/startup_stubs/list_copy_texture.h @@ -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 +#include + +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(index % mips), static_cast((index / mips) % array_size), + static_cast(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 diff --git a/src/startup_stubs/list_copy_texture.inl b/src/startup_stubs/list_copy_texture.inl new file mode 100644 index 0000000000..3d9a4c31cf --- /dev/null +++ b/src/startup_stubs/list_copy_texture.inl @@ -0,0 +1,165 @@ +// Included by d3d12_xs_device.cpp after d3d12_draw.inl. Command list slot 16, CopyTextureRegion (#870). +// Each location's resource is mapped to its desktop resource, the call is checked by +// list_copy_texture.h, the two resources move to COPY_SOURCE / COPY_DEST for the call (the list's Close +// puts them back to COMMON), and a call the desktop would reject is counted and logged, not forwarded. +#include "list_copy_texture.h" + +volatile LONG list_copy_texture_forwarded, list_copy_texture_refused; + +namespace { + +// Memory planes of a format on the resource's device (1 when the device cannot say). +UINT copy_texture_planes(ID3D12Resource* resource, DXGI_FORMAT format) +{ + ID3D12Device* device = nullptr; + if (FAILED(resource->GetDevice(IID_PPV_ARGS(&device))) || !device) return 1; + D3D12_FEATURE_DATA_FORMAT_INFO info{format, 1}; + const bool known = SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, &info, sizeof(info))); + device->Release(); + return known && info.PlaneCount ? info.PlaneCount : 1; +} + +// One side of the copy, read from the desktop resource it names. False when the location is unusable. +bool copy_texture_side(const D3D12_TEXTURE_COPY_LOCATION* location, ID3D12Resource*& real, x2w_list_copy_texture::Side& side, + DXGI_FORMAT& format, bool& bad_subresource) +{ + Wrapper* w = location && x2w_local::readable(location, sizeof(*location)) ? as_wrapper(location->pResource) : nullptr; + if (!w || w->own().kind != resource_kind || !w->own().real) return false; + real = static_cast(w->own().real); + const D3D12_RESOURCE_DESC desc = real->GetDesc(); + side = {}; + side.block_w = side.block_h = 1; + side.samples = desc.SampleDesc.Count ? desc.SampleDesc.Count : 1; + side.depth_stencil = (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) != 0 || + desc.Format == DXGI_FORMAT_D16_UNORM || desc.Format == DXGI_FORMAT_D32_FLOAT; + side.mips = side.array_size = side.planes = 1; + if (location->Type == D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT) { + if (desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER) return false; + const auto& fp = location->PlacedFootprint; + side.kind = x2w_list_copy_texture::Kind::placed_footprint; + side.width = fp.Footprint.Width; + side.height = fp.Footprint.Height; + side.depth = fp.Footprint.Depth; + side.offset = fp.Offset; + side.row_pitch = fp.Footprint.RowPitch; + side.resource_bytes = desc.Width; + side.format = fp.Footprint.Format; + format = fp.Footprint.Format; + return true; + } + if (location->Type != D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX || desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) return false; + side.mips = desc.MipLevels ? desc.MipLevels : 1; + side.array_size = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? 1 : desc.DepthOrArraySize; + side.planes = copy_texture_planes(real, desc.Format); + side.subresource = location->SubresourceIndex; + side.format = desc.Format; + // The index must lie inside mips * array slices * planes; the mip it names sizes this side. + x2w_list_copy_texture::Subresource parts{}; + if (!x2w_list_copy_texture::decode_subresource(side.subresource, side.mips, side.array_size, side.planes, parts)) { + bad_subresource = true; + return false; + } + const UINT mip = parts.mip; + side.kind = x2w_list_copy_texture::Kind::subresource; + side.width = static_cast((std::max)(1, desc.Width >> mip)); + side.height = (std::max)(1, desc.Height >> mip); + side.depth = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? (std::max)(1, desc.DepthOrArraySize >> mip) : 1; + format = desc.Format; + return true; +} + +// Texels per block and bytes per block, from the desktop's own footprint for a one-subresource copy. +void copy_texture_block(ID3D12Resource* texture, x2w_list_copy_texture::Side& side) +{ + const D3D12_RESOURCE_DESC desc = texture->GetDesc(); + ID3D12Device* device = nullptr; + if (FAILED(texture->GetDevice(IID_PPV_ARGS(&device))) || !device) return; + D3D12_PLACED_SUBRESOURCE_FOOTPRINT fp{}; + UINT rows = 0; + UINT64 row_bytes = 0, total = 0; + device->GetCopyableFootprints(&desc, 0, 1, 0, &fp, &rows, &row_bytes, &total); + device->Release(); + const bool bc = (desc.Format >= DXGI_FORMAT_BC1_TYPELESS && desc.Format <= DXGI_FORMAT_BC5_SNORM) || + (desc.Format >= DXGI_FORMAT_BC6H_TYPELESS && desc.Format <= DXGI_FORMAT_BC7_UNORM_SRGB); + side.block_w = side.block_h = bc ? 4 : 1; + const UINT64 blocks = (std::max)(1, (desc.Width + side.block_w - 1) / side.block_w); + side.bytes_per_block = static_cast(row_bytes / blocks); +} + +// Upload buffers stay in GENERIC_READ and readback buffers in COPY_DEST: the desktop refuses a barrier on either. +bool fixed_heap_state(ID3D12Resource* resource, D3D12_HEAP_TYPE& type) +{ + D3D12_HEAP_PROPERTIES props{}; + D3D12_HEAP_FLAGS flags{}; + if (FAILED(resource->GetHeapProperties(&props, &flags))) return false; + type = props.Type; + return type == D3D12_HEAP_TYPE_UPLOAD || type == D3D12_HEAP_TYPE_READBACK; +} + +} // namespace + +// List slot 16 (unshifted, like slots 12 to 25), CopyTextureRegion(dst, x, y, z, src, box). +void __stdcall list_copy_texture(Wrapper* self, const D3D12_TEXTURE_COPY_LOCATION* dst, UINT x, UINT y, UINT z, + const D3D12_TEXTURE_COPY_LOCATION* src, const D3D12_BOX* box) +{ + if (!self->own().list_open) return; + static LONG logged; + const auto refuse = [&](const char* why) { + InterlockedIncrement(&list_copy_texture_refused); + if (first_calls(logged, 6)) log("x2w-d3d12 CopyTextureRegion refused: %s", why); + }; + ID3D12Resource *real_dst = nullptr, *real_src = nullptr; + x2w_list_copy_texture::Side side_dst{}, side_src{}; + DXGI_FORMAT format_dst = DXGI_FORMAT_UNKNOWN, format_src = DXGI_FORMAT_UNKNOWN; + bool bad_subresource = false; + if (!copy_texture_side(dst, real_dst, side_dst, format_dst, bad_subresource) || + !copy_texture_side(src, real_src, side_src, format_src, bad_subresource)) + return refuse(bad_subresource ? "bad_subresource" : "unknown_resource"); + // Planar (depth-stencil, video) copies name a plane footprint format; only single-plane copies are forwarded. + if (side_dst.planes > 1 || side_src.planes > 1) return refuse("planar_unsupported"); + ID3D12Resource* texture = side_dst.kind == x2w_list_copy_texture::Kind::subresource ? real_dst : real_src; + if (side_dst.kind == x2w_list_copy_texture::Kind::subresource || side_src.kind == x2w_list_copy_texture::Kind::subresource) { + x2w_list_copy_texture::Side blocks{}; + copy_texture_block(texture, blocks); + for (auto* s : {&side_dst, &side_src}) { + s->block_w = blocks.block_w ? blocks.block_w : 1; + s->block_h = blocks.block_h ? blocks.block_h : 1; + if (s->kind == x2w_list_copy_texture::Kind::placed_footprint) s->bytes_per_block = blocks.bytes_per_block; + } + } + x2w_list_copy_texture::Box copy_box{}; + if (box && x2w_local::readable(box, sizeof(*box))) copy_box = {box->left, box->top, box->front, box->right, box->bottom, box->back}; + const auto verdict = x2w_list_copy_texture::check(side_dst, x, y, z, side_src, box ? ©_box : nullptr); + if (verdict != x2w_list_copy_texture::Verdict::ok) return refuse(x2w_list_copy_texture::name(verdict)); + // One resource tracks one state, so two subresources of it cannot be COPY_SOURCE and COPY_DEST at once. + if (real_src == real_dst) return refuse("same_resource"); + D3D12_HEAP_TYPE heap = D3D12_HEAP_TYPE_DEFAULT; + const bool src_fixed = fixed_heap_state(real_src, heap); + if (src_fixed && heap == D3D12_HEAP_TYPE_READBACK) return refuse("bad_heap"); + const bool dst_fixed = fixed_heap_state(real_dst, heap); + if (dst_fixed && heap == D3D12_HEAP_TYPE_UPLOAD) return refuse("bad_heap"); + Wrapper* const src_wrapper = as_wrapper(src->pResource); + Wrapper* const dst_wrapper = as_wrapper(dst->pResource); + // A copied (non-aliased) buffer has no refresh path here, so its desktop bytes may be stale. + if (side_src.kind == x2w_list_copy_texture::Kind::placed_footprint) { + const auto* info = resource_info(src_wrapper); + if (info && info->placement == Placement::copied_buffer) return refuse("stale_source"); + } + // Bring a texture's desktop copy up to date with its guest backing before the copy reads or partly overwrites it, + // and record that upload so a later sample does not replace the copied texels. + if (side_src.kind == x2w_list_copy_texture::Kind::subresource) upload_texture(self, src_wrapper, real_src); + if (side_dst.kind == x2w_list_copy_texture::Kind::subresource) upload_texture(self, dst_wrapper, real_dst); + if (!src_fixed) require_state(self, real_src, D3D12_RESOURCE_STATE_COPY_SOURCE); + if (!dst_fixed) require_state(self, real_dst, D3D12_RESOURCE_STATE_COPY_DEST); + D3D12_TEXTURE_COPY_LOCATION to = *dst, from = *src; + to.pResource = real_dst; + from.pResource = real_src; + static_cast(self->own().real)->CopyTextureRegion(&to, x, y, z, &from, box ? box : nullptr); + InterlockedIncrement(&list_copy_texture_forwarded); +} + +extern "C" __declspec(dllexport) void x2w_d3d12_list_copy_texture_stats(LONG* forwarded, LONG* refused) +{ + if (forwarded) *forwarded = list_copy_texture_forwarded; + if (refused) *refused = list_copy_texture_refused; +} diff --git a/tests/startup_list_copy_texture_check.cpp b/tests/startup_list_copy_texture_check.cpp new file mode 100644 index 0000000000..bfd55e9c8b --- /dev/null +++ b/tests/startup_list_copy_texture_check.cpp @@ -0,0 +1,130 @@ +// Experimental (docs/startup-runtime.md, #870): the argument checks of command list slot 16, +// CopyTextureRegion (src/startup_stubs/list_copy_texture.h). Plain standard C++, synthetic sizes only. +// --mutate overflow drops the destination bounds check, --mutate slice reads an out-of-range array slice as valid; each run must then fail (negative controls). +#include "../src/startup_stubs/list_copy_texture.h" + +#include +#include + +using namespace x2w_list_copy_texture; + +namespace { +int failures; +void expect(Verdict got, Verdict want, const char* what) +{ + if (got != want) { + std::fprintf(stderr, "FAIL: %s: got %s, want %s\n", what, name(got), name(want)); + ++failures; + } +} +constexpr std::uint32_t rgba8 = 28; // DXGI_FORMAT_R8G8B8A8_UNORM +constexpr std::uint32_t bc1 = 71; // DXGI_FORMAT_BC1_UNORM +Side texture(std::uint32_t w, std::uint32_t h, std::uint32_t block = 1) +{ + return {Kind::subresource, w, h, 1, 0, 0, 0, 0, 1, block, block, block > 1 ? bc1 : rgba8, 0, 1, 1, 1}; +} +Side footprint(std::uint32_t w, std::uint32_t h, std::uint64_t offset, std::uint32_t pitch, std::uint32_t bpb, + std::uint64_t bytes, std::uint32_t block = 1) +{ + return {Kind::placed_footprint, w, h, 1, offset, pitch, bpb, bytes, 1, block, block, block > 1 ? bc1 : rgba8, 0, 1, 1, 1}; +} +} // namespace + +int main(int argc, char** argv) +{ + const bool mutate = argc > 2 && !std::strcmp(argv[1], "--mutate") && !std::strcmp(argv[2], "overflow"); + const bool mutate_slice = argc > 2 && !std::strcmp(argv[1], "--mutate") && !std::strcmp(argv[2], "slice"); + + expect(check(texture(64, 64), 0, 0, 0, texture(64, 64), nullptr), Verdict::ok, "whole texture"); + expect(check(texture(64, 64), 32, 32, 0, texture(64, 64), &(const Box&)Box{0, 0, 0, 32, 32, 1}), Verdict::ok, "corner box"); + // The destination origin pushes the box past the destination. + const Verdict past = check(texture(64, 64), 40, 0, 0, texture(64, 64), &(const Box&)Box{0, 0, 0, 32, 32, 1}); + expect(mutate ? Verdict::ok : past, Verdict::dest_overflow, "dest overflow"); + expect(check(texture(64, 64), 0, 0, 0, texture(64, 64), &(const Box&)Box{0, 0, 0, 65, 8, 1}), Verdict::source_overflow, "source overflow"); + expect(check(texture(64, 64), 0, 0, 0, texture(64, 64), &(const Box&)Box{8, 0, 0, 8, 8, 1}), Verdict::bad_box, "empty box"); + expect(check(footprint(16, 16, 0, 64, 4, 4096), 0, 0, 0, footprint(16, 16, 0, 64, 4, 4096), nullptr), Verdict::buffer_to_buffer, "two buffers"); + + // Texture to a 16x16 RGBA8 footprint: 64-byte rows would be below 256, so the pitch is refused. + expect(check(footprint(16, 16, 0, 64, 4, 1 << 20), 0, 0, 0, texture(16, 16), nullptr), Verdict::bad_pitch, "pitch not 256"); + expect(check(footprint(16, 16, 0, 256, 4, 1 << 20), 0, 0, 0, texture(16, 16), nullptr), Verdict::ok, "footprint ok"); + expect(check(footprint(16, 16, 256, 256, 4, 1 << 20), 0, 0, 0, texture(16, 16), nullptr), Verdict::bad_offset, "offset not 512"); + // 16 rows of 256 bytes, the last row 64 bytes: 15 * 256 + 64 = 3904 bytes from offset 512. + expect(check(footprint(16, 16, 512, 256, 4, 512 + 3904), 0, 0, 0, texture(16, 16), nullptr), Verdict::ok, "last row not padded"); + expect(check(footprint(16, 16, 512, 256, 4, 512 + 3903), 0, 0, 0, texture(16, 16), nullptr), Verdict::footprint_overflow, "one byte short"); + // Row wider than the pitch. + expect(check(footprint(128, 4, 0, 256, 4, 1 << 20), 0, 0, 0, texture(128, 4), nullptr), Verdict::bad_pitch, "row over pitch"); + + // Block-compressed: 4x4 blocks of 8 bytes; a 6-texel start is not on a block. + expect(check(texture(64, 64, 4), 6, 0, 0, texture(64, 64, 4), nullptr), Verdict::bad_alignment, "block alignment"); + expect(check(footprint(64, 64, 0, 256, 8, 256 * 16, 4), 0, 0, 0, texture(64, 64, 4), nullptr), Verdict::ok, "bc footprint"); + + // The destination origin counts: a 1x1 RGBA8 copy at Y=63 of a 64x64, 256-pitch footprint ends at byte 16132. + expect(check(footprint(64, 64, 0, 256, 4, 256), 0, 63, 0, texture(1, 1), nullptr), Verdict::footprint_overflow, "dest origin counts"); + expect(check(footprint(64, 64, 0, 256, 4, 16132), 0, 63, 0, texture(1, 1), nullptr), Verdict::ok, "dest origin fits"); + // The source box origin counts too. + expect(check(texture(1, 1), 0, 0, 0, footprint(64, 64, 0, 256, 4, 256), &(const Box&)Box{0, 63, 0, 1, 64, 1}), Verdict::footprint_overflow, "box origin counts"); + // 3D: a slice is as tall as the footprint (64 rows), not as the copy (1 row). + Side volume = footprint(64, 64, 0, 256, 4, 256 * 64); + volume.depth = 4; + expect(check(volume, 0, 0, 1, texture(1, 1), nullptr), Verdict::footprint_overflow, "slice stride from footprint height"); + + Side ms = texture(64, 64); + ms.samples = 4; + expect(check(texture(64, 64), 0, 0, 0, ms, nullptr), Verdict::sample_mismatch, "multisampled source"); + Side ms2 = ms; + ms2.samples = 2; + expect(check(ms2, 0, 0, 0, ms, nullptr), Verdict::sample_mismatch, "2x against 4x"); + expect(check(ms, 0, 0, 0, ms, nullptr), Verdict::ok, "whole 4x subresource"); + const Box full_box{0, 0, 0, 64, 64, 1}; + expect(check(ms, 0, 0, 0, ms, &full_box), Verdict::partial_multisample, "4x explicit full box"); + Side depth = texture(64, 64); + depth.depth_stencil = true; + expect(check(depth, 0, 0, 0, depth, nullptr), Verdict::ok, "whole depth subresource"); + expect(check(depth, 0, 0, 0, depth, &full_box), Verdict::partial_depth_stencil, "depth explicit full box"); + expect(check(ms, 8, 0, 0, ms, &(const Box&)Box{0, 0, 0, 56, 64, 1}), Verdict::partial_multisample, "4x offset"); + expect(check(ms, 0, 0, 0, ms, &(const Box&)Box{0, 0, 0, 32, 32, 1}), Verdict::partial_multisample, "4x partial box"); + expect(check(footprint(64, 64, 0, 256, 4, 1 << 20), 0, 0, 0, ms, nullptr), Verdict::sample_mismatch, "4x to buffer"); + + // Formats must be the same. + Side bgra = texture(64, 64); + bgra.format = 87; // DXGI_FORMAT_B8G8R8A8_UNORM + expect(check(bgra, 0, 0, 0, texture(64, 64), nullptr), Verdict::format_mismatch, "texture formats differ"); + Side fp_bc = footprint(64, 64, 0, 256, 4, 1 << 20); + fp_bc.format = bc1; + expect(check(fp_bc, 0, 0, 0, texture(64, 64), nullptr), Verdict::format_mismatch, "footprint format differs"); + + // Subresource index = mip + slice * mips + plane * mips * array_size. + Subresource parts{}; + expect(decode_subresource(7, 3, 2, 2, parts) && parts.mip == 1 && parts.slice == 0 && parts.plane == 1 ? Verdict::ok : Verdict::bad_box, + Verdict::ok, "decode mip 1, slice 0, plane 1"); + expect(decode_subresource(5, 3, 2, 1, parts) && parts.mip == 2 && parts.slice == 1 && parts.plane == 0 ? Verdict::ok : Verdict::bad_box, + Verdict::ok, "decode mip 2, slice 1"); + expect(decode_subresource(11, 3, 2, 2, parts) ? Verdict::ok : Verdict::bad_subresource, Verdict::ok, "last index of 3 x 2 x 2"); + expect(decode_subresource(12, 3, 2, 2, parts) ? Verdict::ok : Verdict::bad_subresource, Verdict::bad_subresource, "index past the planes"); + expect(decode_subresource(0, 0, 1, 1, parts) ? Verdict::ok : Verdict::bad_subresource, Verdict::bad_subresource, "zero mips"); + // A 4-mip, 3-slice texture has 12 subresources; index 12 is the first past the last slice, and the old + // mip-only modulo would have read it as mip 0. + Side arr = texture(64, 64); + arr.mips = 4; + arr.array_size = 3; + arr.subresource = 11; + expect(check(arr, 0, 0, 0, texture(64, 64), nullptr), Verdict::ok, "last array subresource"); + arr.subresource = 12; + expect(mutate_slice ? Verdict::ok : check(arr, 0, 0, 0, texture(64, 64), nullptr), Verdict::bad_subresource, "slice out of range"); + expect(check(texture(64, 64), 0, 0, 0, arr, nullptr), Verdict::bad_subresource, "source slice out of range"); + arr.subresource = 0xffffffffu; + expect(check(arr, 0, 0, 0, texture(64, 64), nullptr), Verdict::bad_subresource, "huge index"); + + // Footprint arithmetic is checked at every multiply and add. + Side wide = footprint(64, 0xffffffffu, 0, 256, 4, ~std::uint64_t{0}); + wide.depth = 0xffffffffu; + Side big = texture(64, 0xffffffffu); + big.depth = 0xffffffffu; + expect(check(wide, 0, 0, 0xfffffffeu, big, &(const Box&)Box{0, 0, 0, 1, 1, 1}), Verdict::footprint_overflow, "slice stride overflow"); + expect(check(footprint(1, 3, ~std::uint64_t{0} - 511, 256, 4, ~std::uint64_t{0}), 0, 0, 0, texture(1, 3), nullptr), + Verdict::footprint_overflow, "offset plus size wraps"); + + if (failures) return 1; + std::puts("ok"); + return 0; +} diff --git a/tests/startup_list_copy_texture_warp.cpp b/tests/startup_list_copy_texture_warp.cpp new file mode 100644 index 0000000000..46e6e033a0 --- /dev/null +++ b/tests/startup_list_copy_texture_warp.cpp @@ -0,0 +1,216 @@ +// CopyTextureRegion (command list slot 16) through the wrapped list, read back (#870). +// +// startup_list_copy_texture_warp (X2W_D3D12_PERMISSIVE=1 X2W_D3D12_LIST_NOOP=1 X2W_D3D12_RENDER=1 X2W_D3D12_DEBUG=1) +// +// Through D3D12XboxCreateDevice the probe makes a 4x4 RGBA8 texture with 2 mips and 3 array slices (six +// subresources), an upload buffer holding two distinct 4x4 images and a readback buffer, all as console +// resources. Then it records, submits and reads back: +// +// 1. upload image A -> subresource 2 (mip 0, slice 1), image B -> subresource 4 (mip 0, slice 2), and both +// back to the readback buffer: the readback holds A then B, and both copies were forwarded. +// 2. a list whose three calls must each be refused before any desktop copy: subresource 6 (one past the last +// array slice, which a mip-only modulo would read as mip 0), subresource 0xffffffff, and a footprint +// format that differs from the texture's. The readback buffer, pre-filled with a sentinel, is unchanged, +// and the refusal counter rises by three with the forward counter unchanged. +// +// Exit codes: 0 pass, 77 no physical adapter, other values name the failing step. +#include +#include +#include +#include + +#include +#include +#include +#include + +using Microsoft::WRL::ComPtr; +extern "C" __declspec(dllimport) HRESULT WINAPI D3D12XboxCreateDevice(IUnknown*, const void*, REFIID, void**); +extern "C" __declspec(dllimport) void x2w_d3d12_list_copy_texture_stats(LONG* forwarded, LONG* refused); + +namespace { +using Word = std::uint64_t; +template Word arg(T* value) { return reinterpret_cast(value); } +Word invoke(void* object, unsigned index, Word a = 0, Word b = 0, Word c = 0, Word d = 0, Word e = 0, Word f = 0, Word g = 0) +{ + using Slot = Word (WINAPI*)(void*, Word, Word, Word, Word, Word, Word, Word); + return reinterpret_cast((*static_cast(object))[index])(object, a, b, c, d, e, f, g); +} +bool ok(Word hr) { return SUCCEEDED(static_cast(hr)); } + +constexpr UINT side = 4, mips = 2, slices = 3, pitch = 256; +constexpr UINT image_bytes = 1024; // 4 rows at the 256-byte pitch; image B starts at this offset (a multiple of 512) +constexpr std::uint8_t sentinel = 0xcd; + +// A console resource through device slot 127 (CreateCommittedResource3). +void* make_resource(void* device, D3D12_HEAP_TYPE type, const D3D12_RESOURCE_DESC& desc, D3D12_RESOURCE_STATES state) +{ + using Create = HRESULT(WINAPI*)(void*, const D3D12_HEAP_PROPERTIES*, UINT, const D3D12_RESOURCE_DESC*, UINT, const void*, + void*, UINT, const void*, const GUID*, void**); + const D3D12_HEAP_PROPERTIES heap{type}; + void* resource = nullptr; + const auto create = reinterpret_cast((*static_cast(device))[127]); + return SUCCEEDED(create(device, &heap, D3D12_HEAP_FLAG_NONE, &desc, state, nullptr, nullptr, 0, nullptr, + &__uuidof(ID3D12Resource), &resource)) + ? resource + : nullptr; +} + +D3D12_RESOURCE_DESC buffer_desc(UINT64 bytes) +{ + return {D3D12_RESOURCE_DIMENSION_BUFFER, 0, bytes, 1, 1, 1, DXGI_FORMAT_UNKNOWN, {1, 0}, D3D12_TEXTURE_LAYOUT_ROW_MAJOR}; +} + +D3D12_TEXTURE_COPY_LOCATION footprint(void* buffer, UINT64 offset, DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM) +{ + D3D12_TEXTURE_COPY_LOCATION location{static_cast(buffer), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; + location.PlacedFootprint = {offset, {format, side, side, 1, pitch}}; + return location; +} + +D3D12_TEXTURE_COPY_LOCATION subresource(void* texture, UINT index) +{ + D3D12_TEXTURE_COPY_LOCATION location{static_cast(texture), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; + location.SubresourceIndex = index; + return location; +} + +// Image A holds byte i*3+1 at texel byte i, image B byte 255-i*5 (both wrap at 256). +std::uint8_t image_byte(int image, UINT row, UINT column) +{ + const UINT i = row * side * 4 + column; + return image == 0 ? static_cast(i * 3 + 1) : static_cast(255 - i * 5); +} + +bool wait_for(void* fence, Word value) +{ + const auto deadline = GetTickCount64() + 10000; + while (invoke(fence, 8) < value) { + if (GetTickCount64() >= deadline) return false; + Sleep(1); + } + return true; +} + +// Compares the 4x4 image the readback buffer holds at offset against image number, row by row. +bool image_matches(const std::uint8_t* memory, UINT offset, int image) +{ + for (UINT row = 0; row < side; ++row) + for (UINT column = 0; column < side * 4; ++column) + if (memory[offset + row * pitch + column] != image_byte(image, row, column)) { + std::printf("image %d row %u byte %u: got %02x want %02x\n", image, row, column, memory[offset + row * pitch + column], + image_byte(image, row, column)); + return false; + } + return true; +} +} // namespace + +int main(int argc, char** argv) +{ + if (argc != 2) return 2; + ComPtr factory; + if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) return 3; + ComPtr adapter; + if (std::strcmp(argv[1], "hardware") == 0) { + for (UINT n = 0;; ++n) { + ComPtr candidate; + const HRESULT hr = factory->EnumAdapterByGpuPreference(n, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&candidate)); + if (hr == DXGI_ERROR_NOT_FOUND) return 77; + if (FAILED(hr)) return 3; + DXGI_ADAPTER_DESC1 desc{}; + if (FAILED(candidate->GetDesc1(&desc))) return 3; + if (!(desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) && + SUCCEEDED(D3D12CreateDevice(candidate.Get(), D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), nullptr))) { + adapter = candidate; + break; + } + } + } else if (std::strcmp(argv[1], "warp") != 0 || FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&adapter)))) { + return 3; + } + DXGI_ADAPTER_DESC adapter_desc{}; + if (FAILED(adapter->GetDesc(&adapter_desc))) return 3; + std::printf("adapter=%ls\n", adapter_desc.Description); + + std::array parameters{}; + void* device = nullptr; + if (FAILED(D3D12XboxCreateDevice(adapter.Get(), parameters.data(), __uuidof(ID3D12Device), &device))) return 4; + + const D3D12_RESOURCE_DESC texture_desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, side, side, slices, mips, + DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE}; + void* texture = make_resource(device, D3D12_HEAP_TYPE_DEFAULT, texture_desc, D3D12_RESOURCE_STATE_COMMON); + void* upload = make_resource(device, D3D12_HEAP_TYPE_UPLOAD, buffer_desc(2 * image_bytes), D3D12_RESOURCE_STATE_GENERIC_READ); + void* readback = make_resource(device, D3D12_HEAP_TYPE_READBACK, buffer_desc(2 * image_bytes), D3D12_RESOURCE_STATE_COPY_DEST); + if (!texture || !upload || !readback) return 5; + + void* memory = nullptr; + if (!ok(invoke(upload, 8, 0, 0, arg(&memory))) || !memory) return 6; + for (int image = 0; image < 2; ++image) + for (UINT row = 0; row < side; ++row) + for (UINT column = 0; column < side * 4; ++column) + static_cast(memory)[image * image_bytes + row * pitch + column] = image_byte(image, row, column); + invoke(upload, 9, 0, 0); + + D3D12_COMMAND_QUEUE_DESC queue_desc{}; + const unsigned list_desc[] = {D3D12_COMMAND_LIST_TYPE_DIRECT, 2, 1, 0}; + void *queue = nullptr, *fence = nullptr, *allocator = nullptr, *list = nullptr; + if (!ok(invoke(device, 8, arg(&queue_desc), arg(&__uuidof(ID3D12CommandQueue)), arg(&queue))) || + !ok(invoke(device, 36, 0, 0, arg(&__uuidof(ID3D12Fence)), arg(&fence))) || + !ok(invoke(device, 9, D3D12_COMMAND_LIST_TYPE_DIRECT, arg(&__uuidof(ID3D12CommandAllocator)), arg(&allocator))) || + !ok(invoke(device, 73, arg(list_desc), arg(allocator), 0, arg(&__uuidof(ID3D12GraphicsCommandList)), arg(&list)))) + return 7; + Word fence_value = 0; + const auto submit = [&] { + void* lists[] = {list}; + invoke(queue, 10, 1, arg(lists)); + return ok(invoke(queue, 14, arg(fence), ++fence_value)) && wait_for(fence, fence_value); + }; + const auto copy = [&](const D3D12_TEXTURE_COPY_LOCATION& to, const D3D12_TEXTURE_COPY_LOCATION& from) { + invoke(list, 16, arg(&to), 0, 0, 0, arg(&from), 0); + }; + LONG forwarded = 0, refused = 0; + x2w_d3d12_list_copy_texture_stats(&forwarded, &refused); + + // 1. Two distinct images into two array slices and back. + const auto a_up = footprint(upload, 0), b_up = footprint(upload, image_bytes); + const auto a_out = footprint(readback, 0), b_out = footprint(readback, image_bytes); + const auto slice1 = subresource(texture, 2), slice2 = subresource(texture, 4); + if (!ok(invoke(list, 139, arg(allocator), 0))) return 8; + copy(slice1, a_up); + copy(slice2, b_up); + copy(a_out, slice1); + copy(b_out, slice2); + if (!ok(invoke(list, 9)) || !submit()) return 9; + LONG forwarded1 = 0, refused1 = 0; + x2w_d3d12_list_copy_texture_stats(&forwarded1, &refused1); + std::printf("1 forwarded +%ld refused +%ld (expected +4, +0)\n", forwarded1 - forwarded, refused1 - refused); + if (forwarded1 - forwarded != 4 || refused1 != refused) return 10; + if (!ok(invoke(readback, 8, 0, 0, arg(&memory))) || !memory) return 11; + const bool images = image_matches(static_cast(memory), 0, 0) && + image_matches(static_cast(memory), image_bytes, 1); + std::memset(memory, sentinel, 2 * image_bytes); // mapped readback memory is CPU-writable for the next step + invoke(readback, 9, 0, 0); + if (!images) return 12; + + // 2. Out-of-range subresources and a format mismatch are refused before any desktop copy. + if (!ok(invoke(list, 139, arg(allocator), 0))) return 13; + copy(a_out, subresource(texture, mips * slices)); // one past the last array slice + copy(subresource(texture, 0xffffffffu), a_up); // an index far outside + copy(subresource(texture, 0), footprint(upload, 0, DXGI_FORMAT_B8G8R8A8_UNORM)); // formats differ + if (!ok(invoke(list, 9)) || !submit()) return 14; + LONG forwarded2 = 0, refused2 = 0; + x2w_d3d12_list_copy_texture_stats(&forwarded2, &refused2); + std::printf("2 forwarded +%ld refused +%ld (expected +0, +3)\n", forwarded2 - forwarded1, refused2 - refused1); + if (forwarded2 != forwarded1 || refused2 - refused1 != 3) return 15; + if (!ok(invoke(readback, 8, 0, 0, arg(&memory))) || !memory) return 16; + bool unchanged = true; + for (UINT i = 0; i < 2 * image_bytes; ++i) unchanged = unchanged && static_cast(memory)[i] == sentinel; + invoke(readback, 9, 0, 0); + if (!unchanged) { + std::puts("2 the readback buffer changed"); + return 17; + } + std::puts("ok"); + return 0; +}