| 1 |
// Copyright 2022 Dolphin Emulator Project |
| 2 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
|
| 4 |
#include "VideoBackends/Metal/MTLPerfQuery.h" |
| 5 |
|
| 6 |
#include "VideoBackends/Metal/MTLStateTracker.h" |
| 7 |
|
| 8 |
void Metal::PerfQuery::EnableQuery(PerfQueryGroup type) |
| 9 |
{ |
| 10 |
if (type == PQG_ZCOMP_ZCOMPLOC || type == PQG_ZCOMP) |
| 11 |
g_state_tracker->EnablePerfQuery(type, m_current_query); |
| 12 |
} |
| 13 |
|
| 14 |
void Metal::PerfQuery::DisableQuery(PerfQueryGroup type) |
| 15 |
{ |
| 16 |
if (type == PQG_ZCOMP_ZCOMPLOC || type == PQG_ZCOMP) |
| 17 |
g_state_tracker->DisablePerfQuery(); |
| 18 |
} |
| 19 |
|
| 20 |
void Metal::PerfQuery::ResetQuery() |
| 21 |
{ |
| 22 |
std::lock_guard<std::mutex> lock(m_results_mtx); |
| 23 |
m_current_query++; |
| 24 |
for (std::atomic<u32>& result : m_results) |
| 25 |
result.store(0, std::memory_order_relaxed); |
| 26 |
} |
| 27 |
|
| 28 |
u32 Metal::PerfQuery::GetQueryResult(PerfQueryType type) |
| 29 |
{ |
| 30 |
u32 result = 0; |
| 31 |
if (type == PQ_ZCOMP_INPUT_ZCOMPLOC || type == PQ_ZCOMP_OUTPUT_ZCOMPLOC) |
| 32 |
{ |
| 33 |
result = m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed); |
| 34 |
} |
| 35 |
else if (type == PQ_ZCOMP_INPUT || type == PQ_ZCOMP_OUTPUT) |
| 36 |
{ |
| 37 |
result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed); |
| 38 |
} |
| 39 |
else if (type == PQ_BLEND_INPUT) |
| 40 |
{ |
| 41 |
result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed) + |
| 42 |
m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed); |
| 43 |
} |
| 44 |
else if (type == PQ_EFB_COPY_CLOCKS) |
| 45 |
{ |
| 46 |
result = m_results[PQG_EFB_COPY_CLOCKS].load(std::memory_order_relaxed); |
| 47 |
} |
| 48 |
|
| 49 |
return result / 4; |
| 50 |
} |
| 51 |
|
| 52 |
void Metal::PerfQuery::FlushResults() |
| 53 |
{ |
| 54 |
if (IsFlushed()) |
| 55 |
return; |
| 56 |
|
| 57 |
// There's a possibility that some active performance queries are unflushed |
| 58 |
g_state_tracker->FlushEncoders(); |
| 59 |
g_state_tracker->NotifyOfCPUGPUSync(); |
| 60 |
|
| 61 |
std::unique_lock<std::mutex> lock(m_results_mtx); |
| 62 |
while (!IsFlushed()) |
| 63 |
m_cv.wait(lock); |
| 64 |
} |
| 65 |
|
| 66 |
bool Metal::PerfQuery::IsFlushed() const |
| 67 |
{ |
| 68 |
return m_query_count.load(std::memory_order_acquire) == 0; |
| 69 |
} |
| 70 |
|
| 71 |
void Metal::PerfQuery::ReturnResults(const u64* data, const PerfQueryGroup* groups, size_t count, |
| 72 |
u32 query_id) |
| 73 |
{ |
| 74 |
{ |
| 75 |
std::lock_guard<std::mutex> lock(m_results_mtx); |
| 76 |
if (m_current_query == query_id) |
| 77 |
{ |
| 78 |
for (size_t i = 0; i < count; ++i) |
| 79 |
{ |
| 80 |
u64 native_res_result = |
| 81 |
data[i] * (EFB_WIDTH * EFB_HEIGHT) / |
| 82 |
(g_framebuffer_manager->GetEFBWidth() * g_framebuffer_manager->GetEFBHeight()); |
| 83 |
|
| 84 |
native_res_result /= g_ActiveConfig.iMultisamples; |
| 85 |
|
| 86 |
m_results[groups[i]].fetch_add(native_res_result, std::memory_order_relaxed); |
| 87 |
} |
| 88 |
} |
| 89 |
m_query_count.fetch_sub(1, std::memory_order_release); |
| 90 |
} |
| 91 |
m_cv.notify_one(); |
| 92 |
} |