MRT & Z Pre-Pass manually ?

Started by
4 comments, last by _Camus_ 13 years, 2 months ago
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.
Advertisement
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.
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.
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.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

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).
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.
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.
If the exact same geo is drawn with the exact same matrices, there won't be z-fighting.

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


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);


And I was right:

zfighting.png

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


...
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);



And, I was wrong:

bias.png


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

Thanks Again

This topic is closed to new replies.

Advertisement