From d504acad6800e6d18f69756572a2d9e43d188ce1 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Tue, 30 Apr 2019 21:01:55 +0200 Subject: [PATCH] - add VulkanQueryPool and QueryPoolBuilder --- src/rendering/vulkan/system/vk_builders.h | 36 ++++++++++++++ src/rendering/vulkan/system/vk_objects.h | 57 +++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/src/rendering/vulkan/system/vk_builders.h b/src/rendering/vulkan/system/vk_builders.h index c2f221865..5353d3153 100644 --- a/src/rendering/vulkan/system/vk_builders.h +++ b/src/rendering/vulkan/system/vk_builders.h @@ -159,6 +159,19 @@ private: VkDescriptorPoolCreateInfo poolInfo = {}; }; +class QueryPoolBuilder +{ +public: + QueryPoolBuilder(); + + void setQueryType(VkQueryType type, int count, VkQueryPipelineStatisticFlags pipelineStatistics = 0); + + std::unique_ptr create(VulkanDevice *device); + +private: + VkQueryPoolCreateInfo poolInfo = {}; +}; + class FramebufferBuilder { public: @@ -641,6 +654,29 @@ inline std::unique_ptr DescriptorPoolBuilder::create(Vulka ///////////////////////////////////////////////////////////////////////////// +inline QueryPoolBuilder::QueryPoolBuilder() +{ + poolInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO; +} + +inline void QueryPoolBuilder::setQueryType(VkQueryType type, int count, VkQueryPipelineStatisticFlags pipelineStatistics) +{ + poolInfo.queryType = type; + poolInfo.queryCount = count; + poolInfo.pipelineStatistics = pipelineStatistics; +} + +inline std::unique_ptr QueryPoolBuilder::create(VulkanDevice *device) +{ + VkQueryPool queryPool; + VkResult result = vkCreateQueryPool(device->device, &poolInfo, nullptr, &queryPool); + if (result != VK_SUCCESS) + I_FatalError("Could not create query pool"); + return std::make_unique(device, queryPool); +} + +///////////////////////////////////////////////////////////////////////////// + inline FramebufferBuilder::FramebufferBuilder() { framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; diff --git a/src/rendering/vulkan/system/vk_objects.h b/src/rendering/vulkan/system/vk_objects.h index af94143ad..3ca97f7bf 100644 --- a/src/rendering/vulkan/system/vk_objects.h +++ b/src/rendering/vulkan/system/vk_objects.h @@ -202,6 +202,22 @@ private: VulkanDescriptorPool &operator=(const VulkanDescriptorPool &) = delete; }; +class VulkanQueryPool +{ +public: + VulkanQueryPool(VulkanDevice *device, VkQueryPool pool); + ~VulkanQueryPool(); + + void SetDebugName(const char *name) { device->SetDebugObjectName(name, (uint64_t)pool, VK_OBJECT_TYPE_QUERY_POOL); } + + VulkanDevice *device = nullptr; + VkQueryPool pool = VK_NULL_HANDLE; + +private: + VulkanQueryPool(const VulkanQueryPool &) = delete; + VulkanQueryPool &operator=(const VulkanQueryPool &) = delete; +}; + class VulkanPipeline { public: @@ -317,10 +333,15 @@ public: void resetEvent(VkEvent event, VkPipelineStageFlags stageMask); void waitEvents(uint32_t eventCount, const VkEvent* pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers, uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier* pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier* pImageMemoryBarriers); void pipelineBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers, uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier* pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier* pImageMemoryBarriers); + void beginQuery(VulkanQueryPool *queryPool, uint32_t query, VkQueryControlFlags flags); void beginQuery(VkQueryPool queryPool, uint32_t query, VkQueryControlFlags flags); + void endQuery(VulkanQueryPool *queryPool, uint32_t query); void endQuery(VkQueryPool queryPool, uint32_t query); + void resetQueryPool(VulkanQueryPool *queryPool, uint32_t firstQuery, uint32_t queryCount); void resetQueryPool(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount); + void writeTimestamp(VkPipelineStageFlagBits pipelineStage, VulkanQueryPool *queryPool, uint32_t query); void writeTimestamp(VkPipelineStageFlagBits pipelineStage, VkQueryPool queryPool, uint32_t query); + void copyQueryPoolResults(VulkanQueryPool *queryPool, uint32_t firstQuery, uint32_t queryCount, VulkanBuffer *dstBuffer, VkDeviceSize dstOffset, VkDeviceSize stride, VkQueryResultFlags flags); void copyQueryPoolResults(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize stride, VkQueryResultFlags flags); void pushConstants(VulkanPipelineLayout *layout, VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size, const void* pValues); void pushConstants(VkPipelineLayout layout, VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size, const void* pValues); @@ -775,26 +796,51 @@ inline void VulkanCommandBuffer::pipelineBarrier(VkPipelineStageFlags srcStageMa vkCmdPipelineBarrier(buffer, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers); } +inline void VulkanCommandBuffer::beginQuery(VulkanQueryPool *queryPool, uint32_t query, VkQueryControlFlags flags) +{ + beginQuery(queryPool->pool, query, flags); +} + inline void VulkanCommandBuffer::beginQuery(VkQueryPool queryPool, uint32_t query, VkQueryControlFlags flags) { vkCmdBeginQuery(buffer, queryPool, query, flags); } +inline void VulkanCommandBuffer::endQuery(VulkanQueryPool *queryPool, uint32_t query) +{ + endQuery(queryPool->pool, query); +} + inline void VulkanCommandBuffer::endQuery(VkQueryPool queryPool, uint32_t query) { vkCmdEndQuery(buffer, queryPool, query); } +inline void VulkanCommandBuffer::resetQueryPool(VulkanQueryPool *queryPool, uint32_t firstQuery, uint32_t queryCount) +{ + resetQueryPool(queryPool->pool, firstQuery, queryCount); +} + inline void VulkanCommandBuffer::resetQueryPool(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount) { vkCmdResetQueryPool(buffer, queryPool, firstQuery, queryCount); } +inline void VulkanCommandBuffer::writeTimestamp(VkPipelineStageFlagBits pipelineStage, VulkanQueryPool *queryPool, uint32_t query) +{ + writeTimestamp(pipelineStage, queryPool, query); +} + inline void VulkanCommandBuffer::writeTimestamp(VkPipelineStageFlagBits pipelineStage, VkQueryPool queryPool, uint32_t query) { vkCmdWriteTimestamp(buffer, pipelineStage, queryPool, query); } +inline void VulkanCommandBuffer::copyQueryPoolResults(VulkanQueryPool *queryPool, uint32_t firstQuery, uint32_t queryCount, VulkanBuffer *dstBuffer, VkDeviceSize dstOffset, VkDeviceSize stride, VkQueryResultFlags flags) +{ + copyQueryPoolResults(queryPool->pool, firstQuery, queryCount, dstBuffer->buffer, dstOffset, stride, flags); +} + inline void VulkanCommandBuffer::copyQueryPoolResults(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize stride, VkQueryResultFlags flags) { vkCmdCopyQueryPoolResults(buffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset, stride, flags); @@ -902,6 +948,17 @@ inline std::unique_ptr VulkanDescriptorPool::allocate(Vulka ///////////////////////////////////////////////////////////////////////////// +inline VulkanQueryPool::VulkanQueryPool(VulkanDevice *device, VkQueryPool pool) : device(device), pool(pool) +{ +} + +inline VulkanQueryPool::~VulkanQueryPool() +{ + vkDestroyQueryPool(device->device, pool, nullptr); +} + +///////////////////////////////////////////////////////////////////////////// + inline VulkanFramebuffer::VulkanFramebuffer(VulkanDevice *device, VkFramebuffer framebuffer) : device(device), framebuffer(framebuffer) { }