H3D API  2.4.1
ComposedShader.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 #ifndef __COMPOSEDSHADER_H__
30 #define __COMPOSEDSHADER_H__
31 
32 #include <H3D/X3DShaderNode.h>
33 #include <H3D/ShaderPart.h>
36 #include <H3D/FieldTemplates.h>
37 #include <H3D/SField.h>
38 #include <string>
39 #include <H3D/ShaderFunctions.h>
41 #include <H3D/ShaderConstants.h>
42 
43 namespace H3D {
44 
71 
72 
73 //#define EXPORT_SHADER
74 
75  class H3DAPI_API ComposedShader :
76  public X3DShaderNode,
78  public:
79 
87 
88  class H3DAPI_API SFShaderConstants : public SFShaderConstantsBase {
89  protected:
90  virtual void onAdd( Node *n );
91  };
92 
93  typedef std::map< string , H3D::Shaders::UniformInfo > UniformFieldMap;
94 
95  // a map to maintain the uniform values and their related properties
96  UniformFieldMap uniformFields;
97 
100  typedef DependentMFNode< ShaderPart,
104  true >
106 
109  class H3DAPI_API MFShaderPart: public MFShaderPartBase {
110  protected:
111  virtual void onAdd( Node *);
112  virtual void onRemove( Node *);
113  };
114 
115 #ifdef EXPORT_SHADER
117  class H3DAPI_API UpdateSaveShadersToUrl: public OnNewValueSField < AutoUpdate < SFString > >
118  {
119  protected:
120  virtual void onNewValue( const std::string &v );
121  };
122 
123 
124 #endif
125 
127  ComposedShader( Inst< DisplayList > _displayList = 0,
128  Inst< SFNode > _metadata = 0,
129  Inst< SFBool > _isSelected = 0,
130  Inst< SFBool > _isValid = 0,
131  Inst< SFBool > _activate = 0,
132  Inst< SFString > _language = 0,
133  Inst< MFShaderPart > _parts = 0,
134  Inst< SFBool > _suppressUniformWarnings = 0,
135  Inst< SFString > _geometryInputType = 0,
136  Inst< SFString > _geometryOutputType = 0,
137  Inst< SFInt32 > _geometryVerticesOut = 0,
138  Inst< SFString > _transparencyDetectMode = 0,
139  Inst< MFString > _transformFeedbackVaryings = 0,
140  Inst< SFBool > _printShaderWarnings = 0,
141  Inst< SFShaderConstants > _shaderConstants = 0
142 #ifdef EXPORT_SHADER
143  ,
144  Inst< UpdateSaveShadersToUrl > _saveShadersToUrl = 0
145 #endif
146  );
147 
149  virtual ~ComposedShader();
150 
153  virtual bool addField( const string &_name,
154  const Field::AccessType &access,
155  Field *field );
156 
159  virtual bool removeField ( const string& _name );
160 
165  virtual void traverseSG ( TraverseInfo& ti );
166 
168  virtual void render();
169 
171  virtual void preRender();
172 
174  virtual void postRender();
175 
179  virtual GLbitfield getAffectedGLAttribs();
180 
185  virtual bool isTransparent( X3DMaterialNode *material );
186 
189  GLhandleARB getProgramHandle() {
190  return program_handle;
191  }
192 
193 #ifdef EXPORT_SHADER
195  H3DUniquePtr< UpdateSaveShadersToUrl > saveShadersToUrl;
196 #endif
197 
204  H3DUniquePtr< SFShaderConstants > shaderConstants;
205 
211  H3DUniquePtr< MFShaderPart > parts;
212 
227  H3DUniquePtr< SFBool > suppressUniformWarnings;
228 
238  H3DUniquePtr< SFString > geometryInputType;
239 
248  H3DUniquePtr< SFString > geometryOutputType;
249 
257  H3DUniquePtr< SFInt32 > geometryVerticesOut;
258 
259 
280  H3DUniquePtr< SFString > transparencyDetectMode;
281 
292  H3DUniquePtr< MFString > transformFeedbackVaryings;
293 
300  H3DUniquePtr< SFBool > printShaderWarnings;
301 
304  protected:
305 
307  class UpdateUniforms : public EventCollectingField < Field > {
308  virtual void update ();
309  };
310 
311  static bool shader_support_checked;
312 
316 
318  GLhandleARB program_handle;
319 
322  vector< GLhandleARB > current_shaders;
323 
324  // list of program handles separated by key
325  static map<string, GLhandleARB> phandles_map;
326  static map<GLhandleARB, int> phandle_counts;
327 
328  // create program handle for a shader
329  static GLhandleARB createHandle(ComposedShader* shader);
330 
331  // try to find existing handle (with the same signature), if not, go create one
332  static std::string genKeyFromShader(ComposedShader* shader);
333 
335  void setGeometryShaderParameters( GLenum _program_handle);
336 
341  class SetupDynamicRoutes : public AutoUpdate< Field > {
342  protected:
343  virtual void update();
344 
345  public:
346  // Map from Fields to nodes in the fields, needed in
347  // order to remove nodes properly.
348  map< Field *, NodeVector > fields_to_nodes;
349  };
350 
352  H3DUniquePtr< SetupDynamicRoutes > setupDynamicRoutes;
353 
355  list<H3DSingleTextureNode*> shader_textures;
356 
358  H3DUniquePtr<UpdateUniforms> updateUniforms;
359 
361  class UpdateCache: public OnValueChangeSField< SFBool >
362  {
363  protected:
364  virtual void onValueChange( const bool &new_value );
365  };
366  // this field will be used to react to global caching changing, this will
367  // be used to control whether to route uniform field to displayList
368  H3DUniquePtr<UpdateCache> updateCache;
369 
370  H3DInt32 max_texture_in_shader;
371 
372  H3DInt32 max_image_in_shader;
373 
374  bool printShaderLog();
375  DebugOptions *debug_options_previous;
376  public:
377  virtual void initialize();
378  };
379 }
380 
381 #endif
Contains the DependentSFNode and DependentMFNode template classes.
Contains different templates to modify field behaviour.
Header file for H3DSingleTextureNode, X3D scene-graph node.
Contains the SField template class.
Header file for ShaderConstants.
Header file for help functions used by shader nodes.
Header file for ShaderPart, X3D scene-graph node.
Header file for X3DProgrammableShaderObject.
Header file for X3DShaderNode, X3D scene-graph node.
MFShaderPart is specialized to transfer a pointer from itself to the ShaderPart instances contained i...
Definition: ComposedShader.h:109
Class used to set up routes from nodes in SFNode and MFNode fields to correctly break cache.
Definition: ComposedShader.h:341
event collection field to update when global caching is update
Definition: ComposedShader.h:362
A field used to update any uniforms from their corresponding fields.
Definition: ComposedShader.h:307
The ComposedShader node defines a shader where the individual source files are not individually progr...
Definition: ComposedShader.h:77
vector< GLhandleARB > current_shaders
A vector of the handles to all shader objects that are currently linked into the program object.
Definition: ComposedShader.h:322
H3DUniquePtr< SetupDynamicRoutes > setupDynamicRoutes
C++ fields only. Contains instance of SetupDynamicRoutes.
Definition: ComposedShader.h:352
DependentSFNode< ShaderConstants, FieldRef< H3DDisplayListObject, H3DDisplayListObject::DisplayList, &H3DDisplayListObject::displayList >, true > SFShaderConstantsBase
The SFShaderConstants field is dependent on the displayList field of the containing ShaderConstants n...
Definition: ComposedShader.h:86
H3DUniquePtr< MFString > transformFeedbackVaryings
List of varying names to capture during transform feedback.
Definition: ComposedShader.h:292
GLhandleARB program_handle
The handle to the program object used for the shader in OpenGL.
Definition: ComposedShader.h:318
DependentMFNode< ShaderPart, FieldRef< ShaderPart, ShaderPart::SFShaderString, &ShaderPart::shaderString >, true > MFShaderPartBase
The MFShaderPartBase is dependent on the url field of the containing ShaderPart node.
Definition: ComposedShader.h:105
H3DUniquePtr< MFShaderPart > parts
The shader parts to use in the ComposedShader.
Definition: ComposedShader.h:211
static bool tessellation_support_checked
True if a warning has already been output about missing tessellation shader support.
Definition: ComposedShader.h:315
GLhandleARB getProgramHandle()
Returns the OpenGL shader program handle used by the ComposedShader.
Definition: ComposedShader.h:189
static H3DNodeDatabase database
The H3DNodeDatabase for this node.
Definition: ComposedShader.h:303
H3DUniquePtr< SFInt32 > geometryVerticesOut
The number of vertices the geometry shader generates.
Definition: ComposedShader.h:257
list< H3DSingleTextureNode * > shader_textures
list of textures to be used by the composedshader
Definition: ComposedShader.h:355
H3DUniquePtr< SFBool > suppressUniformWarnings
Suppress the warnings printed to the console about setting uniform fields that do not exist in the sh...
Definition: ComposedShader.h:227
H3DUniquePtr< SFString > geometryInputType
The input geometry type of the geometry being rendered with a geometry shader.
Definition: ComposedShader.h:238
H3DUniquePtr< SFBool > printShaderWarnings
Always print shader warnings to console.
Definition: ComposedShader.h:300
H3DUniquePtr< SFString > geometryOutputType
The output geometry type of the geometry being rendered with a geometry shader.
Definition: ComposedShader.h:248
H3DUniquePtr< SFString > transparencyDetectMode
Determines how the render system will determine if the shader used generates any fragment that has an...
Definition: ComposedShader.h:280
H3DUniquePtr< UpdateUniforms > updateUniforms
A field used to update any uniforms from their corresponding fields.
Definition: ComposedShader.h:358
H3DUniquePtr< SFShaderConstants > shaderConstants
The shaderConstants field can contain a ShaderConstants node defining constants that can be used by a...
Definition: ComposedShader.h:204
Specifies parameters for if displaying debug information like bounding boxes, haptically rendered tri...
Definition: DebugOptions.h:46
DependentMFNode are modified TypedMFNode fields where the field dirty status is dependent on fields i...
Definition: DependentNodeFields.h:336
DependentSFNode are modified TypedSFNode fields where the field dirty status is dependent on fields i...
Definition: DependentNodeFields.h:294
The EventCollection field collects all fields routed to it that generates event between calls to the ...
Definition: FieldTemplates.h:145
Class used for specifying a field member of a node in a DependentSFNode or DependentMFNode specificat...
Definition: DependentNodeFields.h:48
The Field class.
Definition: Field.h:46
AccessType
The different access types that a field can have.
Definition: Field.h:53
We use the Field's lazy evaluation mechanisms to manage the GL display lists for rendering,...
Definition: H3DDisplayListObject.h:60
This abstract interface class is the base class for all node types that wants to create an OpenGL dis...
Definition: H3DDisplayListObject.h:54
H3DUniquePtr< DisplayList > displayList
The DisplayList instance handling the OpenGL caching of this object.
Definition: H3DDisplayListObject.h:256
Node is the base class for all classes that can be part of the H3D scene-graph.
Definition: Node.h:46
Template for adding the virtual function onNewValue that can be overridden in subclasses in order to ...
Definition: FieldTemplates.h:79
Template for adding the virtual function onValueChange that can be overridden in subclasses in order ...
Definition: FieldTemplates.h:49
The ShaderConstants node is a node will be used to gather all static shader inputs to be used in the ...
Definition: ShaderConstants.h:34
Update the string to use as shader depending from the urls given.
Definition: ShaderPart.h:83
The ShaderPart node defines the source for a single object to be used by a ComposedShader node.
Definition: ShaderPart.h:79
H3DUniquePtr< SFShaderString > shaderString
A field that contains the string loaded from file and to be used as input to the shader.
Definition: ShaderPart.h:163
TraverseInfo is a structure that is passed along when traversing the scene graph.
Definition: TraverseInfo.h:57
This is the base node type for all Material nodes.
Definition: X3DMaterialNode.h:41
This abstract node type is the base type for all node types that specify arbitrary fields for interfa...
Definition: X3DProgrammableShaderObject.h:64
This abstract node type is the base type for all node types which specify a programmable shader.
Definition: X3DShaderNode.h:61
int H3DInt32
H3D API namespace.
Definition: Anchor.h:38
The AutoUpdate field is a template to force the BaseField to update itself as soon as an event is rec...
Definition: FieldTemplates.h:130
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194