From 61ed393f6c51563afa58f78de1e14527bedef931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Tue, 30 Sep 2014 21:16:24 -0700 Subject: [PATCH] DX11: Allow lost device for few frames. --- include/bgfx.c99.h | 3 +++ include/bgfx.h | 3 +++ src/bgfx.cpp | 3 ++- src/renderer_d3d11.cpp | 36 ++++++++++++++++++++++++++++++++---- src/renderer_d3d9.cpp | 2 +- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/include/bgfx.c99.h b/include/bgfx.c99.h index c2c90e14..bd7f46fc 100644 --- a/include/bgfx.c99.h +++ b/include/bgfx.c99.h @@ -282,6 +282,9 @@ typedef enum bgfx_fatal BGFX_FATAL_INVALID_SHADER, BGFX_FATAL_UNABLE_TO_INITIALIZE, BGFX_FATAL_UNABLE_TO_CREATE_TEXTURE, + BGFX_FATAL_DEVICE_LOST, + + BGFX_FATAL_COUNT } bgfx_fatal_t; diff --git a/include/bgfx.h b/include/bgfx.h index 8d04572d..685dee7f 100644 --- a/include/bgfx.h +++ b/include/bgfx.h @@ -32,6 +32,9 @@ namespace bgfx InvalidShader, UnableToInitialize, UnableToCreateTexture, + DeviceLost, + + Count }; }; diff --git a/src/bgfx.cpp b/src/bgfx.cpp index e14c3d8d..0c5bf7a0 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -1349,7 +1349,7 @@ again: { RendererType::Enum first = RendererType::Direct3D9; RendererType::Enum second = RendererType::Direct3D11; - if (0x601 == getWindowsVersion() ) + if (0x601 <= getWindowsVersion() ) { first = RendererType::Direct3D11; second = RendererType::Direct3D9; @@ -2722,6 +2722,7 @@ again: #include #include +BX_STATIC_ASSERT(bgfx::Fatal::Count == bgfx::Fatal::Enum(BGFX_FATAL_COUNT) ); BX_STATIC_ASSERT(bgfx::RendererType::Count == bgfx::RendererType::Enum(BGFX_RENDERER_TYPE_COUNT) ); BX_STATIC_ASSERT(bgfx::Attrib::Count == bgfx::Attrib::Enum(BGFX_ATTRIB_COUNT) ); BX_STATIC_ASSERT(bgfx::AttribType::Count == bgfx::AttribType::Enum(BGFX_ATTRIB_TYPE_COUNT) ); diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 7398449e..6b8514e7 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -539,7 +539,8 @@ RENDERDOC_IMPORT struct RendererContextD3D11 : public RendererContextI { RendererContextD3D11() - : m_captureTexture(NULL) + : m_lost(0) + , m_captureTexture(NULL) , m_captureResolve(NULL) , m_wireframe(false) , m_flags(BGFX_RESET_NONE) @@ -1158,16 +1159,42 @@ RENDERDOC_IMPORT capturePostReset(); } + static bool isLost(HRESULT _hr) + { + return DXGI_ERROR_DEVICE_REMOVED == _hr + || DXGI_ERROR_DEVICE_HUNG == _hr + || DXGI_ERROR_DEVICE_RESET == _hr + || DXGI_ERROR_DRIVER_INTERNAL_ERROR == _hr + || DXGI_ERROR_NOT_CURRENTLY_AVAILABLE == _hr + ; + } + void flip() BX_OVERRIDE { if (NULL != m_swapChain) { + HRESULT hr = 0; uint32_t syncInterval = !!(m_flags & BGFX_RESET_VSYNC); - for (uint32_t ii = 1, num = m_numWindows; ii < num; ++ii) + for (uint32_t ii = 1, num = m_numWindows && SUCCEEDED(hr); ii < num; ++ii) { - DX_CHECK(m_frameBuffers[m_windows[ii].idx].m_swapChain->Present(syncInterval, 0) ); + hr = m_frameBuffers[m_windows[ii].idx].m_swapChain->Present(syncInterval, 0); + } + + if (SUCCEEDED(hr) ) + { + hr = m_swapChain->Present(syncInterval, 0); + } + + if (FAILED(hr) + && isLost(hr) ) + { + ++m_lost; + BGFX_FATAL(10 > m_lost, bgfx::Fatal::DeviceLost, "Device is lost. FAILED 0x%08x", hr); + } + else + { + m_lost = 0; } - DX_CHECK(m_swapChain->Present(syncInterval, 0) ); } } @@ -1978,6 +2005,7 @@ RENDERDOC_IMPORT IDXGIFactory* m_factory; IDXGISwapChain* m_swapChain; + uint16_t m_lost; uint16_t m_numWindows; FrameBufferHandle m_windows[BGFX_CONFIG_MAX_FRAME_BUFFERS]; diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index bfa94017..ada4a3ac 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -1042,7 +1042,7 @@ namespace bgfx postReset(); } - bool isLost(HRESULT _hr) const + static bool isLost(HRESULT _hr) { return D3DERR_DEVICELOST == _hr || D3DERR_DRIVERINTERNALERROR == _hr