Jump to content
  • Advertisement
Sign in to follow this  
FoxHunter2

Usages for Early Z pass

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

Hi everybody, I recently read lots about doing an early z-pass for the entire scene, which enables a lot of useful uses. But which are those? I know about one of those, which is doing screen space ambient occlusion, like Crysis does it. I haven't tried to implement it yet, though. What else can be done using this early-z technique? Right now I'm using this shader to write the depth into a R32F texture, and the result looks correct.
void vs(
	in float4 Position : POSITION,
	out float4 vPosition : POSITION,
	out float Depth : TEXCOORD0)
{
	vPosition = mul(Position, mul(World, mul(View, Projection)));

	float distanceToCam = length(mul(Position, mul(World, View)));
	Depth = distanceToCam / FarPlane; // range [0..1]
}

float4 ps(in float Depth : TEXCOORD0) : COLOR
{
	return float4(Depth, 0, 0, 1);
}

Is this shader fine, can it be done better? Is there any setup that needs to be done before rendering, like setting certain renderstates or do some sorting? Also, which nice things can I now do with this depth information? :) regards

Share this post


Link to post
Share on other sites
Advertisement
Early-Z is an optimization, plain and simple. If you take it beyond that then you're more into the realm of deferred rendering and post-processing.

For the most part an early-z approach is almost a hybrid between traditional forward and deferred rendering - by allowing you to avoid expensive shading operations on pixels that do not contribute to the final image.

As for your shader, I've not checked it but you don't need anything special here. The real trick is at the application level unless you specifically want to feed the depth information into a later stage.

hth
jack

Share this post


Link to post
Share on other sites
Thanks so far, the shader is correct.
And yes, i'd like to use it as a hybrid approach for forward and deferred rendering, that's what I read also. I have quite some complex shaders that may be executed unnecessarily because the final primitives will be hidden by other objects. I'm already using frustum culling, LOD and front-to-back rendering, but I'd like to take this one step further.
I just haven't found enough articles yet what early-z can be really useful for.

Share this post


Link to post
Share on other sites
As you said, the main reason for EarlyZ passes is to prefill the depth buffer with the correct scene so that any unneccessary pixel shader executions can be eliminated. It's useful if the main pass uses long shaders, but the exact length of the shaders to justify the additional CPU and fillrate overhead is uncertain.

If you have such a prepass, you can as well write out the scene depth or some other informations into a separate buffer. Once you have the depth, there's a lot of techniques that can make use of it. You can do fog calculations in a post processing effect very easily, you can employ Depth Of Field Blur on the image, you can use Soft Particles and so on...

I integrated a EarlyZ pass into our engine some time ago. It shows that on most of the upper field gfx boards it is actually a loss of performance, only midrange boards such as the GF6600 or similar did win by this prepass. But now that a lot of post processing effekts rely on the depth rendered out at this pass, we can't remove it anymore :-)

DX10 is said to be able to solve this because you can bind the ZBuffer directly as a texture and sample from it. It would help us alot, but the minimum specs we're aiming for don't allow the use of DX10.

Share this post


Link to post
Share on other sites
This sounds like some very useful stuff. I especially like the idea of applying fog, depth blur etc as a post process, I can imagine it's relatively easy to do this stuff once you have the depth already captured.

I'll see if I will implement it in my little engine.
Are there any renderstates etc I need to set before drawing everything in my z-pass shader?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!