Created
June 7, 2012 05:06
-
-
Save vinjn/2886702 to your computer and use it in GitHub Desktop.
GfxDeviceD3D9::ReadbackImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool GfxDeviceD3D9::ReadbackImage( /* params */ ) | |
{ | |
HRESULT hr; | |
IDirect3DDevice9* dev = GetD3DDevice(); | |
SurfacePointer renderTarget; | |
hr = dev->GetRenderTarget( 0, &renderTarget ); | |
if( !renderTarget || FAILED(hr) ) | |
return false; | |
D3DSURFACE_DESC rtDesc; | |
renderTarget->GetDesc( &rtDesc ); | |
SurfacePointer resolvedSurface; | |
if( rtDesc.MultiSampleType != D3DMULTISAMPLE_NONE ) | |
{ | |
hr = dev->CreateRenderTarget( rtDesc.Width, rtDesc.Height, rtDesc.Format, D3DMULTISAMPLE_NONE, 0, FALSE, &resolvedSurface, NULL ); | |
if( FAILED(hr) ) | |
return false; | |
hr = dev->StretchRect( renderTarget, NULL, resolvedSurface, NULL, D3DTEXF_NONE ); | |
if( FAILED(hr) ) | |
return false; | |
renderTarget = resolvedSurface; | |
} | |
SurfacePointer offscreenSurface; | |
hr = dev->CreateOffscreenPlainSurface( rtDesc.Width, rtDesc.Height, rtDesc.Format, D3DPOOL_SYSTEMMEM, &offscreenSurface, NULL ); | |
if( FAILED(hr) ) | |
return false; | |
hr = dev->GetRenderTargetData( renderTarget, offscreenSurface ); | |
bool ok = SUCCEEDED(hr); | |
if( ok ) | |
{ | |
// Here we have data in offscreenSurface. | |
D3DLOCKED_RECT lr; | |
RECT rect; | |
rect.left = 0; | |
rect.right = rtDesc.Width; | |
rect.top = 0; | |
rect.bottom = rtDesc.Height; | |
// Lock the surface to read pixels | |
hr = offscreenSurface->LockRect( &lr, &rect, D3DLOCK_READONLY ); | |
if( SUCCEEDED(hr) ) | |
{ | |
// Pointer to data is lt.pBits, each row is | |
// lr.Pitch bytes apart (often it is the same as width*bpp, but | |
// can be larger if driver uses padding) | |
// Read the data here! | |
offscreenSurface->UnlockRect(); | |
} | |
else | |
{ | |
ok = false; | |
} | |
} | |
return ok; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment