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
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,26 @@ foreach(backend IN ITEMS warp hardware)
ENVIRONMENT "X2W_D3D12_PERMISSIVE=1;X2W_D3D12_LIST_NOOP=1;X2W_D3D12_RENDER=1;X2W_D3D12_DEBUG=1")
endforeach()

# CopyResource, ResolveSubresource and CopyBufferRegion on a recording list (slots 17, 19 and 15).
add_executable(startup_list_copy tests/startup_list_copy.cpp)
target_compile_features(startup_list_copy PRIVATE cxx_std_17)
target_link_libraries(startup_list_copy PRIVATE d3d12_xs_shim d3d12 dxgi)
foreach(backend IN ITEMS warp hardware)
add_test(NAME startup_list_copy_${backend} COMMAND startup_list_copy ${backend})
set_tests_properties(startup_list_copy_${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()

# Validation of the whole-resource copies (src/startup_stubs/list_copy.h); the mutated runs are negative controls.
add_executable(startup_list_copy_check tests/startup_list_copy_check.cpp)
target_include_directories(startup_list_copy_check PRIVATE src)
target_compile_features(startup_list_copy_check PRIVATE cxx_std_17)
add_test(NAME startup_list_copy_validation COMMAND startup_list_copy_check)
foreach(mutation IN ITEMS overflow typeless samples)
add_test(NAME startup_list_copy_validation_${mutation}_control COMMAND startup_list_copy_check --mutate ${mutation})
set_tests_properties(startup_list_copy_validation_${mutation}_control PROPERTIES WILL_FAIL TRUE)
endforeach()

add_executable(startup_nv12_draw tests/startup_nv12_draw.cpp tests/desktop_gpu.cpp)
target_compile_features(startup_nv12_draw PRIVATE cxx_std_17)
target_include_directories(startup_nv12_draw PRIVATE src)
Expand Down
20 changes: 20 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,26 @@ inside the range, an empty view alone) and reads the counters through the `x2w_d
then copies a view into an empty slot, clears the copy red and reads the target back. It does not draw. The export reads
the same counters the totals line prints.

Command-list slots 15 (`CopyBufferRegion`), 17 (`CopyResource`) and 19 (`ResolveSubresource`), unshifted like slots 12 to 25,
copy whole resources in render mode (`src/startup_stubs/list_copy.inl`; the checks are in `list_copy.h`). Each argument is
mapped to its desktop resource, the pair is moved to the copy or resolve states for the call and back to COMMON at Close
(an upload buffer stays readable and a readback buffer stays a copy destination), and the call is forwarded. A call the
desktop would reject is counted, logged (first six) and not forwarded, with the reason: `unresolved`, `same_resource`,
`not_buffer`, `not_texture`, `dimension_mismatch`, `size_mismatch`, `format_mismatch`, `sample_mismatch`,
`bad_subresource`, `source_not_multisampled`, `destination_multisampled`, `bad_format`, `zero_size` or
`range_overflow`, `heap_direction` (an upload buffer as a destination, a readback buffer as a source),
`initial_state` (a placed buffer that does not start in COMMON) or `title_backed_source` (a title-backed default-heap
buffer or an upload buffer, which have no desktop copy the list can refresh without rewriting bytes an earlier submission may
still read). A source the title maps at its own memory must be current: a texture takes the draw path's upload and is refused
when that upload cannot stage it (unreadable or unsupported memory), and an aliased placed buffer is already current. A copy or resolve destination with title memory is marked GPU-written when its list is submitted (on its own list at once), so a later draw does not
upload the stale title memory over the result; a list discarded by Reset or a failed Close marks nothing. CopyResource accepts formats of one DXGI family (colour, depth, 8/16/32-bit
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.
`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.

## Composite provenance fixture

Issue #1628 asks whether a uniform colour left in a composite's destination came from the composite or was
Expand Down
8 changes: 8 additions & 0 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,8 @@ struct TextureUpload {
bool done = false, tiled = false;
ID3D12CommandList* pending_list = nullptr;
u64 revision = 0;
bool staged = false; // the last upload attempt put the title memory into the desktop texture
bool gpu_written = false; // a list copy or resolve wrote the texture; its title memory is stale and must not be uploaded
};
std::unordered_map<ID3D12Resource*, TextureUpload>& texture_uploads() { static std::unordered_map<ID3D12Resource*, TextureUpload> m; return m; }
u64 address_upload_revision;
Expand Down Expand Up @@ -1001,6 +1003,7 @@ u64 hash_memory(const unsigned char* data, std::size_t bytes)
for (std::size_t i = words * 8; i < bytes; ++i) h = (h ^ data[i]) * 0x100000001b3ull;
return h;
}
bool list_wrote(Wrapper* list, ID3D12Resource* resource); // list_copy.inl
bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
{
auto* const memory = static_cast<const unsigned char*>(resource->own().title_memory);
Expand All @@ -1024,6 +1027,8 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
}
}
if (!memory || !list || !uploadable) return !console;
if (list_wrote(self, texture)) return !console; // this recording's own copy; not yet submitted
if (const auto written_by_gpu = texture_uploads().find(texture); written_by_gpu != texture_uploads().end() && written_by_gpu->second.gpu_written) return !console;
if (desc.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) return !console;
const UINT mips = desc.MipLevels ? desc.MipLevels : 1;
const UINT slices = desc.DepthOrArraySize ? desc.DepthOrArraySize : 1;
Expand Down Expand Up @@ -1083,6 +1088,7 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
if (!state.done) InterlockedIncrement(&textures_unreadable);
if (console) return false;
state.done = true;
state.staged = false;
state.generation = generation;
return true;
}
Expand All @@ -1108,6 +1114,7 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
}
std::memset(owned.cpu, 0, static_cast<std::size_t>(bytes));
}
state.staged = false;
require_state(self, texture, D3D12_RESOURCE_STATE_COPY_DEST);
if (console) {
for (UINT slice = 0; slice < slices; ++slice) {
Expand Down Expand Up @@ -1178,6 +1185,7 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
state.checked_frame = frame_number;
state.tiled = tiled;
state.done = true;
state.staged = true;
InterlockedIncrement(&textures_uploaded);
InterlockedExchangeAdd(&texture_bytes_uploaded, static_cast<LONG>(total >> 10));
static LONG shown;
Expand Down
10 changes: 10 additions & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "startup_stubs/memory_srv_report.h"
#include "startup_stubs/memory_srv_buffer.h"
#include "startup_stubs/single_element_srv.h"
#include "startup_stubs/list_copy.h"

#include "startup_stubs/slice_footprints.h"
#include "startup_stubs/present_signature_sampler.h"
Expand Down Expand Up @@ -1084,6 +1085,8 @@ HRESULT __stdcall device_residency(Wrapper* self, UINT count, void* const* objec
void release_targets(Wrapper* self);
void end_open_queries(Wrapper* self, ID3D12GraphicsCommandList* list);
void reset_list_dynamic_state(Wrapper* self);
void discard_list_gpu_writes(Wrapper* self);
void commit_list_gpu_writes(Wrapper* self);
void discard_draw_sampler_heaps(ID3D12CommandList* list);
void discard_address_uploads(ID3D12CommandList* list);
void use_as_target(Wrapper* self, ID3D12Resource* resource);
Expand Down Expand Up @@ -1409,6 +1412,7 @@ HRESULT __stdcall list_reset(Wrapper* self, void* allocator)
ReleaseSRWLockExclusive(&allocator_lock);
if (SUCCEEDED(hr)) {
reset_list_dynamic_state(self);
discard_list_gpu_writes(self);
discard_draw_sampler_heaps(previous);
discard_address_uploads(previous);
if (draw_dump_frame()) draw_dumps().discard(previous);
Expand Down Expand Up @@ -1443,6 +1447,7 @@ HRESULT __stdcall list_close(Wrapper* self)
ReleaseSRWLockExclusive(&allocator_lock);
InterlockedIncrement(&render_closes);
if (FAILED(hr)) {
discard_list_gpu_writes(self);
discard_draw_sampler_heaps(list);
discard_address_uploads(list);
if (draw_dump_frame()) draw_dumps().discard(list);
Expand Down Expand Up @@ -2725,6 +2730,7 @@ void __stdcall queue_execute(Wrapper* self, UINT count, void* const* lists)
retire_address_uploads(queue, ready);
retire_draw_sampler_heaps(queue, ready);
note_submits(queue, submitted);
for (Wrapper* w : submitted) commit_list_gpu_writes(w);
InterlockedIncrement(&render_executes);
}
ReleaseSRWLockExclusive(&allocator_lock);
Expand Down Expand Up @@ -3404,6 +3410,7 @@ HRESULT __stdcall device_place_resource_x(Wrapper* self, void* address, const D3
}

#include "d3d12_memory_srv.inl"
#include "startup_stubs/list_copy.inl"

// The title exports __XMemAllocationHooks__ = {alloc(size, attributes), free(ptr, attributes), ...}.
// Its alloc routine selects a hook chain by allocatorId (attribute bits 16-23). Around the creation
Expand Down Expand Up @@ -4081,6 +4088,9 @@ void build_tables()
if (render_mode()) {
list_typed[9] = reinterpret_cast<ListTyped>(&list_close);
list_typed[52] = reinterpret_cast<ListTyped>(&list_clear_target);
list_typed[15] = reinterpret_cast<ListTyped>(&list_copy_buffer_region);
list_typed[17] = reinterpret_cast<ListTyped>(&list_copy_resource);
list_typed[19] = reinterpret_cast<ListTyped>(&list_resolve_subresource);
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);
Expand Down
Loading
Loading