Drawing text in DirectX 12 is incredibly difficult compared to earlier versions.
I present the refactored text rendering code from DirectX Tool Kit (aka DirectXTK12).
I chose this ToolKit because I think it is the most popular among beginners, and my code can be useful to more people.
Everything unnecessary has been removed, connections have been "straightened" to flat code, all options have been removed, asynchrony has been removed, only for Windows. Headers only, no global variables, no external dependencies, macro-free. Tested for memory and resource leaks.
The depth of the call stack is no more than two, so as not to get lost.
Many variable names have been retained and many old comments remain; this will help people who know ToolKit navigate the code.
There is usage.cpp
in the repository as a complete and working example of use.
The example code is also chosen to be as simple as possible; this is the minimum possible set of calls to launch shaders.
The author of the code in which text drawing is built in is PrzemyslawZaworski, thanks to him.
The usage consists of two calls:
- To initialize a context object:
auto ctx = Plain::fontWorks( mDevice, mCommandQueue );
- To display text in a frame using context:
auto holder = Plain::drawWorks( ctx, text );
the returned result of drawWorks
must be saved until the current drawing cycle completes.
The text can be displayed in the desired coordinates and selected color,
deeper one call: you can change the scale and all those things that the DrawString
function from DirectXTK12 provided.
A hardcoded font of the Arial family is used, size 28, generated via DirectXTK12\MakeSpriteFont.exe
, included as a header with a data array.
I like to display the FPS data on screen with my rendered image
The text in the example outputs FPS as stated in the question.
Here is the entire project in the repo
https://github.com/Alex0vSky/MinimalDx12DrawText
Great for educational purposes due to its flatness and simplicity.
I wonder how complex DirectX 13 will become...