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

Threaded loading with graphical content

Last post 03-24-2008 7:49 PM by Nick Gravelyn. 3 replies.
  • 03-24-2008 4:31 PM

    Threaded loading with graphical content

    Hey,

    I'm running into a problem when loading my game levels in a separate thread. The problem is, part of the level loading generates cubemaps (requiring the graphics device), but my gamescreen rendering code also requires the graphics device. This causes concurrency issues (for example, if the cubemap code happens at the same time as the rendering code, an exception will be thrown). This isn't safe, it sounds like per the DirectX guidelines that all rendering code should be in a single thread. Is there a good workaround for this, or a better way to handle this?

    Thanks for your help!

    -Bryan

  • 03-24-2008 5:03 PM In reply to

    Re: Threaded loading with graphical content

    You can use the 'lock' keyword to have your loading thread get an exclusive lock on the graphics device. This will cause blocking in the other thread where your rendering takes place, but as long as it is fast it shouldn't matter. Just get whatever you need done with the graphics device inside the lock area and then do the rest afterwards so that your other thread can keep moving.

    Here's the MSDN on 'lock': http://msdn2.microsoft.com/en-us/library/c5kehkcz(VS.71).aspx.
  • 03-24-2008 7:47 PM In reply to

    Re: Threaded loading with graphical content

    Hey Nick,

    Thanks for the reply! I was thinking along these lines but was concerned with the overhead of locking. As I understand there is overhead when obtaining a mutex (even if there is nothing blocking), and I didn't like the idea of that overhead in my render code. I guess I could give it a shot though and see if affects performance. Maybe the overhead is very small (or i could be totally wrong about their being overhead) compared to the work done by the rendering-

  • 03-24-2008 7:49 PM In reply to

    Re: Threaded loading with graphical content

    I've used some locks and never really seen a problem. As long as your code inside the lock is as quick as can be, you should be fine. You might get a hiccup, but if it's a loading screen it should be fine. And if this is for streaming data, a lot of games usually have some small hiccups once in a while. So the key still is just make sure that you do as little as needed in the lock and do the rest of the work outside of the lock.
Page 1 of 1 (4 items) Previous Next