setUniform1fv/i for GPUFont and slight optimization for setting uniforms

This commit is contained in:
jrkb 2023-08-22 16:18:49 +02:00
parent 927a12e51e
commit 1f5b02e947
4 changed files with 121 additions and 55 deletions

View file

@ -22,7 +22,6 @@ void GPUFontAtlasLayerCombo::setup(const ComboIdentifier & identifier,
string shaderDir = "data/ofxGPUFont/shaders/GL3"; string shaderDir = "data/ofxGPUFont/shaders/GL3";
#endif #endif
shaderCatalog = std::make_shared <ofxGPUFont::ShaderCatalog>(shaderDir); shaderCatalog = std::make_shared <ofxGPUFont::ShaderCatalog>(shaderDir);
//backgroundShader = shaderCatalog->get("background");
if(this->settings.bufferTargetType == GL_TEXTURE_2D_ARRAY){ if(this->settings.bufferTargetType == GL_TEXTURE_2D_ARRAY){
fontShader = shaderCatalog->get("font_ta"); fontShader = shaderCatalog->get("font_ta");
@ -160,13 +159,6 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){
glm::vec2 mouse = glm::vec2(ofGetMouseX(), ofGetMouseY()); 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 //glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // background color
//glClear(GL_COLOR_BUFFER_BIT); // clear background with background color //glClear(GL_COLOR_BUFFER_BIT); // clear background with background color
@ -186,12 +178,22 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){
//enableSuperSamplingAntiAliasing = ofGetMouseY() > ofGetHeight() / 2; //enableSuperSamplingAntiAliasing = ofGetMouseY() > ofGetHeight() / 2;
if(font){ if(font){
OFX_PROFILER_SCOPE("draw font"); OFX_PROFILER_SCOPE("draw font");
fontShaderProgram = fontShader->program; fontShaderProgram = fontShader->program;
glUseProgram(fontShaderProgram); glUseProgram(fontShaderProgram);
font->program = fontShaderProgram; font->program = fontShaderProgram;
font->drawSetup(); font->drawSetup();
bool resized = width != previousWidth || height != previousHeight;
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, "projection"); location = glGetUniformLocation(fontShaderProgram, "projection");
glUniformMatrix4fv(location, 1, false, glm::value_ptr(projection)); glUniformMatrix4fv(location, 1, false, glm::value_ptr(projection));
@ -199,17 +201,23 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){
glUniformMatrix4fv(location, 1, false, glm::value_ptr(view)); glUniformMatrix4fv(location, 1, false, glm::value_ptr(view));
location = glGetUniformLocation(fontShaderProgram, "model"); location = glGetUniformLocation(fontShaderProgram, "model");
glUniformMatrix4fv(location, 1, false, glm::value_ptr(model)); glUniformMatrix4fv(location, 1, false, glm::value_ptr(model));
float z = 0;
location = glGetUniformLocation(fontShaderProgram, "z");
glUniform1f(location, z);
previousWidth = width;
previousHeight = height;
}
//float z = 0;
//location = glGetUniformLocation(fontShaderProgram, "z");
//glUniform1f(location, z);
if(firstRun){
location = glGetUniformLocation(fontShaderProgram, "antiAliasingWindowSize"); location = glGetUniformLocation(fontShaderProgram, "antiAliasingWindowSize");
glUniform1f(location, (float)antiAliasingWindowSize); glUniform1f(location, (float)antiAliasingWindowSize);
location = glGetUniformLocation(fontShaderProgram, "enableSuperSamplingAntiAliasing"); location = glGetUniformLocation(fontShaderProgram, "enableSuperSamplingAntiAliasing");
glUniform1i(location, enableSuperSamplingAntiAliasing); glUniform1i(location, enableSuperSamplingAntiAliasing);
location = glGetUniformLocation(fontShaderProgram, "enableControlPointsVisualization"); location = glGetUniformLocation(fontShaderProgram, "enableControlPointsVisualization");
glUniform1i(location, enableControlPointsVisualization); glUniform1i(location, enableControlPointsVisualization);
firstRun = false;
}
std::vector <ofxGPUFont::Font::BufferVertex> vertices; std::vector <ofxGPUFont::Font::BufferVertex> vertices;
std::vector <int32_t> indices; std::vector <int32_t> indices;
@ -427,17 +435,6 @@ void GPUFontAtlasLayerCombo::draw(int width,
int height, int height,
const shared_ptr <GPUFontLayer> & layer){ const shared_ptr <GPUFontLayer> & layer){
OFX_PROFILER_FUNCTION(); 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. // Uses premultiplied-alpha.
ofDisableDepthTest(); ofDisableDepthTest();
@ -452,6 +449,19 @@ void GPUFontAtlasLayerCombo::draw(int width,
//enableSuperSamplingAntiAliasing = ofGetMouseY() > ofGetHeight() / 2; //enableSuperSamplingAntiAliasing = ofGetMouseY() > ofGetHeight() / 2;
if(font){ if(font){
OFX_PROFILER_SCOPE("draw font"); OFX_PROFILER_SCOPE("draw font");
bool resized = width != previousWidth || height != previousHeight;
if(resized){
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);
fontShaderProgram = fontShader->program; fontShaderProgram = fontShader->program;
glUseProgram(fontShaderProgram); glUseProgram(fontShaderProgram);
@ -465,17 +475,20 @@ void GPUFontAtlasLayerCombo::draw(int width,
glUniformMatrix4fv(location, 1, false, glm::value_ptr(view)); glUniformMatrix4fv(location, 1, false, glm::value_ptr(view));
location = glGetUniformLocation(fontShaderProgram, "model"); location = glGetUniformLocation(fontShaderProgram, "model");
glUniformMatrix4fv(location, 1, false, glm::value_ptr(model)); glUniformMatrix4fv(location, 1, false, glm::value_ptr(model));
float z = 0; }
location = glGetUniformLocation(fontShaderProgram, "z"); //float z = 0;
glUniform1f(location, z); //location = glGetUniformLocation(fontShaderProgram, "z");
//glUniform1f(location, z);
if(firstRun){
GLuint location;
location = glGetUniformLocation(fontShaderProgram, "antiAliasingWindowSize"); location = glGetUniformLocation(fontShaderProgram, "antiAliasingWindowSize");
glUniform1f(location, (float)antiAliasingWindowSize); glUniform1f(location, (float)antiAliasingWindowSize);
location = glGetUniformLocation(fontShaderProgram, "enableSuperSamplingAntiAliasing"); location = glGetUniformLocation(fontShaderProgram, "enableSuperSamplingAntiAliasing");
glUniform1i(location, enableSuperSamplingAntiAliasing); glUniform1i(location, enableSuperSamplingAntiAliasing);
location = glGetUniformLocation(fontShaderProgram, "enableControlPointsVisualization"); location = glGetUniformLocation(fontShaderProgram, "enableControlPointsVisualization");
glUniform1i(location, enableControlPointsVisualization); glUniform1i(location, enableControlPointsVisualization);
firstRun = false;
}
std::vector <ofxGPUFont::Font::BufferVertex> vertices; std::vector <ofxGPUFont::Font::BufferVertex> vertices;
std::vector <int32_t> indices; std::vector <int32_t> indices;

View file

@ -87,6 +87,7 @@ class GPUFontAtlasLayerCombo : public AtlasLayerCombo {
std::vector <ofxGPUFont::GlyphIdentity> variationText; std::vector <ofxGPUFont::GlyphIdentity> variationText;
bool isDirty = false; bool isDirty = false;
GLuint fontShaderProgram;
private: private:
int wrapBoundingBoxes(std::vector <ofxGPUFont::Font::BoundingBox> & bbs, int wrapBoundingBoxes(std::vector <ofxGPUFont::Font::BoundingBox> & bbs,
const std::vector <ofxGPUFont::GlyphIdentity> & _variationText, const std::vector <ofxGPUFont::GlyphIdentity> & _variationText,
@ -122,13 +123,14 @@ class GPUFontAtlasLayerCombo : public AtlasLayerCombo {
bool enableSuperSamplingAntiAliasing = true; bool enableSuperSamplingAntiAliasing = true;
// Draw control points for debugging (green - on curve, magenta - off curve). // Draw control points for debugging (green - on curve, magenta - off curve).
bool enableControlPointsVisualization = false; bool enableControlPointsVisualization = false;
GLuint fontShaderProgram;
std::vector <ofxGPUFont::Font::FontVariationAxisParameters> fontVariationAxesParameters; std::vector <ofxGPUFont::Font::FontVariationAxisParameters> fontVariationAxesParameters;
std::vector <ofxGPUFont::Font::FontVariationAxis> fontVariationAxes; std::vector <ofxGPUFont::Font::FontVariationAxis> fontVariationAxes;
VFlipState vFlip; VFlipState vFlip;
int totalCharacters = 0; int totalCharacters = 0;
int previousWidth = 0, previousHeight;
bool firstRun = true;
}; };

View file

@ -7,6 +7,7 @@
#include "MsdfLayer.h" #include "MsdfLayer.h"
#include "Utils.h" #include "Utils.h"
#include "ofUtils.h" #include "ofUtils.h"
#include <GLES3/gl3.h>
#include <memory> #include <memory>
namespace ofxVariableLab { namespace ofxVariableLab {
@ -243,6 +244,10 @@ void LayerComposition::setLayerOrder(const vector <LayerID> & layerOrder){
} }
void LayerComposition::setMsdfUniform1fv(const vector <string> & names, void LayerComposition::setMsdfUniform1fv(const vector <string> & names,
const vector <float> & values){ const vector <float> & values){
setUniform1fv(names, values);
}
void LayerComposition::setUniform1fv(const vector <string> & names,
const vector <float> & values){
for(const auto & [identifier, layerCombo] : atlasLayerCombos){ for(const auto & [identifier, layerCombo] : atlasLayerCombos){
if(identifier.type == LayerType::MSDFGEN){ if(identifier.type == LayerType::MSDFGEN){
auto combo = static_pointer_cast <MsdfAtlasLayerCombo>(layerCombo); auto combo = static_pointer_cast <MsdfAtlasLayerCombo>(layerCombo);
@ -251,6 +256,46 @@ void LayerComposition::setMsdfUniform1fv(const vector <string> & names,
combo->getShader()->setUniform1f(names[i], values[i]); combo->getShader()->setUniform1f(names[i], values[i]);
} }
combo->getShader()->end(); combo->getShader()->end();
}else if(identifier.type == LayerType::GPUFONT){
auto combo = static_pointer_cast <GPUFontAtlasLayerCombo>(layerCombo);
GLint location;
GLint currentProgram;
glGetIntegerv(GL_CURRENT_PROGRAM, &currentProgram);
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 <string> & names,
const vector <int> & values){
for(const auto & [identifier, layerCombo] : atlasLayerCombos){
if(identifier.type == LayerType::MSDFGEN){
auto combo = static_pointer_cast <MsdfAtlasLayerCombo>(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 <GPUFontAtlasLayerCombo>(layerCombo);
GLint location;
GLint currentProgram;
glGetIntegerv(GL_CURRENT_PROGRAM, &currentProgram);
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);
} }
} }
} }

View file

@ -36,8 +36,14 @@ class LayerComposition {
const unordered_map <ComboIdentifier, shared_ptr <AtlasLayerCombo> > & getAtlasLayerCombos() const; const unordered_map <ComboIdentifier, shared_ptr <AtlasLayerCombo> > & getAtlasLayerCombos() const;
void setVFlip(bool vFlip); void setVFlip(bool vFlip);
void setLayerOrder(const vector <LayerID> & layerOrder); void setLayerOrder(const vector <LayerID> & layerOrder);
[[deprecated("Replaced by setUniform1fv which also covers GPUFont")]]
void setMsdfUniform1fv(const vector <string> & names, void setMsdfUniform1fv(const vector <string> & names,
const vector <float> & values); const vector <float> & values);
void setUniform1fv(const vector <string> & names,
const vector <float> & values);
void setUniform1iv(const vector <string> & names,
const vector <int> & values);
const LayerID getNextFreeLayerID() const; const LayerID getNextFreeLayerID() const;
private: private:
VFlipState vFlipState = V_FLIP_UNKNOWN; VFlipState vFlipState = V_FLIP_UNKNOWN;