Sign in to follow this  

Writing to multiple RenderTarget2D's from HLSL pixel shader

Recommended Posts

[font=arial,helvetica,sans-serif]I need to write to two render targets: one for colour and another for depth+normal (for post-processing). I have a problem, though. Both targets seem to be getting the value output by COLOR0, while the second should logically be getting the value from COLOR1. What did I do wrong?[/font]

[font=arial,helvetica,sans-serif]RT Initialization:[/font]
[font=arial,helvetica,sans-serif][CODE]renderTarget = new RenderTarget2D(device, gameResolution.Width, gameResolution.Height, false, graphics.PreferredBackBufferFormat, DepthFormat.Depth24);
extraTarget = new RenderTarget2D(device, gameResolution.Width, gameResolution.Height, false, SurfaceFormat.Color, DepthFormat.None);[/CODE][/font]

[font=arial,helvetica,sans-serif]Using RTs:[/font]
[font=arial,helvetica,sans-serif][CODE]Device.SetRenderTargets(new RenderTargetBinding(renderTarget), new RenderTargetBinding(extraTarget));[/CODE][/font]

[font=arial,helvetica,sans-serif]Relevant HLSL:[/font]
[font=arial,helvetica,sans-serif][CODE]struct Pixel
float4 Color : COLOR0;
float4 Extra : COLOR1;

Pixel PS(Middle input)
Pixel output;

// perform other shader code

output.Color = float4(totalDiffuse * matDiffuseColor * color.rgb + totalSpecular * matSpecularColor, color.a);
output.Extra = float4(input.Position.z / input.Position.w, clippedNormal);

return output;

Share this post

Link to post
Share on other sites
First of all, you will have precision problems with this scheme. You should store depth using a single surface format and normals in a floating point render target.

Why don't you try: Device.SetRenderTargets(renderTarget, extraTarget);

If this work, then you have a problem with RenderTargetBinding (that is used to avoid garbage). Try to pass the RenderTargetBinding like you should, in a array.

Share this post

Link to post
Share on other sites
I managed to get the separate writing working properly. Using the same shader code, however, it seems my Extra RT is not getting depth-tested. I gave it a depth buffer, but the buffer accumulates, causing the "color" output that should be occluded to draw like it's additive.

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