Created
January 17, 2014 03:35
-
-
Save karlgluck/8467971 to your computer and use it in GitHub Desktop.
This is the code for accessing pixel data from the backbuffer in a D3D9 application. Keywords: LPDIRECT3DSURFACE9 read backbuffer copy back buffer directly access back buffer Direct3D device DirectX 9
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
void demoExtractBackBufferPixels(LPDIRECT3DDEVICE9 d3d_device) { | |
// TODO: In your app, add FAILED() macros to check the HRESULTs passed back | |
// by each of the API calls. I leave these out for clarity. | |
// Grab the backbuffer from the Direct3D device | |
LPDIRECT3DSURFACE9 back_buffer = NULL; | |
d3d_device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back_buffer); | |
// Get the buffer's description and make an offscreen surface in system memory. | |
// We need to do this because the backbuffer is in video memory and can't be locked | |
// unless the device was created with a special flag (D3DPRESENTFLAG_LOCKABLE_BACKBUFFER). | |
// Unfortunately, a video-memory buffer CAN be locked with LockRect. The effect is | |
// that it crashes your app when you try to read or write to it. | |
D3DLOCKED_RECT d3dlr; | |
D3DSURFACE_DESC desc; | |
LPDIRECT3DSURFACE9 offscreen_surface = NULL; | |
back_buffer->GetDesc(&desc); | |
d3d_device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, | |
D3DPOOL_SYSTEMMEM, &offscreen_surface, NULL); | |
// Copy from video memory to system memory | |
d3d_device->GetRenderTargetData(back_buffer, offscreen_surface); | |
// Lock the surface using some flags for optimization | |
offscreen_surface->LockRect(&d3dlr, NULL, D3DLOCK_NO_DIRTY_UPDATE|D3DLOCK_READONLY); | |
// TODO: do some processing based on the pixel format, d3dlr and desc.Width/desc.Height | |
switch (desc.Format) { | |
case D3DFMT_R8G8B8: | |
case D3DFMT_X1R5G5B5: | |
case D3DFMT_R5G6B5: | |
case D3DFMT_A8R8G8B8: | |
case D3DFMT_X8R8G8B8: | |
default: | |
} | |
// Release all of our references | |
offscreen_surface->UnlockRect(); | |
offscreen_surface->Release(); | |
back_buffer->Release(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment