Skip to content
Draft
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
54 changes: 53 additions & 1 deletion src/d3d11/d3d11_context_imm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ class MTLD3D11ImmediateContext : public ImmediateContextBase {
} else {
static_cast<MTLD3D11EventQuery *>(pAsync)->Issue(cmd_queue.GetCurrentEventSeqId());
}
/* MADEIRA: the signal (this one, or the earlier one it reuses) may still be
* in the chunk being recorded; GetData only parks once that chunk is out. */
static_cast<MTLD3D11EventQuery *>(pAsync)->SetIssueChunk(cmd_queue.CurrentSeqId());
break;
}
case D3D11_QUERY_OCCLUSION:
Expand All @@ -420,6 +423,52 @@ class MTLD3D11ImmediateContext : public ImmediateContextBase {
}
}

/* MADEIRA: games build a GPU fence out of `while (GetData(event) == S_FALSE) {}`
* (Path of Exile 2 does it every frame: up to ~130K polls a frame, millions a
* second). Under emulation every poll is an x64 -> ARM64EC round trip plus a
* Metal call, and the polling thread burns a P-core for nothing, which on a
* phone is heat that throttles the threads doing real work. Once the polls
* of one query come in a burst (two pending polls less than 250 us apart),
* this parks the caller on the queue's shared event until the query's value
* is signaled, at most 1 ms (Metal's timeout is in milliseconds), and then
* answers. A one-off poll (a query read once a frame, a frame or more after
* it was issued) never waits. The park only happens once the chunk holding
* the signal has been committed: before that, no wait can see it complete.
* The flush that GetData would do anyway (no DONOTFLUSH) is done first, so
* the signal is on its way before the caller parks. Same idea as D3D9's
* WaitCPUFenceBounded parking (d3d9_query.cpp), on the D3D11 event value.
* Opt-in: env.MADEIRA_D3D11_QUERY_PARK = 1. */
static bool
QueryParkEnabled() {
static const bool enabled = [] {
const char *value = std::getenv("MADEIRA_D3D11_QUERY_PARK");
bool on = value && !std::strcmp(value, "1");
if (on)
ERR("[query-park] D3D11 event-query parking on (env.MADEIRA_D3D11_QUERY_PARK = 1)");
return on;
}();
return enabled;
}

HRESULT
ParkOnEventQuery(MTLD3D11EventQuery *query, UINT &GetDataFlags) {
if ((GetDataFlags & D3D11_ASYNC_GETDATA_DONOTFLUSH) == 0) {
cmd_queue.CurrentFrameStatistics().event_stall++;
Flush();
GetDataFlags |= D3D11_ASYNC_GETDATA_DONOTFLUSH; /* done: the tail of GetData must not flush again */
}
auto now = std::chrono::steady_clock::now();
uint64_t now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
if (!query->NotePendingPoll(now_ns) || cmd_queue.CurrentSeqId() <= query->IssueChunk())
return S_FALSE;
cmd_queue.event.waitUntilSignaledValue(query->SignalSeqId(), 1);
g_perf.polls_parked.fetch_add(1, std::memory_order_relaxed);
g_perf.park_ns.fetch_add(
std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - now).count(),
std::memory_order_relaxed);
return query->CheckEventState(cmd_queue.SignaledEventSeqId()) == EventState::Signaled ? S_OK : S_FALSE;
}

