Skip to content
Open
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
85 changes: 76 additions & 9 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<int>(D3D12_HEAP_TYPE_DEFAULT) == x2w_startup::gpu_to_cpu_heap_default, "heap type values");
static_assert(static_cast<int>(D3D12_HEAP_TYPE_UPLOAD) == x2w_startup::gpu_to_cpu_heap_upload, "heap type values");
static_assert(static_cast<int>(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<int>(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;
Expand Down Expand Up @@ -4166,34 +4213,39 @@ 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;
for (UINT i = 0; i < count; ++i)
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<ID3D12Resource*>(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<D3D12_QUERY_TYPE>(type), start, count, resource, offset);
}
// Device slot 39, CreateQueryHeap(desc, riid, ppv): forwarded, and the desc kept for the query slots.
Expand Down Expand Up @@ -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<ID3D12Resource*>(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<ID3D12Resource*>(self->own().real));
if (data) *data = SUCCEEDED(hr) ? mapped : nullptr;
if (FAILED(hr)) check_device("Resource::Map", 8);
static LONG calls, failures;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
46 changes: 46 additions & 0 deletions src/startup_stubs/gpu_to_cpu_counters.h
Original file line number Diff line number Diff line change
@@ -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 <cstddef>
#include <cstdio>

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
18 changes: 15 additions & 3 deletions src/startup_stubs/list_copy.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand All @@ -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<ID3D12GraphicsCommandList*>(self->own().real)->CopyResource(to.resource, from.resource);
Expand All @@ -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);
}

Expand All @@ -333,13 +341,17 @@ void __stdcall list_resolve_subresource(Wrapper* self, u64 destination, UINT des
desktop_format(static_cast<DXGI_FORMAT>(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<ID3D12GraphicsCommandList*>(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);
}

Expand Down
5 changes: 4 additions & 1 deletion src/startup_stubs/list_copy_texture.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
30 changes: 30 additions & 0 deletions tests/cmake/startup_gpu_to_cpu.cmake
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 4 additions & 2 deletions tests/standin_exports_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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}),
Expand Down Expand Up @@ -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",
Expand Down
Loading
Loading