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() {
glSupport.setOrthoProjection(Rect(0, 0, _dx, _dy), Rect(0, 0, _dx, _dy));
_scene.draw();
GLProgram.unbind();
glSupport.flushGL();
destroy(_scene);
_scene = null;

View File

@ -220,14 +220,19 @@ class GLProgram {
return true;
}
static GLuint currentProgram;
/// binds program to current context
void bind() {
if(program != currentProgram) {
checkgl!glUseProgram(program);
currentProgram = program;
}
}
/// unbinds program from current context
void unbind() {
static void unbind() {
checkgl!glUseProgram(0);
currentProgram = 0;
}
/// 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 vertexLocation;
protected GLint colAttrLocation;
@ -296,6 +289,14 @@ class SolidFillProgram : GLProgram {
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) {
if(!check())
return false;
@ -314,8 +315,6 @@ class SolidFillProgram : GLProgram {
checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3);
afterExecute();
destroy(vbo);
destroy(vao);
return true;
@ -341,8 +340,6 @@ class LineProgram : SolidFillProgram {
checkgl!glDrawArrays(GL_LINES, 0, cast(int)vertices.length/3);
afterExecute();
destroy(vbo);
destroy(vao);
return true;
@ -410,12 +407,6 @@ class TextureProgram : SolidFillProgram {
checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3);
glDisableVertexAttribArray(vertexLocation);
glDisableVertexAttribArray(colAttrLocation);
glDisableVertexAttribArray(texCoordLocation);
afterExecute();
destroy(vbo);
destroy(vao);