Open Inventor Release 2024.1.1
 
Loading...
Searching...
No Matches
SoShaderObject.h
1/*=======================================================================
2 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), ***
3 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. ***
4 *** ***
5 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS ***
6 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR ***
7 *** WRITTEN AUTHORIZATION OF FEI S.A.S. ***
8 *** ***
9 *** RESTRICTED RIGHTS LEGEND ***
10 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS ***
11 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN ***
12 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT ***
13 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN ***
14 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. ***
15 *** ***
16 *** COPYRIGHT (C) 1996-2023 BY FEI S.A.S, ***
17 *** BORDEAUX, FRANCE ***
18 *** ALL RIGHTS RESERVED ***
19**=======================================================================*/
20/*=======================================================================
21** Author : VSG (MMM YYYY)
22**=======================================================================*/
23
24 /* include files */
25#ifndef _SO_SHADER_OBJECT_H_
26#define _SO_SHADER_OBJECT_H_
27
28#include <Inventor/SbBasic.h>
29#include <Inventor/SbString.h>
30#include <Inventor/nodes/SoNode.h>
31#include <Inventor/fields/SoSFFilePathString.h>
32#include <Inventor/fields/SoSFEnum.h>
33#include <Inventor/fields/SoSFBool.h>
34#include <Inventor/fields/SoMFUniformShaderParameter.h>
35#include <Inventor/SbEnums.h>
36
37#include <Inventor/STL/cassert>
38#include <Inventor/STL/map>
39#include <Inventor/helpers/SbConstCharMap.h>
40
41class SoGLShaderContainer ;
42class SoGLShaderObject ;
43class SoFieldSensor ;
44class SoNodeSensor ;
45class SoFileSensor ;
46class SoShaderParser;
47
48#ifdef _MSC_VER
49#pragma warning( push )
50#pragma warning(disable:4251)
51#endif
52
204class SoShaderObject : public SoNode
205{
206
207 SO_NODE_ABSTRACT_HEADER(SoShaderObject);
208
209public:
210
213 {SoDEPRECATED
219SoDEPRECATED
225
230
235 };
236
241
252
268
274
291 template<typename UniformParamType, typename ParamValueType>
292 UniformParamType* addShaderParameter(const SbString& name, ParamValueType val);
293
296 {
298 VERTEX_SHADER = SbEnums::SHADER_TYPE_VERTEX,
300 GEOMETRY_SHADER = SbEnums::SHADER_TYPE_GEOMETRY,
302 FRAGMENT_SHADER = SbEnums::SHADER_TYPE_FRAGMENT,
304 TESSELLATION_CONTROL_SHADER = SbEnums::SHADER_TYPE_TESS_CTRL,
306 TESSELLATION_EVALUATION_SHADER = SbEnums::SHADER_TYPE_TESS_EVAL,
308 COMPUTE_SHADER = SbEnums::SHADER_TYPE_COMPUTE,
309 };
310
312 virtual ShaderType getShaderType() const = 0;
313
328 template<typename UniformParamType, typename ParamValueType>
329 void setShaderParameter(const SbString& name, ParamValueType val);
330
331#ifndef HIDDEN_FROM_DOC
332 //Generate all setFragmentParameter* methods
333#define MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(ParamType)\
334 \
335 template<typename ParamValueType>\
336 void\
337 setShaderParameter ## ParamType(const SbString& name, ParamValueType val)\
338 {\
339 setShaderParameter<SoShaderParameter ## ParamType>(name, val);\
340 }
341 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(1i)
342 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(2i)
343 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(3i)
344 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(4i)
345 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(1f)
346 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(2f)
347 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(3f)
348 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(4f)
349 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array1f)
350 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array2f)
351 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array3f)
352 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array4f)
353 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array1i)
354 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array2i)
355 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array3i)
356 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array4i)
357 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Matrix)
358 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Matrix3)
359#undef MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER
360
361//Generate all addShaderParameter* methods
362#define MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(ParamType)\
363 \
364 template<typename ParamValueType>\
365 SoShaderParameter ## ParamType *\
366 addShaderParameter ## ParamType(const SbString& name, ParamValueType val)\
367 {\
368 return addShaderParameter<SoShaderParameter ## ParamType>(name, val);\
369 }
370 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(1i)
371 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(2i)
372 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(3i)
373 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(4i)
374 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(1f)
375 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(2f)
376 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(3f)
377 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(4f)
378 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array1f)
379 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array2f)
380 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array3f)
381 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array4f)
382 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array1i)
383 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array2i)
384 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array3i)
385 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array4i)
386 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Matrix)
387 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Matrix3)
388#undef MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER
389#endif //HIDDEN_FROM_DOC
390
391 /*----------------------------------------------------------------------------*/
392
393private:
394
396 void setShaderObjectInfos(SoState* state, const SbString& shaderVersion, const SbString& shaderProfile);
397
399 SourceType getSourceType();
400
401 static void initClass();
402 static void exitClass() ;
403
408 SbBool isInternal() const;
409
410 // Returns TRUE if at least one parameter is of SoShaderStateMatrixParameter type
411 SbBool containStateMatrixParameters() const
412 { return (m_stateMatrixParameters.getLength() != 0) ; }
413
414 const SbString& getSourceProgram() const
415 { return m_sourceProgram;}
416
420 inline bool isFileFound() const;
421
423 inline bool needUpdate() const { return m_needUpdate; }
424
426 void invalidate();
427
429 void setDefine(const char* name, const char* value);
430 void setDefine(const char* name, const SbString& value)
431 { setDefine(name,value.toLatin1()); }
432
434 void removeDefine(const char* name);
435
437 void checkValidity();
438
440 inline void setVersion(const SbString& version) { m_version = version; }
441
443 const SbString& getVersion();
444
446 const SbString& getProfile();
447
449 inline void setProfile(const SbString& profile) { m_profile = profile; }
450
452 static bool isShaderObjectAvailable(SoState* state);
453
455 static bool isGLSLLanguageAvailable(SoState* state);
456
458 void invalidateSource();
459
461 void buildSourceToLoad(SbString &builtSource);
462
464 SbString getFileAbsolutePath(const SbString& filename);
465
467 virtual void notify(SoNotList *list);
468
470 void getHeaderFilenames( std::vector<SbString>& filenames );
471
473 void setIsFixedPipeline(bool isFixedPipelineShader)
474 {
475 m_isFixedPipelineShader = isFixedPipelineShader;
476 }
477
479 bool isFixedPipeline() const
480 {
481 return m_isFixedPipelineShader;
482 }
483
484private:
485
486 friend class SoShaderProgram;
488 typedef SbConstCharMap<SbString> DefineMap;
489 typedef DefineMap::iterator DefineMapIt;
490
492 typedef std::vector<SbString> SbStringVector;
493
495 void setGlobalDefine(const DefineMap& map);
496
498 void setDefine(DefineMap::value_type& p);
499
501 bool hasDefine(const char* name) const;
502
504 typedef std::set<SbString> HeaderSet;
505 typedef HeaderSet::iterator HeaderSetIt;
506
508 void setGlobalHeader(const HeaderSet& map);
509
510 // Extract the source from a given file if sourceType equals FILENAME.
511 void readSource();
512
517
518 // Destructor
519 virtual ~SoShaderObject();
520
521private:
522 SoShaderParser* m_parser;
523 SbString m_sourceProgram;
524
526 SbString m_version;
527 SbString m_profile;
529 typedef std::map<std::string, std::string> ExtensionMap;
530 ExtensionMap m_extensionList;
531
532 typedef std::map<SbString, SbString> StrStrMap;
533
541 void extractInfosFromSource(SbString& version, SbString& source,SbString& profile, ExtensionMap& extensionList);
542
544 bool isEmptyLine(const std::string& line) const;
545
547 static SbString definesExtension(const SbString& source, const ExtensionMap::value_type& p);
548
550 static SbString definesAcc(const SbString& source, const DefineMap::value_type& p);
551
554 void getSourceFilename(std::vector<SbString>& filenames) const;
555
557 DefineMap m_defineMap;
558
560 DefineMap m_defineGlobalMap;
561
563 HeaderSet m_headerGlobalSet;
564
566 SbStringVector m_includeSourceVector;
567
569 SbStringVector m_includeFilenameVector;
570
572 SbString m_directoryFound;
573
574 // Update the list of state matrix parameters
575 void updateStateMatrixList() ;
576
577 // Store the list of state matrix parameters
578 SoNodeList m_stateMatrixParameters ;
579
580 // True if the shader can't be read
581 bool m_fileFound;
582
584 bool m_needUpdate;
585
587 bool m_needRead;
588
589 bool m_isFixedPipelineShader;
590
592 static StrStrMap s_shadersPaths;
593 static SbThreadRWMutex s_shadersPathsMutex;
594 static bool s_debugCache;
595};
596/*----------------------------------------------------------------------------*/
597
598/*******************************************************************************/
599template<typename UniformParamType, typename ParamValueType>
600UniformParamType*
601SoShaderObject::addShaderParameter(const SbString& name, ParamValueType val)
602{
603 UniformParamType *param = dynamic_cast<UniformParamType *>(parameter.findUniformParameterByName(name));
604 if (param)
605 {
606 // This parameter already exist => prefer to replace it
607 param->value.setValue(val);
608 }
609 else
610 {
611 param = new UniformParamType;
612 param->name = name;
613 param->value.setValue(val);
615 }
616
617 return param;
618}
619
620/*****************************************************************************/
621template<typename UniformParamType, typename ParamValueType>
622void
623SoShaderObject::setShaderParameter(const SbString& name, ParamValueType val)
624{
626 assert(param);
627 assert(param->isOfType(UniformParamType::getClassTypeId()) == TRUE);
628 UniformParamType* i = static_cast<UniformParamType*>(param);
629 i->value = val;
630}
631
632/*****************************************************************************/
633bool
634SoShaderObject::isFileFound() const
635{
636 return m_fileFound;
637}
638
639#ifdef _MSC_VER
640#pragma warning( pop )
641#endif
642
643#endif /*_SO_SHADER_OBJECT_H_*/
644
645
int getLength() const
Returns number of pointers in list.
Definition SbPList.h:125
Class for smart character strings.
Definition SbString.h:202
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Portable readers...
Sensor class that can be attached to Open Inventor fields.
Sensor that triggers a callback each time a file is changed.
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Multiple-value f...
void addShaderParameter(SoUniformShaderParameter *shaderParameter)
Adds a uniform shader parameter as last one in group.
SoUniformShaderParameter * findUniformParameterByName(const SbString &name)
Find the first parameter with the given name.
Abstract base class for all database nodes.
Definition SoNode.h:145
Maintains a list of pointers to nodes.
Definition SoNodeList.h:74
Sensor class that can be attached to Open Inventor nodes.
Field containing a single Boolean value.
Definition SoSFBool.h:79
Field containing an enumerated value.
Definition SoSFEnum.h:89
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Field containing...
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Abstract node cl...
void setShaderParameter(const SbString &name, ParamValueType val)
UniformParamType is the type of parameter to set.
SoSFBool isActive
Specifies if the shader object is active or not.
SoSFFilePathString sourceProgram
Contains the shader object's source program, specified by a filename (sourceType set to FILENAME) or ...
UniformParamType * addShaderParameter(const SbString &name, ParamValueType val)
Convenience method to create an SoShaderUniformParameter with the specified name and value and add it...
virtual ShaderType getShaderType() const =0
Must be redefined by derived class.
SoMFUniformShaderParameter parameter
Contains the shader's uniform parameters.
ShaderType
Type of the shader.
@ TESSELLATION_EVALUATION_SHADER
The shader is a tessellation evaluation shader.
@ VERTEX_SHADER
The shader is a vertex shader.
@ COMPUTE_SHADER
The shader is a compute shader.
@ FRAGMENT_SHADER
The shader is a fragment shader.
@ GEOMETRY_SHADER
The shader is a geometry shader.
@ TESSELLATION_CONTROL_SHADER
The shader is a tessellation control shader.
SoSFEnum sourceType
Specifies the shader object's source type.
SourceType
Shader Object source type possible values.
@ CG_PROGRAM
The source is a CG program.
@ FILENAME
Only the name of the file containing the source is given (default).
@ GLSL_PROGRAM
The source is an OpenGL Shading Language program.
@ ARB_PROGRAM
The source is an ARB vertex or fragment program.
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Shader program p...
Traversal state.
Definition SoState.h:74
SbBool isOfType(const SoType &type) const
Returns TRUE if this object is of the type specified in type or is derived from that type.
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Abstract base cl...
int SbBool
Boolean type.
Definition SbBase.h:87