From 351a9ac2ec3aa021740913526b4a5a0ed032a896 Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Wed, 26 Apr 2023 16:20:58 +0200 Subject: [PATCH] rename gl variables for hopeful clarification --- src/gpufont/font.hpp | 250 +++++++++++++++++++++---------------------- 1 file changed, 123 insertions(+), 127 deletions(-) diff --git a/src/gpufont/font.hpp b/src/gpufont/font.hpp index 37dd612..a387aeb 100644 --- a/src/gpufont/font.hpp +++ b/src/gpufont/font.hpp @@ -193,6 +193,27 @@ class Font { float x0, y0, x1, y1, x2, y2; }; + // keep this, just because I wrote it + // though, we don't need it as openGL takes care + // of managing the units + // + //static set gl_texture_units; + //static set gl_texture_units_freed; + + //static int claimFreeTextureName(){ + //if(gl_texture_units_freed.size() == 0){ + //int unit = gl_texture_units.size(); + //gl_texture_units.insert(unit); + //return unit; + //}else{ + //auto unit_it = gl_texture_units_freed.begin(); + //int unit = *unit_it; + //gl_texture_units.insert(unit); + //gl_texture_units_freed.erase(unit_it); + //return unit; + //} + //} + public: struct BufferVertex { float x, y, u, v; @@ -398,9 +419,9 @@ class Font { // Otherwise, worldSize can be an arbitrary floating-point value. Font(FT_Face face, int gpuTextureOffset, float worldSize = 1.0f, bool hinting = false) : face(face), - gpuTextureOffset(gpuTextureOffset), + hinting(hinting), worldSize(worldSize), - hinting(hinting){ + gpuTextureOffset(gpuTextureOffset){ // TODO: modularize init, so we can initialize with settings and text if(hinting){ @@ -419,20 +440,37 @@ class Font { emSize = face->units_per_EM; } + //int amounters = 0; + //for(int i = 0; i < 1024; i++){ + //testGL(10 + i); + //amounters = i + 1; + //cout << "added " << ofToString(amounters) << " LOL" << endl; + //bool hasError = false; + //GLenum err; + //while((err = glGetError()) != GL_NO_ERROR){ + //hasError = true; + //// Process/log the error. + //} + //if(hasError){ + //cout << "WHOOOPS" << endl; + //break; + //} + //} + glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); glGenBuffers(1, &ebo); #ifndef TARGET_OPENGLES - glGenTextures(1, &glyphTexture); - glGenTextures(1, &curveTexture); + glGenTextures(1, &glyphTextureName); + glGenTextures(1, &curveTextureName); - glGenBuffers(1, &glyphBuffer); - glGenBuffers(1, &curveBuffer); + glGenBuffers(1, &glyphBufferName); + glGenBuffers(1, &curveBufferName); #else - glGenTextures(1, &glyphBuffer); - glGenTextures(1, &curveBuffer); + glGenTextures(1, &glyphBufferName); + glGenTextures(1, &curveBufferName); #endif glBindVertexArray(vao); @@ -451,12 +489,12 @@ class Font { uploadBuffers(); #ifndef TARGET_OPENGLES - glBindTexture(GL_TEXTURE_BUFFER, glyphTexture); - glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32I, glyphBuffer); + glBindTexture(GL_TEXTURE_BUFFER, glyphTextureName); + glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32I, glyphBufferName); glBindTexture(GL_TEXTURE_BUFFER, 0); - glBindTexture(GL_TEXTURE_BUFFER, curveTexture); - glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, curveBuffer); + glBindTexture(GL_TEXTURE_BUFFER, curveTextureName); + glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, curveBufferName); glBindTexture(GL_TEXTURE_BUFFER, 0); #else #endif @@ -469,14 +507,14 @@ class Font { glDeleteBuffers(1, &ebo); #ifndef TARGET_OPENGLES - glDeleteTextures(1, &glyphTexture); - glDeleteTextures(1, &curveTexture); + glDeleteTextures(1, &glyphTextureName); + glDeleteTextures(1, &curveTextureName); - glDeleteBuffers(1, &glyphBuffer); - glDeleteBuffers(1, &curveBuffer); + glDeleteBuffers(1, &glyphBufferName); + glDeleteBuffers(1, &curveBufferName); #else - glDeleteTextures(1, &glyphBuffer); - glDeleteTextures(1, &curveBuffer); + glDeleteTextures(1, &glyphBufferName); + glDeleteTextures(1, &curveBufferName); #endif FT_Done_Face(face); @@ -590,24 +628,70 @@ class Font { int calcTextureOffset(){ return gpuTextureOffset * 2; } + void testGL(GLuint texture = 10){ + //int size = 0; + //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size); + //cout << "GL_MAX_TEXTURE_SIZE: " << ofToString(size) << endl; + //size = 0; + //glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, &size); + //cout << "GL_MAX_ARRAY_TEXTURE_LAYERS: " << ofToString(size) << endl; + //size = 0; + + GLsizei width = 16384; + GLsizei height = 1; + GLsizei layerCount = 4; + GLsizei mipLevelCount = 1; + +// Read you texels here. In the current example, we have 2*2*2 = 8 texels, with each texel being 4 GLubytes. + GLubyte texels[layerCount][width * height]; + for(int l = 0; l < layerCount; l++){ + for(int i = 0; i < width * height; i++){ + texels[l][i] = 127; + } + } + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D_ARRAY, texture); +// Allocate the storage. + glTexStorage3D(GL_TEXTURE_2D_ARRAY, mipLevelCount, GL_RGBA8, width, height, layerCount); +// Upload pixel data. +// The first 0 refers to the mipmap level (level 0, since there's only 1) +// The following 2 zeroes refers to the x and y offsets in case you only want to specify a subrectangle. +// The final 0 refers to the layer index offset (we start from index 0 and have 2 levels). +// Altogether you can specify a 3D box subset of the overall texture, but only one mip level at a time. + for(int i = 0; i < (int)layerCount; i++){ + //cout << "OFFSET " << ofToString(i) << endl; + glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, width, height, layerCount, GL_RGBA, GL_UNSIGNED_BYTE, texels[i]); + } + +// Always set reasonable texture parameters + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + } void uploadBuffers(){ //cout << "bufferGlyphs.size(): " << bufferGlyphs.size() << endl; //cout << "bufferCurves.size(): " << bufferCurves.size() << endl; #ifndef TARGET_OPENGLES - glBindBuffer(GL_TEXTURE_BUFFER, glyphBuffer); + glBindBuffer(GL_TEXTURE_BUFFER, glyphBufferName); glBufferData(GL_TEXTURE_BUFFER, sizeof(BufferGlyph) * bufferGlyphs.size(), bufferGlyphs.data(), GL_STATIC_DRAW); glBindBuffer(GL_TEXTURE_BUFFER, 0); - glBindBuffer(GL_TEXTURE_BUFFER, curveBuffer); + glBindBuffer(GL_TEXTURE_BUFFER, curveBufferName); glBufferData(GL_TEXTURE_BUFFER, sizeof(BufferCurve) * bufferCurves.size(), bufferCurves.data(), GL_STATIC_DRAW); glBindBuffer(GL_TEXTURE_BUFFER, 0); #else + int size; + //glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &size); + //cout << "GL_MAX_SHADER_STORAGE_BLOCK_SIZE: " << ofToString(size) << endl; + glyphBufferUnit = 1; + curveBufferUnit = 2; int bufferGlyphsSize = bufferGlyphs.size(); if(!isPowerOfTwo(bufferGlyphsSize)){ bufferGlyphsSize = calculateUpperPowerOfTwo(bufferGlyphsSize); } - glActiveTexture(GL_TEXTURE3 + calcTextureOffset()); - glBindTexture(GL_TEXTURE_2D, glyphBuffer); + glActiveTexture(GL_TEXTURE0 + glyphBufferUnit); + glBindTexture(GL_TEXTURE_2D, glyphBufferName); if(!initializedGlyphsBufferTexture){ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); @@ -633,8 +717,9 @@ class Font { if(!isPowerOfTwo(bufferCurvesSize)){ bufferCurvesSize = calculateUpperPowerOfTwo(bufferCurvesSize); } - glActiveTexture(GL_TEXTURE4 + calcTextureOffset()); - glBindTexture(GL_TEXTURE_2D, curveBuffer); + cout << "bufferCurvesSize: " << ofToString(bufferCurvesSize) << endl; + glActiveTexture(GL_TEXTURE0 + curveBufferUnit); + glBindTexture(GL_TEXTURE_2D, curveBufferName); if(!initializedCurvesBufferTexture){ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); @@ -884,25 +969,25 @@ class Font { location = glGetUniformLocation(program, "curves"); glUniform1i(location, 1); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_BUFFER, glyphTexture); + glBindTexture(GL_TEXTURE_BUFFER, glyphTextureName); glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_BUFFER, curveTexture); + glBindTexture(GL_TEXTURE_BUFFER, curveTextureName); glActiveTexture(GL_TEXTURE0); #else // Important! The GLint for the location needs to stick around // we cannot reuse a location as in Desktop for some reason - glyphBufferLocation = glGetUniformLocation(program, "glyphs"); - glUniform1i(glyphBufferLocation, 3 + calcTextureOffset()); - curveBufferLocation = glGetUniformLocation(program, "curves"); - glUniform1i(curveBufferLocation, 4 + calcTextureOffset()); + glyphsUniformLocation = glGetUniformLocation(program, "glyphs"); + glUniform1i(glyphsUniformLocation, glyphBufferUnit); + curvesUniformLocation = glGetUniformLocation(program, "curves"); + glUniform1i(curvesUniformLocation, curveBufferUnit); - glActiveTexture(GL_TEXTURE3 + calcTextureOffset()); - glBindTexture(GL_TEXTURE_2D, glyphBuffer); + glActiveTexture(GL_TEXTURE0 + glyphBufferUnit); + glBindTexture(GL_TEXTURE_2D, glyphBufferName); - glActiveTexture(GL_TEXTURE4 + calcTextureOffset()); - glBindTexture(GL_TEXTURE_2D, curveBuffer); + glActiveTexture(GL_TEXTURE0 + curveBufferUnit); + glBindTexture(GL_TEXTURE_2D, curveBufferName); glActiveTexture(GL_TEXTURE0); #endif @@ -1085,96 +1170,6 @@ class Font { } } - // NOTE: following only exists for nostalgic reason - // - //void collectVerticesAndIndices(const ofNode & node, - //const std::vector & variationText, - //std::vector & vertices, - //std::vector & indices, - //const glm::vec4 & color = glm::vec4(1), - //const bool vFlip = false, - //const float fontSize_px = 42){ - //float advanceX = 0; - //float advanceY = 0; - - //FT_UInt previous = 0; - //for(const GlyphIdentity & glyphIdentity : variationText){ - //if(glyphIdentity.charcode == '\0'){ - //break; - //} - //float letterFontSize_px = fontSize_px; // can be individual - //const uint32_t & charcode = glyphIdentity.charcode; - - //if(charcode == '\r'){ - //continue; - //} - - //if(charcode == '\n' || charcode == 'n'){ - //advanceX = 0; - //advanceY += ((float)face->height * fontSize_px) / (float)face->units_per_EM * worldSize; - //if(!vFlip){ - //advanceY *= -1; - //} - //if(hinting){ - //advanceY = std::round(advanceY); - //} - //continue; - //} - - //auto glyphIt = glyphs.find(glyphIdentity); - //Glyph & glyph = (glyphIt == glyphs.end()) ? glyphs.begin()->second : glyphIt->second; // in case we have no glyph, draw first one? - //// NOTE: should we do this? - - //if(previous != 0 && glyph.index != 0){ - //FT_Vector kerning; - //FT_Error error = FT_Get_Kerning(face, previous, glyph.index, kerningMode, &kerning); - //if(!error){ - //advanceX += ((float)kerning.x * fontSize_px) / emSize * worldSize; - //} - //} - - //// Do not emit quad for empty glyphs (whitespace). - //if(glyph.curveCount){ - //FT_Pos d = (FT_Pos)(emSize * dilation); - - //float u0 = (float)(glyph.bearingX - d) / emSize; - //float v0 = (float)(glyph.bearingY - glyph.height - d) / emSize; - //float u1 = (float)(glyph.bearingX + glyph.width + d) / emSize; - //float v1 = (float)(glyph.bearingY + d) / emSize; - - //float x0 = advanceX + u0 * worldSize * letterFontSize_px; - //float y0 = v0 * worldSize * letterFontSize_px; - //float x1 = advanceX + u1 * worldSize * letterFontSize_px; - //float y1 = v1 * worldSize * letterFontSize_px; - - //if(vFlip){ - //float _v = v0; - //v0 = v1; - //v1 = _v; - - //y0 = -v0 * worldSize * letterFontSize_px; - //y1 = -v1 * worldSize * letterFontSize_px; - //} - - //glm::vec4 p0 = node.getGlobalTransformMatrix() * glm::vec4(x0, y0 + advanceY, 0, 1); - //glm::vec4 p1 = node.getGlobalTransformMatrix() * glm::vec4(x1, y0 + advanceY, 0, 1); - //glm::vec4 p2 = node.getGlobalTransformMatrix() * glm::vec4(x1, y1 + advanceY, 0, 1); - //glm::vec4 p3 = node.getGlobalTransformMatrix() * glm::vec4(x0, y1 + advanceY, 0, 1); - - //int32_t base = static_cast (vertices.size()); - //vertices.push_back(BufferVertex{p0.x, p0.y, u0, v0, glyph.bufferIndex, color.r, color.g, color.b, color.a}); - //vertices.push_back(BufferVertex{p1.x, p1.y, u1, v0, glyph.bufferIndex, color.r, color.g, color.b, color.a}); - //vertices.push_back(BufferVertex{p2.x, p2.y, u1, v1, glyph.bufferIndex, color.r, color.g, color.b, color.a}); - //vertices.push_back(BufferVertex{p3.x, p3.y, u0, v1, glyph.bufferIndex, color.r, color.g, color.b, color.a}); - - //indices.insert(indices.end(), {base, base + 1, base + 2, base + 2, base + 3, base}); - //} - - //advanceX += ((float)glyph.advance * letterFontSize_px) / emSize * worldSize; - //previous = glyph.index; - //} - //} - void draw(const std::vector & vertices, const std::vector & indices){ glBindVertexArray(vao); @@ -1215,11 +1210,12 @@ class Font { GLuint vao, vbo, ebo; #ifndef TARGET_OPENGLES - GLuint glyphTexture, curveTexture; + GLuint glyphTextureName, curveTextureName; #else - GLint glyphBufferLocation, curveBufferLocation; + GLint glyphsUniformLocation, curvesUniformLocation; #endif - GLuint glyphBuffer, curveBuffer; + GLuint glyphBufferName, curveBufferName; + GLuint glyphBufferUnit, curveBufferUnit; std::vector bufferGlyphs; std::vector bufferCurves;