From 0104a9335e15779815dcb3a7ff2f7e6f9d7b206f Mon Sep 17 00:00:00 2001 From: assiduous Date: Wed, 14 Feb 2024 14:51:17 -0800 Subject: [PATCH] Buffer Suballocator: removed the VirtualSize create info parameter It is redundant as there is the MaxSize --- .../interface/BufferSuballocator.h | 7 ++- .../GraphicsTools/src/BufferSuballocator.cpp | 52 +++++++++---------- Graphics/GraphicsTools/src/DynamicBuffer.cpp | 2 + .../src/BufferSuballocatorTest.cpp | 6 ++- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/Graphics/GraphicsTools/interface/BufferSuballocator.h b/Graphics/GraphicsTools/interface/BufferSuballocator.h index 111377b33..6b2c9610d 100644 --- a/Graphics/GraphicsTools/interface/BufferSuballocator.h +++ b/Graphics/GraphicsTools/interface/BufferSuballocator.h @@ -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. diff --git a/Graphics/GraphicsTools/src/BufferSuballocator.cpp b/Graphics/GraphicsTools/src/BufferSuballocator.cpp index 550c08788..5b653d420 100644 --- a/Graphics/GraphicsTools/src/BufferSuballocator.cpp +++ b/Graphics/GraphicsTools/src/BufferSuballocator.cpp @@ -128,40 +128,38 @@ class BufferSuballocatorImpl final : public ObjectBase 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(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(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), @@ -305,6 +303,9 @@ class BufferSuballocatorImpl final : public ObjectBase } private: + const Uint64 m_MaxSize; + const Uint32 m_ExpansionSize; + std::mutex m_MgrMtx; VariableSizeAllocationsManager m_Mgr; @@ -313,9 +314,6 @@ class BufferSuballocatorImpl final : public ObjectBase DynamicBuffer m_Buffer; std::atomic m_BufferSize{0}; - const Uint32 m_ExpansionSize; - const Uint64 m_MaxSize; - std::atomic m_AllocationCount{0}; std::atomic m_UsedSize{0}; std::atomic m_MaxFreeBlockSize{0}; diff --git a/Graphics/GraphicsTools/src/DynamicBuffer.cpp b/Graphics/GraphicsTools/src/DynamicBuffer.cpp index 5dd2cedc7..ae321274c 100644 --- a/Graphics/GraphicsTools/src/DynamicBuffer.cpp +++ b/Graphics/GraphicsTools/src/DynamicBuffer.cpp @@ -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 diff --git a/Tests/DiligentCoreAPITest/src/BufferSuballocatorTest.cpp b/Tests/DiligentCoreAPITest/src/BufferSuballocatorTest.cpp index 4abe8a4f4..6c7733d3a 100644 --- a/Tests/DiligentCoreAPITest/src/BufferSuballocatorTest.cpp +++ b/Tests/DiligentCoreAPITest/src/BufferSuballocatorTest.cpp @@ -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"); @@ -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 pAllocator; CreateBufferSuballocator(pDevice, CI, &pAllocator);