Skip to content

Commit 4abfbe4

Browse files
committed
ecere/gfx/drivers/GL/GLAB: Improved handling element buffers with VAO
- 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
1 parent 6b7f422 commit 4abfbe4

File tree

9 files changed

+199
-182
lines changed

9 files changed

+199
-182
lines changed

butterbur/src/opengl/shaders/butterbur.frag

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ void main(void)
428428

429429
#if FOG_ON
430430
{
431-
float fog = clamp(exp(fogZ), 0.0, 1.0);
431+
float fog = clamp(exp(-fogZ * fogZ), 0.0, 1.0);
432+
//float fog = clamp(exp(fogZ), 0.0, 1.0);
432433
c = vec4(fog * c.xyz + (1.0 - fog) * fogColor, c.w);
433434
}
434435
#endif

butterbur/src/presentation/DrawingManager.ec

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public class MDManager : DrawingManager
134134
}
135135
}
136136

137-
if(glCaps_vao) glBindVertexArray(0);
137+
if(glCaps_vao) GLABBindVertexArray(0);
138138
}
139139
}
140140

@@ -227,7 +227,7 @@ class Perspective3DManager : MDManager
227227
GLFlushMatrices();
228228

229229
texture.bind();
230-
if(glCaps_vao) glBindVertexArray(md.vao);
230+
if(glCaps_vao) GLABBindVertexArray(md.vao);
231231
for(n = 0; n < md.commandsCount; n++)
232232
{
233233
const GLDrawCommand *cmd = &md.commands[n];
@@ -251,7 +251,7 @@ class Perspective3DManager : MDManager
251251
glDisableVertexAttribArray(transform2Attribute);
252252
glDisableVertexAttribArray(transform3Attribute);
253253
}*/
254-
if(glCaps_vao) glBindVertexArray(0);
254+
if(glCaps_vao) GLABBindVertexArray(0);
255255

256256
butterburShader.transform3D = false;
257257
}
@@ -264,7 +264,7 @@ class Perspective3DManager : MDManager
264264
{
265265
PrimitiveGroup group;
266266

267-
if(glCaps_vao) glBindVertexArray(defaultVAO);
267+
if(glCaps_vao) GLABBindVertexArray(defaultVAO);
268268
glDisplay.SelectMesh(mesh);
269269

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

286-
if(glCaps_vao) glBindVertexArray(0);
286+
if(glCaps_vao) GLABBindVertexArray(0);
287287
}
288288
}
289289
}

butterbur/src/presentation/GraphicalSurface.ec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public class GraphicalSurface : MultiPresentation
113113
#endif
114114

115115
glDisable(GL_SCISSOR_TEST);
116-
if(glCaps_vao) glBindVertexArray(0);
116+
if(glCaps_vao) GLABBindVertexArray(0);
117117
glBindFramebuffer(GL_FRAMEBUFFER, texturesFramebuffer);
118118
butterburShader.activate();
119119
butterburShader.lighting(false);
@@ -268,7 +268,7 @@ public class GraphicalSurface : MultiPresentation
268268
butterburShader.textureArray(false);
269269
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
270270
glEnable(GL_SCISSOR_TEST);
271-
if(glCaps_vao) glBindVertexArray(defaultVAO);
271+
if(glCaps_vao) GLABBindVertexArray(defaultVAO);
272272
DefaultShader::shader().texturing(false);
273273
DefaultShader::shader().texturing(true);
274274
DefaultShader::shader().activate();

butterbur/src/presentation/TIManager.ec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ public class TIManager : DrawingManager
6969
float transform[2] = { originOffset.x, originOffset.y };
7070
if(drawManager)
7171
drawManager.ready(width, height);
72-
if(glCaps_vao) glBindVertexArray(defaultVAO);
72+
if(glCaps_vao) GLABBindVertexArray(defaultVAO);
7373
presentation.prepareDraw(renderFlags, this, transform);
7474
}
7575

7676
void draw()
7777
{
7878
// TODO: Proper VAO support for text & images?
79-
if(glCaps_vao) glBindVertexArray(defaultVAO);
79+
if(glCaps_vao) GLABBindVertexArray(defaultVAO);
8080
if(drawManager)
8181
drawManager.flushImages();
8282
if(glCaps_shaders)
8383
glEnableVertexAttribArray(GLBufferContents::vertex);
84-
if(glCaps_vao) glBindVertexArray(0);
84+
if(glCaps_vao) GLABBindVertexArray(0);
8585
}
8686

8787
void addTextCommand(const String text, GEFont font, float opacity, Alignment2D alignment, float * transform)

ecere/src/gfx/drivers/OpenGLDisplayDriver.ec

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ Shader activeShader;
398398

399399
static int displayWidth, displayHeight;
400400

401+
uint defaultVAO; // Only works with a single display / context
402+
401403
static bool useSingleGLContext = false;
402404
class OGLDisplay : struct
403405
{
@@ -1373,8 +1375,11 @@ class OpenGLDisplayDriver : DisplayDriver
13731375
#if ENABLE_GL_VAO
13741376
if(oglDisplay.capabilities.vao)
13751377
{
1378+
// VAOs cannot be shared across contexts, but in single context mode
1379+
// we should re-use the same VAO across displays.
13761380
glGenVertexArrays(1, &oglDisplay.vao);
1377-
glBindVertexArray(oglDisplay.vao);
1381+
defaultVAO = oglDisplay.vao; // NOTE: This currently only works with a single display / context
1382+
GLABBindVertexArray(oglDisplay.vao);
13781383
}
13791384
#endif
13801385

@@ -1413,8 +1418,7 @@ class OpenGLDisplayDriver : DisplayDriver
14131418
GLDisableClientState(TANGENTS1);
14141419
GLDisableClientState(TANGENTS2);
14151420
#if ENABLE_GL_VAO
1416-
if(glBindVertexArray)
1417-
glBindVertexArray(0);
1421+
GLABBindVertexArray(0);
14181422
#endif
14191423
if(glUseProgram)
14201424
glUseProgram(0);
@@ -1425,7 +1429,7 @@ class OpenGLDisplayDriver : DisplayDriver
14251429

14261430
#if ENABLE_GL_VAO
14271431
if(glCaps_vao)
1428-
glBindVertexArray(oglDisplay.vao);
1432+
GLABBindVertexArray(oglDisplay.vao);
14291433
#endif
14301434

14311435
GLEnableClientState(VERTICES);
@@ -2039,7 +2043,7 @@ class OpenGLDisplayDriver : DisplayDriver
20392043
if(glCaps_vao)
20402044
{
20412045
OGLDisplay oglDisplay = display.driverData;
2042-
glBindVertexArray(oglDisplay.vao);
2046+
GLABBindVertexArray(oglDisplay.vao);
20432047
}
20442048
#endif
20452049
GLABBindBuffer(GL_ARRAY_BUFFER, 0);

ecere/src/gfx/drivers/gl3/GLMultiDraw.ec

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ public struct GLMultiDraw
639639

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

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

683683
#if (!defined(_GLES) && !defined(_GLES2)) || defined(_GLES3)
684-
if(glCaps_vao) glBindVertexArray(vao);
684+
if(glCaps_vao) GLABBindVertexArray(vao);
685685
#endif
686686
GLFlushMatrices();
687687

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

699-
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexGLMB.ab.buffer);
699+
if(!glCaps_vao) // Don't modify VAO state.
700+
{
701+
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexGLMB.ab.buffer);
702+
703+
glDisableVertexAttribArray((GLBufferContents)drawIDAttribute);
704+
glDisableVertexAttribArray((GLBufferContents)posOffsetAttribute);
705+
}
706+
else if(vao == defaultVAO)
707+
PrintLn("WARNING: MultiDraw with default VAO");
708+
else if(!glabCurVertexArray)
709+
PrintLn("WARNING (MultiDraw): No VAO selected");
700710

701-
glDisableVertexAttribArray((GLBufferContents)drawIDAttribute);
702-
glDisableVertexAttribArray((GLBufferContents)posOffsetAttribute);
703711
for(n = 0; n < commandsCount; n++)
704712
{
705713
const GLDrawCommand *cmd = &commands[n];
@@ -783,7 +791,7 @@ public struct GLMultiDraw
783791
#endif
784792
#endif
785793
#if (!defined(_GLES) && !defined(_GLES2)) || defined(_GLES3)
786-
if(glCaps_vao) glBindVertexArray(0);
794+
if(glCaps_vao) GLABBindVertexArray(0);
787795
#endif
788796
}
789797
};

ecere/src/gfx/drivers/gl3/glab.ec

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ default dllexport void GLABUnbindBuffer(int target)
1515
glBindBuffer(target, 0);
1616
if(target == GL_ARRAY_BUFFER)
1717
glabCurArrayBuffer = 0;
18-
else if(target == GL_ELEMENT_ARRAY_BUFFER)
18+
else if(target == GL_ELEMENT_ARRAY_BUFFER && (!glCaps_vao || glabCurVertexArray == defaultVAO))
1919
glabCurElementBuffer = 0;
2020
// NOTE: Actually ES 3.1 is required, separate define?
2121
#if !defined(_GLES) && !defined(_GLES2) && !defined(_GLES3)
@@ -33,7 +33,7 @@ public void GLABBindBuffer(int target, uint buffer)
3333
glBindBuffer(target, buffer);
3434
if(target == GL_ARRAY_BUFFER)
3535
glabCurArrayBuffer = buffer;
36-
else if(target == GL_ELEMENT_ARRAY_BUFFER)
36+
else if(target == GL_ELEMENT_ARRAY_BUFFER && (!glCaps_vao || glabCurVertexArray == defaultVAO))
3737
glabCurElementBuffer = buffer;
3838
// NOTE: Actually ES 3.1 is required, separate define?
3939
#if !defined(_GLES) && !defined(_GLES2) && !defined(_GLES3)
@@ -43,10 +43,6 @@ public void GLABBindBuffer(int target, uint buffer)
4343
}
4444
}
4545

