Handmade Hero»Forums»Code
19 posts
Fonts and textures
I am currently looking at how font rendering is implemented and it seems that we create a texture for each code point.
I have a few questions about this:

1. Is this overkill in some way? When we eventually render the text we have to switch the texture (bind texture) for each character and I thought texture switching is expensive. At least that's what I thought, especially as other projects usually create a single texture font atlas so no texture switching is necessary; I understand if this portion will still get some polish/re-write for production quality code, just was wondering if this is the case.

2. All the textures we create for all the characters are non-power of two. Nowadays this is supported in hardware/OpenGL, but is there still any downside to this? Does the GPU still pad the textures out to power of two and are there any stretching/filtering issues with that?

3. Some code bases I have seen add an offset of 0.5/0.375 when the texture is mapped onto a quad; if this isn't done the textures looked very blurry. We don't do it here on handmade hero and the textures actually look fine. Was this offset fixing some other issue?

4. How would one verify if the texture switching is expensive or not? If this was done on the CPU I could easily benchmark this, but since this is done through OpenGL on the GPU I will only know if I hit my target frame rate or not.

Thanks.
Mārtiņš Možeiko
2559 posts / 2 projects
Fonts and textures
Edited by Mārtiņš Možeiko on
Yes, its an overkill. But if I remember correctly Casey stated that text rendering is only for debug purposes. I assume if he will want to show real text during game, then that will be prebaked into asset files (one sprite for whole text/sentence).

Modern GPU's (anything that supports GL 2.0+ or D3D10+) now handle NPOT textures just fine. Including filtering and mipmapping.

Adding 0.5f is needed for D3D9, because how D3D9 maps texels to screen. It positions integer coordinates at corner of pixel. But usually you want it to be center of pixel. In GL and D3D10+ this is not an issue. This article explains what D3D9 does to texels to map them to pixels: https://msdn.microsoft.com/en-us/...ary/windows/desktop/bb219690.aspx

There are timer queries in OpenGL which let's you measure how much time commands take in GPU. This mechanism is called Query Objects: https://www.opengl.org/wiki/Query_Object
Mārtiņš Možeiko
2559 posts / 2 projects
Fonts and textures
Edited by Mārtiņš Možeiko on
// delete me
19 posts
Fonts and textures
Thanks for clarifying this, Mārtiņš!