HRESULT
STDMETHODCALLTYPE
GetData(ID3D11Asynchronous *pAsync, void *pData, UINT DataSize, UINT GetDataFlags) override {
Expand All @@ -438,7 +487,8 @@ class MTLD3D11ImmediateContext : public ImmediateContextBase {
case D3D11_QUERY_EVENT:
case D3D11_QUERY_TIMESTAMP:
case D3D11_QUERY_TIMESTAMP_DISJOINT: {
switch (static_cast<MTLD3D11EventQuery *>(pAsync)->CheckEventState(cmd_queue.SignaledEventSeqId())) {
auto query = static_cast<MTLD3D11EventQuery *>(pAsync);
switch (query->CheckEventState(cmd_queue.SignaledEventSeqId())) {
case EventState::Pending:
break;
case EventState::Stall:
Expand All @@ -450,6 +500,8 @@ class MTLD3D11ImmediateContext : public ImmediateContextBase {
case EventState::Invalid:
return DXGI_ERROR_INVALID_CALL;
}
if (hr == S_FALSE && QueryParkEnabled())
hr = ParkOnEventQuery(query, GetDataFlags);
break;
}
default:
Expand Down
33 changes: 33 additions & 0 deletions src/d3d11/d3d11_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,24 @@ enum class EventState {

constexpr size_t kEventStallThreshold = 64;

/* MADEIRA: burst polling of an event query. Two pending polls less than this
* apart continue a burst; after kEventPollsBeforePark of them the immediate
* context parks the caller on the queue's shared event (see GetData). */
constexpr uint64_t kEventPollBurstGapNs = 250 * 1000;
constexpr uint32_t kEventPollsBeforePark = 2;

struct MTLD3D11EventQuery : public ID3D11Query {
virtual void Issue(uint64_t current_seq_id) = 0;
virtual EventState CheckEventState(uint64_t coherent_seq_id) = 0;
/* The event value this query completes at. */
virtual uint64_t SignalSeqId() = 0;
/* The command chunk that was being recorded when the query was issued: its
* signal reaches the GPU only once that chunk is committed. */
virtual void SetIssueChunk(uint64_t chunk_seq_id) = 0;
virtual uint64_t IssueChunk() = 0;
/* Records a pending poll at now_ns; true when it continues a burst long
* enough to park on. */
virtual bool NotePendingPoll(uint64_t now_ns) = 0;
};

template <typename DataType>
Expand All @@ -138,6 +153,21 @@ class MTLD3D11EventQueryImpl : public MTLD3DQueryBase<MTLD3D11EventQuery> {
state = QueryState::Issued;
should_be_signaled_at = current_seq_id;
stall_counter = 0;
issue_chunk = 0;
poll_burst = 0;
last_pending_poll_ns = 0;
}

uint64_t SignalSeqId() override { return should_be_signaled_at; }
void SetIssueChunk(uint64_t chunk_seq_id) override { issue_chunk = chunk_seq_id; }
uint64_t IssueChunk() override { return issue_chunk; }
bool NotePendingPoll(uint64_t now_ns) override {
if (last_pending_poll_ns && now_ns - last_pending_poll_ns <= kEventPollBurstGapNs)
poll_burst = std::min(poll_burst + 1, kEventPollsBeforePark + 1);
else
poll_burst = 1;
last_pending_poll_ns = now_ns;
return poll_burst > kEventPollsBeforePark;
}

EventState CheckEventState(uint64_t coherent_seq_id) override {
Expand All @@ -162,6 +192,9 @@ class MTLD3D11EventQueryImpl : public MTLD3DQueryBase<MTLD3D11EventQuery> {
QueryState state = QueryState::Undefined;
uint32_t stall_counter = 0;
uint64_t should_be_signaled_at = 0;
uint64_t issue_chunk = 0;
uint32_t poll_burst = 0;
uint64_t last_pending_poll_ns = 0;
};

HRESULT CreateOcculusionQuery(MTLD3D11Device *pDevice,
Expand Down
9 changes: 6 additions & 3 deletions src/dxmt/dxmt_command_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,21 +243,24 @@ CommandQueue::AccountGpuTime(obj_handle_t cmdbuf, uint64_t frame) {
const double waits = take(g_perf.map_waits), wait_ms = take(g_perf.map_wait_ns) / 1e6,
unsubmitted = take(g_perf.map_waits_unsubmitted);
const double pe = take(g_perf.polls_event), po = take(g_perf.polls_other);
const double parked = take(g_perf.polls_parked), park_ms = take(g_perf.park_ns) / 1e6;
const double draws = take(g_perf.draws), tess = take(g_perf.draws_tess), gs = take(g_perf.draws_gs);
const double dec_ms = take(g_perf.decode_ns) / 1e6, refill_ms = take(g_perf.refill_ns) / 1e6;
const double rw_ms = take(g_perf.refill_worker_ns) / 1e6, rq = take(g_perf.refill_queued),
ra = take(g_perf.refill_applied), rs = take(g_perf.refill_stale), ro = take(g_perf.refill_overbudget);
char line[800];
char line[900];
snprintf(line, sizeof(line),
"[gpu-perf] ml1254 %.0f frames in %.0f ms (%.1f fps): GPU busy %.0f ms = %.0f%% (%.1f ms/frame), "
"%.1f cmdbufs/frame | readback Map waits %.2f/frame, %.1f ms/frame (%.2f/frame on unsubmitted work) | "
"not-ready polls/frame: event %.1f, other %.1f | draws/frame %.0f, tess %.0f, GS %.0f | "
"game-thread BC decode %.2f ms/frame (%.0f ms per 64 frames), lost-level refill %.2f ms/frame | "
"async refill: worker %.2f ms/frame, queued %.0f applied %.0f stale %.0f over-budget %.0f",
"async refill: worker %.2f ms/frame, queued %.0f applied %.0f stale %.0f over-budget %.0f | "
"parked event polls/frame %.1f, %.2f ms/frame",
frames, wall_ms, wall_ms > 0 ? frames * 1000.0 / wall_ms : 0.0, busy_ms,
wall_ms > 0 ? busy_ms * 100.0 / wall_ms : 0.0, busy_ms / frames, perf_.cmdbufs / frames, waits / frames,
wait_ms / frames, unsubmitted / frames, pe / frames, po / frames, draws / frames, tess / frames, gs / frames,
dec_ms / frames, dec_ms, refill_ms / frames, rw_ms / frames, rq, ra, rs, ro);
dec_ms / frames, dec_ms, refill_ms / frames, rw_ms / frames, rq, ra, rs, ro, parked / frames,
park_ms / frames);
ERR(line);
perf_.window_frame = frame;
perf_.window_start = perf_.last_end;
Expand Down
1 change: 1 addition & 0 deletions src/dxmt/dxmt_command_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ constexpr uint32_t kCommandChunkCount = 32;
struct PerfCounters {
std::atomic<uint64_t> map_wait_ns{0}, map_waits{0}, map_waits_unsubmitted{0};
std::atomic<uint64_t> polls_event{0}, polls_other{0};
std::atomic<uint64_t> polls_parked{0}, park_ns{0}; /* MADEIRA: D3D11 event-query parking */
std::atomic<uint64_t> draws{0}, draws_tess{0}, draws_gs{0};
std::atomic<uint64_t> decode_ns{0}, refill_ns{0}; /* BC work on the game thread */
std::atomic<uint64_t> refill_worker_ns{0}, refill_queued{0}, refill_applied{0}, refill_stale{0},
Expand Down
Loading