reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofShader.h
Go to the documentation of this file.
1#pragma once
2
3
4/*
5 todo: add support for attachment of multiple shaders
6 if a uniform or attribute isn't available, this will cause an error
7 make sure to catch and report that error.
8 */
9
10#include "ofConstants.h"
11#include "glm/fwd.hpp"
12#include <unordered_map>
13
14class ofTexture;
15class ofMatrix3x3;
17class ofBufferObject;
19
20template<typename T>
21class ofColor_;
23
24enum ofLogLevel: short;
25
27 std::unordered_map<GLuint, of::filesystem::path> shaderFiles;
28 std::unordered_map<GLuint, std::string> shaderSources;
29 std::unordered_map<std::string, int> intDefines;
30 std::unordered_map<std::string, float> floatDefines;
31 of::filesystem::path sourceDirectoryPath;
32 bool bindDefaults = true;
33};
34
35class ofShader {
36
37 struct Source{
38 // FIXME: change source to of::filesystem::path
39 Source(GLuint type, const std::string & source, const of::filesystem::path & directoryPath)
40 :type(type)
41 ,source(source)
42 ,directoryPath(directoryPath){}
43
44 Source(){}
45
46 GLuint type;
47 std::string source;
48 std::string expandedSource;
49 of::filesystem::path directoryPath;
50 std::unordered_map<std::string, int> intDefines;
51 std::unordered_map<std::string, float> floatDefines;
52 };
53
54public:
55 ofShader();
56 ~ofShader();
57 ofShader(const ofShader & shader);
58 ofShader & operator=(const ofShader & shader);
59 ofShader(ofShader && shader);
60 ofShader & operator=(ofShader && shader);
61
62 bool load(const of::filesystem::path& shaderName);
63 bool load(const of::filesystem::path& vertName, const of::filesystem::path& fragName, const of::filesystem::path& geomName="");
64#if !defined(TARGET_OPENGLES) && defined(glDispatchCompute)
65 bool loadCompute(const of::filesystem::path& shaderName);
66#endif
67
68#if !defined(TARGET_OPENGLES) || defined(TARGET_EMSCRIPTEN)
70 std::unordered_map<GLuint, of::filesystem::path> shaderFiles;
71 std::unordered_map<GLuint, std::string> shaderSources;
72 std::vector<std::string> varyingsToCapture;
73 std::unordered_map<std::string, int> intDefines;
74 std::unordered_map<std::string, float> floatDefines;
75 of::filesystem::path sourceDirectoryPath;
76 bool bindDefaults = true;
77 GLuint bufferMode = GL_INTERLEAVED_ATTRIBS; // GL_INTERLEAVED_ATTRIBS or GL_SEPARATE_ATTRIBS
78 };
79
84 TransformFeedbackRangeBinding(const ofBufferObject & buffer, GLuint offset, GLuint size);
85
86 GLuint index = 0;
87 GLuint offset = 0;
88 GLuint size;
89 private:
90 const ofBufferObject & buffer;
91 friend class ofShader;
92 };
93
99
100 GLuint index = 0;
101 private:
102 const ofBufferObject & buffer;
103 friend class ofShader;
104 };
105#endif
106
107 bool setup(const ofShaderSettings & settings);
108#if !defined(TARGET_OPENGLES) || defined(TARGET_EMSCRIPTEN)
109 bool setup(const TransformFeedbackSettings & settings);
110#endif
111
112 // these are essential to call before linking the program with geometry shaders
113 void setGeometryInputType(GLenum type); // type: GL_POINTS, GL_LINES, GL_LINES_ADJACENCY_EXT, GL_TRIANGLES, GL_TRIANGLES_ADJACENCY_EXT
114 void setGeometryOutputType(GLenum type); // type: GL_POINTS, GL_LINE_STRIP or GL_TRIANGLE_STRIP
115 void setGeometryOutputCount(int count); // set number of output vertices
116
117 int getGeometryMaxOutputCount() const; // returns maximum number of supported vertices
118
119
120 void unload();
121
122 bool isLoaded() const;
123
124 void begin() const;
125 void end() const;
126
127#if !defined(TARGET_OPENGLES) || defined(TARGET_EMSCRIPTEN)
128 void beginTransformFeedback(GLenum mode) const;
129 void beginTransformFeedback(GLenum mode, const TransformFeedbackRangeBinding & binding) const;
130 void beginTransformFeedback(GLenum mode, const std::vector<TransformFeedbackRangeBinding> & binding) const;
131 void beginTransformFeedback(GLenum mode, const TransformFeedbackBaseBinding & binding) const;
132 void beginTransformFeedback(GLenum mode, const std::vector<TransformFeedbackBaseBinding> & binding) const;
133 void endTransformFeedback() const;
134 void endTransformFeedback(const TransformFeedbackRangeBinding & binding) const;
135 void endTransformFeedback(const std::vector<TransformFeedbackRangeBinding> & binding) const;
136 void endTransformFeedback(const TransformFeedbackBaseBinding & binding) const;
137 void endTransformFeedback(const std::vector<TransformFeedbackBaseBinding> & binding) const;
138#endif
139
140#if !defined(TARGET_OPENGLES) && defined(glDispatchCompute)
141 void dispatchCompute(GLuint x, GLuint y, GLuint z) const;
142#endif
143
144 // set a texture reference
145 void setUniformTexture(const std::string & name, const ofBaseHasTexture& img, int textureLocation) const;
146 void setUniformTexture(const std::string & name, const ofTexture& img, int textureLocation) const;
147 void setUniformTexture(const std::string & name, int textureTarget, GLint textureID, int textureLocation) const;
148
149 // set a single uniform value
150 void setUniform1i(const std::string & name, int v1) const;
151 void setUniform2i(const std::string & name, int v1, int v2) const;
152 void setUniform3i(const std::string & name, int v1, int v2, int v3) const;
153 void setUniform4i(const std::string & name, int v1, int v2, int v3, int v4) const;
154
155 void setUniform1f(const std::string & name, float v1) const;
156 void setUniform2f(const std::string & name, float v1, float v2) const;
157 void setUniform3f(const std::string & name, float v1, float v2, float v3) const;
158 void setUniform4f(const std::string & name, float v1, float v2, float v3, float v4) const;
159
160 void setUniform2f(const std::string & name, const glm::vec2 & v) const;
161 void setUniform3f(const std::string & name, const glm::vec3 & v) const;
162 void setUniform4f(const std::string & name, const glm::vec4 & v) const;
163 void setUniform4f(const std::string & name, const ofFloatColor & v) const;
164
165 // set an array of uniform values
166 void setUniform1iv(const std::string & name, const int* v, int count = 1) const;
167 void setUniform2iv(const std::string & name, const int* v, int count = 1) const;
168 void setUniform3iv(const std::string & name, const int* v, int count = 1) const;
169 void setUniform4iv(const std::string & name, const int* v, int count = 1) const;
170
171 void setUniform1fv(const std::string & name, const float* v, int count = 1) const;
172 void setUniform2fv(const std::string & name, const float* v, int count = 1) const;
173 void setUniform3fv(const std::string & name, const float* v, int count = 1) const;
174 void setUniform4fv(const std::string & name, const float* v, int count = 1) const;
175
176 void setUniforms(const ofParameterGroup & parameters) const;
177
178 // note: it may be more optimal to use a 4x4 matrix than a 3x3 matrix, if possible
179 void setUniformMatrix3f(const std::string & name, const glm::mat3 & m, int count = 1) const;
180 void setUniformMatrix4f(const std::string & name, const glm::mat4 & m, int count = 1) const;
181
182 GLint getUniformLocation(const std::string & name) const;
183
184 // set attributes that vary per vertex (look up the location before glBegin)
185 GLint getAttributeLocation(const std::string & name) const;
186
187#ifndef TARGET_OPENGLES
188#ifdef GLEW_ARB_uniform_buffer_object
189 GLint getUniformBlockIndex(const std::string & name) const;
190 GLint getUniformBlockBinding(const std::string & name) const;
191 void bindUniformBlock(GLuint bindind, const std::string & name) const;
192 void printActiveUniformBlocks() const;
193#endif
194#endif
195
196#ifndef TARGET_OPENGLES
197 void setAttribute1s(GLint location, short v1) const;
198 void setAttribute2s(GLint location, short v1, short v2) const;
199 void setAttribute3s(GLint location, short v1, short v2, short v3) const;
200 void setAttribute4s(GLint location, short v1, short v2, short v3, short v4) const;
201#endif
202
203 void setAttribute1f(GLint location, float v1) const;
204 void setAttribute2f(GLint location, float v1, float v2) const;
205 void setAttribute3f(GLint location, float v1, float v2, float v3) const;
206 void setAttribute4f(GLint location, float v1, float v2, float v3, float v4) const;
207
208#ifndef TARGET_OPENGLES
209 void setAttribute1d(GLint location, double v1) const;
210 void setAttribute2d(GLint location, double v1, double v2) const;
211 void setAttribute3d(GLint location, double v1, double v2, double v3) const;
212 void setAttribute4d(GLint location, double v1, double v2, double v3, double v4) const;
213#endif
214
215 void setAttribute1fv(const std::string & name, const float* v, GLsizei stride=sizeof(float)) const;
216 void setAttribute2fv(const std::string & name, const float* v, GLsizei stride=sizeof(float)*2) const;
217 void setAttribute3fv(const std::string & name, const float* v, GLsizei stride=sizeof(float)*3) const;
218 void setAttribute4fv(const std::string & name, const float* v, GLsizei stride=sizeof(float)*4) const;
219
220 void bindAttribute(GLuint location, const std::string & name) const;
221
222 void printActiveUniforms() const;
223 void printActiveAttributes() const;
224
225
226 // advanced use
227 bool setShadowUniforms( int textureLocation ) const;
228 bool setPbrEnvironmentMapUniforms( int textureLocation ) const;
229
230 // these methods create and compile a shader from source or file
231 // type: GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, GL_GEOMETRY_SHADER_EXT etc.
232 bool setupShaderFromSource(GLenum type, std::string source, std::string sourceDirectoryPath = "");
233 bool setupShaderFromFile(GLenum type, const of::filesystem::path& filename);
234
235 // links program with all compiled shaders
236 bool linkProgram();
237
238 // binds default uniforms and attributes, only useful for
239 // fixed pipeline simulation under programmable renderer
240 // has to be called before linking
241 bool bindDefaults();
242
243 GLuint getProgram() const;
244 GLuint getShader(GLenum type) const;
245
246 bool operator==(const ofShader & other) const;
247 bool operator!=(const ofShader & other) const;
248
249
250 // these are used only for openGL ES2 or GL3/4 using the programmable GL renderer
252 POSITION_ATTRIBUTE=0, // tig: was =1, and BOY, what a performance hog this was!!! see: http://www.chromium.org/nativeclient/how-tos/3d-tips-and-best-practices
256 INDEX_ATTRIBUTE // usually not used except for compute shades
257 };
258
261 std::string getShaderSource(GLenum type) const;
262
263
264private:
265 GLuint program = 0;
266 bool bLoaded = false;
267
268 struct Shader{
269 GLuint id;
270 Source source;
271 };
272
273 std::unordered_map<GLenum, Shader> shaders;
274 std::unordered_map<std::string, GLint> uniformsCache;
275 mutable std::unordered_map<std::string, GLint> attributesBindingsCache;
276
277#ifndef TARGET_OPENGLES
278 std::unordered_map<std::string, GLint> uniformBlocksCache;
279#endif
280
281 bool setupShaderFromSource(Source && source);
282 ofShader::Source sourceFromFile(GLenum type, const of::filesystem::path& filename);
283 void checkProgramInfoLog();
284 bool checkProgramLinkStatus();
285 void checkShaderInfoLog(GLuint shader, GLenum type, ofLogLevel logLevel);
286 template<typename T>
287 void setDefineConstantTemp(const std::string & name, T value);
288 template<typename T>
289 void setConstantTemp(const std::string & name, const std::string & type, T value);
290
291 static std::string nameForType(GLenum type);
292
299 static std::string parseForIncludes( const std::string& source, const of::filesystem::path& sourceDirectoryPath = "");
300 static std::string parseForIncludes( const std::string& source, std::vector<std::string>& included, int level = 0, const of::filesystem::path& sourceDirectoryPath = "");
301
302 void checkAndCreateProgram();
303#ifdef TARGET_ANDROID
304 void unloadGL();
305 void reloadGL();
306#endif
307};
308
An abstract class representing an object that can have an ofTexture.
Definition ofGLBaseTypes.h:11
Definition ofBufferObject.h:8
ofColor represents a color in openFrameworks.
Definition ofColor.h:20
A 3x3 Matrix.
Definition ofMatrix3x3.h:20
Definition ofParameter.h:84
Definition ofShader.h:35
void setUniform3fv(const std::string &name, const float *v, int count=1) const
Definition ofShader.cpp:1126
bool setup(const ofShaderSettings &settings)
Definition ofShader.cpp:227
void setUniform3f(const std::string &name, float v1, float v2, float v3) const
bool operator==(const ofShader &other) const
Definition ofShader.cpp:1543
bool setupShaderFromFile(GLenum type, const of::filesystem::path &filename)
Definition ofShader.cpp:298
void setUniform1iv(const std::string &name, const int *v, int count=1) const
Definition ofShader.cpp:1078
void setGeometryOutputCount(int count)
Definition ofShader.cpp:558
bool linkProgram()
Definition ofShader.cpp:679
GLint getUniformLocation(const std::string &name) const
Definition ofShader.cpp:1309
void unload()
Definition ofShader.cpp:819
void setAttribute1s(GLint location, short v1) const
Definition ofShader.cpp:1184
void bindAttribute(GLuint location, const std::string &name) const
Definition ofShader.cpp:798
void endTransformFeedback() const
Definition ofShader.cpp:902
void setGeometryInputType(GLenum type)
Definition ofShader.cpp:542
void setUniformTexture(const std::string &name, int textureTarget, GLint textureID, int textureLocation) const
void setUniform4i(const std::string &name, int v1, int v2, int v3, int v4) const
Definition ofShader.cpp:1017
void setUniform3iv(const std::string &name, const int *v, int count=1) const
Definition ofShader.cpp:1094
bool load(const of::filesystem::path &shaderName)
Definition ofShader.cpp:202
void setUniform1i(const std::string &name, int v1) const
Definition ofShader.cpp:993
void setAttribute3s(GLint location, short v1, short v2, short v3) const
Definition ofShader.cpp:1196
bool setShadowUniforms(int textureLocation) const
Definition ofShader.cpp:1463
void printActiveUniforms() const
Definition ofShader.cpp:1406
void setUniform4f(const std::string &name, const glm::vec4 &v) const
bool setPbrEnvironmentMapUniforms(int textureLocation) const
Definition ofShader.cpp:1507
void setAttribute1d(GLint location, double v1) const
Definition ofShader.cpp:1279
bool setupShaderFromSource(GLenum type, std::string source, std::string sourceDirectoryPath="")
bool isLoaded() const
Definition ofShader.cpp:851
void setAttribute3fv(const std::string &name, const float *v, GLsizei stride=sizeof(float) *3) const
Definition ofShader.cpp:1256
GLint getAttributeLocation(const std::string &name) const
Definition ofShader.cpp:1304
std::string getShaderSource(GLenum type) const
returns the shader source as it was passed to the GLSL compiler
Definition ofShader.cpp:531
void setUniform2f(const std::string &name, const glm::vec2 &v) const
ofShader()
Definition ofShader.cpp:106
void setUniformMatrix3f(const std::string &name, const glm::mat3 &m, int count=1) const
Definition ofShader.cpp:1167
void setAttribute2d(GLint location, double v1, double v2) const
Definition ofShader.cpp:1285
void setAttribute4d(GLint location, double v1, double v2, double v3, double v4) const
Definition ofShader.cpp:1297
void setUniform2fv(const std::string &name, const float *v, int count=1) const
Definition ofShader.cpp:1118
void setUniform2iv(const std::string &name, const int *v, int count=1) const
Definition ofShader.cpp:1086
defaultAttributes
Definition ofShader.h:251
@ NORMAL_ATTRIBUTE
Definition ofShader.h:254
@ POSITION_ATTRIBUTE
Definition ofShader.h:252
@ TEXCOORD_ATTRIBUTE
Definition ofShader.h:255
@ INDEX_ATTRIBUTE
Definition ofShader.h:256
@ COLOR_ATTRIBUTE
Definition ofShader.h:253
void setAttribute3d(GLint location, double v1, double v2, double v3) const
Definition ofShader.cpp:1291
int getGeometryMaxOutputCount() const
Definition ofShader.cpp:566
GLuint getShader(GLenum type) const
Definition ofShader.cpp:1533
void setAttribute1f(GLint location, float v1) const
Definition ofShader.cpp:1209
void setUniformMatrix4f(const std::string &name, const glm::mat4 &m, int count=1) const
Definition ofShader.cpp:1175
void printActiveAttributes() const
Definition ofShader.cpp:1436
ofShader & operator=(const ofShader &shader)
Definition ofShader.cpp:141
void setUniform1f(const std::string &name, float v1) const
Definition ofShader.cpp:1025
void setAttribute4s(GLint location, short v1, short v2, short v3, short v4) const
Definition ofShader.cpp:1202
GLuint getProgram() const
Definition ofShader.cpp:1528
void setUniforms(const ofParameterGroup &parameters) const
Definition ofShader.cpp:1142
void setAttribute1fv(const std::string &name, const float *v, GLsizei stride=sizeof(float)) const
Definition ofShader.cpp:1233
void setUniform4fv(const std::string &name, const float *v, int count=1) const
Definition ofShader.cpp:1134
void begin() const
Definition ofShader.cpp:856
void setUniformTexture(const std::string &name, const ofTexture &img, int textureLocation) const
void setAttribute4f(GLint location, float v1, float v2, float v3, float v4) const
Definition ofShader.cpp:1227
void setUniform4f(const std::string &name, const ofFloatColor &v) const
void setUniform2i(const std::string &name, int v1, int v2) const
Definition ofShader.cpp:1001
void setAttribute4fv(const std::string &name, const float *v, GLsizei stride=sizeof(float) *4) const
Definition ofShader.cpp:1267
bool operator!=(const ofShader &other) const
Definition ofShader.cpp:1548
void setAttribute2f(GLint location, float v1, float v2) const
Definition ofShader.cpp:1215
void setGeometryOutputType(GLenum type)
Definition ofShader.cpp:550
void setUniform2f(const std::string &name, float v1, float v2) const
void setUniform4iv(const std::string &name, const int *v, int count=1) const
Definition ofShader.cpp:1102
void setUniform3f(const std::string &name, const glm::vec3 &v) const
void beginTransformFeedback(GLenum mode) const
Definition ofShader.cpp:867
void setAttribute3f(GLint location, float v1, float v2, float v3) const
Definition ofShader.cpp:1221
~ofShader()
Definition ofShader.cpp:113
bool bindDefaults()
Definition ofShader.cpp:804
void setAttribute2s(GLint location, short v1, short v2) const
Definition ofShader.cpp:1190
void setUniform3i(const std::string &name, int v1, int v2, int v3) const
Definition ofShader.cpp:1009
void setUniform1fv(const std::string &name, const float *v, int count=1) const
Definition ofShader.cpp:1110
void setUniform4f(const std::string &name, float v1, float v2, float v3, float v4) const
void setUniformTexture(const std::string &name, const ofBaseHasTexture &img, int textureLocation) const
void end() const
Definition ofShader.cpp:861
void setAttribute2fv(const std::string &name, const float *v, GLsizei stride=sizeof(float) *2) const
Definition ofShader.cpp:1244
A wrapper class for an OpenGL texture.
Definition ofTexture.h:253
ofLogLevel
The supported logging levels. Default is OF_LOG_NOTICE.
Definition ofLog.h:116
ofColor_< float > ofFloatColor
Definition ofShader.h:22
GLuint index
Definition ofShader.h:100
GLuint size
Definition ofShader.h:88
GLuint index
Definition ofShader.h:86
GLuint offset
Definition ofShader.h:87
Definition ofShader.h:69
bool bindDefaults
Definition ofShader.h:76
std::unordered_map< std::string, float > floatDefines
Definition ofShader.h:74
std::vector< std::string > varyingsToCapture
Definition ofShader.h:72
GLuint bufferMode
Definition ofShader.h:77
std::unordered_map< GLuint, of::filesystem::path > shaderFiles
Definition ofShader.h:70
of::filesystem::path sourceDirectoryPath
Definition ofShader.h:75
std::unordered_map< std::string, int > intDefines
Definition ofShader.h:73
std::unordered_map< GLuint, std::string > shaderSources
Definition ofShader.h:71
Definition ofShader.h:26
std::unordered_map< std::string, float > floatDefines
Definition ofShader.h:30
of::filesystem::path sourceDirectoryPath
Definition ofShader.h:31
std::unordered_map< std::string, int > intDefines
Definition ofShader.h:29
bool bindDefaults
Definition ofShader.h:32
std::unordered_map< GLuint, of::filesystem::path > shaderFiles
Definition ofShader.h:27
std::unordered_map< GLuint, std::string > shaderSources
Definition ofShader.h:28