diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index c85af6416..e07c054af 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -57,6 +57,7 @@ #include "startup_stubs/resource_address_index.h" #include "startup_stubs/placed_build.h" #include "startup_stubs/placed_buffer_range.h" +#include "startup_stubs/gpu_to_cpu_counters.h" #include "startup_stubs/linear_buffer_backing.h" #include "startup_stubs/memory_srv_report.h" #include "startup_stubs/memory_srv_census.h" @@ -473,6 +474,52 @@ const char* const time_names[time_count] = {"list", "device-noop", "forward", "c volatile LONG64 time_ticks[time_count], time_calls[time_count]; volatile LONG64 list_slot_ticks[256], list_slot_calls[256]; // per command-list slot volatile LONG64 list_slot_census[256]; // per command-list slot, reported with the render totals + +// GPU-to-CPU census (#3676 item f). Counts since the start, 64-bit, never reset. Logging only: +// nothing here changes allocation, lifetime, copy forwarding or the pointer Map returns. +// map_readback: Resource::Map on a desktop READBACK heap, including a resource whose title memory +// is returned without calling the desktop Map. +// copy_dest_title: a forwarded CopyBufferRegion, CopyResource or CopyTextureRegion whose destination +// has title memory. The GPU write stays on the desktop object. +// resolve_dest_title: a forwarded ResolveSubresource with that same destination. +// dest_title_refusals: any refused copy or resolve whose destination has title memory. +// dest_title_query_refusals: the subset where ResolveQueryData rejects title memory specifically. +volatile LONG64 gpu_to_cpu_map_readback, gpu_to_cpu_copy_dest_title, gpu_to_cpu_resolve_dest_title, + gpu_to_cpu_dest_title_refusals, gpu_to_cpu_dest_title_query_refusals; +static_assert(static_cast(D3D12_HEAP_TYPE_DEFAULT) == x2w_startup::gpu_to_cpu_heap_default, "heap type values"); +static_assert(static_cast(D3D12_HEAP_TYPE_UPLOAD) == x2w_startup::gpu_to_cpu_heap_upload, "heap type values"); +static_assert(static_cast(D3D12_HEAP_TYPE_READBACK) == x2w_startup::gpu_to_cpu_heap_readback, "heap type values"); + +void note_gpu_to_cpu_map(ID3D12Resource* resource) +{ + D3D12_HEAP_PROPERTIES properties{}; + if (!resource || FAILED(resource->GetHeapProperties(&properties, nullptr))) return; + if (x2w_startup::gpu_to_cpu_map_is_readback(static_cast(properties.Type))) + InterlockedIncrement64(&gpu_to_cpu_map_readback); +} +void note_gpu_to_cpu_copy_dest(const Wrapper* destination) +{ + if (destination && destination->own().title_memory) InterlockedIncrement64(&gpu_to_cpu_copy_dest_title); +} +void note_gpu_to_cpu_resolve_dest(const Wrapper* destination) +{ + if (destination && destination->own().title_memory) InterlockedIncrement64(&gpu_to_cpu_resolve_dest_title); +} +void note_gpu_to_cpu_dest_refusal(const Wrapper* destination) +{ + if (destination && destination->own().title_memory) InterlockedIncrement64(&gpu_to_cpu_dest_title_refusals); +} +void note_gpu_to_cpu_query_dest_refusal() { InterlockedIncrement64(&gpu_to_cpu_dest_title_query_refusals); } +x2w_startup::GpuToCpuCounts gpu_to_cpu_counts() +{ + x2w_startup::GpuToCpuCounts counts{}; + counts.map_readback = gpu_to_cpu_map_readback; + counts.copy_dest_title = gpu_to_cpu_copy_dest_title; + counts.resolve_dest_title = gpu_to_cpu_resolve_dest_title; + counts.dest_title_refusals = gpu_to_cpu_dest_title_refusals; + counts.dest_title_query_refusals = gpu_to_cpu_dest_title_query_refusals; + return counts; +} struct Timed { int category; LARGE_INTEGER start; @@ -4166,10 +4213,15 @@ void end_open_queries(Wrapper* self, ID3D12GraphicsCommandList* list) // a readback heap, which stays in COPY_DEST, so it is not moved through the list's COMMON state tracking. void __stdcall list_resolve_query(Wrapper* self, void* heap, UINT type, UINT start, UINT count, void* destination, u64 offset) { + const Wrapper* w = as_wrapper(destination); + const auto refuse = [&](const char* why) { + note_gpu_to_cpu_dest_refusal(w); + refuse_query(resolve_query, why, type, start, count); + }; auto* list = recording(self); const char* reason = nullptr; ID3D12QueryHeap* real = occlusion_heap(list, heap, type, start, count, &reason); - if (!real) return refuse_query(resolve_query, reason, type, start, count); + if (!real) return refuse(reason); // Resolving a query that was never ended, or ended with the other occlusion type, reads uninitialized data and // can remove the device: every index in the range must have a matching EndQuery on this recording. const auto& ended = pass_of(self).ended_queries; @@ -4177,23 +4229,23 @@ void __stdcall list_resolve_query(Wrapper* self, void* heap, UINT type, UINT sta if (std::none_of(ended.begin(), ended.end(), [&](const Pass::OpenQuery& q) { return q.heap == real && q.index == start + i && q.type == type; })) - return refuse_query(resolve_query, "an index in the range was not ended with this type on this recording", type, - start, count); - const Wrapper* w = as_wrapper(destination); + return refuse("an index in the range was not ended with this type on this recording"); if (!w || w->own().kind != resource_kind || !w->own().real) - return refuse_query(resolve_query, "destination has no desktop resource", type, start, count); + return refuse("destination has no desktop resource"); // Map of a resource with title memory returns that memory, which a desktop resolve never reaches. - if (w->own().title_memory) - return refuse_query(resolve_query, "destination has title memory", type, start, count); + if (w->own().title_memory) { + note_gpu_to_cpu_query_dest_refusal(); + return refuse("destination has title memory"); + } auto* resource = static_cast(w->own().real); D3D12_HEAP_PROPERTIES properties{}; const auto desc = resource->GetDesc(); if (desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || FAILED(resource->GetHeapProperties(&properties, nullptr)) || properties.Type != D3D12_HEAP_TYPE_READBACK) - return refuse_query(resolve_query, "destination is not a readback buffer", type, start, count); + return refuse("destination is not a readback buffer"); const u64 bytes = u64{count} * sizeof(UINT64); if ((offset & 7) || offset > desc.Width || bytes > desc.Width - offset) - return refuse_query(resolve_query, "outside the destination buffer", type, start, count); + return refuse("outside the destination buffer"); list->ResolveQueryData(real, static_cast(type), start, count, resource, offset); } // Device slot 39, CreateQueryHeap(desc, riid, ppv): forwarded, and the desc kept for the query slots. @@ -6002,6 +6054,8 @@ HRESULT __stdcall resource_map(Wrapper* self, UINT subresource, const D3D12_RANG HRESULT hr = S_OK; void* mapped = self->own().title_memory; if (!mapped) hr = static_cast(self->own().real)->Map(subresource, range, &mapped); + // A READBACK heap is counted even when title memory is returned and the desktop Map is not called. + if (self->own().real) note_gpu_to_cpu_map(static_cast(self->own().real)); if (data) *data = SUCCEEDED(hr) ? mapped : nullptr; if (FAILED(hr)) check_device("Resource::Map", 8); static LONG calls, failures; @@ -9053,6 +9107,11 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa x2w_startup::placed_backing_line(placed_backing_counts(), line, sizeof(line)); log("%s", line); } + { + char line[320]; + x2w_startup::gpu_to_cpu_line(gpu_to_cpu_counts(), line, sizeof(line)); + log("%s", line); + } if (descriptor_heap_shared_boundary || descriptor_heap_off_boundary) log("x2w-d3d12 descriptor heap lookups the handle did not settle: %lld on a boundary of several heaps, " "%lld on no boundary", descriptor_heap_shared_boundary, descriptor_heap_off_boundary); @@ -9415,6 +9474,14 @@ extern "C" __declspec(dllexport) BOOL x2w_d3d12_placed_backing_snapshot(x2w_star return TRUE; } +// Map, copy and resolve counts since the start (startup_gpu_to_cpu_*). Logging only. +extern "C" __declspec(dllexport) BOOL x2w_d3d12_gpu_to_cpu_snapshot(x2w_startup::GpuToCpuCounts* out, SIZE_T bytes) +{ + if (!out || bytes != sizeof(*out)) return FALSE; + *out = gpu_to_cpu_counts(); + return TRUE; +} + extern "C" __declspec(dllexport) BOOL x2w_d3d12_budget_snapshot(x2w_budget::Snapshot* out, SIZE_T bytes) { if (!out || bytes != sizeof(*out) || !x2w_budget::enabled()) return FALSE; diff --git a/src/startup_stubs/gpu_to_cpu_counters.h b/src/startup_stubs/gpu_to_cpu_counters.h new file mode 100644 index 000000000..7d3f820d4 --- /dev/null +++ b/src/startup_stubs/gpu_to_cpu_counters.h @@ -0,0 +1,46 @@ +#pragma once + +// GPU-to-CPU census snapshot (#3676 item f). Counts only: a Map on a READBACK heap, forwarded +// copies and resolves whose destination has title memory, all refused copy or resolve calls whose +// destination has title memory, and the subset of those refusals where ResolveQueryData rejects +// title memory specifically. No D3D12 types, so a host without the Windows SDK can compile the +// line and the heap-type check. + +#include +#include + +namespace x2w_startup { + +// D3D12_HEAP_TYPE. d3d12_xs_device.cpp static_asserts these against the SDK enum. +constexpr int gpu_to_cpu_heap_default = 1; +constexpr int gpu_to_cpu_heap_upload = 2; +constexpr int gpu_to_cpu_heap_readback = 3; + +struct GpuToCpuCounts { + long long map_readback = 0; + long long copy_dest_title = 0; + long long resolve_dest_title = 0; + long long dest_title_refusals = 0; + long long dest_title_query_refusals = 0; +}; + +inline bool gpu_to_cpu_map_is_readback(int heap_type) +{ + return heap_type == gpu_to_cpu_heap_readback; +} + +// dest_title_refusals counts every refused copy or resolve whose destination has title memory. +// dest_title_query_refusals is the subset refused because ResolveQueryData cannot write title memory. +inline void gpu_to_cpu_line(const GpuToCpuCounts& counts, char* out, std::size_t size) +{ + if (!out || size == 0) return; + const int wrote = std::snprintf( + out, size, + "x2w-d3d12 gpu-to-cpu: map_readback %lld, copy_dest_title %lld, resolve_dest_title %lld, " + "dest_title_refusals %lld, dest_title_query_refusals %lld", + counts.map_readback, counts.copy_dest_title, counts.resolve_dest_title, counts.dest_title_refusals, + counts.dest_title_query_refusals); + if (wrote < 0) out[0] = 0; +} + +} // namespace x2w_startup diff --git a/src/startup_stubs/list_copy.inl b/src/startup_stubs/list_copy.inl index 6d3ac77b6..d3d84846e 100644 --- a/src/startup_stubs/list_copy.inl +++ b/src/startup_stubs/list_copy.inl @@ -248,7 +248,10 @@ void __stdcall list_copy_buffer_region(Wrapper* self, u64 destination, u64 desti if (refusal == x2w_startup::CopyRefusal::none && !authoritative && !sync_title_source(self, from) && !title_copy_snapshot(self, from, source_offset, bytes, &snapshot, &snapshot_offset)) refusal = x2w_startup::CopyRefusal::title_backed_source; - if (refusal != x2w_startup::CopyRefusal::none) return refuse_copy("CopyBufferRegion", refusal); + if (refusal != x2w_startup::CopyRefusal::none) { + note_gpu_to_cpu_dest_refusal(to.wrapper); + return refuse_copy("CopyBufferRegion", refusal); + } if (snapshot) { static LONG snapshot_copies = 0; if (InterlockedIncrement(&snapshot_copies) <= 10) @@ -285,6 +288,7 @@ void __stdcall list_copy_buffer_region(Wrapper* self, u64 destination, u64 desti note_copy_gpu_range(self, to.resource, destination_offset, bytes, authoritative && info && info->placement == Placement::copied_buffer && info->placement_device == self->own().list_device); } + note_gpu_to_cpu_copy_dest(to.wrapper); InterlockedIncrement(&list_buffer_copies); } @@ -300,7 +304,10 @@ void __stdcall list_copy_resource(Wrapper* self, u64 destination, u64 source) auto refusal = x2w_startup::check_copy_resource(to.resource ? &to_desc : nullptr, from.resource ? &from_desc : nullptr, to.resource == from.resource); if (refusal == x2w_startup::CopyRefusal::none) refusal = check_roles_and_sync(self, to, from, true); - if (refusal != x2w_startup::CopyRefusal::none) return refuse_copy("CopyResource", refusal); + if (refusal != x2w_startup::CopyRefusal::none) { + note_gpu_to_cpu_dest_refusal(to.wrapper); + return refuse_copy("CopyResource", refusal); + } require_copy_state(self, from.resource, D3D12_RESOURCE_STATE_COPY_SOURCE); require_copy_state(self, to.resource, D3D12_RESOURCE_STATE_COPY_DEST); static_cast(self->own().real)->CopyResource(to.resource, from.resource); @@ -313,6 +320,7 @@ void __stdcall list_copy_resource(Wrapper* self, u64 destination, u64 source) note_copy_gpu_range(self, to.resource, 0, to_desc.Width, false); note_linear_gpu_write(self, to); } else mark_gpu_written(self, to); + note_gpu_to_cpu_copy_dest(to.wrapper); InterlockedIncrement(&list_resource_copies); } @@ -333,13 +341,17 @@ void __stdcall list_resolve_subresource(Wrapper* self, u64 destination, UINT des desktop_format(static_cast(format)), to.resource == from.resource); auto refusal = plan.refusal; if (refusal == x2w_startup::CopyRefusal::none) refusal = check_roles_and_sync(self, to, from, false); - if (refusal != x2w_startup::CopyRefusal::none) return refuse_copy("ResolveSubresource", refusal); + if (refusal != x2w_startup::CopyRefusal::none) { + note_gpu_to_cpu_dest_refusal(to.wrapper); + return refuse_copy("ResolveSubresource", refusal); + } require_copy_state(self, from.resource, D3D12_RESOURCE_STATE_RESOLVE_SOURCE); require_copy_state(self, to.resource, D3D12_RESOURCE_STATE_RESOLVE_DEST); static_cast(self->own().real) ->ResolveSubresource(to.resource, destination_subresource, from.resource, source_subresource, plan.format); retire_hold(self, to.wrapper); // the source is multisampled, so never a member of the retire class mark_gpu_written(self, to); + note_gpu_to_cpu_resolve_dest(to.wrapper); InterlockedIncrement(&list_resolves); } diff --git a/src/startup_stubs/list_copy_texture.inl b/src/startup_stubs/list_copy_texture.inl index 1583b81b5..78af08683 100644 --- a/src/startup_stubs/list_copy_texture.inl +++ b/src/startup_stubs/list_copy_texture.inl @@ -103,8 +103,11 @@ void __stdcall list_copy_texture(Wrapper* self, const D3D12_TEXTURE_COPY_LOCATIO const D3D12_TEXTURE_COPY_LOCATION* src, const D3D12_BOX* box) { if (!self->own().list_open) return; + Wrapper* const dst_wrapper = + dst && x2w_local::readable(dst, sizeof(*dst)) ? as_wrapper(dst->pResource) : nullptr; static LONG logged; const auto refuse = [&](const char* why) { + note_gpu_to_cpu_dest_refusal(dst_wrapper); InterlockedIncrement(&list_copy_texture_refused); if (first_calls(logged, 6)) log("x2w-d3d12 CopyTextureRegion refused: %s", why); }; @@ -139,7 +142,6 @@ void __stdcall list_copy_texture(Wrapper* self, const D3D12_TEXTURE_COPY_LOCATIO 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); @@ -171,6 +173,7 @@ void __stdcall list_copy_texture(Wrapper* self, const D3D12_TEXTURE_COPY_LOCATIO if (side_dst.kind == x2w_list_copy_texture::Kind::subresource) mark_gpu_written(self, {real_dst, dst_wrapper}); retire_hold(self, dst_wrapper); retire_hold(self, src_wrapper); + note_gpu_to_cpu_copy_dest(dst_wrapper); InterlockedIncrement(&list_copy_texture_forwarded); } diff --git a/tests/cmake/startup_gpu_to_cpu.cmake b/tests/cmake/startup_gpu_to_cpu.cmake new file mode 100644 index 000000000..0f2d3c28d --- /dev/null +++ b/tests/cmake/startup_gpu_to_cpu.cmake @@ -0,0 +1,30 @@ +# GPU-to-CPU census (#3676 item f): Map on a READBACK heap, forwarded copies and resolves whose +# destination has title memory, and ResolveQueryData refusals of that destination. Counters only. +add_executable(startup_gpu_to_cpu_line_check tests/startup_gpu_to_cpu_line_check.cpp) +target_include_directories(startup_gpu_to_cpu_line_check PRIVATE src) +target_compile_features(startup_gpu_to_cpu_line_check PRIVATE cxx_std_17) +add_test(NAME startup_gpu_to_cpu_line COMMAND startup_gpu_to_cpu_line_check) +add_test(NAME startup_gpu_to_cpu_line_misattribute_control COMMAND startup_gpu_to_cpu_line_check --mutate readback) +set_tests_properties(startup_gpu_to_cpu_line_misattribute_control PROPERTIES + PASS_REGULAR_EXPRESSION "FAIL: misattribute") + +if(WIN32) + add_executable(startup_gpu_to_cpu_check tests/startup_gpu_to_cpu_check.cpp) + target_include_directories(startup_gpu_to_cpu_check PRIVATE src) + target_compile_features(startup_gpu_to_cpu_check PRIVATE cxx_std_17) + target_link_libraries(startup_gpu_to_cpu_check PRIVATE d3d12_xs_shim d3d12 dxgi) + foreach(backend IN ITEMS warp hardware) + foreach(mode IN ITEMS map-readback map-other copy-dest resolve-refusal) + set(case startup_gpu_to_cpu_${mode}_${backend}) + string(REPLACE "-" "_" case "${case}") + add_test(NAME ${case} COMMAND startup_gpu_to_cpu_check ${backend} ${mode}) + set_tests_properties(${case} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 + ENVIRONMENT "X2W_D3D12_PERMISSIVE=1;X2W_D3D12_LIST_NOOP=1;X2W_D3D12_RENDER=1;X2W_D3D12_QUERY_SLOTS=1") + endforeach() + set(control startup_gpu_to_cpu_misattribute_${backend}_control) + add_test(NAME ${control} COMMAND startup_gpu_to_cpu_check ${backend} misattribute) + set_tests_properties(${control} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 + PASS_REGULAR_EXPRESSION "FAIL: misattribute map_readback" + ENVIRONMENT "X2W_D3D12_PERMISSIVE=1;X2W_D3D12_LIST_NOOP=1;X2W_D3D12_RENDER=1;X2W_D3D12_QUERY_SLOTS=1") + endforeach() +endif() diff --git a/tests/standin_exports_check.py b/tests/standin_exports_check.py index 2d8c73394..6a017d40d 100644 --- a/tests/standin_exports_check.py +++ b/tests/standin_exports_check.py @@ -116,7 +116,7 @@ def test_real_catalogs(): expected = set(modules[:8]) | {"xgameruntime.dll"} check(set(catalogs) == expected, f"load_catalogs keys {sorted(catalogs)} != {sorted(expected)}") - counts = {"xmem": 54, "d3d12_xs": 79, "acphal": 10, "chad": 5, + counts = {"xmem": 54, "d3d12_xs": 80, "acphal": 10, "chad": 5, "xapu": 6, "xcrdapi": 3, "xg_xs": 2, "gameinput": 2} for stem, path in DEFAULT_DEFS.items(): catalog = catalogs[stem + ".dll"] @@ -155,7 +155,7 @@ def test_real_catalogs(): == Counter({"trap": 9, "implemented": 1}), "acphal status counts differ") check(Counter(e.status for e in catalogs["d3d12_xs.dll"].entries.values()) - == Counter({"implemented": 35, "unmeasured": 44}), + == Counter({"implemented": 35, "unmeasured": 45}), "d3d12_xs status counts differ") check(Counter(e.status for e in catalogs["xgameruntime.dll"].entries.values()) == Counter({"implemented": 38}), @@ -234,6 +234,8 @@ def test_real_catalogs(): "src/startup_stubs/d3d12_compute_dispatch.inl", "x2w_d3d12_compute_census"), "d3d12_xs direct export source is the included file") + check(d3d12.lookup("x2w_d3d12_gpu_to_cpu_snapshot").status == "unmeasured", + "d3d12_xs GPU-to-CPU census export") check(d3d12.lookup("NoSuchExport").status == "absent", "d3d12_xs absent name") check(d3d12.lookup(999).status == "unmeasured", diff --git a/tests/startup_gpu_to_cpu_check.cpp b/tests/startup_gpu_to_cpu_check.cpp new file mode 100644 index 000000000..6f8d8b8ff --- /dev/null +++ b/tests/startup_gpu_to_cpu_check.cpp @@ -0,0 +1,547 @@ +// GPU-to-CPU census (#3676 item f). Logging and counters only. +// +// startup_gpu_to_cpu_check +// +// The stand-in is d3d12_xs.dll through D3D12XboxCreateDevice (X2W_D3D12_PERMISSIVE=1, +// X2W_D3D12_LIST_NOOP=1, X2W_D3D12_RENDER=1, X2W_D3D12_QUERY_SLOTS=1). Counts come from +// x2w_d3d12_gpu_to_cpu_snapshot. Nothing here changes Map's pointer, copy forwarding or a refusal. +// +// map-readback Map a slot-27 READBACK resource with no title memory, then a slot-76 READBACK +// resource whose title memory is returned without a desktop Map. map_readback +// moves for both. +// map-other Map a slot-27 DEFAULT resource and a slot-27 UPLOAD resource. map_readback stays. +// copy-dest CopyBufferRegion into a slot-76 buffer that carries title memory stays forwarded +// (x2w_d3d12_list_copy_stats) and copy_dest_title moves. A zero-byte copy into that +// buffer increments dest_title_refusals. Refused CopyResource, CopyTextureRegion and +// ResolveSubresource calls into title-memory destinations do too; their valid calls +// still forward and count on the forwarded fields. A copy into a slot-27 buffer with +// no title memory does not count. After the queue fence the buffer's title bytes are +// unchanged. +// resolve-refusal With the query slots on, both an unended ResolveQueryData and the title-memory-specific +// rejection increment dest_title_refusals; only the latter increments +// dest_title_query_refusals. Both leave the destination bytes unchanged. +// misattribute Requires the DEFAULT Map to have been counted as readback. The census does not, so +// this mode fails. +// +// Exit codes: 0 pass, 1 a check failed, 2 usage, 3 setup failed, 77 no physical adapter. +#include "startup_stubs/gpu_to_cpu_counters.h" + +#include +#include + +#include +#include +#include +#include +#include + +extern "C" __declspec(dllimport) HRESULT WINAPI D3D12XboxCreateDevice(IUnknown*, const void*, REFIID, void**); +extern "C" __declspec(dllimport) BOOL x2w_d3d12_gpu_to_cpu_snapshot(x2w_startup::GpuToCpuCounts*, SIZE_T); +extern "C" __declspec(dllimport) void x2w_d3d12_list_copy_stats(unsigned*, unsigned*, unsigned*, unsigned*); +extern "C" __declspec(dllimport) void x2w_d3d12_list_copy_texture_stats(LONG*, LONG*); + +// Slot 76 gives a committed resource title memory only through this export. It stays off until a case +// asks for title memory, so slot 76 resources created before that have none. +static bool hook_on = false; +static void* hooked = nullptr; +static void* __stdcall fixture_xmem_alloc(SIZE_T size, ULONGLONG) +{ + if (!hook_on || !size) return nullptr; + hooked = VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + return hooked; +} +extern "C" __declspec(dllexport) void* __XMemAllocationHooks__[2] = {reinterpret_cast(&fixture_xmem_alloc), nullptr}; + +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 unsigned char title_fill = 0xA5; +constexpr unsigned char upload_fill = 0x5A; + +struct Counts { + long long map_readback = 0, copy_dest_title = 0, resolve_dest_title = 0, dest_title_refusals = 0, + dest_title_query_refusals = 0; +}; +struct CopyCounts { + unsigned buffers = 0, resources = 0, resolves = 0, refused = 0; + LONG texture_forwarded = 0, texture_refused = 0; +}; + +Counts census() +{ + x2w_startup::GpuToCpuCounts raw{}; + if (!x2w_d3d12_gpu_to_cpu_snapshot(&raw, sizeof(raw))) return {}; + return {raw.map_readback, raw.copy_dest_title, raw.resolve_dest_title, raw.dest_title_refusals, + raw.dest_title_query_refusals}; +} +CopyCounts copies() +{ + CopyCounts c; + x2w_d3d12_list_copy_stats(&c.buffers, &c.resources, &c.resolves, &c.refused); + x2w_d3d12_list_copy_texture_stats(&c.texture_forwarded, &c.texture_refused); + return c; +} +bool census_ok() +{ + x2w_startup::GpuToCpuCounts raw{}; + return x2w_d3d12_gpu_to_cpu_snapshot(&raw, sizeof(raw)) == TRUE && + x2w_d3d12_gpu_to_cpu_snapshot(&raw, sizeof(raw) - 1) == FALSE; +} + +D3D12_RESOURCE_DESC buffer_desc(UINT64 width) +{ + return {D3D12_RESOURCE_DIMENSION_BUFFER, 0, width, 1, 1, 1, DXGI_FORMAT_UNKNOWN, {1, 0}, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, + D3D12_RESOURCE_FLAG_NONE}; +} +D3D12_RESOURCE_DESC texture_desc(UINT samples, D3D12_RESOURCE_FLAGS flags) +{ + return {D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, 4, 4, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM, {samples, 0}, + D3D12_TEXTURE_LAYOUT_UNKNOWN, flags}; +} + +// Device slot 27: committed resource, no title memory. +void* create27(void* device, D3D12_HEAP_TYPE type, const D3D12_RESOURCE_DESC& desc, UINT state) +{ + const D3D12_HEAP_PROPERTIES heap{type}; + void* resource = nullptr; + const Word hr = invoke(device, 27, arg(&heap), D3D12_HEAP_FLAG_NONE, arg(&desc), state, 0, arg(&__uuidof(ID3D12Resource)), + arg(&resource)); + if (!ok(hr)) + std::printf("slot 27 hr=0x%08lx heap=%d\n", static_cast(hr), static_cast(type)); + return ok(hr) ? resource : nullptr; +} +// Device slot 76. Title memory is whatever the hook returned for this call (null while hook_on is false). +void* create76(void* device, D3D12_HEAP_TYPE type, const D3D12_RESOURCE_DESC& desc, UINT state, const D3D12_CLEAR_VALUE* clear, + void** title) +{ + hooked = nullptr; + const D3D12_HEAP_PROPERTIES heap{type}; + void* resource = nullptr; + const Word hr = invoke(device, 76, arg(&heap), D3D12_HEAP_FLAG_NONE, arg(&desc), state, arg(clear), + arg(&__uuidof(ID3D12Resource)), arg(&resource)); + if (title) *title = hooked; + if (!ok(hr)) + std::printf("slot 76 hr=0x%08lx heap=%d\n", static_cast(hr), static_cast(type)); + return ok(hr) ? resource : nullptr; +} + +Word map_resource(void* resource, void** mapped) +{ + *mapped = nullptr; + return invoke(resource, 8, 0, 0, arg(mapped)); +} + +bool line_is(const Counts& c, const char* expect) +{ + x2w_startup::GpuToCpuCounts raw{c.map_readback, c.copy_dest_title, c.resolve_dest_title, c.dest_title_refusals, + c.dest_title_query_refusals}; + char line[320]; + x2w_startup::gpu_to_cpu_line(raw, line, sizeof(line)); + if (std::strcmp(line, expect) == 0) { + std::puts(line); + return true; + } + std::printf("FAIL: line '%s'\n", line); + return false; +} + +struct Recording { + void* queue = nullptr; + void* fence = nullptr; + void* allocator = nullptr; + void* list = nullptr; + Word fence_value = 0; + + bool open(void* device) + { + D3D12_COMMAND_QUEUE_DESC queue_desc{}; + const unsigned list_desc[] = {D3D12_COMMAND_LIST_TYPE_DIRECT, 2, 1, 0}; + 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))) || + !ok(invoke(list, 139, arg(allocator)))) + return false; + return list != nullptr; + } + bool submit(void* device) + { + if (!ok(invoke(list, 9))) return false; + void* lists[] = {list}; + invoke(queue, 10, 1, arg(lists)); + if (!ok(invoke(queue, 14, arg(fence), ++fence_value))) return false; + const auto deadline = GetTickCount64() + 10000; + while (invoke(fence, 8) < fence_value) { + if (GetTickCount64() >= deadline) return false; + Sleep(1); + } + return ok(invoke(device, 37)); + } +}; + +int fail_counts(const char* step, const Counts& got, const Counts& want, const CopyCounts* copied = nullptr, + const CopyCounts* copy_want = nullptr) +{ + std::printf("FAIL: %s map_readback %lld (%lld), copy_dest_title %lld (%lld), resolve_dest_title %lld (%lld), " + "dest_title_refusals %lld (%lld), dest_title_query_refusals %lld (%lld)\n", + step, got.map_readback, want.map_readback, got.copy_dest_title, want.copy_dest_title, got.resolve_dest_title, + want.resolve_dest_title, got.dest_title_refusals, want.dest_title_refusals, + got.dest_title_query_refusals, want.dest_title_query_refusals); + if (copied && copy_want) + std::printf("FAIL: %s copies buffers %u (%u), resources %u (%u), resolves %u (%u), refused %u (%u), " + "texture forwarded %ld (%ld), texture refused %ld (%ld)\n", + step, copied->buffers, copy_want->buffers, copied->resources, copy_want->resources, copied->resolves, + copy_want->resolves, copied->refused, copy_want->refused, copied->texture_forwarded, + copy_want->texture_forwarded, copied->texture_refused, copy_want->texture_refused); + return 1; +} +} // namespace + +int main(int argc, char** argv) +{ + if (argc != 3) { + std::puts("usage: startup_gpu_to_cpu_check " + ""); + return 2; + } + const bool warp = std::strcmp(argv[1], "warp") == 0; + if (!warp && std::strcmp(argv[1], "hardware") != 0) return 2; + const char* mode = argv[2]; + const bool map_readback = std::strcmp(mode, "map-readback") == 0; + const bool map_other = std::strcmp(mode, "map-other") == 0; + const bool copy_dest = std::strcmp(mode, "copy-dest") == 0; + const bool resolve_refusal = std::strcmp(mode, "resolve-refusal") == 0; + const bool misattribute = std::strcmp(mode, "misattribute") == 0; + if (!map_readback && !map_other && !copy_dest && !resolve_refusal && !misattribute) return 2; + + SetEnvironmentVariableA("X2W_D3D12_PERMISSIVE", "1"); + SetEnvironmentVariableA("X2W_D3D12_LIST_NOOP", "1"); + SetEnvironmentVariableA("X2W_D3D12_RENDER", "1"); + SetEnvironmentVariableA("X2W_D3D12_QUERY_SLOTS", "1"); + + IDXGIFactory6* factory = nullptr; + if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) return 3; + IDXGIAdapter* adapter = nullptr; + if (warp) { + if (FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&adapter)))) return 3; + } else { + for (UINT n = 0;; ++n) { + IDXGIAdapter1* candidate = nullptr; + 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{}; + const bool usable = SUCCEEDED(candidate->GetDesc1(&desc)) && !(desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) && + SUCCEEDED(D3D12CreateDevice(candidate, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), nullptr)); + if (usable) { + adapter = candidate; + break; + } + candidate->Release(); + } + } + 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, parameters.data(), __uuidof(ID3D12Device), &device)) || !census_ok()) return 3; + + const D3D12_RESOURCE_DESC bytes = buffer_desc(256); + if (map_readback || map_other || misattribute) { + if (map_readback) { + void* readback = create27(device, D3D12_HEAP_TYPE_READBACK, bytes, D3D12_RESOURCE_STATE_COPY_DEST); + if (!readback) return 3; + void* mapped = nullptr; + if (!ok(map_resource(readback, &mapped)) || !mapped) { + std::puts("FAIL: slot 27 readback Map"); + return 1; + } + Counts got = census(); + if (got.map_readback != 1 || got.copy_dest_title || got.resolve_dest_title || got.dest_title_refusals) + return fail_counts("slot 27 readback", got, {1, 0, 0, 0}); + hook_on = true; + void* title = nullptr; + void* titled = create76(device, D3D12_HEAP_TYPE_READBACK, bytes, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, &title); + hook_on = false; + if (!titled || !title) return 3; + std::memset(title, 0x11, 16); + mapped = nullptr; + if (!ok(map_resource(titled, &mapped)) || mapped != title || static_cast(title)[0] != 0x11) { + std::puts("FAIL: title-memory readback Map did not return title memory"); + return 1; + } + got = census(); + if (got.map_readback != 2 || got.copy_dest_title || got.resolve_dest_title || got.dest_title_refusals) + return fail_counts("title-memory readback", got, {2, 0, 0, 0}); + if (!line_is(got, "x2w-d3d12 gpu-to-cpu: map_readback 2, copy_dest_title 0, resolve_dest_title 0, " + "dest_title_refusals 0, dest_title_query_refusals 0")) + return 1; + } + if (map_other || misattribute) { + void* plain = create27(device, D3D12_HEAP_TYPE_DEFAULT, bytes, D3D12_RESOURCE_STATE_COMMON); + void* upload = create27(device, D3D12_HEAP_TYPE_UPLOAD, bytes, D3D12_RESOURCE_STATE_GENERIC_READ); + if (!plain || !upload) return 3; + const Counts before = census(); + void* mapped = nullptr; + map_resource(plain, &mapped); + const Counts after_default = census(); + if (!ok(map_resource(upload, &mapped)) || !mapped) { + std::puts("FAIL: upload Map"); + return 1; + } + const Counts after = census(); + if (misattribute) { + if (after_default.map_readback != before.map_readback) { + std::printf("BUG: default map counted as readback (%lld)\n", after_default.map_readback); + return 1; + } + std::printf("FAIL: misattribute map_readback stayed %lld\n", after_default.map_readback); + return 1; + } + if (after.map_readback != before.map_readback || after.copy_dest_title != before.copy_dest_title || + after.resolve_dest_title != before.resolve_dest_title || after.dest_title_refusals != before.dest_title_refusals) + return fail_counts("default and upload", after, before); + if (!line_is(after, "x2w-d3d12 gpu-to-cpu: map_readback 0, copy_dest_title 0, resolve_dest_title 0, " + "dest_title_refusals 0, dest_title_query_refusals 0")) + return 1; + } + std::puts("PASS"); + return 0; + } + + Recording recording; + if (!recording.open(device)) { + std::puts("FAIL: recording was not opened"); + return 3; + } + + if (copy_dest) { + void* upload = nullptr; + void* upload_memory = nullptr; + hook_on = false; + upload = create76(device, D3D12_HEAP_TYPE_UPLOAD, bytes, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, &upload_memory); + if (!upload || upload_memory) return 3; + if (!ok(map_resource(upload, &upload_memory)) || !upload_memory) return 3; + std::memset(upload_memory, upload_fill, 256); + invoke(upload, 9, 0, 0); + void* plain = create27(device, D3D12_HEAP_TYPE_DEFAULT, bytes, D3D12_RESOURCE_STATE_COMMON); + hook_on = true; + void* title = nullptr; + void* titled = create76(device, D3D12_HEAP_TYPE_DEFAULT, bytes, D3D12_RESOURCE_STATE_COMMON, nullptr, &title); + hook_on = false; + if (!plain || !titled || !title) return 3; + std::memset(title, title_fill, 256); + + Counts before = census(); + CopyCounts copy_before = copies(); + invoke(recording.list, 15, arg(titled), 0, arg(upload), 0, 0); + Counts got = census(); + CopyCounts copied = copies(); + CopyCounts want_copy = copy_before; + want_copy.refused += 1; + Counts want_census = before; + want_census.dest_title_refusals += 1; + if (got.copy_dest_title != want_census.copy_dest_title || + got.dest_title_refusals != want_census.dest_title_refusals || + got.dest_title_query_refusals != want_census.dest_title_query_refusals || + copied.refused != want_copy.refused || copied.buffers != copy_before.buffers) + return fail_counts("zero-byte copy into title memory", got, want_census, &copied, &want_copy); + + invoke(recording.list, 15, arg(plain), 0, arg(upload), 0, 64); + got = census(); + copied = copies(); + want_copy.buffers += 1; + if (got.copy_dest_title != want_census.copy_dest_title || + got.dest_title_refusals != want_census.dest_title_refusals || copied.buffers != want_copy.buffers) + return fail_counts("copy into a buffer with no title memory", got, want_census, &copied, &want_copy); + + invoke(recording.list, 15, arg(titled), 0, arg(upload), 0, 64); + got = census(); + copied = copies(); + want_copy.buffers += 1; + const Counts want_buffer{before.map_readback, before.copy_dest_title + 1, before.resolve_dest_title, + before.dest_title_refusals + 1, before.dest_title_query_refusals}; + if (got.copy_dest_title != want_buffer.copy_dest_title || got.dest_title_refusals != want_buffer.dest_title_refusals || + copied.buffers != want_copy.buffers || copied.refused != want_copy.refused) + return fail_counts("CopyBufferRegion into title memory", got, want_buffer, &copied, &want_copy); + + const D3D12_RESOURCE_DESC tex = texture_desc(1, D3D12_RESOURCE_FLAG_NONE); + void* tex_src = create76(device, D3D12_HEAP_TYPE_DEFAULT, tex, D3D12_RESOURCE_STATE_COMMON, nullptr, nullptr); + hook_on = true; + void* tex_title = nullptr; + void* tex_dst = create76(device, D3D12_HEAP_TYPE_DEFAULT, tex, D3D12_RESOURCE_STATE_COMMON, nullptr, &tex_title); + hook_on = false; + void* region_src = create76(device, D3D12_HEAP_TYPE_DEFAULT, tex, D3D12_RESOURCE_STATE_COMMON, nullptr, nullptr); + hook_on = true; + void* region_title = nullptr; + void* region_dst = create76(device, D3D12_HEAP_TYPE_DEFAULT, tex, D3D12_RESOURCE_STATE_COMMON, nullptr, ®ion_title); + hook_on = false; + const D3D12_CLEAR_VALUE clear{DXGI_FORMAT_R8G8B8A8_UNORM, {0, 0, 0, 1}}; + void* msaa = create76(device, D3D12_HEAP_TYPE_DEFAULT, texture_desc(4, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET), + D3D12_RESOURCE_STATE_COMMON, &clear, nullptr); + hook_on = true; + void* resolve_title = nullptr; + void* resolved = create76(device, D3D12_HEAP_TYPE_DEFAULT, tex, D3D12_RESOURCE_STATE_COMMON, nullptr, &resolve_title); + hook_on = false; + if (!tex_src || !tex_dst || !tex_title || !region_src || !region_dst || !region_title || !msaa || !resolved || + !resolve_title) { + std::puts("FAIL: a texture or the multisample source was not created"); + return 3; + } + + invoke(recording.list, 17, arg(tex_dst), arg(upload)); + got = census(); + copied = copies(); + want_copy.refused += 1; + want_census = want_buffer; + want_census.dest_title_refusals += 1; + if (got.dest_title_refusals != want_census.dest_title_refusals || + got.dest_title_query_refusals != want_census.dest_title_query_refusals || + copied.resources != want_copy.resources || copied.refused != want_copy.refused) + return fail_counts("refused CopyResource into title memory", got, want_census, &copied, &want_copy); + + invoke(recording.list, 17, arg(tex_dst), arg(tex_src)); + got = census(); + copied = copies(); + want_copy.resources += 1; + const Counts want_resource{before.map_readback, before.copy_dest_title + 2, before.resolve_dest_title, + before.dest_title_refusals + 2, before.dest_title_query_refusals}; + if (got.copy_dest_title != want_resource.copy_dest_title || copied.resources != want_copy.resources) + return fail_counts("CopyResource into title memory", got, want_resource, &copied, &want_copy); + + D3D12_TEXTURE_COPY_LOCATION to{static_cast(region_dst), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; + D3D12_TEXTURE_COPY_LOCATION from{static_cast(region_src), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; + D3D12_TEXTURE_COPY_LOCATION same{static_cast(region_dst), + D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; + invoke(recording.list, 16, arg(&to), 0, 0, 0, arg(&same), 0); + got = census(); + copied = copies(); + want_copy.texture_refused += 1; + want_census = want_resource; + want_census.dest_title_refusals += 1; + if (got.dest_title_refusals != want_census.dest_title_refusals || + got.dest_title_query_refusals != want_census.dest_title_query_refusals || + copied.texture_forwarded != want_copy.texture_forwarded || + copied.texture_refused != want_copy.texture_refused) + return fail_counts("refused CopyTextureRegion into title memory", got, want_census, &copied, &want_copy); + + invoke(recording.list, 16, arg(&to), 0, 0, 0, arg(&from), 0); + got = census(); + copied = copies(); + want_copy.texture_forwarded += 1; + const Counts want_region{before.map_readback, before.copy_dest_title + 3, before.resolve_dest_title, + before.dest_title_refusals + 3, before.dest_title_query_refusals}; + if (got.copy_dest_title != want_region.copy_dest_title || copied.texture_forwarded != want_copy.texture_forwarded) + return fail_counts("CopyTextureRegion into title memory", got, want_region, &copied, &want_copy); + + invoke(recording.list, 19, arg(resolved), 0, arg(tex_src), 0, DXGI_FORMAT_R8G8B8A8_UNORM); + got = census(); + copied = copies(); + want_copy.refused += 1; + want_census = want_region; + want_census.dest_title_refusals += 1; + if (got.dest_title_refusals != want_census.dest_title_refusals || + got.dest_title_query_refusals != want_census.dest_title_query_refusals || + copied.resolves != want_copy.resolves || copied.refused != want_copy.refused) + return fail_counts("refused ResolveSubresource into title memory", got, want_census, &copied, &want_copy); + + invoke(recording.list, 19, arg(resolved), 0, arg(msaa), 0, DXGI_FORMAT_R8G8B8A8_UNORM); + got = census(); + copied = copies(); + want_copy.resolves += 1; + const Counts want_resolve{before.map_readback, before.copy_dest_title + 3, before.resolve_dest_title + 1, + before.dest_title_refusals + 4, before.dest_title_query_refusals}; + if (got.resolve_dest_title != want_resolve.resolve_dest_title || got.copy_dest_title != want_resolve.copy_dest_title || + copied.resolves != want_copy.resolves || got.map_readback != before.map_readback) + return fail_counts("ResolveSubresource into title memory", got, want_resolve, &copied, &want_copy); + + if (!recording.submit(device)) { + std::puts("FAIL: copy submission"); + return 1; + } + void* mapped = nullptr; + if (!ok(map_resource(titled, &mapped)) || mapped != title || + std::memcmp(title, std::vector(256, title_fill).data(), 256) != 0) { + std::puts("FAIL: title memory changed after the forwarded copy"); + return 1; + } + got = census(); + if (got.map_readback != want_resolve.map_readback || got.copy_dest_title != want_resolve.copy_dest_title || + got.resolve_dest_title != want_resolve.resolve_dest_title || got.dest_title_refusals != want_resolve.dest_title_refusals) + return fail_counts("map of the copy destination", got, want_resolve); + if (!line_is(got, "x2w-d3d12 gpu-to-cpu: map_readback 0, copy_dest_title 3, resolve_dest_title 1, " + "dest_title_refusals 4, dest_title_query_refusals 0")) + return 1; + std::puts("PASS"); + return 0; + } + + hook_on = true; + void* title = nullptr; + void* titled = create76(device, D3D12_HEAP_TYPE_DEFAULT, bytes, D3D12_RESOURCE_STATE_COMMON, nullptr, &title); + hook_on = false; + if (!titled || !title) return 3; + std::memset(title, title_fill, 256); + const D3D12_QUERY_HEAP_DESC heap_desc{D3D12_QUERY_HEAP_TYPE_OCCLUSION, 1, 0}; + void* heap = nullptr; + if (!ok(invoke(device, 39, arg(&heap_desc), arg(&__uuidof(ID3D12QueryHeap)), arg(&heap))) || !heap) return 3; + const Counts before = census(); + const CopyCounts copy_before = copies(); + invoke(recording.list, 30, arg(heap), D3D12_QUERY_TYPE_OCCLUSION, 0, 1, arg(titled), 0); + const Counts before_end = census(); + const Counts want_before_end{before.map_readback, before.copy_dest_title, before.resolve_dest_title, + before.dest_title_refusals + 1, before.dest_title_query_refusals}; + if (before_end.dest_title_refusals != want_before_end.dest_title_refusals || + before_end.dest_title_query_refusals != want_before_end.dest_title_query_refusals) + return fail_counts("ResolveQueryData without an ended query into title memory", before_end, want_before_end); + + invoke(recording.list, 28, arg(heap), D3D12_QUERY_TYPE_OCCLUSION, 0); + invoke(recording.list, 29, arg(heap), D3D12_QUERY_TYPE_OCCLUSION, 0); + invoke(recording.list, 30, arg(heap), D3D12_QUERY_TYPE_OCCLUSION, 0, 1, arg(titled), 0); + const Counts got = census(); + const CopyCounts copied = copies(); + const Counts want_refusal{before.map_readback, before.copy_dest_title, before.resolve_dest_title, + before.dest_title_refusals + 2, before.dest_title_query_refusals + 1}; + if (got.dest_title_refusals != want_refusal.dest_title_refusals || got.copy_dest_title != want_refusal.copy_dest_title || + got.resolve_dest_title != want_refusal.resolve_dest_title || + got.dest_title_query_refusals != want_refusal.dest_title_query_refusals || + copied.resolves != copy_before.resolves || copied.refused != copy_before.refused) + return fail_counts("ResolveQueryData into title memory", got, want_refusal, &copied, ©_before); + if (std::memcmp(title, std::vector(256, title_fill).data(), 256) != 0) { + std::puts("FAIL: destination bytes changed on the refused resolve"); + return 1; + } + if (!recording.submit(device)) { + std::puts("FAIL: resolve submission"); + return 1; + } + if (std::memcmp(title, std::vector(256, title_fill).data(), 256) != 0) { + std::puts("FAIL: destination bytes changed after submission"); + return 1; + } + void* mapped = nullptr; + if (!ok(map_resource(titled, &mapped)) || mapped != title) { + std::puts("FAIL: Map of the resolve destination did not return title memory"); + return 1; + } + const Counts after_map = census(); + if (after_map.map_readback != before.map_readback || after_map.dest_title_refusals != before.dest_title_refusals + 2 || + after_map.dest_title_query_refusals != before.dest_title_query_refusals + 1) + return fail_counts("map after the refusal", after_map, {before.map_readback, before.copy_dest_title, + before.resolve_dest_title, before.dest_title_refusals + 2, + before.dest_title_query_refusals + 1}); + if (!line_is(after_map, "x2w-d3d12 gpu-to-cpu: map_readback 0, copy_dest_title 0, resolve_dest_title 0, " + "dest_title_refusals 2, dest_title_query_refusals 1")) + return 1; + std::puts("PASS"); + return 0; +} diff --git a/tests/startup_gpu_to_cpu_line_check.cpp b/tests/startup_gpu_to_cpu_line_check.cpp new file mode 100644 index 000000000..fbb33a06f --- /dev/null +++ b/tests/startup_gpu_to_cpu_line_check.cpp @@ -0,0 +1,79 @@ +// Line and heap-type check for the GPU-to-CPU census. No D3D12 device. +// +// startup_gpu_to_cpu_line_check +// startup_gpu_to_cpu_line_check --mutate readback +// +// The mutated run treats a DEFAULT heap as READBACK. The real check does not, so that run fails. +#include "startup_stubs/gpu_to_cpu_counters.h" + +#include +#include + +namespace { +int failures = 0; + +void check(bool ok, const char* what) +{ + if (!ok) { + std::printf("FAIL: %s\n", what); + ++failures; + } +} +} // namespace + +int main(int argc, char** argv) +{ + const bool mutate = argc == 3 && std::strcmp(argv[1], "--mutate") == 0 && std::strcmp(argv[2], "readback") == 0; + if (argc != 1 && !mutate) { + std::puts("usage: startup_gpu_to_cpu_line_check [--mutate readback]"); + return 2; + } + + using x2w_startup::GpuToCpuCounts; + using x2w_startup::gpu_to_cpu_heap_default; + using x2w_startup::gpu_to_cpu_heap_readback; + using x2w_startup::gpu_to_cpu_heap_upload; + + const int heaps[] = {gpu_to_cpu_heap_default, gpu_to_cpu_heap_upload, gpu_to_cpu_heap_readback, 4, 0}; + for (int heap : heaps) { + const bool got = x2w_startup::gpu_to_cpu_map_is_readback(heap); + // The mutant counts every non-zero heap as readback, so DEFAULT is attributed to map_readback. + const bool expect = mutate ? heap != 0 : heap == gpu_to_cpu_heap_readback; + if (got != expect) { + std::printf("FAIL: misattribute heap %d readback=%d expected=%d\n", heap, got ? 1 : 0, expect ? 1 : 0); + ++failures; + } + } + + GpuToCpuCounts counts{}; + counts.map_readback = 2; + counts.copy_dest_title = 3; + counts.resolve_dest_title = 1; + counts.dest_title_refusals = 4; + counts.dest_title_query_refusals = 2; + char line[320]; + x2w_startup::gpu_to_cpu_line(counts, line, sizeof(line)); + const char* expect = + "x2w-d3d12 gpu-to-cpu: map_readback 2, copy_dest_title 3, resolve_dest_title 1, dest_title_refusals 4, " + "dest_title_query_refusals 2"; + check(std::strcmp(line, expect) == 0, "census line"); + if (std::strcmp(line, expect) != 0) std::printf("line: %s\n", line); + + GpuToCpuCounts zeros{}; + x2w_startup::gpu_to_cpu_line(zeros, line, sizeof(line)); + check(std::strcmp(line, "x2w-d3d12 gpu-to-cpu: map_readback 0, copy_dest_title 0, resolve_dest_title 0, " + "dest_title_refusals 0, dest_title_query_refusals 0") == 0, + "zero line"); + + char tiny[1] = {'X'}; + x2w_startup::gpu_to_cpu_line(counts, tiny, 1); + check(tiny[0] == 0, "truncated line stays terminated"); + x2w_startup::gpu_to_cpu_line(counts, nullptr, 0); + + if (failures) { + std::printf("FAIL: %d check(s)\n", failures); + return 1; + } + std::puts("PASS"); + return 0; +}