Quick Help - Depth Buffer

Started by
1 comment, last by Evil Steve 17 years, 10 months ago
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.

//GRAPHICS DRAWING HERE

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:

D3DPRESENT_PARAMETERS d3dpp;
D3DDISPLAYMODE d3ddm;

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;
d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;

r = pD3D ->		CreateDevice
				(
					D3DADAPTER_DEFAULT,
					D3DDEVTYPE_HAL,
					hWndTarget,
						 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
					&d3dpp,
					ppDevice
				);

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.
Advertisement
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.
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?

This topic is closed to new replies.

Advertisement