Fix DX11 screenshot color format (#3551)

This commit is contained in:
Vincent Bousquet
2026-01-14 23:29:26 +01:00
committed by GitHub
parent 4baed6e076
commit 0d7526dfb9

View File

@@ -1997,6 +1997,27 @@ namespace bgfx { namespace d3d11
D3D11_TEXTURE2D_DESC backBufferDesc; D3D11_TEXTURE2D_DESC backBufferDesc;
backBuffer->GetDesc(&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; D3D11_TEXTURE2D_DESC desc;
bx::memCopy(&desc, &backBufferDesc, sizeof(desc) ); bx::memCopy(&desc, &backBufferDesc, sizeof(desc) );
desc.SampleDesc.Count = 1; desc.SampleDesc.Count = 1;
@@ -2029,22 +2050,69 @@ namespace bgfx { namespace d3d11
D3D11_MAPPED_SUBRESOURCE mapped; D3D11_MAPPED_SUBRESOURCE mapped;
DX_CHECK(m_deviceCtx->Map(texture, 0, D3D11_MAP_READ, 0, &mapped) ); DX_CHECK(m_deviceCtx->Map(texture, 0, D3D11_MAP_READ, 0, &mapped) );
bimg::imageSwizzleBgra8( if (colorFormat == TextureFormat::RGBA8)
mapped.pData {
, mapped.RowPitch bimg::imageSwizzleBgra8(
, backBufferDesc.Width mapped.pData
, backBufferDesc.Height , mapped.RowPitch
, mapped.pData , backBufferDesc.Width
, mapped.RowPitch , backBufferDesc.Height
); , mapped.pData
g_callback->screenShot(_filePath , mapped.RowPitch
, backBufferDesc.Width );
, backBufferDesc.Height g_callback->screenShot(
, mapped.RowPitch _filePath
, mapped.pData , backBufferDesc.Width
, backBufferDesc.Height*mapped.RowPitch , backBufferDesc.Height
, false , 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); m_deviceCtx->Unmap(texture, 0);
DX_RELEASE(texture, 0); DX_RELEASE(texture, 0);