Generating Debug Bitmaps for DirectX

Introduction

Debugging a DirectX application is not the easiest thing to do. Most times, you're looking for reasons why your image did not display on the screen or why it displayed incorrectly. The following snippet of code should help in your debugging efforts. Cut and paste it to your DirectX program and call it before calling the DirectX Present() method. It will allow you to capture an image of a screen buffer into a bitmap file. Each image is named in a sequential fashion. This will provide a snapshot of exactly what is being presented to the screen.

Notes:
  1. This code assumes you have one back buffer defined.
  2. This code assumes your DirectX device is defined as 'windowed'.
  3. m_rect is the CRect for the associated device window.
//
// This method is used for debugging. It will dump the current
// back buffer surface to a bitmap. This provides a snapshot of
// what is currently ready for presentation.
//
void debug_dump_buffer()
{
   IDirect3DSurface9* pRenderTarget=NULL;
   IDirect3DSurface9* pDestTarget=NULL;
   D3DDISPLAYMODE d3ddisplaymode;

   // sanity checks.
   if (pDirect3DDevice == NULL)
      return;

   // get the render target surface.
   HRESULT hr = pDirect3DDevice->GetRenderTarget(0, &pRenderTarget);
   // get the current adapter display mode.
   hr = pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
                                         &d3ddisplaymode);
   // create a destination surface.
   hr = pDirect3DDevice->CreateOffscreenPlainSurface(m_rect.Width(),
                         m_rect.Height(),
                         d3ddisplaymode.Format,
                         D3DPOOL_SYSTEMMEM,
                         &pDestTarget,
                         NULL);
   //copy the render target to the destination surface.
   hr = pDirect3DDevice->GetRenderTargetData(pRenderTarget,
                                             pDestTarget);
   //save its contents to a bitmap file.
   file.Format("DX Buffer %d.bmp", ndebug++);
   hr = D3DXSaveSurfaceToFile(file,
                              D3DXIFF_BMP,
                              pDestTarget,
                              NULL,
                              NULL);

   // clean up.
   pRenderTarget->Release();
   pDestTarget->Release();
}


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds