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
84 changes: 84 additions & 0 deletions src/startup_stubs/clear_census.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#pragma once

#include <array>
#include <cstdint>
#include <cstdio>
#include <mutex>

namespace x2w_startup {

enum class ClearKind { rtv, dsv, uav_uint, uav_float };

struct ClearKey {
ClearKind kind;
unsigned format, dimension, width_class, height_class, depth_class;
std::array<std::uint32_t, 4> bits{};

bool operator==(const ClearKey& other) const
{
return kind == other.kind && format == other.format && dimension == other.dimension &&
width_class == other.width_class && height_class == other.height_class &&
depth_class == other.depth_class && bits == other.bits;
}
};

// Classes round up to a power of two, represented by its exponent.
inline unsigned clear_size_class(std::uint64_t size)
{
unsigned result = 0;
for (auto n = size ? size - 1 : 0; n; n >>= 1) ++result;
return result;
}

class ClearCensus {
struct Entry {
ClearKey key;
long first_frame;
unsigned long long calls = 1, reported = 0;
};
std::mutex mutex_;
std::array<Entry, 128> entries_{};
unsigned used_ = 0;
unsigned long long total_ = 0, overflow_ = 0, reported_ = 0;

public:
void note(const ClearKey& key, long frame)
{
std::lock_guard<std::mutex> lock(mutex_);
++total_;
for (unsigned i = 0; i < used_; ++i) {
if (entries_[i].key == key) {
++entries_[i].calls;
return;
}
}
if (used_ == entries_.size()) ++overflow_;
else entries_[used_++] = {key, frame};
}

template <class Emit>
void report(Emit emit, bool only_if_changed)
{
std::lock_guard<std::mutex> lock(mutex_);
if (!total_ || (only_if_changed && total_ == reported_)) return;
if (only_if_changed) reported_ = total_;
char line[256];
std::snprintf(line, sizeof(line), "x2w-render clear census: total %llu; shapes %u (overflow calls %llu)",
total_, used_, overflow_);
emit(line);
constexpr const char* names[] = {"rtv", "dsv", "uav_uint", "uav_float"};
for (unsigned i = 0; i < used_; ++i) {
auto& entry = entries_[i];
if (only_if_changed && entry.calls == entry.reported) continue;
if (only_if_changed) entry.reported = entry.calls;
const auto& k = entry.key;
std::snprintf(line, sizeof(line), "x2w-render clear %s fmt=%u dim=%u size_log2=%ux%ux%u "
"value_bits=%08x,%08x,%08x,%08x calls=%llu first_seen_frame=%ld",
names[static_cast<unsigned>(k.kind)], k.format, k.dimension, k.width_class, k.height_class,
k.depth_class, k.bits[0], k.bits[1], k.bits[2], k.bits[3], entry.calls, entry.first_frame);
emit(line);
}
}
};

} // namespace x2w_startup
46 changes: 32 additions & 14 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2958,7 +2958,7 @@ bool fixed_vertex_view(Wrapper* self, const Pass& pass, const Translation& tr, b

// X2W_PIPELINE_COLOUR=1 (pipeline_colour.h), a debug switch read once. last_draw_refusal: why the last
// draw_translated on this thread drew nothing, and its pipeline key (0 when the draw stopped before a translation
// was found). Only written while the switch is on.
// was found). The prose is only written while the switch is on.
bool pipeline_colour_enabled()
{
static const bool enabled = x2w_local::env_on("X2W_PIPELINE_COLOUR");
Expand All @@ -2967,14 +2967,16 @@ bool pipeline_colour_enabled()

struct DrawRefusal {
u64 key = 0;
const char* code = nullptr;
char why[192] = "";
};
thread_local DrawRefusal last_draw_refusal;

void note_draw_refusal(u64 key, const char* why, const char* detail = nullptr)
void note_draw_refusal(u64 key, const char* code, const char* why, const char* detail = nullptr)
{
if (!pipeline_colour_enabled()) return;
last_draw_refusal.key = key;
last_draw_refusal.code = code;
if (!pipeline_colour_enabled()) return;
if (detail) std::snprintf(last_draw_refusal.why, sizeof(last_draw_refusal.why), "%s: %s", why, detail);
else std::snprintf(last_draw_refusal.why, sizeof(last_draw_refusal.why), "%s", why);
}
Expand Down Expand Up @@ -3108,21 +3110,25 @@ void draw_pipeline_colour(Wrapper* self, u64 key, const char* why)
bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UINT start, INT base_vertex, UINT start_instance,
TranslatedIndirectDraw* indirect, bool* used_fallback = nullptr)
{
last_draw_refusal.code = nullptr;
if (pipeline_colour_enabled()) last_draw_refusal = {};
auto* list = recording(self);
Pass& pass = pass_of(self);
const bool rect = pass.topology == 0x11;
const bool indexed_rect = rect && indexed && indirect;
if (!list || !pass.pipeline || !pass.targets_bound || !(rect || pass.topology == 4 || pass.topology == 5) ||
(rect && indexed && !indexed_rect)) {
note_draw_refusal(0, !list ? "no recording list" : !pass.pipeline ? "no pipeline bound"
note_draw_refusal(0, !list ? "no_recording_list" : !pass.pipeline ? "no_pipeline_bound"
: !pass.targets_bound ? "render_targets_not_bound" : "topology_not_translated",
!list ? "no recording list" : !pass.pipeline ? "no pipeline bound"
: !pass.targets_bound ? "render targets not bound" : "topology not translated");
return false;
}
AcquireSRWLockExclusive(&draw_lock);
BoundPipeline* bound = translation_for(pass.pipeline);
Translation* tr = bound ? translation_for_targets(bound->translation, pass) : nullptr;
if (!tr) note_draw_refusal(0, bound ? "no translation for the bound targets" : "pipeline description not decoded");
if (!tr) note_draw_refusal(0, bound ? "no_target_translation" : "pipeline_not_decoded",
bound ? "no translation for the bound targets" : "pipeline description not decoded");
if (used_fallback && tr && tr->manifest_retry.pending() &&
draw_missing_translation(device_of(self->own().list_device), list, pass, count, instances, start, start_instance)) {
*used_fallback = true;
Expand All @@ -3145,7 +3151,10 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
#ifdef X2W_NATIVE_SHADERS
if (!tr->native_refusal.empty()) native = tr->native_refusal.c_str();
#endif
note_draw_refusal(tr->key, translation_ok ? (!d16_ok ? "d16 texture view not ready" : "formatted buffer not ready")
const char* code = translation_ok ? (!d16_ok ? "d16_view_not_ready" : "formatted_buffer_not_ready")
: native ? "native_graphics_refused"
: tr->manifest_retry.pending() ? "manifest_absent" : "manifest_refused";
note_draw_refusal(tr->key, code, translation_ok ? (!d16_ok ? "d16 texture view not ready" : "formatted buffer not ready")
: native ? "native graphics refused"
: tr->manifest_retry.pending() ? "no translation (manifest absent)"
: "translation unusable (manifest refused)",
Expand All @@ -3160,7 +3169,7 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
if (fixed_stream && (indirect || !fixed_vertex_view(self, pass, *tr, indexed, start, count, fixed_view, fixed_resource))) {
if (indirect) indirect->set_refusal("fixed vertex stream indirect arguments are unsupported",
indirect_refusal_code::unsupported_resource_or_entry);
note_draw_refusal(tr->key, "fixed vertex stream refused");
note_draw_refusal(tr->key, "fixed_vertex_stream_refused", "fixed vertex stream refused");
static LONG refused;
if (first_calls(refused, 16))
log("x2w-draw %016llx fixed vertex stream refused: rect=%u indirect=%u vb=%llx bytes=%u",
Expand Down Expand Up @@ -3228,7 +3237,8 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
if (indirect && !(indexed_rect && !pipeline))
indirect->refusal_code = !pipeline ? indirect_refusal_code::pipeline_unavailable
: indirect_refusal_code::sampler_heap_unavailable;
note_draw_refusal(tr->key, !pipeline ? "pipeline state unavailable" : "sampler heap unavailable");
note_draw_refusal(tr->key, !pipeline ? "pipeline_state_unavailable" : "sampler_heap_unavailable",
!pipeline ? "pipeline state unavailable" : "sampler heap unavailable");
ReleaseSRWLockExclusive(&draw_lock);
return false;
}
Expand All @@ -3237,12 +3247,12 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
// draws can alias it through guest addresses, so do not guess their independence.
if (raw_uav && (indexed || indirect || tr->uavs.size() != 1 || !tr->srvs.empty())) {
log("x2w-draw %016llx raw UAV requires one writable view and a direct non-indexed draw without SRVs", tr->key);
note_draw_refusal(tr->key, "raw UAV draw shape unsupported");
note_draw_refusal(tr->key, "raw_uav_shape_unsupported", "raw UAV draw shape unsupported");
ReleaseSRWLockExclusive(&draw_lock);
return false;
}
// Every later refusal (views, buffer copies, index data, default scissor) shares one reason.
note_draw_refusal(tr->key, "resource binding refused after the pipeline was created");
note_draw_refusal(tr->key, "resource_binding_refused", "resource binding refused after the pipeline was created");
PixelUavBinding uav_bindings[pixel_uav_limit];
if (!resolve_pixel_uavs(*tr, pass, uav_bindings) ||
(raw_uav && !retain_with_recording(self, x2w_startup::ComPtr<ID3D12Resource>(uav_bindings[0].resource)))) {
Expand Down Expand Up @@ -3788,6 +3798,16 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI

LONG missing_shader_fallback_draws;

void count_direct_draw(Wrapper* self, bool drawn)
{
InterlockedIncrement(drawn ? &translated_draws : &untranslated_draws);
if (drawn) return;
const auto& refusal = last_draw_refusal;
const u64 key = refusal.code && refusal.key ? refusal.key : known_pipeline_key(pass_of(self).pipeline);
direct_draw_refusal_census.note(key, refusal.code, InterlockedCompareExchange(&frame_number, 0, 0));
if (pipeline_colour_enabled()) draw_pipeline_colour(self, refusal.key, refusal.why);
}

void draw_startup_direct(Wrapper* self, UINT count, UINT instances, UINT start, UINT start_instance)
{
bool used_fallback = false;
Expand All @@ -3796,8 +3816,7 @@ void draw_startup_direct(Wrapper* self, UINT count, UINT instances, UINT start,
const LONG n = InterlockedIncrement(&missing_shader_fallback_draws);
if (n == 1) log("x2w-draw recorded first missing shader fallback");
} else {
InterlockedIncrement(drawn ? &translated_draws : &untranslated_draws);
if (!drawn && pipeline_colour_enabled()) draw_pipeline_colour(self, last_draw_refusal.key, last_draw_refusal.why);
count_direct_draw(self, drawn);
}
}

Expand Down Expand Up @@ -3969,6 +3988,5 @@ void __stdcall list_vertex_buffers(Wrapper* self, UINT start, UINT count, const
void __stdcall list_draw_indexed(Wrapper* self, UINT count, UINT instances, UINT start, INT base_vertex, UINT start_instance)
{
const bool drawn = draw_translated(self, true, count, instances, start, base_vertex, start_instance);
InterlockedIncrement(drawn ? &translated_draws : &untranslated_draws);
if (!drawn && pipeline_colour_enabled()) draw_pipeline_colour(self, last_draw_refusal.key, last_draw_refusal.why);
count_direct_draw(self, drawn);
}
43 changes: 38 additions & 5 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/memory_srv_census.h"
#include "startup_stubs/memory_srv_as.h"
#include "startup_stubs/per_reason_refusal_census.h"
#include "startup_stubs/clear_census.h"
#include "startup_stubs/buffer_format_word3.h"
#include "startup_stubs/memory_srv_buffer.h"
#include "startup_stubs/single_element_srv.h"
Expand Down Expand Up @@ -130,8 +131,7 @@ namespace {

using u64 = std::uint64_t;
constexpr int slot_count = 512;
// Refused slot 70 calls, "token" texture nulls (memory_srv_census.h), and indirect refusal censuses
// per (pipeline key, reason) (per_reason_refusal_census.h), printed with the five-second report.
// Render diagnostics printed with the five-second report.
x2w_startup::MemorySrvCensus memory_srv_census;
x2w_startup::TokenNullCensus token_null_census;
x2w_startup::MemorySrvWrites memory_srv_writes;
Expand All @@ -140,6 +140,16 @@ x2w_startup::PerReasonRefusalCensus indirect_other_refusal_census{"indirect-othe
// Graphics-pipeline, compute-pipeline and placeholder-signature refusals, by pipeline key.
// Acceptance is unchanged; this census only names the cause.
x2w_startup::PerReasonRefusalCensus indirect_pipeline_refusal_census{"indirect-pipeline"};
x2w_startup::PerReasonRefusalCensus direct_draw_refusal_census{"direct-draw"};
x2w_startup::ClearCensus clear_census;

template <class Emit>
void report_draw_and_clear_censuses(Emit emit, bool only_if_changed)
{
if (only_if_changed || direct_draw_refusal_census.total())
direct_draw_refusal_census.report(emit, only_if_changed);
clear_census.report(emit, only_if_changed);
}

enum Kind : int {
device_kind, queue_kind, fence_kind, allocator_kind, list_kind, descriptor_heap_kind, root_signature_kind,
Expand Down Expand Up @@ -4325,9 +4335,8 @@ void __stdcall list_graphics_root(Wrapper* self, void* root)
log("x2w-render root signature %p with heap flags 0x%x not set on the desktop list (title heaps are not forwarded)",
static_cast<void*>(w), pass.root_heap_flags);
}
DXGI_FORMAT view_format_of(u64 desktop_handle, ID3D12Resource* target)
DXGI_FORMAT view_format_of(u64 desktop_handle, DXGI_FORMAT format)
{
DXGI_FORMAT format = target->GetDesc().Format;
if (const std::uint32_t id = shadow_id(desktop_handle)) {
AcquireSRWLockShared(&note_lock);
DXGI_FORMAT view_format = DXGI_FORMAT_UNKNOWN;
Expand All @@ -4338,6 +4347,20 @@ DXGI_FORMAT view_format_of(u64 desktop_handle, ID3D12Resource* target)
}
return format;
}
DXGI_FORMAT view_format_of(u64 desktop_handle, ID3D12Resource* target)
{
return view_format_of(desktop_handle, target->GetDesc().Format);
}

void note_clear(x2w_startup::ClearKind kind, u64 handle, ID3D12Resource* target, const void* values)
{
using namespace x2w_startup;
const auto desc = target->GetDesc();
ClearKey key{kind, static_cast<unsigned>(view_format_of(handle, desc.Format)), static_cast<unsigned>(desc.Dimension),
clear_size_class(desc.Width), clear_size_class(desc.Height), clear_size_class(desc.DepthOrArraySize)};
std::memcpy(key.bits.data(), values, 4 * sizeof(std::uint32_t));
clear_census.note(key, InterlockedCompareExchange(&frame_number, 0, 0));
}
// The view a descriptor holds now, read from its shadow record so copies are followed. A record of the
// other kind is wrong_kind; a handle with no record falls back to the creation-time map (render targets only).
struct ResolvedView {
Expand Down Expand Up @@ -4451,6 +4474,12 @@ void __stdcall list_clear_depth_stencil(Wrapper* self, u64 handle, D3D12_CLEAR_F
list->ClearDepthStencilView(D3D12_CPU_DESCRIPTOR_HANDLE{static_cast<SIZE_T>(desktop)}, flags, depth, stencil,
rect_count, rect_count ? rects : nullptr);
red_producers().after(self, red_sample);
// The four census words are depth bits, stencil, effective clear flags and zero.
const float cleared_depth = flags & D3D12_CLEAR_FLAG_DEPTH ? depth : 0.0f;
std::uint32_t values[] = {0, flags & D3D12_CLEAR_FLAG_STENCIL ? static_cast<unsigned>(stencil) : 0u,
static_cast<unsigned>(flags), 0};
std::memcpy(values, &cleared_depth, sizeof(cleared_depth));
note_clear(x2w_startup::ClearKind::dsv, desktop, target, values);
InterlockedIncrement(&render_clears);
}
// List slot 52 (desktop 48 + 4), ClearRenderTargetView(view, colour, rect count, rects). Desktop
Expand Down Expand Up @@ -4482,6 +4511,7 @@ void __stdcall list_clear_target(Wrapper* self, u64 handle, const float* colour,
list->ClearRenderTargetView(D3D12_CPU_DESCRIPTOR_HANDLE{static_cast<SIZE_T>(desktop)}, colour, rect_count,
rect_count ? rects : nullptr);
red_producers().after(self, red_sample);
note_clear(x2w_startup::ClearKind::rtv, desktop, target, colour);
InterlockedIncrement(&render_clears);
}
// The desktop shader-visible CBV/SRV/UAV heap that owns the GPU handle's descriptor, or null.
Expand Down Expand Up @@ -4548,6 +4578,7 @@ void __stdcall list_clear_uav(Wrapper* self, u64 gpu_handle, u64 cpu_handle, u64
const D3D12_CPU_DESCRIPTOR_HANDLE cpu{static_cast<SIZE_T>(desktop)};
if (Float) list->ClearUnorderedAccessViewFloat(gpu, cpu, target, static_cast<const float*>(values), rect_count, rect_count ? rects : nullptr);
else list->ClearUnorderedAccessViewUint(gpu, cpu, target, static_cast<const UINT*>(values), rect_count, rect_count ? rects : nullptr);
note_clear(Float ? x2w_startup::ClearKind::uav_float : x2w_startup::ClearKind::uav_uint, desktop, target, values);
InterlockedIncrement(&render_clears);
}
// List slot 55 (desktop 51 + 4), DiscardResource(resource, region). Discard is only a hint, so a resource
Expand Down Expand Up @@ -7901,7 +7932,7 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa
static LONG frames;
static ULONGLONG started, last_report;
const LONG frame = InterlockedIncrement(&frames);
frame_number = frame;
InterlockedExchange(&frame_number, frame);
draw_dump_flush();
red_producers().flush();
{
Expand Down Expand Up @@ -8112,6 +8143,7 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa
indirect_other_refusal_census.report([](const char* line) { log("%s", line); }, true);
indirect_pipeline_refusal_census.report([](const char* line) { log("%s", line); }, true);
emit_pipeline_refusal_counts([](const char* line) { log("%s", line); });
report_draw_and_clear_censuses([](const char* line) { log("%s", line); }, true);
log("x2w-d3d12 heartbeat: %ld frames presented in %.1f s (%.1f fps overall); frame waits by type %ld/%ld/%ld/%ld, "
"%lld ms slept", frame, (now - started) / 1000.0, frame * 1000.0 / (now - started ? now - started : 1),
frame_waits[0], frame_waits[1], frame_waits[2], frame_waits[3], static_cast<long long>(frame_wait_ms));
Expand Down Expand Up @@ -8141,6 +8173,7 @@ extern "C" __declspec(dllexport) std::size_t x2w_d3d12_census_report(char* out,
if (indirect_other_refusal_census.total()) indirect_other_refusal_census.report(emit, false);
if (indirect_pipeline_refusal_census.total()) indirect_pipeline_refusal_census.report(emit, false);
emit_pipeline_refusal_counts(emit);
report_draw_and_clear_censuses(emit, false);
if (out && capacity) out[length < capacity ? length : capacity - 1] = 0;
return length;
}
Expand Down
Loading
Loading