Merge pull request #11286 from K0bin/vk-query-fix

VideoBackends: Query fixes and cleanups
This commit is contained in:
JMC47
2022-12-19 03:15:48 -05:00
committed by GitHub
9 changed files with 75 additions and 76 deletions

View File

@ -73,11 +73,6 @@ void StagingBuffer::InvalidateGPUCache(VkCommandBuffer command_buffer,
VkPipelineStageFlagBits dest_pipeline_stage,
VkDeviceSize offset, VkDeviceSize size)
{
VkMemoryPropertyFlags flags = 0;
vmaGetAllocationMemoryProperties(g_vulkan_context->GetMemoryAllocator(), m_alloc, &flags);
if (flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) [[likely]]
return;
ASSERT((offset + size) <= m_size || (offset < m_size && size == VK_WHOLE_SIZE));
BufferMemoryBarrier(command_buffer, m_buffer, VK_ACCESS_HOST_WRITE_BIT, dest_access_flags, offset,
size, VK_PIPELINE_STAGE_HOST_BIT, dest_pipeline_stage);
@ -88,25 +83,15 @@ void StagingBuffer::PrepareForGPUWrite(VkCommandBuffer command_buffer,
VkPipelineStageFlagBits dst_pipeline_stage,
VkDeviceSize offset, VkDeviceSize size)
{
VkMemoryPropertyFlags flags = 0;
vmaGetAllocationMemoryProperties(g_vulkan_context->GetMemoryAllocator(), m_alloc, &flags);
if (flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) [[likely]]
return;
ASSERT((offset + size) <= m_size || (offset < m_size && size == VK_WHOLE_SIZE));
BufferMemoryBarrier(command_buffer, m_buffer, 0, dst_access_flags, offset, size,
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, dst_pipeline_stage);
BufferMemoryBarrier(command_buffer, m_buffer, VK_ACCESS_MEMORY_WRITE_BIT, dst_access_flags,
offset, size, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, dst_pipeline_stage);
}
void StagingBuffer::FlushGPUCache(VkCommandBuffer command_buffer, VkAccessFlagBits src_access_flags,
VkPipelineStageFlagBits src_pipeline_stage, VkDeviceSize offset,
VkDeviceSize size)
{
VkMemoryPropertyFlags flags = 0;
vmaGetAllocationMemoryProperties(g_vulkan_context->GetMemoryAllocator(), m_alloc, &flags);
if (flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) [[likely]]
return;
ASSERT((offset + size) <= m_size || (offset < m_size && size == VK_WHOLE_SIZE));
BufferMemoryBarrier(command_buffer, m_buffer, src_access_flags, VK_ACCESS_HOST_READ_BIT, offset,
size, src_pipeline_stage, VK_PIPELINE_STAGE_HOST_BIT);

View File

@ -35,10 +35,13 @@ bool PerfQuery::Initialize()
return false;
}
// Vulkan requires query pools to be reset after creation
ResetQuery();
return true;
}
void PerfQuery::EnableQuery(PerfQueryGroup type)
void PerfQuery::EnableQuery(PerfQueryGroup group)
{
// Block if there are no free slots.
// Otherwise, try to keep half of them available.
@ -50,11 +53,12 @@ void PerfQuery::EnableQuery(PerfQueryGroup type)
// a buffer with open queries.
StateTracker::GetInstance()->Bind();
if (type == PQG_ZCOMP_ZCOMPLOC || type == PQG_ZCOMP)
if (group == PQG_ZCOMP_ZCOMPLOC || group == PQG_ZCOMP)
{
ActiveQuery& entry = m_query_buffer[m_query_next_pos];
DEBUG_ASSERT(!entry.has_value);
entry.has_value = true;
entry.query_group = group;
// Use precise queries if supported, otherwise boolean (which will be incorrect).
VkQueryControlFlags flags =
@ -67,11 +71,14 @@ void PerfQuery::EnableQuery(PerfQueryGroup type)
}
}
void PerfQuery::DisableQuery(PerfQueryGroup type)
void PerfQuery::DisableQuery(PerfQueryGroup group)
{
if (type == PQG_ZCOMP_ZCOMPLOC || type == PQG_ZCOMP)
if (group == PQG_ZCOMP_ZCOMPLOC || group == PQG_ZCOMP)
{
vkCmdEndQuery(g_command_buffer_mgr->GetCurrentCommandBuffer(), m_query_pool, m_query_next_pos);
ActiveQuery& entry = m_query_buffer[m_query_next_pos];
entry.fence_counter = g_command_buffer_mgr->GetCurrentFenceCounter();
m_query_next_pos = (m_query_next_pos + 1) % PERF_QUERY_BUFFER_SIZE;
m_query_count.fetch_add(1, std::memory_order_relaxed);
}
@ -119,8 +126,10 @@ u32 PerfQuery::GetQueryResult(PerfQueryType type)
void PerfQuery::FlushResults()
{
while (!IsFlushed())
if (!IsFlushed())
PartialFlush(true);
ASSERT(IsFlushed());
}
bool PerfQuery::IsFlushed() const
@ -185,13 +194,17 @@ void PerfQuery::ReadbackQueries(u32 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);
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), VK_QUERY_RESULT_WAIT_BIT);
if (res != VK_SUCCESS)
LOG_VULKAN_ERROR(res, "vkGetQueryPoolResults failed: ");
StateTracker::GetInstance()->EndRenderPass();
vkCmdResetQueryPool(g_command_buffer_mgr->GetCurrentCommandBuffer(), m_query_pool,
m_query_readback_pos, query_count);
// Remove pending queries.
for (u32 i = 0; i < query_count; i++)
{
@ -207,8 +220,8 @@ void PerfQuery::ReadbackQueries(u32 query_count)
const u64 native_res_result = static_cast<u64>(m_query_result_buffer[i]) * EFB_WIDTH /
g_renderer->GetTargetWidth() * EFB_HEIGHT /
g_renderer->GetTargetHeight();
m_results[entry.query_type].fetch_add(static_cast<u32>(native_res_result),
std::memory_order_relaxed);
m_results[entry.query_group].fetch_add(static_cast<u32>(native_res_result),
std::memory_order_relaxed);
}
m_query_readback_pos = (m_query_readback_pos + query_count) % PERF_QUERY_BUFFER_SIZE;

View File

@ -22,8 +22,8 @@ public:
bool Initialize();
void EnableQuery(PerfQueryGroup type) override;
void DisableQuery(PerfQueryGroup type) override;
void EnableQuery(PerfQueryGroup group) override;
void DisableQuery(PerfQueryGroup group) override;
void ResetQuery() override;
u32 GetQueryResult(PerfQueryType type) override;
void FlushResults() override;
@ -40,7 +40,7 @@ private:
struct ActiveQuery
{
u64 fence_counter;
PerfQueryType query_type;
PerfQueryGroup query_group;
bool has_value;
};