• Advertisement
Sign in to follow this  

Pixel shader depth comparison issue

This topic is 2076 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

Hey there, I was wondering if anyone could help me with a bit of a puzzle. I'm working on a shader idea I had to introduce some kind of alpha-blending to my deferred renderer implementation, the idea is to re-use the depth texture produced in my GBuffer, and then compare (on a pixel shader baisis) weather a render seporate of the deferred process is occluded by the depth texture data.

To do this i've written this:


float4x4 World;
float4x4 View;
float4x4 Projection;
float4 WireColor;
float2 ScreenSize;
sampler DepthTexture : register(s0);
struct VertexShaderInput { float4 Position : POSITION0; };
struct VertexShaderOutput { float4 Position : POSITION0; float4 ProjectedPos : TEXCOORD0; float3 Depth : TEXCOORD1; };


float4 manualSample(sampler Sampler, float2 UV, float2 textureSize)
{
float2 texelpos = textureSize * UV;
float2 lerps = frac(texelpos);
float texelSize = 1.0 / textureSize;
float4 sourcevals[4];
sourcevals[0] = tex2D(Sampler, UV);
sourcevals[1] = tex2D(Sampler, UV + float2(texelSize, 0));
sourcevals[2] = tex2D(Sampler, UV + float2(0, texelSize));
sourcevals[3] = tex2D(Sampler, UV + float2(texelSize, texelSize));
float4 interpolated = lerp(lerp(sourcevals[0], sourcevals[1], lerps.x), lerp(sourcevals[2], sourcevals[3], lerps.x), lerps.y);
return interpolated;
}


VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;

float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);

output.Position = mul(viewPosition, Projection);
output.ProjectedPos = output.Position;

output.Depth.x = output.Position.z;
output.Depth.y = output.Position.w;
output.Depth.z = viewPosition.z - output.Position.z;

return output;
}


float4 PixelShaderFunction(VertexShaderOutput input, in float2 ScreenPos : VPOS) : COLOR0
{
// Calculate screen uv.
float2 NormalizedScreenPos = ScreenPos / ScreenSize;
float2 ScreenUV = (NormalizedScreenPos + (0.5f / ScreenSize.x)) * ScreenSize;

// Sample depth from gbuffer.
float GBufferDepth = manualSample(DepthTexture, ScreenUV, ScreenSize).x;

// Get the current depth.
float PixelDepth = (input.Depth.x / input.Depth.y);

// If GBuffer sample is closer, discard this pixel.
if(GBufferDepth < PixelDepth) { discard; }

return WireColor;
}


technique Technique1
{
pass Pass1
{
VertexShader = compile vs_3_0 VertexShaderFunction();
PixelShader = compile ps_3_0 PixelShaderFunction();
}
}


The issue I am facing is that I believe that I'm either re-constructing the depth from the GBuffer incorrectly, not getting the proper depth within the pixel shader, or even both. Instinct is telling me that for some reason the depth is not being calculated properly somewhere. Thanks to anyone who can help me wrap my head around this.

Aimee. Edited by AmzBee

Share this post


Link to post
Share on other sites
Advertisement
Hello Aimee,

The depth passed in from the vertex shader looks fine and it’s also right that you do the perspective division in the pixel shader.
I think the way you compute the texture coordinate for reading from the depth map is a little off.

You see, here you scaled the normalized texture coordinate (in [0,1]) up to the screen size.


// Calculate screen uv.
float2 NormalizedScreenPos = ScreenPos / ScreenSize;
float2 ScreenUV = (NormalizedScreenPos + (0.5f / ScreenSize.x)) * ScreenSize;



First thing you do in manualSample is to scale it again.


float2 texelpos = textureSize * UV;


In actuality, I think you shouldn’t have scaled it up even once, because texture coordinates are in [0,1] when you use tex2D.

I haven’t tested it, but I think this should work just fine:
// scale-bias the clipping space pos from [-1,1] to [0,1]
float2 NormalizedScreenPos = 0.5*input.ProjectedPos.xy/input.ProjectedPos.w+0.5;
// flip the y-coordinate as the origin of the window and the texture are different
NormalizedScreenPos.y = 1- NormalizedScreenPos.y;
float GBufferDepth = tex2D(Sampler, NormalizedScreenPos + HalfPixelOffset);

This way, you also don’t need to pass in the resolution of the viewport to find the screen coordinate.

It’s also interesting to see you implementing bilinear filtering yourself. A little experimenting, right? smile.png As you probably know, the sampler can already do this for you.

Would you mind explaining how you like to use this for alpha blending in your deferred renderer?
Are you looking for something like soft particles?

Hope this helps, though!
Cheers! Edited by Tsus

Share this post


Link to post
Share on other sites
Thanks for the reply, I thought I might have been going the long way round with the UV lookup lol. What I'm trying to achieve is a post pass that I can use as a template for blending translucent stuff like water over the already rendered deferred scene, and most importantly culling pixels that are occluded by checking the depth I store in the GBuffer. I know there are a few other techniques like using the stencil buffer for each entity during the deferred process, but as I'm still learning this stuff, it's easier for me to understand a post pass method for now.

Next thing I've got to get round is weather the test I'm doing is actually correct, the following seems logical to me:


if(GBufferDepth < PixelDepth) { discard; }


...although what I see on the screen doesn't tie up with my logic. For the example I'm using a quad in front of another quad, then repeating the render of the two quads instead drawing a wireframe, to me if that test worked, then the wired quad behind would be occluded by the opaque quad, but it doesn't quite work. Here's a screenie showing what I mean:

gd1x.jpg

I've highlighted the part of the geometry that I'm attempting to cull, thanks again for your help.

Aimee.

Share this post


Link to post
Share on other sites
Hi Aimee,

First off, I made not only one, but two mistakes in my last post. Sorry for that. :-/
(The scale-bias is 0.5*x+0.5 instead of 2*x-1. I used the inverse operation… Aside from that we still need the half pixel offset in Dx9.)

I wrote you a small sample, demonstrating what it should look like.

You probably find the difference to your code very soon.
Does your depth map have single precision?

Best regards

Share this post


Link to post
Share on other sites
For the depth render target I'm using SurfaceFormat.Vector2, which is 64bit, 32-bits for the red, and 32-bits for the green, so yea it's single precision as I'm only using the red channel for now.

That example you've written is exactly what I needed, thanks for your help, and I'll make sure to mention you somewhere when doing credits. you've saved me a weekend of work hahaha. kudos :D Edited by AmzBee

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement