Skip to content

Commit 4b480ec

Browse files
Buffer suballocator: some updates to properly handle sparse buffers
1 parent 130c330 commit 4b480ec

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

Graphics/GraphicsTools/interface/BufferSuballocator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ struct IBufferSuballocation : public IObject
8282
struct BufferSuballocatorUsageStats
8383
{
8484
/// The size of the internal buffer, in bytes.
85-
Uint32 Size = 0;
85+
Uint64 Size = 0;
8686

8787
/// The total used size, in bytes.
88-
Uint32 UsedSize = 0;
88+
Uint64 UsedSize = 0;
8989

9090
/// The maximum size of the continuous free chunk in the buffer, in bytes.
91-
Uint32 MaxFreeChunkSize = 0;
91+
Uint64 MaxFreeChunkSize = 0;
9292

9393
/// The current number of allocations.
9494
Uint32 AllocationCount = 0;

Graphics/GraphicsTools/src/BufferSuballocator.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,22 @@ class BufferSuballocatorImpl final : public ObjectBase<IBufferSuballocator>
162162
if (Size != m_Buffer.GetDesc().Size)
163163
{
164164
m_Buffer.Resize(pDevice, pContext, Size);
165+
166+
// Actual buffer size may be larger due to alignment requirements
167+
// (for sparse buffers, the size is aligned by the memory page size)
168+
const auto BufferSize = m_Buffer.GetDesc().Size;
169+
VERIFY_EXPR(BufferSize >= Size);
170+
if (BufferSize > Size)
171+
{
172+
std::lock_guard<std::mutex> Lock{m_MgrMtx};
173+
// Since we released the mutex, the size may have changed in other thread
174+
Size = m_Mgr.GetMaxSize();
175+
if (BufferSize > Size)
176+
{
177+
m_Mgr.Extend(StaticCast<size_t>(BufferSize - Size));
178+
}
179+
VERIFY_EXPR(BufferSize == m_Mgr.GetMaxSize());
180+
}
165181
}
166182

167183
return m_Buffer.GetBuffer(pDevice, pContext);
@@ -230,9 +246,9 @@ class BufferSuballocatorImpl final : public ObjectBase<IBufferSuballocator>
230246
virtual void GetUsageStats(BufferSuballocatorUsageStats& UsageStats) override final
231247
{
232248
std::lock_guard<std::mutex> Lock{m_MgrMtx};
233-
UsageStats.Size = static_cast<Uint32>(m_Mgr.GetMaxSize());
234-
UsageStats.UsedSize = static_cast<Uint32>(m_Mgr.GetUsedSize());
235-
UsageStats.MaxFreeChunkSize = static_cast<Uint32>(m_Mgr.GetMaxFreeBlockSize());
249+
UsageStats.Size = m_Mgr.GetMaxSize();
250+
UsageStats.UsedSize = m_Mgr.GetUsedSize();
251+
UsageStats.MaxFreeChunkSize = m_Mgr.GetMaxFreeBlockSize();
236252
UsageStats.AllocationCount = m_AllocationCount.load();
237253
}
238254

0 commit comments

Comments
 (0)