Skip to content

Commit 7ba6cc2

Browse files
committed
Introduce allocator_state_t to IDescriptorPool.
1 parent 2ff2940 commit 7ba6cc2

File tree

3 files changed

+56
-41
lines changed

3 files changed

+56
-41
lines changed

include/nbl/video/IDescriptorPool.h

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,10 @@ class NBL_API IDescriptorPool : public core::IReferenceCounted, public IBackendO
7676
m_maxDescriptorCount[static_cast<uint32_t>(poolSizes[i].type)] += poolSizes[i].count;
7777

7878
for (auto i = 0; i < static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT); ++i)
79-
{
80-
if (m_maxDescriptorCount[i] > 0)
81-
{
82-
if (m_flags & ECF_FREE_DESCRIPTOR_SET_BIT)
83-
{
84-
m_generalAllocatorReservedSpace[i] = std::make_unique<uint8_t[]>(core::GeneralpurposeAddressAllocator<uint32_t>::reserved_size(1u, m_maxDescriptorCount[i], 1u));
85-
m_generalAllocators[i] = core::GeneralpurposeAddressAllocator<uint32_t>(m_generalAllocatorReservedSpace[i].get(), 0u, 0u, 1u, m_maxDescriptorCount[i], 1u);
86-
}
87-
else
88-
{
89-
m_linearAllocators[i] = core::LinearAddressAllocator<uint32_t>(nullptr, 0u, 0u, 1u, m_maxDescriptorCount[i]);
90-
}
91-
}
92-
}
79+
m_descriptorAllocators[i] = std::make_unique<allocator_state_t>(m_maxDescriptorCount[i], m_flags & ECF_FREE_DESCRIPTOR_SET_BIT);
9380

9481
// For (possibly) mutable samplers.
95-
if (m_flags & ECF_FREE_DESCRIPTOR_SET_BIT)
96-
{
97-
m_generalAllocatorReservedSpace[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)] = std::make_unique<uint8_t[]>(core::GeneralpurposeAddressAllocator<uint32_t>::reserved_size(1u, m_maxDescriptorCount[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COMBINED_IMAGE_SAMPLER)], 1u));
98-
m_generalAllocators[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)] = core::GeneralpurposeAddressAllocator<uint32_t>(m_generalAllocatorReservedSpace[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)].get(), 0u, 0u, 1u, m_maxDescriptorCount[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COMBINED_IMAGE_SAMPLER)], 1u);
99-
}
100-
else
101-
{
102-
m_linearAllocators[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)] = core::LinearAddressAllocator<uint32_t>(nullptr, 0u, 0u, 1u, m_maxDescriptorCount[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COMBINED_IMAGE_SAMPLER)]);
103-
}
82+
m_descriptorAllocators[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)] = std::make_unique<allocator_state_t>(m_maxDescriptorCount[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)], m_flags & ECF_FREE_DESCRIPTOR_SET_BIT);
10483

10584
// Initialize the storages.
10685
m_textureStorage = std::make_unique<core::StorageTrivializer<core::smart_refctd_ptr<video::IGPUImageView>>[]>(m_maxDescriptorCount[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COMBINED_IMAGE_SAMPLER)]);
@@ -124,13 +103,56 @@ class NBL_API IDescriptorPool : public core::IReferenceCounted, public IBackendO
124103
SDescriptorOffsets allocateDescriptorOffsets(const IGPUDescriptorSetLayout* layout);
125104

126105
const IDescriptorPool::E_CREATE_FLAGS m_flags;
106+
127107
uint32_t m_maxDescriptorCount[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)];
128-
union
108+
109+
struct allocator_state_t
129110
{
130-
core::LinearAddressAllocator<uint32_t> m_linearAllocators[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)+1];
131-
core::GeneralpurposeAddressAllocator<uint32_t> m_generalAllocators[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)+1];
111+
allocator_state_t(const uint32_t maxDescriptorCount, const bool allowsFreeing)
112+
{
113+
if (maxDescriptorCount == 0)
114+
return;
115+
116+
if (allowsFreeing)
117+
{
118+
generalAllocatorReservedSpace = std::make_unique<uint8_t[]>(core::GeneralpurposeAddressAllocator<uint32_t>::reserved_size(1u, maxDescriptorCount, 1u));
119+
generalAllocator = core::GeneralpurposeAddressAllocator<uint32_t>(generalAllocatorReservedSpace.get(), 0u, 0u, 1u, maxDescriptorCount, 1u);
120+
}
121+
else
122+
{
123+
linearAllocator = core::LinearAddressAllocator<uint32_t>(nullptr, 0u, 0u, 1u, maxDescriptorCount);
124+
}
125+
}
126+
127+
~allocator_state_t() {}
128+
129+
inline uint32_t allocate(const uint32_t count, const bool allowsFreeing)
130+
{
131+
if (allowsFreeing)
132+
{
133+
assert(generalAllocatorReservedSpace);
134+
return generalAllocator.alloc_addr(count, 1u);
135+
}
136+
else
137+
{
138+
return linearAllocator.alloc_addr(count, 1u);
139+
}
140+
}
141+
142+
inline void free(const uint32_t allocatedOffset, const uint32_t count)
143+
{
144+
assert(generalAllocatorReservedSpace);
145+
generalAllocator.free_addr(allocatedOffset, count);
146+
}
147+
148+
union
149+
{
150+
core::LinearAddressAllocator<uint32_t> linearAllocator;
151+
core::GeneralpurposeAddressAllocator<uint32_t> generalAllocator;
152+
};
153+
std::unique_ptr<uint8_t[]> generalAllocatorReservedSpace = nullptr;
132154
};
133-
std::unique_ptr<uint8_t[]> m_generalAllocatorReservedSpace[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)+1];
155+
std::unique_ptr<allocator_state_t> m_descriptorAllocators[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT) + 1];
134156

