Sign in to follow this  

DX11 [DX11] problem with depth buffer getting no writes

This topic is 2322 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 already posted this on beginners forum, seems to me in is pout of place there... since i wrote to a couple of forum mods, and the thread hasnt been moved, ill repost here...
link to the old post: [url="http://www.gamedev.net/topic/607898-directx-11-depth-buffer-problem/"]http://www.gamedev.n...buffer-problem/[/url] if any mod would be so kind as to delete that

hi im kinda new to these forums so not sure if this is the correct board for posting this, but i have a problem with my depth buffer not working (properly)...

i have set it up like this (following the code from [url="http://www.rastertek.com/dx11tut03.html"]http://www.rastertek.../dx11tut03.html[/url]):

[code]
// Initialize the description of the depth buffer.
D3D11_TEXTURE2D_DESC dbDesc;
ZeroMemory(&dbDesc, sizeof(dbDesc));

// Set up the description of the depth buffer.
dbDesc.Width = SCREEN_WIDTH;
dbDesc.Height = SCREEN_HEIGHT;
dbDesc.MipLevels = 1;
dbDesc.ArraySize = 1;
dbDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
dbDesc.SampleDesc.Count = 1;
dbDesc.SampleDesc.Quality = 0;
dbDesc.Usage = D3D11_USAGE_DEFAULT;
dbDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
dbDesc.CPUAccessFlags = 0;
dbDesc.MiscFlags = 0;

// Create the texture for the depth buffer using the filled out description.
hr = m_mainPtrs.m_pDev->CreateTexture2D(&dbDesc, NULL, &m_depthStencilBuffer);
if (FAILED(hr)) {SHOWERRORMSG("Creating depth stencil buffer target failed"); return FALSE;}

// Initialize the description of the stencil state.
D3D11_DEPTH_STENCIL_DESC dsDesc;
ZeroMemory(&dsDesc, sizeof(dsDesc));

// Set up the description of the stencil state.
dsDesc.DepthEnable = true;
dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = D3D11_COMPARISON_LESS;

dsDesc.StencilEnable = false;
dsDesc.StencilReadMask = 0xFF;
dsDesc.StencilWriteMask = 0xFF;

// Stencil operations if pixel is front-facing.
dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

// Stencil operations if pixel is back-facing.
dsDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
dsDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

// Create the depth stencil state.
hr = m_mainPtrs.m_pDev->CreateDepthStencilState(&dsDesc, &m_depthStencilState);
if (FAILED(hr)) {SHOWERRORMSG("Creating depth stencil state description failed"); return FALSE;}

// Set the depth stencil state.
m_mainPtrs.m_pDevCon->OMSetDepthStencilState(m_depthStencilState, 1);

// Initailze the depth stencil view.
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
ZeroMemory(&dsvDesc, sizeof(dsvDesc));

// Set up the depth stencil view description.
dsvDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
dsvDesc.Texture2D.MipSlice = 0;

// Create the depth stencil view.
hr = m_mainPtrs.m_pDev->CreateDepthStencilView(m_depthStencilBuffer, &dsvDesc, &m_mainPtrs.m_depthStencilView);
if (FAILED(hr)) {SHOWERRORMSG("Creating depth stencil view description failed"); return FALSE;}

// Bind the render target view and depth stencil buffer to the output render pipeline.
m_mainPtrs.m_pDevCon->OMSetRenderTargets(1, &m_mainPtrs.m_pBackbuffer, m_mainPtrs.m_depthStencilView);
[/code]
of course i clear the depth buffer with
[code]
mptrs.m_pDevCon->ClearDepthStencilView(mptrs.m_depthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);
[/code]

now strange enough, depth testing isnt working... the code compiles correctly, i also checked if the used DXGI_FORMAT_D24_UNORM_S8_UINT format is supported for D3D11_FORMAT_SUPPORT_DEPTH_STENCIL, and it is. Further i tried clearing only the depth stencil, turning stencil test off and various depth functions.

the different depth functions also dont give results as expected;
i draw two intersecting objects, always one of them is completely obscuring the other, dependent on the depth comparison function (1 means obscures 2, - means nothing is drawn , obj1 is sent to the pipeline first):
depth buffer cleared to 1.0f:
[code]
LESS LEQ ALWAYS NEVER GRTR GREQ EQ
Obj1 1 2 2 _ _ _ _
Obj2 2 1 1
[/code]

depth buffer cleared to 0.0f:
[code]
LESS LEQ ALWAYS NEVER GRTR GREQ EQ
Obj1 1 2 2 _ _ _ 2
Obj2 2 1 1 1
[/code]

EDIT: hm seems that monospace copy & paste didnt quite pan out on those tables^^, but well you get what is should be where i think

it looks like the whole geometry is collapsed to the near clipping plane. can it be that a projection matrix collapses geometry to the near clipping plane (i use D3DPerspectivesomething to calculate my projection matrix)? never thought about that... and if so why does the LESS depth func act different? obj1 is still getting sent to pipeline first, so it should be below obj2, but its not.

also, obj1 should be partly behind obj2 (since their geometry intersects) however always one is completely drawn above the other....

ive been sitting at this for hours! pls help!!!

thx in advance

system: Windows7 (obviously with dx11)
compiler: VS 10 express
DX SDK Version: ummmm... i downloaded the latest one last week

btw: excuse my bad english, if anything is unclear, or you need more info, just ask

EDIT: update: by passing the vertex shaders output position to the pixel shader i tried drawing the actual depth values in color. the pixelshader now just returns
[code]
float4((depthvalue-0.9)*10.0f,0,0,1)
[/code]
, where depthvalue is position.z/position.w. the subtraction by 0.9 and multiplication by 10 is just so the range 0.9 to 1 gets a better resolution.
i can see clearly that my depth values are calculated correctly, they simply aren't written to the depthbuffer properly :///

[code]

cbuffer pfConst
{
matrix matView;
matrix matProj;
}

cbuffer poConst
{
matrix matWorld;
}

struct VIn
{
float4 position : Position;
float3 normal : Normal;
float2 tex : TexCoord0;
float4 tangent : Tangent0;
};

struct PIn
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 lightDirSurf : TEXCOORD1;
float4 lightParam : TEXCOORD2;
};

