# DX11 Depth issue in geometry shader

## Recommended Posts

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:

" rel="external">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!

Edited by GreenGodDiary

##### Share on other sites
38 minutes ago, GreenGodDiary said:

fail the Z test

Do you enabled a Z-test in the first place?

##### Share on other sites
42 minutes ago, matt77hias said:

Do you enabled a Z-test in the first place?

I dont think ive done that explicitly, no, but I can't recall having done that for any of the other DX apps ive worked on.
Isn't the Z-test done automatically by the rasterizer?

Edited by GreenGodDiary

##### Share on other sites
14 hours ago, GreenGodDiary said:

Isn't the Z-test done automatically by the rasterizer?

You need to specify the right depth/stencil state. The default D3D11_DEPTH_STENCIL_DESC enables the depth, but I do not know if you specify a state yourself. If you have no idea about the latter, just specify two different depths for these quads in your GS, and see what happens. Then repeat but swap the depths.

Edited by matt77hias

##### Share on other sites

You also have to have a depth buffer bound by calling OMSetRenderTargets and providing a depth-stencil view of a texture as the third argument.

##### Share on other sites

11 hours ago, turanszkij said:

You also have to have a depth buffer bound by calling OMSetRenderTargets and providing a depth-stencil view of a texture as the third argument.

This was the issue. I called OMSetRenderTargets only before I actually created the depth-stencil view.
Works as expected now, thank you!

## Create an account

Register a new account

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633669
• Total Posts
3013256
×