I'm trying to render a 3D scene to an off-screen image, so that I can then do some post-processing to apply a Gaussian blur to the final image before rendering it to the screen.
I've posted the body of the main Draw method below.
The DoDraw method contains all the detailed scene rendering.
1. If I take out the wrapper and just use the code in DoDraw, everything works fine.
2. But with the Draw wrapper code in place, the FIRST frame renders OK (including blurring), but all subsequent frames just give a black image - the first substantive line in DoDraw is:
TheGraphicsDevice.Clear(Color.Black);
The rest of the drawing code uses
DrawUserPrimitives
to do the rendering.
3. First time through, the ColorData I get from the RawImage texture is fine - after that, R=G=B=0 / A=255 for all pixels. N.B. Those lines are only there to try to find out what's going on - I wouldn't normally be using GetData!
4. If I change the RawImage to
Texture2D RawImage = (Texture2D)SomeTextureLoadedFromContent;
then everything works fine - blurring works as intended - all frames display fine.
5. The RawRenderTarget is initialised in the LoadContent method using
ThePresentationParameters = TheGraphicsDevice.PresentationParameters;
RawRenderTarget = new RenderTarget2D(TheGraphicsDevice, ThePresentationParameters.BackBufferWidth, ThePresentationParameters.BackBufferHeight, false, ThePresentationParameters.BackBufferFormat, ThePresentationParameters.DepthStencilFormat);
The implication is that somehow DoDraw is failing to render properly to the RawRenderTarget after the first frame. I've tried various options for how the RawRenderTarget is initialised (including initialising a new object for every frame!!), playing with depth buffer formats, trying different clear options etc., but nothing I do seems to make any difference.
Can anyone suggest anything else I can try or anything I might be missing?
This is XNA 4.0 using VS 2010 in debug mode on a Win7 x64 machine.
If any further info on anything would help, please let me know.
Thanks,
NuttingCDEF
************************************************
TheGraphicsDevice.SetRenderTarget(RawRenderTarget);
DoDraw(TheGameTime);
Texture2D RawImage = (Texture2D)RawRenderTarget;
TheGraphicsDevice.SetRenderTarget(BlurredRenderTarget1);
Color[] ColorData = new Color[1024 * 768];
RawImage.GetData<Color>(ColorData);
Rectangle SrcRect = new Rectangle(0, 0, RawImage.Width, RawImage.Height);
GaussianEffect.CurrentTechnique = GaussianEffect.Techniques["GaussianBlur"];
GaussianEffect.Parameters["weights"].SetValue(TheKernel);
Rectangle DestRect1 = new Rectangle(0, 0, BlurredRenderTarget1.Width, BlurredRenderTarget1.Height);
GaussianEffect.Parameters["colorMapTexture"].SetValue(RawImage);
GaussianEffect.Parameters["offsets"].SetValue(TheOffsetsH);
TheSpriteBatch.Begin(0, BlendState.Opaque, null, null, null, GaussianEffect);
TheSpriteBatch.Draw(RawImage, DestRect1, Color.White);
TheSpriteBatch.End();
Texture2D BlurredTexture1 = (Texture2D)BlurredRenderTarget1;
Rectangle DestRect2 = new Rectangle(0, 0, BlurredRenderTarget2.Width, BlurredRenderTarget2.Height);
TheGraphicsDevice.SetRenderTarget(BlurredRenderTarget2);
GaussianEffect.Parameters["colorMapTexture"].SetValue(BlurredRenderTarget1);
GaussianEffect.Parameters["offsets"].SetValue(TheOffsetsV);
TheSpriteBatch.Begin(0, BlendState.Opaque, null, null, null, GaussianEffect);
TheSpriteBatch.Draw(BlurredTexture1, DestRect2, Color.White);
TheSpriteBatch.End();
Texture2D BlurredTexture2 = (Texture2D)BlurredRenderTarget2;
TheGraphicsDevice.SetRenderTarget(null);
Rectangle DisplayRect = new Rectangle(0, 0, ThePresentationParameters.BackBufferWidth, ThePresentationParameters.BackBufferWidth);
TheSpriteBatch.Begin();
TheSpriteBatch.Draw(BlurredTexture2, DisplayRect, Color.White);
TheSpriteBatch.End();