1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2024-11-29 19:24:10 +01:00

[dxvk] Refactor query management

Moved all query-related state tracking and management into a
separate class. This allows for new query types to be added
in the future, and makes less dodgy assumptions about the
current state when beginning or ending a query.
This commit is contained in:
Philip Rebohle 2018-08-31 15:34:53 +02:00
parent 9fd30cf5bf
commit 657093c14e
No known key found for this signature in database
GPG Key ID: C8CC613427A31C99
8 changed files with 296 additions and 137 deletions

View File

@ -16,7 +16,8 @@ namespace dxvk {
m_pipeMgr (pipelineManager),
m_metaClear (metaClearObjects),
m_metaMipGen (metaMipGenObjects),
m_metaResolve (metaResolveObjects) { }
m_metaResolve (metaResolveObjects),
m_queries (device->vkd()) { }
DxvkContext::~DxvkContext() {
@ -50,9 +51,7 @@ namespace dxvk {
Rc<DxvkCommandList> DxvkContext::endRecording() {
this->spillRenderPass();
this->trackQueryPool(m_queryPools[VK_QUERY_TYPE_OCCLUSION]);
this->trackQueryPool(m_queryPools[VK_QUERY_TYPE_PIPELINE_STATISTICS]);
this->trackQueryPool(m_queryPools[VK_QUERY_TYPE_TIMESTAMP]);
m_queries.trackQueryPools(m_cmd);
m_barriers.recordCommands(m_cmd);
@ -63,31 +62,13 @@ namespace dxvk {
void DxvkContext::beginQuery(const DxvkQueryRevision& query) {
query.query->beginRecording(query.revision);
if (m_flags.test(DxvkContextFlag::GpRenderPassBound)) {
DxvkQueryHandle handle = this->allocQuery(query);
m_cmd->cmdBeginQuery(
handle.queryPool,
handle.queryId,
handle.flags);
}
this->insertActiveQuery(query);
m_queries.enableQuery(m_cmd, query);
}
void DxvkContext::endQuery(const DxvkQueryRevision& query) {
if (m_flags.test(DxvkContextFlag::GpRenderPassBound)) {
DxvkQueryHandle handle = query.query->getHandle();
m_cmd->cmdEndQuery(
handle.queryPool,
handle.queryId);
}
m_queries.disableQuery(m_cmd, query);
query.query->endRecording(query.revision);
this->eraseActiveQuery(query);
}
@ -1631,7 +1612,7 @@ namespace dxvk {
void DxvkContext::writeTimestamp(const DxvkQueryRevision& query) {
DxvkQueryHandle handle = this->allocQuery(query);
DxvkQueryHandle handle = m_queries.allocQuery(m_cmd, query);
m_cmd->cmdWriteTimestamp(
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
@ -1803,7 +1784,10 @@ namespace dxvk {
m_state.om.renderTargets,
m_state.om.renderPassOps);
this->beginActiveQueries();
// Begin occlusion queries
m_queries.beginQueries(m_cmd, {
VK_QUERY_TYPE_OCCLUSION,
VK_QUERY_TYPE_PIPELINE_STATISTICS });
}
}
@ -1814,7 +1798,11 @@ namespace dxvk {
if (m_flags.test(DxvkContextFlag::GpRenderPassBound)) {
m_flags.clr(DxvkContextFlag::GpRenderPassBound);
this->endActiveQueries();
m_queries.endQueries(m_cmd, {
VK_QUERY_TYPE_OCCLUSION,
VK_QUERY_TYPE_PIPELINE_STATISTICS });
this->renderPassUnbindFramebuffer();
}
}
@ -2514,77 +2502,4 @@ namespace dxvk {
}
}
DxvkQueryHandle DxvkContext::allocQuery(const DxvkQueryRevision& query) {
const VkQueryType queryType = query.query->type();
DxvkQueryHandle queryHandle = DxvkQueryHandle();
Rc<DxvkQueryPool> queryPool = m_queryPools[queryType];
if (queryPool != nullptr)
queryHandle = queryPool->allocQuery(query);
if (queryHandle.queryPool == VK_NULL_HANDLE) {
if (queryPool != nullptr)
this->trackQueryPool(queryPool);
m_queryPools[queryType] = m_device->createQueryPool(queryType, MaxNumQueryCountPerPool);
queryPool = m_queryPools[queryType];
queryPool->reset(m_cmd);
queryHandle = queryPool->allocQuery(query);
}
return queryHandle;
}
void DxvkContext::trackQueryPool(const Rc<DxvkQueryPool>& pool) {
if (pool != nullptr) {
DxvkQueryRange range = pool->getActiveQueryRange();
if (range.queryCount > 0)
m_cmd->trackQueryRange(std::move(range));
}
}
void DxvkContext::beginActiveQueries() {
for (const DxvkQueryRevision& query : m_activeQueries) {
DxvkQueryHandle handle = this->allocQuery(query);
m_cmd->cmdBeginQuery(
handle.queryPool,
handle.queryId,
handle.flags);
}
}
void DxvkContext::endActiveQueries() {
for (const DxvkQueryRevision& query : m_activeQueries) {
DxvkQueryHandle handle = query.query->getHandle();
m_cmd->cmdEndQuery(
handle.queryPool,
handle.queryId);
}
}
void DxvkContext::insertActiveQuery(const DxvkQueryRevision& query) {
m_activeQueries.push_back(query);
}
void DxvkContext::eraseActiveQuery(const DxvkQueryRevision& query) {
for (auto i = m_activeQueries.begin(); i != m_activeQueries.end(); i++) {
if (i->query == query.query && i->revision == query.revision) {
m_activeQueries.erase(i);
return;
}
}
}
}

View File

@ -12,6 +12,7 @@
#include "dxvk_pipecache.h"
#include "dxvk_pipemanager.h"
#include "dxvk_query.h"
#include "dxvk_query_manager.h"
#include "dxvk_query_pool.h"
#include "dxvk_util.h"
@ -645,8 +646,7 @@ namespace dxvk {
DxvkBarrierSet m_barriers;
DxvkBarrierSet m_transitions;
// TODO implement this properly...
Rc<DxvkQueryPool> m_queryPools[3] = { nullptr, nullptr, nullptr };
DxvkQueryManager m_queries;
VkPipeline m_gpActivePipeline = VK_NULL_HANDLE;
VkPipeline m_cpActivePipeline = VK_NULL_HANDLE;
@ -654,8 +654,6 @@ namespace dxvk {
VkDescriptorSet m_gpSet = VK_NULL_HANDLE;
VkDescriptorSet m_cpSet = VK_NULL_HANDLE;
std::vector<DxvkQueryRevision> m_activeQueries;
std::array<DxvkShaderResourceSlot, MaxNumResourceSlots> m_rc;
std::array<DxvkDescriptorInfo, MaxNumActiveBindings> m_descInfos;
std::array<uint32_t, MaxNumActiveBindings> m_descOffsets;
@ -731,22 +729,6 @@ namespace dxvk {
void commitComputeInitBarriers();
void commitComputePostBarriers();
DxvkQueryHandle allocQuery(
const DxvkQueryRevision& query);
void trackQueryPool(
const Rc<DxvkQueryPool>& pool);
void beginActiveQueries();
void endActiveQueries();
void insertActiveQuery(
const DxvkQueryRevision& query);
void eraseActiveQuery(
const DxvkQueryRevision& query);
Rc<DxvkBuffer> getTransferBuffer(VkDeviceSize size);
};

View File

@ -166,13 +166,6 @@ namespace dxvk {
}
Rc<DxvkQueryPool> DxvkDevice::createQueryPool(
VkQueryType queryType,
uint32_t queryCount) {
return new DxvkQueryPool(m_vkd, queryType, queryCount);
}
Rc<DxvkSampler> DxvkDevice::createSampler(
const DxvkSamplerCreateInfo& createInfo) {
return new DxvkSampler(m_vkd, createInfo);

View File

@ -242,14 +242,6 @@ namespace dxvk {
const Rc<DxvkImage>& image,
const DxvkImageViewCreateInfo& createInfo);
/**
* \brief Creates a query pool
* \param [in] queryType Query type
*/
Rc<DxvkQueryPool> createQueryPool(
VkQueryType queryType,
uint32_t queryCount);
/**
* \brief Creates a sampler object
*

View File

@ -0,0 +1,155 @@
#include "dxvk_query_manager.h"
#include "dxvk_query_pool.h"
namespace dxvk {
DxvkQueryManager::DxvkQueryManager(const Rc<vk::DeviceFn>& vkd)
: m_vkd(vkd) {
}
DxvkQueryManager::~DxvkQueryManager() {
}
DxvkQueryHandle DxvkQueryManager::allocQuery(
const Rc<DxvkCommandList>& cmd,
const DxvkQueryRevision& query) {
const VkQueryType queryType = query.query->type();
DxvkQueryHandle queryHandle = DxvkQueryHandle();
Rc<DxvkQueryPool>& queryPool = this->getQueryPool(queryType);
if (queryPool != nullptr)
queryHandle = queryPool->allocQuery(query);
if (queryHandle.queryPool == VK_NULL_HANDLE) {
if (queryPool != nullptr)
this->trackQueryPool(cmd, queryPool);
queryPool = new DxvkQueryPool(m_vkd, queryType, MaxNumQueryCountPerPool);
queryPool->reset(cmd);
queryHandle = queryPool->allocQuery(query);
}
return queryHandle;
}
void DxvkQueryManager::enableQuery(
const Rc<DxvkCommandList>& cmd,
const DxvkQueryRevision& query) {
m_activeQueries.push_back(query);
if (m_activeTypes.test(query.query->type())) {
DxvkQueryHandle handle = this->allocQuery(cmd, query);
cmd->cmdBeginQuery(
handle.queryPool,
handle.queryId,
handle.flags);
}
}
void DxvkQueryManager::disableQuery(
const Rc<DxvkCommandList>& cmd,
const DxvkQueryRevision& query) {
auto iter = m_activeQueries.begin();
while (iter != m_activeQueries.end()) {
if (iter->query == query.query
&& iter->revision == query.revision)
break;
iter++;
}
if (iter != m_activeQueries.end()) {
if (m_activeTypes.test(iter->query->type())) {
DxvkQueryHandle handle = iter->query->getHandle();
cmd->cmdEndQuery(
handle.queryPool,
handle.queryId);
}
m_activeQueries.erase(iter);
}
}
void DxvkQueryManager::beginQueries(
const Rc<DxvkCommandList>& cmd,
DxvkQueryTypeFlags types) {
m_activeTypes.set(types);
for (const DxvkQueryRevision& query : m_activeQueries) {
if (types.test(query.query->type())) {
DxvkQueryHandle handle = this->allocQuery(cmd, query);
cmd->cmdBeginQuery(
handle.queryPool,
handle.queryId,
handle.flags);
}
}
}
void DxvkQueryManager::endQueries(
const Rc<DxvkCommandList>& cmd,
DxvkQueryTypeFlags types) {
m_activeTypes.clr(types);
for (const DxvkQueryRevision& query : m_activeQueries) {
if (types.test(query.query->type())) {
DxvkQueryHandle handle = query.query->getHandle();
cmd->cmdEndQuery(
handle.queryPool,
handle.queryId);
}
}
}
void DxvkQueryManager::trackQueryPools(const Rc<DxvkCommandList>& cmd) {
this->trackQueryPool(cmd, m_occlusion);
this->trackQueryPool(cmd, m_pipeStats);
this->trackQueryPool(cmd, m_timestamp);
}
void DxvkQueryManager::trackQueryPool(
const Rc<DxvkCommandList>& cmd,
const Rc<DxvkQueryPool>& pool) {
if (pool != nullptr) {
DxvkQueryRange range = pool->getActiveQueryRange();
if (range.queryCount > 0)
cmd->trackQueryRange(std::move(range));
}
}
Rc<DxvkQueryPool>& DxvkQueryManager::getQueryPool(VkQueryType type) {
switch (type) {
case VK_QUERY_TYPE_OCCLUSION:
return m_occlusion;
case VK_QUERY_TYPE_PIPELINE_STATISTICS:
return m_pipeStats;
case VK_QUERY_TYPE_TIMESTAMP:
return m_timestamp;
default:
throw DxvkError("DXVK: Invalid query type");
}
}
}

View File

@ -0,0 +1,118 @@
#pragma once
#include <vector>
#include "dxvk_cmdlist.h"
#include "dxvk_query.h"
#include "dxvk_query_pool.h"
namespace dxvk {
using DxvkQueryTypeFlags = Flags<VkQueryType>;
/**
* \brief Query manager
*
* Manages Vulkan query pools
* and the current query state.
*/
class DxvkQueryManager {
public:
DxvkQueryManager(const Rc<vk::DeviceFn>& vkd);
~DxvkQueryManager();
/**
* \brief Allocates a Vulkan query
*
* Creates a query pool of the correct type if
* necessary, and allocates one query from it.
* \param [in] cmd The context's command list
* \param [in] query The DXVK query revision
* \returns Allocated query handle
*/
DxvkQueryHandle allocQuery(
const Rc<DxvkCommandList>& cmd,
const DxvkQueryRevision& query);
/**
* \brief Enables a query
*
* Starts tracking a query. Depending on the
* query type, unterlying Vulkan queries will
* begin and end on render pass boundaries.
* \param [in] cmd The context's command list
* \param [in] query The query to enable
*/
void enableQuery(
const Rc<DxvkCommandList>& cmd,
const DxvkQueryRevision& query);
/**
* \brief Disables a query
*
* Ends the query if it is currently active,
* and stops tracking any further state changes.
* \param [in] cmd The context's command list
* \param [in] query The query to enable
*/
void disableQuery(
const Rc<DxvkCommandList>& cmd,
const DxvkQueryRevision& query);
/**
* \brief Begins active queries
*
* Creates a Vulkan query for each enabled
* query of the given types and begins them.
* \param [in] cmd The context's command list
* \param [in] types Query types to begin
*/
void beginQueries(
const Rc<DxvkCommandList>& cmd,
DxvkQueryTypeFlags types);
/**
* \brief Ends active queries
*
* Ends active queries of the given types.
* \param [in] cmd The context's command list
* \param [in] types Query types to begin
*/
void endQueries(
const Rc<DxvkCommandList>& cmd,
DxvkQueryTypeFlags types);
/**
* \brief Tracks query pools
*
* Adds all current non-empty query pools to
* the query tracker of the given command list.
* \param [in] cmd The context's command list
*/
void trackQueryPools(
const Rc<DxvkCommandList>& cmd);
private:
const Rc<vk::DeviceFn> m_vkd;
DxvkQueryTypeFlags m_activeTypes;
Rc<DxvkQueryPool> m_occlusion;
Rc<DxvkQueryPool> m_pipeStats;
Rc<DxvkQueryPool> m_timestamp;
std::vector<DxvkQueryRevision> m_activeQueries;
void trackQueryPool(
const Rc<DxvkCommandList>& cmd,
const Rc<DxvkQueryPool>& pool);
Rc<DxvkQueryPool>& getQueryPool(
VkQueryType type);
};
}

View File

@ -11,6 +11,9 @@ namespace dxvk {
/**
* \brief Query range
*
* Stores an index of a query in a query pool,
* and the number of queries to track.
*/
struct DxvkQueryRange {
Rc<DxvkQueryPool> queryPool;

View File

@ -64,6 +64,7 @@ dxvk_src = files([
'dxvk_pipemanager.cpp',
'dxvk_query.cpp',
'dxvk_query_pool.cpp',
'dxvk_query_manager.cpp',
'dxvk_query_tracker.cpp',
'dxvk_queue.cpp',
'dxvk_renderpass.cpp',