Windows API Reference

Graphics and Multimedia / DirectX / DirectCompute

DirectCompute Overview

DirectCompute is a modern, programmable compute solution for Windows. It enables developers to harness the power of the GPU for general-purpose computation, complementing traditional graphics rendering by offloading computationally intensive tasks from the CPU to the GPU. This approach significantly accelerates a wide range of applications, from scientific simulations and data analysis to video processing and machine learning.

What is DirectCompute?

DirectCompute is part of the DirectX family of technologies. It allows developers to write compute shaders, which are programs that run on the GPU. These shaders can operate on data stored in various GPU-accessible resources, such as textures and buffers. By leveraging the massively parallel architecture of modern GPUs, DirectCompute can perform calculations much faster than the CPU for many types of problems.

Key Features and Benefits

How it Works

DirectCompute operates by dispatching compute shaders to the GPU. These shaders execute in parallel across many threads. Data is typically loaded into GPU resources (buffers or textures), processed by the compute shader, and then the results are read back to the CPU or used in subsequent graphics rendering passes.

The basic workflow involves:

  1. Creating Compute Shaders: Writing shaders in HLSL and compiling them.
  2. Preparing Data: Uploading input data to GPU resources like ID3D11Buffer or ID3D11Texture2D.
  3. Binding Resources: Binding these resources to the Direct3D pipeline.
  4. Dispatching Threads: Using the ID3D11DeviceContext::Dispatch method to execute the compute shader with a specified number of thread groups.
  5. Reading Results: Unbinding resources and potentially reading the processed data back to the CPU.
Note: DirectCompute requires a GPU that supports Shader Model 5.0 or later for full functionality, though basic compute capabilities are available with earlier models.

Shader Model 5 and Beyond

Shader Model 5 (SM5) introduced significant enhancements for compute, including:

Modern GPUs and DirectX versions continue to build upon these foundations, offering even more power and flexibility for GPGPU (General-Purpose computing on Graphics Processing Units) tasks.

Getting Started

To begin using DirectCompute, you'll need:

Familiarity with Direct3D 11 (or later) API is highly recommended, as DirectCompute is tightly integrated with it.

Caution: While GPUs excel at parallelizable tasks, not all problems are suitable for GPU acceleration. Analyze your workload to ensure DirectCompute will provide a tangible benefit.

Explore the related sections to delve deeper into the core concepts, shader programming, resource management, and performance optimization techniques for DirectCompute.