use uniform buffer

This commit is contained in:
jrkb 2023-06-09 06:57:31 +02:00
parent 1ab9d7b2cc
commit e66e5ae502
4 changed files with 63 additions and 8 deletions

View file

@ -1,6 +1,10 @@
#include "MsdfAtlasLayerCombo.h" #include "MsdfAtlasLayerCombo.h"
#include "AtlasLayerCombo.h" #include "AtlasLayerCombo.h"
#include "MsdfLayer.h"
#include "Utils.h" #include "Utils.h"
#include "ofFileUtils.h"
#include <GL/glext.h>
#include <GLES3/gl3.h>
namespace ofxVariableLab { namespace ofxVariableLab {
@ -20,7 +24,11 @@ void MsdfAtlasLayerCombo::setup(const ComboIdentifier & layerIdentifier,
msdfShader = make_shared <ofShader>(); msdfShader = make_shared <ofShader>();
#ifdef TARGET_EMSCRIPTEN #ifdef TARGET_EMSCRIPTEN
msdfShader->load("ofxMsdfgen/shaders/mix/ES3/shader"); if(ofFile("shaders/ES3/msdf/shader.frag").exists()){
msdfShader->load("shaders/ES3/msdf/shader");
}else{
msdfShader->load("ofxMsdfgen/shaders/mix/ES3/shader");
}
#else #else
msdfShader->load("ofxMsdfgen/shaders/mix/GL3/shader"); msdfShader->load("ofxMsdfgen/shaders/mix/GL3/shader");
#endif #endif
@ -35,6 +43,28 @@ void MsdfAtlasLayerCombo::update(){
atlas->generate(useCache, saveToCache); atlas->generate(useCache, saveToCache);
isDirty = false; isDirty = false;
} }
if(!uboIsSetup){
setupUbo();
}
}
void MsdfAtlasLayerCombo::setupUbo(){
msdfShader->begin();
GLint prog = 0;
glGetIntegerv(GL_CURRENT_PROGRAM, &prog);
msdfShader->end();
glGenBuffers(1, &uboName);
uboIndex = glGetUniformBlockIndex(prog, "ubo");
glBindBufferBase(GL_UNIFORM_BUFFER, 0, uboName);
MsdfLayer::UboParameters up;
glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) * 8, NULL, GL_DYNAMIC_DRAW);
glUniformBlockBinding(prog, uboIndex, 0);
for(const auto & layer : layers){
layer->uboName = uboName;
layer->uboIndex = uboIndex;
}
uboIsSetup = true;
} }
void MsdfAtlasLayerCombo::careForChild(shared_ptr <Layer> layer){ void MsdfAtlasLayerCombo::careForChild(shared_ptr <Layer> layer){
@ -44,6 +74,8 @@ void MsdfAtlasLayerCombo::careForChild(shared_ptr <Layer> layer){
msdfLayer->setDirtyDirty(true); msdfLayer->setDirtyDirty(true);
msdfLayer->setAtlas(this->atlas); msdfLayer->setAtlas(this->atlas);
msdfLayer->setShader(msdfShader); msdfLayer->setShader(msdfShader);
msdfLayer->uboName = uboName;
msdfLayer->uboIndex = uboIndex;
msdfLayer->scaleFactor = scaleFactor; msdfLayer->scaleFactor = scaleFactor;
auto & as = atlas->settings; auto & as = atlas->settings;
for(const char c : layer->getProps().text){ for(const char c : layer->getProps().text){

View file

@ -34,5 +34,10 @@ class MsdfAtlasLayerCombo : public AtlasLayerCombo {
ComboIdentifier identifier; ComboIdentifier identifier;
bool isDirty = false; bool isDirty = false;
float scaleFactor = 1.0; float scaleFactor = 1.0;
GLuint uboName;
GLint uboIndex;
void setupUbo();
bool uboIsSetup = false;
}; };
} }

View file

@ -136,6 +136,7 @@ void MsdfLayer::draw(glm::vec3 position){
//cout << "translation_a: " << ofToString(translation_a) << endl; //cout << "translation_a: " << ofToString(translation_a) << endl;
//cout << "scale_a: " << ofToString(scale_a) << endl; //cout << "scale_a: " << ofToString(scale_a) << endl;
//} //}
//
shader->begin(); shader->begin();
shader->setUniform4f("fontColor", ofFloatColor::yellow); shader->setUniform4f("fontColor", ofFloatColor::yellow);
shader->setUniform2f("translation_a", translation_a); shader->setUniform2f("translation_a", translation_a);
@ -143,6 +144,7 @@ void MsdfLayer::draw(glm::vec3 position){
shader->setUniform2f("translation_b", translation_b); shader->setUniform2f("translation_b", translation_b);
shader->setUniform2f("scale_b", scale_b); shader->setUniform2f("scale_b", scale_b);
shader->setUniform1f("msdf_mix", mix); shader->setUniform1f("msdf_mix", mix);
atlasImage.draw(0, 0, w, h); atlasImage.draw(0, 0, w, h);
shader->end(); shader->end();
ofPopMatrix(); ofPopMatrix();
@ -176,7 +178,7 @@ void MsdfLayer::drawCharacter(const char character,
shader->begin(); shader->begin();
shader->setUniformTexture("msdf", atlasImage.getTexture(), 0); shader->setUniformTexture("msdf", atlasImage.getTexture(), 0);
shader->setUniform2f("unitRange", unitRange); //shader->setUniform2f("unitRange", unitRange);
shader->end(); shader->end();
ofPushMatrix(); ofPushMatrix();
@ -244,12 +246,15 @@ void MsdfLayer::drawCharacter(const char character,
glm::vec2 scale_b = glm::vec2(float(w_b) / float(atlas_w), glm::vec2 scale_b = glm::vec2(float(w_b) / float(atlas_w),
float(h_b) / float(atlas_h)); float(h_b) / float(atlas_h));
shader->begin(); shader->begin();
shader->setUniform4f("fontColor", color); uboParameters.unitRange = unitRange;
shader->setUniform2f("translation_a", translation_a); uboParameters.fontColor = color;
shader->setUniform2f("scale_a", scale_a); uboParameters.translation_a = translation_a;
shader->setUniform2f("translation_b", translation_b); uboParameters.translation_b = translation_b;
shader->setUniform2f("scale_b", scale_b); uboParameters.scale_a = scale_a;
shader->setUniform1f("msdf_mix", mix); uboParameters.scale_b = scale_b;
uboParameters.mix = mix;
glBindBufferBase(GL_UNIFORM_BUFFER, 0, uboName);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(UboParameters), &uboParameters);
ofRotateDeg(rotation, 0, 0, 1); ofRotateDeg(rotation, 0, 0, 1);
atlasImage.draw(0, 0, w * scale, h * scale); atlasImage.draw(0, 0, w * scale, h * scale);
shader->end(); shader->end();

View file

@ -11,6 +11,16 @@ namespace ofxVariableLab {
class MsdfLayer : public Layer { class MsdfLayer : public Layer {
public: public:
struct UboParameters {
glm::vec4 fontColor;
glm::vec4 bgColor;
glm::vec2 unitRange;
glm::vec2 translation_a;
glm::vec2 scale_a;
glm::vec2 translation_b;
glm::vec2 scale_b;
float mix;
};
void setup(const LayerSettings & settings = LayerSettings()) override; void setup(const LayerSettings & settings = LayerSettings()) override;
void update() override; void update() override;
void draw(glm::vec3 position = glm::vec3(0, 0, 0)) override; void draw(glm::vec3 position = glm::vec3(0, 0, 0)) override;
@ -53,6 +63,9 @@ class MsdfLayer : public Layer {
string id = ""; string id = "";
VFlipState vFlip = V_FLIP_UNKNOWN; VFlipState vFlip = V_FLIP_UNKNOWN;
float scaleFactor = 1.0; float scaleFactor = 1.0;
GLuint uboName;
GLint uboIndex;
UboParameters uboParameters;
private: private:
Props lastProps; Props lastProps;