Browse Source

libobs-d3d11: Don't crash if unable to rebuild shared texture

When rebuilding the graphics subsystem, it's possible a shared texture
may no longer be available.  In this case, just soft fail and allow the
texture to be rebuilt rather than crash the entire program over it.
jp9000 8 years ago
parent
commit
8cc5f8de55
2 changed files with 32 additions and 3 deletions
  1. 31 3
      libobs-d3d11/d3d11-rebuild.cpp
  2. 1 0
      libobs-d3d11/d3d11-subsystem.hpp

+ 31 - 3
libobs-d3d11/d3d11-rebuild.cpp

@@ -31,15 +31,43 @@ inline void gs_index_buffer::Rebuild(ID3D11Device *dev)
 		throw HRError("Failed to create buffer", hr);
 		throw HRError("Failed to create buffer", hr);
 }
 }
 
 
+void gs_texture_2d::RebuildSharedTextureFallback()
+{
+	td                  = {};
+	td.Width            = 2;
+	td.Height           = 2;
+	td.MipLevels        = 1;
+	td.Format           = DXGI_FORMAT_B8G8R8A8_UNORM;
+	td.ArraySize        = 1;
+	td.SampleDesc.Count = 1;
+
+	width      = td.Width;
+	height     = td.Height;
+	dxgiFormat = td.Format;
+	levels     = 1;
+
+	resourceDesc = {};
+	resourceDesc.Format              = td.Format;
+	resourceDesc.ViewDimension       = D3D11_SRV_DIMENSION_TEXTURE2D;
+	resourceDesc.Texture2D.MipLevels = 1;
+
+	isShared = false;
+}
+
 inline void gs_texture_2d::Rebuild(ID3D11Device *dev)
 inline void gs_texture_2d::Rebuild(ID3D11Device *dev)
 {
 {
 	HRESULT hr;
 	HRESULT hr;
 	if (isShared) {
 	if (isShared) {
 		hr = dev->OpenSharedResource((HANDLE)(uintptr_t)sharedHandle,
 		hr = dev->OpenSharedResource((HANDLE)(uintptr_t)sharedHandle,
 				__uuidof(ID3D11Texture2D), (void**)&texture);
 				__uuidof(ID3D11Texture2D), (void**)&texture);
-		if (FAILED(hr))
-			throw HRError("Failed to open shared 2D texture", hr);
-	} else {
+		if (FAILED(hr)) {
+			blog(LOG_WARNING, "Failed to rebuild shared texture: ",
+					"0x%08lX", hr);
+			RebuildSharedTextureFallback();
+		}
+	}
+
+	if (!isShared) {
 		hr = dev->CreateTexture2D(&td,
 		hr = dev->CreateTexture2D(&td,
 				data.size() ? srd.data() : nullptr,
 				data.size() ? srd.data() : nullptr,
 				&texture);
 				&texture);

+ 1 - 0
libobs-d3d11/d3d11-subsystem.hpp

@@ -362,6 +362,7 @@ struct gs_texture_2d : gs_texture {
 	void InitRenderTargets();
 	void InitRenderTargets();
 	void BackupTexture(const uint8_t **data);
 	void BackupTexture(const uint8_t **data);
 
 
+	void RebuildSharedTextureFallback();
 	inline void Rebuild(ID3D11Device *dev);
 	inline void Rebuild(ID3D11Device *dev);
 
 
 	inline void Release()
 	inline void Release()