The speed of Managed DirectX

It seems that at least once a week i'm answering questions directly regarding the
performance of managed code, and Managed DirectX in particular.  One of the more
common questions i hear is some paraphrase of "Is it as fast as unmanaged code?".

Obviously in a general sense it isn't.  Regardless of the quality of the Managed
DirectX API, the fact remains that it still has to run through the same DirectX API
that the unmanaged code does.  There is naturally going to be a slight overhead
for this, but does it have a large negative impact on the majority of applications? 
Of course not.  No one is suggesting that one of the top of the line polygon
pushing game coming out today (say, Half Life 2 or Doom 3) should be written in Managed
DirectX, but that doesn't mean that there isn't a whole slew of games that could be. 
I'll get more to that later.

I'm also asked quite a bit things along the lines of "Why is it so slow?"  Sometimes
the person hasn't even ran a managed application, they just assume it has to be. 
Other times, they may have run numerous various 'scenarios' comparing against the
unmanaged code (including running the SDK samples) and have found that in some instances
there is large differences.

Like I've mentioned earlier in this blog, all of the samples in the SDK use the dreaded
'DoEvents' loop, which can artificially slow down the application due to allocations
and the subsequent large amounts of colllections.  The fact that most of the
samples run with similar frame rates as the unmanaged API is a testament to the speed
of the API to begin with.

The reality is that for many of the developers out there today, they simply don't
know how to write well performing managed code.  This isn't through any shortcoming
of the developer, but rather the newness of the API, combined with not enough documentation
on performance, and how to get the best out of the CLR. Luckily, this is changing,
for example, see Rico Mariani's blog (or
his old blog). For the most part,
we are all newbies in this area, but things will only get better.

It's not at all dissimilar to the change from assembler to C++ code for games. 
It all comes down to a simple question.  Do the benefits outweigh the negatives? 
Are you willing to sacrifice a small bit of performance for the easier development
of managed code?  The quicker time to market?  The greater security? 
The easier debugging? 

Like i said earlier, there are certain games today that aren't good fits for having
the main engine written in managed code, but there are plenty of titles that are. 
The top 10 selling PC games a few weeks ago included two versions of the Sims, Zoo
Tycoon (+ expansion), Age of Mythology, Backyard Basketball 2004, Uru: Ages beyond
myst, any of which could have been written in managed code.

Anyone who's take the time to write some code in one of the managed languages normally
realizes the benefits pretty quickly.

Comments (17)

  1. pappasmurf says:

    Good point, although I question whether Age of Mythology could have been written in managed code. I believe for an application with its complicated object structure the lack of determinism in the GC will be a hindrance to its frame rate. Its the type of game where allocations are occurring in real-time during gameplay, and that cannot be avoided. Granted one might be able to create a cache or pool of such objects ahead of time, but the fact is that the number of entity types in the game is so large that it would require a pretty sophisticated guessing algorithm to determine exactly which type of entity pool to create.

    Is there any truth to the rumor that a ref counting algorithm is in the works for the GC?

  2. malcolm says:

    I got a problem here.
    I was trying out DX9 in C# and whilst reading your book i realised there was little mention of getting 2d graphics out i mean like .jpgs?
    I want to make a DX9 title screen (those seen in games and with all those fanciful options) but all i wan is to render a .jpg(background out) and with .x meshes aas text how ?.

  3. Tom Miller says:

    malcom: You can render 2d graphics just fine (see chapter 16 in the book). Use the sprite class and render your texture anywhere onscreen you want.

    My next book actually shows how to design UI for games using the Direct3D interfaces..

  4. Has there been any research into the difference of performance between managed and unmanaged DirectX? I completely understand that there will be. I would just like to know if anyone has anything between C++ and C#?

  5. Tom,
    Thought I’d share some performance issues am having, am slowly finding that building games is more of dealing with perf issues rather than the game itself ;-)…

    Along with ur book (am in ch.7), I’ve been building my own simplified car game and i’ve been having some performance issues with the CPU going up to 100% constantly. The X file is only ~ 50kb with textures that I compressed to the max… Then I decreased the Projection Matrix "far range" (less objects r visible) which made a big difference CPU 60-70% as well as removing some lights (from 5 directional to 2 point lights)… tried to add some fog but that slowed it again… Worth mentioning is that my pc is relatively old P3 500Mhz with 320 MB RAM and a ATI Radeon 7000 32 MB VRAM.
    Any idea what would be the min. PC reqs. to run MDX with decent performance? (if that’s the main reason for this issue)
    When is the next book release? Hope u show us techniques to divide the world into diff sections etc… ie performance enhancements…

  6. James Raine says:

    Looking forward to that UI section.
    I do my UI in GDI (backbuffer.GetGraphics()…) at the moment – slows down quite a bit with a complex menu, but I’m sure with a bit of intelligence in the painting code (everything is painted every frame right now) it will be fine.

    Having said that, i rarely drop below 30fps which for a menu screen is perfectly acceptable to me.

  7. Jules says:

    Well I am a lazy, and rather dim witted programmer. So I pretty soon gave up on trying to learn or do anything in C++, and took up Java as a readable language few years back. (previously taught Pascal)
    C# is nice and simple to learn, and with Tom’s book I am able to get into Managed DirectX. (This is the only way I am
    going to get into DirectX technologies.)

    So after reading half of Tom’s Book, I got impatient and got into developing an Tank game across an elevation grid.
    And yes, running on a PIII 800MHz, with a Radeon 9700 Pro, I am now discovering performane problems.

    I now know that my performance is bettter with
    a) Only ue a single Directional Light rtaher than several ! (as highlighted ion the book)
    b) Reducing the Far plane, to significantly reduce the polygons being rendered
    c) Using a TriangleList instead of Meshes for my Large Elevation grid
    d) Use of small and tiled (Wrapped) textures, rather than large and many textures

    All part of the DirectX learning experience, and I have a lot more to learn. None of these relate to the performance of MDX or C#.


  8. Brook says:

    I’m Also looking forward to your next book, Your first MDX book is great. Will your next book be entirely focused on d3d or will you have other sections dedicated to the other namespaces similiar to the first book? Any chance you could let us know what some of the topics might be, or perhaps a possible release date?


  9. Emi says:

    i’d really like a comparison between MDX and conventional DX. Developing a game from scratch in MDX will never give you any signs that DX is faster. The performance tips i read here are the same you would have to make with conventional DX.

  10. Sagar Indurkhya says:

    I was just wondering. If I want to do some time critical stuff, and I write it in unmanaged code,

    does that also compile to MSIL?

  11. Sagar Indurkhya says:

    I have been doing native DX in C++ for a while, and was wondering if it was appropriate to do mdx in Managed C++. Is it even possible?

  12. Tom Miller says:

    Yes you can use MDX from MC++.

  13. Colin o connor says:

    im creating a c# dx9 space game for my project in college, its to be handed up soon, but my problem is running it on other machines that dont have vs 2003. is there some way of compiling the c# code so that it will work on all machines that have dx9.

  14. David says:


    # re: The speed of Managed DirectX 3/27/2004 1:56 PM Tom Miller

    Yes you can use MDX from MC++.


    How? I can’t seem to find any documentation on this…

Skip to main content