Alpha testing with the depthbuffer enabled ?

If i remember right, In the old xna 3.1 you could flip on the depth buffer with alpha test on.
turn multisample anti aliasing off and set the filter to point clamp or wrap and get a cheap quick zbuffered alpha test.

But i have no clue how what to do here whats the quickest way to get both at once or do i need to do this thru my shader now.

If so how do i work with the depth buffer within the shader.
I have no clue how to access and use that depth info within a shader (i suck with shaders).

What steps should i take ? Or in what order should i take them to do that or learn how to do that ?

To illustrate clearly.

The problem is shown below.

With depth testing on the order affects the gpu’s alpha discarding of pixels, i.e. the entire quad is set to the depth buffer regardless of transparent alpha pixels on the texture or not.

I guess im wondering if there is a way to just tell the gpu to not set the depth buffer while building up the buffers color on transparent pixels, until that pixel actually becomes opaque and to then set the discarding depth per pixel depending on alpha. In a easy way.

The problem with turning of the depth buffer is obvious.
Things bleed thru from behind. Like the red arrows shown below one of them is on the surface the other is behind it but drawn after the mesh.


You need to read the alpha value, and if above a treshold (alphatest), you use clip(-1) to skip the pixel.
When you have the depth from the vertexshader:

output.Depth = input.Depth.x / input.Depth.y;
clip(-output.Depth + 0.9999f); //skip if "too far"
if (useAlphaMask)
        if (output.Color.a < AlphaTestValue)

If you prefer to avoid the depth buffer (sort of, who doesn’t like it ? :slight_smile: ), you can sort your objects from the farthest to the closest based on the distance to the cam, on CPU, before drawing them, this is what is done in the particles sample from XNA (A “speed” mode without test to use with additive particles, and a sorted one for grass for ex).

Did you try to render only the depth in a rendertager to experiment ?
I would say you need to use the DEPTH semantic when you return the value in your pixelshader.

Do you know of any examples and tutorials that do this i have no idea how to use the depth semantic at all in a shader.

I would not depth sort on the cpu not on triangles in 3d.

If you can sort the objects, it can help to reduce the work of transparent objects when trying to discard one pixels many times.

I don’t have my bookmarks here :confused:
But you can have a look at:

To solve your problem, you have to do 2 things:

  • compute the depth in your pixel shader for each pixel,
  • discard them if they are alphatested false

This code will only show your depth, All of the triangles, not just the arrows. This part is “cleaned” in the pixel shader with the code above.

But… If you want to draw the depth, you can get the depth as usual in your shader:

struct VertexShaderOutput
	float4 Position : SV_POSITION0; // POSITION0;
	float2 TexCoord : TEXCOORD0;
	float2 Depth : TEXCOORD1;

in the Vertexshader:

float4 pos = float4(, 1);

float4 worldPosition = mul(pos, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.Depth.x = output.Position.z;
output.Depth.y = output.Position.w;

In the pixelshader:

float4 PixelShaderFunction(VertexShaderOutput input)
float depth = input.Depth.x / input.Depth.y;
return float4(depth,depth,depth,0); //Draw the depth in gray

So you can see if alpha is clipped.

Nb: How can I better present excerpts of code ? isn’t there a code syntax ?

You can do
clip(output.Color.a - AlphaTestValue);
Clip does not draw the pixel if the passed value is less than zero.

I just wrote it this way for the explanation of clip() :wink:

Humm im gonna have to look at the samples lol
This is going right over my head and im sleepy.