xoreos  0.0.5
shader.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
61 #ifndef GRAPHICS_SHADER_SHADER_H
62 #define GRAPHICS_SHADER_SHADER_H
63 
64 #include <vector>
65 #include <map>
66 
67 #include "src/common/ustring.h"
68 #include "src/common/singleton.h"
69 #include "src/common/mutex.h"
70 
71 #include "src/graphics/texture.h"
72 
74 
76 
77 namespace Graphics {
78 
79 namespace Shader {
80 
139 };
140 
144 };
145 
152 };
153 
158 };
159 
160 
161 struct ShaderUBO {
162  GLuint index;
163  GLuint glid;
164  ShaderUBO() : index(0), glid(0) {}
165  ShaderUBO(const ShaderUBO &src) : index(src.index), glid(src.glid) {}
166  ShaderUBO(GLuint idx, GLuint gid) : index(idx), glid(gid) {}
167  inline const ShaderUBO &operator=(const ShaderUBO &src) { index = src.index; glid = src.glid; return *this; }
168 };
169 
170 
178  ShaderSampler() : texture(0), unit(0) {}
180 };
181 
182 
183 class ShaderObject : public GLContainer {
184 public:
187  uint32 count; // Number of variables (normally 1, but higher if an array is defined).
189 
193 
194  inline const ShaderObjectVariable &operator=(const ShaderObjectVariable &src) { type = src.type; count = src.count; name = src.name; return *this; }
195  };
196 
197  ShaderObject();
198 
200  uint32 id; // ID unique to a shader within its type (i.e unique across vertex shaders, or unique across fragment shaders). 0 is an invalid id.
201  GLuint glid; // Defaults to zero if not compiled.
202  ShaderType type; // Type of shader (typically vertex or fragment).
204 
205  std::vector<ShaderObject::ShaderObjectVariable> variablesSelf;
206  std::vector<ShaderObject::ShaderObjectVariable> variablesCombined;
207  std::vector<ShaderObject *> subObjects;
208 
209 protected:
210  void doRebuild();
211  void doDestroy();
212 };
213 
214 class ShaderProgram : public GLContainer {
215 public:
216  ShaderProgram();
217 
218  ShaderObject *vertexObject; // Vertex shader.
219  ShaderObject *fragmentObject; // Fragment shader.
220  std::vector<GLint> vertexVariableLocations;
221  std::vector<GLint> fragmentVariableLocations;
222  uint64 id; // Set to (vertex.id << 32) | fragment.id
223  GLuint glid;
225 
227  glBindAttribLocation(glid, (GLuint)(attrib), name.c_str());
228  }
229 
230  void queue() {
232  }
233 
234 protected:
235  void doRebuild();
236  void doDestroy();
237 };
238 
240 class ShaderManager : public Common::Singleton<ShaderManager> {
241 public:
242  ShaderManager();
243  ~ShaderManager();
244 
246  void init();
248  void deinit();
249 
251  ShaderObject *getShaderObject(const Common::UString &name, const Common::UString &source, ShaderType type);
252 
253  void bindShaderVariable(ShaderObject::ShaderObjectVariable &var, GLint loc, const void *data);
254  void bindShaderInstance(ShaderProgram *program, const void **vertexVariables, const void **fragmentVariables);
255 
256  ShaderProgram *getShaderProgram(ShaderObject *vertexObject, ShaderObject *fragmentObject);
257  ShaderProgram *registerShaderProgram(ShaderObject *vertexObject, ShaderObject *fragmentObject);
258 
259  void genShaderVariableList(ShaderObject *obj, std::vector<ShaderObject::ShaderObjectVariable> &vars);
260 
261  // Takes a string, and returns the appropriate enum representing that type (e.g "vec4" => SHADER_VEC4).
263 
264 private:
266  void registerShaderAttachment(GLuint progid, ShaderObject *obj);
267 
269  void parseShaderVariables(const Common::UString &shaderString, std::vector<ShaderObject::ShaderObjectVariable> &variableList);
270 
271 public:
273  void genGLShader(ShaderObject *object);
274 
276  void genGLProgram(ShaderProgram *program);
277 
278 private:
281  std::map<Common::UString, Shader::ShaderObject *> _shaderObjectMap;
282  std::vector<Shader::ShaderProgram *> _shaderProgramArray;
283 
286 };
287 
288 } // End of namespace Shader
289 
290 } // End of namespace Graphics
291 
293 #define ShaderMan Graphics::Shader::ShaderManager::instance()
294 
295 #endif // GRAPHICS_SHADER_SHADER_H
void parseShaderVariables(const Common::UString &shaderString, std::vector< ShaderObject::ShaderObjectVariable > &variableList)
Parses a given string, representing a GLSL shader, and extracts uniform variable information from it...
Definition: shader.cpp:438
const ShaderObjectVariable & operator=(const ShaderObjectVariable &src)
Definition: shader.h:194
ShaderObjectVariable(ShaderVariableType t, uint32 cnt, const Common::UString &n)
Definition: shader.h:192
Class and macro for implementing singletons.
ShaderProgram * getShaderProgram(ShaderObject *vertexObject, ShaderObject *fragmentObject)
Definition: shader.cpp:335
An object containing shader information.
Definition: types.h:85
A class holding an UTF-8 string.
Definition: ustring.h:48
std::vector< ShaderObject::ShaderObjectVariable > variablesSelf
Definition: shader.h:205
uint64_t uint64
Definition: types.h:206
ShaderProgram * registerShaderProgram(ShaderObject *vertexObject, ShaderObject *fragmentObject)
Definition: shader.cpp:373
Shader runtime builder.
void addToQueue(QueueType queue)
Definition: queueable.cpp:45
A texture.
Definition: texture.h:34
Common::UString shaderString
Definition: shader.h:203
void bindShaderVariable(ShaderObject::ShaderObjectVariable &var, GLint loc, const void *data)
Definition: shader.cpp:228
Common::Mutex _programMutex
Definition: shader.h:285
A handle to an Aurora texture.
ShaderUBO(const ShaderUBO &src)
Definition: shader.h:165
Virtual base class of a texture.
ShaderObject * fragmentObject
Definition: shader.h:219
ShaderObject * getShaderObject(const Common::UString &name, ShaderType type)
Definition: shader.cpp:184
The shader manager.
Definition: shader.h:240
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
ShaderUBO(GLuint idx, GLuint gid)
Definition: shader.h:166
Generic template base class for implementing the singleton design pattern.
Definition: singleton.h:61
A mutex.
Definition: mutex.h:40
Graphics::Aurora::TextureHandle handle
Definition: shader.h:176
ShaderVariableType shaderstringToEnum(const Common::UString &stype)
Definition: shader.cpp:701
ShaderVariableType
Enum of all supported shader variable parsing and automatic binding.
Definition: shader.h:85
ShaderObjectVariable(const ShaderObjectVariable &src)
Definition: shader.h:191
void genGLShader(ShaderObject *object)
Generate GL ids for, and compile a shader object.
Definition: shader.cpp:544
std::vector< GLint > fragmentVariableLocations
Definition: shader.h:221
ShaderObject * vertexObject
Definition: shader.h:218
std::vector< Shader::ShaderProgram * > _shaderProgramArray
Definition: shader.h:282
const ShaderUBO & operator=(const ShaderUBO &src)
Definition: shader.h:167
void deinit()
Deinitialise shader subsystem.
Definition: shader.cpp:165
void registerShaderAttachment(GLuint progid, ShaderObject *obj)
Recursively attaches shader objects to a given program.
Definition: shader.cpp:366
void genShaderVariableList(ShaderObject *obj, std::vector< ShaderObject::ShaderObjectVariable > &vars)
Definition: shader.cpp:408
Unicode string handling.
Thread mutex classes.
std::vector< ShaderObject * > subObjects
Definition: shader.h:207
Shader sampler is used to bind a texture to a texture unit.
Definition: shader.h:174
void genGLProgram(ShaderProgram *program)
Generate GL id for, and link, a shader program.
Definition: shader.cpp:564
void init()
Initialise shader management, including default shader set creation.
Definition: shader.cpp:86
uint32_t uint32
Definition: types.h:204
std::map< Common::UString, Shader::ShaderObject * > _shaderObjectMap
Definition: shader.h:281
ShaderSampler(Texture *t, uint32 u)
Definition: shader.h:179
std::vector< GLint > vertexVariableLocations
Definition: shader.h:220
A container of OpenGL elements.
Definition: glcontainer.h:35
void bindShaderInstance(ShaderProgram *program, const void **vertexVariables, const void **fragmentVariables)
Definition: shader.cpp:324
A handle to a texture.
Definition: texturehandle.h:51
void bindAttribute(ShaderVertexAttrib attrib, const Common::UString &name)
Definition: shader.h:226
Common::Mutex _shaderMutex
Definition: shader.h:284
std::vector< ShaderObject::ShaderObjectVariable > variablesCombined
Definition: shader.h:206