Skip to main content
gamedev.net
🔒 Locked

Proper use of Z buffering

Started by vmware 10 replies 1.2k views
Original Post
vmware
vmware
Hi, Is there anything special I have to take care of when doing Z buffering? First, I displayed one cube, and it appeared properly. But in case of a more composite shape, though the order of vertices are always proper, it occassionally happens that a set of vertices doesn't appear at all, not even if they are not hidden by any others. These are the related settings I have: BackBufferFormat = D3DFMT_UNKNOWN EnableAutoDepthStencil = TRUE AutoDepthStencilFormat = D3DFMT_D16 D3DRS_ZENABLE = TRUE Thanks.
Evil Steve
Evil Steve
16-bit Z buffers are very imprecise, and some graphics cards (Older NVidia ones) require that the backbuffer and depth buffer are the same format; that means you'll need to have a 16-bit backbuffer on some cards, which looks crappy. But mainly the precision is a big issue for 16-bit depth buffers; 32-bit or 24-bit are far preferable - 16-bit only gives you 65536 uniquer depth values.

Secondly, you need to make sure that your near and far clip planes are as close together as possible, and that your near clip plane is at a non-zero distance (And preferably as far as possible). 70% of the Z-range is used in the first 30% of the scene, since you're more likely to notice Z-artefacts close up than you are at a distance.
vmware
vmware
Quote:

some graphics cards (Older NVidia ones) require that the backbuffer and depth buffer are the same format


If I set also the backbuffer format to D3DFMT_D16 then the application will exit with an exception.

(But I am still thinking about what you've written because I don't understand everything in practice.)
Evil Steve
Evil Steve
Quote:
Original post by vmware
If I set also the backbuffer format to D3DFMT_D16 then the application will exit with an exception.
Sounds like you're not correctly checking for support of various formats. I've written A Tutorial which covers setting up a depth buffer correctly. You need to call CheckDeviceFormat and CheckDepthStencilMatch before you can attempt to create the device with a depth buffer, otherwise there's a reasonable chance it'll fail.

It sounds to me like your card doesn't support 16-bit depth buffers, either at all, or in the current gfx mode.
vmware
vmware
In the tutorial you mentioned there is one variable I could not recreate.

mode.Format has to be supplied for multiple functions, but I could not get mode.

Does it have reference to whether I am in windowed or fullscreen mode?
vmware
vmware
Sorry, I have found it in the 1st tutorial.

D3DDISPLAYMODE mode;
g_pD3D ->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode);

vmware
vmware
But I still don't understand where fmtBackbuffer comes from.

hResult = g_pD3D->CheckDepthStencilMatch(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, mode.Format, fmtBackbuffer, fmtDepths);

I have also tried to set that to mode.Format and fmtDepths.
None of them worked.
Evil Steve
Evil Steve
Quote:
Original post by vmware
But I still don't understand where fmtBackbuffer comes from.

hResult = g_pD3D->CheckDepthStencilMatch(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, mode.Format, fmtBackbuffer, fmtDepths);

I have also tried to set that to mode.Format and fmtDepths.
None of them worked.
fmtBackbuffer is just mode.Format:
// Grab the current desktop formatD3DFORMAT fmtBackbuffer;D3DDISPLAYMODE mode;HRESULT hResult = m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode);if(FAILED(hResult)){	m_strError = L"GetAdapterDisplayMode() failed. Error: " + Util::DXErrorToString(hResult);	m_pD3D->Release();	m_pD3D = NULL;	return false;}fmtBackbuffer = mode.Format;
Have a look over the InitD3DDevice() function for the full checks.
vmware
vmware
This script finds that D3DFMT_D24X8 is the suggested format for me.

But after setting AutoDepthStencilFormat and BackBufferFormat my call to CreateDevice fails.

This is the way I do it:
g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice)))


The error message I get to the output is:
Invalid value for BackBufferFormat. ValidatePresentParameters fails.
Evil Steve
Evil Steve
Quote:
Original post by vmware
This script finds that D3DFMT_D24X8 is the suggested format for me.

But after setting AutoDepthStencilFormat and BackBufferFormat my call to CreateDevice fails.

This is the way I do it:
g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice)))


The error message I get to the output is:
Invalid value for BackBufferFormat. ValidatePresentParameters fails.
What value are you using for BackBufferFormat?
vmware
vmware
I am using D3DFMT_D24X8 for both places.
Evil Steve
Evil Steve
Quote:
Original post by vmware
I am using D3DFMT_D24X8 for both places.
D3DFMT_D24X8 is a depth-buffer format, it's not usable as a backbuffer format. Try D3DFMT_X8R8G8B8 instead, or use the code from my tutorial.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.