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

RenderTarget acting up, help please

Last post 11/15/2009 4:18 PM by Deej. 4 replies.
  • 11/12/2009 3:50 PM

    RenderTarget acting up, help please

    Recently, I implemented a Label class in my game.  It's a floating quad displaying a texture that only has text on it.  To accomplish this, I draw the text to an appropriately sized rendertarget and get the image back.  Then pass it to the quad for display when drawn.

    The problem is that every once in a while (I really can detect no trend), one of the labels will come up  with a blue/purple blank texture instead of the text.  This color corresponds to one that I have seen when clearing rendertargets incorrectly in the past.

    The code to generate the texture is as follows:

          q = new Quad(graphicsDevice); 
          imageSize = FontXLarge.MeasureString(Message); 
     
          RenderTarget2D rt = new RenderTarget2D(graphicsDevice, (int)imageSize.X, (int)imageSize.Y, 1, graphicsDevice.PresentationParameters.BackBufferFormat); 
     
          graphicsDevice.SetRenderTarget(0, rt); 
          graphicsDevice.Clear(Color.TransparentWhite); 
           
          spriteBatch.Begin(); 
          spriteBatch.DrawString(FontXLarge, Message, Vector2.Zero, Color.White); 
          spriteBatch.End(); 
          graphicsDevice.SetRenderTarget(0, null); 
     
          q.Texture = rt.GetTexture(); 

    Am I doing something wrong here? What could cause the texture to come out blank (but only some of the time)?
    I have a feeling that somewhere I'm missing setting up the render state correctly, but I am not knowledgeable enough to know what that could be.

    Any tips would be much appreciated.
  • 11/12/2009 4:09 PM In reply to

    Re: RenderTarget acting up, help please

    Is that your actual code?  If so, you should really only be creating your render target once (just make it big enough for the biggest label you think you might need) and re-use it (since creating them is expensive).  I don't know if that will help with your problem though.

    Anyway, if you're seeing the label come up with a blue/purple texture, that sounds like "uninitialized data" in your render target.  i.e. you haven't drawn to it since the last time you set it with SetRenderTarget (SetRenderTarget will clear the contents, unless you use RenderTargetUsage.PreserveContents, but that only works on the PC).

    I would check your logic to see if there is any way the quad could draw with a rendertarget texture that was passed to SetRenderTarget but not drawn on (or which has never been set/drawn on at all).

  • 11/12/2009 6:53 PM In reply to

    Re: RenderTarget acting up, help please

    mtnPhil:
    Is that your actual code?  If so, you should really only be creating your render target once (just make it big enough for the biggest label you think you might need) and re-use it (since creating them is expensive).  I don't know if that will help with your problem though.

    Yes, it is the code I'm using.  I haven't optimized this part yet (since I just created it and wanted to make sure it would work) but I appreciate the tip about render targets.
    The solution to that would be to set it as a static var that all Labels could use when they need to generate their texture?  Then I would want to draw the text to the upper left corner each time.  After that though, I would have to get the slice of it that actually has the text on it, right? So the part in the upper left of size [imageSize].  How would I go about acquiring that slice as a texture of appropriate size?

    mtnPhil:

    Anyway, if you're seeing the label come up with a blue/purple texture, that sounds like "uninitialized data" in your render target.  i.e. you haven't drawn to it since the last time you set it with SetRenderTarget (SetRenderTarget will clear the contents, unless you use RenderTargetUsage.PreserveContents, but that only works on the PC).

    I would check your logic to see if there is any way the quad could draw with a rendertarget texture that was passed to SetRenderTarget but not drawn on (or which has never been set/drawn on at all).

    I do not believe that there is any way I could be trying to draw a texture extracted from a renderTarget that has not been drawn on.  You are looking at the only place that quad owned by the label gets a texture (which occurs on the label's creation), and if the quad has no texture set, the texture variable is null and skips its draw method.

    Any other ideas?
  • 11/12/2009 8:13 PM In reply to

    Re: RenderTarget acting up, help please

    Also, I just went out and tested it some on my 360 (with no alterations to the project) and I cannot manage to replicate the issue.
    Maybe that bit of info will help someone who knows more about how the underlying graphics work than I do.
  • 11/15/2009 4:18 PM In reply to
    • (920)
    • premium membership
    • Posts 394

    Re: RenderTarget acting up, help please

    Hi,

    I can't remember the solution to your issue, but it's definitely been resolved on here before. Search the forums a little harder... IIRC, it's got something to do with the construction parameters of your rendertarget, or a setting on the graphics device about the format of render targets.

    I know that's not very much use, but the answer is definitely on these forums somewhere :)
    Managing Director, Binary Tweed Ltd.

    binarytweed.com New games that're a bit like old games, but better.

    Clover: A Curious Tale - Out now £5.99/$9.99

    Free Blitz 1UP games in return for testing!
Page 1 of 1 (5 items) Previous Next