XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

[SOLVED] RenderTarget and BackBuffer produce different results

Last post 1/26/2009 2:02 PM by David Flook. 5 replies.
  • 1/24/2009 11:01 PM

    [SOLVED] RenderTarget and BackBuffer produce different results

    I wrote a very simple program that draws two rows of white triangles (png) on black.

    First, I draw a row of overlapping triangles to RenderTarget and copy the result to a Texture.
    Secondly, I draw the resulting RenderTarget Texture along with an identical row of triangles to the BackBuffer.

    The RenderTarget row is on top while the BackBuffer row is below.

    You can see how the triangles which were drawn directly to the BackBuffer overlap in a smooth continuous solid texture as expected.
    The triangles drawn to RenderTarget appear to have a thin black stroke around each triangle.  Although the shapes appear to be stroked, further examination revealed that it's actually empty space.
    If I were to draw a texture below that row of triangles, the texture would show through those stroked areas.

    Image

    You can download the source code here.
  • 1/24/2009 11:25 PM In reply to

    Re: Demonstration of how RenderTarget and BackBuffer produce different results

    That looks correct to me. What you don't see (when you render directly to the screen) is what is in the alpha channel. The alpha channel of your render target is probably causing this to look this way.
    If you mathematically apply the blend equation for the alpha values themselves (the alpha channel is blended too) then you should get exactly what you see as the alpha channel. You are then blending the result onto the screen, which uses that alpha channel.

    What I'm trying to say, is that the RGB of the triangles in your texture *will* be exactly the same as the RGB of the triangles you are drawing to the screen backbuffer. The alpha will be the same too (but different than the RGB), it's just you only see the RGB for the screen, and by using blending, you are seeing RGB * alpha for the texture.

    Xen: Graphics API for XNA
    www.codeplex.com/xen
  • 1/25/2009 12:06 AM In reply to

    Re: Demonstration of how RenderTarget and BackBuffer produce different results

    That's genius thank you.

    So to make the two rows of triangles look the same I need to;

    1) Draw the first row to RenderTarget using SpriteBlendMode.Alpha
    2) Draw the resulting Texture to BackBuffer using SpriteBlendMode.None
    3) Draw the second row of triangles using SpriteBlendMode.Alpha

    Does that make sense?

  • 1/25/2009 10:52 AM In reply to

    Re: Demonstration of how RenderTarget and BackBuffer produce different results

    A pair of questions:

    1) Are you using different multisamples type in any?, look at it as in Xbox and PC are diferent the default one, and using the PreferMultisample=true gives you multisample in backbuffer by default but NOT in rendertargets. Multisample can gives you different rendering results for the same image.

    2) That render is from Xbox or PC?, I say this because I have a render problem in my PC, you can see a slight displacement in the diagonal up-left to down-right, as the render is made over 2 triangles, the UV coordinates of the rendertarget are a bit displaced, but this problem totally dissapears on Xbox. Maybe is a driver glitch for XNA rendering for some PC video cards.

    3) The stretching can produce some effects like those, changing the image a bit. While rendering to the backbuffer there is no stretching, when rendering to rendertarget can be stretching if you draw the rendertarget to the backbuffer using other scale than 1, so take care of using rendertargets like typical 512x512, 1024x512 as they need stretching. to be sure use the Draw funcitons with Vector2 not the Rectangle one.

    For that, try to render that image on Xbox with no multisample and stretching in rendertarget in any case, maybe the problem is solved.
  • 1/25/2009 3:51 PM In reply to

    Re: Demonstration of how RenderTarget and BackBuffer produce different results

    1) I'm not specifically changing the Mutisample type for RenderTarget
    2) Xbox
    3) Both my RenderTarget and BackBuffer are 1280 x 720

    My goal was to be able to feed the RenderTarget surface back into RenderTarget to have a persistent texture I can keep adding information to. 
    Kind of like having a brick wall texture and being able to add permanent bullet holes to that texture without redrawing all the bullet holes every frame.
    Unfortunately, it seems that looping the RenderTarget surface like that degrades the texture over time.  After a few hundred cycles, the texture becomes washed out.
  • 1/26/2009 2:02 PM In reply to

    Re: Demonstration of how RenderTarget and BackBuffer produce different results

    David Flook:
    Unfortunately, it seems that looping the RenderTarget surface like that degrades the texture over time.  After a few hundred cycles, the texture becomes washed out.


    I found a rather simple solution to this.

    Rather than taking the texture generated by GetTexture() and drawing it back into RenderTarget, I simply set RenderTargetUsage to PreserveContents.
    This allows me to continually add information to the Texture without blending the entire texture from RenderTarget to BackBuffer, back and forth continuously.
    I think this constant alpha blending is what was causing the graphical artifacts I was observing. 
    I don't know enough about it to know for sure but this solution combined with the knowledge of double alpha blending mentioned further back looks to do the trick.
Page 1 of 1 (6 items) Previous Next