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