Jump to content
  • Advertisement
Sign in to follow this  

Quick Help - Depth Buffer

This topic is 4415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I am trying to get access to the depth buffer so I can manipulate its values manually just as I am manipulating back buffer values manually in my drawing routines. Here is what I have in my render ():
D3DLOCKED_RECT LockedRect, LockedRectb;
g_pDevice -> Clear (0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB (0, 0, 0), 1.0f, 0);
g_pDevice -> GetBackBuffer (0, 0, D3DBACKBUFFER_TYPE_MONO, &g_pBackSurf);
g_pDevice -> GetDepthStencilSurface (&pZBuffer);
g_pBackSurf -> LockRect (&LockedRect, NULL, 0);
pZBuffer -> LockRect (&LockedRectb, NULL, 0);
DWORD* pData = (DWORD*)(LockedRect.pBits); //back buffer, works fine
float* dbuff = (float*)(LockedRectb.pBits);

//dbuff [0] = 0.1f; 
//When I debug I get an "Error: expression cannot be evaluated" for 
//the above line and for LockedRectb.pBits in the line before it.  
//Program closes fast whenever I try to manipulate or look at the 
//values in the depth buffer.


g_pBackSurf -> UnlockRect ();
pZBuffer -> UnlockRect ();

g_pBackSurf -> Release ();
g_pBackSurf = 0;
pZBuffer -> Release ();
pZBuffer = 0;

g_pDevice -> Present (NULL, NULL, NULL, NULL);
render = false;
return S_OK;

Here are my basic D3D settings:

HRESULT r = 0;
if (*ppDevice)
	(*ppDevice) -> Release ();

ZeroMemory ( &d3dpp, sizeof (D3DPRESENT_PARAMETERS) );
r = pD3D -> GetAdapterDisplayMode (D3DADAPTER_DEFAULT, &d3ddm);
d3dpp.BackBufferWidth = Width;
d3dpp.BackBufferHeight = Height;
d3dpp.BackBufferFormat = bWindowed ? d3ddm.Format : FullScreenFormat;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.hDeviceWindow = hWndTarget;	
d3dpp.Windowed = bWindowed;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.FullScreen_RefreshRateInHz = 0;

r = pD3D ->		CreateDevice

g_DeviceHeight = Height;
g_DeviceWidth = Width;

g_SavedPresParams = d3dpp;

return S_OK;

I am making a small renderer in which I basically just want my own back buffer and depth buffer to deal with. I tried making my own array for the depth buffer but it's just too slow since it can't be reset fast enough each time render () is called. What I want to be able to do is look at and manipulate values in the depth buffer and have it cleared by D3D just as the back buffer is cleared. I appreciate any help.

Share this post

Link to post
Share on other sites
Depending on your hardware, it might be easier to render depth values to a render target, then use those in a pixel shader or whatever you need. Reading back from the depth buffer itself is slow (and I don't know if it's truly supported anymore with D3D9, and I doubt it's even capable in D3D10).

edit: As a note, I don't think the depth buffer is actually stored as floating point values, even for something like D16. I don't know, off hand, how the data is actually stored, but I know it's not floating point values like that.

Share this post

Link to post
Share on other sites
You're not using the debug runtimes and you're not checking error codes, are you? You can't lock a depth buffer, unless it's one of the lockable formats (D3DFMT_D16_LOCKABLE or D3DFMT_D32F_LOCKABLE). And you have to check that your card supports the format you want to use, and that it's compatible with your backbuffer.

Usually reading depth values is something you want to avoid at all costs. Why do you need to?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!