NANDHOO.

DirectX Basics

Chapter 13: DirectX Basics


DirectX is a collection of application programming interfaces (APIs) for handling tasks related to multimedia, especially game programming and video, on Microsoft platforms. While OpenGL is cross-platform, DirectX is the proprietary standard for Windows and Xbox.


What is DirectX?


The "X" in DirectX refers to its individual components:


  1. Direct3D: The 3D graphics API (the primary competitor to OpenGL/Vulkan).
  2. Direct2D: For high-performance 2D vector graphics.
  3. DirectWrite: For high-quality text rendering.
  4. DirectInput / XInput: For handling input devices (keyboards, controllers).
  5. DirectSound / XAudio2: For low-latency audio processing.

DirectX vs. OpenGL


FeatureOpenGLDirectX (Direct3D)
PlatformCross-platform (Windows, Linux, Mobile)Microsoft only (Windows, Xbox)
ArchitectureState-machine basedObject-oriented (COM-based)
Shading LanguageGLSL (OpenGL Shading Language)HLSL (High-Level Shading Language)
ManagementOften easier to set up for simple projectsMore complex setup, but provides more control
VersionsFixed-function (1.x, 2.x), Programmable (3.0+)Fixed-function (up to 8), Programmable (9, 10, 11, 12)

Core Direct3D Concepts


Direct3D (D3D) uses Component Object Model (COM), a Microsoft technology that allows software components to communicate.


  1. The Device: Represents the virtual graphics adapter. Used to create resources (buffers, textures, shaders).
  2. The Device Context: Used to issue rendering commands to the GPU (Direct3D 11).
  3. The Swap Chain: Manages the front and back buffers.
  4. Render Target View: A specialized resource that allows the GPU to draw directly to a texture or the back buffer.

The Direct3D 11 Pipeline


Similar to the OpenGL pipeline, but with different nomenclature:


  1. Input Assembler (IA): Reads vertex data from buffers.
  2. Vertex Shader (VS): Transforms vertices.
  3. Hull / Domain / Geometry Shaders: Optional stages for tessellation and advanced geometry manipulation.
  4. Rasterizer (RS): Converts geometry into pixels.
  5. Pixel Shader (PS): Calculates pixel color (equivalent to OpenGL's Fragment Shader).
  6. Output Merger (OM): Combines pixel colors with the depth and stencil buffers.

HLSL: High-Level Shading Language


DirectX uses HLSL, which is very similar to GLSL but has some syntax differences.


// Example Vertex Shader in HLSL
float4 VSMain(float4 pos : POSITION) : SV_POSITION {
    return pos;
}

// Example Pixel Shader in HLSL float4 PSMain(float4 pos : SV_POSITION) : SV_Target { return float4(1.0f, 0.5f, 0.0f, 1.0f); // Solid Orange }


DirectX 12: Low-Level Access


DirectX 12 (introduced with Windows 10) is a radical departure from D3D11. It is a "low-level" API that gives developers much more control over the GPU, similar to Apple's Metal or the Khronos Group's Vulkan.


  • Manual Synchronization: The developer is responsible for ensuring the CPU and GPU are in sync.
  • Command Queues: Developers manually manage how commands are sent to the GPU.
  • Explicit Memory Management: No longer handled by the driver.

Summary


DirectX remains the industry standard for high-end game development on PC and Xbox. While its API can be more daunting than OpenGL, its deep integration with the Windows ecosystem and its powerful low-level control (in DX12) make it an essential tool for high-performance graphics engineering.