From f6b979c8e4d343693cb361c9b7560f126f3f5d20 Mon Sep 17 00:00:00 2001 From: Powei Feng Date: Wed, 27 Sep 2023 15:01:33 -0700 Subject: [PATCH] vk: refactor VulkanResources.h (#7206) - Make the size of FixedSizedVulkanResources declarable - Rename `acquire` --- filament/backend/src/vulkan/VulkanCommands.h | 2 +- filament/backend/src/vulkan/VulkanHandles.h | 5 +-- .../src/vulkan/VulkanPipelineCache.cpp | 2 +- filament/backend/src/vulkan/VulkanResources.h | 31 ++++++++++++++----- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/filament/backend/src/vulkan/VulkanCommands.h b/filament/backend/src/vulkan/VulkanCommands.h index 660e3e106c5..29de6f772fc 100644 --- a/filament/backend/src/vulkan/VulkanCommands.h +++ b/filament/backend/src/vulkan/VulkanCommands.h @@ -83,7 +83,7 @@ struct VulkanCommandBuffer { } inline void acquire(VulkanAcquireOnlyResourceManager* srcResources) { - mResourceManager.acquire(srcResources); + mResourceManager.acquireAll(srcResources); } inline void reset() { diff --git a/filament/backend/src/vulkan/VulkanHandles.h b/filament/backend/src/vulkan/VulkanHandles.h index 1d3d032444d..223a13dbd60 100644 --- a/filament/backend/src/vulkan/VulkanHandles.h +++ b/filament/backend/src/vulkan/VulkanHandles.h @@ -139,7 +139,7 @@ struct VulkanVertexBuffer : public HwVertexBuffer, VulkanResource { }; PipelineInfo* mInfo; - FixedSizeVulkanResourceManager mResources; + FixedSizeVulkanResourceManager mResources; }; struct VulkanIndexBuffer : public HwIndexBuffer, VulkanResource { @@ -186,7 +186,8 @@ struct VulkanRenderPrimitive : public HwRenderPrimitive, VulkanResource { VkPrimitiveTopology primitiveTopology; private: - FixedSizeVulkanResourceManager mResources; + // Keep references to the vertex buffer and the index buffer. + FixedSizeVulkanResourceManager<2> mResources; }; struct VulkanFence : public HwFence, VulkanResource { diff --git a/filament/backend/src/vulkan/VulkanPipelineCache.cpp b/filament/backend/src/vulkan/VulkanPipelineCache.cpp index b554af2b54b..fb0ef037c4c 100644 --- a/filament/backend/src/vulkan/VulkanPipelineCache.cpp +++ b/filament/backend/src/vulkan/VulkanPipelineCache.cpp @@ -156,7 +156,7 @@ bool VulkanPipelineCache::bindDescriptors(VkCommandBuffer cmdbuffer) noexcept { = std::make_unique(mResourceAllocator); resourceEntry = mDescriptorResources.find(cacheEntry->id); } - resourceEntry->second->acquire(&mPipelineBoundResources); + resourceEntry->second->acquireAll(&mPipelineBoundResources); vkCmdBindDescriptorSets(cmdbuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, getOrCreatePipelineLayout()->handle, 0, VulkanPipelineCache::DESCRIPTOR_TYPE_COUNT, diff --git a/filament/backend/src/vulkan/VulkanResources.h b/filament/backend/src/vulkan/VulkanResources.h index 538b07a82a4..77b6498b860 100644 --- a/filament/backend/src/vulkan/VulkanResources.h +++ b/filament/backend/src/vulkan/VulkanResources.h @@ -17,7 +17,6 @@ #ifndef TNT_FILAMENT_BACKEND_VULKANRESOURCES_H #define TNT_FILAMENT_BACKEND_VULKANRESOURCES_H -#include // For MAX_VERTEX_BUFFER_COUNT #include #include @@ -156,13 +155,13 @@ namespace { // When the size of the resource set is known to be small, (for example for VulkanRenderPrimitive), // we just use a std::array to back the set. +template class FixedCapacityResourceSet { private: - constexpr static size_t const SIZE = MAX_VERTEX_BUFFER_COUNT; using FixedSizeArray = std::array; public: - using const_iterator = FixedSizeArray::const_iterator; + using const_iterator = typename FixedSizeArray::const_iterator; inline ~FixedCapacityResourceSet() { clear(); @@ -186,7 +185,7 @@ class FixedCapacityResourceSet { } inline const_iterator find(VulkanResource* resource) { - return std::find(mArray.begin(), mArray.end(), resource); + return std::find(begin(), end(), resource); } inline void insert(VulkanResource* resource) { @@ -205,6 +204,10 @@ class FixedCapacityResourceSet { mInd = 0; } + inline size_t size() { + return mInd; + } + private: FixedSizeArray mArray{nullptr}; size_t mInd = 0; @@ -272,14 +275,21 @@ class VulkanResourceManagerImpl { } // Transfers ownership from one resource set to another - inline void acquire(VulkanResourceManagerImpl* srcResources) { + template + inline void acquireAll(VulkanResourceManagerImpl* srcResources) { + copyAll(srcResources); + srcResources->clear(); + } + + // Transfers ownership from one resource set to another + template + inline void copyAll(VulkanResourceManagerImpl* srcResources) { LOCK_IF_NEEDED(); for (auto iter = srcResources->mResources.begin(); iter != srcResources->mResources.end(); iter++) { acquire(*iter); } UNLOCK_IF_NEEDED(); - srcResources->clear(); } inline void release(ResourceType* resource) { @@ -323,13 +333,18 @@ class VulkanResourceManagerImpl { VulkanResourceAllocator* mAllocator; SetType mResources; std::unique_ptr mMutex; + + template friend class VulkanResourceManagerImpl; }; using VulkanAcquireOnlyResourceManager = VulkanResourceManagerImpl; using VulkanResourceManager = VulkanResourceManagerImpl; -using FixedSizeVulkanResourceManager - = VulkanResourceManagerImpl; + +template +using FixedSizeVulkanResourceManager = + VulkanResourceManagerImpl>; + using VulkanThreadSafeResourceManager = VulkanResourceManagerImpl;