DirectXTK
The DirectX Tool Kit (aka DirectXTK) is a collection of helper classes for writing DirectX 11.x code in C++
Top Related Projects
DirectXTex texture processing library
DirectXMath is an all inline SIMD C++ linear algebra library for use in games and graphics apps
Cross-platform, graphics API agnostic, "Bring Your Own Engine/Framework" style rendering library.
minimal cross-platform standalone C headers
Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
Quick Overview
DirectXTK (DirectX Tool Kit) is a collection of helper classes for writing DirectX 11 and DirectX 12 code in C++. It provides a set of utility classes and functions to simplify common tasks in DirectX programming, such as sprite rendering, text rendering, and basic geometry creation.
Pros
- Simplifies DirectX programming by providing high-level abstractions
- Supports both DirectX 11 and DirectX 12
- Well-documented with extensive tutorials and code samples
- Actively maintained by Microsoft
Cons
- Limited to Windows platforms
- May introduce some overhead compared to raw DirectX API calls
- Learning curve for developers new to DirectX concepts
- Some advanced DirectX features may not be fully exposed
Code Examples
- Creating a sprite batch and rendering a sprite:
SpriteBatch* spriteBatch;
Texture2D* texture;
// In initialization
spriteBatch = new SpriteBatch(d3dContext);
CreateWICTextureFromFile(d3dDevice, L"texture.png", nullptr, &texture);
// In render loop
spriteBatch->Begin();
spriteBatch->Draw(texture, XMFLOAT2(100, 100));
spriteBatch->End();
- Creating and drawing simple geometry:
GeometricPrimitive* cube;
// In initialization
cube = GeometricPrimitive::CreateCube(d3dContext);
// In render loop
cube->Draw(world, view, projection, Colors::White, texture);
- Rendering text:
SpriteFont* font;
SpriteBatch* spriteBatch;
// In initialization
font = new SpriteFont(d3dDevice, L"myFont.spritefont");
spriteBatch = new SpriteBatch(d3dContext);
// In render loop
spriteBatch->Begin();
font->DrawString(spriteBatch, L"Hello, DirectXTK!", XMFLOAT2(10, 10), Colors::White);
spriteBatch->End();
Getting Started
-
Add the DirectXTK NuGet package to your Visual Studio project:
PM> Install-Package Microsoft.DirectXTK
-
Include the necessary headers in your code:
#include <SpriteBatch.h> #include <SpriteFont.h> #include <GeometricPrimitive.h>
-
Initialize DirectXTK objects in your application's setup code:
auto device = // ... your D3D device auto context = // ... your D3D context m_spriteBatch = std::make_unique<SpriteBatch>(context); m_font = std::make_unique<SpriteFont>(device, L"myFont.spritefont"); m_cube = GeometricPrimitive::CreateCube(context);
-
Use DirectXTK objects in your render loop as shown in the code examples above.
Competitor Comparisons
DirectXTex texture processing library
Pros of DirectXTex
- Specialized in texture processing and image manipulation
- Supports a wide range of texture formats and compression methods
- Provides advanced texture loading, saving, and conversion capabilities
Cons of DirectXTex
- More focused scope compared to DirectXTK's broader utility set
- May require additional libraries for non-texture-related tasks
- Steeper learning curve for developers not familiar with texture processing
Code Comparison
DirectXTex (Texture loading):
DirectX::ScratchImage image;
HRESULT hr = DirectX::LoadFromWICFile(L"texture.png", DirectX::WIC_FLAGS_NONE, nullptr, image);
DirectXTK (Sprite rendering):
std::unique_ptr<SpriteBatch> spriteBatch;
spriteBatch = std::make_unique<SpriteBatch>(d3dContext);
spriteBatch->Begin();
spriteBatch->Draw(texture, position, nullptr, Colors::White);
spriteBatch->End();
DirectXTex focuses on texture manipulation, while DirectXTK provides a broader set of utilities for game development. DirectXTex excels in texture processing tasks, offering more specialized functionality in that area. DirectXTK, on the other hand, provides a wider range of tools for game development, including sprite rendering, input handling, and audio support. The choice between the two depends on the specific needs of your project, with DirectXTex being more suitable for texture-heavy applications and DirectXTK for general game development tasks.
DirectXMath is an all inline SIMD C++ linear algebra library for use in games and graphics apps
Pros of DirectXMath
- Focused on low-level math operations, providing optimized SIMD implementations
- Lightweight and header-only library, easy to integrate into projects
- Offers a wide range of mathematical functions for 3D graphics programming
Cons of DirectXMath
- Limited to math operations, lacking higher-level graphics functionality
- Requires more manual work to implement complex rendering techniques
- Less beginner-friendly compared to DirectXTK's more comprehensive toolkit
Code Comparison
DirectXMath:
XMVECTOR v1 = XMLoadFloat3(&float3);
XMVECTOR v2 = XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
XMVECTOR result = XMVectorAdd(v1, v2);
DirectXTK:
SimpleMath::Vector3 v1(float3);
SimpleMath::Vector3 v2(1.0f, 2.0f, 3.0f);
SimpleMath::Vector3 result = v1 + v2;
DirectXMath focuses on low-level SIMD operations, while DirectXTK provides a more intuitive, object-oriented approach with its SimpleMath wrapper. DirectXTK builds upon DirectXMath, offering additional utilities and abstractions for game development, making it more suitable for higher-level graphics programming and rapid prototyping.
Cross-platform, graphics API agnostic, "Bring Your Own Engine/Framework" style rendering library.
Pros of bgfx
- Cross-platform support (Windows, macOS, Linux, iOS, Android, web browsers)
- Supports multiple rendering APIs (DirectX, OpenGL, Vulkan, Metal)
- More low-level control and flexibility for advanced graphics programming
Cons of bgfx
- Steeper learning curve and more complex API
- Less documentation and examples compared to DirectXTK
- Smaller community and ecosystem
Code Comparison
DirectXTK (Simple sprite rendering):
spriteBatch->Begin();
spriteBatch->Draw(texture.Get(), XMFLOAT2(x, y));
spriteBatch->End();
bgfx (Simple sprite rendering):
bgfx::setTexture(0, s_texColor, texture);
bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
screenSpaceQuad((float)width, (float)height, texelHalf, caps->originBottomLeft);
bgfx::submit(0, program);
Summary
DirectXTK is a higher-level toolkit specifically for DirectX development on Windows, offering easier integration and use for DirectX-based projects. bgfx provides a more versatile, cross-platform solution with support for multiple rendering APIs, but at the cost of increased complexity and a steeper learning curve.
minimal cross-platform standalone C headers
Pros of sokol
- Cross-platform support (Windows, macOS, Linux, iOS, Android, web)
- Lightweight and minimal API design
- Easy integration with various build systems and IDEs
Cons of sokol
- Less comprehensive feature set compared to DirectXTK
- Smaller community and ecosystem
- Limited documentation and examples
Code Comparison
DirectXTK (C++):
#include <d3d11.h>
#include <DirectXTK/SpriteBatch.h>
SpriteBatch* spriteBatch;
spriteBatch = new SpriteBatch(d3dContext);
spriteBatch->Begin();
spriteBatch->Draw(texture, position, nullptr, Colors::White);
spriteBatch->End();
sokol (C):
#include "sokol_gfx.h"
#include "sokol_app.h"
sg_pass_action pass_action = {0};
sg_begin_default_pass(&pass_action, sapp_width(), sapp_height());
sg_apply_pipeline(pip);
sg_apply_bindings(&bind);
sg_draw(0, 3, 1);
sg_end_pass();
DirectXTK is a comprehensive toolkit for DirectX development, offering a wide range of features and utilities specifically for Windows platforms. It provides robust support for DirectX 11 and 12, with extensive documentation and examples.
sokol, on the other hand, is a lightweight, cross-platform graphics API that aims for simplicity and ease of use. It supports multiple backends (including DirectX, OpenGL, and Metal) and can be easily integrated into various projects and build systems.
Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
Pros of ImGui
- Lightweight and easy to integrate into existing projects
- Highly customizable and flexible for creating immediate mode GUIs
- Cross-platform compatibility with support for multiple rendering backends
Cons of ImGui
- Limited built-in controls compared to DirectXTK's more comprehensive toolkit
- Requires more manual management of UI state and layout
- Less integrated with DirectX-specific features and optimizations
Code Comparison
ImGui example:
ImGui::Begin("My Window");
if (ImGui::Button("Click me!"))
doSomething();
ImGui::End();
DirectXTK example:
auto button = std::make_unique<Button>();
button->SetText(L"Click me!");
button->AddClickHandler([](Button&) { doSomething(); });
Summary
ImGui is a lightweight, flexible GUI library that excels in creating quick, customizable interfaces across platforms. It's ideal for developers who need a simple, immediate mode GUI solution. DirectXTK, on the other hand, provides a more comprehensive toolkit specifically tailored for DirectX development, offering a wider range of pre-built controls and deeper integration with DirectX features. The choice between the two depends on the project's specific requirements, development environment, and the desired level of DirectX integration.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
DirectX Tool Kit for DirectX 11
http://go.microsoft.com/fwlink/?LinkId=248929
Copyright (c) Microsoft Corporation.
September 4, 2024
This package contains the "DirectX Tool Kit", a collection of helper classes for writing Direct3D 11 C++ code for Universal Windows Platform (UWP) apps for Windows 11, Windows 10, Xbox One, and Win32 desktop applications for Windows 7 Service Pack 1 or later.
This code is designed to build with Visual Studio 2019 (16.11), Visual Studio 2022, clang for Windows v12 or later, or MinGW 12.2. Use of the Windows 10 May 2020 Update SDK (19041) or later is required for Visual Studio.
These components are designed to work without requiring any content from the legacy DirectX SDK. For details, see Where is the DirectX SDK?.
Directory Layout
-
Inc\
-
Public Header Files (in the DirectX C++ namespace):
- Audio.h - low-level audio API using XAudio2 (DirectXTK for Audio public header)
- BufferHelpers.h - C++ helpers for creating D3D resources from CPU data
- CommonStates.h - factory providing commonly used D3D state objects
- DDSTextureLoader.h - light-weight DDS file texture loader
- DirectXHelpers.h - misc C++ helpers for D3D programming
- Effects.h - set of built-in shaders for common rendering tasks
- GamePad.h - gamepad controller helper using XInput, Windows.Gaming.Input, or GameInput
- GeometricPrimitive.h - draws basic shapes such as cubes and spheres
- GraphicsMemory.h - helper for managing dynamic graphics memory allocation
- Keyboard.h - keyboard state tracking helper
- Model.h - draws meshes loaded from .CMO, .SDKMESH, or .VBO files
- Mouse.h - mouse helper
- PostProcess.h - set of built-in shaders for common post-processing operations
- PrimitiveBatch.h - simple and efficient way to draw user primitives
- ScreenGrab.h - light-weight screen shot saver
- SimpleMath.h - simplified C++ wrapper for DirectXMath
- SpriteBatch.h - simple & efficient 2D sprite rendering
- SpriteFont.h - bitmap based text rendering
- VertexTypes.h - structures for commonly used vertex data formats
- WICTextureLoader.h - WIC-based image file texture loader
- XboxDDSTextureLoader.h - Xbox One exclusive apps variant of DDSTextureLoader
-
-
Src\
- DirectXTK source files and internal implementation headers
-
Audio\
- DirectXTK for Audio source files and internal implementation headers
-
MakeSpriteFont\
- Command line tool used to generate binary resources for use with SpriteFont
-
XWBTool\
- Command line tool for building XACT-style wave banks for use with DirectXTK for Audio's WaveBank class
-
build\
- Contains YAML files for the build pipelines along with some miscellaneous build files and scripts.
Documentation
Documentation is available on the GitHub wiki.
Notices
All content and source code for this package are subject to the terms of the MIT License.
For the latest version of DirectXTK, bug reports, etc. please visit the project site on GitHub.
Release Notes
FOR SECURITY ADVISORIES, see GitHub.
For a full change history, see CHANGELOG.md.
-
Starting with the February 2023 release, the Mouse class implementation of relative mouse movement was updated to accumulate changes between calls to
GetState
. By default, each time you callGetState
the deltas are reset which works for scenarios where you use relative movement but only call the method once per frame. If you call it more than once per frame, then add an explicit call toEndOfInputFrame
to use an explicit reset model instead. -
As of the September 2022 release, the library makes use of C++11 inline namespaces for differing types that have the same names in the DirectX 11 and DirectX 12 version of the DirectX Tool Kit. This provides a link-unique name such as
DirectX::DX11::SpriteBatch
that will appear in linker output messages. In most use cases, however, there is no need to add explicitDX11
namespace resolution in client code. -
Starting with the July 2022 release, the
bool forceSRGB
parameter for DDSTextureLoaderEx
functions is now aDDS_LOADER_FLAGS
typed enum bitmask flag parameter. This may have a breaking change impact to client code. Replacetrue
withDDS_LOADER_FORCE_SRGB
andfalse
withDDS_LOADER_DEFAULT
. -
As of the October 2021 release, the DGSLEffect no longer directly supports skinning. Instead, make use of SkinnedDGSLEffect which is derived from DGSLEffect.
-
Starting with the June 2020 release, this library makes use of typed enum bitmask flags per the recommendation of the C++ Standard section 17.5.2.1.3 Bitmask types. This may have breaking change impacts to client code:
-
You cannot pass the
0
literal as your flags value. Instead you must make use of the appropriate default enum value:AudioEngine_Default
,SoundEffectInstance_Default
,ModelLoader_Clockwise
, orWIC_LOADER_DEFAULT
. -
Use the enum type instead of
DWORD
if building up flags values locally with bitmask operations. For example,WIC_LOADER_FLAGS flags = WIC_LOADER_DEFAULT; if (...) flags |= WIC_LOADER_FORCE_SRGB;
-
-
The UWP projects and the Win10 classic desktop project include configurations for the ARM64 platform. Building these requires installing the ARM64 toolset.
-
When using clang/LLVM for the ARM64 platform, the Windows 11 SDK (22000) or later is required.
-
The
CompileShaders.cmd
script must have Windows-style (CRLF) line-endings. If it is changed to Linux-style (LF) line-endings, it can fail to build all the required shaders. -
Xbox One support for DirectX 11 requires the legacy Xbox One XDK. See February 2023 or earlier releases of DirectX Tool Kit for the required project files.
Support
For questions, consider using Stack Overflow with the directxtk tag, or the DirectX Discord Server in the dx9-dx11-developers channel.
For bug reports and feature requests, please use GitHub issues for this project.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
Tests for new features should also be submitted as a PR to the Test Suite repository.
Code of Conduct
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Trademarks
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.
Credits
The DirectX Tool Kit is the work of Shawn Hargreaves and Chuck Walbourn, with contributions from Aaron Rodriguez Hernandez and Dani Roman.
Thanks to Shanon Drone for the SDKMESH file format.
Thanks to Adrian Tsai for the geodesic sphere implementation.
Thanks to Garrett Serack for his help in creating the NuGet packages for DirectX Tool Kit.
Thanks to Roberto Sonnino for his help with the CMO
, DGSL rendering, and the VS Starter Kit animation.
Thanks to Pete Lewis and Justin Saunders for the normal-mapped and PBR shaders implementation.
Thanks to Andrew Farrier and Scott Matloff for their on-going help with code reviews.
Top Related Projects
DirectXTex texture processing library
DirectXMath is an all inline SIMD C++ linear algebra library for use in games and graphics apps
Cross-platform, graphics API agnostic, "Bring Your Own Engine/Framework" style rendering library.
minimal cross-platform standalone C headers
Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot