File indexing completed on 2025-01-18 10:04:35
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
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
0034 enum OpenGl_StateVariable
0035 {
0036
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
0051 OpenGl_OCC_CLIP_PLANE_EQUATIONS,
0052 OpenGl_OCC_CLIP_PLANE_CHAINS,
0053 OpenGl_OCC_CLIP_PLANE_COUNT,
0054
0055
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,
0061 OpenGl_OCC_LIGHT_SHADOWMAP_SAMPLERS,
0062 OpenGl_OCC_LIGHT_SHADOWMAP_MATRICES,
0063
0064
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
0073 OpenGl_OCCT_OIT_OUTPUT,
0074 OpenGl_OCCT_OIT_DEPTH_FACTOR,
0075
0076
0077 OpenGl_OCCT_TEXTURE_TRSF2D,
0078 OpenGl_OCCT_POINT_SIZE,
0079
0080
0081 OpenGl_OCCT_VIEWPORT,
0082 OpenGl_OCCT_LINE_WIDTH,
0083 OpenGl_OCCT_LINE_FEATHER,
0084 OpenGl_OCCT_LINE_STIPPLE_PATTERN,
0085 OpenGl_OCCT_LINE_STIPPLE_FACTOR,
0086 OpenGl_OCCT_WIREFRAME_COLOR,
0087 OpenGl_OCCT_QUAD_MODE_STATE,
0088
0089
0090 OpenGl_OCCT_ORTHO_SCALE,
0091 OpenGl_OCCT_SILHOUETTE_THICKNESS,
0092
0093
0094 OpenGl_OCCT_NB_SPEC_IBL_LEVELS,
0095
0096
0097 OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
0098 };
0099
0100
0101 struct OpenGl_SetterInterface
0102 {
0103
0104 virtual void Set (const Handle(OpenGl_Context)& theCtx,
0105 const Handle(Graphic3d_ShaderVariable)& theVariable,
0106 OpenGl_ShaderProgram* theProgram) = 0;
0107
0108
0109 virtual ~OpenGl_SetterInterface() {}
0110 };
0111
0112
0113 typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)> OpenGl_ShaderList;
0114
0115
0116 typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList;
0117
0118
0119 class OpenGl_VariableSetterSelector
0120 {
0121 public:
0122
0123
0124 OpenGl_VariableSetterSelector();
0125
0126
0127 ~OpenGl_VariableSetterSelector();
0128
0129
0130 void Set (const Handle(OpenGl_Context)& theCtx,
0131 const Handle(Graphic3d_ShaderVariable)& theVariable,
0132 OpenGl_ShaderProgram* theProgram) const;
0133
0134 private:
0135
0136
0137 OpenGl_SetterList mySetterList;
0138 };
0139
0140
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
0155 class OpenGl_ShaderUniformLocation
0156 {
0157 public:
0158
0159 static const GLint INVALID_LOCATION = -1;
0160 public:
0161
0162
0163 OpenGl_ShaderUniformLocation() : myLocation (INVALID_LOCATION) {}
0164
0165
0166 explicit OpenGl_ShaderUniformLocation (GLint theLocation) : myLocation (theLocation) {}
0167
0168
0169
0170 bool IsValid() const { return myLocation != INVALID_LOCATION; }
0171
0172
0173 operator bool() const { return myLocation != INVALID_LOCATION; }
0174
0175
0176 operator GLint() const { return myLocation; }
0177
0178 private:
0179 GLint myLocation;
0180 };
0181
0182
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
0191 static const GLuint NO_PROGRAM = 0;
0192
0193
0194 static const GLint INVALID_LOCATION = -1;
0195
0196
0197 static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
0198
0199
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
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
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
0226 Standard_EXPORT virtual ~OpenGl_ShaderProgram();
0227
0228
0229 Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
0230
0231
0232 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
0233
0234
0235 virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE { return 0; }
0236
0237
0238 Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)& theCtx,
0239 const Handle(OpenGl_ShaderObject)& theShader);
0240
0241
0242 Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)& theCtx,
0243 const Handle(OpenGl_ShaderObject)& theShader);
0244
0245
0246 Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)& theCtx,
0247 const Graphic3d_ShaderObjectList& theShaders);
0248
0249
0250
0251
0252 Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx,
0253 bool theIsVerbose = true);
0254
0255
0256 Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
0257 TCollection_AsciiString& theLog);
0258
0259
0260 Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
0261
0262
0263 const Handle(Graphic3d_ShaderProgram)& Proxy() const { return myProxy; }
0264
0265
0266 inline bool IsValid() const
0267 {
0268 return myProgramID != NO_PROGRAM;
0269 }
0270
0271
0272 inline GLuint ProgramId() const
0273 {
0274 return myProgramID;
0275 }
0276
0277 public:
0278
0279
0280 Standard_Boolean HasTessellationStage() const { return myHasTessShader; }
0281
0282
0283
0284 Standard_Integer NbLightsMax() const { return myNbLightsMax; }
0285
0286
0287 Standard_Integer NbShadowMaps() const { return myNbShadowMaps; }
0288
0289
0290
0291 Standard_Integer NbClipPlanesMax() const { return myNbClipPlanesMax; }
0292
0293
0294
0295 Standard_Integer NbFragmentOutputs() const { return myNbFragOutputs; }
0296
0297
0298 Standard_Boolean HasAlphaTest() const { return myHasAlphaTest; }
0299
0300
0301 Graphic3d_RenderTransparentMethod OitOutput() const { return myOitOutput; }
0302
0303
0304 Standard_Integer TextureSetBits() const { return myTextureSetBits; }
0305
0306 private:
0307
0308
0309 Standard_Size ActiveState (const OpenGl_UniformStateType theType) const
0310 {
0311 return theType < OpenGl_UniformStateType_NB
0312 ? myCurrentState[theType]
0313 : 0;
0314 }
0315
0316
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
0329 Standard_EXPORT OpenGl_ShaderUniformLocation GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
0330 const GLchar* theName) const;
0331
0332
0333 Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
0334 const GLchar* theName) const;
0335
0336
0337 const OpenGl_ShaderUniformLocation& GetStateLocation (OpenGl_StateVariable theVariable) const { return myStateLocations[theVariable]; }
0338
0339 public:
0340
0341
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
0351
0352 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
0353 GLint theLocation,
0354 OpenGl_Vec4i& theValue) const;
0355
0356
0357
0358 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
0359 GLint theLocation,
0360 OpenGl_Vec4& theValue) const;
0361
0362 public:
0363
0364
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
0374
0375 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
0376 GLint theIndex,
0377 OpenGl_Vec4i& theValue) const;
0378
0379
0380
0381 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
0382 GLint theIndex,
0383 OpenGl_Vec4& theValue) const;
0384
0385 public:
0386
0387
0388 Standard_EXPORT Standard_Boolean SetAttributeName (const Handle(OpenGl_Context)& theCtx,
0389 GLint theIndex,
0390 const GLchar* theName);
0391
0392
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
0402 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
0403 GLint theIndex,
0404 GLfloat theValue);
0405
0406
0407 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
0408 GLint theIndex,
0409 const OpenGl_Vec2& theValue);
0410
0411
0412 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
0413 GLint theIndex,
0414 const OpenGl_Vec3& theValue);
0415
0416
0417 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
0418 GLint theIndex,
0419 const OpenGl_Vec4& theValue);
0420
0421 public:
0422
0423
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
0433
0434 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0435 GLint theLocation,
0436 GLint theValue);
0437
0438
0439
0440 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0441 GLint theLocation,
0442 const OpenGl_Vec2i& theValue);
0443
0444
0445
0446 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0447 GLint theLocation,
0448 const OpenGl_Vec3i& theValue);
0449
0450
0451
0452 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0453 GLint theLocation,
0454 const OpenGl_Vec4i& theValue);
0455
0456 public:
0457
0458
0459
0460 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0461 GLint theLocation,
0462 const OpenGl_Vec2u& theValue);
0463
0464
0465
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
0472
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
0481
0482 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0483 GLint theLocation,
0484 GLfloat theValue);
0485
0486
0487
0488 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0489 GLint theLocation,
0490 const OpenGl_Vec2& theValue);
0491
0492
0493
0494 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0495 GLint theLocation,
0496 const OpenGl_Vec3& theValue);
0497
0498
0499
0500 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0501 GLint theLocation,
0502 const OpenGl_Vec4& theValue);
0503
0504 public:
0505
0506
0507
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
0514
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
0524
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
0531
0532 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0533 GLint theLocation,
0534 GLuint theCount,
0535 const OpenGl_Mat4* theData);
0536
0537
0538
0539 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0540 GLint theLocation,
0541 GLuint theCount,
0542 const Standard_ShortReal* theData);
0543
0544
0545
0546 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0547 GLint theLocation,
0548 GLuint theCount,
0549 const OpenGl_Vec2* theData);
0550
0551
0552
0553 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0554 GLint theLocation,
0555 GLuint theCount,
0556 const OpenGl_Vec3* theData);
0557
0558
0559
0560 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0561 GLint theLocation,
0562 GLuint theCount,
0563 const OpenGl_Vec4* theData);
0564
0565
0566
0567 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0568 GLint theLocation,
0569 GLuint theCount,
0570 const Standard_Integer* theData);
0571
0572
0573
0574 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0575 GLint theLocation,
0576 GLuint theCount,
0577 const OpenGl_Vec2i* theData);
0578
0579
0580
0581 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
0582 GLint theLocation,
0583 GLuint theCount,
0584 const OpenGl_Vec3i* theData);
0585
0586
0587
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
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
0604 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
0605 GLint theLocation,
0606 const Graphic3d_TextureUnit theTextureUnit);
0607
0608 public:
0609
0610
0611
0612
0613
0614
0615
0616
0617
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
0626
0627
0628 bool Share()
0629 {
0630 return ++myShareCount == 1;
0631 }
0632
0633
0634
0635
0636 bool UnShare()
0637 {
0638 return --myShareCount == 0;
0639 }
0640
0641
0642 Standard_EXPORT Standard_Boolean link (const Handle(OpenGl_Context)& theCtx);
0643
0644 protected:
0645
0646 GLuint myProgramID;
0647 OpenGl_ShaderList myShaderObjects;
0648 Handle(Graphic3d_ShaderProgram) myProxy;
0649 Standard_Integer myShareCount;
0650 Standard_Integer myNbLightsMax;
0651 Standard_Integer myNbShadowMaps;
0652 Standard_Integer myNbClipPlanesMax;
0653 Standard_Integer myNbFragOutputs;
0654 Standard_Integer myTextureSetBits;
0655 Graphic3d_RenderTransparentMethod myOitOutput;
0656 Standard_Boolean myHasAlphaTest;
0657 Standard_Boolean myHasTessShader;
0658
0659 protected:
0660
0661 Standard_Size myCurrentState[OpenGl_UniformStateType_NB];
0662
0663
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