From 61ae05c00378d355ac69c2ae95c3d5d3a3a547a9 Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Mon, 3 Apr 2023 16:07:18 +0200 Subject: [PATCH] the power of two --- example/src/gpufont/font.hpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/example/src/gpufont/font.hpp b/example/src/gpufont/font.hpp index 0e6673c..9a1df70 100644 --- a/example/src/gpufont/font.hpp +++ b/example/src/gpufont/font.hpp @@ -17,6 +17,22 @@ #define F16DOT16_TO_DOUBLE(x) (1 / 65536. * double(x)) #define DOUBLE_TO_F16DOT16(x) FT_Fixed(65536. * x) +inline int32_t calculateUpperPowerOfTwo(int32_t v){ + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v++; + return v; +} + +inline bool isPowerOfTwo(int i){ + return (i & (i - 1)) == 0; +} + + class Font { struct Glyph { FT_UInt index; @@ -369,10 +385,15 @@ class Font { GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + int bufferGlyphsSize = bufferGlyphs.size(); + if(!isPowerOfTwo(bufferGlyphsSize)){ + bufferGlyphsSize = calculateUpperPowerOfTwo(bufferGlyphsSize); + } glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32I, - bufferGlyphs.size(), + bufferGlyphsSize, 1, 0, GL_RG_INTEGER, @@ -412,6 +433,10 @@ class Font { //GL_INT, //data); + int bufferCurvesSize = bufferCurves.size(); + if(!isPowerOfTwo(bufferCurvesSize)){ + bufferCurvesSize = calculateUpperPowerOfTwo(bufferCurvesSize); + } glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, curveBuffer); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -423,7 +448,7 @@ class Font { glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32F, - bufferCurves.size(), + bufferCurvesSize, 1, 0, GL_RG,