Skip to content

Commit 96ff988

Browse files
author
devsh
committed
useers should not know or case we use LinearAddressAllocatorST under the hood to suballocate
1 parent d2ea2e1 commit 96ff988

File tree

3 files changed

+20
-37
lines changed

3 files changed

+20
-37
lines changed

examples_tests

include/nbl/ext/ImGui/ImGui.h

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,6 @@ class UI final : public core::IReferenceCounted
2121
COUNT,
2222
};
2323

24-
struct SMdiBuffer
25-
{
26-
//! composes memory available for the general purpose allocator to suballocate memory ranges
27-
using compose_t = video::StreamingTransientDataBufferST<core::allocator<uint8_t>>;
28-
29-
//! traits for MDI buffer suballocator - fills the data given the mdi allocator memory request
30-
using suballocator_traits_t = core::address_allocator_traits<core::LinearAddressAllocatorST<uint32_t>>;
31-
32-
//! streaming mdi buffer
33-
core::smart_refctd_ptr<typename compose_t> compose;
34-
35-
//! required buffer allocate flags
36-
static constexpr auto RequiredAllocateFlags = core::bitflag<video::IDeviceMemoryAllocation::E_MEMORY_ALLOCATE_FLAGS>(video::IDeviceMemoryAllocation::EMAF_DEVICE_ADDRESS_BIT);
37-
38-
//! required buffer usage flags
39-
static constexpr auto RequiredUsageFlags = core::bitflag(asset::IBuffer::EUF_INDIRECT_BUFFER_BIT) | asset::IBuffer::EUF_INDEX_BUFFER_BIT | asset::IBuffer::EUF_VERTEX_BUFFER_BIT | asset::IBuffer::EUF_SHADER_DEVICE_ADDRESS_BIT;
40-
};
41-
4224
struct SResourceParameters
4325
{
4426
//! for a given pipeline layout we need to know what is intended for UI resources
@@ -73,14 +55,22 @@ class UI final : public core::IReferenceCounted
7355

7456
struct SCachedCreationParams
7557
{
58+
using streaming_buffer_t = video::StreamingTransientDataBufferST<core::allocator<uint8_t>>;
59+
60+
//! required buffer allocate flags
61+
static constexpr auto RequiredAllocateFlags = core::bitflag<video::IDeviceMemoryAllocation::E_MEMORY_ALLOCATE_FLAGS>(video::IDeviceMemoryAllocation::EMAF_DEVICE_ADDRESS_BIT);
62+
63+
//! required buffer usage flags
64+
static constexpr auto RequiredUsageFlags = core::bitflag(asset::IBuffer::EUF_INDIRECT_BUFFER_BIT) | asset::IBuffer::EUF_INDEX_BUFFER_BIT | asset::IBuffer::EUF_VERTEX_BUFFER_BIT | asset::IBuffer::EUF_SHADER_DEVICE_ADDRESS_BIT;
65+
7666
//! required, you provide us information about your required UI binding resources which we validate at creation time
7767
SResourceParameters resources;
7868

7969
//! required
8070
core::smart_refctd_ptr<video::IUtilities> utilities;
8171

82-
//! optional, default MDI buffer allocated if not provided
83-
core::smart_refctd_ptr<typename SMdiBuffer::compose_t> streamingBuffer = nullptr;
72+
//! optional, default MDI buffer allocated if not provided
73+
core::smart_refctd_ptr<streaming_buffer_t> streamingBuffer = nullptr;
8474

8575
//! optional, default single one
8676
uint32_t viewportCount = 1u;
@@ -160,7 +150,7 @@ class UI final : public core::IReferenceCounted
160150
inline video::IGPUImageView* getFontAtlasView() const { return m_fontAtlasTexture.get(); }
161151

162152
//! mdi streaming buffer
163-
inline const typename SMdiBuffer::compose_t* getStreamingBuffer() const { return m_mdi.compose.get(); }
153+
inline const auto* getStreamingBuffer() const {return m_cachedCreationParams.streamingBuffer.get();}
164154

165155
//! ImGUI context, you are supposed to cast it, eg. reinterpret_cast<ImGuiContext*>(this->getContext());
166156
void* getContext();
@@ -176,7 +166,6 @@ class UI final : public core::IReferenceCounted
176166
core::smart_refctd_ptr<video::IGPUGraphicsPipeline> m_pipeline;
177167
core::smart_refctd_ptr<video::IGPUImageView> m_fontAtlasTexture;
178168

179-
SMdiBuffer m_mdi;
180169
std::vector<std::function<void()>> m_subscribers {};
181170
};
182171
}

src/nbl/ext/ImGui/ImGui.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ using namespace nbl::hlsl;
2323

