Browse Source

Added configurable minimum resource command buffer size.

Бранимир Караџић 5 years ago
parent
commit
8065659e90
9 changed files with 77 additions and 28 deletions
  1. 2 0
      bindings/cs/bgfx.cs
  2. 3 1
      bindings/d/types.d
  3. 5 3
      include/bgfx/bgfx.h
  4. 2 0
      include/bgfx/c99/bgfx.h
  5. 1 1
      include/bgfx/defines.h
  6. 6 4
      scripts/bgfx.idl
  7. 13 4
      src/bgfx.cpp
  8. 42 12
      src/bgfx_p.h
  9. 3 3
      src/config.h

+ 2 - 0
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;
 		}

+ 3 - 1
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.
 }

+ 5 - 3
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.
 		};

+ 2 - 0
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.     */
 

+ 1 - 1
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.

+ 6 - 4
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 }

+ 13 - 4
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;
 

+ 42 - 12
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;
 

+ 3 - 3
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)