diff --git a/examples/common/font/text_buffer_manager.cpp b/examples/common/font/text_buffer_manager.cpp index b98be22b..72fa54fc 100644 --- a/examples/common/font/text_buffer_manager.cpp +++ b/examples/common/font/text_buffer_manager.cpp @@ -698,8 +698,6 @@ void TextBufferManager::submitTextBuffer(TextBufferHandle _handle, uint8_t _id, return; } - const bgfx::Memory* mem; - bgfx::setTexture(0, u_texColor, m_fontManager->getAtlas()->getTextureHandle() ); switch (bc.fontType) @@ -739,21 +737,22 @@ void TextBufferManager::submitTextBuffer(TextBufferHandle _handle, uint8_t _id, if (bgfx::invalidHandle == bc.vertexBufferHandleIdx) { - mem = bgfx::alloc(indexSize); - memcpy(mem->data, bc.textBuffer->getIndexBuffer(), indexSize); - ibh = bgfx::createIndexBuffer(mem); + ibh = bgfx::createIndexBuffer( + bgfx::copy(bc.textBuffer->getIndexBuffer(), indexSize) + ); - mem = bgfx::alloc(vertexSize); - memcpy(mem->data, bc.textBuffer->getVertexBuffer(), vertexSize); - vbh = bgfx::createVertexBuffer(mem, m_vertexDecl); + vbh = bgfx::createVertexBuffer( + bgfx::copy(bc.textBuffer->getVertexBuffer(), vertexSize) + , m_vertexDecl + ); - bc.indexBufferHandleIdx = ibh.idx; bc.vertexBufferHandleIdx = vbh.idx; + bc.indexBufferHandleIdx = ibh.idx; } else { - ibh.idx = bc.indexBufferHandleIdx; vbh.idx = bc.vertexBufferHandleIdx; + ibh.idx = bc.indexBufferHandleIdx; } bgfx::setVertexBuffer(vbh, 0, bc.textBuffer->getVertexCount() ); @@ -768,13 +767,14 @@ void TextBufferManager::submitTextBuffer(TextBufferHandle _handle, uint8_t _id, if (bgfx::invalidHandle == bc.vertexBufferHandleIdx ) { - mem = bgfx::alloc(indexSize); - memcpy(mem->data, bc.textBuffer->getIndexBuffer(), indexSize); - ibh = bgfx::createDynamicIndexBuffer(mem); + ibh = bgfx::createDynamicIndexBuffer( + bgfx::copy(bc.textBuffer->getIndexBuffer(), indexSize) + ); - mem = bgfx::alloc(vertexSize); - memcpy(mem->data, bc.textBuffer->getVertexBuffer(), vertexSize); - vbh = bgfx::createDynamicVertexBuffer(mem, m_vertexDecl); + vbh = bgfx::createDynamicVertexBuffer( + bgfx::copy(bc.textBuffer->getVertexBuffer(), vertexSize) + , m_vertexDecl + ); bc.indexBufferHandleIdx = ibh.idx; bc.vertexBufferHandleIdx = vbh.idx; @@ -784,13 +784,13 @@ void TextBufferManager::submitTextBuffer(TextBufferHandle _handle, uint8_t _id, ibh.idx = bc.indexBufferHandleIdx; vbh.idx = bc.vertexBufferHandleIdx; - mem = bgfx::alloc(indexSize); - memcpy(mem->data, bc.textBuffer->getIndexBuffer(), indexSize); - bgfx::updateDynamicIndexBuffer(ibh, mem); + bgfx::updateDynamicIndexBuffer(ibh + , bgfx::copy(bc.textBuffer->getIndexBuffer(), indexSize) + ); - mem = bgfx::alloc(vertexSize); - memcpy(mem->data, bc.textBuffer->getVertexBuffer(), vertexSize); - bgfx::updateDynamicVertexBuffer(vbh, mem); + bgfx::updateDynamicVertexBuffer(vbh + , bgfx::copy(bc.textBuffer->getVertexBuffer(), vertexSize) + ); } bgfx::setVertexBuffer(vbh, bc.textBuffer->getVertexCount() ); diff --git a/include/bgfx.h b/include/bgfx.h index 3e8e2338..9d577229 100644 --- a/include/bgfx.h +++ b/include/bgfx.h @@ -712,6 +712,9 @@ namespace bgfx /// Allocate buffer to pass to bgfx calls. Data will be freed inside bgfx. const Memory* alloc(uint32_t _size); + /// Allocate buffer and copy data into it. Data will be freed inside bgfx. + const Memory* copy(const void* _data, uint32_t _size); + /// Make reference to data to pass to bgfx. Unlike bgfx::alloc this call /// doesn't allocate memory for data. It just copies pointer to data. /// You must make sure data is available for at least 2 bgfx::frame calls. diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 1b0ab46b..cef0ca19 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -1709,6 +1709,13 @@ namespace bgfx return mem; } + const Memory* copy(const void* _data, uint32_t _size) + { + const Memory* mem = alloc(_size); + memcpy(mem->data, _data, _size); + return mem; + } + const Memory* makeRef(const void* _data, uint32_t _size) { Memory* mem = (Memory*)BX_ALLOC(g_allocator, sizeof(Memory) );