cache shader state

This commit is contained in:
gazer 2016-01-04 19:37:43 +03:00
parent 82ab5357a3
commit 6c95d1f7d6
2 changed files with 16 additions and 24 deletions

View File

@ -77,6 +77,7 @@ class GLDrawBuf : DrawBuf, GLConfigCallback {
override void afterDrawing() { override void afterDrawing() {
glSupport.setOrthoProjection(Rect(0, 0, _dx, _dy), Rect(0, 0, _dx, _dy)); glSupport.setOrthoProjection(Rect(0, 0, _dx, _dy), Rect(0, 0, _dx, _dy));
_scene.draw(); _scene.draw();
GLProgram.unbind();
glSupport.flushGL(); glSupport.flushGL();
destroy(_scene); destroy(_scene);
_scene = null; _scene = null;

View File

@ -220,14 +220,19 @@ class GLProgram {
return true; return true;
} }
static GLuint currentProgram;
/// binds program to current context /// binds program to current context
void bind() { void bind() {
checkgl!glUseProgram(program); if(program != currentProgram) {
checkgl!glUseProgram(program);
currentProgram = program;
}
} }
/// unbinds program from current context /// unbinds program from current context
void unbind() { static void unbind() {
checkgl!glUseProgram(0); checkgl!glUseProgram(0);
currentProgram = 0;
} }
/// get uniform location from program, returns -1 if location is not found /// get uniform location from program, returns -1 if location is not found
@ -274,18 +279,6 @@ class SolidFillProgram : GLProgram {
}; };
} }
void beforeExecute() {
glEnable(GL_BLEND);
checkgl!glDisable(GL_CULL_FACE);
checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
bind();
checkgl!glUniformMatrix4fv(matrixLocation, 1, false, glSupport.projectionMatrix.m.ptr);
}
void afterExecute() {
unbind();
}
protected GLint matrixLocation; protected GLint matrixLocation;
protected GLint vertexLocation; protected GLint vertexLocation;
protected GLint colAttrLocation; protected GLint colAttrLocation;
@ -296,6 +289,14 @@ class SolidFillProgram : GLProgram {
return matrixLocation >= 0 && vertexLocation >= 0 && colAttrLocation >= 0; return matrixLocation >= 0 && vertexLocation >= 0 && colAttrLocation >= 0;
} }
void beforeExecute() {
glEnable(GL_BLEND);
checkgl!glDisable(GL_CULL_FACE);
checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
bind();
checkgl!glUniformMatrix4fv(matrixLocation, 1, false, glSupport.projectionMatrix.m.ptr);
}
bool execute(float[] vertices, float[] colors) { bool execute(float[] vertices, float[] colors) {
if(!check()) if(!check())
return false; return false;
@ -314,8 +315,6 @@ class SolidFillProgram : GLProgram {
checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3); checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3);
afterExecute();
destroy(vbo); destroy(vbo);
destroy(vao); destroy(vao);
return true; return true;
@ -341,8 +340,6 @@ class LineProgram : SolidFillProgram {
checkgl!glDrawArrays(GL_LINES, 0, cast(int)vertices.length/3); checkgl!glDrawArrays(GL_LINES, 0, cast(int)vertices.length/3);
afterExecute();
destroy(vbo); destroy(vbo);
destroy(vao); destroy(vao);
return true; return true;
@ -410,12 +407,6 @@ class TextureProgram : SolidFillProgram {
checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3); checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3);
glDisableVertexAttribArray(vertexLocation);
glDisableVertexAttribArray(colAttrLocation);
glDisableVertexAttribArray(texCoordLocation);
afterExecute();
destroy(vbo); destroy(vbo);
destroy(vao); destroy(vao);