Open Inventor Release 2024.2.1
 
Loading...
Searching...
No Matches
SoShaderProgram.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-2024 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_PROGRAM_H_
26#define _SO_SHADER_PROGRAM_H_
27
28#include <Inventor/SbBasic.h>
29#include <Inventor/nodes/SoNode.h>
30#include <Inventor/nodes/SoNode.h>
31#include <Inventor/fields/SoMFNode.h>
32#include <Inventor/nodes/SoShaderParameter.h>
33#include <Inventor/nodes/SoFragmentShader.h>
34#include <Inventor/nodes/SoVertexShader.h>
35#include <Inventor/nodes/SoTessellationControlShader.h>
36#include <Inventor/nodes/SoTessellationEvaluationShader.h>
37#include <Inventor/nodes/SoTexture.h>
38#include <Inventor/nodes/SoGeometryShader.h>
39#include <Inventor/nodes/SoShaderParameterImage.h>
40#include <Inventor/STL/vector>
41#include <Inventor/STL/cassert>
42#include <Inventor/helpers/SbConstCharMap.h>
43#include <Inventor/elements/SoEnvironmentElement.h>
44#include <memory>
45#include <unordered_map>
46#include <Inventor/renderer/RendererResourceMacro.h>
47#include <Inventor/sensors/SoFileSensor.h>
48
49
50#ifdef _WIN32
51#pragma warning(push)
52#pragma warning(disable:4251)
53#endif
54
56class SoGLShaderProgram ;
57class SoGLShaderObject;
58class SoFieldSensor ;
59class SoLight;
60class SoCache;
62class SoComputeShader;
63
65namespace inventor { namespace helper { class ShaderLibrary; } }
66namespace inventor { namespace renderer { class ShaderStateUniforms; } }
69/*----------------------------------------------------------------------------*/
70
232{
233
234 SO_NODE_HEADER( SoShaderProgram );
235 RENDERER_RESOURCE(SoShaderProgram);
236
237public:
240 {
247 POINTS_INPUT = GL_POINTS,
248
257 LINES_INPUT = GL_LINES,
258
267 TRIANGLES_INPUT = GL_TRIANGLES
268 };
269
274 {
275 POINTS_OUTPUT = GL_POINTS,
276 LINE_STRIP_OUTPUT = GL_LINE_STRIP,
280 TRIANGLE_STRIP_OUTPUT = GL_TRIANGLE_STRIP
281 };
282
290
298
306
314
333
341
342
354
362
369
376
381
385 inline SoFragmentShader* getFragmentShader(int pos) const;
386
390 inline SoVertexShader* getVertexShader(int pos) const;
391
396 inline SoGeometryShader* getGeometryShader(int pos) const;
397
402 virtual SoFragmentShader* setFragmentShader(int pos, const SbString& filenameOrSource,
404
409 virtual SoVertexShader* setVertexShader(int pos, const SbString& filenameOrSource,
411
417 virtual SoGeometryShader* setGeometryShader(int pos, const SbString& filenameOrSource,
419
424 virtual SoComputeShader* setComputeShader(int pos, const SbString& filenameOrSource,
426
431 static unsigned int getNumReservedTextures();
432
437
442
447 virtual SoTessellationControlShader* setTessellationControlShader(int pos, const SbString& filenameOrSource,
449
456
457 /*----------------------------------------------------------------------------*/
458
464
465private:
466
467 // GLRender
468 virtual void doAction(SoAction *action);
469 virtual void GLRender( SoGLRenderAction *action );
470 virtual void getBoundingBox( SoGetBoundingBoxAction *action );
471 virtual void pick( SoPickAction *action );
472
473private:
475 enum GPUVendorType
476 {
477 GPU_TYPE_NOT_INITIALIZED = -1,
478 GPU_NVIDIA = 0,
479 GPU_ATI = 1,
480 GPU_INTEL = 2,
481 GPU_FIREPRO_MAC = 3,
482 GPU_MESA = 4
483 };
484
485 // Constant, max number of texture image properties that can be set
486 enum { MAX_IMG_UNITS = 8 };
487
489 static const char* getMainSlotName(SbEnums::ShaderType shaderType);
490
492 bool hasPrivateMainShaderType(SbEnums::ShaderType shaderType) const;
493
495 bool hasPublicMainShaderType(SbEnums::ShaderType shaderType) const;
496
498 bool hasMainShaderType(SbEnums::ShaderType shaderType) const;
499
501 bool hasFixedPipelineMainShaderType(SbEnums::ShaderType shaderType) const;
502
504 bool hasFixedPipelineMain() const;
505
507 void getAllParameters(SoState* state, std::vector<SoUniformShaderParameter*>& shaderParameters);
508
510 void getShaderObjects(std::vector<SoShaderObject*>& shaderObjectsList) const;
511
513 SbString getLinkedShaderFileNames(SbEnums::ShaderType shaderType) const;
514
516 void displayLinkedFileNames() const;
517
518 //Return true if its an internal Oiv/Vviz name
519 static bool isOivReservedName(const std::string &paramName);
520
522 virtual void notify(SoNotList *list);
523
525 void setDefine(const char* name, const char* value);
526 void setDefine(const char* name, const SbString& value)
527 { setDefine(name,value.toLatin1()); }
528
532 bool getDefine(const char* name, SbString& value) const;
533
535 void removeDefine(const char* name);
536
538 void setHeader(const SbString& filename);
539
541 void removeHeader(const SbString& name);
542
546 static GLint offsetToGLTextureUnit(SoGLRenderAction* action, const char* offset);
547
551 static int offsetToTextureUnit(SoGLRenderAction* action, const char* offset);
552
553 // Initializes the classes.
554 static void initClass();
555 static void exitClass();
556
558 void invalidate();
559
561 bool isGlslProgram() const;
562
564 bool isComputeShader() const;
565
567 bool isShadowShader() const;
568
570 SoShaderProgram* getShadowPassShader() const;
571
573 void setShadowShader(bool flag);
574
577 void setHiddenShaderObject(const char* objName, SoShaderObject* obj);
578
580 void removeHiddenShaderObject(const char* objName);
581
583 SoShaderObject* getHiddenShaderObject(const char* objName) const;
584
586 const SbConstCharMap<SoShaderObject*>& getHiddenShaderObjects() const;
587
589 static GPUVendorType getGPUVendorType();
590
591
595 template<typename T>
596 T* setupPrivateShaderStage(const char* hiddenName, const SbString& shaderSource, SoShaderObject::SourceType sourceType = SoShaderObject::FILENAME)
597 {
598 // check if it is already allocated
599 T* fp = (T*)getHiddenShaderObject(hiddenName);
600 if (!fp || (fp->sourceProgram.getValue() != shaderSource))
601 {
602 fp = new T;
603 fp->sourceProgram.setValue(shaderSource);
604 fp->sourceType = sourceType;
605 setHiddenShaderObject(hiddenName, fp);
606 }
607 return fp;
608 }
609
611 void removeAllPrivateShaderType(SbEnums::ShaderType shaderType);
612
614 enum ShaderLibraryState
615 {
616 LIBRARY_NOT_INSTALLED,
617 LIBRARY_INSTALLED_EMPTY,
618 LIBRARY_INSTALLED,
619 };
620
622 ShaderLibraryState getLibraryState( inventor::helper::ShaderLibrary* library ) const;
623
625 void setLibraryState( inventor::helper::ShaderLibrary* library, ShaderLibraryState libraryState );
626
627 bool getPreviousNeedDepthPeelingLibrary() const
628 {
629 return m_previousNeedDepthPeelingLibrary;
630 }
631
633 void setIsFixedPipeline(bool isFixedPipelineShader)
634 {
635 m_isFixedPipelineShader = isFixedPipelineShader;
636 }
637
639 bool isFixedPipeline() const
640 {
641 return m_isFixedPipelineShader;
642 }
643
653 SoSFBool multiSampling;
654
655private:
656 typedef std::vector<SoShaderObject*> ShaderObjectVector;
657 typedef SbConstCharMap<SoShaderObject*> ShaderObjectMap;
659 typedef SoShaderObject::DefineMap DefineMap;
660 typedef SoShaderObject::HeaderSet HeaderSet;
661
663 struct Members
664 {
665 Members();
666 ~Members();
667 void unref();
668
670 bool m_isShadowShader;
671
673 ShaderObjectMap m_hiddenShaderObjects;
674
675 // Contains the list of vertex and fragment shaders,
676 // before modification.
677 // Enable to track add/remove/change or shader objects.
678 std::vector<SoShaderObject*> m_prevShaderObject;
679 std::vector< SoNode * > m_toUnrefShaderObjects;
680
682 SoShaderProgram* m_shadowPassShader;
683
685 DefineMap m_defineMap;
686
688 HeaderSet m_headerMap;
689
691 bool m_isValid;
692 };
693 Members m_members;
694
695 // Destructor
696 virtual ~SoShaderProgram();
697
698 // Check if all shader objects are valid
699 SbBool isValidShaderObjects() const;
700
701 // Check if at least one of the shader objects has
702 // its "isActive" field set to true
703 SbBool isOneShaderObjectActive();
704
706 static SoShaderProgram::Members* getMembers(const SoShaderProgram* prog);
707
708private:
709 // Required for compat with some old IV files. (cf ebug#3765)
710 SoMFNode prevShaderObject;
711
713 void addDefines(SoShaderObject* obj);
714
716 void addHeaders(SoShaderObject* obj);
717
719 void addHiddenShaderObjects(ShaderObjectVector& shaderObjectsList);
720
722 static bool lessVersion(SoShaderObject* obj1, SoShaderObject* obj2);
723
725 static SbString getMaxVersion(const ShaderObjectVector& objList);
726
728 static bool lessProfile(SoShaderObject* obj1, SoShaderObject* obj2);
729
732 static SbString getLoosestProfile(const ShaderObjectVector& objList);
733
735 bool hasShaderObjectsChanged() const;
736
738 void updatePrevShaderObject();
739
741 static void invalidate(ShaderObjectMap::value_type& p);
742
744 void addGPUVendorDefine();
745
747 void setShaderBufferObject(SoGLRenderAction* action);
748
750 void setShaderTextureImages(SoGLRenderAction* action) const;
751
753private:
758 void notifyAddedShaderObject(const char* hiddenShaderObjectName);
759
761 static void fileSensorCB(void *data, SoSensor *) ;
762
764 void updateFileSensor();
765
767 std::unique_ptr<SoFileSensor> m_fileSensor;
768
770 bool m_previousNeedDepthPeelingLibrary;
771
772 typedef std::unordered_map<inventor::helper::ShaderLibrary*, ShaderLibraryState> ShaderLibraryStateMap;
773 ShaderLibraryStateMap m_shaderLibrariesStates;
774
776 static GPUVendorType s_gpuVendorType;
777
779 static int s_firstUsedTextureUnit;
780
782 static bool s_debugCache;
783
784
785 bool m_isFixedPipelineShader;
786
788};
789/*----------------------------------------------------------------------------*/
790
791/*******************************************************************************/
794{
795 assert(pos >= 0 && pos < shaderObject.getNum());
796 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
797
798 if ( !obj )
799 return NULL;
800
802
803 return static_cast<SoFragmentShader*>(obj);
804}
805
806/*******************************************************************************/
809{
810 assert(pos >= 0 && pos < shaderObject.getNum());
811 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
812
813 if ( !obj )
814 return NULL;
815
816 assert(obj->getTypeId() == SoVertexShader::getClassTypeId());
817
818 return static_cast<SoVertexShader*>(obj);
819}
820
821/*******************************************************************************/
824{
825 assert(pos >= 0 && pos < shaderObject.getNum());
826 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
827
828 if ( !obj )
829 return NULL;
830
832
833 return static_cast<SoGeometryShader*>(obj);
834}
835
836/*******************************************************************************/
839{
840 assert(pos >= 0 && pos < shaderObject.getNum());
841 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
842
843 if ( !obj )
844 return NULL;
845
847
848 return static_cast<SoTessellationControlShader*>(obj);
849}
850
851/*******************************************************************************/
854{
855 assert(pos >= 0 && pos < shaderObject.getNum());
856 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
857
858 if ( !obj )
859 return NULL;
860
862
863 return static_cast<SoTessellationEvaluationShader*>(obj);
864}
865
866#ifdef _WIN32
867#pragma warning(pop)
868#endif
869
870#endif /*_SO_SHADER_PROGRAM_H_*/
871
872
Class for smart character strings.
Definition SbString.h:202
Abstract base class for all actions.
Definition SoAction.h:132
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Node that define...
Sensor class that can be attached to Open Inventor fields.
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Node that define...
static SoType getClassTypeId()
Returns the type identifier for this class.
Renders a scene graph using Open Inventor's Render Engine.
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Node that define...
static SoType getClassTypeId()
Returns the type identifier for this class.
Computes bounding box of a scene.
Abstract base class for all light source nodes.
Definition SoLight.h:124
Multiple-value field containing any number of nodes.
Definition SoMFNode.h:94
int getNum() const
Returns the number of values currently in the field.
Definition SoMField.h:146
Abstract base class for all database nodes.
Definition SoNode.h:145
Abstract base class for picking objects in a scene.
Field containing a single Boolean value.
Definition SoSFBool.h:79
Field containing an enumerated value.
Definition SoSFEnum.h:89
Field containing a int32_t integer.
Definition SoSFInt32.h:80
void setValue(const SbString &newValue)
Sets this field to newValue.
Abstract base class for Open Inventor sensors.
Definition SoSensor.h:100
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Abstract node cl...
SoSFFilePathString sourceProgram
Contains the shader object's source program, specified by a filename (sourceType set to FILENAME) or ...
virtual SoType getTypeId() const
Returns the type identifier for this specific instance.
SourceType
Shader Object source type possible values.
@ FILENAME
Only the name of the file containing the source is given (default).
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Shader parameter...
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Shader parameter...
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Shader program p...
virtual SoFragmentShader * setFragmentShader(int pos, const SbString &filenameOrSource, SoShaderObject::SourceType sourceType=SoShaderObject::FILENAME)
Convenience method to create a fragment shader with the specified filename and add it at the specifie...
SoSFBool vertexProgramTwoSide
If set to TRUE, vertex shaders will operate in two-sided color mode.
GeometryInputType
Geometry input type.
@ POINTS_INPUT
The input geometry should be interpreted as points.
@ LINES_INPUT
The input geometry should be interpreted as lines.
@ TRIANGLES_INPUT
The input geometry should be interpreted as triangles.
SoTessellationEvaluationShader * getTessellationEvaluationShader(int pos) const
Returns the tessellation evaluation shader at the specified position.
SoSFEnum geometryInputType
Specifies the input primitive type of the current geometry shader if any (not used otherwise).
SoShaderParameterImage * addShaderParameterImage(const SbString &name, SoTexture *tex)
Convenience method to create an SoShaderParameterImage with the specified name and value and add it t...
SoGeometryShader * getGeometryShader(int pos) const
Returns the geometry shader at the specified position.
virtual SoComputeShader * setComputeShader(int pos, const SbString &filenameOrSource, SoShaderObject::SourceType sourceType=SoShaderObject::FILENAME)
Convenience method to create a compute shader with the specified filename and add it at the specified...
SoMFNode images
Specifies a list of SoShaderParameterImage nodes to use with this shader.
SoSFBool shadowShader
Only used when an SoShadowGroup is active.
GeometryOutputType
Geometry ouput type.
@ TRIANGLE_STRIP_OUTPUT
Default.
SoSFEnum geometryOutputType
Specifies the output primitive type of the current geometry shader if any (not used otherwise).
SoSFBool generateTransparency
If set to TRUE, then shapes affected by this shader will be considered transparent.
SoFragmentShader * getFragmentShader(int pos) const
Returns the fragment shader at the specified position.
static unsigned int getNumReservedTextures()
Returns the number of reserved texture units.
SoVertexShader * getVertexShader(int pos) const
Returns the vertex shader at the specified position.
SoShaderProgram()
Constructor.
SoTessellationControlShader * getTessellationControlShader(int pos) const
Returns the tessellation control shader at the specified position.
virtual SoVertexShader * setVertexShader(int pos, const SbString &filenameOrSource, SoShaderObject::SourceType sourceType=SoShaderObject::FILENAME)
Convenience method to create a vertex shader with the specified filename and add it at the specified ...
SoMFNode shaderObject
Specifies the list of shader objects (i.e., vertex shaders, geometry and fragment shaders) which form...
virtual SoTessellationControlShader * setTessellationControlShader(int pos, const SbString &filenameOrSource, SoShaderObject::SourceType sourceType=SoShaderObject::FILENAME)
Convenience method to create a tessellation control shader with the specified filename and add it at ...
virtual SoTessellationEvaluationShader * setTessellationEvaluationShader(int pos, const SbString &filenameOrSource, SoShaderObject::SourceType sourceType=SoShaderObject::FILENAME)
Convenience method to create a tessellation evaluation shader with the specified filename and add it ...
SoSFInt32 maxGeometryOutputVertices
Set the maximum number of vertices the geometry shader will emit in one invocation.
SoMFNode bufferObjects
Specifies a list of SoShaderParameterBufferObject to use with this shader.
virtual SoGeometryShader * setGeometryShader(int pos, const SbString &filenameOrSource, SoShaderObject::SourceType sourceType=SoShaderObject::FILENAME)
Convenience method to create a geometry shader with the specified filename and add it at the specifie...
SoSFInt32 patchLength
Set the length of the fixed-size collection of vertices used by tessellation shaders.
Traversal state.
Definition SoState.h:74
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Node that define...
static SoType getClassTypeId()
Returns the type identifier for this class.
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Node that define...
static SoType getClassTypeId()
Returns the type identifier for this class.
Abstract base class for texture mapping nodes.
Definition SoTexture.h:156
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Abstract base cl...
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Node that define...
static SoType getClassTypeId()
Returns the type identifier for this class.
int SbBool
Boolean type.
Definition SbBase.h:87
int GLint
Definition SoGLType.h:33