• Advertisement
Sign in to follow this  

Writing to multiple RenderTarget2D's from HLSL pixel shader

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

[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]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);
[/font]

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

[font=arial,helvetica,sans-serif]Relevant HLSL:[/font]
[font=arial,helvetica,sans-serif]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;
}
[/font]

Share this post


Link to post
Share on other sites
Advertisement
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
Sign in to follow this  

  • Advertisement