Jump to content

  • Log In with Google      Sign In   
  • Create Account


Pixel shader depth comparison issue


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Tarika   Members   -  Reputation: 481

Like
0Likes
Like

Posted 17 May 2012 - 03:42 PM

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, 17 May 2012 - 11:27 PM.

We are now on Tumblr, so please come and check out our site! 

 

http://xpod-games.com


Sponsor:

#2 Tsus   Members   -  Reputation: 1022

Like
1Likes
Like

Posted 18 May 2012 - 09:35 AM

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? Posted Image 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, 18 May 2012 - 07:05 PM.

Acagamics e.V. – IGDA Student Game Development Club (University of Magdeburg, Germany)


#3 Tarika   Members   -  Reputation: 481

Like
0Likes
Like

Posted 18 May 2012 - 03:04 PM

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:

Posted Image

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

Aimee.

We are now on Tumblr, so please come and check out our site! 

 

http://xpod-games.com


#4 Tsus   Members   -  Reputation: 1022

Like
1Likes
Like

Posted 18 May 2012 - 07:04 PM

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

Attached Files


Acagamics e.V. – IGDA Student Game Development Club (University of Magdeburg, Germany)


#5 Tarika   Members   -  Reputation: 481

Like
0Likes
Like

Posted 18 May 2012 - 07:21 PM

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, 18 May 2012 - 07:31 PM.

We are now on Tumblr, so please come and check out our site! 

 

http://xpod-games.com





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS