Skip to content

Commit

Permalink
ecere/gfx/drivers/GL/GLAB: Improved handling element buffers with VAO
Browse files Browse the repository at this point in the history
- Not modifying active element buffer when VAOs are on, except for default VAO
- Not using glabCurElementBuffer with VAO except for defaultVAO, as each VAO may have a different active element buffer
- Removed unecessary / wrong WebGL-specific logic
- New GLABBindVertexArray() call to keep track of active VAO
- NOTE: VAO usage currently expects a single context / display
- butterbur: Updated to use GLABBindVertexArray()
- samples/3D/CubicWorld: Updated to use GLABBindVertexArray()
- samples/3D/CubicWorld: Improved readability e.g., splitting prepareMultiDraw() into multiple methods
  • Loading branch information
jerstlouis committed Nov 5, 2022
1 parent 6b7f422 commit 4abfbe4
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 182 deletions.
3 changes: 2 additions & 1 deletion butterbur/src/opengl/shaders/butterbur.frag
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@ void main(void)

#if FOG_ON
{
float fog = clamp(exp(fogZ), 0.0, 1.0);
float fog = clamp(exp(-fogZ * fogZ), 0.0, 1.0);
//float fog = clamp(exp(fogZ), 0.0, 1.0);
c = vec4(fog * c.xyz + (1.0 - fog) * fogColor, c.w);
}
#endif
Expand Down
10 changes: 5 additions & 5 deletions butterbur/src/presentation/DrawingManager.ec
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public class MDManager : DrawingManager
}
}

if(glCaps_vao) glBindVertexArray(0);
if(glCaps_vao) GLABBindVertexArray(0);
}
}

Expand Down Expand Up @@ -227,7 +227,7 @@ class Perspective3DManager : MDManager
GLFlushMatrices();

texture.bind();
if(glCaps_vao) glBindVertexArray(md.vao);
if(glCaps_vao) GLABBindVertexArray(md.vao);
for(n = 0; n < md.commandsCount; n++)
{
const GLDrawCommand *cmd = &md.commands[n];
Expand All @@ -251,7 +251,7 @@ class Perspective3DManager : MDManager
glDisableVertexAttribArray(transform2Attribute);
glDisableVertexAttribArray(transform3Attribute);
}*/
if(glCaps_vao) glBindVertexArray(0);
if(glCaps_vao) GLABBindVertexArray(0);

butterburShader.transform3D = false;
}
Expand All @@ -264,7 +264,7 @@ class Perspective3DManager : MDManager
{
PrimitiveGroup group;

if(glCaps_vao) glBindVertexArray(defaultVAO);
if(glCaps_vao) GLABBindVertexArray(defaultVAO);
glDisplay.SelectMesh(mesh);

for(group = mesh.groups.first; group; group = group.next)
Expand All @@ -283,7 +283,7 @@ class Perspective3DManager : MDManager
glDisplay.DrawPrimitives((PrimitiveSingle *)&group.type, mesh);
}

if(glCaps_vao) glBindVertexArray(0);
if(glCaps_vao) GLABBindVertexArray(0);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions butterbur/src/presentation/GraphicalSurface.ec
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public class GraphicalSurface : MultiPresentation
#endif

glDisable(GL_SCISSOR_TEST);
if(glCaps_vao) glBindVertexArray(0);
if(glCaps_vao) GLABBindVertexArray(0);
glBindFramebuffer(GL_FRAMEBUFFER, texturesFramebuffer);
butterburShader.activate();
butterburShader.lighting(false);
Expand Down Expand Up @@ -268,7 +268,7 @@ public class GraphicalSurface : MultiPresentation
butterburShader.textureArray(false);
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
glEnable(GL_SCISSOR_TEST);
if(glCaps_vao) glBindVertexArray(defaultVAO);
if(glCaps_vao) GLABBindVertexArray(defaultVAO);
DefaultShader::shader().texturing(false);
DefaultShader::shader().texturing(true);
DefaultShader::shader().activate();
Expand Down
6 changes: 3 additions & 3 deletions butterbur/src/presentation/TIManager.ec
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ public class TIManager : DrawingManager
float transform[2] = { originOffset.x, originOffset.y };
if(drawManager)
drawManager.ready(width, height);
if(glCaps_vao) glBindVertexArray(defaultVAO);
if(glCaps_vao) GLABBindVertexArray(defaultVAO);
presentation.prepareDraw(renderFlags, this, transform);
}

void draw()
{
// TODO: Proper VAO support for text & images?
if(glCaps_vao) glBindVertexArray(defaultVAO);
if(glCaps_vao) GLABBindVertexArray(defaultVAO);
if(drawManager)
drawManager.flushImages();
if(glCaps_shaders)
glEnableVertexAttribArray(GLBufferContents::vertex);
if(glCaps_vao) glBindVertexArray(0);
if(glCaps_vao) GLABBindVertexArray(0);
}

void addTextCommand(const String text, GEFont font, float opacity, Alignment2D alignment, float * transform)
Expand Down
14 changes: 9 additions & 5 deletions ecere/src/gfx/drivers/OpenGLDisplayDriver.ec
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ Shader activeShader;

static int displayWidth, displayHeight;

uint defaultVAO; // Only works with a single display / context

static bool useSingleGLContext = false;
class OGLDisplay : struct
{
Expand Down Expand Up @@ -1373,8 +1375,11 @@ class OpenGLDisplayDriver : DisplayDriver
#if ENABLE_GL_VAO
if(oglDisplay.capabilities.vao)
{
// VAOs cannot be shared across contexts, but in single context mode
// we should re-use the same VAO across displays.
glGenVertexArrays(1, &oglDisplay.vao);
glBindVertexArray(oglDisplay.vao);
defaultVAO = oglDisplay.vao; // NOTE: This currently only works with a single display / context
GLABBindVertexArray(oglDisplay.vao);
}
#endif

Expand Down Expand Up @@ -1413,8 +1418,7 @@ class OpenGLDisplayDriver : DisplayDriver
GLDisableClientState(TANGENTS1);
GLDisableClientState(TANGENTS2);
#if ENABLE_GL_VAO
if(glBindVertexArray)
glBindVertexArray(0);
GLABBindVertexArray(0);
#endif
if(glUseProgram)
glUseProgram(0);
Expand All @@ -1425,7 +1429,7 @@ class OpenGLDisplayDriver : DisplayDriver

#if ENABLE_GL_VAO
if(glCaps_vao)
glBindVertexArray(oglDisplay.vao);
GLABBindVertexArray(oglDisplay.vao);
#endif

GLEnableClientState(VERTICES);
Expand Down Expand Up @@ -2039,7 +2043,7 @@ class OpenGLDisplayDriver : DisplayDriver
if(glCaps_vao)
{
OGLDisplay oglDisplay = display.driverData;
glBindVertexArray(oglDisplay.vao);
GLABBindVertexArray(oglDisplay.vao);
}
#endif
GLABBindBuffer(GL_ARRAY_BUFFER, 0);
Expand Down
20 changes: 14 additions & 6 deletions ecere/src/gfx/drivers/gl3/GLMultiDraw.ec
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ public struct GLMultiDraw

// TOCHECK: No attrib divisor support in ES 2 -- will it be needed?
#if (!defined(_GLES) && !defined(_GLES2)) || defined(_GLES3)
if(glCaps_vao) glBindVertexArray(vao);
if(glCaps_vao) GLABBindVertexArray(vao);

#ifndef CLIENT_MEM_COMMANDS
commandsB.upload(0, commandsCount * sizeof(GLDrawCommand), commands);
Expand Down Expand Up @@ -681,7 +681,7 @@ public struct GLMultiDraw
// TOCHECK: Should this re-do all the use() here if there is no VAO support?

#if (!defined(_GLES) && !defined(_GLES2)) || defined(_GLES3)
if(glCaps_vao) glBindVertexArray(vao);
if(glCaps_vao) GLABBindVertexArray(vao);
#endif
GLFlushMatrices();

Expand All @@ -696,10 +696,18 @@ public struct GLMultiDraw
GLAB ab { vertexGLMB.ab.buffer };
Shader shader = activeShader;

GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexGLMB.ab.buffer);
if(!glCaps_vao) // Don't modify VAO state.
{
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexGLMB.ab.buffer);

glDisableVertexAttribArray((GLBufferContents)drawIDAttribute);
glDisableVertexAttribArray((GLBufferContents)posOffsetAttribute);
}
else if(vao == defaultVAO)
PrintLn("WARNING: MultiDraw with default VAO");
else if(!glabCurVertexArray)
PrintLn("WARNING (MultiDraw): No VAO selected");

glDisableVertexAttribArray((GLBufferContents)drawIDAttribute);
glDisableVertexAttribArray((GLBufferContents)posOffsetAttribute);
for(n = 0; n < commandsCount; n++)
{
const GLDrawCommand *cmd = &commands[n];
Expand Down Expand Up @@ -783,7 +791,7 @@ public struct GLMultiDraw
#endif
#endif
#if (!defined(_GLES) && !defined(_GLES2)) || defined(_GLES3)
if(glCaps_vao) glBindVertexArray(0);
if(glCaps_vao) GLABBindVertexArray(0);
#endif
}
};
Expand Down
61 changes: 41 additions & 20 deletions ecere/src/gfx/drivers/gl3/glab.ec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ default dllexport void GLABUnbindBuffer(int target)
glBindBuffer(target, 0);
if(target == GL_ARRAY_BUFFER)
glabCurArrayBuffer = 0;
else if(target == GL_ELEMENT_ARRAY_BUFFER)
else if(target == GL_ELEMENT_ARRAY_BUFFER && (!glCaps_vao || glabCurVertexArray == defaultVAO))
glabCurElementBuffer = 0;
// NOTE: Actually ES 3.1 is required, separate define?
#if !defined(_GLES) && !defined(_GLES2) && !defined(_GLES3)
Expand All @@ -33,7 +33,7 @@ public void GLABBindBuffer(int target, uint buffer)
glBindBuffer(target, buffer);
if(target == GL_ARRAY_BUFFER)
glabCurArrayBuffer = buffer;
else if(target == GL_ELEMENT_ARRAY_BUFFER)
else if(target == GL_ELEMENT_ARRAY_BUFFER && (!glCaps_vao || glabCurVertexArray == defaultVAO))
glabCurElementBuffer = buffer;
// NOTE: Actually ES 3.1 is required, separate define?
#if !defined(_GLES) && !defined(_GLES2) && !defined(_GLES3)
Expand All @@ -43,10 +43,6 @@ public void GLABBindBuffer(int target, uint buffer)
}
}

