From 55f997002ffbb54013e14ac06adaac3e2efff719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Wed, 29 Oct 2025 13:52:30 -0700 Subject: [PATCH] Added ability to configure minimum uniform buffer size. PR #3478. --- src/bgfx_p.h | 11 +++++++---- src/config.h | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/bgfx_p.h b/src/bgfx_p.h index ac1049438..3b0e852ed 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -1478,7 +1478,7 @@ namespace bgfx class UniformBuffer { public: - static UniformBuffer* create(uint32_t _size) // = BGFX_CONFIG_UNIFORM_BUFFER_SIZE) + static UniformBuffer* create(uint32_t _size) { const uint32_t structSize = sizeof(UniformBuffer)-sizeof(UniformBuffer::m_buffer); @@ -1493,13 +1493,16 @@ namespace bgfx bx::free(g_allocator, _uniformBuffer); } - static void update(UniformBuffer** _uniformBuffer, uint32_t _threshold = 64<<10, uint32_t _grow = 1<<20) + static void update(UniformBuffer** _uniformBuffer) { + constexpr uint32_t kThreshold = BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_THRESHOLD_SIZE; + constexpr uint32_t kIncrement = BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_INCREMENT_SIZE; + UniformBuffer* uniformBuffer = *_uniformBuffer; - if (_threshold >= uniformBuffer->m_size - uniformBuffer->m_pos) + if (kThreshold >= uniformBuffer->m_size - uniformBuffer->m_pos) { const uint32_t structSize = sizeof(UniformBuffer)-sizeof(UniformBuffer::m_buffer); - uint32_t size = bx::alignUp(uniformBuffer->m_size + _grow, 16); + uint32_t size = bx::alignUp(uniformBuffer->m_size + kIncrement, 16); void* data = bx::realloc(g_allocator, uniformBuffer, size+structSize); uniformBuffer = reinterpret_cast(data); uniformBuffer->m_size = size; diff --git a/src/config.h b/src/config.h index 906a96fcf..5b3592afe 100644 --- a/src/config.h +++ b/src/config.h @@ -338,6 +338,16 @@ static_assert(bx::isPowerOf2(BGFX_CONFIG_MAX_VIEWS), "BGFX_CONFIG_MAX_VIEWS must # define BGFX_CONFIG_MIN_UNIFORM_BUFFER_SIZE (1<<20) #endif // BGFX_CONFIG_MIN_UNIFORM_BUFFER_SIZE +#ifndef BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_THRESHOLD_SIZE +/// Max amount of unused uniform buffer space before uniform buffer resize. +# define BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_THRESHOLD_SIZE (64<<10) +#endif // BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_THRESHOLD_SIZE + +#ifndef BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_INCREMENT_SIZE +/// Increment of uniform buffer resize. +# define BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_INCREMENT_SIZE (1<<20) +#endif // BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_INCREMENT_SIZE + #ifndef BGFX_CONFIG_CACHED_DEVICE_MEMORY_ALLOCATIONS_SIZE /// Amount of allowed memory allocations left on device to use for recycling during /// later allocations. This can be beneficial in case the driver is slow allocating memory