From 1f5b02e947aaedbe859a058e27c7c8badd668c6d Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Tue, 22 Aug 2023 16:18:49 +0200 Subject: [PATCH] setUniform1fv/i for GPUFont and slight optimization for setting uniforms --- src/GPUFontAtlasLayerCombo.cpp | 121 ++++++++++++++++++--------------- src/GPUFontAtlasLayerCombo.h | 4 +- src/LayerComposition.cpp | 45 ++++++++++++ src/LayerComposition.h | 6 ++ 4 files changed, 121 insertions(+), 55 deletions(-) diff --git a/src/GPUFontAtlasLayerCombo.cpp b/src/GPUFontAtlasLayerCombo.cpp index 0035548..802ec90 100644 --- a/src/GPUFontAtlasLayerCombo.cpp +++ b/src/GPUFontAtlasLayerCombo.cpp @@ -22,7 +22,6 @@ void GPUFontAtlasLayerCombo::setup(const ComboIdentifier & identifier, string shaderDir = "data/ofxGPUFont/shaders/GL3"; #endif shaderCatalog = std::make_shared (shaderDir); - //backgroundShader = shaderCatalog->get("background"); if(this->settings.bufferTargetType == GL_TEXTURE_2D_ARRAY){ fontShader = shaderCatalog->get("font_ta"); @@ -160,13 +159,6 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){ glm::vec2 mouse = glm::vec2(ofGetMouseX(), ofGetMouseY()); - //glm::mat4 projection = transform.getProjectionMatrix((float)width / height); - glm::mat4 projection = transform.getOrthoProjectionMatrix(width, - height, - Transform::TOP_LEFT); - //Transform::CENTERED); - glm::mat4 view = transform.getViewMatrix(); - glm::mat4 model = glm::mat4(1.0f); //glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // background color //glClear(GL_COLOR_BUFFER_BIT); // clear background with background color @@ -186,30 +178,46 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){ //enableSuperSamplingAntiAliasing = ofGetMouseY() > ofGetHeight() / 2; if(font){ OFX_PROFILER_SCOPE("draw font"); + fontShaderProgram = fontShader->program; glUseProgram(fontShaderProgram); - font->program = fontShaderProgram; font->drawSetup(); - location = glGetUniformLocation(fontShaderProgram, "projection"); - glUniformMatrix4fv(location, 1, false, glm::value_ptr(projection)); + bool resized = width != previousWidth || height != previousHeight; - location = glGetUniformLocation(fontShaderProgram, "view"); - glUniformMatrix4fv(location, 1, false, glm::value_ptr(view)); - location = glGetUniformLocation(fontShaderProgram, "model"); - glUniformMatrix4fv(location, 1, false, glm::value_ptr(model)); - float z = 0; - location = glGetUniformLocation(fontShaderProgram, "z"); - glUniform1f(location, z); + if(resized){ + glm::mat4 projection = transform.getOrthoProjectionMatrix(width, + height, + Transform::TOP_LEFT); + glm::mat4 view = transform.getViewMatrix(); + glm::mat4 model = glm::mat4(1.0f); - location = glGetUniformLocation(fontShaderProgram, "antiAliasingWindowSize"); - glUniform1f(location, (float)antiAliasingWindowSize); - location = glGetUniformLocation(fontShaderProgram, "enableSuperSamplingAntiAliasing"); - glUniform1i(location, enableSuperSamplingAntiAliasing); - location = glGetUniformLocation(fontShaderProgram, "enableControlPointsVisualization"); - glUniform1i(location, enableControlPointsVisualization); + location = glGetUniformLocation(fontShaderProgram, "projection"); + glUniformMatrix4fv(location, 1, false, glm::value_ptr(projection)); + + location = glGetUniformLocation(fontShaderProgram, "view"); + glUniformMatrix4fv(location, 1, false, glm::value_ptr(view)); + location = glGetUniformLocation(fontShaderProgram, "model"); + glUniformMatrix4fv(location, 1, false, glm::value_ptr(model)); + + previousWidth = width; + previousHeight = height; + } + //float z = 0; + //location = glGetUniformLocation(fontShaderProgram, "z"); + //glUniform1f(location, z); + + if(firstRun){ + location = glGetUniformLocation(fontShaderProgram, "antiAliasingWindowSize"); + glUniform1f(location, (float)antiAliasingWindowSize); + location = glGetUniformLocation(fontShaderProgram, "enableSuperSamplingAntiAliasing"); + glUniform1i(location, enableSuperSamplingAntiAliasing); + location = glGetUniformLocation(fontShaderProgram, "enableControlPointsVisualization"); + glUniform1i(location, enableControlPointsVisualization); + firstRun = false; + } std::vector vertices; std::vector indices; @@ -427,17 +435,6 @@ void GPUFontAtlasLayerCombo::draw(int width, int height, const shared_ptr & layer){ OFX_PROFILER_FUNCTION(); - GLuint location; - - glm::vec2 mouse = glm::vec2(ofGetMouseX(), ofGetMouseY()); - - //glm::mat4 projection = transform.getProjectionMatrix((float)width / height); - glm::mat4 projection = transform.getOrthoProjectionMatrix(width, - height, - Transform::TOP_LEFT); - //Transform::CENTERED); - glm::mat4 view = transform.getViewMatrix(); - glm::mat4 model = glm::mat4(1.0f); // Uses premultiplied-alpha. ofDisableDepthTest(); @@ -452,30 +449,46 @@ void GPUFontAtlasLayerCombo::draw(int width, //enableSuperSamplingAntiAliasing = ofGetMouseY() > ofGetHeight() / 2; if(font){ OFX_PROFILER_SCOPE("draw font"); - fontShaderProgram = fontShader->program; - glUseProgram(fontShaderProgram); + bool resized = width != previousWidth || height != previousHeight; + if(resized){ + GLuint location; - font->program = fontShaderProgram; - font->drawSetup(); + glm::vec2 mouse = glm::vec2(ofGetMouseX(), ofGetMouseY()); - location = glGetUniformLocation(fontShaderProgram, "projection"); - glUniformMatrix4fv(location, 1, false, glm::value_ptr(projection)); + //glm::mat4 projection = transform.getProjectionMatrix((float)width / height); + glm::mat4 projection = transform.getOrthoProjectionMatrix(width, + height, + Transform::TOP_LEFT); + //Transform::CENTERED); + glm::mat4 view = transform.getViewMatrix(); + glm::mat4 model = glm::mat4(1.0f); + fontShaderProgram = fontShader->program; + glUseProgram(fontShaderProgram); - location = glGetUniformLocation(fontShaderProgram, "view"); - glUniformMatrix4fv(location, 1, false, glm::value_ptr(view)); - location = glGetUniformLocation(fontShaderProgram, "model"); - glUniformMatrix4fv(location, 1, false, glm::value_ptr(model)); - float z = 0; - location = glGetUniformLocation(fontShaderProgram, "z"); - glUniform1f(location, z); + font->program = fontShaderProgram; + font->drawSetup(); + location = glGetUniformLocation(fontShaderProgram, "projection"); + glUniformMatrix4fv(location, 1, false, glm::value_ptr(projection)); - location = glGetUniformLocation(fontShaderProgram, "antiAliasingWindowSize"); - glUniform1f(location, (float)antiAliasingWindowSize); - location = glGetUniformLocation(fontShaderProgram, "enableSuperSamplingAntiAliasing"); - glUniform1i(location, enableSuperSamplingAntiAliasing); - location = glGetUniformLocation(fontShaderProgram, "enableControlPointsVisualization"); - glUniform1i(location, enableControlPointsVisualization); + location = glGetUniformLocation(fontShaderProgram, "view"); + glUniformMatrix4fv(location, 1, false, glm::value_ptr(view)); + location = glGetUniformLocation(fontShaderProgram, "model"); + glUniformMatrix4fv(location, 1, false, glm::value_ptr(model)); + } + //float z = 0; + //location = glGetUniformLocation(fontShaderProgram, "z"); + //glUniform1f(location, z); + if(firstRun){ + GLuint location; + location = glGetUniformLocation(fontShaderProgram, "antiAliasingWindowSize"); + glUniform1f(location, (float)antiAliasingWindowSize); + location = glGetUniformLocation(fontShaderProgram, "enableSuperSamplingAntiAliasing"); + glUniform1i(location, enableSuperSamplingAntiAliasing); + location = glGetUniformLocation(fontShaderProgram, "enableControlPointsVisualization"); + glUniform1i(location, enableControlPointsVisualization); + firstRun = false; + } std::vector vertices; std::vector indices; diff --git a/src/GPUFontAtlasLayerCombo.h b/src/GPUFontAtlasLayerCombo.h index a52600b..a09479c 100644 --- a/src/GPUFontAtlasLayerCombo.h +++ b/src/GPUFontAtlasLayerCombo.h @@ -87,6 +87,7 @@ class GPUFontAtlasLayerCombo : public AtlasLayerCombo { std::vector variationText; bool isDirty = false; + GLuint fontShaderProgram; private: int wrapBoundingBoxes(std::vector & bbs, const std::vector & _variationText, @@ -122,13 +123,14 @@ class GPUFontAtlasLayerCombo : public AtlasLayerCombo { bool enableSuperSamplingAntiAliasing = true; // Draw control points for debugging (green - on curve, magenta - off curve). bool enableControlPointsVisualization = false; - GLuint fontShaderProgram; std::vector fontVariationAxesParameters; std::vector fontVariationAxes; VFlipState vFlip; int totalCharacters = 0; + int previousWidth = 0, previousHeight; + bool firstRun = true; }; diff --git a/src/LayerComposition.cpp b/src/LayerComposition.cpp index f32e5c2..c87579c 100644 --- a/src/LayerComposition.cpp +++ b/src/LayerComposition.cpp @@ -7,6 +7,7 @@ #include "MsdfLayer.h" #include "Utils.h" #include "ofUtils.h" +#include #include namespace ofxVariableLab { @@ -243,6 +244,10 @@ void LayerComposition::setLayerOrder(const vector & layerOrder){ } void LayerComposition::setMsdfUniform1fv(const vector & names, const vector & values){ + setUniform1fv(names, values); +} +void LayerComposition::setUniform1fv(const vector & names, + const vector & values){ for(const auto & [identifier, layerCombo] : atlasLayerCombos){ if(identifier.type == LayerType::MSDFGEN){ auto combo = static_pointer_cast (layerCombo); @@ -251,6 +256,46 @@ void LayerComposition::setMsdfUniform1fv(const vector & names, combo->getShader()->setUniform1f(names[i], values[i]); } combo->getShader()->end(); + }else if(identifier.type == LayerType::GPUFONT){ + auto combo = static_pointer_cast (layerCombo); + GLint location; + GLint currentProgram; + glGetIntegerv(GL_CURRENT_PROGRAM, ¤tProgram); + glUseProgram(combo->fontShaderProgram); + + for(int i = 0; i < names.size(); i++){ + cout << "LayerComposition::setUniform1fv " << i << " - " << names[i] << ": " << ofToString(values[i]) << endl; + location = glGetUniformLocation(combo->fontShaderProgram, + names[i].c_str()); + glUniform1f(location, values[i]); + } + glUseProgram(currentProgram); + } + } +} +void LayerComposition::setUniform1iv(const vector & names, + const vector & values){ + for(const auto & [identifier, layerCombo] : atlasLayerCombos){ + if(identifier.type == LayerType::MSDFGEN){ + auto combo = static_pointer_cast (layerCombo); + combo->getShader()->begin(); + for(int i = 0; i < names.size(); i++){ + combo->getShader()->setUniform1i(names[i], values[i]); + } + combo->getShader()->end(); + }else if(identifier.type == LayerType::GPUFONT){ + auto combo = static_pointer_cast (layerCombo); + GLint location; + GLint currentProgram; + glGetIntegerv(GL_CURRENT_PROGRAM, ¤tProgram); + glUseProgram(combo->fontShaderProgram); + + for(int i = 0; i < names.size(); i++){ + location = glGetUniformLocation(combo->fontShaderProgram, + names[i].c_str()); + glUniform1i(location, values[i]); + } + glUseProgram(currentProgram); } } } diff --git a/src/LayerComposition.h b/src/LayerComposition.h index 119d7ad..d77d269 100644 --- a/src/LayerComposition.h +++ b/src/LayerComposition.h @@ -36,8 +36,14 @@ class LayerComposition { const unordered_map > & getAtlasLayerCombos() const; void setVFlip(bool vFlip); void setLayerOrder(const vector & layerOrder); + + [[deprecated("Replaced by setUniform1fv which also covers GPUFont")]] void setMsdfUniform1fv(const vector & names, const vector & values); + void setUniform1fv(const vector & names, + const vector & values); + void setUniform1iv(const vector & names, + const vector & values); const LayerID getNextFreeLayerID() const; private: VFlipState vFlipState = V_FLIP_UNKNOWN;