| globjects 1.0.0.000000000000 Strict OpenGL objects wrapper.

Program.h
Go to the documentation of this file.
1
2 #pragma once
3
4 #include <set>
5 #include <unordered_map>
6 #include <vector>
7
8 #include <glm/fwd.hpp>
9
10 #include <globjects/base/ChangeListener.h>
11 #include <globjects/base/Changeable.h>
12 #include <globjects/base/ref_ptr.h>
13
14 #include <globjects/globjects_api.h>
15
16 #include <globjects/Object.h>
17 #include <globjects/LocationIdentity.h>
18 #include <globjects/UniformBlock.h>
19
20
21 namespace globjects
22 {
23
24
25 class AbstractUniform;
26 class ProgramBinary;
27 class Shader;
28
29 template <typename T>
30 class Uniform;
31
78 class GLOBJECTS_API Program : public Object, protected ChangeListener, public Changeable
79 {
80 friend class UniformBlock;
81 friend class ProgramBinaryImplementation_GetProgramBinaryARB;
82 friend class ProgramBinaryImplementation_None;
83
84 public:
85 enum class BinaryImplementation
86 {
87 GetProgramBinaryARB
88 , None
89 };
90
91 static void hintBinaryImplementation(BinaryImplementation impl);
92
93 public:
94 Program();
95 Program(ProgramBinary * binary);
96
97 virtual void accept(ObjectVisitor & visitor) override;
98
99 void use() const;
100 static void release();
101
102 bool isUsed() const;
103 bool isLinked() const;
104
105 void attach(Shader * shader);
106 template <class ...Shaders>
107 void attach(Shader * shader, Shaders... shaders);
108
109 void detach(Shader * shader);
110
111 std::set<Shader*> shaders() const;
112
113 void link() const;
114 void invalidate() const;
115
116 void setBinary(ProgramBinary * binary);
117 ProgramBinary * getBinary() const;
118
119 std::string infoLog() const;
120 gl::GLint get(gl::GLenum pname) const;
121
122 bool isValid() const;
123 void validate();
124
125 void setParameter(gl::GLenum pname, gl::GLint value) const;
126 void setParameter(gl::GLenum pname, gl::GLboolean value) const;
127
128 void getActiveAttrib(gl::GLuint index, gl::GLsizei bufSize, gl::GLsizei * length, gl::GLint * size, gl::GLenum * type, gl::GLchar * name) const;
129
130 gl::GLint getAttributeLocation(const std::string & name) const;
131 gl::GLint getUniformLocation(const std::string & name) const;
132
133 std::vector<gl::GLint> getAttributeLocations(const std::vector<std::string> & names) const;
134 std::vector<gl::GLint> getUniformLocations(const std::vector<std::string> & names) const;
135
136 void bindAttributeLocation(gl::GLuint index, const std::string & name) const;
137 void bindFragDataLocation(gl::GLuint index, const std::string & name) const;
138
139 gl::GLint getFragDataLocation(const std::string & name) const;
140 gl::GLint getFragDataIndex(const std::string & name) const;
141
142 void getInterface(gl::GLenum programInterface, gl::GLenum pname, gl::GLint * params) const;
143 gl::GLuint getResourceIndex(gl::GLenum programInterface, const std::string & name) const;
144 void getResourceName(gl::GLenum programInterface, gl::GLuint index, gl::GLsizei bufSize, gl::GLsizei * length, char * name) const;
145 void getResource(gl::GLenum programInterface, gl::GLuint index, gl::GLsizei propCount, const gl::GLenum * props, gl::GLsizei bufSize, gl::GLsizei * length, gl::GLint * params) const;
146 gl::GLint getResourceLocation(gl::GLenum programInterface, const std::string & name) const;
147 gl::GLint getResourceLocationIndex(gl::GLenum programInterface, const std::string & name) const;
148
151 gl::GLint getInterface(gl::GLenum programInterface, gl::GLenum pname) const;
152
153
156 gl::GLint getResource(gl::GLenum programInterface, gl::GLuint index, gl::GLenum prop, gl::GLsizei * length = nullptr) const;
157 std::vector<gl::GLint> getResource(gl::GLenum programInterface, gl::GLuint index, const std::vector<gl::GLenum> & props, gl::GLsizei * length = nullptr) const;
158 void getResource(gl::GLenum programInterface, gl::GLuint index, const std::vector<gl::GLenum> & props, gl::GLsizei bufSize, gl::GLsizei * length, gl::GLint * params) const;
159
160 gl::GLuint getUniformBlockIndex(const std::string& name) const;
161 UniformBlock * uniformBlock(gl::GLuint uniformBlockIndex);
162 const UniformBlock * uniformBlock(gl::GLuint uniformBlockIndex) const;
163 UniformBlock * uniformBlock(const std::string& name);
164 const UniformBlock * uniformBlock(const std::string& name) const;
165 void getActiveUniforms(gl::GLsizei uniformCount, const gl::GLuint * uniformIndices, gl::GLenum pname, gl::GLint * params) const;
166 std::vector<gl::GLint> getActiveUniforms(const std::vector<gl::GLuint> & uniformIndices, gl::GLenum pname) const;
167 std::vector<gl::GLint> getActiveUniforms(const std::vector<gl::GLint> & uniformIndices, gl::GLenum pname) const;
168 gl::GLint getActiveUniform(gl::GLuint uniformIndex, gl::GLenum pname) const;
169 std::string getActiveUniformName(gl::GLuint uniformIndex) const;
170
171 template<typename T>
172 void setUniform(const std::string & name, const T & value);
173 template<typename T>
174 void setUniform(gl::GLint location, const T & value);
175
178 template<typename T>
179 Uniform<T> * getUniform(const std::string & name);
180 template<typename T>
181 const Uniform<T> * getUniform(const std::string & name) const;
182 template<typename T>
183 Uniform<T> * getUniform(gl::GLint location);
184 template<typename T>
185 const Uniform<T> * getUniform(gl::GLint location) const;
186
192 void addUniform(AbstractUniform * uniform);
193
194 void setShaderStorageBlockBinding(gl::GLuint storageBlockIndex, gl::GLuint storageBlockBinding) const;
195
196 void dispatchCompute(gl::GLuint numGroupsX, gl::GLuint numGroupsY, gl::GLuint numGroupsZ);
197 void dispatchCompute(const glm::uvec3 & numGroups);
198 void dispatchComputeGroupSize(gl::GLuint numGroupsX, gl::GLuint numGroupsY, gl::GLuint numGroupsZ, gl::GLuint groupSizeX, gl::GLuint groupSizeY, gl::GLuint groupSizeZ);
199 void dispatchComputeGroupSize(const glm::uvec3 & numGroups, const glm::uvec3 & groupSizes);
200
201 virtual gl::GLenum objectType() const override;
202
203 protected:
204 virtual ~Program();
205
206 bool checkLinkStatus() const;
207 void checkDirty() const;
208
209 bool compileAttachedShaders() const;
210 void updateUniforms() const;
211 void updateUniformBlockBindings() const;
212
213 // ChangeListener Interface
214
215 virtual void notifyChanged(const Changeable * sender) override;
216
217 protected:
218 static gl::GLuint createProgram();
219
220 template<typename T>
221 void setUniformByIdentity(const LocationIdentity & identity, const T & value);
222 template<typename T>
223 Uniform<T> * getUniformByIdentity(const LocationIdentity & identity);
224 template<typename T>
225 const Uniform<T> * getUniformByIdentity(const LocationIdentity & identity) const;
226
227 UniformBlock * getUniformBlockByIdentity(const LocationIdentity & identity);
228 const UniformBlock * getUniformBlockByIdentity(const LocationIdentity & identity) const;
229
230 protected:
231 std::set<ref_ptr<Shader>> m_shaders;
232 ref_ptr<ProgramBinary> m_binary;
233
234 std::unordered_map<LocationIdentity, ref_ptr<AbstractUniform>> m_uniforms;
235 std::unordered_map<LocationIdentity, UniformBlock> m_uniformBlocks;
236
237 mutable bool m_linked;
238 mutable bool m_dirty;
239 };
240
241
242 } // namespace globjects
243
244
245 #include <globjects/Program.inl>
globjects::Uniform
Wraps access to typed global GLSL variables.
Definition: AbstractUniform.h:25
ChangeListener.h
globjects::ref_ptr
The ref_ptr class provides the interface for a reference pointer.
Definition: LogMessageBuilder.h:20
globjects
Contains all the classes that wrap OpenGL functionality.
globjects::Object
Superclass of all wrapped OpenGL objects.
Definition: Object.h:26
globjects::UniformBlock
Definition: UniformBlock.h:16
globjects::Program
Wraps an OpenGL program.
Definition: Program.h:78
ref_ptr.h
globjects::Program::m_linked
bool m_linked
Definition: Program.h:237
globjects::Program::BinaryImplementation
BinaryImplementation
Definition: Program.h:85
Changeable.h
globjects::Program::m_shaders
std::set< ref_ptr< Shader > > m_shaders
Definition: Program.h:231
globjects::Changeable
Superclass of all objects that want others to signal that they have changed.
Definition: Changeable.h:22
globjects::ObjectVisitor
Implements a Visitor Pattern to iterate over all tracked globjects objects.
Definition: ObjectVisitor.h:29
Object.h
globjects::AbstractUniform
Abstract base class for templated Uniforms.
Definition: AbstractUniform.h:34
globjects::Program::m_uniformBlocks
std::unordered_map< LocationIdentity, UniformBlock > m_uniformBlocks
Definition: Program.h:235
globjects::ProgramBinary
The ProgramBinary class is used for directly setting binary sources for a Program.
Definition: ProgramBinary.h:27
globjects::Program::m_dirty
bool m_dirty
Definition: Program.h:238
globjects::ChangeListener
Allows listening to any Changeable.
Definition: ChangeListener.h:22
globjects::Program::m_uniforms
std::unordered_map< LocationIdentity, ref_ptr< AbstractUniform > > m_uniforms
Definition: Program.h:234
globjects::Shader
Encapsulates OpenGL shaders.
Definition: Shader.h:40
globjects::LocationIdentity
Definition: LocationIdentity.h:16
UniformBlock.h
globjects::Program::m_binary
ref_ptr< ProgramBinary > m_binary
Definition: Program.h:232
LocationIdentity.h