Text Management with the Direct3D 12 Plugin



The latest iteration of GeeXLab comes with the management of text in the Direct3D 12 plugin.

This text management allows to create a font from a TTF font (the TTF font must be added into Windows’ fonts folder) and to use that font to draw text where you want on the screen. It’s really simple to use.

The following code snippet shows how to create two font (sizes 24 and 44) with different styles and weights (INIT script):

local max_strings = 16
local font_weight = "bold" -- bold, regular, black, light
local font_sytle = "normal" -- normal, oblique, italic
font24 = gh_renderer.d3d12_font_create("Verdana", 24, font_weight, font_sytle, max_strings)

font_weight = "regular"
font_sytle = "italic"
font44 = gh_renderer.d3d12_font_create("Verdana", 44, font_weight, font_sytle, max_strings)

 
Once the font is created you can directly use it in the INIT script:

gh_renderer.d3d12_font_add_text(font24, 10, 10, 1, 1, 0, 1, "Direct3D 12 Demo")
gh_renderer.d3d12_font_add_text(font44, 10, 30, 1, 0.5, 0, 1, "Triangle of Death")

 
The texts are stored in the font so it’s enough to call d3d12_font_add_text() once. This is great for static texts. If you need dynamic texts, you have to update them in the FRAME script. Dynamic texts use the same function d3d12_font_add_text but require that internal counter to be reset before drawing:

local elapsed_time = gh_utils.get_elapsed_time()

gh_renderer.d3d12_font_reset_all_text_counters()
gh_renderer.d3d12_font_add_text(font24, 10, 10, 1, 1, 0, 1, "Direct3D 12 Demo")
gh_renderer.d3d12_font_add_text(font44, 10, 30, 1, 0.5, 0, 1, "Time: " .. elapsed_time .. "sec.")

 
Font management is based on Direct2D and can have an impact on the framerate especially with lightweight scenes. With scenes with important graphics loads, the font rendering has no impact.

A simple RGB triangle is rendered at around 4500 FPS on a GeForce GTX 960:


Direct3D 12 triangle rendered on a GTX 960

 
If we add some text, the FPS drops at around 2300 FPS:


Direct3D 12 triangle + Direct2D text rendered on a GTX 960

 
But as I said, on heavy scenes, the text drawing does not slow down the rendering.

A demo that shows how to use fonts with the DX12 plugin is available in the host_api/Direct3D12/ folder (02-triangle-of-death-with-text.xml) of the code sample pack.





Leave a Comment

Your email address will not be published. Required fields are marked *