In|Framez® Papers

Living The Direct3D Forums

By Wessam Bahnassi



Welcome…

With the introduction of the MSDN Game Development forums and the launch of XNA Studio, I’ve noticed a big increase in the number of beginner users who want to program games using XNA and DirectX, so they post questions and issues in the DirectX forums. One observation is that most of these issues are caused by lack of knowledge about the whole development process.

After you take the time to read this, you should be able to handle a lot of your problems independently without wasting your time (nor ours) by posting common-sense questions.

Before I move into the Q and A section, I just want to take some space to mention one very important tool in your arsenal, and how it is used in the context of DirectX/Game programming. The debugger:
Use the debugger! The debugger allows you to see how your program is working line by line. Thanks to Visual Studio, it’s never been easier to debug an application. The following page on MSDN will get you up and running if you never used it before:
http://msdn2.microsoft.com/en-us/library/sc65sadd(VS.80).aspx

Basically, your debugger will allow you to ensure that your program is running as you expect. Now, when you hit a problem with DirectX it usually gives helpful information directly in the exception it throws (if you’re running Managed). But sometimes this information is just not enough. DirectX has an excellent feature called the Debug Runtime. This allows DirectX to report errors and warnings to you while running your program under the debugger. With this feature on, almost every DirectX function call failure is accompanied by a helpful output message that is displayed in Visual Studio’s Output Window. This message will tell you why DirectX thinks your call is invalid, so you can correct the situation and run your program again.

To setup this feature, open the DirectX Control Panel, under the Debug/Retail D3D Runtime group box, make sure that the Use Debug Version of Direct3D option is selected. On the left, crank up the Debug Output Level slider all the way towards More (I personally leave it just one tick before the end). Note that a similar setup can be done for other DirectX components as well.
Managed DirectX applications have one more step left before this finally works, which is to Enable Unmanaged Debugging for the project. Follow this link on MSDN for the exact steps:
http://msdn2.microsoft.com/en-us/library/tdw0c6sf(VS.80).aspx

Unmanaged applications would want to link against the debug version of the D3DX library also, which gives important information for all D3DX function calls. Just make sure you link against D3DX9D.lib instead of D3DX9.lib in your Debug project configuration.



Common Questions & Answers…