46-
#if !defined(_GLES) && !defined(_GLES2) && !defined(_GLES3)
47-
uint glabCurDrawIndirectBuffer;
48-
#endif
49-
5046
public enum GLBufferContents { vertex, normal, texCoord, color, tangent1, tangent2, lightVector, boneIndices1, boneIndices2, boneIndices3, boneWeights1, boneWeights2, boneWeights3 };
5147

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

5652
public define noAB = GLAB { 0 };
5753

58-
uint glabCurArrayBuffer;
54+
public void GLABBindVertexArray(uint vao)
55+
{
56+
#if (!defined(_GLES) && !defined(_GLES2)) || defined(_GLES3)
57+
if(glCaps_vao) // && vao != glabCurVertexArray) // VAOs are not shared across contexts / displays
58+
{
59+
#ifdef _DEBUG
60+
if(vao != glabCurVertexArray)
61+
;//PrintLn("WARNING: Redundant VAO binding");
62+
#endif
63+
glBindVertexArray(vao);
64+
glabCurVertexArray = vao;
65+
}
66+
#endif
67+
}
68+
69+
uint glabCurVertexArray; // Currently bound VAO.
70+
71+
uint glabCurArrayBuffer; // Buffer currently bound for GL_ARRAY_BUFFER. *NOT* part of VAO state
72+
uint glabCurElementBuffer; // Buffer currently bound for GL_ELEMENT_ARRAY_BUFFER. This *IS* part of the VAO state. With VAOs, this is for 'defaultVAO'.
73+
74+
#if !defined(_GLES) && !defined(_GLES2) && !defined(_GLES3)
75+
uint glabCurDrawIndirectBuffer;
76+
#endif
5977

6078
static short *shortVPBuffer = null;
6179
static uint shortVPSize = 0;
@@ -508,7 +526,7 @@ public struct GLB
508526
{
509527
if(buffer == glabCurArrayBuffer)
510528
GLABBindBuffer(GL_ARRAY_BUFFER, 0);
511-
else if(buffer == glabCurElementBuffer)
529+
else if(buffer == glabCurElementBuffer && (!glCaps_vao || glabCurVertexArray == defaultVAO))
512530
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
513531
#if !defined(_GLES) && !defined(_GLES2) && !defined(_GLES3)
514532
else if(buffer == glabCurDrawIndirectBuffer)
@@ -620,8 +638,6 @@ public struct GLAB : GLB
620638
}
621639
};
622640

623-
uint glabCurElementBuffer;
624-
625641
public define noEAB = GLEAB { 0 };
626642

627643
public struct GLCAB : GLB
@@ -657,11 +673,13 @@ public struct GLEAB : GLB
657673
#endif
658674
))
659675
{
660-
661-
#if !defined(__EMSCRIPTEN__)
662-
if(glCaps_vertexBuffer && glabCurElementBuffer != ((this != null) ? buffer : 0))
663-
#endif
664-
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((this != null) ? buffer : 0));
676+
if(!glCaps_vao || glabCurVertexArray == defaultVAO)
677+
{
678+
if(glCaps_vertexBuffer && glabCurElementBuffer != ((this != null) ? buffer : 0))
679+
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((this != null) ? buffer : 0));
680+
}
681+
else if(glCaps_vao && !glabCurVertexArray)
682+
PrintLn("WARNING (draw): No VAO selected");
665683
if(!glCaps_intAndDouble)
666684
type = GL_UNSIGNED_SHORT;
667685

@@ -680,11 +698,14 @@ public struct GLEAB : GLB
680698
#endif
681699
))
682700
{
683-
#if !defined(__EMSCRIPTEN__)
684-
if(glCaps_vertexBuffer)
685-
#endif
686-
if(glabCurElementBuffer != ((this != null) ? buffer : 0))
687-
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((this != null) ? buffer : 0));
701+
if(!glCaps_vao || glabCurVertexArray == defaultVAO)
702+
{
703+
if(glCaps_vertexBuffer && glabCurElementBuffer != ((this != null) ? buffer : 0))
704+
GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((this != null) ? buffer : 0));
705+
}
706+
else if(glCaps_vao && !glabCurVertexArray)
707+
PrintLn("WARNING (draw2): No VAO selected");
708+
688709
if(!glCaps_intAndDouble)
689710
type = GL_UNSIGNED_SHORT;
690711

0 commit comments

Comments
 (0)