diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 25f268d2..f0428069 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -1233,6 +1233,11 @@ BX_PRAGMA_DIAGNOSTIC_POP(); DX_RELEASE(m_backBufferDepthStencil, 0); DX_RELEASE(m_backBufferColor, 0); + for (uint32_t ii = 0; ii < BX_COUNTOF(m_frameBuffers); ++ii) + { + m_frameBuffers[ii].preReset(); + } + // invalidateCache(); capturePreReset(); @@ -1274,6 +1279,11 @@ BX_PRAGMA_DIAGNOSTIC_POP(); m_currentColor = m_backBufferColor; m_currentDepthStencil = m_backBufferDepthStencil; + for (uint32_t ii = 0; ii < BX_COUNTOF(m_frameBuffers); ++ii) + { + m_frameBuffers[ii].postReset(); + } + capturePostReset(); } @@ -3016,35 +3026,10 @@ BX_PRAGMA_DIAGNOSTIC_POP(); m_dsv = NULL; m_swapChain = NULL; - m_num = 0; - for (uint32_t ii = 0; ii < _num; ++ii) - { - TextureHandle handle = _handles[ii]; - if (isValid(handle) ) - { - const TextureD3D11& texture = s_renderD3D11->m_textures[handle.idx]; - if (isDepth( (TextureFormat::Enum)texture.m_textureFormat) ) - { - BX_CHECK(NULL == m_dsv, "Frame buffer already has depth-stencil attached."); + m_numTh = _num; + memcpy(m_th, _handles, _num*sizeof(TextureHandle) ); - const uint32_t msaaQuality = bx::uint32_satsub( (texture.m_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT, 1); - const DXGI_SAMPLE_DESC& msaa = s_msaa[msaaQuality]; - - D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc; - dsvDesc.Format = s_textureFormat[texture.m_textureFormat].m_fmtDsv; - dsvDesc.ViewDimension = 1 < msaa.Count ? D3D11_DSV_DIMENSION_TEXTURE2DMS : D3D11_DSV_DIMENSION_TEXTURE2D; - dsvDesc.Flags = 0; - dsvDesc.Texture2D.MipSlice = 0; - DX_CHECK(s_renderD3D11->m_device->CreateDepthStencilView(texture.m_ptr, &dsvDesc, &m_dsv) ); - } - else - { - DX_CHECK(s_renderD3D11->m_device->CreateRenderTargetView(texture.m_ptr, NULL, &m_rtv[m_num]) ); - DX_CHECK(s_renderD3D11->m_device->CreateShaderResourceView(texture.m_ptr, NULL, &m_srv[m_num]) ); - m_num++; - } - } - } + postReset(); } void FrameBufferD3D11::create(uint16_t _denseIdx, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat) @@ -3075,6 +3060,21 @@ BX_PRAGMA_DIAGNOSTIC_POP(); } uint16_t FrameBufferD3D11::destroy() + { + preReset(); + + DX_RELEASE(m_swapChain, 0); + + m_num = 0; + m_numTh = 0; + + uint16_t denseIdx = m_denseIdx; + m_denseIdx = UINT16_MAX; + + return denseIdx; + } + + void FrameBufferD3D11::preReset() { for (uint32_t ii = 0, num = m_num; ii < num; ++ii) { @@ -3083,14 +3083,42 @@ BX_PRAGMA_DIAGNOSTIC_POP(); } DX_RELEASE(m_dsv, 0); - DX_RELEASE(m_swapChain, 0); + } - m_num = 0; + void FrameBufferD3D11::postReset() + { + if (0 < m_numTh) + { + m_num = 0; + for (uint32_t ii = 0; ii < m_numTh; ++ii) + { + TextureHandle handle = m_th[ii]; + if (isValid(handle) ) + { + const TextureD3D11& texture = s_renderD3D11->m_textures[handle.idx]; + if (isDepth( (TextureFormat::Enum)texture.m_textureFormat) ) + { + BX_CHECK(NULL == m_dsv, "Frame buffer already has depth-stencil attached."); - uint16_t denseIdx = m_denseIdx; - m_denseIdx = UINT16_MAX; + const uint32_t msaaQuality = bx::uint32_satsub( (texture.m_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT, 1); + const DXGI_SAMPLE_DESC& msaa = s_msaa[msaaQuality]; - return denseIdx; + D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc; + dsvDesc.Format = s_textureFormat[texture.m_textureFormat].m_fmtDsv; + dsvDesc.ViewDimension = 1 < msaa.Count ? D3D11_DSV_DIMENSION_TEXTURE2DMS : D3D11_DSV_DIMENSION_TEXTURE2D; + dsvDesc.Flags = 0; + dsvDesc.Texture2D.MipSlice = 0; + DX_CHECK(s_renderD3D11->m_device->CreateDepthStencilView(texture.m_ptr, &dsvDesc, &m_dsv) ); + } + else + { + DX_CHECK(s_renderD3D11->m_device->CreateRenderTargetView(texture.m_ptr, NULL, &m_rtv[m_num]) ); + DX_CHECK(s_renderD3D11->m_device->CreateShaderResourceView(texture.m_ptr, NULL, &m_srv[m_num]) ); + m_num++; + } + } + } + } } void FrameBufferD3D11::resolve() diff --git a/src/renderer_d3d11.h b/src/renderer_d3d11.h index 1a5c5655..1bbc3b29 100644 --- a/src/renderer_d3d11.h +++ b/src/renderer_d3d11.h @@ -287,12 +287,16 @@ namespace bgfx { namespace d3d11 FrameBufferD3D11() : m_denseIdx(UINT16_MAX) , m_num(0) + , m_numTh(0) + , m_dsv(NULL) { } void create(uint8_t _num, const TextureHandle* _handles); void create(uint16_t _denseIdx, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat); uint16_t destroy(); + void preReset(); + void postReset(); void resolve(); void clear(const Clear& _clear, const float _palette[][4]); @@ -302,6 +306,8 @@ namespace bgfx { namespace d3d11 IDXGISwapChain* m_swapChain; uint16_t m_denseIdx; uint8_t m_num; + uint8_t m_numTh; + TextureHandle m_th[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS]; }; } /* namespace d3d11 */ } // namespace bgfx diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index b4662e6d..4a42dac7 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -1948,8 +1948,7 @@ namespace bgfx { namespace gl for (uint32_t ii = 0; ii < BX_COUNTOF(m_frameBuffers); ++ii) { - FrameBufferGL& frameBuffer = m_frameBuffers[ii]; - frameBuffer.postReset(); + m_frameBuffers[ii].postReset(); } ovrPreReset(); @@ -4273,11 +4272,10 @@ namespace bgfx { namespace gl void FrameBufferGL::create(uint8_t _num, const TextureHandle* _handles) { GL_CHECK(glGenFramebuffers(1, &m_fbo[0]) ); - for (uint32_t ii = 0; ii < _num; ++ii) - { - m_th[ii] = _handles[ii]; - } + m_numTh = _num; + memcpy(m_th, _handles, _num*sizeof(TextureHandle) ); + postReset(); }