From 8065659e90a2673dd2ac4b12f193604a631833e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Wed, 3 Jun 2020 17:58:54 -0700 Subject: [PATCH] Added configurable minimum resource command buffer size. --- bindings/cs/bgfx.cs | 2 ++ bindings/d/types.d | 4 ++- include/bgfx/bgfx.h | 8 +++--- include/bgfx/c99/bgfx.h | 2 ++ include/bgfx/defines.h | 2 +- scripts/bgfx.idl | 10 +++++--- src/bgfx.cpp | 17 ++++++++++--- src/bgfx_p.h | 54 ++++++++++++++++++++++++++++++++--------- src/config.h | 6 ++--- 9 files changed, 77 insertions(+), 28 deletions(-) diff --git a/bindings/cs/bgfx.cs b/bindings/cs/bgfx.cs index fca974045..c7b8b3bab 100644 --- a/bindings/cs/bgfx.cs +++ b/bindings/cs/bgfx.cs @@ -1970,6 +1970,7 @@ public static partial class bgfx public uint maxUniforms; public uint maxOcclusionQueries; public uint maxEncoders; + public uint minResourceCbSize; public uint transientVbSize; public uint transientIbSize; } @@ -2016,6 +2017,7 @@ public static partial class bgfx public unsafe struct Limits { public ushort maxEncoders; + public uint minResourceCbSize; public uint transientVbSize; public uint transientIbSize; } diff --git a/bindings/d/types.d b/bindings/d/types.d index 208c36ada..008677811 100644 --- a/bindings/d/types.d +++ b/bindings/d/types.d @@ -10,7 +10,7 @@ public import core.stdc.stdarg : va_list; extern(C) @nogc nothrow: -enum uint BGFX_API_VERSION = 107; +enum uint BGFX_API_VERSION = 108; alias bgfx_view_id_t = ushort; @@ -723,6 +723,7 @@ struct bgfx_caps_limits_t uint maxUniforms; /// Maximum number of uniform handles. uint maxOcclusionQueries; /// Maximum number of occlusion query handles. uint maxEncoders; /// Maximum number of encoder threads. + uint minResourceCbSize; /// Minimum resource command buffer size. uint transientVbSize; /// Maximum transient vertex buffer size. uint transientIbSize; /// Maximum transient index buffer size. } @@ -818,6 +819,7 @@ struct bgfx_resolution_t struct bgfx_init_limits_t { ushort maxEncoders; /// Maximum number of encoder threads. + uint minResourceCbSize; /// Minimum resource command buffer size. uint transientVbSize; /// Maximum transient vertex buffer size. uint transientIbSize; /// Maximum transient index buffer size. } diff --git a/include/bgfx/bgfx.h b/include/bgfx/bgfx.h index f27f562f5..b363094c1 100644 --- a/include/bgfx/bgfx.h +++ b/include/bgfx/bgfx.h @@ -679,9 +679,10 @@ namespace bgfx { Limits(); - uint16_t maxEncoders; //!< Maximum number of encoder threads. - uint32_t transientVbSize; //!< Maximum transient vertex buffer size. - uint32_t transientIbSize; //!< Maximum transient index buffer size. + uint16_t maxEncoders; //!< Maximum number of encoder threads. + uint32_t minResourceCbSize; //!< Minimum resource command buffer size. + uint32_t transientVbSize; //!< Maximum transient vertex buffer size. + uint32_t transientIbSize; //!< Maximum transient index buffer size. }; Limits limits; // Configurable runtime limits. @@ -779,6 +780,7 @@ namespace bgfx uint32_t maxUniforms; //!< Maximum number of uniform handles. uint32_t maxOcclusionQueries; //!< Maximum number of occlusion query handles. uint32_t maxEncoders; //!< Maximum number of encoder threads. + uint32_t minResourceCbSize; //!< Minimum resource command buffer size. uint32_t transientVbSize; //!< Maximum transient vertex buffer size. uint32_t transientIbSize; //!< Maximum transient index buffer size. }; diff --git a/include/bgfx/c99/bgfx.h b/include/bgfx/c99/bgfx.h index 2e04803b6..8b379b720 100644 --- a/include/bgfx/c99/bgfx.h +++ b/include/bgfx/c99/bgfx.h @@ -514,6 +514,7 @@ typedef struct bgfx_caps_limits_s uint32_t maxUniforms; /** Maximum number of uniform handles. */ uint32_t maxOcclusionQueries; /** Maximum number of occlusion query handles. */ uint32_t maxEncoders; /** Maximum number of encoder threads. */ + uint32_t minResourceCbSize; /** Minimum resource command buffer size. */ uint32_t transientVbSize; /** Maximum transient vertex buffer size. */ uint32_t transientIbSize; /** Maximum transient index buffer size. */ @@ -629,6 +630,7 @@ typedef struct bgfx_resolution_s typedef struct bgfx_init_limits_s { uint16_t maxEncoders; /** Maximum number of encoder threads. */ + uint32_t minResourceCbSize; /** Minimum resource command buffer size. */ uint32_t transientVbSize; /** Maximum transient vertex buffer size. */ uint32_t transientIbSize; /** Maximum transient index buffer size. */ diff --git a/include/bgfx/defines.h b/include/bgfx/defines.h index 8de44bb75..96c48294b 100644 --- a/include/bgfx/defines.h +++ b/include/bgfx/defines.h @@ -15,7 +15,7 @@ #ifndef BGFX_DEFINES_H_HEADER_GUARD #define BGFX_DEFINES_H_HEADER_GUARD -#define BGFX_API_VERSION UINT32_C(107) +#define BGFX_API_VERSION UINT32_C(108) /** * Color RGB/alpha/depth write. When it's not specified write will be disabled. diff --git a/scripts/bgfx.idl b/scripts/bgfx.idl index 1e9dc6829..dd65dc30d 100644 --- a/scripts/bgfx.idl +++ b/scripts/bgfx.idl @@ -1,7 +1,7 @@ -- vim: syntax=lua -- bgfx interface -version(107) +version(108) typedef "bool" typedef "char" @@ -699,6 +699,7 @@ struct.Limits { namespace = "Caps" } .maxUniforms "uint32_t" --- Maximum number of uniform handles. .maxOcclusionQueries "uint32_t" --- Maximum number of occlusion query handles. .maxEncoders "uint32_t" --- Maximum number of encoder threads. + .minResourceCbSize "uint32_t" --- Minimum resource command buffer size. .transientVbSize "uint32_t" --- Maximum transient vertex buffer size. .transientIbSize "uint32_t" --- Maximum transient index buffer size. @@ -764,9 +765,10 @@ struct.Resolution { ctor } --- Configurable runtime limits parameters. struct.Limits { namespace = "Init" } - .maxEncoders "uint16_t" --- Maximum number of encoder threads. - .transientVbSize "uint32_t" --- Maximum transient vertex buffer size. - .transientIbSize "uint32_t" --- Maximum transient index buffer size. + .maxEncoders "uint16_t" --- Maximum number of encoder threads. + .minResourceCbSize "uint32_t" --- Minimum resource command buffer size. + .transientVbSize "uint32_t" --- Maximum transient vertex buffer size. + .transientIbSize "uint32_t" --- Maximum transient index buffer size. --- Initialization parameters used by `bgfx::init`. struct.Init { ctor } diff --git a/src/bgfx.cpp b/src/bgfx.cpp index f02fda082..1b19eb6eb 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -323,7 +323,13 @@ namespace bgfx // BK - CallbackStub will be deleted after printing this info, so there is always one // leak if CallbackStub is used. BX_WARN(uint32_t(NULL != s_callbackStub ? 1 : 0) == m_numBlocks - , "MEMORY LEAK: %d (max: %d)" + , "\n\n" + "\n########################################################" + "\n" + "\nMEMORY LEAK: Number of leaked blocks %d (Max blocks: %d)" + "\n" + "\n########################################################" + "\n\n" , m_numBlocks , m_maxBlocks ); @@ -1594,7 +1600,7 @@ namespace bgfx BX_TRACE(""); BX_TRACE("Limits:"); -#define LIMITS(_x) BX_TRACE("\t%-24s %d", #_x, g_caps.limits._x) +#define LIMITS(_x) BX_TRACE("\t%-24s%10d", #_x, g_caps.limits._x) LIMITS(maxDrawCalls); LIMITS(maxBlits); LIMITS(maxTextureSize); @@ -1616,6 +1622,7 @@ namespace bgfx LIMITS(maxUniforms); LIMITS(maxOcclusionQueries); LIMITS(maxEncoders); + LIMITS(minResourceCbSize); LIMITS(transientVbSize); LIMITS(transientIbSize); #undef LIMITS @@ -1807,10 +1814,10 @@ namespace bgfx m_debug = BGFX_DEBUG_NONE; m_frameTimeLast = bx::getHPCounter(); - m_submit->create(); + m_submit->create(_init.limits.minResourceCbSize); #if BGFX_CONFIG_MULTITHREADED - m_render->create(); + m_render->create(_init.limits.minResourceCbSize); if (s_renderFrameCalled) { @@ -3326,6 +3333,7 @@ namespace bgfx Init::Limits::Limits() : maxEncoders(BGFX_CONFIG_DEFAULT_MAX_ENCODERS) + , minResourceCbSize(BGFX_CONFIG_MIN_RESOURCE_COMMAND_BUFFER_SIZE) , transientVbSize(BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE) , transientIbSize(BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE) { @@ -3433,6 +3441,7 @@ namespace bgfx g_caps.limits.maxOcclusionQueries = BGFX_CONFIG_MAX_OCCLUSION_QUERIES; g_caps.limits.maxFBAttachments = 1; g_caps.limits.maxEncoders = (0 != BGFX_CONFIG_MULTITHREADED) ? _init.limits.maxEncoders : 1; + g_caps.limits.minResourceCbSize = _init.limits.minResourceCbSize; g_caps.limits.transientVbSize = _init.limits.transientVbSize; g_caps.limits.transientIbSize = _init.limits.transientIbSize; diff --git a/src/bgfx_p.h b/src/bgfx_p.h index bf8243b52..aaac2bc0e 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -771,12 +771,26 @@ namespace bgfx public: CommandBuffer() - : m_pos(0) - , m_size(BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE) + : m_buffer(NULL) + , m_pos(0) + , m_size(0) + , m_minCapacity(0) { + resize(); finish(); } + ~CommandBuffer() + { + BX_FREE(g_allocator, m_buffer); + } + + void init(uint32_t _minCapacity) + { + m_minCapacity = bx::alignUp(_minCapacity, 1024); + resize(); + } + enum Enum { RendererInit, @@ -814,15 +828,20 @@ namespace bgfx RequestScreenShot, }; + void resize(uint32_t _capacity = 0) + { + m_capacity = bx::alignUp(bx::max(_capacity, m_minCapacity), 1024); + m_buffer = (uint8_t*)BX_REALLOC(g_allocator, m_buffer, m_capacity); + } + void write(const void* _data, uint32_t _size) { - BX_CHECK(m_size == BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE, "Called write outside start/finish?"); - BX_CHECK(m_pos + _size <= m_size - , "CommandBuffer::write error (pos: %d-%d, size: %d)." - , m_pos - , m_pos + _size - , m_size - ); + BX_CHECK(m_size == 0, "Called write outside start/finish (m_size: %d)?", m_size); + if (m_pos + _size > m_capacity) + { + resize(m_capacity + (16<<10) ); + } + bx::memCopy(&m_buffer[m_pos], _data, _size); m_pos += _size; } @@ -888,7 +907,7 @@ namespace bgfx void start() { m_pos = 0; - m_size = BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE; + m_size = 0; } void finish() @@ -897,11 +916,19 @@ namespace bgfx write(cmd); m_size = m_pos; m_pos = 0; + + if (m_size < m_minCapacity + && m_capacity != m_minCapacity) + { + resize(); + } } + uint8_t* m_buffer; uint32_t m_pos; uint32_t m_size; - uint8_t m_buffer[BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE]; + uint32_t m_capacity; + uint32_t m_minCapacity; }; // @@ -1948,8 +1975,11 @@ constexpr uint64_t kSortKeyComputeProgramMask = uint64_t(BGFX_CONFIG_MAX_PROGRA { } - void create() + void create(uint32_t _minResourceCbSize) { + m_cmdPre.init(_minResourceCbSize); + m_cmdPost.init(_minResourceCbSize); + { const uint32_t num = g_caps.limits.maxEncoders; diff --git a/src/config.h b/src/config.h index 067ff7525..941100240 100644 --- a/src/config.h +++ b/src/config.h @@ -311,9 +311,9 @@ BX_STATIC_ASSERT(bx::isPowerOf2(BGFX_CONFIG_MAX_VIEWS), "BGFX_CONFIG_MAX_VIEWS m # define BGFX_CONFIG_MAX_OCCLUSION_QUERIES 256 #endif // BGFX_CONFIG_MAX_OCCLUSION_QUERIES -#ifndef BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE -# define BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE (64<<10) -#endif // BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE +#ifndef BGFX_CONFIG_MIN_RESOURCE_COMMAND_BUFFER_SIZE +# define BGFX_CONFIG_MIN_RESOURCE_COMMAND_BUFFER_SIZE (64<<10) +#endif // BGFX_CONFIG_MIN_RESOURCE_COMMAND_BUFFER_SIZE #ifndef BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE # define BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE (6<<20)