135157
std::unique_ptr<core::StorageTrivializer<core::smart_refctd_ptr<video::IGPUImageView>>[]> m_textureStorage;
136158
std::unique_ptr<core::StorageTrivializer<core::smart_refctd_ptr<video::IGPUSampler>>[]> m_mutableSamplerStorage;

src/nbl/video/CVulkanCommandBuffer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ bool CVulkanCommandBuffer::copyBuffer_impl(const buffer_t* srcBuffer, buffer_t*
6262
VkBuffer vk_dstBuffer = IBackendObject::compatibility_cast<const CVulkanBuffer*>(dstBuffer, this)->getInternalObject();
6363

6464
constexpr uint32_t MAX_BUFFER_COPY_REGION_COUNT = 681u;
65+
assert(regionCount <= MAX_BUFFER_COPY_REGION_COUNT);
6566
VkBufferCopy vk_bufferCopyRegions[MAX_BUFFER_COPY_REGION_COUNT];
6667
for (uint32_t i = 0u; i < regionCount; ++i)
6768
{

src/nbl/video/IDescriptorPool.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,22 @@ IDescriptorPool::SDescriptorOffsets IDescriptorPool::allocateDescriptorOffsets(c
3030
if (count == 0ull)
3131
continue;
3232

33-
if (m_flags & IDescriptorPool::ECF_FREE_DESCRIPTOR_SET_BIT)
34-
offsets.data[i] = m_generalAllocators[i].alloc_addr(count, 1u);
35-
else
36-
offsets.data[i] = m_linearAllocators[i].alloc_addr(count, 1u);
33+
offsets.data[i] = m_descriptorAllocators[i]->allocate(count, m_flags & ECF_FREE_DESCRIPTOR_SET_BIT);
3734

3835
assert((offsets.data[i] < m_maxDescriptorCount[i]) && "PANIC: Allocation failed. This shoudn't have happened! Check your descriptor pool.");
3936
}
4037

4138
const auto mutableSamplerCount = layout->getTotalMutableSamplerCount();
4239
if (mutableSamplerCount != 0ull)
43-
{
44-
if (m_flags & IDescriptorPool::ECF_FREE_DESCRIPTOR_SET_BIT)
45-
offsets.data[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)] = m_generalAllocators[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)].alloc_addr(mutableSamplerCount, 1u);
46-
else
47-
offsets.data[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)] = m_linearAllocators[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)].alloc_addr(mutableSamplerCount, 1u);
48-
}
40+
offsets.data[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)] = m_descriptorAllocators[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)]->allocate(mutableSamplerCount, m_flags & ECF_FREE_DESCRIPTOR_SET_BIT);
4941

5042
return offsets;
5143
}
5244

5345
bool IDescriptorPool::freeDescriptorSets(const uint32_t descriptorSetCount, IGPUDescriptorSet* const* const descriptorSets)
5446
{
55-
const bool allowsFreeingDescriptorSets = m_flags & IDescriptorPool::ECF_FREE_DESCRIPTOR_SET_BIT;
56-
if (!allowsFreeingDescriptorSets)
47+
const bool allowsFreeing = m_flags & ECF_FREE_DESCRIPTOR_SET_BIT;
48+
if (!allowsFreeing)
5749
return false;
5850

5951
for (auto i = 0u; i < descriptorSetCount; ++i)
@@ -75,7 +67,7 @@ bool IDescriptorPool::freeDescriptorSets(const uint32_t descriptorSetCount, IGPU
7567
for (auto c = 0u; c < count; ++c)
7668
descriptors[c].~smart_refctd_ptr();
7769

78-
m_generalAllocators[t].free_addr(allocatedOffset, count);
70+
m_descriptorAllocators[t]->free(allocatedOffset, count);
7971
}
8072

8173
const uint32_t count = descriptorSets[i]->getLayout()->getTotalMutableSamplerCount();
@@ -91,7 +83,7 @@ bool IDescriptorPool::freeDescriptorSets(const uint32_t descriptorSetCount, IGPU
9183
for (auto c = 0u; c < count; ++c)
9284
samplers[c].~smart_refctd_ptr();
9385

94-
m_generalAllocators[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)].free_addr(allocatedOffset, count);
86+
m_descriptorAllocators[static_cast<uint32_t>(asset::IDescriptor::E_TYPE::ET_COUNT)]->free(allocatedOffset, count);
9587
}
9688
}
9789

0 commit comments

Comments
 (0)