• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
AmzBee

Pixel shader depth comparison issue

4 posts in this topic

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:

[CODE]
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();
}
}
[/CODE]

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
0

Share this post


Link to post
Share on other sites
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.
[quote name='AmzBee' timestamp='1337290940' post='4941037']
[CODE]
// Calculate screen uv.
float2 NormalizedScreenPos = ScreenPos / ScreenSize;
float2 ScreenUV = (NormalizedScreenPos + (0.5f / ScreenSize.x)) * ScreenSize;
[/CODE]
[/quote]

First thing you do in manualSample is to scale it again.
[quote name='AmzBee' timestamp='1337290940' post='4941037']
[CODE]
float2 texelpos = textureSize * UV;
[/CODE]
[/quote]
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:
[CODE]// 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);[/CODE]
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? [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] 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
1

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:

[CODE]
if(GBufferDepth < PixelDepth) { discard; }
[/CODE]

...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:

[img]http://xpod.be/gd1x.jpg[/img]

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

Aimee.
0

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
1

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
0

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
Sign in to follow this  
Followers 0