Skip to content

Commit f1c7029

Browse files
authored
feat: implement uniform caching in ShaderProgram (#13)
1 parent 7ee3436 commit f1c7029

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

src/common/shader/shader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ bool our::ShaderProgram::attach(const std::string& filename, GLenum type) const
3737
}
3838

3939
bool our::ShaderProgram::link() const {
40+
uniformLocations.clear();
4041
glLinkProgram(program);
4142
std::string error = checkForLinkingErrors(program);
4243
if (!error.empty()) {

src/common/shader/shader.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
#include <glm/glm.hpp>
77
#include <glm/gtc/type_ptr.hpp>
88
#include <string>
9+
#include <unordered_map>
910

1011
namespace our {
1112

1213
class ShaderProgram {
1314
private:
1415
// Shader Program Handle
1516
GLuint program;
17+
mutable std::unordered_map<std::string, GLint> uniformLocations;
1618

1719
public:
1820
ShaderProgram() {
@@ -30,8 +32,12 @@ namespace our {
3032
glUseProgram(program);
3133
}
3234

33-
GLuint getUniformLocation(const std::string& name) {
34-
return glGetUniformLocation(program, name.c_str());
35+
GLint getUniformLocation(const std::string& name) {
36+
if (uniformLocations.find(name) != uniformLocations.end()) return uniformLocations[name];
37+
38+
GLint location = glGetUniformLocation(program, name.c_str());
39+
uniformLocations[name] = location;
40+
return location;
3541
}
3642

3743
void set(const std::string& uniform, GLfloat value) {

0 commit comments

Comments
 (0)