Original Post
I am experiencing a strange problem with depth. Here's the background:
1. I successfuly create the depth buffer
2. I successfuly render objects with perspective
---
My depth is incorrect. I tried to output the depth from the shader using:
VS:
psInput.Pos = mul( Pos, mul(mul(World,View),Projection) );
PS:
float depth = psInput.Pos.z;
return float4(depth,depth,depth,1);
The result is black screen. In contrast, when I show psInput.Pos.x or psInput.Pos.y i get correct gradient.
---
I decided it was transformation problem so I examined in debug mode the world, view, and projection matrices, which looked like this:
WORLD: Identity Matrix
VIEW: D3DXMatrixLookAtLH( &mViewMatrix, &eye, &view, &up );
1,0,0,0
0,1,0,0
0,0,1,0
5,5,5,1
PROJECTION: D3DXMatrixPerspectiveFovLH ( &mProjectionMatrix, mCamera->mFOV, mCamera->mAspectRatio, mCamera->mNearClipping, mCamera->mFarClipping);
1.8, 0, 0, 0
0, 2.4, 0, 0
0, 0, 1, 1
0, 0, -1, 0
---
Heres my rasterizer setup:
Here's my depth setup:
Thanks in advance.
Cheers,
Dimo
1. I successfuly create the depth buffer
2. I successfuly render objects with perspective
---
My depth is incorrect. I tried to output the depth from the shader using:
VS:
psInput.Pos = mul( Pos, mul(mul(World,View),Projection) );
PS:
float depth = psInput.Pos.z;
return float4(depth,depth,depth,1);
The result is black screen. In contrast, when I show psInput.Pos.x or psInput.Pos.y i get correct gradient.
---
I decided it was transformation problem so I examined in debug mode the world, view, and projection matrices, which looked like this:
WORLD: Identity Matrix
VIEW: D3DXMatrixLookAtLH( &mViewMatrix, &eye, &view, &up );
1,0,0,0
0,1,0,0
0,0,1,0
5,5,5,1
PROJECTION: D3DXMatrixPerspectiveFovLH ( &mProjectionMatrix, mCamera->mFOV, mCamera->mAspectRatio, mCamera->mNearClipping, mCamera->mFarClipping);
1.8, 0, 0, 0
0, 2.4, 0, 0
0, 0, 1, 1
0, 0, -1, 0
---
Heres my rasterizer setup:
D3D10_RASTERIZER_DESC rasterizerState;
rasterizerState.CullMode = D3D10_CULL_BACK;
rasterizerState.FillMode = D3D10_FILL_SOLID;
rasterizerState.FrontCounterClockwise = true;
rasterizerState.DepthBias = false;
rasterizerState.DepthBiasClamp = 0;
rasterizerState.SlopeScaledDepthBias = 0;
rasterizerState.DepthClipEnable = true;
rasterizerState.ScissorEnable = false;
rasterizerState.MultisampleEnable = false;
rasterizerState.AntialiasedLineEnable = true;Here's my depth setup:
// Create depth stencil texture
D3D10_TEXTURE2D_DESC descDepth;
ZeroMemory( &descDepth, sizeof(D3D10_TEXTURE2D_DESC));
descDepth.Width = mWidth;
descDepth.Height = mHeight;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D10_USAGE_DEFAULT;
descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
mDX10Device->CreateTexture2D( &descDepth, NULL, &pDepthStencil );
D3D10_DEPTH_STENCIL_DESC dsDesc;
ZeroMemory( &dsDesc, sizeof(D3D10_DEPTH_STENCIL_DESC));
// Depth test parameters
dsDesc.DepthEnable = true;
dsDesc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = D3D10_COMPARISON_LESS;
// Stencil test parameters
dsDesc.StencilEnable = true;
dsDesc.StencilReadMask = 0xFF;
dsDesc.StencilWriteMask = 0xFF;
// Stencil operations if pixel is front-facing
dsDesc.FrontFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilDepthFailOp = D3D10_STENCIL_OP_INCR;
dsDesc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
// Stencil operations if pixel is back-facing
dsDesc.BackFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilDepthFailOp = D3D10_STENCIL_OP_DECR;
dsDesc.BackFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
// Create depth stencil state
mDX10DSState = NULL;
mDX10Device->CreateDepthStencilState(&dsDesc, &mDX10DSState);
mDX10Device->OMSetDepthStencilState(mDX10DSState, 1);
// Create the depth stencil view
D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory( &descDSV, sizeof(D3D10_DEPTH_STENCIL_VIEW_DESC));
descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
mDX10Device->CreateDepthStencilView( pDepthStencil, &descDSV, &mDX10DSView );
// Set render targets
mDX10Device->OMSetRenderTargets( 1, &mDX10RTView, mDX10DSView );Thanks in advance.
Cheers,
Dimo