diff --git a/android/src/main/cpp/graphics/objects/LineGroup2dOpenGl.cpp b/android/src/main/cpp/graphics/objects/LineGroup2dOpenGl.cpp index 44c770186..74bea30b4 100644 --- a/android/src/main/cpp/graphics/objects/LineGroup2dOpenGl.cpp +++ b/android/src/main/cpp/graphics/objects/LineGroup2dOpenGl.cpp @@ -60,12 +60,16 @@ void LineGroup2dOpenGl::setup(const std::shared_ptr<::RenderingContextInterface> segmentStartLPosHandle = glGetAttribLocation(program, "vSegmentStartLPos"); styleInfoHandle = glGetAttribLocation(program, "vStyleInfo"); - glGenBuffers(1, &vertexAttribBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &vertexAttribBuffer); + } glBindBuffer(GL_ARRAY_BUFFER, vertexAttribBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * lineAttributes.size(), &lineAttributes[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); - glGenBuffers(1, &indexBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &indexBuffer); + } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * lineIndices.size(), &lineIndices[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); @@ -74,6 +78,7 @@ void LineGroup2dOpenGl::setup(const std::shared_ptr<::RenderingContextInterface> scaleFactorHandle = glGetUniformLocation(program, "scaleFactor"); ready = true; + glDataBuffersGenerated = true; } void LineGroup2dOpenGl::clear() { @@ -85,8 +90,11 @@ void LineGroup2dOpenGl::clear() { } void LineGroup2dOpenGl::removeGlBuffers() { - glDeleteBuffers(1, &vertexAttribBuffer); - glDeleteBuffers(1, &indexBuffer); + if (glDataBuffersGenerated) { + glDeleteBuffers(1, &vertexAttribBuffer); + glDeleteBuffers(1, &indexBuffer); + glDataBuffersGenerated = false; + } } void LineGroup2dOpenGl::setIsInverseMasked(bool inversed) { isMaskInversed = inversed; } diff --git a/android/src/main/cpp/graphics/objects/LineGroup2dOpenGl.h b/android/src/main/cpp/graphics/objects/LineGroup2dOpenGl.h index 92556e931..6b395f775 100644 --- a/android/src/main/cpp/graphics/objects/LineGroup2dOpenGl.h +++ b/android/src/main/cpp/graphics/objects/LineGroup2dOpenGl.h @@ -64,10 +64,11 @@ class LineGroup2dOpenGl : public GraphicsObjectInterface, int vertexIndexHandle; int segmentStartLPosHandle; int styleInfoHandle; - GLuint vertexAttribBuffer; + GLuint vertexAttribBuffer = -1; std::vector lineAttributes; - GLuint indexBuffer; + GLuint indexBuffer = -1; std::vector lineIndices; + bool glDataBuffersGenerated = false; bool ready = false; bool dataReady = false; diff --git a/android/src/main/cpp/graphics/objects/Polygon2dOpenGl.cpp b/android/src/main/cpp/graphics/objects/Polygon2dOpenGl.cpp index 4957cd60e..c1d566edf 100644 --- a/android/src/main/cpp/graphics/objects/Polygon2dOpenGl.cpp +++ b/android/src/main/cpp/graphics/objects/Polygon2dOpenGl.cpp @@ -61,13 +61,17 @@ void Polygon2dOpenGl::prepareGlData(int program) { glUseProgram(program); positionHandle = glGetAttribLocation(program, "vPosition"); - glGenBuffers(1, &vertexBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &vertexBuffer); + } glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), &vertices[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); - glGenBuffers(1, &indexBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &indexBuffer); + } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * indices.size(), &indices[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); @@ -84,8 +88,11 @@ void Polygon2dOpenGl::clear() { } void Polygon2dOpenGl::removeGlBuffers() { - glDeleteBuffers(1, &vertexBuffer); - glDeleteBuffers(1, &indexBuffer); + if (glDataBuffersGenerated) { + glDeleteBuffers(1, &vertexBuffer); + glDeleteBuffers(1, &indexBuffer); + glDataBuffersGenerated = false; + } } void Polygon2dOpenGl::setIsInverseMasked(bool inversed) { isMaskInversed = inversed; } diff --git a/android/src/main/cpp/graphics/objects/Polygon2dOpenGl.h b/android/src/main/cpp/graphics/objects/Polygon2dOpenGl.h index 3517902d2..233faaa46 100644 --- a/android/src/main/cpp/graphics/objects/Polygon2dOpenGl.h +++ b/android/src/main/cpp/graphics/objects/Polygon2dOpenGl.h @@ -66,6 +66,8 @@ class Polygon2dOpenGl : public GraphicsObjectInterface, std::vector vertices; GLuint indexBuffer; std::vector indices; + bool glDataBuffersGenerated = false; + bool dataReady = false; bool ready = false; diff --git a/android/src/main/cpp/graphics/objects/PolygonGroup2dOpenGl.cpp b/android/src/main/cpp/graphics/objects/PolygonGroup2dOpenGl.cpp index fa9f982c1..7c886aaf0 100644 --- a/android/src/main/cpp/graphics/objects/PolygonGroup2dOpenGl.cpp +++ b/android/src/main/cpp/graphics/objects/PolygonGroup2dOpenGl.cpp @@ -55,12 +55,16 @@ void PolygonGroup2dOpenGl::setup(const std::shared_ptr<::RenderingContextInterfa positionHandle = glGetAttribLocation(program, "vPosition"); styleIndexHandle = glGetAttribLocation(program, "vStyleIndex"); - glGenBuffers(1, &attribBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &attribBuffer); + } glBindBuffer(GL_ARRAY_BUFFER, attribBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * polygonAttributes.size(), &polygonAttributes[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); - glGenBuffers(1, &indexBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &indexBuffer); + } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * polygonIndices.size(), &polygonIndices[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); @@ -68,6 +72,7 @@ void PolygonGroup2dOpenGl::setup(const std::shared_ptr<::RenderingContextInterfa mvpMatrixHandle = glGetUniformLocation(program, "uMVPMatrix"); ready = true; + glDataBuffersGenerated = true; } void PolygonGroup2dOpenGl::clear() { @@ -79,8 +84,11 @@ void PolygonGroup2dOpenGl::clear() { } void PolygonGroup2dOpenGl::removeGlBuffers() { - glDeleteBuffers(1, &attribBuffer); - glDeleteBuffers(1, &indexBuffer); + if (glDataBuffersGenerated) { + glDeleteBuffers(1, &attribBuffer); + glDeleteBuffers(1, &indexBuffer); + glDataBuffersGenerated = false; + } } void PolygonGroup2dOpenGl::setIsInverseMasked(bool inversed) { isMaskInversed = inversed; } diff --git a/android/src/main/cpp/graphics/objects/PolygonGroup2dOpenGl.h b/android/src/main/cpp/graphics/objects/PolygonGroup2dOpenGl.h index af6c1ac6a..be59ea9ba 100644 --- a/android/src/main/cpp/graphics/objects/PolygonGroup2dOpenGl.h +++ b/android/src/main/cpp/graphics/objects/PolygonGroup2dOpenGl.h @@ -59,10 +59,11 @@ class PolygonGroup2dOpenGl : public GraphicsObjectInterface, int mvpMatrixHandle; int positionHandle; int styleIndexHandle; - GLuint attribBuffer; + GLuint attribBuffer = -1; std::vector polygonAttributes; - GLuint indexBuffer; + GLuint indexBuffer = -1; std::vector polygonIndices; + bool glDataBuffersGenerated = false; bool dataReady = false; bool ready = false; diff --git a/android/src/main/cpp/graphics/objects/PolygonPatternGroup2dOpenGl.cpp b/android/src/main/cpp/graphics/objects/PolygonPatternGroup2dOpenGl.cpp index 11082728c..5806483b8 100644 --- a/android/src/main/cpp/graphics/objects/PolygonPatternGroup2dOpenGl.cpp +++ b/android/src/main/cpp/graphics/objects/PolygonPatternGroup2dOpenGl.cpp @@ -66,13 +66,17 @@ void PolygonPatternGroup2dOpenGl::prepareGlData(int program) { positionHandle = glGetAttribLocation(program, "vPosition"); styleIndexHandle = glGetAttribLocation(program, "vStyleIndex"); - glGenBuffers(1, &vertexBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &vertexBuffer); + } glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), &vertices[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); - glGenBuffers(1, &indexBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &indexBuffer); + } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * indices.size(), &indices[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); @@ -92,8 +96,11 @@ void PolygonPatternGroup2dOpenGl::clear() { } void PolygonPatternGroup2dOpenGl::removeGlBuffers() { - glDeleteBuffers(1, &vertexBuffer); - glDeleteBuffers(1, &indexBuffer); + if (glDataBuffersGenerated) { + glDeleteBuffers(1, &vertexBuffer); + glDeleteBuffers(1, &indexBuffer); + glDataBuffersGenerated = false; + } } void PolygonPatternGroup2dOpenGl::loadTexture(const std::shared_ptr<::RenderingContextInterface> &context, diff --git a/android/src/main/cpp/graphics/objects/PolygonPatternGroup2dOpenGl.h b/android/src/main/cpp/graphics/objects/PolygonPatternGroup2dOpenGl.h index 9b8f22c9b..328c31295 100644 --- a/android/src/main/cpp/graphics/objects/PolygonPatternGroup2dOpenGl.h +++ b/android/src/main/cpp/graphics/objects/PolygonPatternGroup2dOpenGl.h @@ -81,6 +81,7 @@ class PolygonPatternGroup2dOpenGl : public GraphicsObjectInterface, std::vector vertices; GLuint indexBuffer; std::vector indices; + bool glDataBuffersGenerated = false; std::shared_ptr textureHolder; int texturePointer; diff --git a/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.cpp b/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.cpp index d8e1bf8c1..05abb60c2 100644 --- a/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.cpp +++ b/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.cpp @@ -81,29 +81,36 @@ void Quad2dInstancedOpenGl::prepareGlData(int program) { glUseProgram(program); positionHandle = glGetAttribLocation(program, "vPosition"); - glGenBuffers(1, &vertexBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &vertexBuffer); + } glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), &vertices[0], GL_STATIC_DRAW); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &dynamicInstanceDataBuffer); + } + glBindBuffer(GL_ARRAY_BUFFER, dynamicInstanceDataBuffer); + glBufferData(GL_ARRAY_BUFFER, instanceCount * instValuesSizeBytes, nullptr, GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); - glGenBuffers(1, &indexBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &indexBuffer); + } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * indices.size(), &indices[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); instPositionsHandle = glGetAttribLocation(program, "aPosition"); - glGenBuffers(1, &positionsBuffer); instRotationsHandle = glGetAttribLocation(program, "aRotation"); - glGenBuffers(1, &rotationsBuffer); instTextureCoordinatesHandle = glGetAttribLocation(program, "aTexCoordinate"); - glGenBuffers(1, &textureCoordinatesListBuffer); instScalesHandle = glGetAttribLocation(program, "aScale"); - glGenBuffers(1, &scalesBuffer); instAlphasHandle = glGetAttribLocation(program, "aAlpha"); - glGenBuffers(1, &alphasBuffer); mvpMatrixHandle = glGetUniformLocation(program, "uMVPMatrix"); + + glDataBuffersGenerated = true; } void Quad2dInstancedOpenGl::prepareTextureCoordsGlData(int program) { @@ -128,14 +135,12 @@ void Quad2dInstancedOpenGl::prepareTextureCoordsGlData(int program) { } void Quad2dInstancedOpenGl::removeGlBuffers() { - glDeleteBuffers(1, &vertexBuffer); - glDeleteBuffers(1, &indexBuffer); - - glDeleteBuffers(1, &positionsBuffer); - glDeleteBuffers(1, &alphasBuffer); - glDeleteBuffers(1, &scalesBuffer); - glDeleteBuffers(1, &textureCoordinatesListBuffer); - glDeleteBuffers(1, &rotationsBuffer); + if (glDataBuffersGenerated) { + glDeleteBuffers(1, &vertexBuffer); + glDeleteBuffers(1, &indexBuffer); + glDeleteBuffers(1, &dynamicInstanceDataBuffer); + glDataBuffersGenerated = false; + } } void Quad2dInstancedOpenGl::removeTextureCoordsGlBuffers() { @@ -217,24 +222,20 @@ void Quad2dInstancedOpenGl::render(const std::shared_ptr<::RenderingContextInter glUniform2f(textureFactorHandle, factorWidth, factorHeight); } - glBindBuffer(GL_ARRAY_BUFFER, positionsBuffer); - glVertexAttribPointer(instPositionsHandle, 2, GL_FLOAT, GL_FALSE, 0, nullptr); + glBindBuffer(GL_ARRAY_BUFFER, dynamicInstanceDataBuffer); + glVertexAttribPointer(instPositionsHandle, 2, GL_FLOAT, GL_FALSE, 0, (float *)(instPositionsOffsetBytes * instanceCount)); glEnableVertexAttribArray(instPositionsHandle); glVertexAttribDivisor(instPositionsHandle, 1); - glBindBuffer(GL_ARRAY_BUFFER, rotationsBuffer); - glVertexAttribPointer(instRotationsHandle, 1, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(instRotationsHandle, 1, GL_FLOAT, GL_FALSE, 0, (float *)(instRotationsOffsetBytes * instanceCount)); glEnableVertexAttribArray(instRotationsHandle); glVertexAttribDivisor(instRotationsHandle, 1); - glBindBuffer(GL_ARRAY_BUFFER, textureCoordinatesListBuffer); - glVertexAttribPointer(instTextureCoordinatesHandle, 4, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(instTextureCoordinatesHandle, 4, GL_FLOAT, GL_FALSE, 0, (float *)(instTextureCoordinatesOffsetBytes * instanceCount)); glEnableVertexAttribArray(instTextureCoordinatesHandle); glVertexAttribDivisor(instTextureCoordinatesHandle, 1); - glBindBuffer(GL_ARRAY_BUFFER, scalesBuffer); - glVertexAttribPointer(instScalesHandle, 2, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(instScalesHandle, 2, GL_FLOAT, GL_FALSE, 0, (float *)(instScalesOffsetBytes * instanceCount)); glEnableVertexAttribArray(instScalesHandle); glVertexAttribDivisor(instScalesHandle, 1); - glBindBuffer(GL_ARRAY_BUFFER, alphasBuffer); - glVertexAttribPointer(instAlphasHandle, 1, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(instAlphasHandle, 1, GL_FLOAT, GL_FALSE, 0, (float *)(instAlphasOffsetBytes * instanceCount)); glEnableVertexAttribArray(instAlphasHandle); glVertexAttribDivisor(instAlphasHandle, 1); @@ -298,46 +299,46 @@ void Quad2dInstancedOpenGl::setInstanceCount(int count) { void Quad2dInstancedOpenGl::setPositions(const SharedBytes &positions) { std::lock_guard lock(dataMutex); - if (writeToBuffer(positions, positionsBuffer)) { + if (writeToDynamicInstanceDataBuffer(positions, instPositionsOffsetBytes)) { buffersNotReady &= ~(1); } } void Quad2dInstancedOpenGl::setRotations(const SharedBytes &rotations) { std::lock_guard lock(dataMutex); - if (writeToBuffer(rotations, rotationsBuffer)) { + if (writeToDynamicInstanceDataBuffer(rotations, instRotationsOffsetBytes)) { buffersNotReady &= ~(1 << 1); } } void Quad2dInstancedOpenGl::setScales(const SharedBytes &scales) { std::lock_guard lock(dataMutex); - if (writeToBuffer(scales, scalesBuffer)) { + if (writeToDynamicInstanceDataBuffer(scales, instScalesOffsetBytes)) { buffersNotReady &= ~(1 << 2); } } void Quad2dInstancedOpenGl::setTextureCoordinates(const SharedBytes &textureCoordinates) { std::lock_guard lock(dataMutex); - if (writeToBuffer(textureCoordinates, textureCoordinatesListBuffer)) { + if (writeToDynamicInstanceDataBuffer(textureCoordinates, instTextureCoordinatesOffsetBytes)) { buffersNotReady &= ~(1 << 3); } } void Quad2dInstancedOpenGl::setAlphas(const SharedBytes &values) { std::lock_guard lock(dataMutex); - if (writeToBuffer(values, alphasBuffer)) { + if (writeToDynamicInstanceDataBuffer(values, instAlphasOffsetBytes)) { buffersNotReady &= ~(1 << 4); } } -bool Quad2dInstancedOpenGl::writeToBuffer(const ::SharedBytes &data, GLuint target) { +bool Quad2dInstancedOpenGl::writeToDynamicInstanceDataBuffer(const ::SharedBytes &data, int targetOffsetBytes) { if(!ready){ // Writing to buffer before it was created return false; } - glBindBuffer(GL_ARRAY_BUFFER, target); - glBufferData(GL_ARRAY_BUFFER, data.elementCount * data.bytesPerElement, (void *) data.address, GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, dynamicInstanceDataBuffer); + glBufferSubData(GL_ARRAY_BUFFER, targetOffsetBytes * instanceCount, data.elementCount * data.bytesPerElement, (void *) data.address); glBindBuffer(GL_ARRAY_BUFFER, 0); return true; } diff --git a/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.h b/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.h index b17378c56..f3b65a076 100644 --- a/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.h +++ b/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.h @@ -94,6 +94,7 @@ class Quad2dInstancedOpenGl : public GraphicsObjectInterface, std::vector textureCoords; GLuint indexBuffer; std::vector indices; + bool glDataBuffersGenerated = false; std::shared_ptr textureHolder; int texturePointer; @@ -114,18 +115,21 @@ class Quad2dInstancedOpenGl : public GraphicsObjectInterface, int instanceCount = 0; + GLuint dynamicInstanceDataBuffer; int instPositionsHandle; - GLuint positionsBuffer; int instRotationsHandle; - GLuint rotationsBuffer; int instScalesHandle; - GLuint scalesBuffer; int instAlphasHandle; - GLuint alphasBuffer; int instTextureCoordinatesHandle; - GLuint textureCoordinatesListBuffer; + + static const int instPositionsOffsetBytes = sizeof(GLfloat) * 0; + static const int instRotationsOffsetBytes = sizeof(GLfloat) * 2; + static const int instTextureCoordinatesOffsetBytes = sizeof(GLfloat) * 3; + static const int instScalesOffsetBytes = sizeof(GLfloat) * 7; + static const int instAlphasOffsetBytes = sizeof(GLfloat) * 9; + static const int instValuesSizeBytes = sizeof(GLfloat) * 10; private: - bool writeToBuffer(const ::SharedBytes &data, GLuint target); + bool writeToDynamicInstanceDataBuffer(const ::SharedBytes &data, int targetOffsetBytes); }; diff --git a/android/src/main/cpp/graphics/objects/Quad2dOpenGl.cpp b/android/src/main/cpp/graphics/objects/Quad2dOpenGl.cpp index baf135b08..a9ada46c5 100644 --- a/android/src/main/cpp/graphics/objects/Quad2dOpenGl.cpp +++ b/android/src/main/cpp/graphics/objects/Quad2dOpenGl.cpp @@ -122,9 +122,11 @@ void Quad2dOpenGl::prepareTextureCoordsGlData(int program) { } void Quad2dOpenGl::removeGlBuffers() { - glDeleteBuffers(1, &vertexBuffer); - glDeleteBuffers(1, &indexBuffer); - glDataBuffersGenerated = false; + if (glDataBuffersGenerated) { + glDeleteBuffers(1, &vertexBuffer); + glDeleteBuffers(1, &indexBuffer); + glDataBuffersGenerated = false; + } } void Quad2dOpenGl::removeTextureCoordsGlBuffers() { diff --git a/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.cpp b/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.cpp index 029d04647..8312584c3 100644 --- a/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.cpp +++ b/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.cpp @@ -82,31 +82,35 @@ void Quad2dStretchedInstancedOpenGl::prepareGlData(int program) { glUseProgram(program); positionHandle = glGetAttribLocation(program, "vPosition"); - glGenBuffers(1, &vertexBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &vertexBuffer); + } glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), &vertices[0], GL_STATIC_DRAW); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &dynamicInstanceDataBuffer); + } + glBindBuffer(GL_ARRAY_BUFFER, dynamicInstanceDataBuffer); + glBufferData(GL_ARRAY_BUFFER, instanceCount * instValuesSizeBytes, nullptr, GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); - glGenBuffers(1, &indexBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &indexBuffer); + } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * indices.size(), &indices[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); instPositionsHandle = glGetAttribLocation(program, "aPosition"); - glGenBuffers(1, &positionsBuffer); instTextureCoordinatesHandle = glGetAttribLocation(program, "aTexCoordinate"); - glGenBuffers(1, &textureCoordinatesListBuffer); instScalesHandle = glGetAttribLocation(program, "aScale"); - glGenBuffers(1, &scalesBuffer); instRotationsHandle = glGetAttribLocation(program, "aRotation"); - glGenBuffers(1, &rotationsBuffer); instAlphasHandle = glGetAttribLocation(program, "aAlpha"); - glGenBuffers(1, &alphasBuffer); instStretchScalesHandle = glGetAttribLocation(program, "aStretchScales"); instStretchXsHandle = glGetAttribLocation(program, "aStretchX"); instStretchYsHandle = glGetAttribLocation(program, "aStretchY"); - glGenBuffers(1, &stretchInfoBuffer); mvpMatrixHandle = glGetUniformLocation(program, "uMVPMatrix"); } @@ -133,15 +137,12 @@ void Quad2dStretchedInstancedOpenGl::prepareTextureCoordsGlData(int program) { } void Quad2dStretchedInstancedOpenGl::removeGlBuffers() { - glDeleteBuffers(1, &vertexBuffer); - glDeleteBuffers(1, &indexBuffer); - - glDeleteBuffers(1, &positionsBuffer); - glDeleteBuffers(1, &alphasBuffer); - glDeleteBuffers(1, &scalesBuffer); - glDeleteBuffers(1, &textureCoordinatesListBuffer); - glDeleteBuffers(1, &rotationsBuffer); - glDeleteBuffers(1, &stretchInfoBuffer); + if (glDataBuffersGenerated) { + glDeleteBuffers(1, &vertexBuffer); + glDeleteBuffers(1, &indexBuffer); + glDeleteBuffers(1, &dynamicInstanceDataBuffer); + glDataBuffersGenerated = false; + } } void Quad2dStretchedInstancedOpenGl::removeTextureCoordsGlBuffers() { @@ -220,43 +221,32 @@ void Quad2dStretchedInstancedOpenGl::render(const std::shared_ptr<::RenderingCon glUniform2f(textureFactorHandle, factorWidth, factorHeight); } - glBindBuffer(GL_ARRAY_BUFFER, positionsBuffer); - glVertexAttribPointer(instPositionsHandle, 2, GL_FLOAT, GL_FALSE, 0, nullptr); + glBindBuffer(GL_ARRAY_BUFFER, dynamicInstanceDataBuffer); + glVertexAttribPointer(instPositionsHandle, 2, GL_FLOAT, GL_FALSE, 0, (float *)(instPositionsOffsetBytes * instanceCount)); glEnableVertexAttribArray(instPositionsHandle); glVertexAttribDivisor(instPositionsHandle, 1); - - glBindBuffer(GL_ARRAY_BUFFER, textureCoordinatesListBuffer); - glVertexAttribPointer(instTextureCoordinatesHandle, 4, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(instTextureCoordinatesHandle, 4, GL_FLOAT, GL_FALSE, 0, (float *)(instTextureCoordinatesOffsetBytes * instanceCount)); glEnableVertexAttribArray(instTextureCoordinatesHandle); glVertexAttribDivisor(instTextureCoordinatesHandle, 1); - - glBindBuffer(GL_ARRAY_BUFFER, scalesBuffer); - glVertexAttribPointer(instScalesHandle, 2, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(instScalesHandle, 2, GL_FLOAT, GL_FALSE, 0, (float *)(instScalesOffsetBytes * instanceCount)); glEnableVertexAttribArray(instScalesHandle); glVertexAttribDivisor(instScalesHandle, 1); - - glBindBuffer(GL_ARRAY_BUFFER, rotationsBuffer); - glVertexAttribPointer(instRotationsHandle, 1, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(instRotationsHandle, 1, GL_FLOAT, GL_FALSE, 0, (float *)(instRotationsOffsetBytes * instanceCount)); glEnableVertexAttribArray(instRotationsHandle); glVertexAttribDivisor(instRotationsHandle, 1); - - glBindBuffer(GL_ARRAY_BUFFER, alphasBuffer); - glVertexAttribPointer(instAlphasHandle, 1, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(instAlphasHandle, 1, GL_FLOAT, GL_FALSE, 0, (float *)(instAlphasOffsetBytes * instanceCount)); glEnableVertexAttribArray(instAlphasHandle); glVertexAttribDivisor(instAlphasHandle, 1); - - glBindBuffer(GL_ARRAY_BUFFER, stretchInfoBuffer); - glVertexAttribPointer(instStretchScalesHandle, 2, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), nullptr); + glVertexAttribPointer(instStretchScalesHandle, 2, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (float *)(instStretchInfoOffsetBytes * instanceCount)); glEnableVertexAttribArray(instStretchScalesHandle); glVertexAttribDivisor(instStretchScalesHandle, 1); - glVertexAttribPointer(instStretchXsHandle, 4, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (float*) (2 * sizeof(GLfloat))); + glVertexAttribPointer(instStretchXsHandle, 4, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (float *)(instStretchInfoOffsetBytes * instanceCount + instStretchXsAddOffsetBytes)); glEnableVertexAttribArray(instStretchXsHandle); glVertexAttribDivisor(instStretchXsHandle, 1); - glVertexAttribPointer(instStretchYsHandle, 4, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (float*) (6 * sizeof(GLfloat))); + glVertexAttribPointer(instStretchYsHandle, 4, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (float *)(instStretchInfoOffsetBytes * instanceCount + instStretchYsAddOffsetBytes)); glEnableVertexAttribArray(instStretchYsHandle); glVertexAttribDivisor(instStretchYsHandle, 1); - shaderProgram->preRender(context); // enable vPosition attribs @@ -284,8 +274,6 @@ void Quad2dStretchedInstancedOpenGl::render(const std::shared_ptr<::RenderingCon glVertexAttribDivisor(instStretchXsHandle, 0); glVertexAttribDivisor(instStretchYsHandle, 0); - - // Disable vertex array glDisableVertexAttribArray(positionHandle); if (textureHolder) { @@ -327,54 +315,53 @@ void Quad2dStretchedInstancedOpenGl::setInstanceCount(int count) { void Quad2dStretchedInstancedOpenGl::setPositions(const SharedBytes &positions) { std::lock_guard lock(dataMutex); - if (writeToBuffer(positions, positionsBuffer)) { + if (writeToDynamicInstanceDataBuffer(positions, instPositionsOffsetBytes)) { buffersNotReady &= ~(1); } } void Quad2dStretchedInstancedOpenGl::setRotations(const SharedBytes &rotations) { std::lock_guard lock(dataMutex); - if (writeToBuffer(rotations, rotationsBuffer)) { + if (writeToDynamicInstanceDataBuffer(rotations, instRotationsOffsetBytes)) { buffersNotReady &= ~(1 << 1); } } void Quad2dStretchedInstancedOpenGl::setScales(const SharedBytes &scales) { std::lock_guard lock(dataMutex); - if (writeToBuffer(scales, scalesBuffer)) { + if (writeToDynamicInstanceDataBuffer(scales, instScalesOffsetBytes)) { buffersNotReady &= ~(1 << 2); } } void Quad2dStretchedInstancedOpenGl::setTextureCoordinates(const SharedBytes &textureCoordinates) { std::lock_guard lock(dataMutex); - if (writeToBuffer(textureCoordinates, textureCoordinatesListBuffer)) { + if (writeToDynamicInstanceDataBuffer(textureCoordinates, instTextureCoordinatesOffsetBytes)) { buffersNotReady &= ~(1 << 3); } } void Quad2dStretchedInstancedOpenGl::setAlphas(const SharedBytes &values) { std::lock_guard lock(dataMutex); - if (writeToBuffer(values, alphasBuffer)) { + if (writeToDynamicInstanceDataBuffer(values, instAlphasOffsetBytes)) { buffersNotReady &= ~(1 << 4); } } void Quad2dStretchedInstancedOpenGl::setStretchInfos(const ::SharedBytes &values) { std::lock_guard lock(dataMutex); - if (writeToBuffer(values, stretchInfoBuffer)) { + if (writeToDynamicInstanceDataBuffer(values, instStretchInfoOffsetBytes)) { buffersNotReady &= ~(1 << 5); } } -bool Quad2dStretchedInstancedOpenGl::writeToBuffer(const ::SharedBytes &data, GLuint target) { +bool Quad2dStretchedInstancedOpenGl::writeToDynamicInstanceDataBuffer(const ::SharedBytes &data, GLuint targetOffsetBytes) { if(!ready){ // Writing to buffer before it was created return false; } - - glBindBuffer(GL_ARRAY_BUFFER, target); - glBufferData(GL_ARRAY_BUFFER, data.elementCount * data.bytesPerElement, (void *) data.address, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, dynamicInstanceDataBuffer); + glBufferSubData(GL_ARRAY_BUFFER, targetOffsetBytes * instanceCount, data.elementCount * data.bytesPerElement, (void *) data.address); glBindBuffer(GL_ARRAY_BUFFER, 0); return true; } diff --git a/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.h b/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.h index 00b5f92fa..0dd167ffe 100644 --- a/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.h +++ b/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.h @@ -96,6 +96,7 @@ class Quad2dStretchedInstancedOpenGl : public GraphicsObjectInterface, std::vector textureCoords; GLuint indexBuffer; std::vector indices; + bool glDataBuffersGenerated = false; std::shared_ptr textureHolder; int texturePointer; @@ -116,21 +117,26 @@ class Quad2dStretchedInstancedOpenGl : public GraphicsObjectInterface, int instanceCount = 0; + GLuint dynamicInstanceDataBuffer; int instPositionsHandle; - GLuint positionsBuffer; int instRotationsHandle; - GLuint rotationsBuffer; int instScalesHandle; - GLuint scalesBuffer; int instAlphasHandle; - GLuint alphasBuffer; int instStretchScalesHandle; int instStretchXsHandle; int instStretchYsHandle; - GLuint stretchInfoBuffer; int instTextureCoordinatesHandle; - GLuint textureCoordinatesListBuffer; + + static const int instPositionsOffsetBytes = sizeof(GLfloat) * 0; + static const int instTextureCoordinatesOffsetBytes = sizeof(GLfloat) * 2; + static const int instScalesOffsetBytes = sizeof(GLfloat) * 6; + static const int instRotationsOffsetBytes = sizeof(GLfloat) * 8; + static const int instAlphasOffsetBytes = sizeof(GLfloat) * 9; + static const int instStretchInfoOffsetBytes = sizeof(GLfloat) * 10; + static const int instStretchXsAddOffsetBytes = sizeof(GLfloat) * 2; + static const int instStretchYsAddOffsetBytes = sizeof(GLfloat) * 6; + static const int instValuesSizeBytes = sizeof(GLfloat) * 20; private: - bool writeToBuffer(const ::SharedBytes &data, GLuint target); + bool writeToDynamicInstanceDataBuffer(const ::SharedBytes &data, GLuint targetOffsetBytes); }; diff --git a/android/src/main/cpp/graphics/objects/Text2dInstancedOpenGl.cpp b/android/src/main/cpp/graphics/objects/Text2dInstancedOpenGl.cpp index 7e8b9b7d0..411f69ccc 100644 --- a/android/src/main/cpp/graphics/objects/Text2dInstancedOpenGl.cpp +++ b/android/src/main/cpp/graphics/objects/Text2dInstancedOpenGl.cpp @@ -79,30 +79,34 @@ void Text2dInstancedOpenGl::prepareGlData(int program) { glUseProgram(program); positionHandle = glGetAttribLocation(program, "vPosition"); - glGenBuffers(1, &vertexBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &vertexBuffer); + } glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), &vertices[0], GL_STATIC_DRAW); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &dynamicInstanceDataBuffer); + glGenBuffers(1, &styleBuffer); + } + glBindBuffer(GL_ARRAY_BUFFER, dynamicInstanceDataBuffer); + glBufferData(GL_ARRAY_BUFFER, instanceCount * instValuesSizeBytes, nullptr, GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); - glGenBuffers(1, &indexBuffer); + if (!glDataBuffersGenerated) { + glGenBuffers(1, &indexBuffer); + } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * indices.size(), &indices[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); instPositionsHandle = glGetAttribLocation(program, "aPosition"); - glGenBuffers(1, &positionsBuffer); instTextureCoordinatesHandle = glGetAttribLocation(program, "aTexCoordinate"); - glGenBuffers(1, &textureCoordinatesListBuffer); instScalesHandle = glGetAttribLocation(program, "aScale"); - glGenBuffers(1, &scalesBuffer); instRotationsHandle = glGetAttribLocation(program, "aRotation"); - glGenBuffers(1, &rotationsBuffer); instStyleIndicesHandle = glGetAttribLocation(program, "aStyleIndex"); - glGenBuffers(1, &styleIndicesBuffer); styleBufferHandle = glGetProgramResourceIndex(program, GL_SHADER_STORAGE_BLOCK, "textInstancedStyleBuffer"); - glGenBuffers(1, &styleBuffer); - glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -131,15 +135,13 @@ void Text2dInstancedOpenGl::prepareTextureCoordsGlData(int program) { } void Text2dInstancedOpenGl::removeGlBuffers() { - glDeleteBuffers(1, &vertexBuffer); - glDeleteBuffers(1, &indexBuffer); - - glDeleteBuffers(1, &positionsBuffer); - glDeleteBuffers(1, &styleIndicesBuffer); - glDeleteBuffers(1, &scalesBuffer); - glDeleteBuffers(1, &textureCoordinatesListBuffer); - glDeleteBuffers(1, &rotationsBuffer); - glDeleteBuffers(1, &styleBuffer); + if (glDataBuffersGenerated) { + glDeleteBuffers(1, &vertexBuffer); + glDeleteBuffers(1, &indexBuffer); + glDeleteBuffers(1, &dynamicInstanceDataBuffer); + glDeleteBuffers(1, &styleBuffer); + glDataBuffersGenerated = false; + } } void Text2dInstancedOpenGl::removeTextureCoordsGlBuffers() { @@ -214,28 +216,20 @@ void Text2dInstancedOpenGl::render(const std::shared_ptr<::RenderingContextInter glUniform2f(textureFactorHandle, factorWidth, factorHeight); } - glBindBuffer(GL_ARRAY_BUFFER, positionsBuffer); - glVertexAttribPointer(instPositionsHandle, 2, GL_FLOAT, GL_FALSE, 0, nullptr); + glBindBuffer(GL_ARRAY_BUFFER, dynamicInstanceDataBuffer); + glVertexAttribPointer(instPositionsHandle, 2, GL_FLOAT, GL_FALSE, 0, (float*)(instPositionsOffsetBytes * instanceCount)); glEnableVertexAttribArray(instPositionsHandle); glVertexAttribDivisor(instPositionsHandle, 1); - - glBindBuffer(GL_ARRAY_BUFFER, textureCoordinatesListBuffer); - glVertexAttribPointer(instTextureCoordinatesHandle, 4, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(instTextureCoordinatesHandle, 4, GL_FLOAT, GL_FALSE, 0, (float*)(instTextureCoordinatesOffsetBytes * instanceCount)); glEnableVertexAttribArray(instTextureCoordinatesHandle); glVertexAttribDivisor(instTextureCoordinatesHandle, 1); - - glBindBuffer(GL_ARRAY_BUFFER, scalesBuffer); - glVertexAttribPointer(instScalesHandle, 2, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(instScalesHandle, 2, GL_FLOAT, GL_FALSE, 0, (float*)(instScalesOffsetBytes * instanceCount)); glEnableVertexAttribArray(instScalesHandle); glVertexAttribDivisor(instScalesHandle, 1); - - glBindBuffer(GL_ARRAY_BUFFER, rotationsBuffer); - glVertexAttribPointer(instRotationsHandle, 1, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(instRotationsHandle, 1, GL_FLOAT, GL_FALSE, 0, (float*)(instRotationsOffsetBytes * instanceCount)); glEnableVertexAttribArray(instRotationsHandle); glVertexAttribDivisor(instRotationsHandle, 1); - - glBindBuffer(GL_ARRAY_BUFFER, styleIndicesBuffer); - glVertexAttribIPointer(instStyleIndicesHandle, 1, GL_UNSIGNED_SHORT, 0, nullptr); + glVertexAttribIPointer(instStyleIndicesHandle, 1, GL_UNSIGNED_SHORT, 0, (float*)(instStyleIndicesOffsetBytes * instanceCount)); glEnableVertexAttribArray(instStyleIndicesHandle); glVertexAttribDivisor(instStyleIndicesHandle, 1); @@ -303,35 +297,35 @@ void Text2dInstancedOpenGl::setInstanceCount(int count) { void Text2dInstancedOpenGl::setPositions(const SharedBytes &positions) { std::lock_guard lock(dataMutex); - if (writeToBuffer(positions, positionsBuffer)) { + if (writeToDynamicInstanceDataBuffer(positions, instPositionsOffsetBytes)) { buffersNotReady &= ~(1); } } void Text2dInstancedOpenGl::setRotations(const SharedBytes &rotations) { std::lock_guard lock(dataMutex); - if (writeToBuffer(rotations, rotationsBuffer)) { + if (writeToDynamicInstanceDataBuffer(rotations, instRotationsOffsetBytes)) { buffersNotReady &= ~(1 << 1); } } void Text2dInstancedOpenGl::setScales(const SharedBytes &scales) { std::lock_guard lock(dataMutex); - if (writeToBuffer(scales, scalesBuffer)) { + if (writeToDynamicInstanceDataBuffer(scales, instScalesOffsetBytes)) { buffersNotReady &= ~(1 << 2); } } void Text2dInstancedOpenGl::setTextureCoordinates(const SharedBytes &textureCoordinates) { std::lock_guard lock(dataMutex); - if (writeToBuffer(textureCoordinates, textureCoordinatesListBuffer)) { + if (writeToDynamicInstanceDataBuffer(textureCoordinates, instTextureCoordinatesOffsetBytes)) { buffersNotReady &= ~(1 << 3); } } void Text2dInstancedOpenGl::setStyleIndices(const ::SharedBytes &indices) { std::lock_guard lock(dataMutex); - if (writeToBuffer(indices, styleIndicesBuffer)) { + if (writeToDynamicInstanceDataBuffer(indices, instStyleIndicesOffsetBytes)) { buffersNotReady &= ~(1 << 4); } } @@ -348,14 +342,14 @@ void Text2dInstancedOpenGl::setStyles(const ::SharedBytes &values) { buffersNotReady &= ~(1 << 5); } -bool Text2dInstancedOpenGl::writeToBuffer(const ::SharedBytes &data, GLuint target) { +bool Text2dInstancedOpenGl::writeToDynamicInstanceDataBuffer(const ::SharedBytes &data, GLuint targetOffsetBytes) { if(!ready){ // Writing to buffer before it was created return false; } - glBindBuffer(GL_ARRAY_BUFFER, target); - glBufferData(GL_ARRAY_BUFFER, data.elementCount * data.bytesPerElement, (void *) data.address, GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, dynamicInstanceDataBuffer); + glBufferSubData(GL_ARRAY_BUFFER, targetOffsetBytes * instanceCount, data.elementCount * data.bytesPerElement, (void *) data.address); glBindBuffer(GL_ARRAY_BUFFER, 0); return true; } diff --git a/android/src/main/cpp/graphics/objects/Text2dInstancedOpenGl.h b/android/src/main/cpp/graphics/objects/Text2dInstancedOpenGl.h index b51e2b954..8cb4e5c71 100644 --- a/android/src/main/cpp/graphics/objects/Text2dInstancedOpenGl.h +++ b/android/src/main/cpp/graphics/objects/Text2dInstancedOpenGl.h @@ -89,6 +89,7 @@ class Text2dInstancedOpenGl : public GraphicsObjectInterface, std::vector textureCoords; GLuint indexBuffer; std::vector indices; + bool glDataBuffersGenerated = false; std::shared_ptr textureHolder; int texturePointer; @@ -109,20 +110,24 @@ class Text2dInstancedOpenGl : public GraphicsObjectInterface, int instanceCount = 0; + GLuint dynamicInstanceDataBuffer; int instPositionsHandle; - GLuint positionsBuffer; int instRotationsHandle; - GLuint rotationsBuffer; int instScalesHandle; - GLuint scalesBuffer; int instStyleIndicesHandle; - GLuint styleIndicesBuffer; + int instTextureCoordinatesHandle; + int styleBufferHandle; GLuint styleBuffer; - int instTextureCoordinatesHandle; - GLuint textureCoordinatesListBuffer; + + static const int instPositionsOffsetBytes = sizeof(GLfloat) * 0; + static const int instTextureCoordinatesOffsetBytes = sizeof(GLfloat) * 2; + static const int instScalesOffsetBytes = sizeof(GLfloat) * 6; + static const int instRotationsOffsetBytes = sizeof(GLfloat) * 8; + static const int instStyleIndicesOffsetBytes = sizeof(GLfloat) * 9; + static const int instValuesSizeBytes = sizeof(GLfloat) * 10; private: - bool writeToBuffer(const ::SharedBytes &data, GLuint target); + bool writeToDynamicInstanceDataBuffer(const ::SharedBytes &data, GLuint targetOffsetBytes); }; diff --git a/android/src/main/cpp/graphics/objects/Text2dOpenGl.cpp b/android/src/main/cpp/graphics/objects/Text2dOpenGl.cpp index de6aaf9da..8aef63ffe 100644 --- a/android/src/main/cpp/graphics/objects/Text2dOpenGl.cpp +++ b/android/src/main/cpp/graphics/objects/Text2dOpenGl.cpp @@ -137,17 +137,15 @@ void Text2dOpenGl::prepareGlData(int program) { textureCoordinateHandle = glGetAttribLocation(program, "texCoordinate"); } - if (!hasVertexBuffer) { + if (!glDataBuffersGenerated) { glGenBuffers(1, &vertexAttribBuffer); - hasVertexBuffer = true; } glBindBuffer(GL_ARRAY_BUFFER, vertexAttribBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * textVertexAttributes.size(), &textVertexAttributes[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); - if (!hasIndexBuffer) { + if (!glDataBuffersGenerated) { glGenBuffers(1, &indexBuffer); - hasIndexBuffer = true; } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * textIndices.size(), &textIndices[0], GL_STATIC_DRAW); @@ -159,17 +157,16 @@ void Text2dOpenGl::prepareGlData(int program) { if (textureCoordScaleFactorHandle < 0) { textureCoordScaleFactorHandle = glGetUniformLocation(program, "textureCoordScaleFactor"); } + + glDataBuffersGenerated = true; } void Text2dOpenGl::removeGlBuffers() { std::lock_guard lock(dataMutex); - if (hasVertexBuffer) { + if (glDataBuffersGenerated) { glDeleteBuffers(1, &vertexAttribBuffer); - hasVertexBuffer = false; - } - if (hasIndexBuffer) { glDeleteBuffers(1, &indexBuffer); - hasIndexBuffer = false; + glDataBuffersGenerated = false; } } diff --git a/android/src/main/cpp/graphics/objects/Text2dOpenGl.h b/android/src/main/cpp/graphics/objects/Text2dOpenGl.h index a140d4067..f1f39cac4 100644 --- a/android/src/main/cpp/graphics/objects/Text2dOpenGl.h +++ b/android/src/main/cpp/graphics/objects/Text2dOpenGl.h @@ -71,11 +71,10 @@ class Text2dOpenGl : public GraphicsObjectInterface, int positionHandle = -1; int textureCoordinateHandle = -1; GLuint vertexAttribBuffer = -1; - bool hasVertexBuffer = false; std::vector textVertexAttributes; GLuint indexBuffer = -1; - bool hasIndexBuffer = false; std::vector textIndices; + bool glDataBuffersGenerated = false; std::shared_ptr textureHolder; int texturePointer; diff --git a/android/src/main/cpp/graphics/shader/ColorLineGroup2dShaderOpenGl.cpp b/android/src/main/cpp/graphics/shader/ColorLineGroup2dShaderOpenGl.cpp index 4243760b6..e9492a8eb 100644 --- a/android/src/main/cpp/graphics/shader/ColorLineGroup2dShaderOpenGl.cpp +++ b/android/src/main/cpp/graphics/shader/ColorLineGroup2dShaderOpenGl.cpp @@ -50,7 +50,7 @@ void ColorLineGroup2dShaderOpenGl::preRender(const std::shared_ptr<::RenderingCo return; } int lineStylesHandle = glGetUniformLocation(program, "lineValues"); - glUniform1fv(lineStylesHandle, sizeLineValuesArray, &lineValues[0]); + glUniform1fv(lineStylesHandle, numStyles * sizeLineValues, &lineValues[0]); int numStylesHandle = glGetUniformLocation(program, "numStyles"); glUniform1i(numStylesHandle, numStyles); int dashingScaleFactorHandle = glGetUniformLocation(program, "dashingScaleFactor"); diff --git a/android/src/main/cpp/graphics/shader/ColorPolygonGroup2dShaderOpenGl.cpp b/android/src/main/cpp/graphics/shader/ColorPolygonGroup2dShaderOpenGl.cpp index 1735ddff6..861ee15e2 100644 --- a/android/src/main/cpp/graphics/shader/ColorPolygonGroup2dShaderOpenGl.cpp +++ b/android/src/main/cpp/graphics/shader/ColorPolygonGroup2dShaderOpenGl.cpp @@ -53,7 +53,7 @@ void ColorPolygonGroup2dShaderOpenGl::preRender(const std::shared_ptr<::Renderin return; } int lineStylesHandle = glGetUniformLocation(program, "polygonStyles"); - glUniform1fv(lineStylesHandle, sizeStyleValuesArray, &polygonStyles[0]); + glUniform1fv(lineStylesHandle, numStyles * sizeStyleValues, &polygonStyles[0]); int numStylesHandle = glGetUniformLocation(program, "numStyles"); glUniform1i(numStylesHandle, numStyles); } @@ -66,6 +66,7 @@ void ColorPolygonGroup2dShaderOpenGl::setStyles(const ::SharedBytes & styles) { std::memcpy(this->polygonStyles.data(), (void *) styles.address, styles.elementCount * styles.bytesPerElement); } + this->numStyles = styles.elementCount; } } diff --git a/shared/public/Tiled2dMapTileInfo.h b/shared/public/Tiled2dMapTileInfo.h index b145aca0c..ed3188c1a 100644 --- a/shared/public/Tiled2dMapTileInfo.h +++ b/shared/public/Tiled2dMapTileInfo.h @@ -47,6 +47,10 @@ struct Tiled2dMapTileInfo { std::string to_string() const { return "Tiled2dMapTileInfo(" + std::to_string(zoomIdentifier) + "/" + std::to_string(x) + "/" + std::to_string(y) + "/" + std::to_string(t) + ")"; } + + std::string to_string_short() const { + return std::to_string(zoomIdentifier) + "/" + std::to_string(x) + "/" + std::to_string(y) + "/" + std::to_string(t); + } }; namespace std { diff --git a/shared/public/Value.h b/shared/public/Value.h index 479308667..9a6a13e97 100644 --- a/shared/public/Value.h +++ b/shared/public/Value.h @@ -40,6 +40,7 @@ #include "SymbolZOrder.h" #include "ValueVariant.h" #include "Tiled2dMapVectorStateManager.h" +#include "Logger.h" #include #include @@ -652,11 +653,10 @@ class ValueEvaluator { return *staticValue; } - if((isStateDependant && !context.featureStateManager->empty()) || isZoomDependent) { + if (isZoomDependent || (isStateDependant && !context.featureStateManager->empty())) { return value->evaluateOr(context, defaultValue); } - auto identifier = context.feature->identifier; if(isStateDependant && !context.featureStateManager->empty()) { identifier = (context.feature->identifier << 32) | (uint64_t)(context.featureStateManager->getCurrentState()); @@ -713,8 +713,6 @@ class GetPropertyValue : public Value { const std::string key; }; -#include "Logger.h" - class FeatureStateValue : public Value { public: FeatureStateValue(const std::string key) : key(key) {}; diff --git a/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceRasterTileDataManager.cpp b/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceRasterTileDataManager.cpp index 99459264f..5df91c47a 100644 --- a/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceRasterTileDataManager.cpp +++ b/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceRasterTileDataManager.cpp @@ -151,8 +151,7 @@ void Tiled2dMapVectorSourceRasterTileDataManager::onRasterTilesUpdated(const std auto castedMe = std::static_pointer_cast(shared_from_this()); auto selfActor = WeakActor(mailbox, castedMe); - selfActor.messagePrecisely(MailboxDuplicationStrategy::replaceNewest, MailboxExecutionEnvironment::graphics, - &Tiled2dMapVectorSourceTileDataManager::updateMaskObjects); + noPendingUpdateMasks.clear(); } mapInterface->invalidate(); diff --git a/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceTileDataManager.cpp b/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceTileDataManager.cpp index 42969b01a..0144a8a7a 100644 --- a/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceTileDataManager.cpp +++ b/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceTileDataManager.cpp @@ -17,6 +17,10 @@ #include "RenderPass.h" void Tiled2dMapVectorSourceTileDataManager::update() { + if (!noPendingUpdateMasks.test_and_set()) { + updateMaskObjects(); + } + for (const auto &[tileInfo, subTiles] : tiles) { const auto tileState = tileStateMap.find(tileInfo); diff --git a/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceTileDataManager.h b/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceTileDataManager.h index f96e3f6cb..81b884009 100644 --- a/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceTileDataManager.h +++ b/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceTileDataManager.h @@ -73,6 +73,7 @@ class Tiled2dMapVectorSourceTileDataManager : public Tiled2dMapVectorLayerTileCa std::unordered_set tilesReady; std::unordered_map> tilesReadyControlSet; + std::atomic_flag noPendingUpdateMasks = ATOMIC_FLAG_INIT; std::atomic_flag updateFlag = ATOMIC_FLAG_INIT; std::recursive_mutex updateMutex; std::unordered_map tileMasksToSetup; diff --git a/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceVectorTileDataManager.cpp b/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceVectorTileDataManager.cpp index de68daefd..2722fb7d8 100644 --- a/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceVectorTileDataManager.cpp +++ b/shared/src/map/layers/tiled/vector/sourcemanagers/Tiled2dMapVectorSourceVectorTileDataManager.cpp @@ -155,8 +155,7 @@ void Tiled2dMapVectorSourceVectorTileDataManager::onVectorTilesUpdated(const std auto castedMe = std::static_pointer_cast(shared_from_this()); auto selfActor = WeakActor(mailbox, castedMe); - selfActor.messagePrecisely(MailboxDuplicationStrategy::replaceNewest, MailboxExecutionEnvironment::graphics, - &Tiled2dMapVectorSourceTileDataManager::updateMaskObjects); + noPendingUpdateMasks.clear(); } mapInterface->invalidate(); } diff --git a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSourceSymbolDataManager.cpp b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSourceSymbolDataManager.cpp index ffd2c8664..3b293c5d6 100644 --- a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSourceSymbolDataManager.cpp +++ b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSourceSymbolDataManager.cpp @@ -558,16 +558,17 @@ void Tiled2dMapVectorSourceSymbolDataManager::updateSymbolGroups() { manager->remove(localToRemove); }); + bool clearCoordinator = !tilesToClear.empty(); for (const auto &symbolGroup: tilesToClear) { symbolGroup.syncAccess([](auto symbolGroup) { symbolGroup->clear(); }); } + tilesToClear.clear(); - if (!tilesToClear.empty()) { + if (clearCoordinator) { animationCoordinatorMap->clearAnimationCoordinators(); } - tilesToClear.clear(); } pregenerateRenderPasses(); diff --git a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.cpp b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.cpp index f96734c9b..0b317ca1e 100644 --- a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.cpp +++ b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.cpp @@ -161,7 +161,27 @@ Tiled2dMapVectorSymbolObject::Tiled2dMapVectorSymbolObject(const std::weak_ptr layerDescription, const UsedKeysCollection &usedKeys) { +Tiled2dMapVectorSymbolObject::~Tiled2dMapVectorSymbolObject() { + if (animationCoordinator) { + if (isCoordinateOwner) { + animationCoordinator->isOwned.clear(); + isCoordinateOwner = false; + } + animationCoordinator->decreaseUsage(); + } +} + +void Tiled2dMapVectorSymbolObject::placedInCache() { + if (animationCoordinator) { + if (isCoordinateOwner) { + animationCoordinator->isOwned.clear(); + isCoordinateOwner = false; + } + } +} + +void Tiled2dMapVectorSymbolObject::updateLayerDescription(const std::shared_ptr layerDescription, + const UsedKeysCollection &usedKeys) { this->description = layerDescription; if (labelObject) { labelObject->updateLayerDescription(layerDescription); diff --git a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.h b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.h index a0cf1c053..3eaf269bc 100644 --- a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.h +++ b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.h @@ -52,24 +52,9 @@ class Tiled2dMapVectorSymbolObject { const double dpFactor, const bool persistingSymbolPlacement); - ~Tiled2dMapVectorSymbolObject() { - if (animationCoordinator) { - if (isCoordinateOwner) { - animationCoordinator->isOwned.clear(); - isCoordinateOwner = false; - } - animationCoordinator->decreaseUsage(); - } - } + ~Tiled2dMapVectorSymbolObject(); - void placedInCache() { - if (animationCoordinator) { - if (isCoordinateOwner) { - animationCoordinator->isOwned.clear(); - isCoordinateOwner = false; - } - } - } + void placedInCache(); struct SymbolObjectInstanceCounts { int icons, textCharacters, stretchedIcons; }; diff --git a/shared/src/map/scheduling/ThreadPoolSchedulerImpl.h b/shared/src/map/scheduling/ThreadPoolSchedulerImpl.h index 600d7ef43..9fb160bda 100644 --- a/shared/src/map/scheduling/ThreadPoolSchedulerImpl.h +++ b/shared/src/map/scheduling/ThreadPoolSchedulerImpl.h @@ -57,8 +57,8 @@ class ThreadPoolSchedulerImpl: public SchedulerInterface { std::condition_variable defaultCv; bool separateGraphicsQueue; - static const uint8_t MAX_NUM_GRAPHICS_TASKS = 12; - static const uint64_t MAX_TIME_GRAPHICS_TASKS_MS = 4; + static const uint8_t MAX_NUM_GRAPHICS_TASKS = 128; + static const uint64_t MAX_TIME_GRAPHICS_TASKS_MS = 6; std::mutex graphicsMutex; std::deque> graphicsQueue;