#if !defined(_GLES) && !defined(_GLES2) && !defined(_GLES3)
uint glabCurDrawIndirectBuffer;
#endif

public enum GLBufferContents { vertex, normal, texCoord, color, tangent1, tangent2, lightVector, boneIndices1, boneIndices2, boneIndices3, boneWeights1, boneWeights2, boneWeights3 };

public enum GLBufferUsage { staticDraw, dynamicDraw, streamDraw };
Expand All @@ -55,7 +51,29 @@ static GLint bufferUsages[] = { GL_STATIC_DRAW, GL_DYNAMIC_DRAW, 0x88E0 /*GL_STR

public define noAB = GLAB { 0 };

uint glabCurArrayBuffer;
public void GLABBindVertexArray(uint vao)
{
#if (!defined(_GLES) && !defined(_GLES2)) || defined(_GLES3)
if(glCaps_vao) // && vao != glabCurVertexArray) // VAOs are not shared across contexts / displays
{
#ifdef _DEBUG
if(vao != glabCurVertexArray)
;//PrintLn("WARNING: Redundant VAO binding");
#endif
glBindVertexArray(vao);
glabCurVertexArray = vao;
}
#endif
}

uint glabCurVertexArray; // Currently bound VAO.

uint glabCurArrayBuffer; // Buffer currently bound for GL_ARRAY_BUFFER. *NOT* part of VAO state
uint glabCurElementBuffer; // Buffer currently bound for GL_ELEMENT_ARRAY_BUFFER. This *IS* part of the VAO state. With VAOs, this is for 'defaultVAO'.

#if !defined(_GLES) && !defined(_GLES2) && !defined(_GLES3)
uint glabCurDrawIndirectBuffer;
#endif

static short *shortVPBuffer = null;
static uint shortVPSize = 0;
Expand Down Expand Up @@ -508,7 +526,7 @@ public struct GLB
{
if(buffer == glabCurArrayBuffer)
GLABBindBuffer(GL_ARRAY_BUFFER, 0);
else if(buffer == glabCurElementBuffer)
else if(buffer == glabCurElementBuffer && (!glCaps_vao || glabCurVertexArray == defaultVAO))
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#if !defined(_GLES) && !defined(_GLES2) && !defined(_GLES3)
else if(buffer == glabCurDrawIndirectBuffer)
Expand Down Expand Up @@ -620,8 +638,6 @@ public struct GLAB : GLB
}
};

uint glabCurElementBuffer;

public define noEAB = GLEAB { 0 };

public struct GLCAB : GLB
Expand Down Expand Up @@ -657,11 +673,13 @@ public struct GLEAB : GLB
#endif
))
{

#if !defined(__EMSCRIPTEN__)
if(glCaps_vertexBuffer && glabCurElementBuffer != ((this != null) ? buffer : 0))
#endif
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((this != null) ? buffer : 0));
if(!glCaps_vao || glabCurVertexArray == defaultVAO)
{
if(glCaps_vertexBuffer && glabCurElementBuffer != ((this != null) ? buffer : 0))
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((this != null) ? buffer : 0));
}
else if(glCaps_vao && !glabCurVertexArray)
PrintLn("WARNING (draw): No VAO selected");
if(!glCaps_intAndDouble)
type = GL_UNSIGNED_SHORT;

Expand All @@ -680,11 +698,14 @@ public struct GLEAB : GLB
#endif
))
{
#if !defined(__EMSCRIPTEN__)
if(glCaps_vertexBuffer)
#endif
if(glabCurElementBuffer != ((this != null) ? buffer : 0))
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((this != null) ? buffer : 0));
if(!glCaps_vao || glabCurVertexArray == defaultVAO)
{
if(glCaps_vertexBuffer && glabCurElementBuffer != ((this != null) ? buffer : 0))
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((this != null) ? buffer : 0));
}
else if(glCaps_vao && !glabCurVertexArray)
PrintLn("WARNING (draw2): No VAO selected");

if(!glCaps_intAndDouble)
type = GL_UNSIGNED_SHORT;

Expand Down
Loading

0 comments on commit 4abfbe4

Please sign in to comment.