2016-08-13 06:57:50 -06:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2016-09-30 21:07:50 -06:00
|
|
|
#include "VideoBackends/Vulkan/PerfQuery.h"
|
|
|
|
|
2016-08-13 06:57:50 -06:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include "Common/Assert.h"
|
2016-09-30 21:07:50 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2016-08-13 06:57:50 -06:00
|
|
|
|
|
|
|
#include "VideoBackends/Vulkan/CommandBufferManager.h"
|
2019-02-14 18:59:50 -07:00
|
|
|
#include "VideoBackends/Vulkan/Renderer.h"
|
2016-08-13 06:57:50 -06:00
|
|
|
#include "VideoBackends/Vulkan/StateTracker.h"
|
|
|
|
#include "VideoBackends/Vulkan/VulkanContext.h"
|
|
|
|
|
|
|
|
namespace Vulkan
|
|
|
|
{
|
2019-02-14 18:59:50 -07:00
|
|
|
PerfQuery::PerfQuery() = default;
|
2016-08-13 06:57:50 -06:00
|
|
|
|
|
|
|
PerfQuery::~PerfQuery()
|
|
|
|
{
|
|
|
|
if (m_query_pool != VK_NULL_HANDLE)
|
|
|
|
vkDestroyQueryPool(g_vulkan_context->GetDevice(), m_query_pool, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-10-22 04:50:36 -06:00
|
|
|
bool PerfQuery::Initialize()
|
|
|
|
{
|
2016-08-13 06:57:50 -06:00
|
|
|
if (!CreateQueryPool())
|
|
|
|
{
|
|
|
|
PanicAlert("Failed to create query pool");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfQuery::EnableQuery(PerfQueryGroup type)
|
|
|
|
{
|
|
|
|
// Block if there are no free slots.
|
2019-03-17 08:09:26 -06:00
|
|
|
// Otherwise, try to keep half of them available.
|
|
|
|
if (m_query_count > m_query_buffer.size() / 2)
|
|
|
|
PartialFlush(m_query_count == PERF_QUERY_BUFFER_SIZE);
|
2016-08-13 06:57:50 -06:00
|
|
|
|
2019-04-01 04:49:39 -06:00
|
|
|
// Ensure command buffer is ready to go before beginning the query, that way we don't submit
|
|
|
|
// a buffer with open queries.
|
|
|
|
StateTracker::GetInstance()->Bind();
|
|
|
|
|
2016-08-13 06:57:50 -06:00
|
|
|
if (type == PQG_ZCOMP_ZCOMPLOC || type == PQG_ZCOMP)
|
|
|
|
{
|
2019-03-17 08:09:26 -06:00
|
|
|
ActiveQuery& entry = m_query_buffer[m_query_next_pos];
|
|
|
|
DEBUG_ASSERT(!entry.has_value);
|
|
|
|
entry.has_value = true;
|
2016-08-13 06:57:50 -06:00
|
|
|
|
|
|
|
// Use precise queries if supported, otherwise boolean (which will be incorrect).
|
2019-03-17 08:09:26 -06:00
|
|
|
VkQueryControlFlags flags =
|
|
|
|
g_vulkan_context->SupportsPreciseOcclusionQueries() ? VK_QUERY_CONTROL_PRECISE_BIT : 0;
|
2016-08-13 06:57:50 -06:00
|
|
|
|
|
|
|
// Ensure the query starts within a render pass.
|
2016-10-22 04:50:36 -06:00
|
|
|
StateTracker::GetInstance()->BeginRenderPass();
|
2019-03-17 08:09:26 -06:00
|
|
|
vkCmdBeginQuery(g_command_buffer_mgr->GetCurrentCommandBuffer(), m_query_pool, m_query_next_pos,
|
|
|
|
flags);
|
2016-08-13 06:57:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfQuery::DisableQuery(PerfQueryGroup type)
|
|
|
|
{
|
|
|
|
if (type == PQG_ZCOMP_ZCOMPLOC || type == PQG_ZCOMP)
|
|
|
|
{
|
2019-03-17 08:09:26 -06:00
|
|
|
vkCmdEndQuery(g_command_buffer_mgr->GetCurrentCommandBuffer(), m_query_pool, m_query_next_pos);
|
|
|
|
m_query_next_pos = (m_query_next_pos + 1) % PERF_QUERY_BUFFER_SIZE;
|
|
|
|
m_query_count++;
|
2016-08-13 06:57:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfQuery::ResetQuery()
|
|
|
|
{
|
|
|
|
m_query_count = 0;
|
2019-03-17 08:09:26 -06:00
|
|
|
m_query_readback_pos = 0;
|
|
|
|
m_query_next_pos = 0;
|
2016-08-13 06:57:50 -06:00
|
|
|
std::fill_n(m_results, ArraySize(m_results), 0);
|
|
|
|
|
|
|
|
// Reset entire query pool, ensuring all queries are ready to write to.
|
2016-10-22 04:50:36 -06:00
|
|
|
StateTracker::GetInstance()->EndRenderPass();
|
2016-08-13 06:57:50 -06:00
|
|
|
vkCmdResetQueryPool(g_command_buffer_mgr->GetCurrentCommandBuffer(), m_query_pool, 0,
|
|
|
|
PERF_QUERY_BUFFER_SIZE);
|
|
|
|
|
2019-03-17 08:09:26 -06:00
|
|
|
std::memset(m_query_buffer.data(), 0, sizeof(ActiveQuery) * m_query_buffer.size());
|
2016-08-13 06:57:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 PerfQuery::GetQueryResult(PerfQueryType type)
|
|
|
|
{
|
|
|
|
u32 result = 0;
|
|
|
|
if (type == PQ_ZCOMP_INPUT_ZCOMPLOC || type == PQ_ZCOMP_OUTPUT_ZCOMPLOC)
|
|
|
|
result = m_results[PQG_ZCOMP_ZCOMPLOC];
|
|
|
|
else if (type == PQ_ZCOMP_INPUT || type == PQ_ZCOMP_OUTPUT)
|
|
|
|
result = m_results[PQG_ZCOMP];
|
|
|
|
else if (type == PQ_BLEND_INPUT)
|
|
|
|
result = m_results[PQG_ZCOMP] + m_results[PQG_ZCOMP_ZCOMPLOC];
|
|
|
|
else if (type == PQ_EFB_COPY_CLOCKS)
|
|
|
|
result = m_results[PQG_EFB_COPY_CLOCKS];
|
|
|
|
|
|
|
|
return result / 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerfQuery::FlushResults()
|
|
|
|
{
|
|
|
|
while (!IsFlushed())
|
2019-03-17 08:09:26 -06:00
|
|
|
PartialFlush(true);
|
2016-08-13 06:57:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PerfQuery::IsFlushed() const
|
|
|
|
{
|
|
|
|
return m_query_count == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PerfQuery::CreateQueryPool()
|
|
|
|
{
|
|
|
|
VkQueryPoolCreateInfo info = {
|
|
|
|
VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, // VkStructureType sType
|
|
|
|
nullptr, // const void* pNext
|
|
|
|
0, // VkQueryPoolCreateFlags flags
|
|
|
|
VK_QUERY_TYPE_OCCLUSION, // VkQueryType queryType
|
|
|
|
PERF_QUERY_BUFFER_SIZE, // uint32_t queryCount
|
|
|
|
0 // VkQueryPipelineStatisticFlags pipelineStatistics;
|
|
|
|
};
|
|
|
|
|
|
|
|
VkResult res = vkCreateQueryPool(g_vulkan_context->GetDevice(), &info, nullptr, &m_query_pool);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateQueryPool failed: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-17 08:09:26 -06:00
|
|
|
void PerfQuery::ReadbackQueries()
|
2016-08-13 06:57:50 -06:00
|
|
|
{
|
2019-03-17 08:09:26 -06:00
|
|
|
const u64 completed_fence_counter = g_command_buffer_mgr->GetCompletedFenceCounter();
|
2019-03-16 23:59:22 -06:00
|
|
|
|
2016-08-13 06:57:50 -06:00
|
|
|
// Need to save these since ProcessResults will modify them.
|
2019-03-17 08:09:26 -06:00
|
|
|
const u32 outstanding_queries = m_query_count;
|
|
|
|
u32 readback_count = 0;
|
|
|
|
for (u32 i = 0; i < outstanding_queries; i++)
|
2016-08-13 06:57:50 -06:00
|
|
|
{
|
2019-03-17 08:09:26 -06:00
|
|
|
u32 index = (m_query_readback_pos + readback_count) % PERF_QUERY_BUFFER_SIZE;
|
|
|
|
const ActiveQuery& entry = m_query_buffer[index];
|
|
|
|
if (entry.fence_counter > completed_fence_counter)
|
2016-08-13 06:57:50 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
// If this wrapped around, we need to flush the entries before the end of the buffer.
|
2019-03-17 08:09:26 -06:00
|
|
|
if (index < m_query_readback_pos)
|
2016-08-13 06:57:50 -06:00
|
|
|
{
|
2019-03-17 08:09:26 -06:00
|
|
|
ReadbackQueries(readback_count);
|
|
|
|
DEBUG_ASSERT(m_query_readback_pos == 0);
|
|
|
|
readback_count = 0;
|
2016-08-13 06:57:50 -06:00
|
|
|
}
|
2019-03-17 08:09:26 -06:00
|
|
|
|
|
|
|
readback_count++;
|
2016-08-13 06:57:50 -06:00
|
|
|
}
|
|
|
|
|
2019-03-17 08:09:26 -06:00
|
|
|
if (readback_count > 0)
|
|
|
|
ReadbackQueries(readback_count);
|
2016-08-13 06:57:50 -06:00
|
|
|
}
|
|
|
|
|
2019-03-17 08:09:26 -06:00
|
|
|
void PerfQuery::ReadbackQueries(u32 query_count)
|
2016-08-13 06:57:50 -06:00
|
|
|
{
|
|
|
|
// Should be at maximum query_count queries pending.
|
2019-03-17 08:09:26 -06:00
|
|
|
ASSERT(query_count <= m_query_count &&
|
|
|
|
(m_query_readback_pos + query_count) <= PERF_QUERY_BUFFER_SIZE);
|
|
|
|
|
|
|
|
// Read back from the GPU.
|
|
|
|
VkResult res =
|
|
|
|
vkGetQueryPoolResults(g_vulkan_context->GetDevice(), m_query_pool, m_query_readback_pos,
|
|
|
|
query_count, query_count * sizeof(PerfQueryDataType),
|
|
|
|
m_query_result_buffer.data(), sizeof(PerfQueryDataType), 0);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
LOG_VULKAN_ERROR(res, "vkGetQueryPoolResults failed: ");
|
2016-08-13 06:57:50 -06:00
|
|
|
|
|
|
|
// Remove pending queries.
|
|
|
|
for (u32 i = 0; i < query_count; i++)
|
|
|
|
{
|
2019-03-17 08:09:26 -06:00
|
|
|
u32 index = (m_query_readback_pos + i) % PERF_QUERY_BUFFER_SIZE;
|
2016-08-13 06:57:50 -06:00
|
|
|
ActiveQuery& entry = m_query_buffer[index];
|
|
|
|
|
|
|
|
// Should have a fence associated with it (waiting for a result).
|
2019-03-17 08:09:26 -06:00
|
|
|
DEBUG_ASSERT(entry.fence_counter != 0);
|
2019-03-16 23:59:22 -06:00
|
|
|
entry.fence_counter = 0;
|
2019-03-17 08:09:26 -06:00
|
|
|
entry.has_value = false;
|
2016-08-13 06:57:50 -06:00
|
|
|
|
|
|
|
// NOTE: Reported pixel metrics should be referenced to native resolution
|
|
|
|
m_results[entry.query_type] +=
|
2019-03-17 08:09:26 -06:00
|
|
|
static_cast<u32>(static_cast<u64>(m_query_result_buffer[i]) * EFB_WIDTH /
|
|
|
|
g_renderer->GetTargetWidth() * EFB_HEIGHT / g_renderer->GetTargetHeight());
|
2016-08-13 06:57:50 -06:00
|
|
|
}
|
|
|
|
|
2019-03-17 08:09:26 -06:00
|
|
|
m_query_readback_pos = (m_query_readback_pos + query_count) % PERF_QUERY_BUFFER_SIZE;
|
2016-08-13 06:57:50 -06:00
|
|
|
m_query_count -= query_count;
|
|
|
|
}
|
|
|
|
|
2019-03-17 08:09:26 -06:00
|
|
|
void PerfQuery::PartialFlush(bool blocking)
|
2016-08-13 06:57:50 -06:00
|
|
|
{
|
|
|
|
// Submit a command buffer in the background if the front query is not bound to one.
|
2019-03-17 08:09:26 -06:00
|
|
|
if (blocking || m_query_buffer[m_query_readback_pos].fence_counter ==
|
|
|
|
g_command_buffer_mgr->GetCurrentFenceCounter())
|
|
|
|
{
|
|
|
|
Renderer::GetInstance()->ExecuteCommandBuffer(true, blocking);
|
|
|
|
}
|
2019-03-16 23:59:22 -06:00
|
|
|
|
2019-03-17 08:09:26 -06:00
|
|
|
ReadbackQueries();
|
2016-08-13 06:57:50 -06:00
|
|
|
}
|
2019-02-14 18:59:50 -07:00
|
|
|
} // namespace Vulkan
|