Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SimpleMultiPassFragmentShader.hh
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #ifndef SIMPLE_MULTIPASS_FRAGMENT_SHADER_HH_
26 #define SIMPLE_MULTIPASS_FRAGMENT_SHADER_HH_
27 
28 #include <OpenGLFramework/RenderingContext/glfPBuffer.hh>
29 #include <OpenGLFramework/Utils/ShaderProgramPool.hh>
30 #include <OpenGLFramework/Utils/Texture2DPool.hh>
31 #include <OpenGLFramework/Utils/FramebufferSetupPool.hh>
32 #include <OpenGLFramework/Base/glfBatch.hh>
33 #include <OpenGLFramework/Utils/Primitives.hh>
34 #include <map>
35 #include <bias_config.h>
36 
37 namespace BIAS
38 {
39 
40  /** Class allows to add actions different then rendering to the multipass shader execution.
41  * \ingroup g_openglframework
42  * \author bartczak 03/2009
43  **/
44  class BIASOpenGLFramework_EXPORT SMPFSActionInterface {
45  public:
46  /** Executes the post action.
47  * \param currentIter is the current iteration of the Execute call.
48  * \param numIter is the overall number of iterations of the Execute call.
49  **/
51  virtual void Execute(const unsigned int& currentIter, const unsigned int& numIter) = 0;
52  virtual SMPFSActionInterface* Clone() const = 0;
53  /** Allows to bring the object to some initial position. **/
54  virtual void Reset() {};
55 
56  };
57 
58  /**
59  * this class collects a number of shaders and textures uniquely identified by their names
60  * shader uniforms can be set by addressing the shaders by their names
61  * textures a shader reads from and writes to can be spezified by setting input and output textures addressing the textures through their names
62  * calling Execute with a vector of shader names as parameter executes all shaders on the gpu in the order given by the shader names vector
63  *
64  * Usage Example:
65  * SimpleMultiPassFragmentShader smpfs;
66  * smpfs.Init(); // init pBuffer context. Init(false) requires glContext to be created manually
67  *
68  * smpfs.AddFragmentShader(shader_source, "ShaderName"); // shader_source is a string
69  *
70  * smpfs.LoadTexture(...) or smpfs.CreateTexture(...)
71  *
72  * smpfs.SetShaderVariable(...) // set uniform variables if required; textures not allowed
73  *
74  * smpfs.SetInputTextures("ShaderName", ...) and smpfs.SetOutputTextures("ShaderName", ...)
75  *
76  * for (...) {
77  * smpfs.Execute(...);
78  * smpfs.SetShaderVariable(...); //change uniform variables
79  * }
80  *
81  * smpfs.CopyToImage(string("VariableName"), image); //get result from texture
82  *
83  * \ingroup g_openglframework
84  * \author frick
85  **/
86  class BIASOpenGLFramework_EXPORT SimpleMultiPassFragmentShader
87  {
88  public:
89 
90  /**
91  * standard constructor
92  */
94 
95  /**
96  * initializes openGL context (if createGLContext = true) and some internal variables, must be called first
97  */
98  void
99  Init(bool createGLContext = true);
100 
101  /**
102  * resets this object
103  */
104  void
105  Destroy();
106 
107  /**
108  * adds an fragment shader with name shader_name and shader source code shader_source
109  */
110  void
111  AddFragmentShader(const std::string& shader_source,
112  const std::string& shader_name);
113 
114  /**
115  * adds an fragment shader with name shader_name from file shader_path to the shader_source
116  */
117  void
118  AddFragmentShaderFromFile(const std::string& shader_path,
119  const std::string& shader_name);
120 
121  /**
122  * loads an image as texture with name texture_name
123  */
124  void
125  LoadTexture(const ImageBase& image, const std::string& texture_name,
126  GLenum minFilter = GL_NEAREST, GLenum magFilter = GL_NEAREST,
127  GLenum wrapS = GL_CLAMP, GLenum wrapT = GL_CLAMP,
128  GLenum internalFormat = 0, int mipmap = 0);
129 
130  /**
131  * creates new texture with name texture_name
132  * (for example storageType ImageBase::ST_float, colorModel ImageBase::CM_Grey creates a 1 channel float texture)
133  *
134  */
135  void
136  CreateTexture(int width, int height, ImageBase::EStorageType storageType,
137  ImageBase::EColorModel colorModel, const std::string& texture_name,
138  GLenum minFilter = GL_NEAREST, GLenum magFilter = GL_NEAREST,
139  GLenum wrapS = GL_CLAMP, GLenum wrapT = GL_CLAMP, int mipmap = 0);
140  void
141  CreateTexture(int width, int height, GLenum internalFormat, const std::string& texture_name,
142  GLenum minFilter = GL_NEAREST, GLenum magFilter = GL_NEAREST,
143  GLenum wrapS = GL_CLAMP, GLenum wrapT = GL_CLAMP, int mipmap = 0);
144 
145  /**
146  * sets all textures from vector texture_names as input textures
147  * the vector sampler2D holds the names of the textures in shader source code
148  * each texture_names[i] will be bind to the shader uniform sampler2D variable with name sampler2D[i]
149  */
150  void
151  SetInputTextures(const std::string& shader_name, const std::vector<
152  std::string>& samplers2D,
153  const std::vector<std::string>& texture_names);
154 
155  /**
156  * Asociated the texture from texture pool with name <texturePoolName>, with the
157  * uniform sampler named <uniformName> in shader source of shader <sahderName>.
158  */
159  void
160  SetInputTextures(const std::string& shaderName, const std::string& uniformVarName,
161  const std::string& texturePoolName);
162 
163  /**
164  * sets all textures from vector texture_names as output textures for the shader shader_name
165  */
166  void
167  SetOutputTextures(const std::string& shader_name,
168  const std::vector<std::string>& texture_names, const std::string& depthTextureName = "");
169 
170  /**
171  * sets a texture with name texture_name as output texture for the shader shader_name
172  */
173  void
174  SetOutputTexture(const std::string& shader_name,
175  const std::string& texture_name, const std::string& depthTextureName = "");
176 
177  /**
178  * executes all shaders with names in shader_names vector in the given order (iter_number)-times
179  */
180  void
181  Execute(const std::vector<std::string>& shader_names,
182  unsigned int iter_number = 1);
183 
184  /**
185  * copies texture with the texture_name to image
186  */
187  void
188  CopyToImage(const std::string& texture_name, ImageBase& image);
189 
190  /**
191  * copies texture with the texture_name to image
192  */
193  void
194  CopyChannelsToImage(const std::string& texture_name, GLenum format, ImageBase& image);
195 
196 
197  /**
198  * sets the value of uniform shader variable var_name for shader shader_name
199  * if the variable type is sampler2D then an glfException is thrown
200  * use SetOutput/InputTextures to set this variablews
201  */
202  template<class StorageType>
203  void
204  SetShaderVariable(const std::string& shader_name,
205  const std::string& var_name, StorageType value);
206 
207 
208  /** Sets the depth test for the particular shader execution.
209  * It is not required that the shader already exists, if not used the shaders
210  * use the default behaviour implemented in glfDepthBufferMode.
211  * \author bartczak 03/2009
212  **/
213  void SetDepthTest(const std::string& shaderName,
214  const bool enabled, GLenum depthFunc);
215 
216  /** Enables the clearence of the output, each time when rendered into.
217  * Is per default enabled with the given default parameters.
218  * \author bartczak 03/2009
219  **/
220  void EnableColorClearance(const std::string& shaderName,
221  float red = 0.0f, float green = 0.0f, float blue = 0.0f, float alpha = 0.0f);
222 
223  /** Enables the clearence of the output, each time when rendered into.
224  * Is per default enabled with the given default parameters.
225  * \author bartczak 03/2009
226  **/
227  void EnableDepthClearance(const std::string& shaderName, float depth = 1.0f);
228 
229  /** Disables the clearence of the output when rendered into.
230  * Per default the clearance is enabled.
231  * \author bartczak 03/2009
232  **/
233  void DisableColorClearance(const std::string& shaderName);
234 
235  /** Disables the clearence of the output when rendered into.
236  * Per default the clearance is enabled.
237  * \author bartczak 03/2009
238  **/
239  void DisableDepthClearance(const std::string& shaderName);
240 
241  Texture2DPool& GetTexture2DPool() {return texture_pool_;}
242  FramebufferSetupPool& GetFramebufferSetupPool() {return framebuffer_setup_pool_;}
243  glfFramebufferObject* GetFBO() {return framebuffer_setup_pool_.GetFBO();}
244 
245 
246  /** Sets an action that is performed after the respective shader has run.
247  * Shader does not have to exist.
248  **/
249  void SetPostAction(const std::string& shaderName, const SMPFSActionInterface& postAction);
250  void ResetPostAction(const std::string& shaderName);
251  void ClearPostAction(const std::string& shaderName);
252 
253  /** Instead ogf processing a whole image, the fragment shaders are run only on a set of
254  * symmetric patches.
255  * \param referenceWidth the patches are calculated relative to this target resolution width.
256  * \param referenceHeight the patches are calculated relative to this target resolution height.
257  * \param posX the patche's center x position relative to width.
258  * \param posX the patche's center y position relative to height.
259  * \param hw the patche's half window size.
260  * \attention This has to be changed for every target resolution.
261  * \attention Call after init.
262  * \author bartczak 01/2010
263  **/
264  void EvaluateOnlyPatches(const unsigned int referenceWidth,
265  const unsigned int referenceHeight,
266  const std::vector<unsigned int>& posX,
267  const std::vector<unsigned int>& posY,
268  const unsigned int hw);
269 
270  /** Instead of processing a whole image, the fragment shaders are run only on a region around an image line.
271  * \param referenceWidth the lines are calculated relative to this target resolution width.
272  * \param referenceHeight the lines are calculated relative to this target resolution height.
273  * \param posy the line's y position.
274  * \param hw the half window size around the lines y position.
275  * \attention This has to be changed for every target resolution.
276  * \attention Call after init.
277  * \author bartczak 08/2010
278  **/
279  void EvaluateOnlyLine(const unsigned int referenceWidth,
280  const unsigned int referenceHeight,
281  const unsigned int posY,
282  const unsigned int hw);
283 
284  private:
285 
286  std::vector<std::pair<glfTexture*, std::pair<std::string, std::string> > >*
287  GetInputTextures_(const std::string& shader_name);
288  void
289  SetInputTextures_(const std::string& shader_name, std::vector<std::pair<
290  glfTexture*, std::pair<std::string, std::string> > >& textures);
291  void
292  ActivateInputTextures_(const std::string& shader_name,
293  std::vector<std::pair<glfTexture*, std::pair<std::string,
294  std::string> > >& textures);
295 
296  bool IsInputTexture_(const std::string& texture_name);
297  bool IsOutputTexture_(const std::string& texture_name);
298 
299  std::vector<std::string>*
300  GetOutputTextureNames_(const std::string& shader_name);
301 
302  bool initialized_, createdGLContext_;
303 
304  glfPBuffer pbuffer_;
305  Texture2DPool texture_pool_;
306  FramebufferSetupPool framebuffer_setup_pool_;
307  ShaderProgramPool shader_pool_;
308  // key: shader program name - value: vector of pairs: texture pointer -- pair: texture sampler2D name, texture user name (pool name)
309  std::map<std::string, std::vector<std::pair<glfTexture*, std::pair<
310  std::string, std::string> > > > input_textures_;
311 
312  //##################
313  glfBatch batch_;
314  void
315  InitBatch_()
316  {
317  // batch_.SetDepthBufferMode(&depthTest_);
318  batch_.SetElementBuffer(&elements_);
319  batch_.SetVertexBuffer(&vertices_);
320  batch_.SetViewport(&viewport_);
321  batch_.SetModelViewMatrix(&ModelViewMatrix_);
322  batch_.SetProjectionMatrix(&ProjectionMatrix_);
323  }
324 
325  glfVertexBuffer vertices_;
326  glfElementBuffer elements_;
327  void
328  InitPrimitiveData_()
329  {
330  Primitives::PlainQuad2DWithTexture2D(vertices_, elements_, false);
331  }
332 
333  glfMatrix ModelViewMatrix_;
334  glfMatrix ProjectionMatrix_;
335  glfViewport viewport_;
336  void
337  InitVertexTransformation_()
338  {
339  ModelViewMatrix_.MakeIdentity();
340  ProjectionMatrix_.MakeIdentity();
341  }
342 
343 
344  std::map<std::string, glfDepthBufferMode> depthTest_;
345  void ActivateDepthTest_(const std::string& shaderName);
346 
347  class ActionGuard{
348  public:
349  ActionGuard();
350  ~ActionGuard();
351  ActionGuard(const SMPFSActionInterface* action);
352  //void operator=(const SMPFSActionInterface& action);
353  void operator=(const SMPFSActionInterface* action);
354  void Execute(const unsigned int& currentIter, const unsigned int& numIter);
355  ActionGuard(const ActionGuard& a);
356  ActionGuard& operator=(const ActionGuard& a);
357  void Reset();
358  private:
359  SMPFSActionInterface* action_;
360  };
361  std::map<std::string, ActionGuard> postActions_;
362 
363  };
364 
365  /** Pingpongs **/
366  /*class BIASOpenGLFramework_EXPORT SwapTextureObjectsAction : public SMPFSActionInterface {
367  public:
368  SwapTextureObjectsAction(glfTexture* texA, glfTexture* texB);
369  void SkipLastIteration();
370  void MaxNumExecutions(unsigned int maxExecutions);
371  void Reset();
372 
373 
374  private:
375  bool skipLastIteration_;
376  unsigned int maxExecutions_;
377  unsigned int execCounter_;
378  glfTexture* texA_;
379  glfTexture* texB_;
380 
381  }*/
382 
383 }
384 
385 #endif /* SIMPLE_MULTIPASS_FRAGMENT_SHADER_HH_ */
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
this class represents a set of shader programs one can add and remove shader programs to and from the...
Class allows to add actions different then rendering to the multipass shader execution.
this class collects a number of shaders and textures uniquely identified by their names shader unifor...
A batch represents a single Draw call including all parameters (render states).
Definition: glfBatch.hh:48
virtual ~SMPFSActionInterface()
Executes the post action.
Convenience container for managing 2D textures.
Class containing a framebuffer and a map of setups.
GLX pbuffer rendering context.
static void PlainQuad2DWithTexture2D(BIAS::glfVertexBuffer &vb, BIAS::glfElementBuffer &eb, bool flip=false)
Vertex Order: 3–2 | | 0–1 .
Definition: Primitives.cpp:263
virtual void Reset()
Allows to bring the object to some initial position.
Base class for textures.
Definition: glfTexture.hh:42
void SetElementBuffer(const glfElementBuffer *elementBuffer)
Sets the element buffer.
Definition: glfBatch.cpp:134
This is the base class for images in BIAS.
Definition: ImageBase.hh:102