Skip to content

Commit

Permalink
Merge most of the instanced objects dynamic buffers, moved some symbo…
Browse files Browse the repository at this point in the history
…l object function implementations
  • Loading branch information
maurhofer-ubique committed Jan 22, 2024
1 parent 96d6132 commit 11ade9f
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 153 deletions.
51 changes: 21 additions & 30 deletions android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -308,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
15 changes: 9 additions & 6 deletions android/src/main/cpp/graphics/objects/Quad2dInstancedOpenGl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

};
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -336,54 +315,53 @@ void Quad2dStretchedInstancedOpenGl::setInstanceCount(int count) {

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

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

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

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

void Quad2dStretchedInstancedOpenGl::setAlphas(const SharedBytes &values) {
std::lock_guard<std::recursive_mutex> lock(dataMutex);
if (writeToBuffer(values, alphasBuffer)) {
if (writeToDynamicInstanceDataBuffer(values, instAlphasOffsetBytes)) {
buffersNotReady &= ~(1 << 4);
}
}

void Quad2dStretchedInstancedOpenGl::setStretchInfos(const ::SharedBytes &values) {
std::lock_guard<std::recursive_mutex> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Loading

0 comments on commit 11ade9f

Please sign in to comment.