sampler texSamp;
Texture2D <float4> colorTex;

PIn VShader(VIn input)
{
PIn output;
input.position.w = 1.0f;
float4 worldPos = mul(input.position,matWorld);
float4 viewPos = mul(worldPos,matView);
output.position = mul(viewPos,matProj);
output.tex = input.tex; //ignore this
output.lightDirSurf = float4(0.0,0.0,0.0,0.0); //ignore this
output.lightParam = output.position; //ignore this
// output.lightParam = float4(lightDist, LightPos.w, 0.0f, 0.0f); //used this texccord for debugging depth
return output;
}


float4 PShader(PIn input) : SV_TARGET
{
float4 color = colorTex.Sample( texSamp, input.tex );
// float depthValue;
// float4 color;
// depthValue = input.lightParam.z / input.lightParam.w;
// color = float4((depthValue-0.9)*10.0f, 0.0f, 0.0f, 1.0f);

return color;
}
[/code]
here are 2 screenshots (ignore the ugly texture in the textured.jpg, its drawn as expected)
[url="http://imageshack.us/g/200/depthq.jpg/"]http://imageshack.us/g/200/depthq.jpg/[/url]
brighter red areas are drawn in front of darker red areas, as seen in textured.jpg,though they should actually be hidden by the black heightfield geometry. depth.jpg was taken with exactly the same setting as textured.jpg, only changed the comments in the pixel shader i can see clearly that my depth values are calculated correctly, they simply aren't written to the depthbuffer properly :///

Share this post


Link to post
Share on other sites
actually, from what i found out so far, the title should be: all my geometry is doing 0.0f depthbuffer writes, regardless of the z and w coordinate output by the vertex shader.

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1312743136' post='4845857']
Are you setting the viewport correctly?
[/quote]

.....
for *$§%$%&"$% sake
how could i forget that

of course, that solved it, thanks :)

EDIT: seriously, i spent like 8hrs searching for my problem... then again, now i know everything about depth buffering there is to know^^, even know all enums pertaining to depth in some way by heart =)

Share this post


Link to post
Share on other sites

