Home » Community » Forums » DirectX and XNA » Proper use of Z buffering
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Proper use of Z buffering
Post New Topic  Post Reply 
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.

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.


Steve Macpherson
DirectX MVP and Programmer, Firebrand Games

 User Rating: 2012   |  Rate This User  Send Private MessageView ProfileView JournalView GD Showcase Entries Report this Post to a Moderator | Link

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.)

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 2012   |  Rate This User  Send Private MessageView ProfileView JournalView GD Showcase Entries Report this Post to a Moderator | Link

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?

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Sorry, I have found it in the 1st tutorial.

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



 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

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

I have also tried to set that to mode.Format and [i]fmtDepths.
None of them worked.

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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]);

I have also tried to set that to mode.Format and [i]fmtDepths.
None of them worked.
fmtBackbuffer is just mode.Format:
// Grab the current desktop format
D3DFORMAT 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.

 User Rating: 2012   |  Rate This User  Send Private MessageView ProfileView JournalView GD Showcase Entries Report this Post to a Moderator | Link

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.

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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?

 User Rating: 2012   |  Rate This User  Send Private MessageView ProfileView JournalView GD Showcase Entries Report this Post to a Moderator | Link

I am using D3DFMT_D24X8 for both places.

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 2012   |  Rate This User  Send Private MessageView ProfileView JournalView GD Showcase Entries Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: