Hello everyone, I am currently implementing a DirectX 11 engine and I thought it would be nice to write an article about some of the most important changes I have come across with respect to DirectX 9.
This article is aimed at people who have got at least a medium level of D3D9 and want to begin programming using D3D11. Some of the topics I am going to talk about were already introduced in D3D10.
If you have done some D3D9 programming before it is most likely that you used D3DXVECTOR as your vector type. The problem now is that D3DX11, in contrast to D3DX9 and D3DX10, does not include a vector type (In addition D3DX11 is deprecated for Windows 8).
Instead, Microsoft wants you to use XNAMATH. There’s a good reason for using XNAMATH; XNA provides a vector type called XMVECTOR which take advantage of SIMD (Single instruction, multiple data); this means that we can increase our application performance by using this new type.
Likewise, we should use XMMATRIX (XNAMATH matrix type) for matrix calculations so that we can take advantage of SIMD as well for computations involving matrices.
However there are some considerations when using these types; for instance, XMVECTOR and XMMATRIX must be 16-byte aligned, this is done automatically for local and global variables but not for class members or struct members, in these two cases it is recommended to use XMFLOAT types (XMFLOAT3, XMFLOAT4X4, …) when storing the data into a class and then transform these types into XMVECTOR and XMMATRIX before doing the calculations. There are some other restrictions (like when passing these types as parameters to a function); you can find more information here.
Good bye onLostDevice and OnResetDevice
In D3D9 if we didn’t want our application to crash when resizing the screen, minimizing, maximizing, etc, we had to create onResetDevice and onLostDevices methods for our resources (Information here ). This issue was usually translated into a lot of extra lines of code for assets management.
I’ve just implemented a post-process volumetric light scattering effect.
Volumetric light scattering is a very handy effect we can use for lots of things. For example I wanted to make a scene with a mysterious light hovering in the woods.
The following video shows my scene. Of course it’s powered by Rebirth Engine, and everything is real-time (take a look at the dynamic shadows on the floor):
Implementing this effect is really straightforward, I followed this amazing article from GPU gems http://http.developer.nvidia.com/GPUGems3/gpugems3_ch13.html.
All we need to do is:
- We draw a simple version of the scene where every occluding object is drawn in black (the light source remain bright):
2. Then we process the occlusion scene using a light scattering shader like the one presented in the GPU Gems article and we blend the result over the original scene:
What the GPU gems shader does is basically compute the ray from the current pixel (the pixel we are processing) to the light source in screen space and check whether the ray hits the light or if there’s any occluding object. Unfortunately we have to sample neighboring pixels too, i.e. we have to sample the texture more than once for every pixel.
We can save some cycles by reducing the number of samples we use or by using an occlusion texture resolution smaller than the screen resolution.
The shader from GPU Gems also has some parameters we can change to tweak the effect, we can pass them to the shader in real time, but it’s recommendable to set the number of samples as a constant parameter and re compile if we want to change it because of the performance (Using a loop whose limit is a variable instead of a constant in a shader is really inefficient).
This is it for now; I’m going to enhance my scene adding new effects so keep posted for future updates.
Hi, my name is Angel. I’m from Spain and I’ve been investigating and working on 3D graphics for three years (mostly on my own). Rebirth Engine is the culmination of those years of studying real-time 3D topics. You can check its features in the following video (please watch the video in HD):
If you want to know more about it, please visit: http://graphicprogramming.wordpress.com/rebirthengine/
For now I can’t upload the whole engine code, but don’t worry, from now on I will explain on this blog some of the algorithms and effects used in the engine. If you want me to explain something in particular, please leave a comment or send me message through this form:
If you don’t want to get lost, you should have some C++,DirectX and HLSL knowledge. There are plenty of great books that you can use to gain a good level, I can recommend you some of them if you want to.
Last but not least, this blog is not going to be all about my engine. I’m currently working with Unreal Engine 3 and CryEngine 3 so I want to write about them within a near future, I will also write about others graphic libraries besides DirectX 9.0c like D3D11 and OpenGl.
That’s all for now. See you soon!