Ok, now to the fun part, how to deal with these common problems:
  1. My application acts strangely or crashes and I don’t know why…
    Run it under the debugger, see which line is causing the crash, and make sure it’s receiving the inputs you expect. Read the Output Window for interesting messages from DirectX (if it’s a DirectX exception for example). Unexpected program behavior can also be detected by careful stepping through your code and ensuring that every line is doing the expected functionality.
  2. My application is locking my computer, or causing it to reset, or just doing something really insane (and it shouldn’t)…
    A good question is “Does it happen with the SDK samples?”. If the SDK samples exhibit the same behavior, then it is most likely you have some problem in your system setup (e.g. driver bug, compatibility issues, or even hardware problems). Adjust your system until it runs in a stable manner then see if it solves your problem.
    If the problem does not show in the SDK samples, and you have absolutely no idea what is causing it, then you can start by slowly disabling functionality from your application until it gets back to normal behavior. Once you find the suspected code block, compare how it is written against its matching code block in the SDK samples. You should identify the difference and be able to correct it.
  3. I get crazy rendering artifacts or I’m seeing unexpected results…
    The question of the day would be “Does it happen on the Reference Rasterizer?”. Rendering artifacts can be caused either by your code, or by a driver bug. If the problem disappears when you run the Reference Rasterizer device, then you have most probably hit a driver bug, and you should try to google around to see if similar people have hit it and how are they working around it. Contacting the relevant device vendor is also a good suggestion as this brings the problem to their attention so they can fix it in future driver versions.
    However, if the persists even with RefRast, then it is a problem in your code. Review your inputs carefully (bad vertex buffer contents and out-of-range indices commonly cause such rendering garbage). Use PIX to verify that your device is in the expected state just before issuing the offending Draw command.
  4. I can’t run a shader, or create a certain texture format…etc.
    Different graphics cards have different capabilities. Before you use a certain capability, make sure that your card is capable of doing it. If you ignore this, you can get incorrect output that looks like a driver bug for example, while it is simply your card being incapable of doing what you’re asking it for. Shader compilation failure is a very common pitfall. The SDK ships with a small utility called DirectX Caps Viewer, which is able to show you exactly what your card supports. Description of every entry can be found in the documentation for the D3DCAPS9 structure:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/D3DCAPS9.asp
  5. I try to load an effect/mesh but it doesn’t work. What could I be doing wrong?
    This kind of very generic question shows up a lot, and it really confuses us. Because there are so many ways to make loading effects/meshes/textures/etc fail. With the knowledge of debugging and the DirectX Debug Runtime enabled, you should be able to find out why it is failing, or at least know where it is failing exactly, so your question can be more specific and therefore we can help you quickly.
  6. I draw but I get black screen. What’s wrong?
    As they say, there are many ways to code a black screen in 3D computer graphics. The problem is that there are so many things that need to be setup before being able to even visualize a single triangle. Two advices here, if you’re still beginning, try to start from a sample application that actually draws something on the screen, so at least you can tighten the circle of search around your code only. You can start by duplicating the functionality in the sample, then slowly diverging to your intended behavior. The second advice is simply PIX. This fantastic tool (I’d vote for it being the best tool in the games development so far) allows you to see how every D3D call in your program affects the device state and the displayed output. If your object is not showing on the screen, use PIX to step into the drawing function and assert the state of the device and the input information at that time. It’s really powerful, so don’t act blind about it!
  7. I’m building a 2D game. I use DDraw, and I’m having trouble.
    Don’t! Support for DirectDraw7 has been dropped a very long time ago. Although the name doesn’t directly imply it, Direct3D9 is able to do all what DirectDraw7 in a simpler, faster, and more featured fashion. DirectDraw7’s main feature was the color-keyed blitter which was not even able to do alpha-blending.
    Direct3D9 can be used to draw pure 2D-sprite based games just as perfectly, but with better features such as, filtered rotation and zooming, and alpha blending. Many people think that Direct3D9 restricts you to using power-of-2 textures for your drawing, which can be annoying and wasteful especially for backgrounds. This is just not true. Direct3D9 supports arbitrary surface sizes that can be drawn directly on the screen (and yes, they are supported on all hardware I’m aware of until know). Setting up a Direct3D9 device for 2D rendering takes less code than setting up DirectDraw7 device. You can try it!
    Finally, using Direct3D9 does not enforce your users to use high-end video cards. Your game can work on all nVidia cards since the TNT, and all ATIs since the Rage. Good enough?
  8. The Managed DirectX documentation is really short.
    Unfortunately yes, it is. However, don’t be afraid of reading the C++ DirectX SDK documentation. It is complete, and includes details that are generic and are shared between both the managed and unmanaged versions. In fact, many MDX methods are direct reflections of original C++ DirectX functions. So you can always read more information on these methods by referring to them simply by name. Even if you don’t understand a bird’s s**t of C++, you can still benefit a lot by just reading the Programmer’s Guide sections of the unmanaged documentation.
  9. What is a shader? What is a texture? What is depth buffering? What is…etc.
    Many of these computer graphics terms are explained in the DirectX SDK’s documentation (the C++ ones, under the Programming Guide section). If that is not enough for you, simply look it up in Wikipedia.org. There, you will get the full story and usually more links for further reading.
  10. I want to learn DirectX. How?
    Books! Grab yourself a book that you like and work it out. Make sure you pick the right one based on what programming language you use. There is also a bazillion of tutorials on the web, but I personally prefer the learning pace of a book. It’s totally up to your personal preference. The question below might be of relevance to you also…
  11. I don’t understand the SDK sample code or tutorials on the web.
    There can be several reasons. One, your knowledge in the programming language is inadequate. This is true and it’s surprising how people expect to understand a program without knowing the language in the first place. If you don’t know English well, can you understand an English story book? Got the point?
    Make sure you spend some time solidifying your programming language first, then give those pesky samples another try.
    Another reason why a sample can be cryptic is that the concept it’s building on is unknown to you. There is no point trying to understand a Skinned Character sample if you don’t know how animation, matrices, and weighted blending works. For this, revert to more basic references like Wikipedia.org or some computer graphics text.
    Remember that the SDK samples are built to show you how to use DirectX, not to teach you computer graphics concepts.
  12. Why MS is doing this? Rants about bad decisions taken by Microsoft regarding DirectX will not lead to anything. If you are really annoyed by some decision and you want to change it then be constructive. Explain your exact situation/use-case first, and tell us why the latest decisions are screwing up your work. We will then either help you make the right move to benefit from the decision instead of being crippled by it, or if it was a real-world scenario that was not taken into account, then it can be used to improve the decision in later releases.




Things that would definately help…

So, before I close this happy discussion, I want to provide you with a check-list that (when followed) will help you to save time and to find out your DirectX problems more quickly, or at least helps you provide more specific information to a question you’re posting to these forums:
When applicable:



Last note about using these forums and DirectX MVPs

Since these forums house many DirectX MVPs, it is good to know what an MVP (or Microsoft Most Valuable Professional) does for you. DirectX MVPs are people who have high experience in one or more component of DirectX. They offer their help for free to other people in the community only because they like to.
MVPs usually spend a lot of time in the forums looking for unanswered questions, and trying to help those who need it. Listen to their advice as it is based on high experience. After they answer your question, do not forget to mark their reply as an answer (the Mark as Answer button), this not only benefits other people in the forums, but it also frees the MVP’s mind from worrying about your problem further (and yes, at least I do keep worrying about it until you close it).
Finally, remember that they are helping you only because they want to. They are NOT Microsoft employees, and they are NOT paid by Microsoft. They express their own ideas and do not represent anyone but their own.



I hope this helps you get more comfortable with using DirectX. After all, game programming is about having fun, not struggling with some damn crashing program just because we don’t know what is going on under the hood. Be creative, be open-minded, and… have fun!