Open Inventor Release 2023.2.3
 
Loading...
Searching...
No Matches
SoShaderProgram.h
Go to the documentation of this file.
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_PROGRAM_H_
26#define _SO_SHADER_PROGRAM_H_
27
28#include <Inventor/SbBasic.h>
40#include <Inventor/STL/vector>
41#include <Inventor/STL/cassert>
44#include <memory>
45#include <unordered_map>
46#include <Inventor/renderer/RendererResourceMacro.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
235 RENDERER_RESOURCE(SoShaderProgram);
236
237public:
269
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
644private:
645 typedef std::vector<SoShaderObject*> ShaderObjectVector;
646 typedef SbConstCharMap<SoShaderObject*> ShaderObjectMap;
648 typedef SoShaderObject::DefineMap DefineMap;
649 typedef SoShaderObject::HeaderSet HeaderSet;
650
652 struct Members
653 {
654 Members();
655 ~Members();
656 void unref();
657
659 bool m_isShadowShader;
660
662 ShaderObjectMap m_hiddenShaderObjects;
663
664 // Contains the list of vertex and fragment shaders,
665 // before modification.
666 // Enable to track add/remove/change or shader objects.
667 std::vector<SoShaderObject*> m_prevShaderObject;
668 std::vector< SoNode * > m_toUnrefShaderObjects;
669
671 SoShaderProgram* m_shadowPassShader;
672
674 DefineMap m_defineMap;
675
677 HeaderSet m_headerMap;
678
680 bool m_isValid;
681 };
682 Members m_members;
683
684 // Destructor
685 virtual ~SoShaderProgram();
686
687 // Check if all shader objects are valid
688 SbBool isValidShaderObjects() const;
689
690 // Check if at least one of the shader objects has
691 // its "isActive" field set to true
692 SbBool isOneShaderObjectActive();
693
695 static SoShaderProgram::Members* getMembers(const SoShaderProgram* prog);
696
697private:
698 // Required for compat with some old IV files. (cf ebug#3765)
699 SoMFNode prevShaderObject;
700
702 void addDefines(SoShaderObject* obj);
703
705 void addHeaders(SoShaderObject* obj);
706
708 void addHiddenShaderObjects(ShaderObjectVector& shaderObjectsList);
709
711 static bool lessVersion(SoShaderObject* obj1, SoShaderObject* obj2);
712
714 static SbString getMaxVersion(const ShaderObjectVector& objList);
715
717 static bool lessProfile(SoShaderObject* obj1, SoShaderObject* obj2);
718
721 static SbString getLoosestProfile(const ShaderObjectVector& objList);
722
724 bool hasShaderObjectsChanged() const;
725
727 void updatePrevShaderObject();
728
730 static void invalidate(ShaderObjectMap::value_type& p);
731
733 void addGPUVendorDefine();
734
736 void setShaderBufferObject(SoGLRenderAction* action);
737
739 void setShaderTextureImages(SoGLRenderAction* action) const;
740
742private:
747 void notifyAddedShaderObject(const char* hiddenShaderObjectName);
748
750 static void fileSensorCB(void *data, SoSensor *) ;
751
753 void updateFileSensor();
754
756 std::unique_ptr<SoFileSensor> m_fileSensor;
757
759 bool m_previousNeedDepthPeelingLibrary;
760
761 typedef std::unordered_map<inventor::helper::ShaderLibrary*, ShaderLibraryState> ShaderLibraryStateMap;
762 ShaderLibraryStateMap m_shaderLibrariesStates;
763
765 static GPUVendorType s_gpuVendorType;
766
768 static int s_firstUsedTextureUnit;
769
771 static bool s_debugCache;
772
773
774 bool m_isFixedPipelineShader;
775
777};
778/*----------------------------------------------------------------------------*/
779
780/*******************************************************************************/
783{
784 assert(pos >= 0 && pos < shaderObject.getNum());
785 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
786
787 if ( !obj )
788 return NULL;
789
791
792 return static_cast<SoFragmentShader*>(obj);
793}
794
795/*******************************************************************************/
798{
799 assert(pos >= 0 && pos < shaderObject.getNum());
800 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
801
802 if ( !obj )
803 return NULL;
804
805 assert(obj->getTypeId() == SoVertexShader::getClassTypeId());
806
807 return static_cast<SoVertexShader*>(obj);
808}
809
810/*******************************************************************************/
813{
814 assert(pos >= 0 && pos < shaderObject.getNum());
815 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
816
817 if ( !obj )
818 return NULL;
819
821
822 return static_cast<SoGeometryShader*>(obj);
823}
824
825/*******************************************************************************/
828{
829 assert(pos >= 0 && pos < shaderObject.getNum());
830 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
831
832 if ( !obj )
833 return NULL;
834
836
837 return static_cast<SoTessellationControlShader*>(obj);
838}
839
840/*******************************************************************************/
843{
844 assert(pos >= 0 && pos < shaderObject.getNum());
845 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
846
847 if ( !obj )
848 return NULL;
849
851
852 return static_cast<SoTessellationEvaluationShader*>(obj);
853}
854
855#ifdef _WIN32
856#pragma warning(pop)
857#endif
858
859#endif /*_SO_SHADER_PROGRAM_H_*/
860
861
#define GL_LINE_STRIP
Definition SoGLType.h:66
#define GL_LINES
Definition SoGLType.h:64
int GLint
Definition SoGLType.h:29
#define GL_TRIANGLES
Definition SoGLType.h:67
#define GL_POINTS
Definition SoGLType.h:63
#define GL_TRIANGLE_STRIP
Definition SoGLType.h:68
#define SO_NODE_HEADER(className)
Definition SoSubNode.h:151
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:97
<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:155
<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