diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index a09c9eccd..1e36af039 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -1759,11 +1759,17 @@ BX_PRAGMA_DIAGNOSTIC_POP(); void overrideInternal(TextureHandle _handle, uintptr_t _ptr) BX_OVERRIDE { + // Resource ref. counts might be messed up outside of bgfx. + // Disabling ref. count check once texture is overridden. + setGraphicsDebuggerPresent(true); m_textures[_handle.idx].overrideInternal(_ptr); } uintptr_t getInternal(TextureHandle _handle) BX_OVERRIDE { + // Resource ref. counts might be messed up outside of bgfx. + // Disabling ref. count check once texture is overridden. + setGraphicsDebuggerPresent(true); return uintptr_t(m_textures[_handle.idx].m_ptr); } diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index 879762d00..8214e9c2b 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -999,11 +999,17 @@ namespace bgfx { namespace d3d9 void overrideInternal(TextureHandle _handle, uintptr_t _ptr) BX_OVERRIDE { - BX_UNUSED(_handle, _ptr); + // Resource ref. counts might be messed up outside of bgfx. + // Disabling ref. count check once texture is overridden. + setGraphicsDebuggerPresent(true); + m_textures[_handle.idx].overrideInternal(_ptr); } uintptr_t getInternal(TextureHandle _handle) BX_OVERRIDE { + // Resource ref. counts might be messed up outside of bgfx. + // Disabling ref. count check once texture is overridden. + setGraphicsDebuggerPresent(true); return uintptr_t(m_textures[_handle.idx].m_ptr); } diff --git a/src/renderer_d3d9.h b/src/renderer_d3d9.h index 0bef17370..aecfba573 100644 --- a/src/renderer_d3d9.h +++ b/src/renderer_d3d9.h @@ -325,12 +325,22 @@ namespace bgfx { namespace d3d9 void destroy() { - DX_RELEASE(m_ptr, 0); + if (0 == (m_flags & BGFX_TEXTURE_INTERNAL_SHARED) ) + { + DX_RELEASE(m_ptr, 0); + } DX_RELEASE(m_surface, 0); DX_RELEASE(m_staging, 0); m_textureFormat = TextureFormat::Unknown; } + void overrideInternal(uintptr_t _ptr) + { + destroy(); + m_flags |= BGFX_TEXTURE_INTERNAL_SHARED; + m_ptr = (IDirect3DBaseTexture9*)_ptr; + } + void updateBegin(uint8_t _side, uint8_t _mip); void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem); void updateEnd(); diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 7d800ada4..be4ab0a09 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -2230,7 +2230,7 @@ namespace bgfx { namespace gl void overrideInternal(TextureHandle _handle, uintptr_t _ptr) BX_OVERRIDE { - BX_UNUSED(_handle, _ptr); + m_textures[_handle.idx].overrideInternal(_ptr); } uintptr_t getInternal(TextureHandle _handle) BX_OVERRIDE @@ -4271,7 +4271,8 @@ namespace bgfx { namespace gl void TextureGL::destroy() { - if (0 != m_id) + if (0 == (m_flags & BGFX_TEXTURE_INTERNAL_SHARED) + && 0 != m_id) { GL_CHECK(glBindTexture(m_target, 0) ); GL_CHECK(glDeleteTextures(1, &m_id) ); @@ -4285,6 +4286,13 @@ namespace bgfx { namespace gl } } + void TextureGL::overrideInternal(uintptr_t _ptr) + { + destroy(); + m_flags |= BGFX_TEXTURE_INTERNAL_SHARED; + m_id = (GLuint)_ptr; + } + void TextureGL::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem) { BX_UNUSED(_z, _depth); diff --git a/src/renderer_gl.h b/src/renderer_gl.h index b38ed1061..06b1525fd 100644 --- a/src/renderer_gl.h +++ b/src/renderer_gl.h @@ -1122,6 +1122,7 @@ namespace bgfx { namespace gl bool init(GLenum _target, uint32_t _width, uint32_t _height, uint32_t _depth, TextureFormat::Enum _format, uint8_t _numMips, uint32_t _flags); void create(const Memory* _mem, uint32_t _flags, uint8_t _skip); void destroy(); + void overrideInternal(uintptr_t _ptr); void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem); void setSamplerState(uint32_t _flags, const float _rgba[4]); void commit(uint32_t _stage, uint32_t _flags, const float _palette[][4]);