diff --git a/Source/Core/VideoBackends/Metal/MTLStateTracker.h b/Source/Core/VideoBackends/Metal/MTLStateTracker.h index 2357322e4c..54e16cac7e 100644 --- a/Source/Core/VideoBackends/Metal/MTLStateTracker.h +++ b/Source/Core/VideoBackends/Metal/MTLStateTracker.h @@ -270,6 +270,7 @@ private: u32 texel_buffer_offset0; u32 texel_buffer_offset1; PerfQueryGroup perf_query_group = static_cast(-1); + u32 perf_query_id; } m_state; u32 m_perf_query_tracker_counter = 0; diff --git a/Source/Core/VideoBackends/Metal/MTLStateTracker.mm b/Source/Core/VideoBackends/Metal/MTLStateTracker.mm index 2769910d88..f699657ade 100644 --- a/Source/Core/VideoBackends/Metal/MTLStateTracker.mm +++ b/Source/Core/VideoBackends/Metal/MTLStateTracker.mm @@ -674,6 +674,7 @@ std::shared_ptr Metal::StateTracker::NewP m_perf_query_tracker_counter++]]; tracker->buffer = MRCTransfer(buffer); tracker->contents = static_cast([buffer contents]); + tracker->query_id = m_state.perf_query_id; return tracker; } } @@ -682,6 +683,8 @@ std::shared_ptr Metal::StateTracker::NewP // Reuse an old one std::shared_ptr tracker = std::move(m_perf_query_tracker_cache.back()); m_perf_query_tracker_cache.pop_back(); + tracker->groups.clear(); + tracker->query_id = m_state.perf_query_id; return tracker; } } @@ -689,15 +692,26 @@ std::shared_ptr Metal::StateTracker::NewP void Metal::StateTracker::EnablePerfQuery(PerfQueryGroup group, u32 query_id) { m_state.perf_query_group = group; + m_state.perf_query_id = query_id; if (!m_current_perf_query || m_current_perf_query->query_id != query_id || m_current_perf_query->groups.size() == PERF_QUERY_BUFFER_SIZE) { if (m_current_render_encoder) EndRenderPass(); - if (!m_current_perf_query) - m_current_perf_query = NewPerfQueryTracker(); - m_current_perf_query->groups.clear(); - m_current_perf_query->query_id = query_id; + if (m_current_perf_query) + { + [m_current_render_cmdbuf + addCompletedHandler:[backref = m_backref, q = std::move(m_current_perf_query)](id) { + std::lock_guard guard(backref->mtx); + if (StateTracker* tracker = backref->state_tracker) + { + if (PerfQuery* query = static_cast(g_perf_query.get())) + query->ReturnResults(q->contents, q->groups.data(), q->groups.size(), q->query_id); + tracker->m_perf_query_tracker_cache.emplace_back(std::move(q)); + } + }]; + m_current_perf_query.reset(); + } } }