Jump to content
  • Advertisement
Sign in to follow this  
DeadMG

Depth Buffering In D3D9

This topic is 2810 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

I've got a simple D3D9 app, but the depth buffer seems to refuse to turn on. Kindly, PIX for Windows 64bit refuses to do anything of any use (keeps crashing) and I've been unable to get a lot of information back from it. I've used the AutoDepthStencilBuffer = true; parameter when creating the device and specified D24S8 for the format. I also set my Projection matrix (built by D3DXPerspectiveFovLH) to the device. I did remember to clear the depth/stencil buffers in the Clear() call. I'm not using fixed-function (except for my 2D renders).

I tried manually turning on depth buffering with the SetRenderState method and it didn't return any error, but depth buffering is still not enabled.

Share this post


Link to post
Share on other sites
Advertisement

I've got a simple D3D9 app, but the depth buffer seems to refuse to turn on. Kindly, PIX for Windows 64bit refuses to do anything of any use (keeps crashing) and I've been unable to get a lot of information back from it. I've used the AutoDepthStencilBuffer = true; parameter when creating the device and specified D24S8 for the format. I also set my Projection matrix (built by D3DXPerspectiveFovLH) to the device. I did remember to clear the depth/stencil buffers in the Clear() call. I'm not using fixed-function (except for my 2D renders).

I tried manually turning on depth buffering with the SetRenderState method and it didn't return any error, but depth buffering is still not enabled.


Did you enable both D3DRS_ZENABLE and D3DRS_ZWRITEENABLE? The first controls whether the depth test is performed, while the latter controls whether depth values are written into the depth buffer.

Share this post


Link to post
Share on other sites
They should both be defaulted to true, but I enabled both explicitly for no change.

EDIT:

Turns out that if you work on 64bit, you have to independently turn on debugging for 32bit and 64bit Direct3D applications. No wonder D3D wasn't throwing any useful debugging information. What I've got is this:


Direct3D9: (WARN) :Cannot compute WNear and WFar from the supplied projection matrix

Direct3D9: (WARN) :Setting wNear to 0.0 and wFar to 1.0

repeatedly. I checked the documentation for D3DXMatrxPespectiveFovLH and it seems to think that the resulting projection matrix should be fine.

Share this post


Link to post
Share on other sites
Have you used PIX?
If not, this is an excellent opportunity to start. It's located in DirextX SDK folder/Utilities/bin/x86 or x64. If you run your program through it, it can tell you all the current states of your device at each draw-call, and much more.

EDIT: Ah, I see you got debug output now. Show your matrix setup code if it still doesn't work, maybe it's just an unlucky mistake with it.

Share this post


Link to post
Share on other sites
[strike]Why do you say depth "seems" to "refuse" to turn on? Can you post code for your present parameters, and your shader? [/strike]

EDIT: If you have the near plane set to 0, set it to 0.01 or something else small - but not 0. With the warnings you're getting, it's likely something other than your device parameters is the problem. How are you setting the projection and view matrices?

Share this post


Link to post
Share on other sites
PIX for Windows 64 likes to crash and corrupt it's files. It was the first thing that I went through.

Edit:

The scene renders fine, just without depth buffering.

D3DDeviceParameters.Windowed = true;
D3DDeviceParameters.BackBufferHeight = 0;
D3DDeviceParameters.BackBufferWidth = 0;
D3DDeviceParameters.BackBufferCount = 1;
D3DDeviceParameters.BackBufferFormat = D3DFMT_A8R8G8B8;
D3DDeviceParameters.MultiSampleQuality = 0;
D3DDeviceParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
D3DDeviceParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DDeviceParameters.hDeviceWindow = OS->GetHWND();
D3DDeviceParameters.EnableAutoDepthStencil = true;
D3DDeviceParameters.AutoDepthStencilFormat = D3DFMT_D24S8;
D3DDeviceParameters.Flags = 0;
D3DDeviceParameters.FullScreen_RefreshRateInHz = 0;
D3DDeviceParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;



Shader:



uniform extern float4x4 gWVP;
static float a[2] = {0.8f, 0.2f};
static float k[2] = {1.0, 8.0f};
static float w[2] = {1.0f, 8.0f};
static float p[2] = {0.0f, 1.0f};

float SumOfRadialSineWaves (float x, float z)

{
// Distance of vertex from source of waves (which we set
// as the origin of the local space).
float d = sqrt(x*x + z*z);

// Sum the waves.
float sum = 0.0f;
for(int i = 0; i < 2; ++i)
sum += a*sin(k*d - w + p);
return sum;
}

float4 GetColorFromHeight(float y)
{
if( abs(y) <= 0.2f) // black
return float4(0.0f, 0.0f, 0.0f, 1.0f);
else if(abs(y) <= 0.4f ) // blue
return float4(0.0f, 0.0f, 1.0f, 1.0f);
else if(abs(y) <= 0.6f ) // green
return float4(0.0f, 1.0f, 0.0f, 1.0f);
else if(abs(y) <= 0.8f ) // red
return float4(1.0f, 0.0f, 0.0f, 1.0f);

else // yellow
return float4(1.0f, 1.0f, 0.0f, 1.0f);
}

// Structure
struct OutputVS
{
float4 posH : POSITION0;
float4 colour : COLOR0;
};

OutputVS ColourVS(float3 posL : POSITION0)
{
// Zero out our output.
OutputVS outVS = (OutputVS)0;

// Get the height of the vertex--the height is given by
// summing sine waves.
posL.y += 0.001f * SumOfRadialSineWaves(posL.x, posL.z);

// Generate the vertex color based on its height.
outVS.colour = GetColorFromHeight(posL.y);

// Transform to homogeneous clip space.
outVS.posH = mul(float4(posL, 1.0f), gWVP);

// Done--return the output.
return outVS;
}

// Pixel shader
float4 TransformPS(float4 colour : COLOR0) : COLOR
{
return colour;
}

technique TransformTech
{
pass P0
{
// Specify the vertex and pixel shader associated
// with this pass.
vertexShader = compile vs_3_0 ColourVS();
pixelShader = compile ps_3_0 TransformPS();

// Specify the render/device states associated with
// this pass.
FillMode = Wireframe;
}
}


The shader's from a tutorial and just makes the geometry have coloured bands. I don't have any lighting or anything like that.

Share this post


Link to post
Share on other sites

PIX for Windows 64 likes to crash and corrupt it's files. It was the first thing that I went through.


Do you compile your program for x64?
Remember that unless you change it the default working directory will be different when running through PIX, so make sure to use the correct paths to your data files or copy your files to the output directory.

Share this post


Link to post
Share on other sites
The near plane was the answer. I had it set to zero because I was having fun zooming through coloured wireframes and didn't like how they didn't disappear smoothly. I set it to 0.01f and the problem is solved.

I compile for x86, not x64, but PIX has a specific x64 version, so I would expect that it would be able to handle running on x64. I did remember to copy all my other data files like the shader to the working directory. My program executes fine- at least, as fine as it does when run from Visual Studio, and the first few PIX runs worked OK, but it crashes incessantly now and if it succeeds, then I can't view anything and it gives a file corruption error.

Share this post


Link to post
Share on other sites
[strike]Why do you say depth buffering isn't working? [/strike]

EDIT: excellent. glad you found the problem.

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!