From a5f36f526c12b2a36d0254f0165d8beae429a203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 2 Nov 2017 18:21:40 -0700 Subject: [PATCH] Fixed dynamic vertex buffer allocation failure. --- src/bgfx_p.h | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 75681c8d1..181f6a437 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -2703,8 +2703,12 @@ namespace bgfx if (!isValid(declHandle) ) { - VertexDeclHandle temp = { m_vertexDeclHandle.alloc() }; - declHandle = temp; + declHandle.idx = m_vertexDeclHandle.alloc(); + if (!isValid(declHandle) ) + { + return declHandle; + } + CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateVertexDecl); cmdbuf.write(declHandle); cmdbuf.write(_decl); @@ -2717,10 +2721,16 @@ namespace bgfx { VertexBufferHandle handle = { m_vertexBufferHandle.alloc() }; - BX_WARN(isValid(handle), "Failed to allocate vertex buffer handle."); if (isValid(handle) ) { VertexDeclHandle declHandle = findVertexDecl(_decl); + if (!isValid(declHandle) ) + { + BX_TRACE("WARNING: Failed to allocate vertex decl handle (BGFX_CONFIG_MAX_VERTEX_DECLS, max: %d).", BGFX_CONFIG_MAX_VERTEX_DECLS); + m_vertexBufferHandle.free(handle.idx); + return BGFX_INVALID_HANDLE; + } + m_declRef.add(handle, declHandle, _decl.m_hash); m_vertexBuffers[handle.idx].m_stride = _decl.m_stride; @@ -2730,13 +2740,14 @@ namespace bgfx cmdbuf.write(_mem); cmdbuf.write(declHandle); cmdbuf.write(_flags); - } - else - { - release(_mem); + + return handle; } - return handle; + BX_TRACE("WARNING: Failed to allocate vertex buffer handle (BGFX_CONFIG_MAX_VERTEX_BUFFERS, max: %d).", BGFX_CONFIG_MAX_VERTEX_BUFFERS); + release(_mem); + + return BGFX_INVALID_HANDLE; } BGFX_API_FUNC(void destroyVertexBuffer(VertexBufferHandle _handle) ) @@ -2960,6 +2971,7 @@ namespace bgfx VertexBufferHandle vertexBufferHandle = { m_vertexBufferHandle.alloc() }; if (!isValid(vertexBufferHandle) ) { + BX_TRACE("WARNING: Failed to allocate vertex buffer handle (BGFX_CONFIG_MAX_VERTEX_BUFFERS, max: %d).", BGFX_CONFIG_MAX_VERTEX_BUFFERS); return handle; } @@ -2980,8 +2992,19 @@ namespace bgfx } VertexDeclHandle declHandle = findVertexDecl(_decl); + if (!isValid(declHandle) ) + { + BX_TRACE("WARNING: Failed to allocate vertex decl handle (BGFX_CONFIG_MAX_VERTEX_DECLS, max: %d).", BGFX_CONFIG_MAX_VERTEX_DECLS); + return handle; + } handle.idx = m_dynamicVertexBufferHandle.alloc(); + if (!isValid(handle) ) + { + BX_TRACE("WARNING: Failed to allocate dynamic vertex buffer handle (BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS, max: %d).", BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS); + return handle; + } + DynamicVertexBuffer& dvb = m_dynamicVertexBuffers[handle.idx]; dvb.m_handle.idx = uint16_t(ptr>>32); dvb.m_offset = uint32_t(ptr);