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

Fixed Issue #631 #632

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
47 changes: 40 additions & 7 deletions src/nanovg_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ struct GLNVGcontext {
int textureId;
GLuint vertBuf;
#if defined NANOVG_GL3
GLuint vertArr;
GLuint* vertArr; // vertex array buffer, on a per gl context basis
int vertArrayCount;
#endif
#if NANOVG_GL_USE_UNIFORMBUFFER
GLuint fragBuf;
Expand Down Expand Up @@ -690,9 +691,17 @@ static int glnvg__renderCreate(void* uptr)
glnvg__checkError(gl, "uniform locations");
glnvg__getUniforms(&gl->shader);

// Create dynamic vertex array
// Begin the array of dynamic vertex arrays (one for each context/opengl window)
#if defined NANOVG_GL3
glGenVertexArrays(1, &gl->vertArr);
gl->vertArr = (GLuint*) malloc(sizeof(GLuint));
glGenVertexArrays(1, gl->vertArr);

// due to a call to glIsVertexArray() returning false if the array has not been bound yet, we bind and unbind the vertex array
glBindVertexArray(gl->vertArr[0]);
glBindVertexArray(0);
// dont you love opengl

gl->vertArrayCount = 1;
#endif
glGenBuffers(1, &gl->vertBuf);

Expand Down Expand Up @@ -1215,10 +1224,30 @@ static void glnvg__renderFlush(void* uptr)
glBindBuffer(GL_UNIFORM_BUFFER, gl->fragBuf);
glBufferData(GL_UNIFORM_BUFFER, gl->nuniforms * gl->fragSize, gl->uniforms, GL_STREAM_DRAW);
#endif

glnvg__checkError(gl, "uniform buffers");
// Upload vertex data
#if defined NANOVG_GL3
glBindVertexArray(gl->vertArr);
bool foundVertexArr = false;
for (int i = 0; i < gl->vertArrayCount; i++) {
if (glIsVertexArray(gl->vertArr[i] == GL_TRUE)) {
//puts("Found correct vertex array!");
// if the vertex array exists for the current context, bind it
glBindVertexArray(gl->vertArr[i]);
foundVertexArr = true;
break;
}
}

if (!foundVertexArr) {
// create a new vertex array
puts("Creating a new vertex array!");
gl->vertArr = (GLuint*) realloc(gl->vertArr, sizeof(GLuint) * (gl->vertArrayCount + 1));
glGenVertexArrays(1, &gl->vertArr[gl->vertArrayCount]);
glBindVertexArray(gl->vertArr[gl->vertArrayCount]);
gl->vertArrayCount += 1;
}

glnvg__checkError(gl, "vertex arrays");
#endif
glBindBuffer(GL_ARRAY_BUFFER, gl->vertBuf);
glBufferData(GL_ARRAY_BUFFER, gl->nverts * sizeof(NVGvertex), gl->verts, GL_STREAM_DRAW);
Expand Down Expand Up @@ -1539,8 +1568,12 @@ static void glnvg__renderDelete(void* uptr)
if (gl->fragBuf != 0)
glDeleteBuffers(1, &gl->fragBuf);
#endif
if (gl->vertArr != 0)
glDeleteVertexArrays(1, &gl->vertArr);
// idk what to do here, because the vertex arrays are dynamically created, one per context
// so the context we created them in would have to be deleted in order to delete them
// so for now, we will naively assume that the first array has its context present
// wow, opengl really sucks
if (gl->vertArr[0] != 0)
glDeleteVertexArrays(1, &gl->vertArr[0]);
#endif
if (gl->vertBuf != 0)
glDeleteBuffers(1, &gl->vertBuf);
Expand Down