Skip to content

Commit

Permalink
Fix saving thumbnails from BGRA texture format
Browse files Browse the repository at this point in the history
  • Loading branch information
SaiyansKing committed Jul 5, 2023
1 parent 7f5278a commit b417b0a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion D3D11Engine/BaseGraphicsEngine.h
Expand Up @@ -181,7 +181,7 @@ class BaseGraphicsEngine {
virtual INT2 GetBackbufferResolution() { return GetResolution(); }

/** Returns the data of the backbuffer */
virtual void GetBackbufferData( byte** data, int& pixelsize ) {}
virtual void GetBackbufferData( byte** data, INT2& buffersize, int& pixelsize ) {}

/** Draws a fullscreenquad, copying the given texture to the viewport */
virtual void DrawQuad( INT2 position, INT2 size ) {}
Expand Down
29 changes: 13 additions & 16 deletions D3D11Engine/D3D11GraphicsEngine.cpp
Expand Up @@ -5475,9 +5475,9 @@ void D3D11GraphicsEngine::OnUIEvent( EUIEvent uiEvent ) {
}

/** Returns the data of the backbuffer */
void D3D11GraphicsEngine::GetBackbufferData( byte** data, int& pixelsize ) {
constexpr int width = 256;
byte* d = new byte[width * width * 4];
void D3D11GraphicsEngine::GetBackbufferData( byte** data, INT2& buffersize, int& pixelsize ) {
buffersize = Resolution;
byte* d = new byte[Resolution.x * Resolution.y * 4];

// Copy HDR scene to backbuffer
SetDefaultStates();
Expand All @@ -5495,13 +5495,9 @@ void D3D11GraphicsEngine::GetBackbufferData( byte** data, int& pixelsize ) {
ActivePS->GetConstantBuffer()[0]->BindToPixelShader( 0 );

HRESULT hr;

// Buffer for scaling down the image
auto rt = std::make_unique<RenderToTextureBuffer>(
GetDevice().Get(), width, width, DXGI_FORMAT_B8G8R8A8_UNORM );

// Downscale to 256x256
PfxRenderer->CopyTextureToRTV( HDRBackBuffer->GetShaderResView(), rt->GetRenderTargetView(), INT2( width, width ),
GetDevice().Get(), buffersize.x, buffersize.y, DXGI_FORMAT_B8G8R8A8_UNORM );
PfxRenderer->CopyTextureToRTV( HDRBackBuffer->GetShaderResView(), rt->GetRenderTargetView(), INT2( buffersize.x, buffersize.y ),
true );
GetContext()->Flush();

Expand All @@ -5510,9 +5506,8 @@ void D3D11GraphicsEngine::GetBackbufferData( byte** data, int& pixelsize ) {
texDesc.BindFlags = 0;
texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
texDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
texDesc.Width = width; // Gothic transforms the backbufferdata for
// savegamethumbs to 256x256-pictures anyways
texDesc.Height = width;
texDesc.Width = buffersize.x;
texDesc.Height = buffersize.y;
texDesc.MipLevels = 1;
texDesc.MiscFlags = 0;
texDesc.SampleDesc.Count = 1;
Expand All @@ -5533,13 +5528,15 @@ void D3D11GraphicsEngine::GetBackbufferData( byte** data, int& pixelsize ) {
if ( SUCCEEDED( GetContext()->Map( texture.Get(), 0, D3D11_MAP_READ, 0, &res ) ) ) {
unsigned char* dstData = reinterpret_cast<unsigned char*>(res.pData);
unsigned char* srcData = reinterpret_cast<unsigned char*>(d);
UINT length = width * 4;
UINT length = buffersize.x * 4;
if ( length == res.RowPitch ) {
memcpy( srcData, dstData, length * width );
memcpy( srcData, dstData, length * buffersize.y );
} else {
if ( length > res.RowPitch )
if ( length > res.RowPitch ) {
length = res.RowPitch;
for ( int row = 0; row < width; ++row ) {
}

for ( int row = 0; row < buffersize.y; ++row ) {
memcpy( srcData, dstData, length );
srcData += length;
dstData += res.RowPitch;
Expand Down
2 changes: 1 addition & 1 deletion D3D11Engine/D3D11GraphicsEngine.h
Expand Up @@ -215,7 +215,7 @@ class D3D11GraphicsEngine : public D3D11GraphicsEngineBase {
INT2 GetBackbufferResolution();

/** Returns the data of the backbuffer */
void GetBackbufferData( byte** data, int& pixelsize );
void GetBackbufferData( byte** data, INT2& buffersize, int& pixelsize );

/** Returns the line renderer object */
BaseLineRenderer* GetLineRenderer();
Expand Down
13 changes: 7 additions & 6 deletions D3D11Engine/D3D7/MyDirectDrawSurface7.cpp
Expand Up @@ -320,22 +320,23 @@ HRESULT MyDirectDrawSurface7::Lock( LPRECT lpDestRect, LPDDSURFACEDESC2 lpDDSurf
{
// Assume 32-bit
byte* data;
INT2 buffersize;
int pixelSize;
reinterpret_cast<D3D11GraphicsEngineBase*>(Engine::GraphicsEngine)->ResetPresentPending();
Engine::GraphicsEngine->OnStartWorldRendering();
Engine::GraphicsEngine->GetBackbufferData( &data, pixelSize );
Engine::GraphicsEngine->GetBackbufferData( &data, buffersize, pixelSize );
reinterpret_cast<D3D11GraphicsEngineBase*>(Engine::GraphicsEngine)->ResetPresentPending();

lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount = 32;
lpDDSurfaceDesc->ddpfPixelFormat.dwRBitMask = 0x000000FF;
lpDDSurfaceDesc->ddpfPixelFormat.dwRBitMask = 0x00FF0000;
lpDDSurfaceDesc->ddpfPixelFormat.dwGBitMask = 0x0000FF00;
lpDDSurfaceDesc->ddpfPixelFormat.dwBBitMask = 0x00FF0000;
lpDDSurfaceDesc->ddpfPixelFormat.dwBBitMask = 0x000000FF;
lpDDSurfaceDesc->ddpfPixelFormat.dwRGBAlphaBitMask = 0x00000000;

// Gothic transforms this into a 256x256 texture anyways
lpDDSurfaceDesc->lPitch = 256 * pixelSize;
lpDDSurfaceDesc->dwWidth = 256;
lpDDSurfaceDesc->dwHeight = 256;
lpDDSurfaceDesc->lPitch = buffersize.x * pixelSize;
lpDDSurfaceDesc->dwWidth = buffersize.x;
lpDDSurfaceDesc->dwHeight = buffersize.y;
lpDDSurfaceDesc->lpSurface = data;

LockedData = data;
Expand Down

0 comments on commit b417b0a

Please sign in to comment.