2424
namespace nbl::ext::imgui
2525
{
26-
using mdi_buffer_t = UI::SMdiBuffer;
27-
using compose_t = typename mdi_buffer_t::compose_t;
28-
29-
3026
static constexpr SPushConstantRange PushConstantRanges[] =
3127
{
3228
{
@@ -899,12 +895,10 @@ void UI::createMDIBuffer(SCreationParameters& m_cachedCreationParams)
899895
return flags;
900896
};
901897

902-
if (m_cachedCreationParams.streamingBuffer)
903-
m_mdi.compose = smart_refctd_ptr<typename compose_t>(m_cachedCreationParams.streamingBuffer);
904-
else
898+
if (!m_cachedCreationParams.streamingBuffer)
905899
{
906900
IGPUBuffer::SCreationParams mdiCreationParams = {};
907-
mdiCreationParams.usage = SMdiBuffer::RequiredUsageFlags;
901+
mdiCreationParams.usage = SCachedCreationParams::RequiredUsageFlags;
908902
mdiCreationParams.size = mdiBufferDefaultSize;
909903

910904
auto buffer = m_cachedCreationParams.utilities->getLogicalDevice()->createBuffer(std::move(mdiCreationParams));
@@ -913,7 +907,7 @@ void UI::createMDIBuffer(SCreationParameters& m_cachedCreationParams)
913907
auto memoryReqs = buffer->getMemoryReqs();
914908
memoryReqs.memoryTypeBits &= m_cachedCreationParams.utilities->getLogicalDevice()->getPhysicalDevice()->getUpStreamingMemoryTypeBits();
915909

916-
auto allocation = m_cachedCreationParams.utilities->getLogicalDevice()->allocate(memoryReqs, buffer.get(), SMdiBuffer::RequiredAllocateFlags);
910+
auto allocation = m_cachedCreationParams.utilities->getLogicalDevice()->allocate(memoryReqs,buffer.get(),SCachedCreationParams::RequiredAllocateFlags);
917911
{
918912
const bool allocated = allocation.isValid();
919913
assert(allocated);
@@ -923,17 +917,17 @@ void UI::createMDIBuffer(SCreationParameters& m_cachedCreationParams)
923917
if (!memory->map({ 0ull, memoryReqs.size }, getRequiredAccessFlags(memory->getMemoryPropertyFlags())))
924918
m_cachedCreationParams.utilities->getLogger()->log("Could not map device memory!", ILogger::ELL_ERROR);
925919

926-
m_mdi.compose = make_smart_refctd_ptr<compose_t>(SBufferRange<IGPUBuffer>{0ull, mdiCreationParams.size, std::move(buffer)}, maxStreamingBufferAllocationAlignment, minStreamingBufferAllocationSize);
920+
m_cachedCreationParams.streamingBuffer = make_smart_refctd_ptr<SCachedCreationParams::streaming_buffer_t>(SBufferRange<IGPUBuffer>{0ull,mdiCreationParams.size,std::move(buffer)},maxStreamingBufferAllocationAlignment,minStreamingBufferAllocationSize);
927921
}
928922

929-
auto buffer = m_mdi.compose->getBuffer();
923+
auto buffer = m_cachedCreationParams.streamingBuffer->getBuffer();
930924
auto binding = buffer->getBoundMemory();
931925

932926
const auto validation = std::to_array
933927
({
934-
std::make_pair(buffer->getCreationParams().usage.hasFlags(SMdiBuffer::RequiredUsageFlags), "MDI buffer must be created with IBuffer::EUF_INDIRECT_BUFFER_BIT | IBuffer::EUF_INDEX_BUFFER_BIT | IBuffer::EUF_VERTEX_BUFFER_BIT | IBuffer::EUF_SHADER_DEVICE_ADDRESS_BIT enabled!"),
928+
std::make_pair(buffer->getCreationParams().usage.hasFlags(SCachedCreationParams::RequiredUsageFlags), "MDI buffer must be created with IBuffer::EUF_INDIRECT_BUFFER_BIT | IBuffer::EUF_INDEX_BUFFER_BIT | IBuffer::EUF_VERTEX_BUFFER_BIT | IBuffer::EUF_SHADER_DEVICE_ADDRESS_BIT enabled!"),
935929
std::make_pair(bool(buffer->getMemoryReqs().memoryTypeBits & m_cachedCreationParams.utilities->getLogicalDevice()->getPhysicalDevice()->getUpStreamingMemoryTypeBits()), "MDI buffer must have up-streaming memory type bits enabled!"),
936-
std::make_pair(binding.memory->getAllocateFlags().hasFlags(SMdiBuffer::RequiredAllocateFlags), "MDI buffer's memory must be allocated with IDeviceMemoryAllocation::EMAF_DEVICE_ADDRESS_BIT enabled!"),
930+
std::make_pair(binding.memory->getAllocateFlags().hasFlags(SCachedCreationParams::RequiredAllocateFlags), "MDI buffer's memory must be allocated with IDeviceMemoryAllocation::EMAF_DEVICE_ADDRESS_BIT enabled!"),
937931
std::make_pair(binding.memory->isCurrentlyMapped(), "MDI buffer's memory must be mapped!"), // streaming buffer contructor already validates it, but cannot assume user won't unmap its own buffer for some reason (sorry if you have just hit it)
938932
std::make_pair(binding.memory->getCurrentMappingAccess().hasFlags(getRequiredAccessFlags(binding.memory->getMemoryPropertyFlags())), "MDI buffer's memory current mapping access flags don't meet requirements!")
939933
});
@@ -982,7 +976,7 @@ bool UI::render(IGPUCommandBuffer* const commandBuffer, ISemaphore::SWaitInfo wa
982976
// allocator initialization needs us to round up to PoT
983977
const auto MaxPOTAlignment = roundUpToPoT(MaxAlignment);
984978
// quick sanity check
985-
auto* streaming = m_mdi.compose.get();
979+
auto* streaming = m_cachedCreationParams.streamingBuffer.get();
986980
if (streaming->getAddressAllocator().max_alignment()<MaxPOTAlignment)
987981
{
988982
logger.log("IMGUI Streaming Buffer cannot guarantee the alignments we require!");

0 commit comments

Comments
 (0)