Developer Documentation
GLSLShader.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 
43 
44 #ifndef GLSLSHADER_H
45 #define GLSLSHADER_H
46 
47 //==============================================================================
48 
49 #include <ACG/Config/ACGDefines.hh>
50 #include <ACG/Math/VectorT.hh>
51 #include <ACG/Math/GLMatrixT.hh>
52 #include <ACG/GL/gl.hh>
53 
54 #include <list>
55 #include <string>
56 #include <QStringList>
57 
58 //==============================================================================
59 
63 namespace GLSL {
64 
65 #define GLSL_MAX_LOGSIZE 16384
66 
67  typedef std::list<std::string> StringList;
68 
71  class ACGDLLEXPORT Shader {
72 
73  public:
74  explicit Shader(GLenum shaderType);
75  virtual ~Shader();
76  void setSource(const StringList& source);
77  void setSource(const QStringList& source);
78 
79  // FIXME implement StringList getSource();
80  bool compile(bool verbose = true);
81 
82  protected:
83  GLuint m_shaderId;
84 
85  friend class Program;
86  };
87 
88  typedef Shader* PtrShader;
89  typedef const Shader* PtrConstShader;
90 
91  //--------------------------------------------------------------------------
92 
95  class ACGDLLEXPORT VertexShader : public Shader {
96 
97  public:
98  VertexShader();
99  virtual ~VertexShader();
100 
101  };
102 
103  typedef VertexShader* PtrVertexShader;
104  typedef const VertexShader* PtrVertexConstShader;
105 
106  //--------------------------------------------------------------------------
107 
110  class ACGDLLEXPORT FragmentShader : public Shader {
111 
112  public:
113  FragmentShader();
114  virtual ~FragmentShader();
115  };
116 
118  typedef const FragmentShader* PtrConstFragmentShader;
119 
120  //--------------------------------------------------------------------------
121 
124  class ACGDLLEXPORT GeometryShader : public Shader {
125 
126  public:
127  GeometryShader();
128  virtual ~GeometryShader();
129  };
130 
132  typedef const GeometryShader* PtrConstGeometryShader;
133 
134  //--------------------------------------------------------------------------
135 
136 #ifdef GL_ARB_tessellation_shader
137 
140  class ACGDLLEXPORT TessControlShader : public Shader {
141 
142  public:
143  TessControlShader();
144  virtual ~TessControlShader();
145  };
146 
147  typedef TessControlShader* PtrTessControlShader;
148  typedef const TessControlShader* PtrConstTessControlShader;
149 
150  //--------------------------------------------------------------------------
151 
154  class ACGDLLEXPORT TessEvaluationShader : public Shader {
155 
156  public:
157  TessEvaluationShader();
158  virtual ~TessEvaluationShader();
159  };
160 
161  typedef TessEvaluationShader* PtrTessEvaluationShader;
162  typedef const TessEvaluationShader* PtrConstTessEvaluationShader;
163 
164 #endif // GL_ARB_tessellation_shader
165 
166  //--------------------------------------------------------------------------
167 
170  class ACGDLLEXPORT ComputeShader : public Shader {
171 
172  public:
173  ComputeShader();
174  virtual ~ComputeShader();
175 
176 
177  // get hw caps
178  struct Caps
179  {
180  int maxUniformBlocks_;
181  int maxTextureImageUnits_;
182  int maxImageUniforms_;
183  int maxSharedMemorySize_;
184  int maxUniformComponents_;
185  int maxAtomicCounterBufs_;
186  int maxAtomicCounters_;
187  int maxCombinedUniformComponents_;
188  int maxWorkGroupInvocations_;
189  int maxWorkGroupCount_[3];
190  int maxWorkGroupSize_[3];
191  };
192 
193  static const Caps& caps();
194 
195  private:
196 
197  static Caps caps_;
198  static bool capsInitialized_;
199  };
200 
202  typedef const ComputeShader* PtrConstComputeShader;
203 
204  //--------------------------------------------------------------------------
205 
206 
211  class ACGDLLEXPORT Program {
212 
213  public:
214  Program();
215  virtual ~Program();
216 
217 
218 
219  //===========================================================================
223  //===========================================================================
224 
225  void attach(PtrConstShader _shader);
226  void detach(PtrConstShader _shader);
227  void link();
228 
231  //===========================================================================
235  //===========================================================================
236 
237  int getAttributeLocation(const char *_name);
238  int getUniformLocation(const char *_name);
239  int getFragDataLocation(const char* _name);
240 
241  void bindAttributeLocation(unsigned int _index, const char *_name);
242  void bindFragDataLocation(unsigned int _index, const char *_name);
243 
246  //===========================================================================
250  //===========================================================================
251 
252  void setUniform(const char *_name, GLint _value);
253  void setUniform(const char *_name, const ACG::Vec2i &_value);
254  void setUniform(const char *_name, const ACG::Vec3i &_value);
255  void setUniform(const char *_name, const ACG::Vec4i &_value);
256 
257  void setUniform(const char *_name, GLuint _value);
258  void setUniform(const char *_name, const ACG::Vec2ui &_value);
259  void setUniform(const char *_name, const ACG::Vec3ui &_value);
260  void setUniform(const char *_name, const ACG::Vec4ui &_value);
261 
262  void setUniform(const char *_name, GLfloat _value);
263  void setUniform(const char *_name, const ACG::Vec2f &_value);
264  void setUniform(const char *_name, const ACG::Vec3f &_value);
265  void setUniform(const char *_name, const ACG::Vec4f &_value);
266 
267 
268  void setUniform(const char *_name, const ACG::GLMatrixf &_value, bool _transposed = false);
269  void setUniformMat3(const char *_name, const ACG::GLMatrixf &_value, bool _transposed = false);
270 
271 
272 
273  void setUniform(const char *_name, const GLint *_values, int _count);
274  void setUniform(const char *_name, const GLfloat *_values, int _count);
275  void setUniform(const char *_name, const ACG::Vec2f *_values, int _count);
276  void setUniform(const char *_name, const ACG::Vec3f *_values, int _count);
277  void setUniform(const char *_name, const ACG::Vec4f *_values, int _count);
278  void setUniform(const char *_name, int _index, bool _value);
279 
280  void setUniform(const char *_name, int _index, int _value);
281  void setUniform(const char *_name, int _index, float _value);
282 
287  //===========================================================================
291  //===========================================================================
292 
293  GLuint getUniformBlockIndex(const char *_name);
294 
295  void setUniformBlockBinding(GLuint _index, int _binding);
296  void setUniformBlockBinding(const char *_name, int _binding);
297 
298  int getUniformBlockSize(GLuint _index);
299  int getUniformBlockSize(const char *_name);
300 
301  void getUniformBlockOffsets(int _numUniforms, const char **_names, int *_outOffsets);
302 
303 
306  //===========================================================================
310  //===========================================================================
311 
312  void setGeometryInputType(GLint _type);
313  void setGeometryOutputType(GLint _type);
314  void setGeometryVertexCount(GLint _numVerticesOut);
315 
318  //===========================================================================
322  //===========================================================================
323 
324  void use();
325  void disable();
326  bool isActive();
327  bool isLinked();
328 
331  GLuint getProgramId();
332 
333  private:
334 
335  std::list<PtrConstShader> m_linkedShaders;
336  GLint m_programId;
337 
338  GLint m_linkStatus;
339  };
340 
341  typedef Program* PtrProgram;
342  typedef const Program* PtrConstProgram;
343 
344  //--------------------------------------------------------------------------
345 
346  GLSL::StringList ACGDLLEXPORT loadShader(const char *filename, const GLSL::StringList *macros = 0, bool appendNewLineChar = true, GLSL::StringList *outIncludes = 0);
347 
348  GLSL::PtrVertexShader ACGDLLEXPORT loadVertexShader(const char *name, const GLSL::StringList *macros = 0, bool verbose = true);
349  GLSL::PtrFragmentShader ACGDLLEXPORT loadFragmentShader(const char *name, const GLSL::StringList *macros = 0, bool verbose = true);
350  GLSL::PtrGeometryShader ACGDLLEXPORT loadGeometryShader(const char *name, const GLSL::StringList *macros = 0, bool verbose = true);
351  GLSL::PtrShader ACGDLLEXPORT loadTessControlShader(const char *name, const GLSL::StringList *macros = 0, bool verbose = true);
352  GLSL::PtrShader ACGDLLEXPORT loadTessEvaluationShader(const char *name, const GLSL::StringList *macros = 0, bool verbose = true);
353  GLSL::PtrComputeShader ACGDLLEXPORT loadComputeShader(const char *name, const GLSL::StringList *macros = 0, bool verbose = true);
354 
360  GLSL::PtrProgram ACGDLLEXPORT loadProgram(const char *vertexShaderFile,
361  const char *fragmentShaderFile,
362  const GLSL::StringList *macros = 0,
363  bool verbose = true);
364 
370  GLSL::PtrProgram ACGDLLEXPORT loadProgram(const char *vertexShaderFile,
371  const char *geometryShaderFile,
372  const char *fragmentShaderFile,
373  const GLSL::StringList *macros = 0,
374  bool verbose = true);
375 
381  GLSL::PtrProgram ACGDLLEXPORT loadProgram(const char *vertexShaderFile,
382  const char *tessControlShaderFile,
383  const char *tessEvaluationShaderFile,
384  const char *geometryShaderFile,
385  const char *fragmentShaderFile,
386  const GLSL::StringList *macros = 0,
387  bool verbose = true);
388 
394  GLSL::PtrProgram ACGDLLEXPORT loadComputeProgram(const char *computeShaderFile,
395  const GLSL::StringList *macros = 0,
396  bool verbose = true);
397 }
398 
399 #endif // GLSLSHADER_H
GLSL fragment shader.
Definition: GLSLShader.hh:110
GLSL::PtrFragmentShader loadFragmentShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new vertex shader.
Definition: GLSLShader.cc:983
GLSL::PtrProgram loadComputeProgram(const char *computeShaderFile, const GLSL::StringList *macros, bool verbose)
Definition: GLSLShader.cc:1154
GLSL compute shader.
Definition: GLSLShader.hh:170
GLSL::StringList loadShader(const char *filename, const GLSL::StringList *macros, bool appendNewLineChar, GLSL::StringList *outIncludes)
Loads the shader source.
Definition: GLSLShader.cc:921
GLSL vertex shader.
Definition: GLSLShader.hh:95
GLSL::PtrShader loadTessControlShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new tessellation control shader.
Definition: GLSLShader.cc:1018
GLSL::PtrProgram loadProgram(const char *vertexShaderFile, const char *tessControlShaderFile, const char *tessEvaluationShaderFile, const char *geometryShaderFile, const char *fragmentShaderFile, const GLSL::StringList *macros, bool verbose)
Definition: GLSLShader.cc:1076
GLSL program class.
Definition: GLSLShader.hh:211
This namespace contains all the classes and functions for handling GLSL shader and program objects...
Definition: AntiAliasing.hh:66
GLSL::PtrShader loadTessEvaluationShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new tessellation evaluation shader.
Definition: GLSLShader.cc:1037
GLSL geometry shader.
Definition: GLSLShader.hh:124
GLSL::PtrComputeShader loadComputeShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new compute shader.
Definition: GLSLShader.cc:1057
GLSL::PtrVertexShader loadVertexShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new vertex shader.
Definition: GLSLShader.cc:969
GLSL::PtrGeometryShader loadGeometryShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new vertex shader.
Definition: GLSLShader.cc:1000
A generic shader base class.
Definition: GLSLShader.hh:71