MorphoGraphX
Shader.hpp
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef SHADER_H
12 #define SHADER_H
13 
14 #include <Config.hpp>
15 #include <GL.hpp>
16 
17 #include <Assert.hpp>
18 #include <Matrix.hpp>
19 #include <Parms.hpp>
20 #include <Vector.hpp>
21 
22 #include <iostream>
23 #include <QHash>
24 #include <QString>
25 #include <string>
26 #include <vector>
27 
28 #define CHECK_GL_ERROR(cmd) \
29  cmd; \
30  Shader::reportGLError(#cmd, __FILE__, __LINE__)
31 #define REPORT_GL_ERROR(str) Shader::reportGLError(str, __FILE__, __LINE__)
32 
33 class QString;
34 
35 namespace mgx {
36 
37 typedef util::Vector<1, GLint> ivec1;
38 typedef util::Vector<2, GLint> ivec2;
39 typedef util::Vector<3, GLint> ivec3;
40 typedef util::Vector<4, GLint> ivec4;
41 typedef util::Vector<1, GLfloat> vec1;
42 typedef util::Vector<2, GLfloat> vec2;
43 typedef util::Vector<3, GLfloat> vec3;
44 typedef util::Vector<4, GLfloat> vec4;
45 typedef util::Matrix<2, 2, GLfloat> mat2;
46 typedef util::Matrix<3, 3, GLfloat> mat3;
47 typedef util::Matrix<4, 4, GLfloat> mat4;
48 
49 enum UNIFORM_TYPE {
50  UNIFORM_INT,
51  UNIFORM_INT2,
52  UNIFORM_INT3,
53  UNIFORM_INT4,
54  UNIFORM_FLOAT,
55  UNIFORM_FLOAT2,
56  UNIFORM_FLOAT3,
57  UNIFORM_FLOAT4,
58  UNIFORM_MATRIX2,
59  UNIFORM_MATRIX3,
60  UNIFORM_MATRIX4
61 };
62 
63 class mgx_EXPORT GLSLValue {
64  class Value {
65 public:
66  virtual ~Value() {
67  }
68  virtual void setUniform(GLint location) const = 0;
69  virtual void setAttrib(GLuint location) const = 0;
70  virtual QTextStream& read(QTextStream& s) = 0;
71  virtual QTextStream& write(QTextStream& s) const = 0;
72  virtual std::istream& read(std::istream& s) = 0;
73  virtual std::ostream& write(std::ostream& s) const = 0;
74  virtual Value* copy() = 0;
75  }* value;
76 
77  template <typename T> class ValueImpl : public Value {
78 public:
79  typedef typename T::value_type value_type;
80  typedef void (*uniform_fct)(GLint, GLsizei, const value_type*);
81  typedef void (*attrib_fct)(GLuint, const value_type*);
82 
83  ValueImpl(uniform_fct ufct, attrib_fct afct, const T* v, int count)
84  : Value()
85  , value(v, v + count)
86  , glUniform(ufct)
87  , glVertexAttrib(afct)
88  {
89  }
90 
91  ValueImpl(uniform_fct ufct, attrib_fct afct)
92  : Value()
93  , glUniform(ufct)
94  , glVertexAttrib(afct)
95  {
96  }
97 
98  ValueImpl(const ValueImpl& copy)
99  : Value()
100  , value(copy.value)
101  , glUniform(copy.glUniform)
102  , glVertexAttrib(copy.glVertexAttrib)
103  {
104  }
105 
106  virtual Value* copy() {
107  return new ValueImpl(*this);
108  }
109 
110  virtual void setUniform(GLint location) const {
111  glUniform(location, (GLint)value.size(), value[0].c_data());
112  }
113  virtual void setAttrib(GLuint location) const {
114  glVertexAttrib(location, value[0].c_data());
115  }
116  virtual QTextStream& read(QTextStream& s)
117  {
118  value.resize(1);
119  s >> value[0];
120  return s;
121  }
122  virtual QTextStream& write(QTextStream& s) const
123  {
124  s << value[0];
125  return s;
126  }
127  virtual std::istream& read(std::istream& s)
128  {
129  value.resize(1);
130  s >> value[0];
131  return s;
132  }
133  virtual std::ostream& write(std::ostream& s) const
134  {
135  s << value[0];
136  return s;
137  }
138  std::vector<T> value;
139  uniform_fct glUniform;
140  attrib_fct glVertexAttrib;
141  };
142 
143  UNIFORM_TYPE type;
144 
145 public:
146  GLSLValue()
147  : value(0)
148  , type(UNIFORM_INT)
149  {
150  }
151 
152  GLSLValue(const GLSLValue& copy)
153  : value(0)
154  , type(copy.type)
155  {
156  if(copy.value)
157  value = copy.value->copy();
158  }
159 
160  template <typename T>
161  explicit GLSLValue(const T& val)
162  : value(0)
163  {
164  setValue(val);
165  }
166 
167  template <typename T>
168  explicit GLSLValue(const std::vector<T>& val)
169  : value(0)
170  {
171  setValue(val);
172  }
173 
174  template <typename T>
175  explicit GLSLValue(const T* val, int count)
176  : value(0)
177  {
178  setValue(val, count);
179  }
180 
181  ~GLSLValue() {
182  delete value;
183  }
184  GLSLValue& operator=(const GLSLValue& copy)
185  {
186  delete value;
187  value = 0;
188  if(copy.value)
189  value = copy.value->copy();
190  type = copy.type;
191  return *this;
192  }
193  void setUniform(GLint location) const {
194  value->setUniform(location);
195  }
196  void setAttrib(GLuint location) const {
197  value->setAttrib(location);
198  }
199  std::istream& read(std::istream& s);
200  std::ostream& write(std::ostream& s) const;
201  QTextStream& read(QTextStream& s);
202  QTextStream& write(QTextStream& s) const;
203 
204  void setValue(const GLint* value, int count);
205  void setValue(const ivec1* value, int count);
206  void setValue(const ivec2* value, int count);
207  void setValue(const ivec3* value, int count);
208  void setValue(const ivec4* value, int count);
209  void setValue(const GLfloat* value, int count);
210  void setValue(const vec1* value, int count);
211  void setValue(const vec2* value, int count);
212  void setValue(const vec3* value, int count);
213  void setValue(const vec4* value, int count);
214  void setValue(const mat2* value, int count);
215  void setValue(const mat3* value, int count);
216  void setValue(const mat4* value, int count);
217 
218  void setValue(const GLint& value) {
219  setValue(&value, 1);
220  }
221  void setValue(const ivec1& value) {
222  setValue(&value, 1);
223  }
224  void setValue(const ivec2& value) {
225  setValue(&value, 1);
226  }
227  void setValue(const ivec3& value) {
228  setValue(&value, 1);
229  }
230  void setValue(const ivec4& value) {
231  setValue(&value, 1);
232  }
233  void setValue(const GLfloat& value) {
234  setValue(&value, 1);
235  }
236  void setValue(const vec1& value) {
237  setValue(&value, 1);
238  }
239  void setValue(const vec2& value) {
240  setValue(&value, 1);
241  }
242  void setValue(const vec3& value) {
243  setValue(&value, 1);
244  }
245  void setValue(const vec4& value) {
246  setValue(&value, 1);
247  }
248  void setValue(const mat2& value) {
249  setValue(&value, 1);
250  }
251  void setValue(const mat3& value) {
252  setValue(&value, 1);
253  }
254  void setValue(const mat4& value) {
255  setValue(&value, 1);
256  }
257 
258  bool valid() const {
259  return value != 0;
260  }
261 };
262 
263 inline QTextStream& operator<<(QTextStream& s, const GLSLValue& ut) {
264  return ut.write(s);
265 }
266 
267 inline QTextStream& operator>>(QTextStream& s, GLSLValue& ut) {
268  return ut.read(s);
269 }
270 
271 inline std::ostream& operator<<(std::ostream& s, const GLSLValue& ut) {
272  return ut.write(s);
273 }
274 
275 inline std::istream& operator>>(std::istream& s, GLSLValue& ut) {
276  return ut.read(s);
277 }
278 
279 class mgx_EXPORT Shader {
280 public:
281  enum ActiveTextures {
282  AT_NONE = 0,
283  AT_TEX3D,
284  AT_SECOND_TEX3D,
285  AT_TEX2D,
286  AT_LABEL_TEX,
287  AT_SURF_TEX,
288  AT_HEAT_TEX,
289  AT_DEPTH_TEX,
290  AT_CMAP_TEX,
291  AT_SECOND_CMAP_TEX,
292  AT_SURF_RENDER_TEX,
293  AT_FINAL_VOLUME_TEX,
294  AT_FRONT_TEX,
295  AT_BACK_TEX,
296  AT_FRONT_COLOR_TEX,
297  AT_OCCLUSION_TEX,
298  AT_END
299  };
300  Shader(int verbosity = 1);
301 
302  static void activeTexture(ActiveTextures at) {
303  glActiveTexture(at + GL_TEXTURE0);
304  }
305 
306  static void activeTexture(int at) {
307  glActiveTexture(at + GL_TEXTURE0);
308  }
309 
310  bool init();
311  void invalidate() {
312  _initialized = false;
313  }
314 
315  // void readParms(Parms& parms, QString shaders_section, QString uniforms_section);
316 
317  bool setupShaders();
318  bool useShaders();
319  static bool stopUsingShaders();
320 
321  static bool reportGLError(const char* id, const char* file, int line);
322  static bool reportGLError(const QString& id, const char* file, int line);
323 
324  bool isVertexShaderCode(unsigned int pos) const {
325  return vertex_shaders_code[pos].second;
326  }
327 
328  bool isFragmentShaderCode(unsigned int pos) const {
329  return fragment_shaders_code[pos].second;
330  }
331 
332  const QString& getVertexShader(unsigned int pos) const {
333  return vertex_shaders_code[pos].first;
334  }
335 
336  const QString& getFragmentShader(unsigned int pos) const {
337  return fragment_shaders_code[pos].first;
338  }
339 
340  void addVertexShaderCode(const QString& code);
341  bool changeVertexShaderCode(int pos, const QString& code);
342  void removeVertexShaderCode(const QString& code);
343  void addFragmentShaderCode(const QString& code);
344  bool changeFragmentShaderCode(int pos, const QString& code);
345  void removeFragmentShaderCode(const QString& code);
346 
347  void addVertexShader(const QString& filename);
348  bool changeVertexShader(int pos, const QString& filename);
349  void removeVertexShader(const QString& filename);
350  void addFragmentShader(const QString& filename);
351  bool changeFragmentShader(int pos, const QString& filename);
352  void removeFragmentShader(const QString& filename);
353  void setVerbosity(int verb) {
354  verbosity = verb;
355  }
356 
357  QString shaderTypeName(GLenum shader_type);
358 
359  GLuint compileShaderFile(GLenum shader_type, QString filename);
360  GLuint compileShader(GLenum shader_type, QString content);
361 
362  void printProgramInfoLog(GLuint object);
363  void printShaderInfoLog(GLuint object);
364  void cleanShaders();
365 
366  bool hasShaders() const {
367  return has_shaders;
368  }
369 
370  GLuint program() const {
371  return _program;
372  }
373 
374  void setupUniforms();
375 
376  GLuint attribLocation(const QString& name);
377 
378  void setAttrib(const QString& name, const GLSLValue& value);
379  void setAttrib(GLuint loc, const GLSLValue& value);
380 
381  bool setUniform(const QString& name, const GLSLValue& value);
382  void setUniform_instant(const QString& name, const GLSLValue& value);
383 
384  bool initialized() const {
385  return _initialized;
386  }
387 
388 protected:
389  void loadUniform(GLint program, const QString& name, const GLSLValue& value);
390 
391  bool has_shaders;
392 
393  // if bool is true, then it's really code, otherwise, it's a file
394  typedef std::pair<QString, bool> code_t;
395  std::vector<code_t> vertex_shaders_code, fragment_shaders_code;
396 
397  std::vector<GLuint> vertex_shaders, fragment_shaders;
398 
399  std::vector<QString> uniform_names, model_uniform_names;
400  std::vector<GLSLValue> uniform_values, model_uniform_values;
401 
402  int verbosity;
403 
404  GLuint _program;
405 
406  bool _initialized;
407 };
408 } // namespace mgx
409 #endif // SHADER_H
Defines the util::Parms class.
Definition: Shader.hpp:279
Graphical (or textual) assertion utility.
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
Definition: Shader.hpp:63
Defines the Matrix class template This file is shared by cuda, do not include headers that nvcc can&#39;t...
Defines the Vector class template This file is shared by cuda, do not include headers that nvcc can&#39;t...