Browse Source

Tracking AbstractShaderProgram state.

Reduces OpenGL call count when subsequently using the same shader for
rendering.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
2dde4b9bf8
  1. 18
      src/AbstractShaderProgram.cpp
  2. 2
      src/AbstractShaderProgram.h
  3. 31
      src/Implementation/ShaderProgramState.h
  4. 4
      src/Implementation/State.cpp
  5. 2
      src/Implementation/State.h

18
src/AbstractShaderProgram.cpp

@ -18,6 +18,8 @@
#include <fstream>
#include "Shader.h"
#include "Implementation/State.h"
#include "Implementation/ShaderProgramState.h"
#define LINKER_MESSAGE_MAX_LENGTH 1024
@ -25,10 +27,24 @@ using namespace std;
namespace Magnum {
AbstractShaderProgram::~AbstractShaderProgram() {
/* Remove current usage from the state */
GLuint& current = Context::current()->state()->shaderProgram->current;
if(current == _id)
current = 0;
glDeleteProgram(_id);
}
bool AbstractShaderProgram::use() {
if(state != Linked) return false;
glUseProgram(_id);
/* Use only if the program isn't already in use */
GLuint& current = Context::current()->state()->shaderProgram->current;
if(current != _id) {
current = _id;
glUseProgram(_id);
}
return true;
}

2
src/AbstractShaderProgram.h

@ -642,8 +642,6 @@ class MAGNUM_EXPORT AbstractShaderProgram {
State state;
};
inline AbstractShaderProgram::~AbstractShaderProgram() { glDeleteProgram(_id); }
}
#endif

31
src/Implementation/ShaderProgramState.h

@ -0,0 +1,31 @@
#ifndef Magnum_Implementation_ShaderProgramState_h
#define Magnum_Implementation_ShaderProgramState_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include "Magnum.h"
namespace Magnum { namespace Implementation {
struct ShaderProgramState {
inline constexpr ShaderProgramState(): current(0) {}
/* Currently used program */
GLuint current;
};
}}
#endif

4
src/Implementation/State.cpp

@ -16,12 +16,14 @@
#include "State.h"
#include "BufferState.h"
#include "ShaderProgramState.h"
namespace Magnum { namespace Implementation {
State::State(): buffer(new BufferState) {}
State::State(): buffer(new BufferState), shaderProgram(new ShaderProgramState) {}
State::~State() {
delete shaderProgram;
delete buffer;
}

2
src/Implementation/State.h

@ -20,12 +20,14 @@
namespace Magnum { namespace Implementation {
struct BufferState;
struct ShaderProgramState;
struct State {
State();
~State();
BufferState* const buffer;
ShaderProgramState* const shaderProgram;
};
}}

Loading…
Cancel
Save