Skip to content

Commit

Permalink
Fixed Issue memononen#631
Browse files Browse the repository at this point in the history
  • Loading branch information
Lochnessdragon committed May 13, 2022
1 parent 397f330 commit 62a51a3
Showing 1 changed file with 40 additions and 7 deletions.
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]);

This comment has been minimized.

Copy link
@nidefawl

nidefawl May 13, 2022

This bind should always happen, not only when a VAO was created

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

0 comments on commit 62a51a3

Please sign in to comment.