-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaderprogram.h
184 lines (141 loc) · 5.87 KB
/
shaderprogram.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#pragma once
#include "context.h"
#include <array>
#include <vector>
#include <unordered_map>
#include <memory>
#include <string>
#include <glm/glm.hpp>
namespace opengl
{
#define GLSL_CODE(version, shader) "#version " #version "\n" #shader
enum class shader_type : GLenum
{
vertex = GL_VERTEX_SHADER,
fragment = GL_FRAGMENT_SHADER,
geometry = GL_GEOMETRY_SHADER,
tesselation_control = GL_TESS_CONTROL_SHADER,
tesselation_eval = GL_TESS_EVALUATION_SHADER,
compute = GL_COMPUTE_SHADER
};
class context;
class shader;
class shader_program
{
private:
context& m_context;
GLuint m_handle;
bool m_linked;
std::unordered_map<std::string, unsigned int> m_uniform_location;
std::array<std::unique_ptr<shader>, 6> m_shader;
public:
~shader_program();
GLint uniform_location(const std::string& name) const;
template <typename T> void uniform(const std::string& name, const T& value);
template <typename T> void uniform(const std::string& name, const T& x, const T& y);
template <typename T> void uniform(const std::string& name, const T& x, const T& y, const T& z);
template <typename T> void uniform(const std::string& name, const T& x, const T& y, const T& z, const T& w);
template <typename T, std::size_t S> void uniform(const std::string& name, const std::array<T, S>& value);
void attach(const std::string& source, shader_type type);
bool load(const std::string& path, shader_type type);
void link();
bool linked() const;
void bind();
void unbind();
GLuint gl_handle() const;
private:
shader_program(context& gl_context);
shader_program(const shader_program&) = delete;
shader_program& operator = (const shader_program&) = delete;
void validate();
void collect_uniforms();
friend context;
};
namespace detail
{
void uniform(GLint location, const glm::vec2& v);
void uniform(GLint location, const glm::vec3& v);
void uniform(GLint location, const glm::vec4& v);
void uniform(GLint location, const glm::dvec2& v);
void uniform(GLint location, const glm::dvec3& v);
void uniform(GLint location, const glm::dvec4& v);
void uniform(GLint location, const glm::ivec2& v);
void uniform(GLint location, const glm::ivec3& v);
void uniform(GLint location, const glm::ivec4& v);
void uniform(GLint location, const glm::uvec2& v);
void uniform(GLint location, const glm::uvec3& v);
void uniform(GLint location, const glm::uvec4& v);
void uniform(GLint location, const glm::bvec2& v);
void uniform(GLint location, const glm::bvec3& v);
void uniform(GLint location, const glm::bvec4& v);
void uniform(GLint location, const glm::mat2& m);
void uniform(GLint location, const glm::mat3& m);
void uniform(GLint location, const glm::mat4& m);
void uniform(GLint location, const glm::mat2x3& m);
void uniform(GLint location, const glm::mat2x4& m);
void uniform(GLint location, const glm::mat3x2& m);
void uniform(GLint location, const glm::mat3x4& m);
void uniform(GLint location, const glm::mat4x2& m);
void uniform(GLint location, const glm::mat4x3& m);
void uniform(GLint location, const glm::dmat2& m);
void uniform(GLint location, const glm::dmat3& m);
void uniform(GLint location, const glm::dmat4& m);
void uniform(GLint location, const glm::dmat2x3& m);
void uniform(GLint location, const glm::dmat2x4& m);
void uniform(GLint location, const glm::dmat3x2& m);
void uniform(GLint location, const glm::dmat3x4& m);
void uniform(GLint location, const glm::dmat4x2& m);
void uniform(GLint location, const glm::dmat4x3& m);
void uniform(GLint location, GLfloat x);
void uniform(GLint location, GLfloat x, GLfloat y);
void uniform(GLint location, GLfloat x, GLfloat y, GLfloat z);
void uniform(GLint location, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
void uniform(GLint location, GLdouble x);
void uniform(GLint location, GLdouble x, GLdouble y);
void uniform(GLint location, GLdouble x, GLdouble y, GLdouble z);
void uniform(GLint location, GLdouble x, GLdouble y, GLdouble z, GLdouble w);
void uniform(GLint location, GLint x);
void uniform(GLint location, GLint x, GLint y);
void uniform(GLint location, GLint x, GLint y, GLint z);
void uniform(GLint location, GLint x, GLint y, GLint z, GLint w);
void uniform(GLint location, GLuint x);
void uniform(GLint location, GLuint x, GLuint y);
void uniform(GLint location, GLuint x, GLuint y, GLuint z);
void uniform(GLint location, GLuint x, GLuint y, GLuint z, GLuint w);
void uniform(GLint location, GLboolean x);
void uniform(GLint location, GLboolean x, GLboolean y);
void uniform(GLint location, GLboolean x, GLboolean y, GLboolean z);
void uniform(GLint location, GLboolean x, GLboolean y, GLboolean z, GLboolean w);
void uniform(GLint location, const glm::mat4& m, const std::size_t size);
}
template <typename T>
void shader_program::uniform(const std::string& name, const T& value)
{
GLint location = uniform_location(name);
detail::uniform(location, value);
}
template <typename T>
void shader_program::uniform(const std::string& name, const T& x, const T& y)
{
GLint location = uniform_location(name);
detail::uniform(location, x, y);
}
template <typename T>
void shader_program::uniform(const std::string& name, const T& x, const T& y, const T& z)
{
GLint location = uniform_location(name);
detail::uniform(location, x, y, z);
}
template <typename T>
void shader_program::uniform(const std::string& name, const T& x, const T& y, const T& z, const T& w)
{
GLint location = uniform_location(name);
detail::uniform(location, x, y, z, w);
}
template <typename T, std::size_t S>
void shader_program::uniform(const std::string& name, const std::array<T, S>& value)
{
GLint location = uniform_location(name);
detail::uniform(location, value[0], S);
}
}