Sign in to follow this  

MRT & Z Pre-Pass manually ?

This topic is 2554 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, I am about to pull the trigger with my first attempt to render to multiple targets, because I want to do SSAO,
the implementation with normals, that's why I am using MRT, the first one is for depth (D3DFMT_D24X8) the
second one is for normals (D3DFMT_A8R8G8B8).

Now, I heard about Z Pre-Pass, and right now I have the depth stored, can I use it as depth stencil and let the
comparison made it automatically with D3DCMP_LESSOREQUAL? or I have to do it in the pixel frament,
and discard every pixel if not pass my depth comparison manually?

I know how to use the depth stencil directly writting black, but I don't know if it is possible with MRT AND
I need the depth as texture to be able to reconstruct position from it.

thanks and please sorry my bad grammar, you can spot my errors, that will help too.

Share this post


Link to post
Share on other sites
What hardware/API are you targetting? In D3D11 with FEATURE_LEVEL_11, you can create a read-only depth stencil view for your depth buffer. That lets you keep the buffer bound for depth/stencil testing (you can't enable depth or stencil writes), while simultaneously sampling it in the pixel shader. For FEATURE_LEVEL_10_1 (DX10.1), your next-best option is to copy the contents of the depth buffer to another texture and use that for depth testing. For FEATURE_LEVEL_10 you don't even have that option, so you're pretty much stuck.

Share this post


Link to post
Share on other sites
The target is D3D9 shader model 3.0, now I don't know what would be the best choice to do, because, I inevitably will draw the geometry twice because of the SSAO,
I thought that is possible to perform a depth test using the depth texture (thinking in shadow map), and discard pixels beyond the depth, but there will be z-fighting for
sure.

Share this post


Link to post
Share on other sites
For dx9 you have to create an extra render target of Float32 and then you should output the depth in what ever space you need --like world space depth -- so that you can simply read it in later without reconstructing anything. In dx9 you cannot read from the depth buffer in the pixel shader. In dx10, you can create a depth buffer with the ability to sample it in the pixel shader.

Also, most early-z passes do not cause an increase in speed. You must have an extremely heavy pixel shader to get any benefit out of it.

Share this post


Link to post
Share on other sites
[quote name='_Camus_' timestamp='1295347548' post='4760642']
Hi, I am about to pull the trigger with my first attempt to render to multiple targets, because I want to do SSAO,
the implementation with normals, that's why I am using MRT, the first one is for depth (D3DFMT_D24X8) the
second one is for normals (D3DFMT_A8R8G8B8).[/quote]D3DFMT_D24X8 is for use as a depth-stencil target, not as an MRT target. As mentioned above, you should use a floating-point, or ARGB8 texture here instead of a D24 one.

Or, if your card supports reading from depth-buffers, then you don't need MRT - just use the depth-stencil target.[quote name='_Camus_' timestamp='1295387980' post='4760914']I thought that is possible to perform a depth test using the depth texture (thinking in shadow map), and discard pixels beyond the depth, but there will be z-fighting for sure.[/quote]If the exact same geo is drawn with the exact same matrices, there won't be z-fighting.[quote name='smasherprog' timestamp='1295413040' post='4761055']
In dx9 you cannot read from the depth buffer in the pixel shader. In dx10, you can create a depth buffer with the ability to sample it in the pixel shader.[/quote]You can read from the depth buffer in DX9 if the end-user has a DX10-level graphics card.

To do this, you make a texture with D3DFMT_INTZ instead of D3DFMT_D24X8. To check if this is supported you can use:[code]const static D3DFORMAT D3DFMT_INTZ = (D3DFORMAT) MAKEFOURCC('I','N','T','Z');
bool supportsIntZ = d3d->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, displayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ);[/code]

Share this post


Link to post
Share on other sites
Hi, Thanks

@Hodgman that DX9 secret capability is awesome, and yes, was my mistake is D3DFMT_R32F, I was thinking in depth stencil too much :rolleyes:

Now I can read it directly, in fact the Z Pre-Pass is working now:

1 - Render to depth
2 - Clear only target, disabling Z Writting and ZFunc to LessEqual

Using 3 point lights, Z Pre Pass saves me about .1 ms on the second pass. Now that I also can read directly from depth, I was curious to make the
depth test by myself on the fragment program, first to ensure that I was using the right projective texture coordinates I did the follow specting to have
z fighting:

[code]
float2 Texcoords = 0.5 * In.Other.xy / In.Other.w + float2( 0.5, 0.5 );
Texcoords.y = 1.0f - Texcoords.y;
float dist = In.Other.z/ In.Other.w;

if(dist == tex2D( nolerpsampler, Texcoords ).r)
return float4(1.0f,0.0f,0.0f,1.0f);
[/code]

And I was right:

[img]http://img233.imageshack.us/img233/7857/zfighting.png[/img]

Well, I thought it would be a matter of adding bias to the distance and do the test:

[code]
...
float dist = (In.Other.z + 0.001f) / In.Other.w;

if(dist > tex2D( nolerpsampler, Texcoords ).r)
return float4(1.0f,0.0f,0.0f,1.0f);

[/code]

And, I was wrong:

[img]http://img717.imageshack.us/img717/2075/bias.png[/img]


Why? It is supposed to be (dist + bias), and not (dist - bias), why it works inverse to what I spected?

Thanks Again

Share this post


Link to post
Share on other sites

This topic is 2554 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.

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