Introduction to DirectX and Creating the Main Game Loop using DirectX11

So when I first began developing a game I found DirectX easier compared to OpenGL be it online tutorials or Support that I got online mostly from Microsoft so what is DirectX then ?


Microsoft DirectX is a collection of application programming interfaces (APIs) for handling tasks related to multimedia, especially game programming and video, on Microsoft platforms. Originally, the names of these APIs all began with Direct, such as Direct3D, DirectDraw, DirectMusic, DirectPlay, DirectSound, and so forth. The name DirectX was coined as a shorthand term for all of these APIs (the X standing in for the particular API names) and soon became the name of the collection. When Microsoft later set out to develop a gaming console, the X was used as the basis of the name Xbox to indicate that the console was based on DirectX technology. The X initial has been carried forward in the naming of APIs designed for the Xbox such as XInput and the Cross-platform Audio Creation Tool (XACT), while the DirectX pattern has been continued for Windows APIs such as Direct2D and DirectWrite.

Direct3D (the 3D graphics API within DirectX) is widely used in the development of video games for Microsoft Windows and the Xbox line of consoles Microsoft unveiled DirectX 11 at the Gamefest 08 event in Seattle, with the major scheduled features including GPGPU support (DirectCompute), and Direct3D 11 with tessellation support and improved multi-threading support to assist video game developers in developing games that better utilize multi-core processors.in 2013 most of the games were being  developed using DirectX 11 so I wanted to know what are it's capabilities 

Capabilities of DirectX 11

Full featured set of APIs enabling one to create high-end 2D and 3D graphics applications/games.​
Direct 2D/Direct 3D: Handles graphics.​
DirectWrite: Handles text rendering features.​
DirectXMath: Provides handy functions for doing the math
XInput: Replaces the old DirectInput API and helps one to provide console controller support.​
XAudio2: Replaces Direct Audio, allowing one to create a high performance audio engine while improving performance at the same time.

​What’s New to DirectX 11

New shader pipeline stages:​
Hull Shader​
Tessellator​
Domain Shader​
Allows for seamless LOD through dynamic​
tessellation.​

So how does shaders in DirectX 11 work

So DirectX 11 has added support for three new shader stages in the drawing pipeline, and this is just as the hardware develops new functionality is added into the hardware as well as the software keeps up with it. We have things like the hull shader and the tessellator and the domain shader stages. There's the little graphics pipeline down the right. And one can see those three have really been slotted right into the middle in between the vertex and the geometry shader. The geometry shader was something that came in last time around in DirectX 10, I believe. And pixel shaders and vertex shaders have been around for quite some time.And these three new stages do have new capabilities

Hull shader for example, it operates once per patch, and a patch is just effectively a set of vertices that make up a triangle or quad within a larger mesh typically used for rendering an object. The hull shader itself can do a couple of basic things: it can transform the input control points into output control points within a patch.But more importantly it can provide what are known as tessellation factors which are effectively just numbers that indicate how much a certain path should be tessellated by the next stage.

Now what do I mean by tessellation?what one call infinite tessellation. You'll see a model at a low poly stage such as there in the picture and effectively all the vertices and the lines that make up that mesh as the camera pans in grow... more detail appears in the model such as a face and it just gets more and more detailed until you zoom right into the individuals wrinkles. And that's effectively what I am talking about when we talk about infinite tessellation.

So the Tessellator is probably one of the most important features really in terms of gamers that is in DirectX 11, and I really want to grab that power, to harness it and to use it in my newer games. A lot of games are starting to bring it in. It allows us to do seamless LOD
One might have, for example, in the original Halo and so on. One could open certain doorways and One'd see geometry that would look pretty blocky and then it would pop and it'd be a little bit smoother. And then, after a couple of more seconds it would pop again and now it's the final model.And that was usually because a lot of the game engines are set up to load models as fast as possible but they only have a limited time to do so, so they load in a low-level detail...Effectively that's what LOD means, 

level of detail lower version LOD time to get the higher resolution one in they'll just pop it out with the one that's there. Eventually we started realizing the popping doesn't look real nice so we'll start trying to blend between the two once we have the secondary model loaded.That looked okay but sometimes you'd get weird facial deformations,somebody's nose starts to come out of their head now that they actually have a nose and things like that. And you could catch models at a weird angle where you see that, and that wasn't working really well. But now with processes like this we're going to be able to literally give an infinite level or tessellation effectively or geometry that we can blend seamlessly and let the graphics card sort of handle it for us without actually even having to make that high detailed model. We can make a lower detailed model, put on the normal bump maps and normal maps that you would see to give it some detail being a low poly model and then,the Graphics card can fill it in with a shader all that extra detail as the camera approaches. 

