| Flutter Impeller |

descriptor_pool_vk.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_DESCRIPTOR_POOL_VK_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_DESCRIPTOR_POOL_VK_H_
7
8 #include <cstdint>
9 #include <unordered_map>
10
11 #include "fml/status_or.h"
12 #include "impeller/renderer/backend/vulkan/context_vk.h"
13 #include "impeller/renderer/pipeline.h"
14
15 namespace impeller {
16
17 /// Used and un-used descriptor sets.
18 struct DescriptorCache {
19 std::vector<vk::DescriptorSet> unused;
20 std::vector<vk::DescriptorSet> used;
21 };
22
23 using DescriptorCacheMap = std::unordered_map<PipelineKey, DescriptorCache>;
24
25 //------------------------------------------------------------------------------
26 /// @brief A per-frame descriptor pool. Descriptors
27 /// from this pool don't need to be freed individually. Instead, the
28 /// pool must be collected after all the descriptors allocated from
29 /// it are done being used.
30 ///
31 /// The pool or it's descriptors may not be accessed from multiple
32 /// threads.
33 ///
34 /// Encoders create pools as necessary as they have the same
35 /// threading and lifecycle restrictions.
36 class DescriptorPoolVK {
37 public:
38 explicit DescriptorPoolVK(std::weak_ptr<const ContextVK> context);
39
40 DescriptorPoolVK(std::weak_ptr<const ContextVK> context,
41 DescriptorCacheMap descriptor_sets,
42 std::vector<vk::UniqueDescriptorPool> pools);
43
44 ~DescriptorPoolVK();
45
46 fml::StatusOr<vk::DescriptorSet> AllocateDescriptorSets(
47 const vk::DescriptorSetLayout& layout,
48 PipelineKey pipeline_key,
49 const ContextVK& context_vk);
50
51 private:
52 friend class DescriptorPoolRecyclerVK;
53
54 std::weak_ptr<const ContextVK> context_;
55 DescriptorCacheMap descriptor_sets_;
56 std::vector<vk::UniqueDescriptorPool> pools_;
57
58 void Destroy();
59
60 fml::Status CreateNewPool(const ContextVK& context_vk);
61
62 DescriptorPoolVK(const DescriptorPoolVK&) = delete;
63
64 DescriptorPoolVK& operator=(const DescriptorPoolVK&) = delete;
65 };
66
67 //------------------------------------------------------------------------------
68 /// @brief Creates and manages the lifecycle of |vk::DescriptorPoolVK|
69 /// objects.
70 class DescriptorPoolRecyclerVK final
71 : public std::enable_shared_from_this<DescriptorPoolRecyclerVK> {
72 public:
73 ~DescriptorPoolRecyclerVK() = default;
74
75 /// The maximum number of descriptor pools this recycler will hold onto.
76 static constexpr size_t kMaxRecycledPools = 32u;
77
78 /// @brief Creates a recycler for the given |ContextVK|.
79 ///
80 /// @param[in] context The context to create the recycler for.
81 explicit DescriptorPoolRecyclerVK(std::weak_ptr<ContextVK> context)
82 : context_(std::move(context)) {}
83
84 /// @brief Gets a descriptor pool.
85 ///
86 /// This may create a new descriptor pool if no existing pools had
87 /// the necessary capacity.
88 vk::UniqueDescriptorPool Get();
89
90 std::shared_ptr<DescriptorPoolVK> GetDescriptorPool();
91
92 void Reclaim(DescriptorCacheMap descriptor_sets,
93 std::vector<vk::UniqueDescriptorPool> pools);
94
95 private:
96 std::weak_ptr<ContextVK> context_;
97
98 Mutex recycled_mutex_;
99 std::vector<std::shared_ptr<DescriptorPoolVK>> recycled_ IPLR_GUARDED_BY(
100 recycled_mutex_);
101
102 /// @brief Creates a new |vk::CommandPool|.
103 ///
104 /// @returns Returns a |std::nullopt| if a pool could not be created.
105 vk::UniqueDescriptorPool Create();
106
107 DescriptorPoolRecyclerVK(const DescriptorPoolRecyclerVK&) = delete;
108
109 DescriptorPoolRecyclerVK& operator=(const DescriptorPoolRecyclerVK&) = delete;
110 };
111
112 } // namespace impeller
113
114 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_DESCRIPTOR_POOL_VK_H_
impeller::ContextVK
Definition: context_vk.h:66
impeller::DescriptorPoolRecyclerVK
Creates and manages the lifecycle of |vk::DescriptorPoolVK| objects.
Definition: descriptor_pool_vk.h:71
impeller::DescriptorPoolRecyclerVK::GetDescriptorPool
std::shared_ptr< DescriptorPoolVK > GetDescriptorPool()
Definition: descriptor_pool_vk.cc:174
impeller::DescriptorPoolRecyclerVK::DescriptorPoolRecyclerVK
DescriptorPoolRecyclerVK(std::weak_ptr< ContextVK > context)
Creates a recycler for the given |ContextVK|.
Definition: descriptor_pool_vk.h:81
impeller::DescriptorPoolRecyclerVK::Reclaim
void Reclaim(DescriptorCacheMap descriptor_sets, std::vector< vk::UniqueDescriptorPool > pools)
Definition: descriptor_pool_vk.cc:111
impeller::DescriptorPoolRecyclerVK::Get
vk::UniqueDescriptorPool Get()
Gets a descriptor pool.
Definition: descriptor_pool_vk.cc:138
impeller::DescriptorPoolRecyclerVK::~DescriptorPoolRecyclerVK
~DescriptorPoolRecyclerVK()=default
impeller::DescriptorPoolRecyclerVK::kMaxRecycledPools
static constexpr size_t kMaxRecycledPools
The maximum number of descriptor pools this recycler will hold onto.
Definition: descriptor_pool_vk.h:76
impeller::DescriptorPoolVK
A per-frame descriptor pool. Descriptors from this pool don't need to be freed individually....
Definition: descriptor_pool_vk.h:36
impeller::DescriptorPoolVK::DescriptorPoolVK
DescriptorPoolVK(std::weak_ptr< const ContextVK > context)
Definition: descriptor_pool_vk.cc:29
impeller::DescriptorPoolVK::AllocateDescriptorSets
fml::StatusOr< vk::DescriptorSet > AllocateDescriptorSets(const vk::DescriptorSetLayout &layout, PipelineKey pipeline_key, const ContextVK &context_vk)
Definition: descriptor_pool_vk.cc:60
impeller::DescriptorPoolVK::~DescriptorPoolVK
~DescriptorPoolVK()
Definition: descriptor_pool_vk.cc:43
impeller
Definition: allocation.cc:12
impeller::DescriptorCacheMap
std::unordered_map< PipelineKey, DescriptorCache > DescriptorCacheMap
Definition: descriptor_pool_vk.h:23
impeller::PipelineKey
int64_t PipelineKey
Definition: pipeline.h:21
std
Definition: comparable.h:95
pipeline.h
context_vk.h
impeller::DescriptorCache
Used and un-used descriptor sets.
Definition: descriptor_pool_vk.h:18
impeller::DescriptorCache::unused
std::vector< vk::DescriptorSet > unused
Definition: descriptor_pool_vk.h:19
impeller::DescriptorCache::used
std::vector< vk::DescriptorSet > used
Definition: descriptor_pool_vk.h:20