Skip to content

Commit

Permalink
vk: refactor VulkanResources.h (#7206)
Browse files Browse the repository at this point in the history
- Make the size of FixedSizedVulkanResources declarable
- Rename `acquire`
  • Loading branch information
poweifeng authored Sep 27, 2023
1 parent 2e9dd03 commit f6b979c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion filament/backend/src/vulkan/VulkanCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ struct VulkanCommandBuffer {
}

inline void acquire(VulkanAcquireOnlyResourceManager* srcResources) {
mResourceManager.acquire(srcResources);
mResourceManager.acquireAll(srcResources);
}

inline void reset() {
Expand Down
5 changes: 3 additions & 2 deletions filament/backend/src/vulkan/VulkanHandles.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ struct VulkanVertexBuffer : public HwVertexBuffer, VulkanResource {
};

PipelineInfo* mInfo;
FixedSizeVulkanResourceManager mResources;
FixedSizeVulkanResourceManager<MAX_VERTEX_BUFFER_COUNT> mResources;
};

struct VulkanIndexBuffer : public HwIndexBuffer, VulkanResource {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion filament/backend/src/vulkan/VulkanPipelineCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ bool VulkanPipelineCache::bindDescriptors(VkCommandBuffer cmdbuffer) noexcept {
= std::make_unique<VulkanAcquireOnlyResourceManager>(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,
Expand Down
31 changes: 23 additions & 8 deletions filament/backend/src/vulkan/VulkanResources.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#ifndef TNT_FILAMENT_BACKEND_VULKANRESOURCES_H
#define TNT_FILAMENT_BACKEND_VULKANRESOURCES_H

#include <backend/DriverEnums.h>// For MAX_VERTEX_BUFFER_COUNT
#include <backend/Handle.h>

#include <tsl/robin_set.h>
Expand Down Expand Up @@ -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<std::size_t SIZE>
class FixedCapacityResourceSet {
private:
constexpr static size_t const SIZE = MAX_VERTEX_BUFFER_COUNT;
using FixedSizeArray = std::array<VulkanResource*, SIZE>;

public:
using const_iterator = FixedSizeArray::const_iterator;
using const_iterator = typename FixedSizeArray::const_iterator;

inline ~FixedCapacityResourceSet() {
clear();
Expand All @@ -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) {
Expand All @@ -205,6 +204,10 @@ class FixedCapacityResourceSet {
mInd = 0;
}

inline size_t size() {
return mInd;
}

private:
FixedSizeArray mArray{nullptr};
size_t mInd = 0;
Expand Down Expand Up @@ -272,14 +275,21 @@ class VulkanResourceManagerImpl {
}

// Transfers ownership from one resource set to another
inline void acquire(VulkanResourceManagerImpl<ResourceType, SetType>* srcResources) {
template <typename tSetType>
inline void acquireAll(VulkanResourceManagerImpl<ResourceType, tSetType>* srcResources) {
copyAll(srcResources);
srcResources->clear();
}

// Transfers ownership from one resource set to another
template <typename tSetType>
inline void copyAll(VulkanResourceManagerImpl<ResourceType, tSetType>* 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) {
Expand Down Expand Up @@ -323,13 +333,18 @@ class VulkanResourceManagerImpl {
VulkanResourceAllocator* mAllocator;
SetType mResources;
std::unique_ptr<utils::Mutex> mMutex;

template <typename, typename> friend class VulkanResourceManagerImpl;
};

using VulkanAcquireOnlyResourceManager
= VulkanResourceManagerImpl<VulkanResource, FastIterationResourceSet>;
using VulkanResourceManager = VulkanResourceManagerImpl<VulkanResource, ResourceSet>;
using FixedSizeVulkanResourceManager
= VulkanResourceManagerImpl<VulkanResource, FixedCapacityResourceSet>;

template<std::size_t SIZE>
using FixedSizeVulkanResourceManager =
VulkanResourceManagerImpl<VulkanResource, FixedCapacityResourceSet<SIZE>>;

using VulkanThreadSafeResourceManager
= VulkanResourceManagerImpl<VulkanThreadSafeResource, ThreadSafeResourceSet>;

Expand Down

0 comments on commit f6b979c

Please sign in to comment.