Question

I'm using Managed Direct X 2.0 with C# and I'm attempting to apply a fragment shader to a texture built by rendering the screen to a texture using the RenderToSurface helper class.

The code I'm using to do this is:

RtsHelper.BeginScene(RenderSurface);
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0);
//pre-render shader setup
preProc.Begin(FX.None);
    preProc.BeginPass(0);
        //mesh drawing
        mesh.DrawSubset(j);
        preProc.CommitChanges();
    preProc.EndPass();
preProc.End();
RtsHelper.EndScene(Filter.None);

which renders to my Surface, RenderSurface, which is attached to a Texture object called RenderTexture

Then I call the following code to render the surface to the screen, applying a second shader "PostProc" to the rendered texture. This shader combines color values on a per pixel basis and transforms the scene to grayscale. I'm following the tutorial here: http://rbwhitaker.wikidot.com/post-processing-effects

device.BeginScene();
{
    using (Sprite sprite = new Sprite(device))
    {
        sprite.Begin(SpriteFlags.DoNotSaveState);
            postProc.Begin(FX.None);
                postProc.BeginPass(0);
                    sprite.Draw(RenderTexture, new Rectangle(0, 0, WINDOWWIDTH, WINDOWHEIGHT), new Vector3(0, 0, 0), new Vector3(0, 0, 0), Color.White);
                    postProc.CommitChanges();
                postProc.EndPass();
            postProc.End();
        sprite.End();
    }

}
device.EndScene();
device.Present();
this.Invalidate();

However all I see is the original rendered scene, as rendered to the texture, but unmodified by the second shader.

FX file is below in case it's important.

//------------------------------ TEXTURE PROPERTIES ----------------------------
// This is the texture that Sprite will try to set before drawing
texture ScreenTexture;

// Our sampler for the texture, which is just going to be pretty simple
sampler TextureSampler = sampler_state
    {
        Texture = <ScreenTexture>;
    };

//------------------------ PIXEL SHADER ----------------------------------------
// This pixel shader will simply look up the color of the texture at the
// requested point, and turns it into a shade of gray
float4 PixelShaderFunction(float2 TextureCoordinate : TEXCOORD0) : COLOR0
{
    float4 color = tex2D(TextureSampler, TextureCoordinate);

    float value = (color.r + color.g + color.b) / 3; 
    color.r = value;
    color.g = value;
    color.b = value;

    return color;
}

//-------------------------- TECHNIQUES ----------------------------------------
// This technique is pretty simple - only one pass, and only a pixel shader
technique BlackAndWhite
{
    pass Pass1
    {
        PixelShader = compile ps_1_1 PixelShaderFunction();
    }
}
Was it helpful?

Solution

Fixed it. Was using the wrong flags for the post processor shader initialization

was:

sprite.Begin(SpriteFlags.DoNotSaveState);
    postProc.Begin(FX.None);

should be:

sprite.Begin(SpriteFlags.DoNotSaveState);
    postProc.Begin(FX.DoNotSaveState);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top