From 5561ef6c43b2e4b933ccc2aa1e18fdb97e4ab431 Mon Sep 17 00:00:00 2001 From: Christoph Maurhofer Date: Fri, 12 Jan 2024 13:54:05 +0100 Subject: [PATCH] Merge most of the instanced objects dynamic buffers, moved some symbol object function implementations --- .../objects/Quad2dInstancedOpenGl.cpp | 51 ++++++------- .../graphics/objects/Quad2dInstancedOpenGl.h | 15 ++-- .../Quad2dStretchedInstancedOpenGl.cpp | 72 +++++++------------ .../objects/Quad2dStretchedInstancedOpenGl.h | 19 +++-- .../objects/Text2dInstancedOpenGl.cpp | 61 ++++++---------- .../graphics/objects/Text2dInstancedOpenGl.h | 18 +++-- shared/public/Tiled2dMapTileInfo.h | 4 ++ .../symbol/Tiled2dMapVectorSymbolObject.cpp | 22 +++++- .../symbol/Tiled2dMapVectorSymbolObject.h | 19 +---- 9 files changed, 128 insertions(+), 153 deletions(-) diff --git a/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.cpp b/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.cpp index 166e41bb1..05abb60c2 100644 --- a/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.cpp +++ b/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.cpp @@ -87,6 +87,12 @@ void Quad2dInstancedOpenGl::prepareGlData(int program) { 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); if (!glDataBuffersGenerated) { @@ -96,13 +102,6 @@ void Quad2dInstancedOpenGl::prepareGlData(int program) { glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * indices.size(), &indices[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - if (!glDataBuffersGenerated) { - glGenBuffers(1, &positionsBuffer); - glGenBuffers(1, &rotationsBuffer); - glGenBuffers(1, &textureCoordinatesListBuffer); - glGenBuffers(1, &scalesBuffer); - glGenBuffers(1, &alphasBuffer); - } instPositionsHandle = glGetAttribLocation(program, "aPosition"); instRotationsHandle = glGetAttribLocation(program, "aRotation"); instTextureCoordinatesHandle = glGetAttribLocation(program, "aTexCoordinate"); @@ -139,11 +138,7 @@ void Quad2dInstancedOpenGl::removeGlBuffers() { if (glDataBuffersGenerated) { glDeleteBuffers(1, &vertexBuffer); glDeleteBuffers(1, &indexBuffer); - glDeleteBuffers(1, &positionsBuffer); - glDeleteBuffers(1, &alphasBuffer); - glDeleteBuffers(1, &scalesBuffer); - glDeleteBuffers(1, &textureCoordinatesListBuffer); - glDeleteBuffers(1, &rotationsBuffer); + glDeleteBuffers(1, &dynamicInstanceDataBuffer); glDataBuffersGenerated = false; } } @@ -227,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); @@ -308,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 6b6d3952f..f3b65a076 100644 --- a/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.h +++ b/android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.h @@ -115,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/Quad2dStretchedInstancedOpenGl.cpp b/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.cpp index 4f80d7b78..8312584c3 100644 --- a/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.cpp +++ b/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.cpp @@ -88,6 +88,12 @@ void Quad2dStretchedInstancedOpenGl::prepareGlData(int program) { 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); if (!glDataBuffersGenerated) { @@ -97,14 +103,6 @@ void Quad2dStretchedInstancedOpenGl::prepareGlData(int program) { glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * indices.size(), &indices[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - if (!glDataBuffersGenerated) { - glGenBuffers(1, &positionsBuffer); - glGenBuffers(1, &textureCoordinatesListBuffer); - glGenBuffers(1, &scalesBuffer); - glGenBuffers(1, &rotationsBuffer); - glGenBuffers(1, &alphasBuffer); - glGenBuffers(1, &stretchInfoBuffer); - } instPositionsHandle = glGetAttribLocation(program, "aPosition"); instTextureCoordinatesHandle = glGetAttribLocation(program, "aTexCoordinate"); instScalesHandle = glGetAttribLocation(program, "aScale"); @@ -142,15 +140,9 @@ void Quad2dStretchedInstancedOpenGl::removeGlBuffers() { if (glDataBuffersGenerated) { glDeleteBuffers(1, &vertexBuffer); glDeleteBuffers(1, &indexBuffer); + glDeleteBuffers(1, &dynamicInstanceDataBuffer); glDataBuffersGenerated = false; } - - glDeleteBuffers(1, &positionsBuffer); - glDeleteBuffers(1, &alphasBuffer); - glDeleteBuffers(1, &scalesBuffer); - glDeleteBuffers(1, &textureCoordinatesListBuffer); - glDeleteBuffers(1, &rotationsBuffer); - glDeleteBuffers(1, &stretchInfoBuffer); } void Quad2dStretchedInstancedOpenGl::removeTextureCoordsGlBuffers() { @@ -229,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 @@ -293,8 +274,6 @@ void Quad2dStretchedInstancedOpenGl::render(const std::shared_ptr<::RenderingCon glVertexAttribDivisor(instStretchXsHandle, 0); glVertexAttribDivisor(instStretchYsHandle, 0); - - // Disable vertex array glDisableVertexAttribArray(positionHandle); if (textureHolder) { @@ -336,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 476aac2e2..0dd167ffe 100644 --- a/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.h +++ b/android/src/main/cpp/graphics/objects/Quad2dStretchedInstancedOpenGl.h @@ -117,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 df12a08cb..411f69ccc 100644 --- a/android/src/main/cpp/graphics/objects/Text2dInstancedOpenGl.cpp +++ b/android/src/main/cpp/graphics/objects/Text2dInstancedOpenGl.cpp @@ -85,6 +85,13 @@ void Text2dInstancedOpenGl::prepareGlData(int program) { 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); if (!glDataBuffersGenerated) { @@ -94,15 +101,6 @@ void Text2dInstancedOpenGl::prepareGlData(int program) { glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * indices.size(), &indices[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - if (!glDataBuffersGenerated) { - glGenBuffers(1, &positionsBuffer); - glGenBuffers(1, &textureCoordinatesListBuffer); - glGenBuffers(1, &scalesBuffer); - glGenBuffers(1, &rotationsBuffer); - glGenBuffers(1, &styleIndicesBuffer); - glGenBuffers(1, &styleBuffer); - } - instPositionsHandle = glGetAttribLocation(program, "aPosition"); instTextureCoordinatesHandle = glGetAttribLocation(program, "aTexCoordinate"); instScalesHandle = glGetAttribLocation(program, "aScale"); @@ -140,15 +138,10 @@ void Text2dInstancedOpenGl::removeGlBuffers() { if (glDataBuffersGenerated) { glDeleteBuffers(1, &vertexBuffer); glDeleteBuffers(1, &indexBuffer); + glDeleteBuffers(1, &dynamicInstanceDataBuffer); + glDeleteBuffers(1, &styleBuffer); glDataBuffersGenerated = false; } - - glDeleteBuffers(1, &positionsBuffer); - glDeleteBuffers(1, &styleIndicesBuffer); - glDeleteBuffers(1, &scalesBuffer); - glDeleteBuffers(1, &textureCoordinatesListBuffer); - glDeleteBuffers(1, &rotationsBuffer); - glDeleteBuffers(1, &styleBuffer); } void Text2dInstancedOpenGl::removeTextureCoordsGlBuffers() { @@ -223,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); @@ -312,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); } } @@ -357,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 b70a2af35..8cb4e5c71 100644 --- a/android/src/main/cpp/graphics/objects/Text2dInstancedOpenGl.h +++ b/android/src/main/cpp/graphics/objects/Text2dInstancedOpenGl.h @@ -110,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/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/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; };