Cleanup.
This commit is contained in:
parent
9ea283ed5a
commit
cfa81f314a
@ -199,15 +199,20 @@ public:
|
||||
|
||||
// 80 bytes stride = 64 bytes for 4x4 matrix + 16 bytes for RGBA color.
|
||||
const uint16_t instanceStride = 80;
|
||||
const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(121, instanceStride);
|
||||
if (NULL != idb)
|
||||
// 11x11 cubes
|
||||
const uint32_t numInstances = 121;
|
||||
|
||||
if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride) )
|
||||
{
|
||||
uint8_t* data = idb->data;
|
||||
bgfx::InstanceDataBuffer idb;
|
||||
bgfx::allocInstanceDataBuffer(&idb, numInstances, instanceStride);
|
||||
|
||||
uint8_t* data = idb.data;
|
||||
|
||||
// Write instance data for 11x11 cubes.
|
||||
for (uint32_t yy = 0, numInstances = 0; yy < 11 && numInstances < idb->num; ++yy)
|
||||
for (uint32_t yy = 0; yy < 11; ++yy)
|
||||
{
|
||||
for (uint32_t xx = 0; xx < 11 && numInstances < idb->num; ++xx, ++numInstances)
|
||||
for (uint32_t xx = 0; xx < 11; ++xx)
|
||||
{
|
||||
float* mtx = (float*)data;
|
||||
bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
|
||||
@ -230,7 +235,7 @@ public:
|
||||
bgfx::setIndexBuffer(m_ibh);
|
||||
|
||||
// Set instance data buffer.
|
||||
bgfx::setInstanceDataBuffer(idb);
|
||||
bgfx::setInstanceDataBuffer(&idb);
|
||||
|
||||
// Set render states.
|
||||
bgfx::setState(BGFX_STATE_DEFAULT);
|
||||
|
@ -258,17 +258,19 @@ public:
|
||||
bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, m_numLights);
|
||||
|
||||
const uint16_t instanceStride = 64;
|
||||
const uint16_t numInstances = 3;
|
||||
const uint16_t numInstances = 3;
|
||||
|
||||
if (m_instancingSupported)
|
||||
{
|
||||
// Write instance data for 3x3 cubes.
|
||||
for (uint32_t yy = 0; yy < 3; ++yy)
|
||||
{
|
||||
const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(numInstances, instanceStride);
|
||||
if (NULL != idb)
|
||||
if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride) )
|
||||
{
|
||||
uint8_t* data = idb->data;
|
||||
bgfx::InstanceDataBuffer idb;
|
||||
bgfx::allocInstanceDataBuffer(&idb, numInstances, instanceStride);
|
||||
|
||||
uint8_t* data = idb.data;
|
||||
|
||||
for (uint32_t xx = 0; xx < 3; ++xx)
|
||||
{
|
||||
@ -282,7 +284,7 @@ public:
|
||||
}
|
||||
|
||||
// Set instance data buffer.
|
||||
bgfx::setInstanceDataBuffer(idb, numInstances);
|
||||
bgfx::setInstanceDataBuffer(&idb, numInstances);
|
||||
|
||||
// Set vertex and index buffer.
|
||||
bgfx::setVertexBuffer(0, m_vbh);
|
||||
|
@ -1531,7 +1531,8 @@ namespace bgfx
|
||||
///
|
||||
/// @attention C99 equivalent is `bgfx_alloc_transient_vertex_buffer`.
|
||||
///
|
||||
void allocTransientVertexBuffer(TransientVertexBuffer* _tvb
|
||||
void allocTransientVertexBuffer(
|
||||
TransientVertexBuffer* _tvb
|
||||
, uint32_t _num
|
||||
, const VertexDecl& _decl
|
||||
);
|
||||
@ -1545,7 +1546,8 @@ namespace bgfx
|
||||
///
|
||||
/// @attention C99 equivalent is `bgfx_alloc_transient_buffers`.
|
||||
///
|
||||
bool allocTransientBuffers(TransientVertexBuffer* _tvb
|
||||
bool allocTransientBuffers(
|
||||
TransientVertexBuffer* _tvb
|
||||
, const VertexDecl& _decl
|
||||
, uint32_t _numVertices
|
||||
, TransientIndexBuffer* _tib
|
||||
@ -1554,13 +1556,23 @@ namespace bgfx
|
||||
|
||||
/// Allocate instance data buffer.
|
||||
///
|
||||
/// @param[out] _idb InstanceDataBuffer structure is filled and is valid
|
||||
/// for duration of frame, and it can be reused for multiple draw
|
||||
/// calls.
|
||||
/// @param[in] _num Number of instances.
|
||||
/// @param[in] _stride Instance stride. Must be multiple of 16.
|
||||
///
|
||||
/// @remarks
|
||||
/// You must call setInstanceDataBuffer after alloc in order to avoid
|
||||
/// memory leak.
|
||||
///
|
||||
/// @attention C99 equivalent is `bgfx_alloc_instance_data_buffer`.
|
||||
///
|
||||
const InstanceDataBuffer* allocInstanceDataBuffer(uint32_t _num, uint16_t _stride);
|
||||
void allocInstanceDataBuffer(
|
||||
InstanceDataBuffer* _idb
|
||||
, uint32_t _num
|
||||
, uint16_t _stride
|
||||
);
|
||||
|
||||
/// Create draw indirect buffer.
|
||||
///
|
||||
|
@ -699,7 +699,7 @@ BGFX_C_API void bgfx_alloc_transient_vertex_buffer(bgfx_transient_vertex_buffer_
|
||||
BGFX_C_API bool bgfx_alloc_transient_buffers(bgfx_transient_vertex_buffer_t* _tvb, const bgfx_vertex_decl_t* _decl, uint32_t _numVertices, bgfx_transient_index_buffer_t* _tib, uint32_t _numIndices);
|
||||
|
||||
/**/
|
||||
BGFX_C_API const bgfx_instance_data_buffer_t* bgfx_alloc_instance_data_buffer(uint32_t _num, uint16_t _stride);
|
||||
BGFX_C_API void bgfx_alloc_instance_data_buffer(bgfx_instance_data_buffer_t* _idb, uint32_t _num, uint16_t _stride);
|
||||
|
||||
/**/
|
||||
BGFX_C_API bgfx_indirect_buffer_handle_t bgfx_create_indirect_buffer(uint32_t _num);
|
||||
|
@ -118,7 +118,7 @@ typedef struct bgfx_interface_vtbl
|
||||
void (*alloc_transient_index_buffer)(bgfx_transient_index_buffer_t* _tib, uint32_t _num);
|
||||
void (*alloc_transient_vertex_buffer)(bgfx_transient_vertex_buffer_t* _tvb, uint32_t _num, const bgfx_vertex_decl_t* _decl);
|
||||
bool (*alloc_transient_buffers)(bgfx_transient_vertex_buffer_t* _tvb, const bgfx_vertex_decl_t* _decl, uint32_t _numVertices, bgfx_transient_index_buffer_t* _tib, uint32_t _numIndices);
|
||||
const bgfx_instance_data_buffer_t* (*alloc_instance_data_buffer)(uint32_t _num, uint16_t _stride);
|
||||
void (*alloc_instance_data_buffer)(bgfx_instance_data_buffer_t* _idb, uint32_t _num, uint16_t _stride);
|
||||
bgfx_indirect_buffer_handle_t (*create_indirect_buffer)(uint32_t _num);
|
||||
void (*destroy_indirect_buffer)(bgfx_indirect_buffer_handle_t _handle);
|
||||
bgfx_shader_handle_t (*create_shader)(const bgfx_memory_t* _mem);
|
||||
|
@ -6,7 +6,7 @@
|
||||
#ifndef BGFX_DEFINES_H_HEADER_GUARD
|
||||
#define BGFX_DEFINES_H_HEADER_GUARD
|
||||
|
||||
#define BGFX_API_VERSION UINT32_C(51)
|
||||
#define BGFX_API_VERSION UINT32_C(52)
|
||||
|
||||
/// Color RGB/alpha/depth write. When it's not specified write will be disabled.
|
||||
#define BGFX_STATE_RGB_WRITE UINT64_C(0x0000000000000001) //!< Enable RGB write.
|
||||
|
10
src/bgfx.cpp
10
src/bgfx.cpp
@ -3071,17 +3071,17 @@ error:
|
||||
return false;
|
||||
}
|
||||
|
||||
const InstanceDataBuffer* allocInstanceDataBuffer(uint32_t _num, uint16_t _stride)
|
||||
void allocInstanceDataBuffer(InstanceDataBuffer* _idb, uint32_t _num, uint16_t _stride)
|
||||
{
|
||||
BGFX_CHECK_MAIN_THREAD();
|
||||
BGFX_CHECK_CAPS(BGFX_CAPS_INSTANCING, "Instancing is not supported!");
|
||||
BX_CHECK(_stride == BX_ALIGN_16(_stride), "Stride must be multiple of 16.");
|
||||
BX_CHECK(0 < _num, "Requesting 0 instanced data vertices.");
|
||||
const InstanceDataBuffer* idb = s_ctx->allocInstanceDataBuffer(_num, _stride);
|
||||
s_ctx->allocInstanceDataBuffer(_idb, _num, _stride);
|
||||
BX_CHECK(_num == idb->size / _stride, "Failed to allocate instance data buffer (requested %d, available %d). Use bgfx::checkAvailTransient* functions to ensure availability."
|
||||
, _num
|
||||
, idb->size / _stride
|
||||
);
|
||||
return idb;
|
||||
}
|
||||
|
||||
IndirectBufferHandle createIndirectBuffer(uint32_t _num)
|
||||
@ -4549,9 +4549,9 @@ BGFX_C_API bool bgfx_alloc_transient_buffers(bgfx_transient_vertex_buffer_t* _tv
|
||||
return bgfx::allocTransientBuffers( (bgfx::TransientVertexBuffer*)_tvb, decl, _numVertices, (bgfx::TransientIndexBuffer*)_tib, _numIndices);
|
||||
}
|
||||
|
||||
BGFX_C_API const bgfx_instance_data_buffer_t* bgfx_alloc_instance_data_buffer(uint32_t _num, uint16_t _stride)
|
||||
BGFX_C_API void bgfx_alloc_instance_data_buffer(bgfx_instance_data_buffer_t* _idb, uint32_t _num, uint16_t _stride)
|
||||
{
|
||||
return (bgfx_instance_data_buffer_t*)bgfx::allocInstanceDataBuffer(_num, _stride);
|
||||
bgfx::allocInstanceDataBuffer( (bgfx::InstanceDataBuffer*)_idb, _num, _stride);
|
||||
}
|
||||
|
||||
BGFX_C_API bgfx_indirect_buffer_handle_t bgfx_create_indirect_buffer(uint32_t _num)
|
||||
|
24
src/bgfx_p.h
24
src/bgfx_p.h
@ -1777,7 +1777,6 @@ namespace bgfx
|
||||
m_draw.m_instanceDataStride = _idb->stride;
|
||||
m_draw.m_numInstances = bx::uint32_min(_idb->num, _num);
|
||||
m_draw.m_instanceDataBuffer = _idb->handle;
|
||||
BX_FREE(g_allocator, const_cast<InstanceDataBuffer*>(_idb) );
|
||||
}
|
||||
|
||||
void setInstanceDataBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _num, uint16_t _stride)
|
||||
@ -2388,7 +2387,6 @@ namespace bgfx
|
||||
, m_numFreeDynamicVertexBufferHandles(0)
|
||||
, m_numFreeOcclusionQueryHandles(0)
|
||||
, m_colorPaletteDirty(0)
|
||||
, m_instBufferCount(0)
|
||||
, m_frames(0)
|
||||
, m_debug(BGFX_DEBUG_NONE)
|
||||
, m_renderCtx(NULL)
|
||||
@ -3061,23 +3059,18 @@ namespace bgfx
|
||||
_tvb->decl = declHandle;
|
||||
}
|
||||
|
||||
BGFX_API_FUNC(const InstanceDataBuffer* allocInstanceDataBuffer(uint32_t _num, uint16_t _stride) )
|
||||
BGFX_API_FUNC(void allocInstanceDataBuffer(InstanceDataBuffer* _idb, uint32_t _num, uint16_t _stride) )
|
||||
{
|
||||
++m_instBufferCount;
|
||||
|
||||
uint16_t stride = BX_ALIGN_16(_stride);
|
||||
uint32_t offset = m_submit->allocTransientVertexBuffer(_num, stride);
|
||||
|
||||
TransientVertexBuffer& dvb = *m_submit->m_transientVb;
|
||||
InstanceDataBuffer* idb = (InstanceDataBuffer*)BX_ALLOC(g_allocator, sizeof(InstanceDataBuffer) );
|
||||
idb->data = &dvb.data[offset];
|
||||
idb->size = _num * stride;
|
||||
idb->offset = offset;
|
||||
idb->num = _num;
|
||||
idb->stride = stride;
|
||||
idb->handle = dvb.handle;
|
||||
|
||||
return idb;
|
||||
_idb->data = &dvb.data[offset];
|
||||
_idb->size = _num * stride;
|
||||
_idb->offset = offset;
|
||||
_idb->num = _num;
|
||||
_idb->stride = stride;
|
||||
_idb->handle = dvb.handle;
|
||||
}
|
||||
|
||||
IndirectBufferHandle createIndirectBuffer(uint32_t _num)
|
||||
@ -4175,8 +4168,6 @@ namespace bgfx
|
||||
|
||||
BGFX_API_FUNC(void setInstanceDataBuffer(const InstanceDataBuffer* _idb, uint32_t _num) )
|
||||
{
|
||||
--m_instBufferCount;
|
||||
|
||||
m_submit->setInstanceDataBuffer(_idb, _num);
|
||||
}
|
||||
|
||||
@ -4528,7 +4519,6 @@ namespace bgfx
|
||||
|
||||
Resolution m_resolution;
|
||||
int64_t m_frameTimeLast;
|
||||
int32_t m_instBufferCount;
|
||||
uint32_t m_frames;
|
||||
uint32_t m_debug;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user