GRAPHICS: Add a method to unbind the active shader

This commit is contained in:
Bastien Bouclet 2016-01-16 08:02:13 +01:00
parent 088055e1f4
commit d1a27666d9
2 changed files with 12 additions and 3 deletions

View file

@ -124,6 +124,8 @@ struct SharedPtrProgramDeleter {
} }
}; };
Shader* Shader::_previousShader = nullptr;
Shader::Shader(const Common::String &name, GLuint vertexShader, GLuint fragmentShader, const char **attributes) Shader::Shader(const Common::String &name, GLuint vertexShader, GLuint fragmentShader, const char **attributes)
: _name(name) { : _name(name) {
assert(attributes); assert(attributes);
@ -163,9 +165,8 @@ Shader *Shader::fromFiles(const char *vertex, const char *fragment, const char *
} }
void Shader::use(bool forceReload) { void Shader::use(bool forceReload) {
static Shader *previousShader = nullptr;
static uint32 previousNumAttributes = 0; static uint32 previousNumAttributes = 0;
if (this == previousShader && !forceReload) if (this == _previousShader && !forceReload)
return; return;
// The previous shader might have had more attributes. Disable any extra ones. // The previous shader might have had more attributes. Disable any extra ones.
@ -175,7 +176,7 @@ void Shader::use(bool forceReload) {
} }
} }
previousShader = this; _previousShader = this;
previousNumAttributes = _attributes.size(); previousNumAttributes = _attributes.size();
glUseProgram(*_shaderNo); glUseProgram(*_shaderNo);
@ -246,6 +247,10 @@ void Shader::disableVertexAttribute(const char *attrib, int size, const float *d
va._const[i] = data[i]; va._const[i] = data[i];
} }
void Shader::unbind() {
glUseProgram(0);
_previousShader = nullptr;
}
} }
#endif #endif

View file

@ -143,6 +143,8 @@ public:
static Shader* fromStrings(const Common::String &name, const char *vertex, const char *fragment, const char **attributes); static Shader* fromStrings(const Common::String &name, const char *vertex, const char *fragment, const char **attributes);
void unbind();
private: private:
Shader(const Common::String &name, GLuint vertexShader, GLuint fragmentShader, const char **attributes); Shader(const Common::String &name, GLuint vertexShader, GLuint fragmentShader, const char **attributes);
@ -155,6 +157,8 @@ private:
Common::Array<VertexAttrib> _attributes; Common::Array<VertexAttrib> _attributes;
Common::SharedPtr<UniformsMap> _uniforms; Common::SharedPtr<UniformsMap> _uniforms;
static Shader *_previousShader;
}; };
} }