# DirectCompute

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/DirectCompute
> Markdown URL: https://mediated.wiki/source/DirectCompute.md
> Source: https://en.wikipedia.org/wiki/DirectCompute
> Source revision: 1277521706
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Application programming interface from Microsoft

**Microsoft DirectCompute** is an [application programming interface](/source/Application_programming_interface) (API) that supports running [compute kernels](/source/Compute_kernel) on [general-purpose computing on graphics processing units](/source/GPGPU) on Microsoft's [Windows Vista](/source/Windows_Vista), [Windows 7](/source/Windows_7) and later versions. DirectCompute is part of the [Microsoft DirectX](/source/Microsoft_DirectX) collection of APIs, and was initially released with the [DirectX 11](/source/DirectX_11) API but runs on graphics processing units that use either [DirectX 10](/source/DirectX_10) or DirectX 11.[1] The DirectCompute architecture shares a range of computational interfaces with its competitors: [OpenCL](/source/OpenCL) from [Khronos Group](/source/Khronos_Group), compute shaders in [OpenGL](/source/OpenGL), and [CUDA](/source/CUDA) from [NVIDIA](/source/NVIDIA).

The DirectCompute API brings enhanced multi-threading capabilities to leverage the emerging advanced compute resources.[2] The API is designed for non-graphical applications to access and use [GPU](/source/Graphics_processing_unit) resources.[3]

## Compute Pipeline

The Compute pipeline is a type of [graphics pipeline](/source/Graphics_pipeline) used for dispatching and executing compute [shaders](/source/Shader). Compute pipelines are run through compute [command lists](/source/Display_list), which are restricted to recording only copy and compute commands. Compute shaders are used for general-purpose algorithms and computations, and are run through parallel [processors](/source/Processor_(computing)) on the GPU. This parallel execution model done by the compute pipeline is organized into a dispatch, thread groups, and [threads](/source/Thread_(computing)). The dispatch is a 3-dimensional container of thread groups, and a thread group is a 3-dimensional container of threads.[4] Thread groups are ran on the GPU in *waves.*[5]

This pipeline allows for workloads to be easily sent to the GPU without the need for restructuring all of a program's code.[6]

A typical compute pipeline contains a read-only shader resource view as an input, constant buffer views for additional resource constants, and an unordered access view for an output. It is important to set the resource states of these resources in order to improve performance.[7]

## Example code

The initialization of the compute pipeline involves creating the root signature, reading the compute shader, and creating the pipeline state object.

// Set the root signature
CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC root_signature_desc{
    1, root_parameters,
    0, nullptr
};

// Serialize the root signature
Microsoft::WRL::ComPtr<ID3DBlob> root_signature_blob{nullptr};
Microsoft::WRL::ComPtr<ID3DBlob> error_blob{nullptr};
D3DX12SerializeVersionedRootSignature(
    &root_signature_desc, D3D_ROOT_SIGNATURE_VERSION_1_1,
    root_signature_blob.GetAddressOf(), error_blob.GetAddressOf()
);

// Create the root signature
Microsoft::WRL::ComPtr<ID3D12RootSignature> root_signature{nullptr};
device->CreateRootSignature(
    0, root_signature_blob->GetBufferPointer(),
    root_signature_blob->GetBufferSize(),
    IID_PPV_ARGS(root_signature.GetAddressOf())
);

// Read the compute shader
Windows::WRL::ComPtr<ID3DBlob> compute_shader{nullptr};
D3DReadFileToBlob(L"C:/path/to/compute/shader", compute_shader.GetAddressOf());

// Create the compute pipeline state object (PSO)
struct PipelineStateStream {
    CD3DX12_PIPELINE_STATE_STREAM_ROOT_SIGNATURE root_signature;
    CD3DX12_PIPELINE_STATE_STREAM_CS compute_shader;
} pipeline_state_stream;

// Setting the root signature and the compute shader to the PSO
pipeline_state_stream.root_signature = root_signature.Get();
pipeline_state_stream.compute_shader = CD3DX12_SHADER_BYTECODE{compute_shader.Get()};

D3D12_PIPELINE_STATE_STREAM_DESC pipeline_state_stream_desc{
    sizeof(PipelineStateStream), &pipeline_state_stream
};

