Skip to main content
GameDev.net gamedev.net
🔒 Locked

Writing to multiple RenderTarget2D's from HLSL pixel shader

Started by Klutzershy Apr 20, 2012 at 6:58 PM 2 replies 3.2k views
Original Post
Klutzershy
Klutzershy
[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]
"So there you have it, ladies and gentlemen: the only API I’ve ever used that requires both elevated privileges and a dedicated user thread just to copy a block of structures from the kernel to the user." - Casey Muratori   boreal.aggydaggy.com
jischneider
jischneider
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.
[size=1]Project page: [size=1] <[size=1] XNA FINAL Engine[size=1] [size=1] >
Klutzershy
Klutzershy
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.
"So there you have it, ladies and gentlemen: the only API I’ve ever used that requires both elevated privileges and a dedicated user thread just to copy a block of structures from the kernel to the user." - Casey Muratori   boreal.aggydaggy.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.