Skip to content

Commit

Permalink
Buffer Suballocator: removed the VirtualSize create info parameter
Browse files Browse the repository at this point in the history
It is redundant as there is the MaxSize
  • Loading branch information
TheMostDiligent committed Feb 14, 2024
1 parent f9bc499 commit 0104a93
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
7 changes: 3 additions & 4 deletions Graphics/GraphicsTools/interface/BufferSuballocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,10 @@ struct BufferSuballocatorCreateInfo
/// more space is needed.
Uint32 ExpansionSize = 0;

/// If Desc.Usage == USAGE_SPARSE, the virtual buffer size; ignored otherwise.
Uint64 VirtualSize = 0;

/// The maximum buffer size, in bytes.
/// If zero, the buffer size is not limited.
/// If Desc.Usage == USAGE_SPARSE, also the buffer virtual size.
///
/// \remarks If MaxSize is zero, the buffer will not be expanded beyond the initial size.
Uint64 MaxSize = 0;

/// Whether to disable debug validation of the internal buffer structure.
Expand Down
52 changes: 25 additions & 27 deletions Graphics/GraphicsTools/src/BufferSuballocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,40 +128,38 @@ class BufferSuballocatorImpl final : public ObjectBase<IBufferSuballocator>
BufferSuballocatorImpl(IReferenceCounters* pRefCounters,
IRenderDevice* pDevice,
const BufferSuballocatorCreateInfo& CreateInfo) :
// clang-format off
TBase{pRefCounters},
m_Mgr
{
VariableSizeAllocationsManager::CreateInfo
{
m_MaxSize{
[](Uint64 Size, Uint64 MaxSize) {
if (MaxSize == 0)
MaxSize = Size;

if (MaxSize < Size)
{
LOG_WARNING_MESSAGE("MaxSize (", MaxSize, ") is less than the initial buffer size (", Size, ").");
MaxSize = Size;
}

return MaxSize;
}(CreateInfo.Desc.Size, CreateInfo.MaxSize)},
m_ExpansionSize{CreateInfo.ExpansionSize},
m_Mgr{
VariableSizeAllocationsManager::CreateInfo{
DefaultRawMemoryAllocator::GetAllocator(),
StaticCast<size_t>(CreateInfo.Desc.Size),
CreateInfo.DisableDebugValidation
}
CreateInfo.DisableDebugValidation,
},
},
m_MgrSize{m_Mgr.GetMaxSize()},
m_Buffer
{
m_Buffer{
pDevice,
DynamicBufferCreateInfo
{
DynamicBufferCreateInfo{
CreateInfo.Desc,
CreateInfo.ExpansionSize != 0 ? CreateInfo.ExpansionSize : static_cast<Uint32>(CreateInfo.Desc.Size), // MemoryPageSize
CreateInfo.VirtualSize
}
m_MaxSize,
},
},
m_BufferSize{m_Buffer.GetDesc().Size},
m_ExpansionSize{CreateInfo.ExpansionSize},
// clang-format on
m_MaxSize{
[](Uint64 Size, Uint64 MaxSize) {
if (MaxSize != 0 && MaxSize < Size)
{
LOG_WARNING_MESSAGE("MaxSize (", MaxSize, ") is less than the initial buffer size (", Size, ").");
MaxSize = Size;
}
return MaxSize;
}(m_BufferSize, CreateInfo.MaxSize)},
m_SuballocationsAllocator{
DefaultRawMemoryAllocator::GetAllocator(),
sizeof(BufferSuballocationImpl),
Expand Down Expand Up @@ -305,6 +303,9 @@ class BufferSuballocatorImpl final : public ObjectBase<IBufferSuballocator>
}

private:
const Uint64 m_MaxSize;
const Uint32 m_ExpansionSize;

std::mutex m_MgrMtx;
VariableSizeAllocationsManager m_Mgr;

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

const Uint32 m_ExpansionSize;
const Uint64 m_MaxSize;

std::atomic<Int32> m_AllocationCount{0};
std::atomic<Uint64> m_UsedSize{0};
std::atomic<Uint64> m_MaxFreeBlockSize{0};
Expand Down
2 changes: 2 additions & 0 deletions Graphics/GraphicsTools/src/DynamicBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ DynamicBuffer::DynamicBuffer(IRenderDevice* pDevice,
m_VirtualSize{CI.Desc.Usage == USAGE_SPARSE ? CI.VirtualSize : 0},
m_MemoryPageSize{CI.MemoryPageSize}
{
DEV_CHECK_ERR(CI.Desc.Usage != USAGE_SPARSE || CI.VirtualSize > 0, "Virtual size must not be 0 for sparse buffers");

m_Desc.Name = m_Name.c_str();
m_PendingSize = m_Desc.Size;
m_Desc.Size = 0; // Current buffer size
Expand Down
6 changes: 4 additions & 2 deletions Tests/DiligentCoreAPITest/src/BufferSuballocatorTest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 Diligent Graphics LLC
* Copyright 2019-2024 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -80,7 +80,9 @@ TEST(BufferSuballocatorTest, Allocate)
BufferSuballocatorCreateInfo CI;
CI.Desc.Name = "Buffer Suballocator Test";
CI.Desc.BindFlags = BIND_VERTEX_BUFFER;
CI.Desc.Size = 1024;
CI.Desc.Size = 32;
CI.ExpansionSize = 32;
CI.MaxSize = 1u << 20u;

RefCntAutoPtr<IBufferSuballocator> pAllocator;
CreateBufferSuballocator(pDevice, CI, &pAllocator);
Expand Down

0 comments on commit 0104a93

Please sign in to comment.