Drilling Down: DX11 And The Multi-Threaded Game Engine
In spite of the fact that multi-threaded programming has been around for decades, mainstream programmers didn't start focusing on parallel programming until multi-core CPUs started coming along. Much general purpose code is straightforward as a single thread; extracting performance via parallel programming can be difficult and isn't always obvious. Even with talented programmers, Amdahl's Law is a bitch: your speed up from parallelization is limited by the percent of code that is necessarily sequential.
Currently, in game development, rendering is one of those "necessarily" sequential tasks. DirectX 10 isn't set up to appropriately handle multiple threads all throwing commands at the GPU. That doesn't mean parallelization of renderers can't happen, but it does limit speed up because costly synchronization techniques or management threads need to be implemented in order to make sure nothing steps out of line. All this limits the benefit of parallelization and discourages programmers from trying too hard. After all, it's a better idea to put more of your effort into areas where performance can be improved more significantly. (John Carmack put it really well once, but I can't remember the quote... and I'm doing too much benchmarking to go look for it now. :-P)
No matter what anyone does, some stuff in the renderer will need to be sequential. Programs, textures, and resources must be loaded up; geometry happens before pixel processing; draw calls intended to be executed while a certain state is active must have that state set first and not changed until completion. Even in such a massively parallel machine, order must be maintained for many things. But order doesn't always matter.
Making more things thread-safe through an extended device interface using multiple contexts and making a lot of synchronization overhead the responsibility of the API and/or graphics driver, Microsoft has enabled game developers to more easily and effortlessly thread not only their rendering code, but their game code as well. These things will also work on DX10 hardware running on a system with DX11, though some missing hardware optimizations will reduce the performance benefit. But the fundamental ability to write code differently will go a long way to getting programmers more used to and better at parallelization. Let's take a look at the tools available to accomplish this in DX11.
First up is free threaded asynchronous resource loading. That's a bit of a mouthful, but this feature gives developers the ability to upload programs, textures, state objects, and all resources in a thread-safe way and, if desired, concurrent with the rendering process. This doesn't mean that all this stuff will get pushed up in parallel with rendering, as the driver will manage what gets sent to the GPU and when based on priority, but it does mean the developer no longer has to think about synchronizing or manually prioritizing resource loading. Multiple threads can start loading whatever resources they need whenever they need them. The fact that this can also be done concurrently with rendering could improve performance for games that stream in data for massive open worlds in addition to enabling multi-threaded opportunities.
In order to enable this and other threading, the D3D device interface is now split into three separate interfaces: the Device, the Immediate Context, and the Deferred Context. Resource creation is done through the Device. The Immediate Context is the interface for setting device state, draw calls, and queries. There can only be one Device and one Immediate Context. The Deferred Context is another interface for state and draw calls, but many can exist in one program and can be used as the per-thread interface (Deferred Contexts themselves are thread unsafe though). Deferred Contexts and the free threaded resource creation through the device are where DX11 gets it multi-threaded benefit.
Multiple threads submit state and draw calls to their Deferred Context which complies a display list that is eventually executed by the Immediate Context. Games will still need a render thread, and this thread will use the Immediate Context to execute state and draw calls and to consume the display lists generated by Deferred Contexts. In this way, the ultimate destination of all state and draw calls is the Immediate Context, but fine grained synchronization is handled by the API and the display driver so that parallel threads can be better used to contribute to the rendering process. Some limitations on Deferred Contexts include the fact that they cannot query the device and they can't download or read back anything from the GPU. Deferred Contexts can, however, consume the display lists generated by other Deferred Contexts.
The end result of all this is that the future will be more parallel friendly. As two and four core CPUs become more and more popular and 8 and 16 (logical) core CPUs are on the horizon, we need all the help we can get when trying to extract performance from parallelism. This is a good move for DirectX and we hope it will help push game engines to more fully utilize more than two or even four cores when the time comes.
109 Comments
View All Comments
bobjones32 - Saturday, January 31, 2009 - link
Derek, Vista has been out for only 2 years. Take a look at XP's first two years and it was a phenomenal disaster comparatively. How people forget its far more significant performance and driver compatibility issues, and of course the Blaster worm, is totally beyond me.Just look at some of the nightmares: http://community.winsupersite.com/blogs/paul/archi...">http://community.winsupersite.com/blogs...8/06/30/...
Business uptake? Initial consumer uptake? Driver support? Gaming speed? Security? Compatibility? Vista's first two years have been incredibly successful compared to XP's first two years.
The difference? In 2001, sensationalist blogging making mountains out of molehills and purporting endless false perceptions without significant evidence wasn't nearly as popular. Apple's ads that only fed into peoples' ignorance only helped that false perception grow.
Zoomer - Monday, February 2, 2009 - link
Compare XP to 98SE.Now compare Vista to XP.
We have to take things in context.
CvP - Saturday, January 31, 2009 - link
TOTALLY AGREE.well said.
Goty - Saturday, January 31, 2009 - link
So the gist of this comment (Derek's) is that Vista doesn't suck, but you still say, "Vista sucks."I'm confused now.
Anyhow, I personally agree with the other people in the section that say Vista is a great operating system. I installed it in January of '07 and the only change I've made is to move from Business to Ultimate (thanks to the fact that I was using an ATI video card at the time and the fact that they didn't slack off on their driver development).
DerekWilson - Saturday, January 31, 2009 - link
Actually, that's an apt statement.Vista is currently something it was not initially. This made it "suck" in the hearts and minds of many people at the start, damaging the Windows brand and frustrating users. While, as I said, those issues are largely gone post SP1. This impacted Microsoft's ability to inspire confidence in the industry that everyone would eventually move to a platform that ran DX10, whether or not the OS eventually got to a stable place.
In spite of this, I use Vista: I have 6GB of RAM and I'm interested in DX10 for the sake of DX10 personally. I'm a bit of a technologist and I like things for the sake of the thing, really, but that doesn't mean that MS didn't drop the ball in their delivery of Vista.
just4U - Wednesday, February 4, 2009 - link
What made it suck in the hearts and minds of many people was the refusal to move away from what they allready knew. Changing your operating system has always brought alot of grumbling from the majority... but it's even more overstated with XP which has been around and in wide use far longer then previous Microsoft Operating systems.The beta form of Vista I found to be buggy as hell.. But ofcourse I was testing it on a 1800+ Tbird with 1G of ram. Later when I adopted it (before sp1) it was fine with very few problems. I'd even go as far as saying way way less then what XP had at launch.
CvP - Saturday, January 31, 2009 - link
and XP was much better *when* it was released?!until XPsp1, it was a piece of shit.
until XPsp2, it wasn't worth switching to XPsp2.
and all those lack of driver etc..it was not MS's fault.
poohbear - Saturday, January 31, 2009 - link
"calling it like it is"? i have vista64 premium and i much prefer it to win XP. I have the hardware to run it flawlessly ofcourse, and in that context it's anything but "sucks". would never go back to XP, so please vista has come a looooooong way and doesnt need superfluos opinions like this in 2009.mechBgon - Saturday, January 31, 2009 - link
Derek, if you take a look at Valve's monthly user survey, the ratio of Vista to WinXP users is now at about 1 to 2.1 and climbing by the month. That's not what I'd call "rejection of Vista." I think you've been reading The Inq too much :)As for your remark about Vista being a "WinME," I disagree. WinXP was the WinME of this era; it was basically Win2000 with a few bonus features, like WinME was a makeover of Win98SE. Once WinXP SP2 was released, *then* WinXP actually had some real merit over Win2000 in the security department, but that was late in the game.
Vista, however, is a paradigm shift, a watershed in Windows history. Stuff is not run at Admin level by default anymore, and never will be again. Pulling that off without breaking 80% of the existing software in the world was an amazing accomplishment that involved virtualizing the Registry and file system (a little-known facet of UAC, as it happens).
This is another misguided factoid:
"MS recognized that they had a problem and implemented and extended again and again downgrade options to Windows XP for customers who were unsatisfied with Vista."
Well heck, OEM Windows XP Pro comes with downgrade rights to run Win98 or WinME. It's not a backpedalling measure, nor a confession that WinME is better than WinXP. That's normal Microsoft policy so customers can license the current OS but use the previous version, temporarily or permanently, for whatever reason.
There will be downgrade rights with the appropriate versions of Windows 7, too.
suryad - Thursday, February 5, 2009 - link
I am not so sure you can just take Valve's survey which is just predominantly consisting of gamers as a good statistic. Thats just my 2 cents. An OS is not just meant for gaming...sure thats a good amount of Vista users in the survey but what about the rest of the people who are not gamers? I am pretty confident that most people who are on Vista and are on the Steam survey are/were sold by the appeal of DX 10 and next generation gaming and graphics and all that.