| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/D3D12/D3D12PerfQuery.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | |
| 8 | #include "Common/Assert.h" |
| 9 | #include "Common/Logging/Log.h" |
| 10 | |
| 11 | #include "VideoBackends/D3D12/Common.h" |
| 12 | #include "VideoBackends/D3D12/D3D12Gfx.h" |
| 13 | #include "VideoBackends/D3D12/DX12Context.h" |
| 14 | #include "VideoCommon/FramebufferManager.h" |
| 15 | #include "VideoCommon/VideoCommon.h" |
| 16 | #include "VideoCommon/VideoConfig.h" |
| 17 | |
| 18 | namespace DX12 |
| 19 | { |
| 20 | PerfQuery::PerfQuery() = default; |
| 21 | |
| 22 | PerfQuery::~PerfQuery() = default; |
| 23 | |
| 24 | bool PerfQuery::Initialize() |
| 25 | { |
| 26 | constexpr D3D12_QUERY_HEAP_DESC desc = {D3D12_QUERY_HEAP_TYPE_OCCLUSION, PERF_QUERY_BUFFER_SIZE}; |
| 27 | HRESULT hr = g_dx_context->GetDevice()->CreateQueryHeap(&desc, IID_PPV_ARGS(&m_query_heap)); |
| 28 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create query heap: {}", DX12HRWrap(hr)); |
| 29 | if (FAILED(hr)) |
| 30 | return false; |
| 31 | |
| 32 | constexpr D3D12_HEAP_PROPERTIES heap_properties = {D3D12_HEAP_TYPE_READBACK}; |
| 33 | constexpr D3D12_RESOURCE_DESC resource_desc = {D3D12_RESOURCE_DIMENSION_BUFFER, |
| 34 | 0, |
| 35 | PERF_QUERY_BUFFER_SIZE * sizeof(PerfQueryDataType), |
| 36 | 1, |
| 37 | 1, |
| 38 | 1, |
| 39 | DXGI_FORMAT_UNKNOWN, |
| 40 | {1, 0}, |
| 41 | D3D12_TEXTURE_LAYOUT_ROW_MAJOR, |
| 42 | D3D12_RESOURCE_FLAG_NONE}; |
| 43 | hr = g_dx_context->GetDevice()->CreateCommittedResource( |
| 44 | &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc, D3D12_RESOURCE_STATE_COPY_DEST, |
| 45 | nullptr, IID_PPV_ARGS(&m_query_readback_buffer)); |
| 46 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create query buffer: {}", DX12HRWrap(hr)); |
| 47 | if (FAILED(hr)) |
| 48 | return false; |
| 49 | |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | void PerfQuery::EnableQuery(PerfQueryGroup group) |
| 54 | { |
| 55 | // Block if there are no free slots. |
| 56 | // Otherwise, try to keep half of them available. |
| 57 | const u32 query_count = m_query_count.load(std::memory_order_relaxed); |
| 58 | if (query_count > m_query_buffer.size() / 2) |
| 59 | { |
| 60 | const bool do_resolve = m_unresolved_queries > m_query_buffer.size() / 2; |
| 61 | const bool blocking = query_count == PERF_QUERY_BUFFER_SIZE; |
| 62 | PartialFlush(do_resolve, blocking); |
| 63 | } |
| 64 | |
| 65 | // Ensure all state is applied before beginning the query. |
| 66 | // This is because we can't leave a query open when submitting a command list, and the draw |
| 67 | // call itself may need to execute a command list if we run out of descriptors. Note that |
| 68 | // this assumes that the caller has bound all required state prior to enabling the query. |
| 69 | Gfx::GetInstance()->ApplyState(); |
| 70 | |
| 71 | if (group == PQG_ZCOMP_ZCOMPLOC || group == PQG_ZCOMP) |
| 72 | { |
| 73 | ActiveQuery& entry = m_query_buffer[m_query_next_pos]; |
| 74 | ASSERT(!entry.has_value && !entry.resolved); |
| 75 | entry.has_value = true; |
| 76 | entry.query_group = group; |
| 77 | |
| 78 | g_dx_context->GetCommandList()->BeginQuery(m_query_heap.Get(), D3D12_QUERY_TYPE_OCCLUSION, |
| 79 | m_query_next_pos); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void PerfQuery::DisableQuery(PerfQueryGroup group) |
| 84 | { |
| 85 | if (group == PQG_ZCOMP_ZCOMPLOC || group == PQG_ZCOMP) |
| 86 | { |
| 87 | g_dx_context->GetCommandList()->EndQuery(m_query_heap.Get(), D3D12_QUERY_TYPE_OCCLUSION, |
| 88 | m_query_next_pos); |
| 89 | m_query_next_pos = (m_query_next_pos + 1) % PERF_QUERY_BUFFER_SIZE; |
| 90 | m_query_count.fetch_add(1, std::memory_order_relaxed); |
| 91 | m_unresolved_queries++; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void PerfQuery::ResetQuery() |
| 96 | { |
| 97 | m_query_count.store(0, std::memory_order_relaxed); |
| 98 | m_unresolved_queries = 0; |
| 99 | m_query_resolve_pos = 0; |
| 100 | m_query_readback_pos = 0; |
| 101 | m_query_next_pos = 0; |
| 102 | for (size_t i = 0; i < m_results.size(); ++i) |
| 103 | m_results[i].store(0, std::memory_order_relaxed); |
| 104 | for (auto& entry : m_query_buffer) |
| 105 | { |
| 106 | entry.fence_value = 0; |
| 107 | entry.resolved = false; |
| 108 | entry.has_value = false; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | u32 PerfQuery::GetQueryResult(PerfQueryType type) |
| 113 | { |
| 114 | u32 result = 0; |
| 115 | if (type == PQ_ZCOMP_INPUT_ZCOMPLOC || type == PQ_ZCOMP_OUTPUT_ZCOMPLOC) |
| 116 | { |
| 117 | result = m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed); |
| 118 | } |
| 119 | else if (type == PQ_ZCOMP_INPUT || type == PQ_ZCOMP_OUTPUT) |
| 120 | { |
| 121 | result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed); |
| 122 | } |
| 123 | else if (type == PQ_BLEND_INPUT) |
| 124 | { |
| 125 | result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed) + |
| 126 | m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed); |
| 127 | } |
| 128 | else if (type == PQ_EFB_COPY_CLOCKS) |
| 129 | { |
| 130 | result = m_results[PQG_EFB_COPY_CLOCKS].load(std::memory_order_relaxed); |
| 131 | } |
| 132 | |
| 133 | return result / 4; |
| 134 | } |
| 135 | |
| 136 | void PerfQuery::FlushResults() |
| 137 | { |
| 138 | while (!IsFlushed()) |
| 139 | PartialFlush(true, true); |
| 140 | } |
| 141 | |
| 142 | bool PerfQuery::IsFlushed() const |
| 143 | { |
| 144 | return m_query_count.load(std::memory_order_relaxed) == 0; |
| 145 | } |
| 146 | |
| 147 | void PerfQuery::ResolveQueries() |
| 148 | { |
| 149 | // Do we need to split the resolve as it's wrapping around? |
| 150 | if ((m_query_resolve_pos + m_unresolved_queries) > PERF_QUERY_BUFFER_SIZE) |
| 151 | ResolveQueries(PERF_QUERY_BUFFER_SIZE - m_query_resolve_pos); |
| 152 | |
| 153 | ResolveQueries(m_unresolved_queries); |
| 154 | } |
| 155 | |
| 156 | void PerfQuery::ResolveQueries(u32 query_count) |
| 157 | { |
| 158 | DEBUG_ASSERT(m_unresolved_queries >= query_count && |
| 159 | (m_query_resolve_pos + query_count) <= PERF_QUERY_BUFFER_SIZE); |
| 160 | |
| 161 | g_dx_context->GetCommandList()->ResolveQueryData( |
| 162 | m_query_heap.Get(), D3D12_QUERY_TYPE_OCCLUSION, m_query_resolve_pos, query_count, |
| 163 | m_query_readback_buffer.Get(), m_query_resolve_pos * sizeof(PerfQueryDataType)); |
| 164 | |
| 165 | // Flag all queries as available, but with a fence that has to be completed first |
| 166 | for (u32 i = 0; i < query_count; i++) |
| 167 | { |
| 168 | ActiveQuery& entry = m_query_buffer[m_query_resolve_pos + i]; |
| 169 | DEBUG_ASSERT(entry.has_value && !entry.resolved); |
| 170 | entry.fence_value = g_dx_context->GetCurrentFenceValue(); |
| 171 | entry.resolved = true; |
| 172 | } |
| 173 | m_query_resolve_pos = (m_query_resolve_pos + query_count) % PERF_QUERY_BUFFER_SIZE; |
| 174 | m_unresolved_queries -= query_count; |
| 175 | } |
| 176 | |
| 177 | void PerfQuery::ReadbackQueries(bool blocking) |
| 178 | { |
| 179 | u64 completed_fence_counter = g_dx_context->GetCompletedFenceValue(); |
| 180 | |
| 181 | // Need to save these since ProcessResults will modify them. |
| 182 | const u32 outstanding_queries = m_query_count.load(std::memory_order_relaxed); |
| 183 | u32 readback_count = 0; |
| 184 | for (u32 i = 0; i < outstanding_queries; i++) |
| 185 | { |
| 186 | u32 index = (m_query_readback_pos + readback_count) % PERF_QUERY_BUFFER_SIZE; |
| 187 | const ActiveQuery& entry = m_query_buffer[index]; |
| 188 | if (!entry.resolved) |
| 189 | break; |
| 190 | |
| 191 | if (entry.fence_value > completed_fence_counter) |
| 192 | { |
| 193 | // Query result isn't ready yet. Wait if blocking, otherwise we can't do any more yet. |
| 194 | if (!blocking) |
| 195 | break; |
| 196 | |
| 197 | ASSERT(entry.fence_value != g_dx_context->GetCurrentFenceValue()); |
| 198 | g_dx_context->WaitForFence(entry.fence_value); |
| 199 | completed_fence_counter = g_dx_context->GetCompletedFenceValue(); |
| 200 | } |
| 201 | |
| 202 | // If this wrapped around, we need to flush the entries before the end of the buffer. |
| 203 | if (index < m_query_readback_pos) |
| 204 | { |
| 205 | AccumulateQueriesFromBuffer(readback_count); |
| 206 | DEBUG_ASSERT(m_query_readback_pos == 0); |
| 207 | readback_count = 0; |
| 208 | } |
| 209 | |
| 210 | readback_count++; |
| 211 | } |
| 212 | |
| 213 | if (readback_count > 0) |
| 214 | AccumulateQueriesFromBuffer(readback_count); |
| 215 | } |
| 216 | |
| 217 | void PerfQuery::AccumulateQueriesFromBuffer(u32 query_count) |
| 218 | { |
| 219 | // Should be at maximum query_count queries pending. |
| 220 | ASSERT(query_count <= m_query_count.load(std::memory_order_relaxed) && |
| 221 | (m_query_readback_pos + query_count) <= PERF_QUERY_BUFFER_SIZE); |
| 222 | |
| 223 | const D3D12_RANGE read_range = {m_query_readback_pos * sizeof(PerfQueryDataType), |
| 224 | (m_query_readback_pos + query_count) * sizeof(PerfQueryDataType)}; |
| 225 | u8* mapped_ptr; |
| 226 | HRESULT hr = m_query_readback_buffer->Map(0, &read_range, reinterpret_cast<void**>(&mapped_ptr)); |
| 227 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to map query readback buffer: {}", DX12HRWrap(hr)); |
| 228 | if (FAILED(hr)) |
| 229 | return; |
| 230 | |
| 231 | // Remove pending queries. |
| 232 | for (u32 i = 0; i < query_count; i++) |
| 233 | { |
| 234 | u32 index = (m_query_readback_pos + i) % PERF_QUERY_BUFFER_SIZE; |
| 235 | ActiveQuery& entry = m_query_buffer[index]; |
| 236 | |
| 237 | // Should have a fence associated with it (waiting for a result). |
| 238 | ASSERT(entry.fence_value != 0); |
| 239 | entry.fence_value = 0; |
| 240 | entry.resolved = false; |
| 241 | entry.has_value = false; |
| 242 | |
| 243 | // Grab result from readback buffer, it will already have been invalidated. |
| 244 | PerfQueryDataType result; |
| 245 | std::memcpy(&result, mapped_ptr + (index * sizeof(PerfQueryDataType)), sizeof(result)); |
| 246 | |
| 247 | // NOTE: Reported pixel metrics should be referenced to native resolution |
| 248 | u64 native_res_result = static_cast<u64>(result) * EFB_WIDTH / |
| 249 | g_framebuffer_manager->GetEFBWidth() * EFB_HEIGHT / |
| 250 | g_framebuffer_manager->GetEFBHeight(); |
| 251 | if (g_ActiveConfig.iMultisamples > 1) |
| 252 | native_res_result /= g_ActiveConfig.iMultisamples; |
| 253 | m_results[entry.query_group].fetch_add(static_cast<u32>(native_res_result), |
| 254 | std::memory_order_relaxed); |
| 255 | } |
| 256 | |
| 257 | constexpr D3D12_RANGE write_range = {0, 0}; |
| 258 | m_query_readback_buffer->Unmap(0, &write_range); |
| 259 | |
| 260 | m_query_readback_pos = (m_query_readback_pos + query_count) % PERF_QUERY_BUFFER_SIZE; |
| 261 | m_query_count.fetch_sub(query_count, std::memory_order_relaxed); |
| 262 | } |
| 263 | |
| 264 | void PerfQuery::PartialFlush(bool resolve, bool blocking) |
| 265 | { |
| 266 | // Submit a command buffer if there are unresolved queries (to write them to the buffer). |
| 267 | if (resolve && m_unresolved_queries > 0) |
| 268 | Gfx::GetInstance()->ExecuteCommandList(false); |
| 269 | |
| 270 | ReadbackQueries(blocking); |
| 271 | } |
| 272 | } // namespace DX12 |