Warning, file /include/opencascade/OpenGl_ShaderProgram.hxx was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
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
0031 enum OpenGl_StateVariable
0032 {
0033
0034 OpenGl_OCC_MODEL_WORLD_MATRIX,
0035 OpenGl_OCC_WORLD_VIEW_MATRIX,
0036 OpenGl_OCC_PROJECTION_MATRIX,
0037 OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE,
0038 OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE,
0039 OpenGl_OCC_PROJECTION_MATRIX_INVERSE,
0040 OpenGl_OCC_MODEL_WORLD_MATRIX_TRANSPOSE,
0041 OpenGl_OCC_WORLD_VIEW_MATRIX_TRANSPOSE,
0042 OpenGl_OCC_PROJECTION_MATRIX_TRANSPOSE,
0043 OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE_TRANSPOSE,
0044 OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE_TRANSPOSE,
0045 OpenGl_OCC_PROJECTION_MATRIX_INVERSE_TRANSPOSE,
0046
0047
0048 OpenGl_OCC_CLIP_PLANE_EQUATIONS,
0049 OpenGl_OCC_CLIP_PLANE_CHAINS,
0050 OpenGl_OCC_CLIP_PLANE_COUNT,
0051
0052
0053 OpenGl_OCC_LIGHT_SOURCE_COUNT,
0054 OpenGl_OCC_LIGHT_SOURCE_TYPES,
0055 OpenGl_OCC_LIGHT_SOURCE_PARAMS,
0056 OpenGl_OCC_LIGHT_AMBIENT,
0057 OpenGl_OCC_LIGHT_SHADOWMAP_SIZE_BIAS,
0058 OpenGl_OCC_LIGHT_SHADOWMAP_SAMPLERS,
0059 OpenGl_OCC_LIGHT_SHADOWMAP_MATRICES,
0060
0061
0062 OpenGl_OCCT_TEXTURE_ENABLE,
0063 OpenGl_OCCT_DISTINGUISH_MODE,
0064 OpenGl_OCCT_PBR_MATERIAL,
0065 OpenGl_OCCT_COMMON_MATERIAL,
0066 OpenGl_OCCT_ALPHA_CUTOFF,
0067 OpenGl_OCCT_COLOR,
0068 OpenGl_OCCT_BACK_COLOR,
0069
0070
0071 OpenGl_OCCT_OIT_OUTPUT,
0072 OpenGl_OCCT_OIT_DEPTH_FACTOR,
0073
0074
0075 OpenGl_OCCT_TEXTURE_TRSF2D,
0076 OpenGl_OCCT_POINT_SIZE,
0077
0078
0079 OpenGl_OCCT_VIEWPORT,
0080 OpenGl_OCCT_LINE_WIDTH,
0081 OpenGl_OCCT_LINE_FEATHER,
0082 OpenGl_OCCT_LINE_STIPPLE_PATTERN,
0083 OpenGl_OCCT_LINE_STIPPLE_FACTOR,
0084 OpenGl_OCCT_WIREFRAME_COLOR,
0085 OpenGl_OCCT_QUAD_MODE_STATE,
0086
0087
0088 OpenGl_OCCT_ORTHO_SCALE,
0089 OpenGl_OCCT_SILHOUETTE_THICKNESS,
0090
0091
0092 OpenGl_OCCT_NB_SPEC_IBL_LEVELS,
0093
0094
0095 OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
0096 };
0097
0098
0099 struct OpenGl_SetterInterface
0100 {
0101
0102 virtual void Set(const occ::handle<OpenGl_Context>& theCtx,
0103 const occ::handle<Graphic3d_ShaderVariable>& theVariable,
0104 OpenGl_ShaderProgram* theProgram) = 0;
0105
0106
0107 virtual ~OpenGl_SetterInterface() = default;
0108 };
0109
0110
0111
0112
0113
0114
0115 class OpenGl_VariableSetterSelector
0116 {
0117 public:
0118
0119 OpenGl_VariableSetterSelector();
0120
0121
0122 ~OpenGl_VariableSetterSelector();
0123
0124
0125 void Set(const occ::handle<OpenGl_Context>& theCtx,
0126 const occ::handle<Graphic3d_ShaderVariable>& theVariable,
0127 OpenGl_ShaderProgram* theProgram) const;
0128
0129 private:
0130
0131 NCollection_DataMap<size_t, OpenGl_SetterInterface*> mySetterList;
0132 };
0133
0134
0135 enum OpenGl_UniformStateType
0136 {
0137 OpenGl_LIGHT_SOURCES_STATE,
0138 OpenGl_CLIP_PLANES_STATE,
0139 OpenGl_MODEL_WORLD_STATE,
0140 OpenGl_WORLD_VIEW_STATE,
0141 OpenGl_PROJECTION_STATE,
0142 OpenGl_MATERIAL_STATE,
0143 OpenGl_SURF_DETAIL_STATE,
0144 OpenGL_OIT_STATE,
0145 OpenGl_UniformStateType_NB
0146 };
0147
0148
0149 class OpenGl_ShaderUniformLocation
0150 {
0151 public:
0152
0153 static const GLint INVALID_LOCATION = -1;
0154
0155 public:
0156
0157 OpenGl_ShaderUniformLocation()
0158 : myLocation(INVALID_LOCATION)
0159 {
0160 }
0161
0162
0163 explicit OpenGl_ShaderUniformLocation(GLint theLocation)
0164 : myLocation(theLocation)
0165 {
0166 }
0167
0168
0169
0170
0171 bool IsValid() const { return myLocation != INVALID_LOCATION; }
0172
0173
0174 operator bool() const { return myLocation != INVALID_LOCATION; }
0175
0176
0177 operator GLint() const { return myLocation; }
0178
0179 private:
0180 GLint myLocation;
0181 };
0182
0183
0184 class OpenGl_ShaderProgram : public OpenGl_NamedResource
0185 {
0186 friend class OpenGl_View;
0187 friend class OpenGl_ShaderManager;
0188 DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderProgram, OpenGl_NamedResource)
0189 public:
0190
0191 static const GLuint NO_PROGRAM = 0;
0192
0193
0194 static const GLint INVALID_LOCATION = -1;
0195
0196
0197 static const char* PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
0198
0199
0200 Standard_EXPORT static bool compileShaderVerbose(
0201 const occ::handle<OpenGl_Context>& theCtx,
0202 const occ::handle<OpenGl_ShaderObject>& theShader,
0203 const TCollection_AsciiString& theSource,
0204 bool theToPrintSource = true);
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219 Standard_EXPORT OpenGl_ShaderProgram(
0220 const occ::handle<Graphic3d_ShaderProgram>& theProxy = nullptr,
0221 const TCollection_AsciiString& theId = "");
0222
0223 protected:
0224 static OpenGl_VariableSetterSelector mySetterSelector;
0225
0226 public:
0227
0228 Standard_EXPORT ~OpenGl_ShaderProgram() override;
0229
0230
0231 Standard_EXPORT bool Create(const occ::handle<OpenGl_Context>& theCtx);
0232
0233
0234 Standard_EXPORT void Release(OpenGl_Context* theCtx) override;
0235
0236
0237 size_t EstimatedDataSize() const override { return 0; }
0238
0239
0240 Standard_EXPORT bool AttachShader(const occ::handle<OpenGl_Context>& theCtx,
0241 const occ::handle<OpenGl_ShaderObject>& theShader);
0242
0243
0244 Standard_EXPORT bool DetachShader(const occ::handle<OpenGl_Context>& theCtx,
0245 const occ::handle<OpenGl_ShaderObject>& theShader);
0246
0247
0248 Standard_EXPORT bool Initialize(
0249 const occ::handle<OpenGl_Context>& theCtx,
0250 const NCollection_Sequence<occ::handle<Graphic3d_ShaderObject>>& theShaders);
0251
0252
0253
0254
0255 Standard_EXPORT bool Link(const occ::handle<OpenGl_Context>& theCtx, bool theIsVerbose = true);
0256
0257
0258 Standard_EXPORT bool FetchInfoLog(const occ::handle<OpenGl_Context>& theCtx,
0259 TCollection_AsciiString& theLog);
0260
0261
0262 Standard_EXPORT bool ApplyVariables(const occ::handle<OpenGl_Context>& theCtx);
0263
0264
0265 const occ::handle<Graphic3d_ShaderProgram>& Proxy() const { return myProxy; }
0266
0267
0268 inline bool IsValid() const { return myProgramID != NO_PROGRAM; }
0269
0270
0271 inline GLuint ProgramId() const { return myProgramID; }
0272
0273 public:
0274
0275 bool HasTessellationStage() const { return myHasTessShader; }
0276
0277
0278
0279 int NbLightsMax() const { return myNbLightsMax; }
0280
0281
0282 int NbShadowMaps() const { return myNbShadowMaps; }
0283
0284
0285
0286
0287 int NbClipPlanesMax() const { return myNbClipPlanesMax; }
0288
0289
0290
0291 int NbFragmentOutputs() const { return myNbFragOutputs; }
0292
0293
0294 bool HasAlphaTest() const { return myHasAlphaTest; }
0295
0296
0297 Graphic3d_RenderTransparentMethod OitOutput() const { return myOitOutput; }
0298
0299
0300 int TextureSetBits() const { return myTextureSetBits; }
0301
0302 private:
0303
0304 size_t ActiveState(const OpenGl_UniformStateType theType) const
0305 {
0306 return theType < OpenGl_UniformStateType_NB ? myCurrentState[theType] : 0;
0307 }
0308
0309
0310 void UpdateState(const OpenGl_UniformStateType theType, const size_t theIndex)
0311 {
0312 if (theType < OpenGl_UniformStateType_NB)
0313 {
0314 myCurrentState[theType] = theIndex;
0315 }
0316 }
0317
0318 public:
0319
0320 Standard_EXPORT OpenGl_ShaderUniformLocation
0321 GetUniformLocation(const occ::handle<OpenGl_Context>& theCtx, const GLchar* theName) const;
0322
0323
0324 Standard_EXPORT GLint GetAttributeLocation(const occ::handle<OpenGl_Context>& theCtx,
0325 const GLchar* theName) const;
0326
0327
0328 const OpenGl_ShaderUniformLocation& GetStateLocation(OpenGl_StateVariable theVariable) const
0329 {
0330 return myStateLocations[theVariable];
0331 }
0332
0333 public:
0334
0335 template <typename ValueType>
0336 bool GetUniform(const occ::handle<OpenGl_Context>& theCtx,
0337 const GLchar* theName,
0338 ValueType& theValue) const
0339 {
0340 return GetUniform(theCtx, GetUniformLocation(theCtx, theName), theValue);
0341 }
0342
0343
0344
0345 Standard_EXPORT bool GetUniform(const occ::handle<OpenGl_Context>& theCtx,
0346 GLint theLocation,
0347 NCollection_Vec4<int>& theValue) const;
0348
0349
0350
0351 Standard_EXPORT bool GetUniform(const occ::handle<OpenGl_Context>& theCtx,
0352 GLint theLocation,
0353 NCollection_Vec4<float>& theValue) const;
0354
0355 public:
0356
0357 template <typename ValueType>
0358 bool GetAttribute(const occ::handle<OpenGl_Context>& theCtx,
0359 const GLchar* theName,
0360 ValueType& theValue) const
0361 {
0362 return GetAttribute(theCtx, GetAttributeLocation(theCtx, theName), theValue);
0363 }
0364
0365
0366
0367 Standard_EXPORT bool GetAttribute(const occ::handle<OpenGl_Context>& theCtx,
0368 GLint theIndex,
0369 NCollection_Vec4<int>& theValue) const;
0370
0371
0372
0373 Standard_EXPORT bool GetAttribute(const occ::handle<OpenGl_Context>& theCtx,
0374 GLint theIndex,
0375 NCollection_Vec4<float>& theValue) const;
0376
0377 public:
0378
0379 Standard_EXPORT bool SetAttributeName(const occ::handle<OpenGl_Context>& theCtx,
0380 GLint theIndex,
0381 const GLchar* theName);
0382
0383
0384 template <typename ValueType>
0385 bool SetAttribute(const occ::handle<OpenGl_Context>& theCtx,
0386 const GLchar* theName,
0387 const ValueType& theValue)
0388 {
0389 return SetAttribute(theCtx, GetAttributeLocation(theCtx, theName), theValue);
0390 }
0391
0392
0393 Standard_EXPORT bool SetAttribute(const occ::handle<OpenGl_Context>& theCtx,
0394 GLint theIndex,
0395 GLfloat theValue);
0396
0397
0398 Standard_EXPORT bool SetAttribute(const occ::handle<OpenGl_Context>& theCtx,
0399 GLint theIndex,
0400 const NCollection_Vec2<float>& theValue);
0401
0402
0403 Standard_EXPORT bool SetAttribute(const occ::handle<OpenGl_Context>& theCtx,
0404 GLint theIndex,
0405 const NCollection_Vec3<float>& theValue);
0406
0407
0408 Standard_EXPORT bool SetAttribute(const occ::handle<OpenGl_Context>& theCtx,
0409 GLint theIndex,
0410 const NCollection_Vec4<float>& theValue);
0411
0412 public:
0413
0414 template <typename ValueType>
0415 bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0416 const GLchar* theName,
0417 const ValueType& theValue)
0418 {
0419 return SetUniform(theCtx, GetUniformLocation(theCtx, theName), theValue);
0420 }
0421
0422
0423
0424 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0425 GLint theLocation,
0426 GLint theValue);
0427
0428
0429
0430 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0431 GLint theLocation,
0432 const NCollection_Vec2<int>& theValue);
0433
0434
0435
0436 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0437 GLint theLocation,
0438 const NCollection_Vec3<int>& theValue);
0439
0440
0441
0442 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0443 GLint theLocation,
0444 const NCollection_Vec4<int>& theValue);
0445
0446 public:
0447
0448
0449 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0450 GLint theLocation,
0451 const NCollection_Vec2<unsigned int>& theValue);
0452
0453
0454
0455 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0456 const GLchar* theName,
0457 const GLsizei theCount,
0458 const NCollection_Vec2<unsigned int>* theValue);
0459
0460
0461
0462 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0463 GLint theLocation,
0464 const GLsizei theCount,
0465 const NCollection_Vec2<unsigned int>* theValue);
0466
0467 public:
0468
0469
0470 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0471 GLint theLocation,
0472 GLfloat theValue);
0473
0474
0475
0476 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0477 GLint theLocation,
0478 const NCollection_Vec2<float>& theValue);
0479
0480
0481
0482 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0483 GLint theLocation,
0484 const NCollection_Vec3<float>& theValue);
0485
0486
0487
0488 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0489 GLint theLocation,
0490 const NCollection_Vec4<float>& theValue);
0491
0492 public:
0493
0494
0495 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0496 GLint theLocation,
0497 GLuint theCount,
0498 const NCollection_Mat3<float>* theData);
0499
0500
0501
0502 bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0503 const GLchar* theName,
0504 const NCollection_Mat3<float>& theValue,
0505 GLboolean theTranspose = GL_FALSE)
0506 {
0507 return SetUniform(theCtx, GetUniformLocation(theCtx, theName), theValue, theTranspose);
0508 }
0509
0510
0511
0512 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0513 GLint theLocation,
0514 const NCollection_Mat3<float>& theValue,
0515 GLboolean theTranspose = GL_FALSE);
0516
0517
0518
0519 bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0520 const GLchar* theName,
0521 const NCollection_Mat4<float>& theValue,
0522 GLboolean theTranspose = GL_FALSE)
0523 {
0524 return SetUniform(theCtx, GetUniformLocation(theCtx, theName), theValue, theTranspose);
0525 }
0526
0527
0528
0529 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0530 GLint theLocation,
0531 const NCollection_Mat4<float>& theValue,
0532 GLboolean theTranspose = GL_FALSE);
0533
0534
0535
0536 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0537 GLint theLocation,
0538 GLuint theCount,
0539 const NCollection_Mat4<float>* theData);
0540
0541
0542
0543 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0544 GLint theLocation,
0545 GLuint theCount,
0546 const float* theData);
0547
0548
0549
0550 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0551 GLint theLocation,
0552 GLuint theCount,
0553 const NCollection_Vec2<float>* theData);
0554
0555
0556
0557 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0558 GLint theLocation,
0559 GLuint theCount,
0560 const NCollection_Vec3<float>* theData);
0561
0562
0563
0564 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0565 GLint theLocation,
0566 GLuint theCount,
0567 const NCollection_Vec4<float>* theData);
0568
0569
0570
0571 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0572 GLint theLocation,
0573 GLuint theCount,
0574 const int* theData);
0575
0576
0577
0578 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0579 GLint theLocation,
0580 GLuint theCount,
0581 const NCollection_Vec2<int>* theData);
0582
0583
0584
0585 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0586 GLint theLocation,
0587 GLuint theCount,
0588 const NCollection_Vec3<int>* theData);
0589
0590
0591
0592 Standard_EXPORT bool SetUniform(const occ::handle<OpenGl_Context>& theCtx,
0593 GLint theLocation,
0594 GLuint theCount,
0595 const NCollection_Vec4<int>* theData);
0596
0597 public:
0598
0599 bool SetSampler(const occ::handle<OpenGl_Context>& theCtx,
0600 const GLchar* theName,
0601 const Graphic3d_TextureUnit theTextureUnit)
0602 {
0603 return SetSampler(theCtx, GetUniformLocation(theCtx, theName), theTextureUnit);
0604 }
0605
0606
0607 Standard_EXPORT bool SetSampler(const occ::handle<OpenGl_Context>& theCtx,
0608 GLint theLocation,
0609 const Graphic3d_TextureUnit theTextureUnit);
0610
0611 public:
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623 Standard_EXPORT bool UpdateDebugDump(const occ::handle<OpenGl_Context>& theCtx,
0624 const TCollection_AsciiString& theFolder = "",
0625 bool theToBeautify = false,
0626 bool theToReset = false);
0627
0628 protected:
0629
0630
0631
0632 bool Share() { return ++myShareCount == 1; }
0633
0634
0635
0636
0637 bool UnShare() { return --myShareCount == 0; }
0638
0639
0640 Standard_EXPORT bool link(const occ::handle<OpenGl_Context>& theCtx);
0641
0642 protected:
0643 GLuint myProgramID;
0644 NCollection_Sequence<occ::handle<OpenGl_ShaderObject>>
0645 myShaderObjects;
0646
0647 occ::handle<Graphic3d_ShaderProgram> myProxy;
0648 int myShareCount;
0649 int myNbLightsMax;
0650 int myNbShadowMaps;
0651 int myNbClipPlanesMax;
0652 int myNbFragOutputs;
0653 int myTextureSetBits;
0654 Graphic3d_RenderTransparentMethod myOitOutput;
0655 bool myHasAlphaTest;
0656 bool myHasTessShader;
0657
0658 protected:
0659
0660 size_t myCurrentState[OpenGl_UniformStateType_NB];
0661
0662
0663
0664 OpenGl_ShaderUniformLocation myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
0665 };
0666
0667 template <class T>
0668 struct OpenGl_VariableSetter : public OpenGl_SetterInterface
0669 {
0670 void Set(const occ::handle<OpenGl_Context>& theCtx,
0671 const occ::handle<Graphic3d_ShaderVariable>& theVariable,
0672 OpenGl_ShaderProgram* theProgram) override
0673 {
0674 theProgram->SetUniform(theCtx, theVariable->Name().ToCString(), theVariable->Value()->As<T>());
0675 }
0676 };
0677
0678 namespace OpenGl_HashMapInitializer
0679 {
0680 template <class K, class V>
0681 struct MapListOfType
0682 {
0683 NCollection_DataMap<K, V> myDictionary;
0684
0685 MapListOfType(K theKey, V theValue) { myDictionary.Bind(theKey, theValue); }
0686
0687 MapListOfType& operator()(K theKey, V theValue)
0688 {
0689 myDictionary.Bind(theKey, theValue);
0690 return *this;
0691 }
0692
0693 operator const NCollection_DataMap<K, V>&() const { return myDictionary; }
0694 };
0695
0696 template <class K, class V>
0697 MapListOfType<K, V> CreateListOf(K theKey, V theValue)
0698 {
0699 return MapListOfType<K, V>(theKey, theValue);
0700 }
0701 }
0702
0703 #endif