So the Tessellator is the one that really generates the new geometry or the new vertices and splits each individual triangle into two triangles or more depending on Ones approach to give that detail. That's what that Tessellator stage does.

Then we have the domain shader which is the third piece of the puzzle that completes the infinite tessellation kind of effect. It's invoked once per vertex generated by the tessellator and each invocation is identified by its coordinate on a generic domain. So the role of Domain Shader that's where it's name is from this conversion to a coordinate in the generic domain... is to convert that coordinate into something tangible like a 3D point in space for use later on in the rest of the stages of the pipeline. So that's really all it does is convert all the math that happened, splitting up all the faces into new positions, points and vertices; it's going to take those points of data and actually build them out into real things that exist in ones game effectively. So this could have a huge impact on games with large, open environments, for example. One might probably noticed that distance object popping; it's starting to go away in a lot of games. Really, they're finding better ways like this to switch between those LOD positions.
The idea is simple One doesn't need three models of the same model, a low poly, a medium poly and a high poly anymore; One don't need those.Microsoft has figured out approaches that would let one create that extra art as quick as possible without actually having to have a guy rebuild it. So maybe he makes the high poly version of the model and then, there are certain other tools that you can use to sort of crush those into a lower poly and then crush it even smaller into basically your flat, almost 2D guy and so on.

Loosely coupled secondary pipeline


  • Use of a compute shader allows the GPU to be used as a​
  • generic grid of data-parallel processors.​
  • It has explicit access to your GPUs faster memory banks.​
  • It can also do scattered reads and writes to memory.​
  • Allows engine developers to move CPU intensive operations to the GPU as a form of load balancing.​
These are called Loosely coupled because you can't have the same sub-resource bound for writing in both of the pipelines simultaneously.

Basic Game Loop

And I've written it out in pseudo-code here so that we don't have to go through all of the code and is effectively breaking it down as simple as possible.it's a generic sort of basic game loop that you will see.

while the window is active​
if the window is visible​
if the game can’t function do to size, or a pop-up, or is simply paused​
wait for a new event that changes the state of the app.​

else ​
do the normal processing of incoming messages.​
update the state of the game and carry out associated actions for that state.​
then get an updated rendering of the current screen.​

else​
Process events until we are the visible window.​
Exiting due to window closing. Make sure to save the game’s state.​

So while the window is active... Okay? If it's not active then I probably should be doing something else... I check if the window is visible. And if it is visible, if the game can't function due to size or a pop-up or it simply paused then we wait for a new event to change the state of the app. So that's just sort of our, "Hold steady. I've notified the user. He accidentally unplugged his keyboard or their controller or something like that, and we'd like their input." So that's just where it sort of hovers and waits. Beyond that we can move down. If there are none of those pop-ups or anything in our way, we go under our else condition where we do the normal processing of any incoming messages whether it's other input or now we need to suspend the game and so on. 
And after that passes through once we've registered all of those,we update the state of the game, carry out all of the actual logic, the rendering, whatever it needs to be doing at this point in time. Then, we get an updated rendering to the current screen so we push that up to the actual device and flip the buffers,So that's effectively the game loop itself.It's just going to keep processing. Usually the game is active. Usually the window is visible; otherwise, it's going to be hard to play the game. So most of the time it's really going to spend the bulk of its time in that else condition just doing the normal processing of the game input messages, updating the state of the world, render.Get messages, update, render. And that's really my basic game loop system. Of course if the window isn't visible, we still need to catch any possible events that make the window visible so we have that else case in there as well. And then, finally at the end we exit due to the window closing or whatever other signifies you want for closing your game. But make sure to save the game's state so that they can come back and play again and pick up where they left off especially if one is doing a mobile device. That's very important for the mobile developers because they get interrupted
The application is going to kick off. It's going to get us in there and, again, those two important things that are there in the Basic Game loop are it's going to be doing is effectively just going to sit in that one chunk of the loop and keep going back, updating the game, rendering the game, updating the game, rendering the game. So the update stage is really the bulk of everything that's going to happen in your application. And I've made an Update function and that's where we've put all that data.

Comments

Popular posts from this blog

Traits Design - Inspirations and Design The Dead Universe

C++ and HTML as UI?