Hello There.

I hope you all are doing well, It's been a while! As you might recall, the last we discussed was about gpu programming in DirectX 11.1 Game Programming blog. From then on, I've been pretty busy and working on so many projects. What I'm up to these days, you might ask! I've decided to start writing again, yet, this time about my experiences and diaries on GPU programming. Also, I've decided to write some tutorials for those programmers out there who want to learn gpu programming or need to use my framework, or even build their own game engine.

So, lets get started!

    The Prerequisites

  • Math, Math and the Math.

The very primary knowledge which is essential for every programmer, whether game or GPU programmers, is math.

Needless to say Basic arithmetic and linear algebra are also very crucial. Additionally extensive knowledge of the following subjects are vital:

  • Vectors, refers to an quantity that represents both magnitude an direction. A Vector is necessary for calculating position, camera look, camera direction, speed and it is used in almost all of rendering techniques such as calculating lights, shadows.
  • Quaternions, is a four dimensional associative normed division algebra over the real numbers, which represents rotations almost as space-efficiently as Euler angle.
  • Matrices, in the world of 3D programming it is used to represent geometric transformations such as scale, rotation and translation. Besides, it is used to change coordinate systems. A matrix is a rectangular array of numbers or other mathematical objects which supports various mathematical operations; including addition or multiplication.

If you are not familiar with these topics, don't worry! We will look at them more closely later in our tutorials.

  • Knowledge of C/C++ language

The second important thing you need to learn is C++ programming language. Why C++? Well, when we talk about the best and most efficient language for GPU programming, the first thing to do is to compare management language vs un-management languages.

Managed programming languages, such as .Net, Java, provide a generalized way to handle the details of memory management and garbage collection, with only a small amount of overhead usage. For example Java is a management language that needs JRE to be able to run on target machine and JRE provides the required libraries, (the Java Virtual Machine (JVM)), and other components to run applications written in Java programming language. The same works for C#, VB.Net that also need .net framework in order to run on Windows platform.

On the other hand, for Unmanaged language, such as C or C++, you must write extra code to manage memory and security, and also to clean up objects after they have served their purpose. This approach will be more efficient for realtime applications, since we need to manage GPU and CPU operations in our codes, using garbage collectors might be the bottleneck of your code, thus using unmanaged code is the right way of hardware programming. In fact, when you write your application on standard C++, it can be launched on almost every operating system.

So how about C++ performance? Robert Hundt published an article, in which he reported a well specified, compact benchmark in four programming languages, namely C++, Java, Go, and Scala. Further, since in most of realtime applications, one needs to implement most of the mathematical algorithms, for instance binary trees or n-body, therefore it is highly recommended to check these benchmarks first to decide which language is most suitable.

Last but not least, in all frankness, I don't care for the war between the languages, as I think every language has its own merits, and it's really up to you to choose which one is the right choice for your approach. Nevertheless, if anything, past experiences shown us for real time application, C/C++ seem to be most suitable.

  • IDE

Before writing any code, you will have to have an IDE which is needed to compile your source code. I used Visual Studio for Windows platform, XCode for MacOS and Netbeans for linux platform. I will be supplying project files for these IDEs. However, there are several other IDEs, which you can use according to your project.

  • Graphics API

Finally you need to learn a graphic API to build your own 2D/3D graphic engine. The most commonly used APIs are OpenGL and DirectX which have been competing for so many years. Open Graphics Library (OpenGL) is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphic processing unit (GPU), to achieve hardware-accelerated rendering. At the time of writing this article, in 2017 SIGGRAPH conference, the Khronos Group announced the immediate public availability of the OpenGL 4.6 specification for it's 25th anniversary. Silicon Graphics Inc., started developing OpenGL in 1991 and released it in January 1992. Since then it is used extensively in the fields of computer-aided design (CAD), virtual reality, scientific visualization, information visualization, flight simulation and video games. Since 2006 OpenGL has been managed by the non-profit technology consortium Khronos Group.

On the one hand, OpenGL is a cross platform and on the other hand Microsoft DirectX can be run only on Microsoft Platforms.

DirectX is composed of multiple APIs:

  1. Direct3D or D3D which is responsible for drawing 3D graphics.
  2. DXGI for enumerating adapters and monitors and managing swap chains for Direct3D 10 and up.
  3. Direct2D which handles 2D graphics.
  4. DirectWrite which is used for font rendering.
  5. DirectCompute which is used for GPU Computing.
  6. DirectX Diagnostics (DxDiag) which is a tool for diagnosing and generating reports on components related to DirectX.
  7. DirectX Media Objects which supports for streaming objects such as encoders, decoders and effects.
  8. DirectSetup is the installation of DirectX components, and the detection of the current DirectX version.
  9. XACT3 is higher-level audio API.
  10. XAudio2 is a low-level API for audio.

They are many components which has deprecated by Microsoft, but continue to be supported for now, such as: DirectX Media, DirectShow, DirectDraw, DirectInput, DirectPlay, DirectSound, DirectSound3D, DirectMusic.

In fact the X stands for theses additional components. Besides, Microsoft released Managed Dircet3D and the XNA graphics library on top of Direct3D 9 for .Net developers.

At the time of writing this article, the final major version of DircetX is 12 which was announced by Microsoft at GDC on March 20, 2014, and was officially launched alongside Windows 10 on July 29, 2015. DircetX 12 is only supported on Windows 10, Windows Mobile 10 and Xbox One. The version of DirectX that runs on the Xbox One is DirectX 11.X which already includes a subset of the features in DirectX 12. If you want to develop DirectX application, you need Windows 10 SDK (find here). The last stand alone DirectX SDK was released on June 2010 and since then, the DirectX SDK is the part of Windows SDK. It also comes with the installation of Visual Studio.

During the competition of OpenGL 4.5 and DirectX 11 in 2013, AMD and DICE cooperated and released a new graphic API called Mantle. Mantle is an alternative for Direct3D and OpenGL. It is, however, not a public SDK. Later in March 2015, the focus was shifted to these two areas: Microsoft DirectX 12 and the Mantle-derived Vulkan API. In fact, Vulkan API is the next generation of OpenGL API achieved by Khronos group. It is a super low-overhead, cross-platform 3D graphics and compute API.

Since June 2, 2014, Apple has released a graphic API called Metal which has been available on iOS devices using the Apple A7 or later. From June 8, 2015 this new API was released on Macs (2012 models or later) running OS X El Capitan or later. Just like DirectX 12 and Vulkan, Metal is a low-level, low-overhead hardware-accelerated graphics and compute API, but it is only available for Apple's devices.

In these tutorials, I'm going to go into more details on Vulkan and DirectX12 and later I will include Metal as well. Please keep in mind that most of the tutorials are released as the samples of Wolf Engine, an open source graphic engine developed by me.

In the next tutorial, we are going to learn the basic and essential math of graphics programming. So, stay tuned!