Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:35

0001 // Created on: 2013-09-19
0002 // Created by: Denis BOGOLEPOV
0003 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef OpenGl_ShaderProgram_HeaderFile
0017 #define OpenGl_ShaderProgram_HeaderFile
0018 
0019 #include <NCollection_DataMap.hxx>
0020 #include <NCollection_Sequence.hxx>
0021 #include <TCollection_AsciiString.hxx>
0022 
0023 #include <Graphic3d_ShaderProgram.hxx>
0024 #include <Graphic3d_TextureSetBits.hxx>
0025 
0026 #include <OpenGl_Vec.hxx>
0027 #include <OpenGl_NamedResource.hxx>
0028 #include <OpenGl_ShaderObject.hxx>
0029 
0030 class OpenGl_ShaderProgram;
0031 DEFINE_STANDARD_HANDLE(OpenGl_ShaderProgram, OpenGl_NamedResource)
0032 
0033 //! The enumeration of OCCT-specific OpenGL/GLSL variables.
0034 enum OpenGl_StateVariable
0035 {
0036   // OpenGL matrix state
0037   OpenGl_OCC_MODEL_WORLD_MATRIX,
0038   OpenGl_OCC_WORLD_VIEW_MATRIX,
0039   OpenGl_OCC_PROJECTION_MATRIX,
0040   OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE,
0041   OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE,
0042   OpenGl_OCC_PROJECTION_MATRIX_INVERSE,
0043   OpenGl_OCC_MODEL_WORLD_MATRIX_TRANSPOSE,
0044   OpenGl_OCC_WORLD_VIEW_MATRIX_TRANSPOSE,
0045   OpenGl_OCC_PROJECTION_MATRIX_TRANSPOSE,
0046   OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE_TRANSPOSE,
0047   OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE_TRANSPOSE,
0048   OpenGl_OCC_PROJECTION_MATRIX_INVERSE_TRANSPOSE,
0049 
0050   // OpenGL clip planes state
0051   OpenGl_OCC_CLIP_PLANE_EQUATIONS,
0052   OpenGl_OCC_CLIP_PLANE_CHAINS,
0053   OpenGl_OCC_CLIP_PLANE_COUNT,
0054 
0055   // OpenGL light state
0056   OpenGl_OCC_LIGHT_SOURCE_COUNT,
0057   OpenGl_OCC_LIGHT_SOURCE_TYPES,
0058   OpenGl_OCC_LIGHT_SOURCE_PARAMS,
0059   OpenGl_OCC_LIGHT_AMBIENT,
0060   OpenGl_OCC_LIGHT_SHADOWMAP_SIZE_BIAS,// occShadowMapSizeBias
0061   OpenGl_OCC_LIGHT_SHADOWMAP_SAMPLERS, // occShadowMapSamplers
0062   OpenGl_OCC_LIGHT_SHADOWMAP_MATRICES, // occShadowMapMatrices
0063 
0064   // Material state
0065   OpenGl_OCCT_TEXTURE_ENABLE,
0066   OpenGl_OCCT_DISTINGUISH_MODE,
0067   OpenGl_OCCT_PBR_MATERIAL,
0068   OpenGl_OCCT_COMMON_MATERIAL,
0069   OpenGl_OCCT_ALPHA_CUTOFF,
0070   OpenGl_OCCT_COLOR,
0071 
0072   // Weighted, Blended Order-Independent Transparency rendering state
0073   OpenGl_OCCT_OIT_OUTPUT,
0074   OpenGl_OCCT_OIT_DEPTH_FACTOR,
0075 
0076   // Context-dependent state
0077   OpenGl_OCCT_TEXTURE_TRSF2D,
0078   OpenGl_OCCT_POINT_SIZE,
0079 
0080   // Wireframe state
0081   OpenGl_OCCT_VIEWPORT,
0082   OpenGl_OCCT_LINE_WIDTH,
0083   OpenGl_OCCT_LINE_FEATHER,
0084   OpenGl_OCCT_LINE_STIPPLE_PATTERN, // occStipplePattern
0085   OpenGl_OCCT_LINE_STIPPLE_FACTOR,  // occStippleFactor
0086   OpenGl_OCCT_WIREFRAME_COLOR,
0087   OpenGl_OCCT_QUAD_MODE_STATE,
0088 
0089   // Parameters of outline (silhouette) shader
0090   OpenGl_OCCT_ORTHO_SCALE,
0091   OpenGl_OCCT_SILHOUETTE_THICKNESS,
0092 
0093   // PBR state
0094   OpenGl_OCCT_NB_SPEC_IBL_LEVELS,
0095 
0096   // DON'T MODIFY THIS ITEM (insert new items before it)
0097   OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
0098 };
0099 
0100 //! Interface for generic setter of user-defined uniform variables.
0101 struct OpenGl_SetterInterface
0102 {
0103   //! Sets user-defined uniform variable to specified program.
0104   virtual void Set (const Handle(OpenGl_Context)&           theCtx,
0105                     const Handle(Graphic3d_ShaderVariable)& theVariable,
0106                     OpenGl_ShaderProgram*                   theProgram) = 0;
0107 
0108   //! Destructor
0109   virtual ~OpenGl_SetterInterface() {}
0110 };
0111 
0112 //! List of OpenGL shader objects.
0113 typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)>    OpenGl_ShaderList;
0114 
0115 //! List of shader variable setters.
0116 typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList;
0117 
0118 //! Support tool for setting user-defined uniform variables.
0119 class OpenGl_VariableSetterSelector
0120 {
0121 public:
0122 
0123   //! Creates new setter selector.
0124   OpenGl_VariableSetterSelector();
0125 
0126   //! Releases memory resources of setter selector.
0127   ~OpenGl_VariableSetterSelector();
0128 
0129   //! Sets user-defined uniform variable to specified program.
0130   void Set (const Handle(OpenGl_Context)&           theCtx,
0131             const Handle(Graphic3d_ShaderVariable)& theVariable,
0132             OpenGl_ShaderProgram*                   theProgram) const;
0133 
0134 private:
0135 
0136   //! List of variable setters.
0137   OpenGl_SetterList mySetterList;
0138 };
0139 
0140 //! Defines types of uniform state variables.
0141 enum OpenGl_UniformStateType
0142 {
0143   OpenGl_LIGHT_SOURCES_STATE,
0144   OpenGl_CLIP_PLANES_STATE,
0145   OpenGl_MODEL_WORLD_STATE,
0146   OpenGl_WORLD_VIEW_STATE,
0147   OpenGl_PROJECTION_STATE,
0148   OpenGl_MATERIAL_STATE,
0149   OpenGl_SURF_DETAIL_STATE,
0150   OpenGL_OIT_STATE,
0151   OpenGl_UniformStateType_NB
0152 };
0153 
0154 //! Simple class represents GLSL program variable location.
0155 class OpenGl_ShaderUniformLocation
0156 {
0157 public:
0158   //! Invalid location of uniform/attribute variable.
0159   static const GLint INVALID_LOCATION = -1;
0160 public:
0161 
0162   //! Construct an invalid location.
0163   OpenGl_ShaderUniformLocation() : myLocation (INVALID_LOCATION) {}
0164 
0165   //! Constructor with initialization.
0166   explicit OpenGl_ShaderUniformLocation (GLint theLocation) : myLocation (theLocation) {}
0167 
0168   //! Note you may safely put invalid location in functions like glUniform* - the data passed in will be silently ignored.
0169   //! @return true if location is not equal to -1.
0170   bool IsValid() const { return myLocation != INVALID_LOCATION; }
0171 
0172   //! Return TRUE for non-invalid location.
0173   operator bool() const { return myLocation != INVALID_LOCATION; }
0174 
0175   //! Convert operators help silently put object to GL functions like glUniform*.
0176   operator GLint() const { return myLocation; }
0177 
0178 private:
0179   GLint myLocation;
0180 };
0181 
0182 //! Wrapper for OpenGL program object.
0183 class OpenGl_ShaderProgram : public OpenGl_NamedResource
0184 {
0185   friend class OpenGl_View;
0186   friend class OpenGl_ShaderManager;
0187   DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderProgram, OpenGl_NamedResource)
0188 public:
0189 
0190   //! Non-valid shader name.
0191   static const GLuint NO_PROGRAM = 0;
0192 
0193   //! Invalid location of uniform/attribute variable.
0194   static const GLint INVALID_LOCATION = -1;
0195 
0196   //! List of pre-defined OCCT state uniform variables.
0197   static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
0198 
0199   //! Wrapper for compiling shader object with verbose printing on error.
0200   Standard_EXPORT static bool compileShaderVerbose (const Handle(OpenGl_Context)& theCtx,
0201                                                     const Handle(OpenGl_ShaderObject)& theShader,
0202                                                     const TCollection_AsciiString& theSource,
0203                                                     bool theToPrintSource = true);
0204 
0205   //! Creates uninitialized shader program.
0206   //!
0207   //! WARNING! This constructor is not intended to be called anywhere but from OpenGl_ShaderManager::Create().
0208   //! Manager has been designed to synchronize camera position, lights definition and other aspects of the program implicitly,
0209   //! as well as sharing same program across rendering groups.
0210   //!
0211   //! Program created outside the manager will be left detached from these routines,
0212   //! and them should be performed manually by caller.
0213   //!
0214   //! This constructor has been made public to provide more flexibility to re-use OCCT OpenGL classes without OCCT Viewer itself.
0215   //! If this is not the case - create the program using shared OpenGl_ShaderManager instance instead.
0216   Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL,
0217                                         const TCollection_AsciiString& theId = "");
0218 
0219 protected:
0220 
0221   static OpenGl_VariableSetterSelector mySetterSelector;
0222 
0223 public:
0224 
0225   //! Releases resources of shader program.
0226   Standard_EXPORT virtual ~OpenGl_ShaderProgram();
0227 
0228   //! Creates new empty shader program of specified type.
0229   Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
0230 
0231   //! Destroys shader program.
0232   Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
0233 
0234   //! Returns estimated GPU memory usage - cannot be easily estimated.
0235   virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE { return 0; }
0236 
0237   //! Attaches shader object to the program object.
0238   Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)&      theCtx,
0239                                                  const Handle(OpenGl_ShaderObject)& theShader);
0240 
0241   //! Detaches shader object to the program object.
0242   Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)&      theCtx,
0243                                                  const Handle(OpenGl_ShaderObject)& theShader);
0244 
0245   //! Initializes program object with the list of shader objects.
0246   Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)&     theCtx,
0247                                                const Graphic3d_ShaderObjectList& theShaders);
0248 
0249   //! Links the program object.
0250   //! @param theCtx bound OpenGL context
0251   //! @param theIsVerbose flag to print log on error
0252   Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx,
0253                                          bool theIsVerbose = true);
0254 
0255   //! Fetches information log of the last link operation.
0256   Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
0257                                                  TCollection_AsciiString&      theLog);
0258 
0259   //! Fetches uniform variables from proxy shader program.
0260   Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
0261   
0262   //! @return proxy shader program.
0263   const Handle(Graphic3d_ShaderProgram)& Proxy() const { return myProxy; }
0264 
0265   //! @return true if current object was initialized
0266   inline bool IsValid() const
0267   {
0268     return myProgramID != NO_PROGRAM;
0269   }
0270 
0271   //! @return program ID
0272   inline GLuint ProgramId() const
0273   {
0274     return myProgramID;
0275   }
0276 
0277 public:
0278 
0279   //! Return TRUE if program defines tessellation stage.
0280   Standard_Boolean HasTessellationStage() const { return myHasTessShader; }
0281 
0282   //! Return the length of array of light sources (THE_MAX_LIGHTS),
0283   //! to be used for initialization occLightSources (OpenGl_OCC_LIGHT_SOURCE_PARAMS).
0284   Standard_Integer NbLightsMax() const { return myNbLightsMax; }
0285 
0286   //! Return the length of array of shadow maps (THE_NB_SHADOWMAPS); 0 by default.
0287   Standard_Integer NbShadowMaps() const { return myNbShadowMaps; }
0288 
0289   //! Return the length of array of clipping planes (THE_MAX_CLIP_PLANES),
0290   //! to be used for initialization occClipPlaneEquations (OpenGl_OCC_CLIP_PLANE_EQUATIONS) and occClipPlaneChains (OpenGl_OCC_CLIP_PLANE_CHAINS).
0291   Standard_Integer NbClipPlanesMax() const { return myNbClipPlanesMax; }
0292 
0293   //! Return the length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS),
0294   //! to be used for initialization occFragColorArray/occFragColorN.
0295   Standard_Integer NbFragmentOutputs() const { return myNbFragOutputs; }
0296 
0297   //! Return true if Fragment Shader should perform alpha test; FALSE by default.
0298   Standard_Boolean HasAlphaTest() const { return myHasAlphaTest; }
0299 
0300   //! Return if Fragment Shader color should output the OIT values; OFF by default.
0301   Graphic3d_RenderTransparentMethod OitOutput() const { return myOitOutput; }
0302 
0303   //! Return texture units declared within the program, @sa Graphic3d_TextureSetBits.
0304   Standard_Integer TextureSetBits() const { return myTextureSetBits; }
0305 
0306 private:
0307 
0308   //! Returns index of last modification of variables of specified state type.
0309   Standard_Size ActiveState (const OpenGl_UniformStateType theType) const
0310   {
0311     return theType < OpenGl_UniformStateType_NB
0312          ? myCurrentState[theType]
0313          : 0;
0314   }
0315 
0316   //! Updates index of last modification of variables of specified state type.
0317   void UpdateState (const OpenGl_UniformStateType theType,
0318                     const Standard_Size           theIndex)
0319   {
0320     if (theType < OpenGl_UniformStateType_NB)
0321     {
0322       myCurrentState[theType] = theIndex;
0323     }
0324   }
0325 
0326 public:
0327 
0328   //! Returns location of the specific uniform variable.
0329   Standard_EXPORT OpenGl_ShaderUniformLocation GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
0330                                                                    const GLchar*                 theName) const;
0331 
0332   //! Returns index of the generic vertex attribute by variable name.
0333   Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
0334                                               const GLchar*                 theName) const;
0335 
0336   //! Returns location of the OCCT state uniform variable.
0337   const OpenGl_ShaderUniformLocation& GetStateLocation (OpenGl_StateVariable theVariable) const { return myStateLocations[theVariable]; }
0338 
0339 public:
0340 
0341   //! Returns the value of the uniform variable from given name.
0342   template<typename ValueType>
0343   bool GetUniform (const Handle(OpenGl_Context)& theCtx,
0344                    const GLchar* theName,
0345                    ValueType& theValue) const
0346   {
0347     return GetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
0348   }
0349 
0350   //! Returns the value of the integer uniform variable.
0351   //! Wrapper for glGetUniformiv()
0352   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
0353                                                GLint                         theLocation,
0354                                                OpenGl_Vec4i&                 theValue) const;
0355 
0356   //! Returns the value of the float uniform variable.
0357   //! Wrapper for glGetUniformfv()
0358   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
0359                                                GLint                         theLocation,
0360                                                OpenGl_Vec4&                  theValue) const;
0361 
0362 public:
0363 
0364   //! Returns the vertex attribute from given name.
0365   template<typename ValueType>
0366   bool GetAttribute (const Handle(OpenGl_Context)& theCtx,
0367                      const GLchar* theName,
0368                      ValueType& theValue) const
0369   {
0370     return GetAttribute (theCtx, GetAttributeLocation (theCtx, theName), theValue);
0371   }
0372 
0373   //! Returns the integer vertex attribute.
0374   //! Wrapper for glGetVertexAttribiv()
0375   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
0376                                                  GLint                         theIndex,
0377                                                  OpenGl_Vec4i&                 theValue) const;
0378 
0379   //! Returns the float vertex attribute.
0380   //! Wrapper for glGetVertexAttribfv()
0381   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
0382                                                  GLint                         theIndex,
0383                                                  OpenGl_Vec4&                  theValue) const;
0384 
0385 public:
0386 
0387   //! Wrapper for glBindAttribLocation()
0388   Standard_EXPORT Standard_Boolean SetAttributeName (const Handle(OpenGl_Context)& theCtx,
0389                                                      GLint                         theIndex,
0390                                                      const GLchar*                 theName);
0391 
0392   //! Wrapper for glVertexAttrib*() for attribute with the given name.
0393   template<typename ValueType>
0394   bool SetAttribute (const Handle(OpenGl_Context)& theCtx,
0395                      const GLchar* theName,
0396                      const ValueType& theValue)
0397   {
0398     return SetAttribute (theCtx, GetAttributeLocation (theCtx, theName), theValue);
0399   }
0400 
0401   //! Wrapper for glVertexAttrib1f()
0402   Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
0403                                                  GLint                         theIndex,
0404                                                  GLfloat                       theValue);
0405 
0406   //! Wrapper for glVertexAttrib2fv()
0407   Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
0408                                                  GLint                         theIndex,
0409                                                  const OpenGl_Vec2&            theValue);
0410 
0411   //! Wrapper for glVertexAttrib3fv()
0412   Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
0413                                                  GLint                         theIndex,
0414                                                  const OpenGl_Vec3&            theValue);
0415 
0416   //! Wrapper for glVertexAttrib4fv()
0417   Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
0418                                                  GLint                         theIndex,
0419                                                  const OpenGl_Vec4&            theValue);
0420 
0421 public:
0422 
0423   //! Specifies the value of the uniform variable with given name.
0424   template<typename ValueType>
0425   bool SetUniform (const Handle(OpenGl_Context)& theCtx,
0426                    const GLchar* theName,
0427                    const ValueType& theValue)
0428   {
0429     return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
0430   }
0431 
0432   //! Specifies the value of the integer uniform variable.
0433   //! Wrapper for glUniform1i()
0434   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0435                                                GLint                         theLocation,
0436                                                GLint                         theValue);
0437 
0438   //! Specifies the value of the integer uniform 2D vector.
0439   //! Wrapper for glUniform2iv()
0440   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0441                                                GLint                         theLocation,
0442                                                const OpenGl_Vec2i&           theValue);
0443 
0444   //! Specifies the value of the integer uniform 3D vector.
0445   //! Wrapper for glUniform3iv()
0446   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0447                                                GLint                         theLocation,
0448                                                const OpenGl_Vec3i&           theValue);
0449 
0450   //! Specifies the value of the integer uniform 4D vector.
0451   //! Wrapper for glUniform4iv()
0452   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0453                                                GLint                         theLocation,
0454                                                const OpenGl_Vec4i&           theValue);
0455 
0456 public:
0457 
0458   //! Specifies the value of the unsigned integer uniform 2D vector (uvec2).
0459   //! Wrapper for glUniform2uiv()
0460   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0461                                                GLint                         theLocation,
0462                                                const OpenGl_Vec2u&           theValue);
0463 
0464   //! Specifies the value of the uvec2 uniform array
0465   //! Wrapper for glUniform2uiv()
0466   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0467                                                const GLchar*                 theName,
0468                                                const GLsizei                 theCount,
0469                                                const OpenGl_Vec2u*           theValue);
0470 
0471   //! Specifies the value of the uvec2 uniform array
0472   //! Wrapper for glUniform2uiv()
0473   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0474                                                GLint                         theLocation,
0475                                                const GLsizei                 theCount,
0476                                                const OpenGl_Vec2u*           theValue);
0477 
0478 public:
0479 
0480   //! Specifies the value of the float uniform variable.
0481   //! Wrapper for glUniform1f()
0482   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0483                                                GLint                         theLocation,
0484                                                GLfloat                       theValue);
0485 
0486   //! Specifies the value of the float uniform 2D vector.
0487   //! Wrapper for glUniform2fv()
0488   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0489                                                GLint                         theLocation,
0490                                                const OpenGl_Vec2&            theValue);
0491 
0492   //! Specifies the value of the float uniform 3D vector.
0493   //! Wrapper for glUniform3fv()
0494   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0495                                                GLint                         theLocation,
0496                                                const OpenGl_Vec3&            theValue);
0497 
0498   //! Specifies the value of the float uniform 4D vector.
0499   //! Wrapper for glUniform4fv()
0500   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0501                                                GLint                         theLocation,
0502                                                const OpenGl_Vec4&            theValue);
0503 
0504 public:
0505 
0506   //! Specifies the value of the array of float uniform 3x3 matrices.
0507   //! Wrapper over glUniformMatrix3fv().
0508   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)&  theCtx,
0509                                                GLint                          theLocation,
0510                                                GLuint                         theCount,
0511                                                const NCollection_Mat3<float>* theData);
0512 
0513   //! Specifies the value of the float uniform 4x4 matrix.
0514   //! Wrapper for glUniformMatrix4fv()
0515   bool SetUniform (const Handle(OpenGl_Context)& theCtx,
0516                    const GLchar*                 theName,
0517                    const OpenGl_Mat4&            theValue,
0518                    GLboolean                     theTranspose = GL_FALSE)
0519   {
0520     return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue, theTranspose);
0521   }
0522 
0523   //! Specifies the value of the float uniform 4x4 matrix.
0524   //! Wrapper for glUniformMatrix4fv()
0525   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0526                                                GLint                         theLocation,
0527                                                const OpenGl_Mat4&            theValue,
0528                                                GLboolean                     theTranspose = GL_FALSE);
0529 
0530   //! Specifies the value of the array of float uniform 4x4 matrices.
0531   //! Wrapper over glUniformMatrix4fv().
0532   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0533                                                GLint                         theLocation,
0534                                                GLuint                        theCount,
0535                                                const OpenGl_Mat4*            theData);
0536 
0537   //! Specifies the value of the float uniform array
0538   //! Wrapper over glUniform1fv()
0539   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0540                                                GLint                         theLocation,
0541                                                GLuint                        theCount,
0542                                                const Standard_ShortReal*     theData);
0543 
0544   //! Specifies the value of the float2 uniform array
0545   //! Wrapper over glUniform2fv()
0546   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0547                                                GLint                         theLocation,
0548                                                GLuint                        theCount,
0549                                                const OpenGl_Vec2*            theData);
0550 
0551   //! Specifies the value of the float3 uniform array
0552   //! Wrapper over glUniform3fv()
0553   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0554                                                GLint                         theLocation,
0555                                                GLuint                        theCount,
0556                                                const OpenGl_Vec3*            theData);
0557 
0558   //! Specifies the value of the float4 uniform array
0559   //! Wrapper over glUniform4fv()
0560   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0561                                                GLint                         theLocation,
0562                                                GLuint                        theCount,
0563                                                const OpenGl_Vec4*            theData);
0564 
0565   //! Specifies the value of the integer uniform array
0566   //! Wrapper over glUniform1iv()
0567   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0568                                                GLint                         theLocation,
0569                                                GLuint                        theCount,
0570                                                const Standard_Integer*       theData);
0571 
0572   //! Specifies the value of the int2 uniform array
0573   //! Wrapper over glUniform2iv()
0574   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0575                                                GLint                         theLocation,
0576                                                GLuint                        theCount,
0577                                                const OpenGl_Vec2i*           theData);
0578 
0579   //! Specifies the value of the int3 uniform array
0580   //! Wrapper over glUniform3iv()
0581   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0582                                                GLint                         theLocation,
0583                                                GLuint                        theCount,
0584                                                const OpenGl_Vec3i*           theData);
0585 
0586   //! Specifies the value of the int4 uniform array
0587   //! Wrapper over glUniform4iv()
0588   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0589                                                GLint                         theLocation,
0590                                                GLuint                        theCount,
0591                                                const OpenGl_Vec4i*           theData);
0592 
0593 public:
0594 
0595   //! Specifies the value of the sampler uniform variable.
0596   bool SetSampler (const Handle(OpenGl_Context)& theCtx,
0597                    const GLchar*                 theName,
0598                    const Graphic3d_TextureUnit   theTextureUnit)
0599   {
0600     return SetSampler (theCtx, GetUniformLocation (theCtx, theName), theTextureUnit);
0601   }
0602 
0603   //! Specifies the value of the sampler uniform variable.
0604   Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
0605                                                GLint                         theLocation,
0606                                                const Graphic3d_TextureUnit   theTextureUnit);
0607 
0608 public:
0609 
0610   //! Update the shader program from external files (per shader stage) in the following way:
0611   //! 1) If external file does not exist, then it will be created (current source code will be dumped, no recompilation) and FALSE will be returned.
0612   //! 2) If external file exists and it has the same timestamp as   myDumpDate, nothing will be done and FALSE will be returned.
0613   //! 3) If external file exists and it has    newer timestamp than myDumpDate, shader  will be recompiled and relinked and TRUE will be returned.
0614   //! @param theCtx OpenGL context bound to this working thread
0615   //! @param theFolder folder to store files; when unspecified, $CSF_ShadersDirectoryDump or current folder will be used instead
0616   //! @param theToBeautify flag improving formatting (add extra newlines)
0617   //! @param theToReset when TRUE, existing dumps will be overridden
0618   Standard_EXPORT Standard_Boolean UpdateDebugDump (const Handle(OpenGl_Context)& theCtx,
0619                                                     const TCollection_AsciiString& theFolder = "",
0620                                                     Standard_Boolean theToBeautify = Standard_False,
0621                                                     Standard_Boolean theToReset = Standard_False);
0622 
0623 protected:
0624 
0625   //! Increments counter of users.
0626   //! Used by OpenGl_ShaderManager.
0627   //! @return true when resource has been restored from delayed release queue
0628   bool Share()
0629   {
0630     return ++myShareCount == 1;
0631   }
0632 
0633   //! Decrements counter of users.
0634   //! Used by OpenGl_ShaderManager.
0635   //! @return true when there are no more users of this program has been left
0636   bool UnShare()
0637   {
0638     return --myShareCount == 0;
0639   }
0640 
0641   //! Links the program object.
0642   Standard_EXPORT Standard_Boolean link (const Handle(OpenGl_Context)& theCtx);
0643 
0644 protected:
0645 
0646   GLuint                          myProgramID;     //!< Handle of OpenGL shader program
0647   OpenGl_ShaderList               myShaderObjects; //!< List of attached shader objects
0648   Handle(Graphic3d_ShaderProgram) myProxy;         //!< Proxy shader program (from application layer)
0649   Standard_Integer                myShareCount;    //!< program users count, initialized with 1 (already shared by one user)
0650   Standard_Integer                myNbLightsMax;   //!< length of array of light sources (THE_MAX_LIGHTS)
0651   Standard_Integer                myNbShadowMaps;  //!< length of array of shadow maps (THE_NB_SHADOWMAPS)
0652   Standard_Integer                myNbClipPlanesMax; //!< length of array of clipping planes (THE_MAX_CLIP_PLANES)
0653   Standard_Integer                myNbFragOutputs; //!< length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS)
0654   Standard_Integer                myTextureSetBits;//!< texture units declared within the program, @sa Graphic3d_TextureSetBits
0655   Graphic3d_RenderTransparentMethod myOitOutput;   //!< flag indicating that Fragment Shader includes OIT outputs
0656   Standard_Boolean                myHasAlphaTest;  //!< flag indicating that Fragment Shader should perform alpha-test
0657   Standard_Boolean                myHasTessShader; //!< flag indicating that program defines tessellation stage
0658 
0659 protected:
0660 
0661   Standard_Size myCurrentState[OpenGl_UniformStateType_NB]; //!< defines last modification for variables of each state type
0662 
0663   //! Stores locations of OCCT state uniform variables.
0664   OpenGl_ShaderUniformLocation myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
0665 
0666 };
0667 
0668 template<class T>
0669 struct OpenGl_VariableSetter : public OpenGl_SetterInterface
0670 {
0671   virtual void Set (const Handle(OpenGl_Context)&           theCtx,
0672                     const Handle(Graphic3d_ShaderVariable)& theVariable,
0673                     OpenGl_ShaderProgram*                   theProgram)
0674   {
0675     theProgram->SetUniform (theCtx,
0676                             theVariable->Name().ToCString(),
0677                             theVariable->Value()->As<T>());
0678   }
0679 };
0680 
0681 namespace OpenGl_HashMapInitializer
0682 {
0683   template<class K, class V>
0684   struct MapListOfType
0685   {
0686     NCollection_DataMap<K, V> myDictionary;
0687 
0688     MapListOfType (K theKey, V theValue)
0689     {
0690       myDictionary.Bind (theKey, theValue);
0691     }
0692 
0693     MapListOfType& operator() (K theKey, V theValue)
0694     {
0695       myDictionary.Bind (theKey, theValue);
0696       return *this;
0697     }
0698 
0699     operator const NCollection_DataMap<K, V>& () const
0700     {
0701       return myDictionary;
0702     }
0703   };
0704 
0705   template<class K, class V>
0706   MapListOfType<K, V> CreateListOf (K theKey, V theValue)
0707   {
0708     return MapListOfType<K, V> (theKey, theValue);
0709   }
0710 }
0711 
0712 #endif // _OpenGl_ShaderProgram_Header