diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 5a5aad31..db3bb331 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -19,14 +19,6 @@ namespace bgfx # define BGFX_CHECK_RENDER_THREAD() #endif // BGFX_CONFIG_MULTITHREADED && !BX_PLATFORM_OSX && !BX_PLATFORM_IOS -#define BGFX_CHECK_HANDLE(_handle, _max) \ - BX_CHECK(isValid(_handle) \ - && _handle.idx < _max \ - , "Invalid handle. %d (< %d " #_max ")" \ - , _handle.idx \ - , _max \ - ); - #if BX_PLATFORM_ANDROID ::ANativeWindow* g_bgfxAndroidWindow = NULL; @@ -2772,21 +2764,18 @@ again: void setUniform(UniformHandle _handle, const void* _value, uint16_t _num) { BGFX_CHECK_MAIN_THREAD(); - BGFX_CHECK_HANDLE(_handle, BGFX_CONFIG_MAX_UNIFORMS); s_ctx->setUniform(_handle, _value, _num); } void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices) { BGFX_CHECK_MAIN_THREAD(); - BGFX_CHECK_HANDLE(_handle, BGFX_CONFIG_MAX_INDEX_BUFFERS); s_ctx->setIndexBuffer(_handle, _firstIndex, _numIndices); } void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices) { BGFX_CHECK_MAIN_THREAD(); - BGFX_CHECK_HANDLE(_handle, BGFX_CONFIG_MAX_DYNAMIC_INDEX_BUFFERS); s_ctx->setIndexBuffer(_handle, _firstIndex, _numIndices); } @@ -2811,14 +2800,12 @@ again: void setVertexBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _numVertices) { BGFX_CHECK_MAIN_THREAD(); - BGFX_CHECK_HANDLE(_handle, BGFX_CONFIG_MAX_VERTEX_BUFFERS); s_ctx->setVertexBuffer(_handle, _startVertex, _numVertices); } void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices) { BGFX_CHECK_MAIN_THREAD(); - BGFX_CHECK_HANDLE(_handle, BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS); s_ctx->setVertexBuffer(_handle, _numVertices); } @@ -2844,21 +2831,18 @@ again: void setInstanceDataBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _num) { BGFX_CHECK_MAIN_THREAD(); - BGFX_CHECK_HANDLE(_handle, BGFX_CONFIG_MAX_VERTEX_BUFFERS); s_ctx->setInstanceDataBuffer(_handle, _startVertex, _num); } void setInstanceDataBuffer(DynamicVertexBufferHandle _handle, uint32_t _startVertex, uint32_t _num) { BGFX_CHECK_MAIN_THREAD(); - BGFX_CHECK_HANDLE(_handle, BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS); s_ctx->setInstanceDataBuffer(_handle, _startVertex, _num); } void setProgram(ProgramHandle _handle) { BGFX_CHECK_MAIN_THREAD(); - BGFX_CHECK_HANDLE(_handle, BGFX_CONFIG_MAX_PROGRAMS); s_ctx->setProgram(_handle); } @@ -2883,28 +2867,24 @@ again: void setBuffer(uint8_t _stage, IndexBufferHandle _handle, Access::Enum _access) { BGFX_CHECK_MAIN_THREAD(); - BGFX_CHECK_HANDLE(_handle, BGFX_CONFIG_MAX_INDEX_BUFFERS); s_ctx->setBuffer(_stage, _handle, _access); } void setBuffer(uint8_t _stage, VertexBufferHandle _handle, Access::Enum _access) { BGFX_CHECK_MAIN_THREAD(); - BGFX_CHECK_HANDLE(_handle, BGFX_CONFIG_MAX_VERTEX_BUFFERS); s_ctx->setBuffer(_stage, _handle, _access); } void setBuffer(uint8_t _stage, DynamicIndexBufferHandle _handle, Access::Enum _access) { BGFX_CHECK_MAIN_THREAD(); - BGFX_CHECK_HANDLE(_handle, BGFX_CONFIG_MAX_DYNAMIC_INDEX_BUFFERS); s_ctx->setBuffer(_stage, _handle, _access); } void setBuffer(uint8_t _stage, DynamicVertexBufferHandle _handle, Access::Enum _access) { BGFX_CHECK_MAIN_THREAD(); - BGFX_CHECK_HANDLE(_handle, BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS); s_ctx->setBuffer(_stage, _handle, _access); } diff --git a/src/bgfx_p.h b/src/bgfx_p.h index c5b14612..b17b7b31 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -29,6 +29,15 @@ #include #include +#define BGFX_CHECK_HANDLE(_desc, _handleAlloc, _handle) \ + BX_CHECK(isValid(_handle) \ + && _handleAlloc.isValid(_handle.idx) \ + , "Invalid handle. %s handle: %d (max %d)" \ + , _desc \ + , _handle.idx \ + , _handleAlloc.getMaxHandles() \ + ); + namespace bgfx { #if BX_COMPILER_CLANG_ANALYZER @@ -1420,7 +1429,10 @@ namespace bgfx { Binding& sampler = m_draw.m_bind[_stage]; sampler.m_idx = _handle.idx; - sampler.m_un.m_draw.m_flags = (_flags&BGFX_SAMPLER_DEFAULT_FLAGS) ? BGFX_SAMPLER_DEFAULT_FLAGS : _flags; + sampler.m_un.m_draw.m_flags = (_flags&BGFX_SAMPLER_DEFAULT_FLAGS) + ? BGFX_SAMPLER_DEFAULT_FLAGS + : _flags + ; if (isValid(_sampler) && (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES) ) ) @@ -1991,6 +2003,8 @@ namespace bgfx BGFX_API_FUNC(void destroyIndexBuffer(IndexBufferHandle _handle) ) { + BGFX_CHECK_HANDLE("destroyIndexBuffer", m_indexBufferHandle, _handle); + CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyIndexBuffer); cmdbuf.write(_handle); m_submit->free(_handle); @@ -2036,6 +2050,8 @@ namespace bgfx BGFX_API_FUNC(void destroyVertexBuffer(VertexBufferHandle _handle) ) { + BGFX_CHECK_HANDLE("destroyVertexBuffer", m_vertexBufferHandle, _handle); + CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyVertexBuffer); cmdbuf.write(_handle); m_submit->free(_handle); @@ -2137,6 +2153,8 @@ namespace bgfx BGFX_API_FUNC(void updateDynamicIndexBuffer(DynamicIndexBufferHandle _handle, const Memory* _mem) ) { + BGFX_CHECK_HANDLE("updateDynamicIndexBuffer", m_dynamicIndexBufferHandle, _handle); + DynamicIndexBuffer& dib = m_dynamicIndexBuffers[_handle.idx]; BX_CHECK(0 == (dib.m_flags & BGFX_BUFFER_COMPUTE_READ_WRITE), "Can't update GPU buffer from CPU."); @@ -2168,6 +2186,8 @@ namespace bgfx BGFX_API_FUNC(void destroyDynamicIndexBuffer(DynamicIndexBufferHandle _handle) ) { + BGFX_CHECK_HANDLE("destroyDynamicIndexBuffer", m_dynamicIndexBufferHandle, _handle); + m_freeDynamicIndexBufferHandle[m_numFreeDynamicIndexBufferHandles++] = _handle; } @@ -2281,6 +2301,8 @@ namespace bgfx BGFX_API_FUNC(void updateDynamicVertexBuffer(DynamicVertexBufferHandle _handle, const Memory* _mem) ) { + BGFX_CHECK_HANDLE("updateDynamicVertexBuffer", m_dynamicVertexBufferHandle, _handle); + DynamicVertexBuffer& dvb = m_dynamicVertexBuffers[_handle.idx]; BX_CHECK(0 == (dvb.m_flags & BGFX_BUFFER_COMPUTE_READ_WRITE), "Can't update GPU buffer from CPU."); @@ -2313,6 +2335,8 @@ namespace bgfx BGFX_API_FUNC(void destroyDynamicVertexBuffer(DynamicVertexBufferHandle _handle) ) { + BGFX_CHECK_HANDLE("destroyDynamicVertexBuffer", m_dynamicVertexBufferHandle, _handle); + m_freeDynamicVertexBufferHandle[m_numFreeDynamicVertexBufferHandles++] = _handle; } @@ -2595,6 +2619,8 @@ namespace bgfx BGFX_API_FUNC(void destroyShader(ShaderHandle _handle) ) { + BGFX_CHECK_HANDLE("destroyShader", m_shaderHandle, _handle); + if (!isValid(_handle) ) { BX_WARN(false, "Passing invalid shader handle to bgfx::destroyShader."); @@ -2706,6 +2732,8 @@ namespace bgfx BGFX_API_FUNC(void destroyProgram(ProgramHandle _handle) ) { + BGFX_CHECK_HANDLE("destroyProgram", m_programHandle, _handle); + CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyProgram); cmdbuf.write(_handle); m_submit->free(_handle); @@ -2771,6 +2799,8 @@ namespace bgfx BGFX_API_FUNC(void destroyTexture(TextureHandle _handle) ) { + BGFX_CHECK_HANDLE("destroyTexture", m_textureHandle, _handle); + if (!isValid(_handle) ) { BX_WARN(false, "Passing invalid texture handle to bgfx::destroyTexture"); @@ -2834,6 +2864,7 @@ namespace bgfx for (uint32_t ii = 0; ii < _num; ++ii) { TextureHandle texHandle = _handles[ii]; + BGFX_CHECK_HANDLE("createFrameBuffer texture handle", m_textureHandle, texHandle); cmdbuf.write(texHandle); @@ -2870,6 +2901,8 @@ namespace bgfx BGFX_API_FUNC(void destroyFrameBuffer(FrameBufferHandle _handle) ) { + BGFX_CHECK_HANDLE("destroyFrameBuffer", m_frameBufferHandle, _handle); + CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyFrameBuffer); cmdbuf.write(_handle); m_submit->free(_handle); @@ -2953,6 +2986,8 @@ namespace bgfx BGFX_API_FUNC(void destroyUniform(UniformHandle _handle) ) { + BGFX_CHECK_HANDLE("destroyUniform", m_uniformHandle, _handle); + UniformRef& uniform = m_uniformRef[_handle.idx]; BX_CHECK(uniform.m_refCount > 0, "Destroying already destroyed uniform %d.", _handle.idx); int32_t refs = --uniform.m_refCount; @@ -3056,6 +3091,7 @@ namespace bgfx BGFX_API_FUNC(void setViewFrameBuffer(uint8_t _id, FrameBufferHandle _handle) ) { + BGFX_CHECK_HANDLE("setViewFrameBuffer", m_frameBufferHandle, _handle); m_fb[_id] = _handle; } @@ -3150,6 +3186,7 @@ namespace bgfx BGFX_API_FUNC(void setUniform(UniformHandle _handle, const void* _value, uint16_t _num) ) { + BGFX_CHECK_HANDLE("setUniform", m_uniformHandle, _handle); UniformRef& uniform = m_uniformRef[_handle.idx]; BX_CHECK(uniform.m_num >= _num, "Truncated uniform update. %d (max: %d)", _num, uniform.m_num); m_submit->writeUniform(uniform.m_type, _handle, _value, bx::uint16_min(uniform.m_num, _num) ); @@ -3157,11 +3194,13 @@ namespace bgfx BGFX_API_FUNC(void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices) ) { + BGFX_CHECK_HANDLE("setIndexBuffer", m_indexBufferHandle, _handle); m_submit->setIndexBuffer(_handle, _firstIndex, _numIndices); } BGFX_API_FUNC(void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices) ) { + BGFX_CHECK_HANDLE("setIndexBuffer", m_dynamicIndexBufferHandle, _handle); m_submit->setIndexBuffer(m_dynamicIndexBuffers[_handle.idx], _firstIndex, _numIndices); } @@ -3172,11 +3211,13 @@ namespace bgfx BGFX_API_FUNC(void setVertexBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _numVertices) ) { + BGFX_CHECK_HANDLE("setVertexBuffer", m_vertexBufferHandle, _handle); m_submit->setVertexBuffer(_handle, _startVertex, _numVertices); } BGFX_API_FUNC(void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices) ) { + BGFX_CHECK_HANDLE("setVertexBuffer", m_dynamicVertexBufferHandle, _handle); m_submit->setVertexBuffer(m_dynamicVertexBuffers[_handle.idx], _numVertices); } @@ -3194,12 +3235,14 @@ namespace bgfx BGFX_API_FUNC(void setInstanceDataBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _num) ) { + BGFX_CHECK_HANDLE("setInstanceDataBuffer", m_vertexBufferHandle, _handle); const VertexBuffer& vb = m_vertexBuffers[_handle.idx]; m_submit->setInstanceDataBuffer(_handle, _startVertex, _num, vb.m_stride); } BGFX_API_FUNC(void setInstanceDataBuffer(DynamicVertexBufferHandle _handle, uint32_t _startVertex, uint32_t _num) ) { + BGFX_CHECK_HANDLE("setInstanceDataBuffer", m_dynamicVertexBufferHandle, _handle); const DynamicVertexBuffer& dvb = m_dynamicVertexBuffers[_handle.idx]; m_submit->setInstanceDataBuffer(dvb.m_handle , dvb.m_startVertex + _startVertex @@ -3210,11 +3253,13 @@ namespace bgfx BGFX_API_FUNC(void setProgram(ProgramHandle _handle) ) { + BGFX_CHECK_HANDLE("setProgram", m_programHandle, _handle); m_submit->setProgram(_handle); } BGFX_API_FUNC(void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint32_t _flags) ) { + BGFX_CHECK_HANDLE("setTexture", m_textureHandle, _handle); m_submit->setTexture(_stage, _sampler, _handle, _flags); } @@ -3240,22 +3285,26 @@ namespace bgfx BGFX_API_FUNC(void setBuffer(uint8_t _stage, IndexBufferHandle _handle, Access::Enum _access) ) { + BGFX_CHECK_HANDLE("setBuffer", m_indexBufferHandle, _handle); m_submit->setBuffer(_stage, _handle, _access); } BGFX_API_FUNC(void setBuffer(uint8_t _stage, VertexBufferHandle _handle, Access::Enum _access) ) { + BGFX_CHECK_HANDLE("setBuffer", m_vertexBufferHandle, _handle); m_submit->setBuffer(_stage, _handle, _access); } BGFX_API_FUNC(void setBuffer(uint8_t _stage, DynamicIndexBufferHandle _handle, Access::Enum _access) ) { + BGFX_CHECK_HANDLE("setBuffer", m_dynamicIndexBufferHandle, _handle); const DynamicIndexBuffer& dib = m_dynamicIndexBuffers[_handle.idx]; m_submit->setBuffer(_stage, dib.m_handle, _access); } BGFX_API_FUNC(void setBuffer(uint8_t _stage, DynamicVertexBufferHandle _handle, Access::Enum _access) ) { + BGFX_CHECK_HANDLE("setBuffer", m_dynamicVertexBufferHandle, _handle); const DynamicVertexBuffer& dvb = m_dynamicVertexBuffers[_handle.idx]; m_submit->setBuffer(_stage, dvb.m_handle, _access); }