// Create pipeline state
device->CreatePipelineState(
    &pipeline_state_stream_desc,
    IID_PPV_ARGS(pipeline_state_stream.GetAddressOf())
);

After the initialization of the compute pipeline, every frame, the pipeline state must be set, the compute root signature must be set, and the dispatch is run.

command_list->SetPipelineState(pipeline_state);
command_list->SetComputeRootSignature(root_signature);
command_list->Dispatch(groups_x, groups_y, groups_z);

## See also

- [OpenCL](/source/OpenCL)

- [CUDA](/source/CUDA)

- [C++ AMP](/source/C%2B%2B_AMP)

- [Graphics Processing Unit](/source/Graphics_Processing_Unit)

## Further reading

- [Compute Shaders in D3D12](https://logins.github.io/graphics/2020/10/31/D3D12ComputeShaders.html)

## References

1. **[^](#cite_ref-1)** ["DirectCompute"](https://developer.nvidia.com/directcompute). *developer.nvidia.com*. [NVIDIA](/source/NVIDIA). 28 September 2010. Retrieved 22 March 2015.

1. **[^](#cite_ref-2)** James, Dave; Chiapetta, Marco. ["The Directx Evolution"](https://search.ebscohost.com/login.aspx?direct=true&db=f6h&AN=108366162&site=eds-live&scope=site) (PDF). *Maximum PC*. **19** (8): 52–59. [ISSN](/source/ISSN_(identifier)) [1522-4279](https://search.worldcat.org/issn/1522-4279). Retrieved 2024-08-07 – via MasterFILE Complete.

1. **[^](#cite_ref-3)** Mohr, Neil. ["Beyond Graphics with Gpgpus: Maximum PC"](https://search.ebscohost.com/login.aspx?direct=true&db=f6h&AN=131014389&site=eds-live&scope=site). *Maximum PC*: 46–51. [ISSN](/source/ISSN_(identifier)) [1522-4279](https://search.worldcat.org/issn/1522-4279) – via MasterFILE Complete.

1. **[^](#cite_ref-4)** Young, Eric (2010-09-20). ["DirectCompute - Optimizations and Best Practices"](https://www.nvidia.com/content/gtc-2010/pdfs/2260_gtc2010.pdf) (PDF). Retrieved 2024-02-14.

1. **[^](#cite_ref-5)** Lively, David; Gruen, Holger (2017-03-03). ["Wave Programming in D3D12 and Vulkan"](https://gpuopen.com/wp-content/uploads/2017/07/GDC2017-Wave-Programming-D3D12-Vulkan.pdf) (PDF). *gpuopen*. Retrieved 2024-02-15.

1. **[^](#cite_ref-6)** Graham-Smith, Darien (September 2021). ["The Return of GPU Computing"](https://search.ebscohost.com/login.aspx?direct=true&db=f6h&AN=151222838&site=eds-live&scope=site). *PCPro* (323): 44–47. [ISSN](/source/ISSN_(identifier)) [1355-4603](https://search.worldcat.org/issn/1355-4603). Retrieved 2024-06-10 – via MasterFILE Complete.

1. **[^](#cite_ref-7)** Kramer, Lou (2022-07-22). ["Compute Shaders"](https://gpuopen.com/presentations/2021/ComputeShaders-final.pdf) (PDF). *gpuopen*. Retrieved 2024-03-11.

## External links

- [Compute Shader Overview](http://msdn.microsoft.com/en-us/library/ff476331%28v=VS.85%29.aspx)

- [DirectCompute Lecture Series](https://channel9.msdn.com/Tags/directcompute-lecture-series)

- [Advanced DirectX 11: DirectCompute by Example](http://www.gdcvault.com/play/1013698/Advanced-DirectX-11-DirectCompute-by)

- [GTC On-Demand](http://on-demand-gtc.gputechconf.com/gtcnew/on-demand-gtc.php?searchByKeyword=DirectCompute&searchItems=&sessionTopic=&sessionEvent=&sessionYear=&sessionFormat=&submit=&select=+)

v t e Microsoft Windows components APIs Architecture 9x NT Booting process Games Management tools App Installer Command Prompt Control Panel Device Manager DirectX Diagnostic Tool Disk Cleanup Drive Optimizer Driver Verifier Event Viewer IExpress Management Console Netsh Performance Monitor PowerShell Recovery Console Resource Monitor Settings Sysprep System Configuration System File Checker System Information System Policy Editor System Restore Task Manager Windows Backup Windows Error Reporting Windows Ink Windows Installer Windows Update Windows Insider WinRE WMI Apps 3D Viewer Calculator Calendar Camera Character Map City Art Search Clipchamp Clock Company Portal Copilot Edge Fax and Scan Feedback Hub Get Help Magnifier Mail Media Player 2022 Mesh Messaging Mobility Center Money Movies & TV Narrator News Notepad OneDrive OneNote Paint PC Manager People Phone Link Photos Quick Assist Remote Desktop Connection Snipping Tool Sound Recorder Speech Recognition Sticky Notes Store Terminal To Do Weather Whiteboard Windows App Xbox Shell Action Center Aero AutoPlay AutoRun ClearType Explorer Search IFilter Indexing Service Namespace Saved search Special folder Start menu Task View Taskbar Windows Spotlight Windows XP visual styles Services BITS CLFS Error Reporting Multimedia Class Scheduler Service Control Manager Shadow Copy Task Scheduler Wireless Zero Configuration File systems CDFS DFS exFAT FAT IFS NTFS EFS Hard link links Mount Point Reparse point TxF ReFS UDF Server Active Directory Active DRM Services DFS Replication Distributed Transaction Coordinator DNS Domains Folder redirection Group Policy Hyper-V IIS MSMQ Network Access Protection Print Services for UNIX PWS Remote Desktop Services Remote Differential Compression Remote Installation Services Roaming user profiles Server Core SharePoint System Resource Manager Windows Deployment Services Windows Media Services WSUS Architecture Boot Manager Console CSRSS Desktop Window Manager Enhanced Write Filter Graphics Device Interface Hardware Abstraction Layer I/O request packet Imaging Format Kernel Transaction Manager Library files Logical Disk Manager LSASS MinWin NTLDR Ntoskrnl.exe Object Manager Open XML Paper Specification Portable Executable DLL EXE Registry Resource Protection Security Account Manager Server Message Block Shadow Copy SMSS System Idle Process USER WHEA Winlogon WinUSB Security Security and Maintenance AppLocker BitLocker Credential Guard Data Execution Prevention Defender Family features Kernel Patch Protection Mandatory Integrity Control Protected Media Path User Account Control User Interface Privilege Isolation Windows Firewall Compatibility COMMAND.COM Windows Subsystem for Linux WoW64 API Active Scripting JScript VBScript WSH COM ActiveX ActiveX Document COM Structured storage DCOM OLE OLE Automation Transaction Server DirectX Native .NET Universal Windows Platform WinAPI Windows Mixed Reality Windows Runtime WinUSB Games Solitaire Collection Surf Discontinued Games 3D Pinball Chess Titans FreeCell Hearts Hold 'Em InkBall Purble Place Solitaire Spider Solitaire Tinker Apps ActiveMovie Address Book Anytime Upgrade Backup and Restore Cardfile CardSpace CD Player Chat Contacts Cortana Desktop Gadgets Diagnostics DriveSpace DVD Maker Easy Transfer Edge Legacy Fax Food & Drink Groove Music Health & Fitness Help and Support Center HyperTerminal Imaging Internet Explorer Journal Make Compatible Maps Media Center Meeting Space Messaging Messenger Mobile Device Center Movie Maker MSN Dial-Up NetMeeting NTBackup Outlook Express Paint 3D Pay Phone Companion Photo Gallery Photo Viewer Program Manager Skype Sports Start Steps Recorder Sysedit Syskey Tips Travel WinHelp WordPad Write Others Desktop Cleanup Wizard File Protection Games for Windows HPFS Interix Media Control Interface MS-DOS 7 Next-Generation Secure Computing Base POSIX subsystem ScanDisk Video for Windows Virtual DOS machine Windows on Windows Windows Services for UNIX Windows SideShow Windows System Assessment Tool Windows To Go WinFS Spun off to Microsoft Store DVD Player File Manager Hover! Mahjong Minesweeper Category List

v t e Microsoft development tools Development environments Visual Studio Code Express Team System Profiler Tools for Applications Tools for Office Others Blend Expression Web FxCop GW-BASIC MACRO-80 Macro Assembler MSBuild Pascal QuickBASIC QBasic QuickC Robotics Developer Studio Roslyn SharePoint Designer FrontPage Small Basic WebMatrix Windows App SDK Windows App Studio Windows SDK CLR Profiler ILAsm Native Image Generator WinDiff XAMLPad Languages Dynamics AX BASIC Visual Basic legacy VB.NET VBA VBScript Bosque Visual C++ C++/CX C++/CLI Managed C++ C++/WinRT C# C/AL Dafny Dexterity F# F* Visual FoxPro Java J++ J# JavaScript TypeScript JScript IronPython IronRuby Lean P Power Fx PowerShell Project Verona Q# Small Basic VPL XAML APIs and frameworks Native Windows API Silverlight XNA DirectX Managed DirectX UWP Xbox Development Kit Windows Installer WinUI .NET ASP.NET Core AJAX Dynamic Data MVC Razor Web Forms ADO.NET Entity Framework MAUI CardSpace Communication Foundation Identity Foundation LINQ Presentation Foundation Workflow Foundation Device drivers WDK WDF KMDF UMDF Windows HLK WDM Database SQL Server Express Compact Management Studio MSDE SQL services Analysis Reporting Integration Notification Other Visual FoxPro Microsoft Access Access Database Engine Extensible Storage Engine Source control Visual SourceSafe Team Foundation Version Control Testing and debugging CodeView OneFuzz Playwright Script Debugger WinDbg xUnit.net Delivery Active Setup ClickOnce npm NuGet vcpkg Web Platform Installer Windows Installer WiX Windows Package Manager Microsoft Store Category

v t e Microsoft APIs and frameworks Graphics and UI Desktop Window Manager Direct2D Direct3D D3D (extensions) GDI / GDI+ WPF Silverlight WinUI Windows Color System Windows Image Acquisition Windows Imaging Component DirectX Graphics Infrastructure (DXGI) Windows Advanced Rasterization Platform WinG Audio DirectMusic DirectSound XACT Speech API XAudio2 Multimedia DirectX Media Objects Video Acceleration Xinput DirectInput DirectShow Managed DirectX Media Foundation XNA Windows Media Video for Windows Web MSHTML JScript VBScript BHO XDR SideBar Gadgets TypeScript Data access Data Access Components (MDAC) ADO ADO.NET ODBC OLE DB Extensible Storage Engine Entity Framework Sync Framework Access Database Engine MSXML OPC Networking Winsock LSP Winsock Kernel Filtering Platform NDIS Windows Rally BITS P2P API MSMQ DirectPlay Communication Messaging API Telephony API WCF Administration and management Win32 console Windows Script Host WMI (extensions) PowerShell Task Scheduler Offline Files Shadow Copy Windows Installer Error Reporting Event Log Common Log File System Component model COM COM+ ActiveX Distributed Component Object Model .NET Framework Libraries Framework Class Library Microsoft Foundation Classes (MFC) Active Template Library (ATL) Windows Template Library (WTL) Device drivers WDM WDF KMDF UMDF WDDM NDIS UAA VxD Security Crypto API CAPICOM Windows CardSpace Data Protection API Security Support Provider Interface (SSPI) .NET ASP.NET ADO.NET Remoting Silverlight TPL WCF WCS WPF WF Software factories Enterprise Library CCF IPC MSRPC Dynamic Data Exchange (DDE) Remoting WCF Accessibility Active Accessibility UI Automation Text and multilingual support DirectWrite Text Services Framework Text Object Model Input method editor Language Interface Pack Multilingual User Interface Uniscribe

This article related to Microsoft is a stub. You can help Wikipedia by adding missing information.

- [v](https://en.wikipedia.org/wiki/Template:Microsoft-stub)
- [t](/source/Template_talk%3AMicrosoft-stub)
- [e](https://en.wikipedia.org/wiki/Special:EditPage/Template:Microsoft-stub)

---
Adapted from the Wikipedia article [DirectCompute](https://en.wikipedia.org/wiki/DirectCompute) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/DirectCompute?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
