| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/Vulkan/VKPerfQuery.h" |
| 5 | |
| 6 | #include <cstring> |
| 7 | |
| 8 | #include "Common/Assert.h" |
| 9 | #include "Common/MsgHandler.h" |
| 10 | |
| 11 | #include "VideoBackends/Vulkan/CommandBufferManager.h" |
| 12 | #include "VideoBackends/Vulkan/StateTracker.h" |
| 13 | #include "VideoBackends/Vulkan/VKGfx.h" |
| 14 | #include "VideoBackends/Vulkan/VulkanContext.h" |
| 15 | #include "VideoCommon/FramebufferManager.h" |
| 16 | #include "VideoCommon/VideoCommon.h" |
| 17 | #include "VideoCommon/VideoConfig.h" |
| 18 | |
| 19 | namespace Vulkan |
| 20 | { |
| 21 | PerfQuery::PerfQuery() = default; |
| 22 | |
| 23 | PerfQuery::~PerfQuery() |
| 24 | { |
| 25 | if (m_query_pool != VK_NULL_HANDLE) |
| 26 | vkDestroyQueryPool(g_vulkan_context->GetDevice(), m_query_pool, nullptr); |
| 27 | } |
| 28 | |
| 29 | bool PerfQuery::Initialize() |
| 30 | { |
| 31 | if (!CreateQueryPool()) |
| 32 | { |
| 33 | PanicAlertFmt("Failed to create query pool"); |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | // Vulkan requires query pools to be reset after creation |
| 38 | ResetQuery(); |
| 39 | |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | void PerfQuery::EnableQuery(PerfQueryGroup group) |
| 44 | { |
| 45 | // Block if there are no free slots. |
| 46 | // Otherwise, try to keep half of them available. |
| 47 | const u32 query_count = m_query_count.load(std::memory_order_relaxed); |
| 48 | if (query_count > m_query_buffer.size() / 2) |
| 49 | PartialFlush(query_count == PERF_QUERY_BUFFER_SIZE); |
| 50 | |
| 51 | // Ensure command buffer is ready to go before beginning the query, that way we don't submit |
| 52 | // a buffer with open queries. |
| 53 | StateTracker::GetInstance()->Bind(); |
| 54 | |
| 55 | if (group == PQG_ZCOMP_ZCOMPLOC || group == PQG_ZCOMP) |
| 56 | { |
| 57 | ActiveQuery& entry = m_query_buffer[m_query_next_pos]; |
| 58 | DEBUG_ASSERT(!entry.has_value); |
| 59 | entry.has_value = true; |
| 60 | entry.query_group = group; |
| 61 | |
| 62 | // Use precise queries if supported, otherwise boolean (which will be incorrect). |
| 63 | VkQueryControlFlags flags = |
| 64 | g_vulkan_context->SupportsPreciseOcclusionQueries() ? VK_QUERY_CONTROL_PRECISE_BIT : 0; |
| 65 | |
| 66 | // Ensure the query starts within a render pass. |
| 67 | StateTracker::GetInstance()->BeginRenderPass(); |
| 68 | vkCmdBeginQuery(g_command_buffer_mgr->GetCurrentCommandBuffer(), m_query_pool, m_query_next_pos, |
| 69 | flags); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void PerfQuery::DisableQuery(PerfQueryGroup group) |
| 74 | { |
| 75 | if (group == PQG_ZCOMP_ZCOMPLOC || group == PQG_ZCOMP) |
| 76 | { |
| 77 | vkCmdEndQuery(g_command_buffer_mgr->GetCurrentCommandBuffer(), m_query_pool, m_query_next_pos); |
| 78 | ActiveQuery& entry = m_query_buffer[m_query_next_pos]; |
| 79 | entry.fence_counter = g_command_buffer_mgr->GetCurrentFenceCounter(); |
| 80 | |
| 81 | m_query_next_pos = (m_query_next_pos + 1) % PERF_QUERY_BUFFER_SIZE; |
| 82 | m_query_count.fetch_add(1, std::memory_order_relaxed); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void PerfQuery::ResetQuery() |
| 87 | { |
| 88 | m_query_count.store(0, std::memory_order_relaxed); |
| 89 | m_query_readback_pos = 0; |
| 90 | m_query_next_pos = 0; |
| 91 | for (size_t i = 0; i < m_results.size(); ++i) |
| 92 | m_results[i].store(0, std::memory_order_relaxed); |
| 93 | |
| 94 | // Reset entire query pool, ensuring all queries are ready to write to. |
| 95 | StateTracker::GetInstance()->EndRenderPass(); |
| 96 | vkCmdResetQueryPool(g_command_buffer_mgr->GetCurrentCommandBuffer(), m_query_pool, 0, |
| 97 | PERF_QUERY_BUFFER_SIZE); |
| 98 | |
| 99 | std::memset(m_query_buffer.data(), 0, sizeof(ActiveQuery) * m_query_buffer.size()); |
| 100 | } |
| 101 | |
| 102 | u32 PerfQuery::GetQueryResult(PerfQueryType type) |
| 103 | { |
| 104 | u32 result = 0; |
| 105 | if (type == PQ_ZCOMP_INPUT_ZCOMPLOC || type == PQ_ZCOMP_OUTPUT_ZCOMPLOC) |
| 106 | { |
| 107 | result = m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed); |
| 108 | } |
| 109 | else if (type == PQ_ZCOMP_INPUT || type == PQ_ZCOMP_OUTPUT) |
| 110 | { |
| 111 | result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed); |
| 112 | } |
| 113 | else if (type == PQ_BLEND_INPUT) |
| 114 | { |
| 115 | result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed) + |
| 116 | m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed); |
| 117 | } |
| 118 | else if (type == PQ_EFB_COPY_CLOCKS) |
| 119 | { |
| 120 | result = m_results[PQG_EFB_COPY_CLOCKS].load(std::memory_order_relaxed); |
| 121 | } |
| 122 | |
| 123 | return result / 4; |
| 124 | } |
| 125 | |
| 126 | void PerfQuery::FlushResults() |
| 127 | { |
| 128 | if (!IsFlushed()) |
| 129 | PartialFlush(true); |
| 130 | |
| 131 | ASSERT(IsFlushed()); |
| 132 | } |
| 133 | |
| 134 | bool PerfQuery::IsFlushed() const |
| 135 | { |
| 136 | return m_query_count.load(std::memory_order_relaxed) == 0; |
| 137 | } |
| 138 | |
| 139 | bool PerfQuery::CreateQueryPool() |
| 140 | { |
| 141 | VkQueryPoolCreateInfo info = { |
| 142 | VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, // VkStructureType sType |
| 143 | nullptr, // const void* pNext |
| 144 | 0, // VkQueryPoolCreateFlags flags |
| 145 | VK_QUERY_TYPE_OCCLUSION, // VkQueryType queryType |
| 146 | PERF_QUERY_BUFFER_SIZE, // uint32_t queryCount |
| 147 | 0 // VkQueryPipelineStatisticFlags pipelineStatistics; |
| 148 | }; |
| 149 | |
| 150 | VkResult res = vkCreateQueryPool(g_vulkan_context->GetDevice(), &info, nullptr, &m_query_pool); |
| 151 | if (res != VK_SUCCESS) |
| 152 | { |
| 153 | LOG_VULKAN_ERROR(res, "vkCreateQueryPool failed: "); |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | void PerfQuery::ReadbackQueries() |
| 161 | { |
| 162 | const u64 completed_fence_counter = g_command_buffer_mgr->GetCompletedFenceCounter(); |
| 163 | |
| 164 | // Need to save these since ProcessResults will modify them. |
| 165 | const u32 outstanding_queries = m_query_count.load(std::memory_order_relaxed); |
| 166 | u32 readback_count = 0; |
| 167 | for (u32 i = 0; i < outstanding_queries; i++) |
| 168 | { |
| 169 | u32 index = (m_query_readback_pos + readback_count) % PERF_QUERY_BUFFER_SIZE; |
| 170 | const ActiveQuery& entry = m_query_buffer[index]; |
| 171 | if (entry.fence_counter > completed_fence_counter) |
| 172 | break; |
| 173 | |
| 174 | // If this wrapped around, we need to flush the entries before the end of the buffer. |
| 175 | if (index < m_query_readback_pos) |
| 176 | { |
| 177 | ReadbackQueries(readback_count); |
| 178 | DEBUG_ASSERT(m_query_readback_pos == 0); |
| 179 | readback_count = 0; |
| 180 | } |
| 181 | |
| 182 | readback_count++; |
| 183 | } |
| 184 | |
| 185 | if (readback_count > 0) |
| 186 | ReadbackQueries(readback_count); |
| 187 | } |
| 188 | |
| 189 | void PerfQuery::ReadbackQueries(u32 query_count) |
| 190 | { |
| 191 | // Should be at maximum query_count queries pending. |
| 192 | ASSERT(query_count <= m_query_count.load(std::memory_order_relaxed) && |
| 193 | (m_query_readback_pos + query_count) <= PERF_QUERY_BUFFER_SIZE); |
| 194 | |
| 195 | // Read back from the GPU. |
| 196 | VkResult res = vkGetQueryPoolResults( |
| 197 | g_vulkan_context->GetDevice(), m_query_pool, m_query_readback_pos, query_count, |
| 198 | query_count * sizeof(PerfQueryDataType), m_query_result_buffer.data(), |
| 199 | sizeof(PerfQueryDataType), VK_QUERY_RESULT_WAIT_BIT); |
| 200 | if (res != VK_SUCCESS) |
| 201 | LOG_VULKAN_ERROR(res, "vkGetQueryPoolResults failed: "); |
| 202 | |
| 203 | StateTracker::GetInstance()->EndRenderPass(); |
| 204 | vkCmdResetQueryPool(g_command_buffer_mgr->GetCurrentCommandBuffer(), m_query_pool, |
| 205 | m_query_readback_pos, query_count); |
| 206 | |
| 207 | // Remove pending queries. |
| 208 | for (u32 i = 0; i < query_count; i++) |
| 209 | { |
| 210 | u32 index = (m_query_readback_pos + i) % PERF_QUERY_BUFFER_SIZE; |
| 211 | ActiveQuery& entry = m_query_buffer[index]; |
| 212 | |
| 213 | // Should have a fence associated with it (waiting for a result). |
| 214 | DEBUG_ASSERT(entry.fence_counter != 0); |
| 215 | entry.fence_counter = 0; |
| 216 | entry.has_value = false; |
| 217 | |
| 218 | // NOTE: Reported pixel metrics should be referenced to native resolution |
| 219 | u64 native_res_result = static_cast<u64>(m_query_result_buffer[i]) * EFB_WIDTH / |
| 220 | g_framebuffer_manager->GetEFBWidth() * EFB_HEIGHT / |
| 221 | g_framebuffer_manager->GetEFBHeight(); |
| 222 | if (g_ActiveConfig.iMultisamples > 1) |
| 223 | native_res_result /= g_ActiveConfig.iMultisamples; |
| 224 | m_results[entry.query_group].fetch_add(static_cast<u32>(native_res_result), |
| 225 | std::memory_order_relaxed); |
| 226 | } |
| 227 | |
| 228 | m_query_readback_pos = (m_query_readback_pos + query_count) % PERF_QUERY_BUFFER_SIZE; |
| 229 | m_query_count.fetch_sub(query_count, std::memory_order_relaxed); |
| 230 | } |
| 231 | |
| 232 | void PerfQuery::PartialFlush(bool blocking) |
| 233 | { |
| 234 | // Submit a command buffer in the background if the front query is not bound to one. |
| 235 | if (blocking || m_query_buffer[m_query_readback_pos].fence_counter == |
| 236 | g_command_buffer_mgr->GetCurrentFenceCounter()) |
| 237 | { |
| 238 | VKGfx::GetInstance()->ExecuteCommandBuffer(true, blocking); |
| 239 | } |
| 240 | |
| 241 | ReadbackQueries(); |
| 242 | } |
| 243 | } // namespace Vulkan |