Skip to content

Commit

Permalink
Added configurable minimum resource command buffer size.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkaradzic committed Jun 4, 2020
1 parent 0a80a7b commit 8065659
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 28 deletions.
2 changes: 2 additions & 0 deletions bindings/cs/bgfx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,7 @@ public unsafe struct Limits
public uint maxUniforms;
public uint maxOcclusionQueries;
public uint maxEncoders;
public uint minResourceCbSize;
public uint transientVbSize;
public uint transientIbSize;
}
Expand Down Expand Up @@ -2016,6 +2017,7 @@ public unsafe struct Init
public unsafe struct Limits
{
public ushort maxEncoders;
public uint minResourceCbSize;
public uint transientVbSize;
public uint transientIbSize;
}
Expand Down
4 changes: 3 additions & 1 deletion bindings/d/types.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
}
Expand Down Expand Up @@ -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.
}
Expand Down
8 changes: 5 additions & 3 deletions include/bgfx/bgfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
};
Expand Down
2 changes: 2 additions & 0 deletions include/bgfx/c99/bgfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */

Expand Down Expand Up @@ -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. */

Expand Down
2 changes: 1 addition & 1 deletion include/bgfx/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 6 additions & 4 deletions scripts/bgfx.idl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- vim: syntax=lua
-- bgfx interface

version(107)
version(108)

typedef "bool"
typedef "char"
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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 }
Expand Down
17 changes: 13 additions & 4 deletions src/bgfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -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);
Expand All @@ -1616,6 +1622,7 @@ namespace bgfx
LIMITS(maxUniforms);
LIMITS(maxOcclusionQueries);
LIMITS(maxEncoders);
LIMITS(minResourceCbSize);
LIMITS(transientVbSize);
LIMITS(transientIbSize);
#undef LIMITS
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;

Expand Down
54 changes: 42 additions & 12 deletions src/bgfx_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -888,7 +907,7 @@ namespace bgfx
void start()
{
m_pos = 0;
m_size = BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE;
m_size = 0;
}

void finish()
Expand All @@ -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;
};

//
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8065659

Please sign in to comment.