Jump to content
  • Advertisement
GreenGodDiary

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites

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

  • 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!