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

Simultaneous rendering

Last post 08/11/2009 9:09 by domko. 12 replies.
  • 06/11/2009 15:32

    Simultaneous rendering

    Hi,

    Is it theoretically possible to create a thread that would render some text and/or textures to a render target simultaneously with the main thread that renders everything else? Could it cause problems with the graphics adapter? For instance, when one thread clears the graphics adapter with a solid color while the other thread still renders models to the screen etc at the same time? I've been trying to figure out a way for dynamic background loading and content creation, but so far background threads are the only thing that comes to my mind..

    Thanks!
  • 06/11/2009 16:18 In reply to

    Re: Simultaneous rendering

    Answer
    Reply Quote
    You cannot render more than one thing using the same GPU at the same time.
    XNA Framework Developer - blog - homepage
  • 06/11/2009 19:04 In reply to

    Re: Simultaneous rendering

    Thank you for your response.

    That's exactly what I thought.. Is it possible to synchronize the threads so that when one thread renders, the other one updates and vice versa? If one thread is done updating before the other thread has finished rendering it could wait for the other one to finish.. Is that a good solution or is there a better one?
  • 06/11/2009 19:51 In reply to

    Re: Simultaneous rendering

    domko:
    Is it possible to synchronize the threads so that when one thread renders, the other one updates and vice versa?


    Sure, it's possible.  Threaded programming is far from easy, though!
    XNA Framework Developer - blog - homepage
  • 06/11/2009 20:03 In reply to

    Re: Simultaneous rendering

    Haha! I know.. I've been playing with that for a while... I'm just afraid that the app would seem unresponsive if a main thread waits for the background thread to finish rendering on a render target.. Anyway thanks for the reply.
  • 06/11/2009 20:38 In reply to

    Re: Simultaneous rendering

    Shawn Hargreaves:
    You cannot render more than one thing using the same GPU at the same time.


    In DirectX 11, with DirectX 11 hardware, it is theoretically possible. In fact, NVIDIA has a GPU scheduler as part of their CUDA framework. Whether it's actually implemented and works all the way through the pipeline (from hardware, to kernel device, to Direct3D runtime, to application(s)) is not certain, though.

    GPUs are becoming more and more like regular multi-processing systems, though, with their own MMUs, thread schedulers, and whatnot.
    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 06/11/2009 22:17 In reply to

    Re: Simultaneous rendering

    jwatte:
    GPUs are becoming more and more like regular multi-processing systems, though, with their own MMUs, thread schedulers, and whatnot.

    And it can't happen soon enough!

    To the OP: If all you want to do is draw some text or an animation while the game is loading content in the background, you can use this. However, as Shawn said this won't work if you need the graphics device on both threads. If that's the case, you either need to change your design so you don't the GPU, or as jwatte said, wait until DX11. :(
    "No programmer can pick up a TV remote without thinking what it would take to add a stun gun. [...] Their motto is 'if it ain't broke, it doesn't have enough features yet'" - Scott Adams, The Dilbert Principle

    The signature that was too big for the 512 char limit
  • 07/11/2009 16:38 In reply to

    Re: Simultaneous rendering

    I'll try to figure something out. Actually I need a background thread for dynamic texture creation during the game. The original idea was to render textures, text or whatever else I can think of to a render target and then get a texture form it and use it in the game. I'll try to work something out..
  • 07/11/2009 17:45 In reply to

    Re: Simultaneous rendering

    Answer
    Reply Quote
    If you want to use the graphics device in a separate thread, do this:

    1) Create an AutoResetEvent
    2) Override Game.BeginDraw and call Event.WaitOne() before calling base.BeginDraw()
    3) Override Game.EndDraw and call Event.Set() after calling base.EndDraw()
    4) Make sure to handle the error case in base.BeginDraw()

    Now, when you want to use the GraphicsDevice from another thread, do this:

    1) Call Event.WaitOne()
    2) Set your render target
    3) Draw
    4) Set render target back to null (after updating whatever texture you need)
    5) Call Event.Set()
    6) Make sure to wrap all this code in try/finally so that Event.Set() *always* gets called

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 07/11/2009 22:02 In reply to

    Re: Simultaneous rendering

    Thanks! It works! Just having a few problems with the render target.. It changes my projection and view matrices for some reason. The camera ends up looking in the sky every time rendering on a render target gets called..
  • 08/11/2009 8:32 In reply to

    Re: Simultaneous rendering

    Solved it. Forgot to reset the matrices. It was too late for proper brain function :D

    Just curious in terms of performance. How complex can my code be before I start seeing some performance issues in the game? So far I only have text rendered on the render target and it is running smoothly, but perspicacious as I am, I tried to put the background thread to sleep for 50 milliseconds. The frame rate has dropped a bit during the loading as expected.
  • 08/11/2009 8:52 In reply to

    Re: Simultaneous rendering

    That isn't a question with a simple answer.  Look at Shawn's blogs on performance for help.  But the short of it is that it depends on what you are trying to do and how many times you are trying to do it.

    A sleep of 50 ms doesn't surprise me that your game is slowing down if your background thread causes your main thread to block for any reason.  In standard 60 frames per second, you only have roughly 16 ms to get everything done.
  • 08/11/2009 9:09 In reply to

    Re: Simultaneous rendering

    I thought so. The main thread is waiting for the background thread to finish rendering because they can't both use the graphics device at the same time.. Usually the main thread doesn't have to wait at all because background thread starts rendering immediately after the main thread has finished rendering so it has enough time to execute before the next frame. I'll just try to keep the code simple..
Page 1 of 1 (13 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG