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  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 #ifndef GLSLSHADER_H
51 #define GLSLSHADER_H
52 
53 //==============================================================================
54 
55 #include <ACG/Config/ACGDefines.hh>
56 #include <ACG/Math/VectorT.hh>
57 #include <ACG/Math/GLMatrixT.hh>
58 #include <ACG/GL/gl.hh>
59 
60 #include <list>
61 #include <string>
62 #include <QStringList>
63 
64 //==============================================================================
65 
69 namespace GLSL {
70 
71 #define GLSL_MAX_LOGSIZE 16384
72 
73  typedef std::list<std::string> StringList;
74 
77  class ACGDLLEXPORT Shader {
78 
79  public:
80  Shader(GLenum shaderType);
81  virtual ~Shader();
82  void setSource(const StringList& source);
83  void setSource(const QStringList& source);
84 
85  // FIXME implement StringList getSource();
86  bool compile(bool verbose = true);
87 
88  protected:
89  GLuint m_shaderId;
90 
91  friend class Program;
92  };
93 
94  typedef Shader* PtrShader;
95  typedef const Shader* PtrConstShader;
96 
97  //--------------------------------------------------------------------------
98 
101  class ACGDLLEXPORT VertexShader : public Shader {
102 
103  public:
104  VertexShader();
105  virtual ~VertexShader();
106 
107  };
108 
109  typedef VertexShader* PtrVertexShader;
110  typedef const VertexShader* PtrVertexConstShader;
111 
112  //--------------------------------------------------------------------------
113 
116  class ACGDLLEXPORT FragmentShader : public Shader {
117 
118  public:
119  FragmentShader();
120  virtual ~FragmentShader();
121  };
122 
124  typedef const FragmentShader* PtrConstFragmentShader;
125 
126  //--------------------------------------------------------------------------
127 
130  class ACGDLLEXPORT GeometryShader : public Shader {
131 
132  public:
133  GeometryShader();
134  virtual ~GeometryShader();
135  };
136 
138  typedef const GeometryShader* PtrConstGeometryShader;
139 
140  //--------------------------------------------------------------------------
141 
142 #ifdef GL_ARB_tessellation_shader
143 
146  class ACGDLLEXPORT TessControlShader : public Shader {
147 
148  public:
149  TessControlShader();
150  virtual ~TessControlShader();
151  };
152 
153  typedef TessControlShader* PtrTessControlShader;
154  typedef const TessControlShader* PtrConstTessControlShader;
155 
156  //--------------------------------------------------------------------------
157 
160  class ACGDLLEXPORT TessEvaluationShader : public Shader {
161 
162  public:
163  TessEvaluationShader();
164  virtual ~TessEvaluationShader();
165  };
166 
167  typedef TessEvaluationShader* PtrTessEvaluationShader;
168  typedef const TessEvaluationShader* PtrConstTessEvaluationShader;
169 
170 #endif // GL_ARB_tessellation_shader
171 
172  //--------------------------------------------------------------------------
173 
176  class ACGDLLEXPORT ComputeShader : public Shader {
177 
178  public:
179  ComputeShader();
180  virtual ~ComputeShader();
181 
182 
183  // get hw caps
184  struct Caps
185  {
186  int maxUniformBlocks_;
187  int maxTextureImageUnits_;
188  int maxImageUniforms_;
189  int maxSharedMemorySize_;
190  int maxUniformComponents_;
191  int maxAtomicCounterBufs_;
192  int maxAtomicCounters_;
193  int maxCombinedUniformComponents_;
194  int maxWorkGroupInvocations_;
195  int maxWorkGroupCount_[3];
196  int maxWorkGroupSize_[3];
197  };
198 
199  static const Caps& caps();
200 
201  private:
202 
203  static Caps caps_;
204  static bool capsInitialized_;
205  };
206 
208  typedef const ComputeShader* PtrConstComputeShader;
209 
210  //--------------------------------------------------------------------------
211 
212 
217  class ACGDLLEXPORT Program {
218 
219  public:
220  Program();
221  virtual ~Program();
222 
223 
224 
225  //===========================================================================
229  //===========================================================================
230 
231  void attach(PtrConstShader _shader);
232  void detach(PtrConstShader _shader);
233  void link();
234 
237  //===========================================================================
241  //===========================================================================
242 
243  int getAttributeLocation(const char *_name);
244  int getUniformLocation(const char *_name);
245  int getFragDataLocation(const char* _name);
246 
247  void bindAttributeLocation(unsigned int _index, const char *_name);
248  void bindFragDataLocation(unsigned int _index, const char *_name);
249 
252  //===========================================================================
256  //===========================================================================
257 
258  void setUniform(const char *_name, GLint _value);
259  void setUniform(const char *_name, const ACG::Vec2i &_value);
260  void setUniform(const char *_name, const ACG::Vec3i &_value);
261  void setUniform(const char *_name, const ACG::Vec4i &_value);
262 
263  void setUniform(const char *_name, GLuint _value);
264  void setUniform(const char *_name, const ACG::Vec2ui &_value);
265  void setUniform(const char *_name, const ACG::Vec3ui &_value);
266  void setUniform(const char *_name, const ACG::Vec4ui &_value);
267 
268  void setUniform(const char *_name, GLfloat _value);
269  void setUniform(const char *_name, const ACG::Vec2f &_value);
270  void setUniform(const char *_name, const ACG::Vec3f &_value);
271  void setUniform(const char *_name, const ACG::Vec4f &_value);
272 
273 
274  void setUniform(const char *_name, const ACG::GLMatrixf &_value, bool _transposed = false);
275  void setUniformMat3(const char *_name, const ACG::GLMatrixf &_value, bool _transposed = false);
276 
277 
278 
279  void setUniform(const char *_name, const GLint *_values, int _count);
280  void setUniform(const char *_name, const GLfloat *_values, int _count);
281  void setUniform(const char *_name, const ACG::Vec2f *_values, int _count);
282  void setUniform(const char *_name, const ACG::Vec3f *_values, int _count);
283  void setUniform(const char *_name, const ACG::Vec4f *_values, int _count);
284  void setUniform(const char *_name, int _index, bool _value);
285 
286  void setUniform(const char *_name, int _index, int _value);
287  void setUniform(const char *_name, int _index, float _value);
288 
293  //===========================================================================
297  //===========================================================================
298 
299  GLuint getUniformBlockIndex(const char *_name);
300 
301  void setUniformBlockBinding(GLuint _index, int _binding);
302  void setUniformBlockBinding(const char *_name, int _binding);
303 
304  int getUniformBlockSize(GLuint _index);
305  int getUniformBlockSize(const char *_name);
306 
307  void getUniformBlockOffsets(int _numUniforms, const char **_names, int *_outOffsets);
308 
309 
312  //===========================================================================
316  //===========================================================================
317 
318  void setGeometryInputType(GLint _type);
319  void setGeometryOutputType(GLint _type);
320  void setGeometryVertexCount(GLint _numVerticesOut);
321 
324  //===========================================================================
328  //===========================================================================
329 
330  void use();
331  void disable();
332  bool isActive();
333  bool isLinked();
334 
337  GLuint getProgramId();
338 
339  private:
340 
341  std::list<PtrConstShader> m_linkedShaders;
342  GLint m_programId;
343 
344  GLint m_linkStatus;
345  };
346 
347  typedef Program* PtrProgram;
348  typedef const Program* PtrConstProgram;
349 
350  //--------------------------------------------------------------------------
351 
352  GLSL::StringList ACGDLLEXPORT loadShader(const char *filename, const GLSL::StringList *macros = 0, bool appendNewLineChar = true, GLSL::StringList *outIncludes = 0);
353 
354  GLSL::PtrVertexShader ACGDLLEXPORT loadVertexShader(const char *name, const GLSL::StringList *macros = 0, bool verbose = true);
355  GLSL::PtrFragmentShader ACGDLLEXPORT loadFragmentShader(const char *name, const GLSL::StringList *macros = 0, bool verbose = true);
356  GLSL::PtrGeometryShader ACGDLLEXPORT loadGeometryShader(const char *name, const GLSL::StringList *macros = 0, bool verbose = true);
357  GLSL::PtrShader ACGDLLEXPORT loadTessControlShader(const char *name, const GLSL::StringList *macros = 0, bool verbose = true);
358  GLSL::PtrShader ACGDLLEXPORT loadTessEvaluationShader(const char *name, const GLSL::StringList *macros = 0, bool verbose = true);
359  GLSL::PtrComputeShader ACGDLLEXPORT loadComputeShader(const char *name, const GLSL::StringList *macros = 0, bool verbose = true);
360 
366  GLSL::PtrProgram ACGDLLEXPORT loadProgram(const char *vertexShaderFile,
367  const char *fragmentShaderFile,
368  const GLSL::StringList *macros = 0,
369  bool verbose = true);
370 
376  GLSL::PtrProgram ACGDLLEXPORT loadProgram(const char *vertexShaderFile,
377  const char *geometryShaderFile,
378  const char *fragmentShaderFile,
379  const GLSL::StringList *macros = 0,
380  bool verbose = true);
381 
387  GLSL::PtrProgram ACGDLLEXPORT loadProgram(const char *vertexShaderFile,
388  const char *tessControlShaderFile,
389  const char *tessEvaluationShaderFile,
390  const char *geometryShaderFile,
391  const char *fragmentShaderFile,
392  const GLSL::StringList *macros = 0,
393  bool verbose = true);
394 
400  GLSL::PtrProgram ACGDLLEXPORT loadComputeProgram(const char *computeShaderFile,
401  const GLSL::StringList *macros = 0,
402  bool verbose = true);
403 }
404 
405 #endif // GLSLSHADER_H
GLSL::PtrFragmentShader loadFragmentShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new vertex shader.
Definition: GLSLShader.cc:989
GLSL compute shader.
Definition: GLSLShader.hh:176
A generic shader base class.
Definition: GLSLShader.hh:77
GLSL::PtrProgram loadComputeProgram(const char *computeShaderFile, const GLSL::StringList *macros, bool verbose)
Definition: GLSLShader.cc:1160
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:1082
GLSL vertex shader.
Definition: GLSLShader.hh:101
GLSL::StringList loadShader(const char *filename, const GLSL::StringList *macros, bool appendNewLineChar, GLSL::StringList *outIncludes)
Loads the shader source.
Definition: GLSLShader.cc:927
GLSL::PtrShader loadTessEvaluationShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new tessellation evaluation shader.
Definition: GLSLShader.cc:1043
GLSL geometry shader.
Definition: GLSLShader.hh:130
GLSL::PtrGeometryShader loadGeometryShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new vertex shader.
Definition: GLSLShader.cc:1006
GLSL::PtrShader loadTessControlShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new tessellation control shader.
Definition: GLSLShader.cc:1024
GLSL::PtrComputeShader loadComputeShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new compute shader.
Definition: GLSLShader.cc:1063
GLSL::PtrVertexShader loadVertexShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new vertex shader.
Definition: GLSLShader.cc:975
This namespace contains all the classes and functions for handling GLSL shader and program objects...
Definition: AntiAliasing.hh:75
GLSL fragment shader.
Definition: GLSLShader.hh:116
GLSL program class.
Definition: GLSLShader.hh:217