From 9c9dc90336f41d53a1dc07a815f2bc371ad31945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 7 Aug 2015 17:31:31 -0700 Subject: [PATCH] Fixed early texture destruction when multiple frame buffers take ownership. --- src/bgfx.cpp | 11 +---------- src/bgfx_p.h | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 46dbd211..dbe9adac 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -2778,16 +2778,7 @@ again: , BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS ); BX_CHECK(NULL != _handles, "_handles can't be NULL"); - FrameBufferHandle handle = s_ctx->createFrameBuffer(_num, _handles); - if (_destroyTextures) - { - for (uint32_t ii = 0; ii < _num; ++ii) - { - destroyTexture(_handles[ii]); - } - } - - return handle; + return s_ctx->createFrameBuffer(_num, _handles, _destroyTextures); } FrameBufferHandle createFrameBuffer(void* _nwh, uint16_t _width, uint16_t _height, TextureFormat::Enum _depthFormat) diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 510538f1..60029d1c 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -2839,6 +2839,7 @@ namespace bgfx ref.m_refCount = 1; ref.m_bbRatio = uint8_t(_ratio); ref.m_format = uint8_t(_info->format); + ref.m_owned = false; CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateTexture); cmdbuf.write(handle); @@ -2883,6 +2884,16 @@ namespace bgfx cmdbuf.write(_height); } + void textureTakeOwnership(TextureHandle _handle) + { + TextureRef& ref = m_textureRef[_handle.idx]; + if (!ref.m_owned) + { + ref.m_owned = true; + textureDecRef(_handle); + } + } + void textureIncRef(TextureHandle _handle) { TextureRef& ref = m_textureRef[_handle.idx]; @@ -2919,7 +2930,7 @@ namespace bgfx cmdbuf.write(_mem); } - BGFX_API_FUNC(FrameBufferHandle createFrameBuffer(uint8_t _num, TextureHandle* _handles) ) + BGFX_API_FUNC(FrameBufferHandle createFrameBuffer(uint8_t _num, TextureHandle* _handles, bool _destroyTextures) ) { FrameBufferHandle handle = { m_frameBufferHandle.alloc() }; BX_WARN(isValid(handle), "Failed to allocate frame buffer handle."); @@ -2949,6 +2960,14 @@ namespace bgfx } } + if (_destroyTextures) + { + for (uint32_t ii = 0; ii < _num; ++ii) + { + textureTakeOwnership(_handles[ii]); + } + } + return handle; } @@ -3565,6 +3584,7 @@ namespace bgfx int16_t m_refCount; uint8_t m_bbRatio; uint8_t m_format; + bool m_owned; }; struct FrameBufferRef