Labyrinth
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
shader.hpp
1 //////////////////////////////
2 //
3 // File: lxs/coregraphics/shader.hpp
4 // Author: Maciej Kaminski
5 // E-mail: maciej.a.kaminski@gmail.com
6 // Website: maciejkaminski.pl
7 //
8 // File creation date: Nov 27 2012
9 // This file is part of Project Labyrinth
10 // This file is going to be released under terms of GNU GPL in near future
11 //
12 //////////////////////////////
13 
14 #ifndef __LXS_SHADER_
15 #define __LXS_SHADER_
16 
17 #include "helper/tr1.h"
18 #include "helper/opengl.h"
19 #include<boost/shared_ptr.hpp>
20 #include<vector>
21 #include<utility>
22 #include<string>
23 #include<map>
24 //#include "lxs/coregraphics/textureset.hpp"
25 #include "lxs/system/servicethread.hpp"
26 namespace LxS
27 {
28  namespace CoreGraphics
29  {
30 
31  class TextureSet;
32  //! class represents pair of vertex/fragment programs (coompiled from GLSL)
33  class Shader
34  {
35  typedef std::map<std::string,GLint > AttribMap;
36 
37  //! opengl handle for shader program (pair of vertex program and fragment ptogram)
38  class ProgramInfo
39  {
40  public:
41  //! opengl handle
42  GLuint programObject_Handle;
43  //! opengl handle of location of matrix - for uploading of matrices during
44  //! each rendering pass. PROJECTION MTX
45  GLint projmatrixLocation;
46  //! model transformation matrix
47  GLint modelmatrixLocation;
48  //! location of various uniform (parameters) of shader
49  std::map<std::string, GLint>uniformLocations;
50  //! location of proper textures
51  std::vector<GLint> samplerLocations;
52  //! shader attributes
53  AttribMap o_AttribMap;
54  //! for reference counting
55  int use_count;
56  };
57  typedef std::map<std::string,ProgramInfo> ProgramManager;
58  static ProgramManager GlobalProgramManager;
59  static System::MyMutex GlobalProgramManagerMutex;
60  ProgramManager::iterator i_ProgramInfo;
61  boost::shared_ptr<TextureSet> p_TextureSet;
62  static GLuint LoadShader ( GLenum type, const char *shaderSrc );
63  static GLuint LoadProgram ( const char *vertShaderSrc, const char *fragShaderSrc );
64  //! if shader object in RAM is loaded and set up
65  bool is_initialized;
66  //! is opengl representation initialized and uploaded onto GPU
67  bool gl_is_init;
68  //! magnification filter
69  GLint mag_filter;
70  //! minificaton filter;
71  GLint min_filter;
72 
73  public:
74  //!vector of attributes
75  typedef std::vector<std::string> AttribVector;
76  //!vector of assigned textures
77  typedef std::vector<std::pair<std::string,std::string> > TexInfoVector;
78 
79  //! set filter -
80  //! applies to all textures used by this particular shading object
81  void SetTexMagFilter(GLint mf) {mag_filter=mf;}
82  //! set minification
83  //! filter (OpenGL) - applies to all textures used by this particular shading object
84  void SetTexMinFilter(GLint mf) { min_filter=mf;}
85  //! used to implement android run states and moving between OpenGL contexts
86  static void ReloadAllShaders();
87 
88  //! after losing context, graphics glitches etc
89  static void MarkNecessityToReload();
90  //! prints infodump on all shaders to a console
91  static void PrintGlobalStatus();
92  Shader();
93  //! get amount of texels within all textures linked to this shader, together
94  unsigned long GetTexelsCount();
95  private:
96 
97  //! configuration parameters passed during construction/configuration
98  //! to be used during initialization.
99  struct InitParams
100  {
101  //! path to program file, excluding extension (pair of .fsr and .vsr files
102  //! assumed to exist at path given);
103  std::string path;
104  //! textures referenced by particular shader program
105  TexInfoVector textures;
106  AttribVector relevantAttributes;
107  //! uniforms = runtime passed variables
108  std::vector<std::string> relevantUniforms;
109  };
110  std::unique_ptr<InitParams> p_InitParams;
111 
112  //! ensure that p_InitParams exists before initialization/setup is called.
113  //! create empty structure if none exists
114  void EnsureInitParamsStruct()
115  {
116  if (!p_InitParams.get())
117  p_InitParams=(std::unique_ptr<InitParams>(new InitParams));
118  }
119 
120  public:
121  //!set shader path
122  //! \param shader_path path to program file, excluding extension (pair of .fsr
123  //! and .vsr files assumed to exist at path given);
124  void SetPath(std::string shader_path)
125  {
126  EnsureInitParamsStruct();
127  p_InitParams->path.assign(shader_path);
128  }
129  //! set pair of name and path of texture (set of such pairs actually)
130  //! for initialization
131  void SetTextures(const TexInfoVector & textures)
132  {
133  EnsureInitParamsStruct();
134  p_InitParams->textures.assign
135  (textures.begin(), textures.end());
136 
137  }
138  //! set all GL attribs important when shader will be used
139  void SetRelevantAttributes(const AttribVector & relevantAttributes)
140  {
141  EnsureInitParamsStruct();
142  p_InitParams->relevantAttributes.assign
143  (relevantAttributes.begin(), relevantAttributes.end());
144  }
145  //! set all GL/GLSL uniforms (variables passed to shader) that will
146  //! be necessary when shader will be used
147  void SetRelevantUniforms(const std::vector<std::string> relevantuniforms)
148  {
149  EnsureInitParamsStruct();
150  p_InitParams->relevantUniforms.assign(relevantuniforms.begin(),relevantuniforms.end());
151  }
152 
153  //! set all 4 initialization params (see descriptions for other functions to
154  //! see what they do)
155  void Set4InitializationParams(const std::string& path,
156  TexInfoVector textures,
157  AttribVector relevantAttributes,
158  const std::vector<std::string> relevantuniforms)
159  {
160  EnsureInitParamsStruct();
161  p_InitParams->path.assign(path);
162  p_InitParams->textures.assign
163  (textures.begin(), textures.end());
164  p_InitParams->relevantAttributes.assign
165  (relevantAttributes.begin(), relevantAttributes.end());
166  p_InitParams->relevantUniforms.assign(relevantuniforms.begin(),relevantuniforms.end());
167  }
168 
169  //! Initialize with parameters given
170  void Initialize(const std::string& path,
171  TexInfoVector textures, AttribVector relevantAttributes);
172  //! initialize with parameters given
173  void Initialize(const std::string& path,
174  TexInfoVector textures, AttribVector relevantAttributes,const std::vector<std::string> relevantuniforms);
175  //! initialize - default function - is called after all params have been set up
176  void Initialize();
177  private:
178  //! part of Initialize that has to be ran in openGL context
179  void InitializeShaderGL(char *vertShaderSrc,char *fragShaderSrc,
180  ProgramManager::iterator it);
181 
182  public:
183  //! use 4dimensional vector as uniform
184  void UseUniformVector4(const char* uniformname, const GLfloat * value);
185  //! set up everything before calling mesh. Note, that you must set all
186  //! the uniforms, matrices etc BEFORE handling this
187  void Use();
188  //! using uniform as 4-dimensional vector.
189  static void UseUniformVector4ForAllShaders(const char* uniformname, const GLfloat * value);
190  //! free textureset resources
191  void Deinitialize();
192  //!GLuint GetProgramHandle() {return programObject_Handle;}
193  GLint GetAttribLocation(std::string name);
194  //! Get location of projection matrix - an opengl handle
196  {return i_ProgramInfo->second.projmatrixLocation;}
197  //! vertex attributes (xyz of vertices, uv coordinates of textures)
198  void UseVertexAttribPointer(std::string name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid * pointer);
199  //! use projection matrix (camera)
200  //! \param value pointer to first of 16 floats constituting a matrix
201  void UseProjectionMatrix( const GLfloat * value=0);
202  //! model matrix is yet another matrix apart of camera matrix - used
203  //! for model rotation and translation. Note that this engine does NOT
204  //! feature complete matrices stack
205  //! \param value pointer to first of 16 floats constituting a matrix
206  void UseModelMatrix( const GLfloat * value);
207  //! return true if a shader is usable
208  bool IsInitialized(){return is_initialized;}
209  //! just to simulate LxS::CoreGraphics::Renderable interface
210  float GetInitializedPercentage() { return IsInitialized()?100:0;}
211  ~Shader() {Deinitialize();}
212  };
213  }
214 }
215 
216 
217 #endif