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";
#endif
shaderCatalog = std::make_shared <ofxGPUFont::ShaderCatalog>(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 <ofxGPUFont::Font::BufferVertex> vertices;
std::vector <int32_t> indices;
@ -427,17 +435,6 @@ void GPUFontAtlasLayerCombo::draw(int width,
int height,
const shared_ptr <GPUFontLayer> & 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 <ofxGPUFont::Font::BufferVertex> vertices;
std::vector <int32_t> indices;

View file

@ -87,6 +87,7 @@ class GPUFontAtlasLayerCombo : public AtlasLayerCombo {
std::vector <ofxGPUFont::GlyphIdentity> variationText;
bool isDirty = false;
GLuint fontShaderProgram;
private:
int wrapBoundingBoxes(std::vector <ofxGPUFont::Font::BoundingBox> & bbs,
const std::vector <ofxGPUFont::GlyphIdentity> & _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 <ofxGPUFont::Font::FontVariationAxisParameters> fontVariationAxesParameters;
std::vector <ofxGPUFont::Font::FontVariationAxis> fontVariationAxes;
VFlipState vFlip;
int totalCharacters = 0;
int previousWidth = 0, previousHeight;
bool firstRun = true;
};

View file

@ -7,6 +7,7 @@
#include "MsdfLayer.h"
#include "Utils.h"
#include "ofUtils.h"
#include <GLES3/gl3.h>
#include <memory>
namespace ofxVariableLab {
@ -243,6 +244,10 @@ void LayerComposition::setLayerOrder(const vector <LayerID> & layerOrder){
}
void LayerComposition::setMsdfUniform1fv(const vector <string> & names,
const vector <float> & values){
setUniform1fv(names, values);
}
void LayerComposition::setUniform1fv(const vector <string> & names,
const vector <float> & values){
for(const auto & [identifier, layerCombo] : atlasLayerCombos){
if(identifier.type == LayerType::MSDFGEN){
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()->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;
void setVFlip(bool vFlip);
void setLayerOrder(const vector <LayerID> & layerOrder);
[[deprecated("Replaced by setUniform1fv which also covers GPUFont")]]
void setMsdfUniform1fv(const vector <string> & names,
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;
private:
VFlipState vFlipState = V_FLIP_UNKNOWN;