MRT with each pass rendering to a specific target

I’m working on a Depth Peeling system that tries to not use Replacement shaders. So I setup multiple render targets but because the diffuse is going to be blended I’m trying to write out the depth in the first pass but output it to a specific target and then in the second pass output to the other target. The problem is that when I look at my second render target( destination for diffuse ) it seems to have blended with the depth texture. Is it possible to setup MRT and have one pass output to one target and the second pass output to a second target??

Tried this:

half4Depth_Frag( _v2fi ) : COLOR0 {
#ifDEPTH_PEEL_ENABLED
floatprevDepth = DecodeFloatRGBA(tex2Dproj(_DepthTex, UNITY_PROJ_COORD(i.screenPos)));

//needslightbias
if( i.depth <= (prevDepth + 0.000001) ) {
discard;
}
#endif

returnEncodeFloatRGBA( i.depth );
}

Then in the second pass( which has alpha blending setup )


half4Color_Frag( _v2fi ) : COLOR1 {
#ifDEPTH_PEEL_ENABLED
floatprevDepth = DecodeFloatRGBA(tex2Dproj(_DepthTex, UNITY_PROJ_COORD(i.screenPos)));

//needslightbias
if( i.depth <= (prevDepth + 0.000001) ) {
discard;
}
#endif

half4texColor = tex2D( _MainTex, i.uv.xy );
returnhalf4( texColor.rgb, _Color.a );
}

The first pass seems to work but when I look at the output of the second pass it seems to have blended with the depth. As if I was outputting to the same render target for both passes.

In my original implementation I had it all in one pass but it occurred to me that I would be blending the depth also which is why I am trying to use two passes.

Just curious if anyone can show what I am doing wrong.

Any help would be great. Thank you

1768866–112128–Bumped-Glossy-ZTestGreater.shader (2.74 KB)
1768866–112130–DepthPeelStateManager.cs (4.3 KB)
1768866–112131–DepthPeelObjectShaderManager.cs (1.21 KB)

If you’re not outputting to multiple render targets at the same time, it’s not MRT.

But besides that, yes, you can of course render one pass to one target and another to another target. But not directly using the passes. Just render the object with shader1 to target1 and then with shader2 to target2.

What would be the best way to do that? My thought process was to try and use multiple passes to reduce redownloading of geometry and textures to the card. I’m assuming with multiple passes, each pass is just a separate program and we would only have to download the second program. Were you thinking of using Graphics.DrawMeshNow??

Thanks again for the help.

You can render the same geometry twice with different shaders. Often all geometry needs to be rendered with a single shader for these extra passes. In steps:

  • Create an empty gameObject with a script attached.
  • Let this script create a camera on start.
  • Copy the contents from the main camera. (camera.CopyFrom(Camera.main))
  • Disable the camera, so it doesn’t render by itself.
  • Set the target using SetTargetBuffers.

Now every frame you can render the scene using RenderWithShader.

If you need to disable some geometry in a pass, I’d suggest using a cullingMask.