Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc. OpenGL improvements and fixes #573

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions android/src/main/cpp/graphics/objects/LineGroup2dOpenGl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -74,6 +78,7 @@ void LineGroup2dOpenGl::setup(const std::shared_ptr<::RenderingContextInterface>
scaleFactorHandle = glGetUniformLocation(program, "scaleFactor");

ready = true;
glDataBuffersGenerated = true;
}

void LineGroup2dOpenGl::clear() {
Expand All @@ -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; }
Expand Down
5 changes: 3 additions & 2 deletions android/src/main/cpp/graphics/objects/LineGroup2dOpenGl.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ class LineGroup2dOpenGl : public GraphicsObjectInterface,
int vertexIndexHandle;
int segmentStartLPosHandle;
int styleInfoHandle;
GLuint vertexAttribBuffer;
GLuint vertexAttribBuffer = -1;
std::vector<GLfloat> lineAttributes;
GLuint indexBuffer;
GLuint indexBuffer = -1;
std::vector<GLuint> lineIndices;
bool glDataBuffersGenerated = false;

bool ready = false;
bool dataReady = false;
Expand Down
15 changes: 11 additions & 4 deletions android/src/main/cpp/graphics/objects/Polygon2dOpenGl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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; }
Expand Down
2 changes: 2 additions & 0 deletions android/src/main/cpp/graphics/objects/Polygon2dOpenGl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class Polygon2dOpenGl : public GraphicsObjectInterface,
std::vector<GLfloat> vertices;
GLuint indexBuffer;
std::vector<GLushort> indices;
bool glDataBuffersGenerated = false;


bool dataReady = false;
bool ready = false;
Expand Down
16 changes: 12 additions & 4 deletions android/src/main/cpp/graphics/objects/PolygonGroup2dOpenGl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,24 @@ 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);

mvpMatrixHandle = glGetUniformLocation(program, "uMVPMatrix");

ready = true;
glDataBuffersGenerated = true;
}

void PolygonGroup2dOpenGl::clear() {
Expand All @@ -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; }
Expand Down
5 changes: 3 additions & 2 deletions android/src/main/cpp/graphics/objects/PolygonGroup2dOpenGl.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ class PolygonGroup2dOpenGl : public GraphicsObjectInterface,
int mvpMatrixHandle;
int positionHandle;
int styleIndexHandle;
GLuint attribBuffer;
GLuint attribBuffer = -1;
std::vector<GLfloat> polygonAttributes;
GLuint indexBuffer;
GLuint indexBuffer = -1;
std::vector<GLushort> polygonIndices;
bool glDataBuffersGenerated = false;

bool dataReady = false;
bool ready = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class PolygonPatternGroup2dOpenGl : public GraphicsObjectInterface,
std::vector<GLfloat> vertices;
GLuint indexBuffer;
std::vector<GLushort> indices;
bool glDataBuffersGenerated = false;

std::shared_ptr<TextureHolderInterface> textureHolder;
int texturePointer;
Expand Down
67 changes: 34 additions & 33 deletions android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -298,46 +299,46 @@ void Quad2dInstancedOpenGl::setInstanceCount(int count) {

void Quad2dInstancedOpenGl::setPositions(const SharedBytes &positions) {
std::lock_guard<std::recursive_mutex> lock(dataMutex);
if (writeToBuffer(positions, positionsBuffer)) {
if (writeToDynamicInstanceDataBuffer(positions, instPositionsOffsetBytes)) {
buffersNotReady &= ~(1);
}
}

void Quad2dInstancedOpenGl::setRotations(const SharedBytes &rotations) {
std::lock_guard<std::recursive_mutex> lock(dataMutex);
if (writeToBuffer(rotations, rotationsBuffer)) {
if (writeToDynamicInstanceDataBuffer(rotations, instRotationsOffsetBytes)) {
buffersNotReady &= ~(1 << 1);
}
}

void Quad2dInstancedOpenGl::setScales(const SharedBytes &scales) {
std::lock_guard<std::recursive_mutex> lock(dataMutex);
if (writeToBuffer(scales, scalesBuffer)) {
if (writeToDynamicInstanceDataBuffer(scales, instScalesOffsetBytes)) {
buffersNotReady &= ~(1 << 2);
}
}

void Quad2dInstancedOpenGl::setTextureCoordinates(const SharedBytes &textureCoordinates) {
std::lock_guard<std::recursive_mutex> lock(dataMutex);
if (writeToBuffer(textureCoordinates, textureCoordinatesListBuffer)) {
if (writeToDynamicInstanceDataBuffer(textureCoordinates, instTextureCoordinatesOffsetBytes)) {
buffersNotReady &= ~(1 << 3);
}
}

void Quad2dInstancedOpenGl::setAlphas(const SharedBytes &values) {
std::lock_guard<std::recursive_mutex> 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;
}
Expand Down
16 changes: 10 additions & 6 deletions android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Quad2dInstancedOpenGl : public GraphicsObjectInterface,
std::vector<GLfloat> textureCoords;
GLuint indexBuffer;
std::vector<GLubyte> indices;
bool glDataBuffersGenerated = false;

std::shared_ptr<TextureHolderInterface> textureHolder;
int texturePointer;
Expand All @@ -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);

};
8 changes: 5 additions & 3 deletions android/src/main/cpp/graphics/objects/Quad2dOpenGl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading
Loading