Procházet zdrojové kódy

D3D12: Fixed swap chain. Issue #1323.

Branimir Karadžić před 7 roky
rodič
revize
20582d8ff9
2 změnil soubory, kde provedl 29 přidání a 5 odebrání
  1. 27 5
      src/renderer_d3d12.cpp
  2. 2 0
      src/renderer_d3d12.h

+ 27 - 5
src/renderer_d3d12.cpp

@@ -1611,6 +1611,16 @@ namespace bgfx { namespace d3d12
 
 
 		void createFrameBuffer(FrameBufferHandle _handle, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat) override
 		void createFrameBuffer(FrameBufferHandle _handle, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat) override
 		{
 		{
+			finishAll(true);
+
+			for (uint32_t ii = 0; ii < BX_COUNTOF(m_frameBuffers); ++ii)
+			{
+				if (m_frameBuffers[ii].m_nwh == _nwh)
+				{
+					m_frameBuffers[ii].destroy();
+				}
+			}
+
 			uint16_t denseIdx = m_numWindows++;
 			uint16_t denseIdx = m_numWindows++;
 			m_windows[denseIdx] = _handle;
 			m_windows[denseIdx] = _handle;
 			m_frameBuffers[_handle.idx].create(denseIdx, _nwh, _width, _height, _depthFormat);
 			m_frameBuffers[_handle.idx].create(denseIdx, _nwh, _width, _height, _depthFormat);
@@ -1618,7 +1628,14 @@ namespace bgfx { namespace d3d12
 
 
 		void destroyFrameBuffer(FrameBufferHandle _handle) override
 		void destroyFrameBuffer(FrameBufferHandle _handle) override
 		{
 		{
-			uint16_t denseIdx = m_frameBuffers[_handle.idx].destroy();
+			FrameBufferD3D12& frameBuffer = m_frameBuffers[_handle.idx];
+
+			if (NULL != frameBuffer.m_swapChain)
+			{
+				finishAll(true);
+			}
+
+			uint16_t denseIdx = frameBuffer.destroy();
 			if (UINT16_MAX != denseIdx)
 			if (UINT16_MAX != denseIdx)
 			{
 			{
 				--m_numWindows;
 				--m_numWindows;
@@ -3060,11 +3077,11 @@ namespace bgfx { namespace d3d12
 			m_commandList = NULL;
 			m_commandList = NULL;
 		}
 		}
 
 
-		void finishAll()
+		void finishAll(bool _alloc = false)
 		{
 		{
 			uint64_t fence = m_cmd.kick();
 			uint64_t fence = m_cmd.kick();
 			m_cmd.finish(fence, true);
 			m_cmd.finish(fence, true);
-			m_commandList = NULL;
+			m_commandList = _alloc ? m_cmd.alloc() : NULL;
 		}
 		}
 
 
 		Dxgi m_dxgi;
 		Dxgi m_dxgi;
@@ -4869,6 +4886,7 @@ namespace bgfx { namespace d3d12
 		}
 		}
 #endif // BX_PLATFORM_WINDOWS
 #endif // BX_PLATFORM_WINDOWS
 
 
+		m_nwh      = _nwh;
 		m_denseIdx = _denseIdx;
 		m_denseIdx = _denseIdx;
 		m_num      = 1;
 		m_num      = 1;
 	}
 	}
@@ -4877,6 +4895,7 @@ namespace bgfx { namespace d3d12
 	{
 	{
 		DX_RELEASE(m_swapChain, 0);
 		DX_RELEASE(m_swapChain, 0);
 
 
+		m_nwh   = NULL;
 		m_numTh = 0;
 		m_numTh = 0;
 		m_needPresent = false;
 		m_needPresent = false;
 
 
@@ -6526,8 +6545,11 @@ namespace bgfx { namespace d3d12
 		for (uint32_t ii = 1, num = m_numWindows; ii < num; ++ii)
 		for (uint32_t ii = 1, num = m_numWindows; ii < num; ++ii)
 		{
 		{
 			FrameBufferD3D12& frameBuffer = m_frameBuffers[m_windows[ii].idx];
 			FrameBufferD3D12& frameBuffer = m_frameBuffers[m_windows[ii].idx];
-			uint8_t idx = uint8_t(frameBuffer.m_swapChain->GetCurrentBackBufferIndex() );
-			frameBuffer.setState(m_commandList, idx, D3D12_RESOURCE_STATE_PRESENT);
+			if (NULL != frameBuffer.m_swapChain)
+			{
+				uint8_t idx = uint8_t(frameBuffer.m_swapChain->GetCurrentBackBufferIndex() );
+				frameBuffer.setState(m_commandList, idx, D3D12_RESOURCE_STATE_PRESENT);
+			}
 		}
 		}
 #endif // BX_PLATFORM_WINDOWS
 #endif // BX_PLATFORM_WINDOWS
 
 

+ 2 - 0
src/renderer_d3d12.h

@@ -337,6 +337,7 @@ namespace bgfx { namespace d3d12
 	{
 	{
 		FrameBufferD3D12()
 		FrameBufferD3D12()
 			: m_swapChain(NULL)
 			: m_swapChain(NULL)
+			, m_nwh(NULL)
 			, m_width(0)
 			, m_width(0)
 			, m_height(0)
 			, m_height(0)
 			, m_denseIdx(UINT16_MAX)
 			, m_denseIdx(UINT16_MAX)
@@ -361,6 +362,7 @@ namespace bgfx { namespace d3d12
 		TextureHandle m_texture[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS];
 		TextureHandle m_texture[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS];
 		TextureHandle m_depth;
 		TextureHandle m_depth;
 		Dxgi::SwapChainI* m_swapChain;
 		Dxgi::SwapChainI* m_swapChain;
+		void* m_nwh;
 		uint32_t m_width;
 		uint32_t m_width;
 		uint32_t m_height;
 		uint32_t m_height;
 		uint16_t m_denseIdx;
 		uint16_t m_denseIdx;