A simple test
Load the Silverlight 5 port of the XNA bloom sample (which you can find in c:/Program Files (x86)/Microsoft SDKs/Silverlight/v5.0/Toolkit/Sep11/Source/Sample source code.zip/Xna after installing the Silverlight toolkit). Run it, and observe the shiny tank.
Now open up MainPage.xaml.cs, find the OnDraw method, and add this line:
int test = SettingsCombo.SelectedIndex;
Run the sample again:
CrossAppDomainMarshaledException was unhandled
System.UnauthorizedAccessException: Invalid cross-thread access.
at System.Windows.DependencyObject.GetValueInternal(DependencyProperty dp)
at System.Windows.FrameworkElement.GetValueInternal(DependencyProperty dp)
at Bloom.MainPage.OnDraw(Object sender, DrawEventArgs e)
What’s going on?
The traditional XNA Game class is not multithreaded. Sure, you can create threads of your own if you want, but parallel programming is strictly opt-in, so if you don’t do anything special to make it happen, you won’t have to worry about the insanity that is threaded programming.
Silverlight used to work the same way. It has a single UI thread which is responsible for all user code, event handlers, and XAML rendering. But Silverlight 5 adds a new composition thread, which takes care of GPU accelerated rendering (although software rendering still runs on the UI thread). The advantage of this architecture is that, even if you block the UI thread running some lengthy operation, the composition thread can independently continue to play back XAML animations, thus providing a smoother UI with less glitching. Of course, the downside is that threaded programming is REALLY HARD, but since all your code still runs on the UI thread, you don’t generally have to worry about this. Those clever coding gnomes on the Silverlight team have taken care of the necessary synchronization so the composition thread can do its thing without customer code needing to know or care that things are now running in parallel behind the scenes.
Enter the 3D DrawingSurface…
Unlike the rest of Silverlight, which uses a retained mode XAML rendering architecture, DrawingSurface provides an immediate mode 3D API. Because this rendering is GPU accelerated, the DrawingSurface.Draw event must occur, not on the UI thread along with the rest of your code, but directly on the composition thread.
Here be dragons! Even though you never created any threads of your own, the simple act of hooking the Draw event means your 3D rendering code can run in parallel with regular UI code. Thus you find yourself inadvertently deep in the land of locks, interlocks, deadlocks, race conditions, and suchlike fun stuff 🙂
What does this mean for me?
If you are making a game that uses only 3D rendering without any XAML UI, you can put all your code (both Update and Draw) inside the DrawingSurface.Draw event. Regardless of whether you choose variable or fixed timestep logic, this means the UI thread will be entirely unused, so there is no parallelism to worry about and everything will Just Work™ the way you expect.
If you combine 3D rendering with XAML UI, you must figure out how to synchronize access to any data that is shared between the two. In the bloom sample (which uses XAML UI to alter bloom settings) you will notice the rendering code does not read its settings directly from the Silverlight controls. Instead, MainPage.xaml hooks the change events from these controls (which run on the UI thread) and copies their settings into fields which can be safely accessed by the composition thread. If the shared data structure was more complex, we would need a lock to coordinate this access.