Open Inventor Release 2026.1.0
 
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-2025 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; } }
68
69/*----------------------------------------------------------------------------*/
70
231class SoShaderProgram : public SoNode
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(SoState* state) 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 enum NotificationsPolicy
526 {
527 DISABLE_NOTIFICATIONS,
528 ENABLE_NOTIFICATIONS,
529 };
530
532 void setDefine(const char* name, const char* value, NotificationsPolicy notifPolicy = DISABLE_NOTIFICATIONS);
533
534 void setDefine(const char* name, const SbString& value, NotificationsPolicy notifPolicy = DISABLE_NOTIFICATIONS)
535 {
536 setDefine(name, value.toLatin1(), notifPolicy);
537 }
538
542 bool getDefine(const char* name, SbString& value) const;
543
545 void removeDefine(const char* name);
546
548 void setHeader(const SbString& filename);
549
551 void removeHeader(const SbString& name);
552
556 static GLint offsetToGLTextureUnit(SoGLRenderAction* action, const char* offset);
557
561 static int offsetToTextureUnit(SoGLRenderAction* action, const char* offset);
562
563 // Initializes the classes.
564 static void initClass();
565 static void exitClass();
566
568 void invalidate();
569
571 bool isGlslProgram() const;
572
574 bool isRasterShader() const;
575
577 bool isShadowShader() const;
578
580 SoShaderProgram* getShadowPassShader() const;
581
583 void setShadowShader(bool flag);
584
587 void setHiddenShaderObject(const char* objName, SoShaderObject* obj);
588
590 void removeHiddenShaderObject(const char* objName);
591
593 SoShaderObject* getHiddenShaderObject(const char* objName) const;
594
596 const SbConstCharMap<SoShaderObject*>& getHiddenShaderObjects() const;
597
599 static GPUVendorType getGPUVendorType();
600
601
605 template<typename T>
606 T* setupPrivateShaderStage(const char* hiddenName, const SbString& shaderSource, SoShaderObject::SourceType sourceType = SoShaderObject::FILENAME)
607 {
608 // check if it is already allocated
609 T* fp = (T*)getHiddenShaderObject(hiddenName);
610 if (!fp || (fp->sourceProgram.getValue() != shaderSource))
611 {
612 fp = new T;
613 fp->sourceProgram.setValue(shaderSource);
614 fp->sourceType = sourceType;
615 setHiddenShaderObject(hiddenName, fp);
616 }
617 return fp;
618 }
619
621 void removeAllPrivateShaderType(SbEnums::ShaderType shaderType);
622
624 enum ShaderLibraryState
625 {
626 LIBRARY_NOT_INSTALLED,
627 LIBRARY_INSTALLED_EMPTY,
628 LIBRARY_INSTALLED_RASTER,
629 LIBRARY_INSTALLED_RAY_TRACING,
630 };
631
633 ShaderLibraryState getLibraryState( inventor::helper::ShaderLibrary* library ) const;
634
636 void setLibraryState( inventor::helper::ShaderLibrary* library, ShaderLibraryState libraryState );
637
638 bool getPreviousNeedDepthPeelingLibrary() const
639 {
640 return m_previousNeedDepthPeelingLibrary;
641 }
642
644 void setIsFixedPipeline(bool isFixedPipelineShader)
645 {
646 m_isFixedPipelineShader = isFixedPipelineShader;
647 }
648
650 bool isFixedPipeline() const
651 {
652 return m_isFixedPipelineShader;
653 }
654
656 void setVertexProgramTwoSide(SoState* state, bool allowVertexTwoSideLighting = true);
657
667 SoSFBool multiSampling;
668
669private:
670 typedef std::vector<SoShaderObject*> ShaderObjectVector;
671 typedef SbConstCharMap<SoShaderObject*> ShaderObjectMap;
673 typedef SoShaderObject::DefineMap DefineMap;
674 typedef SoShaderObject::HeaderSet HeaderSet;
675
677 struct Members
678 {
679 Members();
680 ~Members();
681 void unref();
682
684 bool m_isShadowShader;
685
687 ShaderObjectMap m_hiddenShaderObjects;
688
689 // Contains the list of vertex and fragment shaders,
690 // before modification.
691 // Enable to track add/remove/change or shader objects.
692 std::vector<SoShaderObject*> m_prevShaderObject;
693 std::vector< SoNode * > m_toUnrefShaderObjects;
694
696 SoShaderProgram* m_shadowPassShader;
697
699 DefineMap m_defineMap;
700
702 HeaderSet m_headerMap;
703
705 bool m_isValid;
706 };
707 Members m_members;
708
709 // Destructor
710 virtual ~SoShaderProgram();
711
712 // Check if all shader objects are valid
713 SbBool isValidShaderObjects() const;
714
715 // Check if at least one of the shader objects has
716 // its "isActive" field set to true
717 SbBool isOneShaderObjectActive();
718
720 static SoShaderProgram::Members* getMembers(const SoShaderProgram* prog);
721
722private:
723 // Required for compat with some old IV files. (cf ebug#3765)
724 SoMFNode prevShaderObject;
725
727 void addDefines(SoShaderObject* obj);
728
730 void addHeaders(SoShaderObject* obj);
731
733 void addHiddenShaderObjects(ShaderObjectVector& shaderObjectsList);
734
736 static bool lessVersion(SoShaderObject* obj1, SoShaderObject* obj2);
737
739 static SbString getMaxVersion(const ShaderObjectVector& objList);
740
742 static bool lessProfile(SoShaderObject* obj1, SoShaderObject* obj2);
743
746 static SbString getLoosestProfile(const ShaderObjectVector& objList);
747
749 bool hasShaderObjectsChanged() const;
750
752 void updatePrevShaderObject();
753
755 static void invalidate(ShaderObjectMap::value_type& p);
756
758 void addGPUVendorDefine();
759
761 void setShaderBufferObject(SoGLRenderAction* action);
762
764 void setShaderTextureImages(SoGLRenderAction* action) const;
765
770 void notifyAddedShaderObject(const char* hiddenShaderObjectName);
771
773 static void fileSensorCB(void *data, SoSensor *) ;
774
776 void updateFileSensor();
777
779 std::unique_ptr<SoFileSensor> m_fileSensor;
780
782 bool m_previousNeedDepthPeelingLibrary;
783
784 typedef std::unordered_map<inventor::helper::ShaderLibrary*, ShaderLibraryState> ShaderLibraryStateMap;
785 ShaderLibraryStateMap m_shaderLibrariesStates;
786
788 static GPUVendorType s_gpuVendorType;
789
791 static int s_firstUsedTextureUnit;
792
794 static bool s_debugCache;
795
796
797 bool m_isFixedPipelineShader;
798
800};
801/*----------------------------------------------------------------------------*/
802
803/*******************************************************************************/
806{
807 assert(pos >= 0 && pos < shaderObject.getNum());
808 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
809
810 if ( !obj )
811 return NULL;
812
814
815 return static_cast<SoFragmentShader*>(obj);
816}
817
818/*******************************************************************************/
821{
822 assert(pos >= 0 && pos < shaderObject.getNum());
823 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
824
825 if ( !obj )
826 return NULL;
827
828 assert(obj->getTypeId() == SoVertexShader::getClassTypeId());
829
830 return static_cast<SoVertexShader*>(obj);
831}
832
833/*******************************************************************************/
836{
837 assert(pos >= 0 && pos < shaderObject.getNum());
838 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
839
840 if ( !obj )
841 return NULL;
842
844
845 return static_cast<SoGeometryShader*>(obj);
846}
847
848/*******************************************************************************/
851{
852 assert(pos >= 0 && pos < shaderObject.getNum());
853 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
854
855 if ( !obj )
856 return NULL;
857
859
860 return static_cast<SoTessellationControlShader*>(obj);
861}
862
863/*******************************************************************************/
866{
867 assert(pos >= 0 && pos < shaderObject.getNum());
868 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
869
870 if ( !obj )
871 return NULL;
872
874
875 return static_cast<SoTessellationEvaluationShader*>(obj);
876}
877
878#ifdef _WIN32
879#pragma warning(pop)
880#endif
881
882#endif /*_SO_SHADER_PROGRAM_H_*/
883
884
Class for smart character strings.
Definition SbString.h:202
<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:129
Multiple-value field containing any number of nodes.
Definition SoMFNode.h:94
friend class SoAction
Definition SoNode.h:472
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
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Abstract node cl...
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...
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).
friend class SoUniformShaderParameter
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.
<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> 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