Просмотр исходного кода

Fix DX11 screenshot color format (#3551)

Vincent Bousquet 2 недель назад
Родитель
Сommit
0d7526dfb9
1 измененных файлов с 83 добавлено и 15 удалено
  1. 83 15
      src/renderer_d3d11.cpp

+ 83 - 15
src/renderer_d3d11.cpp

@@ -1997,6 +1997,27 @@ namespace bgfx { namespace d3d11
 			D3D11_TEXTURE2D_DESC backBufferDesc;
 			backBuffer->GetDesc(&backBufferDesc);
 
+			TextureFormat::Enum colorFormat = TextureFormat::Enum::Count;
+			for (int i = 0; i < TextureFormat::Enum::Count; i++)
+			{
+				if (s_textureFormat[i].m_fmt == backBufferDesc.Format)
+				{
+					colorFormat = TextureFormat::Enum(i);
+					break;
+				}
+				if (s_textureFormat[i].m_fmtSrgb == backBufferDesc.Format)
+				{
+					colorFormat = TextureFormat::Enum(i);
+					break;
+				}
+			}
+			if (colorFormat == TextureFormat::Enum::Count)
+			{
+				BX_TRACE("Unable to capture screenshot %s.", _filePath);
+				DX_RELEASE(backBuffer, 0);
+				return;
+			}
+
 			D3D11_TEXTURE2D_DESC desc;
 			bx::memCopy(&desc, &backBufferDesc, sizeof(desc) );
 			desc.SampleDesc.Count = 1;
@@ -2029,22 +2050,69 @@ namespace bgfx { namespace d3d11
 
 				D3D11_MAPPED_SUBRESOURCE mapped;
 				DX_CHECK(m_deviceCtx->Map(texture, 0, D3D11_MAP_READ, 0, &mapped) );
-				bimg::imageSwizzleBgra8(
-					  mapped.pData
-					, mapped.RowPitch
-					, backBufferDesc.Width
-					, backBufferDesc.Height
-					, mapped.pData
-					, mapped.RowPitch
-					);
-				g_callback->screenShot(_filePath
-					, backBufferDesc.Width
-					, backBufferDesc.Height
-					, mapped.RowPitch
-					, mapped.pData
-					, backBufferDesc.Height*mapped.RowPitch
-					, false
+				if (colorFormat == TextureFormat::RGBA8)
+				{
+					bimg::imageSwizzleBgra8(
+						  mapped.pData
+						, mapped.RowPitch
+						, backBufferDesc.Width
+						, backBufferDesc.Height
+						, mapped.pData
+						, mapped.RowPitch
+						);
+					g_callback->screenShot(
+						  _filePath
+						, backBufferDesc.Width
+						, backBufferDesc.Height
+						, mapped.RowPitch
+						, mapped.pData
+						, backBufferDesc.Height*mapped.RowPitch
+						, false
+						);
+				}
+				else if (colorFormat == TextureFormat::BGRA8)
+				{
+					g_callback->screenShot(
+						  _filePath
+						, backBufferDesc.Width
+						, backBufferDesc.Height
+						, mapped.RowPitch
+						, mapped.pData
+						, backBufferDesc.Height*mapped.RowPitch
+						, false
+						);
+				}
+				else
+				{
+					const uint8_t dstBpp = bimg::getBitsPerPixel(bimg::TextureFormat::BGRA8);
+					const uint32_t dstPitch = backBufferDesc.Width * dstBpp / 8;
+					const uint32_t dstSize = backBufferDesc.Height * dstPitch;
+
+					void* dst = bx::alloc(g_allocator, dstSize);
+					bimg::imageConvert(
+						  dst
+						, dstBpp
+						, bimg::getPack(bimg::TextureFormat::BGRA8)
+						, mapped.pData
+						, bimg::getBitsPerPixel(bimg::TextureFormat::Enum(colorFormat))
+						, bimg::getUnpack(bimg::TextureFormat::Enum(colorFormat))
+						, backBufferDesc.Width
+						, backBufferDesc.Height
+						, 1
+						, mapped.RowPitch
+						, dstPitch
 					);
+					g_callback->screenShot(
+						  _filePath
+						, backBufferDesc.Width
+						, backBufferDesc.Height
+						, dstPitch
+						, dst
+						, backBufferDesc.Height*dstPitch
+						, false
+						);
+					bx::free(g_allocator, dst);
+				}
 				m_deviceCtx->Unmap(texture, 0);
 
 				DX_RELEASE(texture, 0);