diff --git a/src/LayerComposition.cpp b/src/LayerComposition.cpp index 9e19007..349934b 100644 --- a/src/LayerComposition.cpp +++ b/src/LayerComposition.cpp @@ -241,5 +241,18 @@ void LayerComposition::setVFlip(bool vFlip){ void LayerComposition::setLayerOrder(const vector & layerOrder){ this->layerOrder = layerOrder; } +void LayerComposition::setMsdfUniform1fv(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()->setUniform1f(names[i], values[i]); + } + combo->getShader()->end(); + } + } +} } diff --git a/src/LayerComposition.h b/src/LayerComposition.h index 117b154..ac76ec9 100644 --- a/src/LayerComposition.h +++ b/src/LayerComposition.h @@ -35,8 +35,9 @@ class LayerComposition { shared_ptr getLayer(const LayerID & layerID); const unordered_map > & getAtlasLayerCombos() const; void setVFlip(bool vFlip); - void setLayerOrder(const vector & layerOrder); + void setMsdfUniform1fv(const vector & names, + const vector & values); private: VFlipState vFlipState = V_FLIP_UNKNOWN; unordered_map > layers; diff --git a/src/MsdfAtlasLayerCombo.cpp b/src/MsdfAtlasLayerCombo.cpp index f8d29e4..faffae0 100644 --- a/src/MsdfAtlasLayerCombo.cpp +++ b/src/MsdfAtlasLayerCombo.cpp @@ -6,9 +6,12 @@ #include "of3dGraphics.h" #include "ofFileUtils.h" #include "ofGraphics.h" +#include "ofUtils.h" #include "ofxProfiler.h" -#include -#include +#ifdef TARGET_EMSCRIPTEN + #include + #include +#endif namespace ofxVariableLab { @@ -154,8 +157,8 @@ void MsdfAtlasLayerCombo::draw(bool fade){ for(const auto & layer : layers){ elements += layer->nElements; } - //vertices.resize(elements * 4); - //indices.resize(elements * 6); + vertices.reserve(elements * 4); + indices.reserve(elements * 6); vertices.clear(); indices.clear(); int offset = 0; @@ -230,4 +233,7 @@ const ofImage & MsdfAtlasLayerCombo::getAtlasImage(){ string MsdfAtlasLayerCombo::getAtlasImagePath(){ return atlas->getAtlasPath(); } +shared_ptr MsdfAtlasLayerCombo::getShader(){ + return msdfShader; +} } diff --git a/src/MsdfAtlasLayerCombo.h b/src/MsdfAtlasLayerCombo.h index 288e0dd..5145f30 100644 --- a/src/MsdfAtlasLayerCombo.h +++ b/src/MsdfAtlasLayerCombo.h @@ -3,6 +3,7 @@ #include "AtlasLayerCombo.h" #include "MsdfLayer.h" #include "Utils.h" +#include namespace ofxVariableLab { using BufferVertex = MsdfLayer::BufferVertex; @@ -25,8 +26,8 @@ class MsdfAtlasLayerCombo : public AtlasLayerCombo { void exit(); const ofImage & getAtlasImage(); string getAtlasImagePath(); - shared_ptr getLayer(); // TODO: is this used shared_ptr atlas; + shared_ptr getShader(); private: MsdfAtlasLayerComboSettings settings; diff --git a/src/MsdfLayer.cpp b/src/MsdfLayer.cpp index 6b7bd40..d78e5f2 100644 --- a/src/MsdfLayer.cpp +++ b/src/MsdfLayer.cpp @@ -376,8 +376,8 @@ bool MsdfLayer::collectCharacter(const char character, glm::vec4 p2 = glm::vec4(w, h, 0, 1); glm::vec4 p3 = glm::vec4(w, 0, 0, 1); - vertices.reserve(nodes.size() * 4); - indices.reserve(nodes.size() * 6); + //vertices.reserve(nodes.size() * 4); + //indices.reserve(nodes.size() * 6); for(int i = 0; i < nodes.size(); i++){ const glm::vec4 & color = colors[i]; ofNode & node = nodes[i]; @@ -387,37 +387,42 @@ bool MsdfLayer::collectCharacter(const char character, //glm::vec4 p3 = node.getGlobalTransformMatrix() * glm::vec4(w, 0, 0, 1); glm::mat4 globalTransform = node.getGlobalTransformMatrix(); + //std::lock_guard vm(vertices_mutex); + vertices_mutex.lock(); int32_t base = static_cast (vertices.size()); - vertices.push_back(BufferVertex{ - {p0.x, p0.y, p0.z, p0.w}, - {uv0_a.x, uv0_a.y}, - {uv0_b.x, uv0_b.y}, - {color.r, color.g, color.b, color.a}, - mix, globalTransform - }); - vertices.push_back(BufferVertex{ - {p1.x, p1.y, p1.z, p1.w}, - {uv1_a.x, uv1_a.y}, - {uv1_b.x, uv1_b.y}, - {color.r, color.g, color.b, color.a}, - mix, globalTransform - }); - vertices.push_back(BufferVertex{ - {p2.x, p2.y, p2.z, p2.w}, - {uv2_a.x, uv2_a.y}, - {uv2_b.x, uv2_b.y}, - {color.r, color.g, color.b, color.a}, - mix, globalTransform - }); - vertices.push_back(BufferVertex{ - {p3.x, p3.y, p3.z, p3.w}, - {uv3_a.x, uv3_a.y}, - {uv3_b.x, uv3_b.y}, - {color.r, color.g, color.b, color.a}, - mix, globalTransform + vertices.insert(vertices.end(), { + BufferVertex{ + {p0.x, p0.y, p0.z, p0.w}, + {uv0_a.x, uv0_a.y}, + {uv0_b.x, uv0_b.y}, + {color.r, color.g, color.b, color.a}, + mix, globalTransform + }, + BufferVertex{ + {p1.x, p1.y, p1.z, p1.w}, + {uv1_a.x, uv1_a.y}, + {uv1_b.x, uv1_b.y}, + {color.r, color.g, color.b, color.a}, + mix, globalTransform + }, + BufferVertex{ + {p2.x, p2.y, p2.z, p2.w}, + {uv2_a.x, uv2_a.y}, + {uv2_b.x, uv2_b.y}, + {color.r, color.g, color.b, color.a}, + mix, globalTransform + }, + BufferVertex{ + {p3.x, p3.y, p3.z, p3.w}, + {uv3_a.x, uv3_a.y}, + {uv3_b.x, uv3_b.y}, + {color.r, color.g, color.b, color.a}, + mix, globalTransform + } }); indices.insert(indices.end(), {base, base + 1, base + 2, base + 2, base + 3, base}); nElements++; + vertices_mutex.unlock(); } return true; } diff --git a/src/MsdfLayer.h b/src/MsdfLayer.h index 5bae77c..366abad 100644 --- a/src/MsdfLayer.h +++ b/src/MsdfLayer.h @@ -101,5 +101,7 @@ class MsdfLayer : public Layer { ofNode outerNode; ofNode innerNode; Layer::BoundingBox boundingBox; + std::mutex vertices_mutex; + std::mutex indices_mutex; }; }