Skip to content

Commit 0104a93

Browse files
Buffer Suballocator: removed the VirtualSize create info parameter
It is redundant as there is the MaxSize
1 parent f9bc499 commit 0104a93

4 files changed

Lines changed: 34 additions & 33 deletions

File tree

Graphics/GraphicsTools/interface/BufferSuballocator.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,10 @@ struct BufferSuballocatorCreateInfo
162162
/// more space is needed.
163163
Uint32 ExpansionSize = 0;
164164

165-
/// If Desc.Usage == USAGE_SPARSE, the virtual buffer size; ignored otherwise.
166-
Uint64 VirtualSize = 0;
167-
168165
/// The maximum buffer size, in bytes.
169-
/// If zero, the buffer size is not limited.
166+
/// If Desc.Usage == USAGE_SPARSE, also the buffer virtual size.
167+
///
168+
/// \remarks If MaxSize is zero, the buffer will not be expanded beyond the initial size.
170169
Uint64 MaxSize = 0;
171170

172171
/// Whether to disable debug validation of the internal buffer structure.

Graphics/GraphicsTools/src/BufferSuballocator.cpp

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -128,40 +128,38 @@ class BufferSuballocatorImpl final : public ObjectBase<IBufferSuballocator>
128128
BufferSuballocatorImpl(IReferenceCounters* pRefCounters,
129129
IRenderDevice* pDevice,
130130
const BufferSuballocatorCreateInfo& CreateInfo) :
131-
// clang-format off
132131
TBase{pRefCounters},
133-
m_Mgr
134-
{
135-
VariableSizeAllocationsManager::CreateInfo
136-
{
132+
m_MaxSize{
133+
[](Uint64 Size, Uint64 MaxSize) {
134+
if (MaxSize == 0)
135+
MaxSize = Size;
136+
137+
if (MaxSize < Size)
138+
{
139+
LOG_WARNING_MESSAGE("MaxSize (", MaxSize, ") is less than the initial buffer size (", Size, ").");
140+
MaxSize = Size;
141+
}
142+
143+
return MaxSize;
144+
}(CreateInfo.Desc.Size, CreateInfo.MaxSize)},
145+
m_ExpansionSize{CreateInfo.ExpansionSize},
146+
m_Mgr{
147+
VariableSizeAllocationsManager::CreateInfo{
137148
DefaultRawMemoryAllocator::GetAllocator(),
138149
StaticCast<size_t>(CreateInfo.Desc.Size),
139-
CreateInfo.DisableDebugValidation
140-
}
150+
CreateInfo.DisableDebugValidation,
151+
},
141152
},
142153
m_MgrSize{m_Mgr.GetMaxSize()},
143-
m_Buffer
144-
{
154+
m_Buffer{
145155
pDevice,
146-
DynamicBufferCreateInfo
147-
{
156+
DynamicBufferCreateInfo{
148157
CreateInfo.Desc,
149158
CreateInfo.ExpansionSize != 0 ? CreateInfo.ExpansionSize : static_cast<Uint32>(CreateInfo.Desc.Size), // MemoryPageSize
150-
CreateInfo.VirtualSize
151-
}
159+
m_MaxSize,
160+
},
152161
},
153162
m_BufferSize{m_Buffer.GetDesc().Size},
154-
m_ExpansionSize{CreateInfo.ExpansionSize},
155-
// clang-format on
156-
m_MaxSize{
157-
[](Uint64 Size, Uint64 MaxSize) {
158-
if (MaxSize != 0 && MaxSize < Size)
159-
{
160-
LOG_WARNING_MESSAGE("MaxSize (", MaxSize, ") is less than the initial buffer size (", Size, ").");
161-
MaxSize = Size;
162-
}
163-
return MaxSize;
164-
}(m_BufferSize, CreateInfo.MaxSize)},
165163
m_SuballocationsAllocator{
166164
DefaultRawMemoryAllocator::GetAllocator(),
167165
sizeof(BufferSuballocationImpl),
@@ -305,6 +303,9 @@ class BufferSuballocatorImpl final : public ObjectBase<IBufferSuballocator>
305303
}
306304

307305
private:
306+
const Uint64 m_MaxSize;
307+
const Uint32 m_ExpansionSize;
308+
308309
std::mutex m_MgrMtx;
309310
VariableSizeAllocationsManager m_Mgr;
310311

@@ -313,9 +314,6 @@ class BufferSuballocatorImpl final : public ObjectBase<IBufferSuballocator>
313314
DynamicBuffer m_Buffer;
314315
std::atomic<Uint64> m_BufferSize{0};
315316

316-
const Uint32 m_ExpansionSize;
317-
const Uint64 m_MaxSize;
318-
319317
std::atomic<Int32> m_AllocationCount{0};
320318
std::atomic<Uint64> m_UsedSize{0};
321319
std::atomic<Uint64> m_MaxFreeBlockSize{0};

Graphics/GraphicsTools/src/DynamicBuffer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ DynamicBuffer::DynamicBuffer(IRenderDevice* pDevice,
6969
m_VirtualSize{CI.Desc.Usage == USAGE_SPARSE ? CI.VirtualSize : 0},
7070
m_MemoryPageSize{CI.MemoryPageSize}
7171
{
72+
DEV_CHECK_ERR(CI.Desc.Usage != USAGE_SPARSE || CI.VirtualSize > 0, "Virtual size must not be 0 for sparse buffers");
73+
7274
m_Desc.Name = m_Name.c_str();
7375
m_PendingSize = m_Desc.Size;
7476
m_Desc.Size = 0; // Current buffer size

Tests/DiligentCoreAPITest/src/BufferSuballocatorTest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -80,7 +80,9 @@ TEST(BufferSuballocatorTest, Allocate)
8080
BufferSuballocatorCreateInfo CI;
8181
CI.Desc.Name = "Buffer Suballocator Test";
8282
CI.Desc.BindFlags = BIND_VERTEX_BUFFER;
83-
CI.Desc.Size = 1024;
83+
CI.Desc.Size = 32;
84+
CI.ExpansionSize = 32;
85+
CI.MaxSize = 1u << 20u;
8486

8587
RefCntAutoPtr<IBufferSuballocator> pAllocator;
8688
CreateBufferSuballocator(pDevice, CI, &pAllocator);

0 commit comments

Comments
 (0)