From d6cb01f72df5007586eaa8628e0ed0f1dd52f1d7 Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Sun, 10 Feb 2013 18:12:52 -0800 Subject: [PATCH] Fixed sampler state ref leak. --- src/renderer_d3d.h | 14 ++++++++++++++ src/renderer_d3d11.cpp | 16 ++++++++++++++-- src/renderer_d3d11.h | 8 +------- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/renderer_d3d.h b/src/renderer_d3d.h index a365f132..7d1abe79 100644 --- a/src/renderer_d3d.h +++ b/src/renderer_d3d.h @@ -34,6 +34,12 @@ namespace bgfx #endif // BGFX_CONFIG_DEBUG #if BGFX_CONFIG_DEBUG +# define DX_CHECK_REFCOUNT(_ptr, _expected) \ + do { \ + ULONG count = getRefCount(_ptr); \ + BX_CHECK(_expected == count, "RefCount is %d (expected %d).", count, _expected); \ + } while (0) + # define DX_RELEASE(_ptr, _expected) \ do { \ if (NULL != _ptr) \ @@ -44,6 +50,7 @@ namespace bgfx } \ } while (0) #else +# define DX_CHECK_REFCOUNT(_ptr, _expected) # define DX_RELEASE(_ptr, _expected) \ do { \ if (NULL != _ptr) \ @@ -53,6 +60,13 @@ namespace bgfx } \ } while (0) #endif // BGFX_CONFIG_DEBUG + + inline int getRefCount(IUnknown* _interface) + { + _interface->AddRef(); + return _interface->Release(); + } + } // namespace bgfx #endif // __RENDERER_D3D_H__ diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index fff62295..235c06a4 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -1423,6 +1423,7 @@ namespace bgfx desc.MinLOD = 0; desc.MaxLOD = D3D11_FLOAT32_MAX; s_renderCtx.m_device->CreateSamplerState(&desc, &m_sampler); + DX_CHECK_REFCOUNT(m_sampler, 1); s_renderCtx.m_samplerStateCache.add(_flags, m_sampler); } @@ -1750,6 +1751,12 @@ namespace bgfx } } + void Texture::destroy() + { + DX_RELEASE(m_srv, 0); + DX_RELEASE(m_ptr, 0); + } + void Texture::commit(uint8_t _stage) { s_renderCtx.m_textureStage.m_srv[_stage] = m_srv; @@ -1834,7 +1841,11 @@ namespace bgfx // DX_CHECK(s_renderCtx.m_device->CreateShaderResourceView(m_depthTexture, NULL, &m_srv) ); } - m_sampler = s_renderCtx.m_samplerStateCache.find(_flags); + _textureFlags &= BGFX_TEXTURE_MIN_MASK|BGFX_TEXTURE_MAG_MASK|BGFX_TEXTURE_MIP_MASK + | BGFX_TEXTURE_U_MASK|BGFX_TEXTURE_V_MASK|BGFX_TEXTURE_W_MASK + ; + + m_sampler = s_renderCtx.m_samplerStateCache.find(_textureFlags); if (NULL == m_sampler) { D3D11_SAMPLER_DESC desc; @@ -1852,8 +1863,9 @@ namespace bgfx desc.MinLOD = 0; desc.MaxLOD = D3D11_FLOAT32_MAX; s_renderCtx.m_device->CreateSamplerState(&desc, &m_sampler); + DX_CHECK_REFCOUNT(m_sampler, 1); - s_renderCtx.m_samplerStateCache.add(_flags, m_sampler); + s_renderCtx.m_samplerStateCache.add(_textureFlags, m_sampler); } } diff --git a/src/renderer_d3d11.h b/src/renderer_d3d11.h index 93dae02c..c6165832 100644 --- a/src/renderer_d3d11.h +++ b/src/renderer_d3d11.h @@ -238,13 +238,7 @@ namespace bgfx } void create(const Memory* _mem, uint32_t _flags); - - void destroy() - { - DX_RELEASE(m_srv, 0); - DX_RELEASE(m_ptr, 0); - } - + void destroy(); void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem); void commit(uint8_t _stage);