Lugdunum  0.1.0
DescriptorSetPool.inl
Go to the documentation of this file.
1 template <size_t maxSets>
2 inline DescriptorSetPool<maxSets>::DescriptorSetPool(Renderer& renderer) : _renderer(renderer) {
3  ++poolCount;
4 }
5 
6 template <size_t maxSets>
8  --poolCount;
9 
10  if (poolCount == 0) {
12  }
13 }
14 
15 template <size_t maxSets>
17  if (static_cast<VkDescriptorPool>(descriptorPool) == VK_NULL_HANDLE) {
18  API::Builder::DescriptorPool descriptorPoolBuilder(_renderer.getDevice());
19 
20  // Use VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT to individually free descritors sets
21  descriptorPoolBuilder.setFlags(VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT);
22 
23  descriptorPoolBuilder.setMaxSets(42); // TODO: Replace this arbitrary number
24 
25  std::vector<VkDescriptorPoolSize> poolSizes{ // TODO: Replace this initialization
26  {
27  /* poolSize.type */ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
28  /* poolSize.descriptorCount */ 42
29  },
30  {
31  /* poolSize.type */ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
32  /* poolSize.descriptorCount */ 42
33  }
34  };
35  descriptorPoolBuilder.setPoolSizes(poolSizes);
36 
37  VkResult result{VK_SUCCESS};
38  if (!descriptorPoolBuilder.build(descriptorPool, &result)) {
39  LUG_LOG.error("DescriptorSetPool: Can't create the descriptor pool: {}", result);
40  return false;
41  }
42  }
43  return true;
44 }
45 
46 template <size_t maxSets>
47 inline std::tuple<bool, const DescriptorSet*> DescriptorSetPool<maxSets>::allocate(size_t hash, const API::DescriptorSetLayout& descriptorSetLayout) {
48  auto it = _descriptorSetsInUse.find(hash);
49  if (it == _descriptorSetsInUse.end()) {
50  DescriptorSet* descriptorSet = allocateNewDescriptorSet(descriptorSetLayout);
51 
52  if (!descriptorSet) {
53  return std::make_tuple(false, nullptr);
54  }
55 
56  descriptorSet->setHash(hash);
57  descriptorSet->_referenceCount += 1;
58 
59  _descriptorSetsInUse[hash] = descriptorSet;
60 
61  return std::make_tuple(true, descriptorSet);
62  }
63 
64  it->second->_referenceCount += 1;
65  return std::make_tuple(false, it->second);
66 }
67 
68 template <size_t maxSets>
69 inline void DescriptorSetPool<maxSets>::free(const DescriptorSet* descriptorSet) {
70  if (!descriptorSet) {
71  return;
72  }
73 
74  const_cast<DescriptorSet*>(descriptorSet)->_referenceCount -= 1;
75 
76  const auto it = _descriptorSetsInUse.find(descriptorSet->getHash());
77  if (descriptorSet->_referenceCount == 0) {
78  if (it != _descriptorSetsInUse.end() && it->second == descriptorSet) {
79  _descriptorSetsInUse.erase(descriptorSet->getHash());
80  }
81 
82  _freeDescriptorSets[_freeDescriptorSetsCount] = const_cast<DescriptorSet*>(descriptorSet);
83  ++_freeDescriptorSetsCount;
84 
85  // TODO: Put this method in API::DescriptorPool
86  VkDescriptorSet vkDescriptorSet = static_cast<VkDescriptorSet>(descriptorSet->getDescriptorSet());
87  vkFreeDescriptorSets(static_cast<VkDevice>(_renderer.getDevice()), static_cast<VkDescriptorPool>(descriptorPool), 1, &vkDescriptorSet);
88  }
89 }
90 
91 template <size_t maxSets>
93  if (_freeDescriptorSetsCount) {
94  --_freeDescriptorSetsCount;
95 
96  DescriptorSet* tmp = _freeDescriptorSets[_freeDescriptorSetsCount];
97  _freeDescriptorSets[_freeDescriptorSetsCount] = nullptr;
98 
99  API::Builder::DescriptorSet descriptorSetBuilder(_renderer.getDevice(), descriptorPool);
100  descriptorSetBuilder.setDescriptorSetLayouts({static_cast<VkDescriptorSetLayout>(descriptorSetLayout)});
101 
102  VkResult result{VK_SUCCESS};
103  if (!descriptorSetBuilder.build(tmp->_descriptorSet, &result)) {
104  LUG_LOG.error("DescriptorSetPool: Can't create descriptor set: {}", result);
105  return nullptr;
106  }
107 
108  return tmp;
109  }
110 
111  if (_descriptorSetsCount < maxSets) {
112  API::Builder::DescriptorSet descriptorSetBuilder(_renderer.getDevice(), descriptorPool);
113  descriptorSetBuilder.setDescriptorSetLayouts({static_cast<VkDescriptorSetLayout>(descriptorSetLayout)});
114 
115  VkResult result{VK_SUCCESS};
116  if (!descriptorSetBuilder.build(_descriptorSets[_descriptorSetsCount]._descriptorSet, &result)) {
117  LUG_LOG.error("DescriptorSetPool: Can't create descriptor set: {}", result);
118  return nullptr;
119  }
120 
121  return &_descriptorSets[_descriptorSetsCount++];
122  } else {
123  LUG_LOG.error("DescriptorSetPool: Can't create descriptor set: Maximum number of sets reached");
124  }
125 
126  return nullptr;
127 }
void setFlags(VkDescriptorPoolCreateFlags flags)
void setDescriptorSetLayouts(const std::vector< VkDescriptorSetLayout > &descriptorSetLayouts)
#define LUG_LOG
Definition: Logger.hpp:73