RenderToSurface, Pixel Shaders and Post Processing [Unsolved]

Started by
5 comments, last by Nik02 18 years, 8 months ago
Hey all, I'm working on a system for my 2D engine to add some image post processing. Basically I'm following the pretty standard procedure of rendering the scene to a texture then using a shader to modify that texture. I've hit a little snag though and I'm not quite sure what the solution is. I render the scene to the texture, loop through the scene processing shader, apply the effects and draw the quad. But I have to draw this quad at the end of every shader pass, which ends up overlapping the one previous to it. So really the only post-processing effect I see is the one last applied. I thought about just enabling some sort of blending mode to merge the two images, but I'm not sure if that's the correct solution to the problem, as I'm new to shaders AND render surfaces (heh). Here's the shader technique:

technique TestTechnique
{
    pass P0
    {
        VertexShader = NULL;
        PixelShader  = compile ps_2_0 TestPS();
    }
    pass P1
    {
        VertexShader = NULL;
        PixelShader  = compile ps_2_0 TestPSGreyscale();
    }
}


Here's the render surface rendering:

_device.Clear(Direct3D.ClearFlags.Target, Color.Black, 1.0f, 0);

_device.BeginScene();

_effect.Technique = "TestTechnique";
_effect.SetValue("WorldViewProj", worldViewProj);
_effect.SetValue("Time", (float)Kernel.Instance.Time.TotalTime);

int numPasses = _effect.Begin(0);
for (int pass = 0; pass < numPasses; pass++)
{
	_effect.BeginPass(pass);

	_device.SetTexture(0, _renderTexture);

	_graphics.DrawRectangle(new Point(0, 0), Kernel.Instance.Window.ClientSize);
	_graphics.Flush();

	_effect.EndPass();
}

_effect.End();

_device.EndScene();

_device.Present();


So what's the deal? Is blending the way to go or have I done something wrong? Thanks for your help. (_graphics is my own class for rendering sprites and such) [Edited by - GroZZleR on August 8, 2005 1:26:30 PM]
Advertisement
I think blending is a perfectly good solution for combining post-process effects. You can set the blending mode in the effect technique, so that your effect is properly combined with previous effect.

Alternative solution would be to use multitexturing. Use your rendertarget-texture in multiple texture stages and use the pixel shader to combine effects.

Btw, how are you integrating post-process effects to your rendering pipeline? I'm interested in this because I'm also in the process of writing a 2D engine.
Come to think of it, I don't think blending is going to work.

If it's not using the pixels from the previous pass, it's not going to look correct. For example, if I blur on the first pass and greyscale on the second, it's going to be sampling the wrong colours.

Any other ideas?

(I'm not sure how it's going to fit into the pipeline just yet, I'm still working on figuring out all the kinks.)
You could use a chain of render target textures, and pass the results forward through them until you reach the backbuffer.

It is worth noting that the less steps you take, the faster your program will be - so try to combine the shaders into as few steps as possible.

Niko Suni

Hate to jump in here, but is there any built-in support for these "chains" or would one have to program this specifically?
Sirob Yes.» - status: Work-O-Rama.
Quote:Original post by sirob
Hate to jump in here, but is there any built-in support for these "chains" or would one have to program this specifically?

No, there's no built-in render target chains, you'll have to do it yourself. Unless I'm missing something here...
By "chains" I meant something like this:


  1. Render effect 1 to rendertarget 1.
  2. Render effect 2 to rendertarget 2 using rendertarget 1 as input.
  3. Render effect 3 to rendertarget 3 using rendertarget 2 as input.
  4. Repeat above steps as necessary.
  5. Render the final effect to the backbuffer.


There isn't a limit to how many render target textures you can create, other than the available video memory.

Niko Suni

This topic is closed to new replies.

Advertisement