This topic is 2322 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      628724
    • Total Posts
      2984404
  • Similar Content

    • By GreenGodDiary
      Having some issues with a geometry shader in a very basic DX app.
      We have an assignment where we are supposed to render a rotating textured quad, and in the geometry shader duplicate this quad and offset it by its normal. Very basic stuff essentially.
      My issue is that the duplicated quad, when rendered in front of the original quad, seems to fail the Z test and thus the original quad is rendered on top of it.
      Whats even weirder is that this only happens for one of the triangles in the duplicated quad, against one of the original quads triangles.

      Here's a video to show you what happens: Video (ignore the stretched textures)

      Here's my GS: (VS is simple passthrough shader and PS is just as basic)
      struct VS_OUT { float4 Pos : SV_POSITION; float2 UV : TEXCOORD; }; struct VS_IN { float4 Pos : POSITION; float2 UV : TEXCOORD; }; cbuffer cbPerObject : register(b0) { float4x4 WVP; }; [maxvertexcount(6)] void main( triangle VS_IN input[3], inout TriangleStream< VS_OUT > output ) { //Calculate normal float4 faceEdgeA = input[1].Pos - input[0].Pos; float4 faceEdgeB = input[2].Pos - input[0].Pos; float3 faceNormal = normalize(cross(faceEdgeA.xyz, faceEdgeB.xyz)); //Input triangle, transformed for (uint i = 0; i < 3; i++) { VS_OUT element; VS_IN vert = input[i]; element.Pos = mul(vert.Pos, WVP); element.UV = vert.UV; output.Append(element); } output.RestartStrip(); for (uint j = 0; j < 3; j++) { VS_OUT element; VS_IN vert = input[j]; element.Pos = mul(vert.Pos + float4(faceNormal, 0.0f), WVP); element.Pos.xyz; element.UV = vert.UV; output.Append(element); } }  
      I havent used geometry shaders much so im not 100% on what happens behind the scenes.
      Any tips appreciated! 
    • By mister345
      Hi, I'm building a game engine using DirectX11 in c++.
      I need a basic physics engine to handle collisions and motion, and no time to write my own.
      What is the easiest solution for this? Bullet and PhysX both seem too complicated and would still require writing my own wrapper classes, it seems. 
      I found this thing called PAL - physics abstraction layer that can support bullet, physx, etc, but it's so old and no info on how to download or install it.
      The simpler the better. Please let me know, thanks!
    • By Hexaa
      I try to draw lines with different thicknesses using the geometry shader approach from here:
      https://forum.libcinder.org/topic/smooth-thick-lines-using-geometry-shader
      It seems to work great on my development machine (some Intel HD). However, if I try it on my target (Nvidia NVS 300, yes it's old) I get different results. See the attached images. There
      seem to be gaps in my sine signal that the NVS 300 device creates, the intel does what I want and expect in the other picture.
      It's a shame, because I just can't figure out why. I expect it to be the same. I get no Error in the debug output, with enabled native debugging. I disabled culling with CullMode.None. Could it be some z-fighting? I have little clue about it but I tested to play around with the RasterizerStateDescription and DepthBias properties with no success, no change at all. Maybe I miss something there?
      I develop the application with SharpDX btw.
      Any clues or help is very welcome
       


    • By Beny Benz
      Hi,
      I'm currently trying to write a shader which shoud compute a fast fourier transform of some data, manipulating the transformed data, do an inverse FFT an then displaying the result as vertex offset and color. I use Unity3d and HLSL as shader language. One of the main problems is that the data should not be passed from CPU to GPU for every frame if possible. My original plan was to use a vertex shader and do the fft there, but I fail to find out how to store changing data betwen shader calls/passes. I found a technique called ping-ponging which seems to be based on writing and exchangeing render targets, but I couldn't find an example for HLSL as a vertex shader yet.
      I found https://social.msdn.microsoft.com/Forums/en-US/c79a3701-d028-41d9-ad74-a2b3b3958383/how-to-render-to-multiple-render-targets-in-hlsl?forum=xnaframework
      which seem to use COLOR0 and COLOR1 as such render targets.
      Is it even possible to do such calculations on the gpu only? (/in this shader stage?, because I need the result of the calculation to modify the vertex offsets there)
      I also saw the use of compute shaders in simmilar projects (ocean wave simulation), do they realy copy data between CPU / GPU for every frame?
      How does this ping-ponging / rendertarget switching technique work in HLSL?
      Have you seen an example of usage?
      Any answer would be helpfull.
      Thank you
      appswert
    • By ADDMX
      Hi
      Just a simple question about compute shaders (CS5, DX11).
      Do the atomic operations (InterlockedAdd in my case) should work without any issues on RWByteAddressBuffer and be globaly coherent ?
      I'v come back from CUDA world and commited fairly simple kernel that does some job, the pseudo-code is as follows:
      (both kernels use that same RWByteAddressBuffer)
      first kernel does some job and sets Result[0] = 0;
      (using Result.Store(0, 0))
      I'v checked with debugger, and indeed the value stored at dword 0 is 0
      now my second kernel
      RWByteAddressBuffer Result;  [numthreads(8, 8, 8)] void main() {     for (int i = 0; i < 5; i++)     {         uint4 v0 = DoSomeCalculations1();         uint4 v1 = DoSomeCalculations2();         uint4 v2 = DoSomeCalculations3();                  if (v0.w == 0 && v1.w == 0 && v2.w)             continue;         //    increment counter by 3, and get it previous value         // this should basically allocate space for 3 uint4 values in buffer         uint prev;         Result.InterlockedAdd(0, 3, prev);                  // this fills the buffer with 3 uint4 values (+1 is here as the first 16 bytes is occupied by DrawInstancedIndirect data)         Result.Store4((prev+0+1)*16, v0);         Result.Store4((prev+1+1)*16, v1);         Result.Store4((prev+2+1)*16, v2);     } } Now I invoke it with Dispatch(4,4,4)
      Now I use DrawInstancedIndirect to draw the buffer, but ocassionaly there is missed triangle here and there for a frame, as if the atomic counter does not work as expected
      do I need any additional synchronization there ?
      I'v tried 'AllMemoryBarrierWithGroupSync' at the end of kernel, but without effect.
      If I do not use atomic counter, and istead just output empty vertices (that will transform into degenerated triangles) the all is OK - as if I'm missing some form of synchronization, but I do not see such a thing in DX11.
      I'v tested on both old and new nvidia hardware (680M and 1080, the behaviour is that same).
       
  • Popular Now