1 // Created on: 2013-09-19 2 // Created by: Denis BOGOLEPOV 3 // Copyright (c) 2013-2014 OPEN CASCADE SAS 4 // 5 // This file is part of Open CASCADE Technology software library. 6 // 7 // This library is free software; you can redistribute it and/or modify it under 8 // the terms of the GNU Lesser General Public License version 2.1 as published 9 // by the Free Software Foundation, with special exception defined in the file 10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT 11 // distribution for complete text of the license and disclaimer of any warranty. 12 // 13 // Alternatively, this file may be used under the terms of Open CASCADE 14 // commercial license or contractual agreement. 15 16 #ifndef OpenGl_ShaderProgram_HeaderFile 17 #define OpenGl_ShaderProgram_HeaderFile 18 19 #include <NCollection_DataMap.hxx> 20 #include <NCollection_Sequence.hxx> 21 #include <TCollection_AsciiString.hxx> 22 23 #include <Graphic3d_ShaderObject.hxx> 24 #include <Graphic3d_ShaderProgram.hxx> 25 #include <Graphic3d_TextureSetBits.hxx> 26 27 #include <OpenGl_Vec.hxx> 28 #include <OpenGl_NamedResource.hxx> 29 #include <OpenGl_ShaderObject.hxx> 30 31 class OpenGl_ShaderProgram; 32 DEFINE_STANDARD_HANDLE(OpenGl_ShaderProgram, OpenGl_NamedResource) 33 34 //! The enumeration of OCCT-specific OpenGL/GLSL variables. 35 enum OpenGl_StateVariable 36 { 37 // OpenGL matrix state 38 OpenGl_OCC_MODEL_WORLD_MATRIX, 39 OpenGl_OCC_WORLD_VIEW_MATRIX, 40 OpenGl_OCC_PROJECTION_MATRIX, 41 OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE, 42 OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE, 43 OpenGl_OCC_PROJECTION_MATRIX_INVERSE, 44 OpenGl_OCC_MODEL_WORLD_MATRIX_TRANSPOSE, 45 OpenGl_OCC_WORLD_VIEW_MATRIX_TRANSPOSE, 46 OpenGl_OCC_PROJECTION_MATRIX_TRANSPOSE, 47 OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE_TRANSPOSE, 48 OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE_TRANSPOSE, 49 OpenGl_OCC_PROJECTION_MATRIX_INVERSE_TRANSPOSE, 50 51 // OpenGL clip planes state 52 OpenGl_OCC_CLIP_PLANE_EQUATIONS, 53 OpenGl_OCC_CLIP_PLANE_CHAINS, 54 OpenGl_OCC_CLIP_PLANE_COUNT, 55 56 // OpenGL light state 57 OpenGl_OCC_LIGHT_SOURCE_COUNT, 58 OpenGl_OCC_LIGHT_SOURCE_TYPES, 59 OpenGl_OCC_LIGHT_SOURCE_PARAMS, 60 OpenGl_OCC_LIGHT_AMBIENT, 61 OpenGl_OCC_LIGHT_SHADOWMAP_SIZE_BIAS,// occShadowMapSizeBias 62 OpenGl_OCC_LIGHT_SHADOWMAP_SAMPLERS, // occShadowMapSamplers 63 OpenGl_OCC_LIGHT_SHADOWMAP_MATRICES, // occShadowMapMatrices 64 65 // Material state 66 OpenGl_OCCT_TEXTURE_ENABLE, 67 OpenGl_OCCT_DISTINGUISH_MODE, 68 OpenGl_OCCT_PBR_MATERIAL, 69 OpenGl_OCCT_COMMON_MATERIAL, 70 OpenGl_OCCT_ALPHA_CUTOFF, 71 OpenGl_OCCT_COLOR, 72 73 // Weighted, Blended Order-Independent Transparency rendering state 74 OpenGl_OCCT_OIT_OUTPUT, 75 OpenGl_OCCT_OIT_DEPTH_FACTOR, 76 77 // Context-dependent state 78 OpenGl_OCCT_TEXTURE_TRSF2D, 79 OpenGl_OCCT_POINT_SIZE, 80 81 // Wireframe state 82 OpenGl_OCCT_VIEWPORT, 83 OpenGl_OCCT_LINE_WIDTH, 84 OpenGl_OCCT_LINE_FEATHER, 85 OpenGl_OCCT_LINE_STIPPLE_PATTERN, // occStipplePattern 86 OpenGl_OCCT_LINE_STIPPLE_FACTOR, // occStippleFactor 87 OpenGl_OCCT_WIREFRAME_COLOR, 88 OpenGl_OCCT_QUAD_MODE_STATE, 89 90 // Parameters of outline (silhouette) shader 91 OpenGl_OCCT_ORTHO_SCALE, 92 OpenGl_OCCT_SILHOUETTE_THICKNESS, 93 94 // PBR state 95 OpenGl_OCCT_NB_SPEC_IBL_LEVELS, 96 97 // DON'T MODIFY THIS ITEM (insert new items before it) 98 OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES 99 }; 100 101 //! Interface for generic setter of user-defined uniform variables. 102 struct OpenGl_SetterInterface 103 { 104 //! Sets user-defined uniform variable to specified program. 105 virtual void Set (const Handle(OpenGl_Context)& theCtx, 106 const Handle(Graphic3d_ShaderVariable)& theVariable, 107 OpenGl_ShaderProgram* theProgram) = 0; 108 109 //! Destructor ~OpenGl_SetterInterfaceOpenGl_SetterInterface110 virtual ~OpenGl_SetterInterface() {} 111 }; 112 113 //! List of OpenGL shader objects. 114 typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)> OpenGl_ShaderList; 115 116 //! List of shader variable setters. 117 typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList; 118 119 //! Support tool for setting user-defined uniform variables. 120 class OpenGl_VariableSetterSelector 121 { 122 public: 123 124 //! Creates new setter selector. 125 OpenGl_VariableSetterSelector(); 126 127 //! Releases memory resources of setter selector. 128 ~OpenGl_VariableSetterSelector(); 129 130 //! Sets user-defined uniform variable to specified program. 131 void Set (const Handle(OpenGl_Context)& theCtx, 132 const Handle(Graphic3d_ShaderVariable)& theVariable, 133 OpenGl_ShaderProgram* theProgram) const; 134 135 private: 136 137 //! List of variable setters. 138 OpenGl_SetterList mySetterList; 139 }; 140 141 //! Defines types of uniform state variables. 142 enum OpenGl_UniformStateType 143 { 144 OpenGl_LIGHT_SOURCES_STATE, 145 OpenGl_CLIP_PLANES_STATE, 146 OpenGl_MODEL_WORLD_STATE, 147 OpenGl_WORLD_VIEW_STATE, 148 OpenGl_PROJECTION_STATE, 149 OpenGl_MATERIAL_STATE, 150 OpenGl_SURF_DETAIL_STATE, 151 OpenGL_OIT_STATE, 152 OpenGl_UniformStateType_NB 153 }; 154 155 //! Simple class represents GLSL program variable location. 156 class OpenGl_ShaderUniformLocation 157 { 158 public: 159 //! Invalid location of uniform/attribute variable. 160 static const GLint INVALID_LOCATION = -1; 161 public: 162 163 //! Construct an invalid location. OpenGl_ShaderUniformLocation()164 OpenGl_ShaderUniformLocation() : myLocation (INVALID_LOCATION) {} 165 166 //! Constructor with initialization. OpenGl_ShaderUniformLocation(GLint theLocation)167 explicit OpenGl_ShaderUniformLocation (GLint theLocation) : myLocation (theLocation) {} 168 169 //! Note you may safely put invalid location in functions like glUniform* - the data passed in will be silently ignored. 170 //! @return true if location is not equal to -1. IsValid() const171 bool IsValid() const { return myLocation != INVALID_LOCATION; } 172 173 //! Return TRUE for non-invalid location. operator bool() const174 operator bool() const { return myLocation != INVALID_LOCATION; } 175 176 //! Convert operators help silently put object to GL functions like glUniform*. operator GLint() const177 operator GLint() const { return myLocation; } 178 179 private: 180 GLint myLocation; 181 }; 182 183 //! Wrapper for OpenGL program object. 184 class OpenGl_ShaderProgram : public OpenGl_NamedResource 185 { 186 friend class OpenGl_View; 187 friend class OpenGl_ShaderManager; 188 DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderProgram, OpenGl_NamedResource) 189 public: 190 191 //! Non-valid shader name. 192 static const GLuint NO_PROGRAM = 0; 193 194 //! Invalid location of uniform/attribute variable. 195 static const GLint INVALID_LOCATION = -1; 196 197 //! List of pre-defined OCCT state uniform variables. 198 static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES]; 199 200 //! Wrapper for compiling shader object with verbose printing on error. 201 Standard_EXPORT static bool compileShaderVerbose (const Handle(OpenGl_Context)& theCtx, 202 const Handle(OpenGl_ShaderObject)& theShader, 203 const TCollection_AsciiString& theSource, 204 bool theToPrintSource = true); 205 206 //! Creates uninitialized shader program. 207 //! 208 //! WARNING! This constructor is not intended to be called anywhere but from OpenGl_ShaderManager::Create(). 209 //! Manager has been designed to synchronize camera position, lights definition and other aspects of the program implicitly, 210 //! as well as sharing same program across rendering groups. 211 //! 212 //! Program created outside the manager will be left detached from these routines, 213 //! and them should be performed manually by caller. 214 //! 215 //! This constructor has been made public to provide more flexibility to re-use OCCT OpenGL classes without OCCT Viewer itself. 216 //! If this is not the case - create the program using shared OpenGl_ShaderManager instance instead. 217 Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL, 218 const TCollection_AsciiString& theId = ""); 219 220 protected: 221 222 static OpenGl_VariableSetterSelector mySetterSelector; 223 224 public: 225 226 //! Releases resources of shader program. 227 Standard_EXPORT virtual ~OpenGl_ShaderProgram(); 228 229 //! Creates new empty shader program of specified type. 230 Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx); 231 232 //! Destroys shader program. 233 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE; 234 235 //! Returns estimated GPU memory usage - cannot be easily estimated. EstimatedDataSize() const236 virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE { return 0; } 237 238 //! Attaches shader object to the program object. 239 Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)& theCtx, 240 const Handle(OpenGl_ShaderObject)& theShader); 241 242 //! Detaches shader object to the program object. 243 Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)& theCtx, 244 const Handle(OpenGl_ShaderObject)& theShader); 245 246 //! Initializes program object with the list of shader objects. 247 Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)& theCtx, 248 const Graphic3d_ShaderObjectList& theShaders); 249 250 //! Links the program object. 251 //! @param theCtx bound OpenGL context 252 //! @param theIsVerbose flag to print log on error 253 Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx, 254 bool theIsVerbose = true); 255 256 //! Fetches information log of the last link operation. 257 Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx, 258 TCollection_AsciiString& theLog); 259 260 //! Fetches uniform variables from proxy shader program. 261 Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx); 262 263 //! @return proxy shader program. Handle(Graphic3d_ShaderProgram)264 const Handle(Graphic3d_ShaderProgram)& Proxy() const { return myProxy; } 265 266 //! @return true if current object was initialized IsValid() const267 inline bool IsValid() const 268 { 269 return myProgramID != NO_PROGRAM; 270 } 271 272 //! @return program ID ProgramId() const273 inline GLuint ProgramId() const 274 { 275 return myProgramID; 276 } 277 278 public: 279 280 //! Return TRUE if program defines tessellation stage. HasTessellationStage() const281 Standard_Boolean HasTessellationStage() const { return myHasTessShader; } 282 283 //! Return the length of array of light sources (THE_MAX_LIGHTS), 284 //! to be used for initialization occLightSources (OpenGl_OCC_LIGHT_SOURCE_PARAMS). NbLightsMax() const285 Standard_Integer NbLightsMax() const { return myNbLightsMax; } 286 287 //! Return the length of array of shadow maps (THE_NB_SHADOWMAPS); 0 by default. NbShadowMaps() const288 Standard_Integer NbShadowMaps() const { return myNbShadowMaps; } 289 290 //! Return the length of array of clipping planes (THE_MAX_CLIP_PLANES), 291 //! to be used for initialization occClipPlaneEquations (OpenGl_OCC_CLIP_PLANE_EQUATIONS) and occClipPlaneChains (OpenGl_OCC_CLIP_PLANE_CHAINS). NbClipPlanesMax() const292 Standard_Integer NbClipPlanesMax() const { return myNbClipPlanesMax; } 293 294 //! Return the length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS), 295 //! to be used for initialization occFragColorArray/occFragColorN. NbFragmentOutputs() const296 Standard_Integer NbFragmentOutputs() const { return myNbFragOutputs; } 297 298 //! Return true if Fragment Shader should perform alpha test; FALSE by default. HasAlphaTest() const299 Standard_Boolean HasAlphaTest() const { return myHasAlphaTest; } 300 301 //! Return if Fragment Shader color should output the OIT values; OFF by default. OitOutput() const302 Graphic3d_RenderTransparentMethod OitOutput() const { return myOitOutput; } 303 304 //! Return texture units declared within the program, @sa Graphic3d_TextureSetBits. TextureSetBits() const305 Standard_Integer TextureSetBits() const { return myTextureSetBits; } 306 307 private: 308 309 //! Returns index of last modification of variables of specified state type. ActiveState(const OpenGl_UniformStateType theType) const310 Standard_Size ActiveState (const OpenGl_UniformStateType theType) const 311 { 312 return theType < OpenGl_UniformStateType_NB 313 ? myCurrentState[theType] 314 : 0; 315 } 316 317 //! Updates index of last modification of variables of specified state type. UpdateState(const OpenGl_UniformStateType theType,const Standard_Size theIndex)318 void UpdateState (const OpenGl_UniformStateType theType, 319 const Standard_Size theIndex) 320 { 321 if (theType < OpenGl_UniformStateType_NB) 322 { 323 myCurrentState[theType] = theIndex; 324 } 325 } 326 327 public: 328 329 //! Returns location of the specific uniform variable. 330 Standard_EXPORT OpenGl_ShaderUniformLocation GetUniformLocation (const Handle(OpenGl_Context)& theCtx, 331 const GLchar* theName) const; 332 333 //! Returns index of the generic vertex attribute by variable name. 334 Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx, 335 const GLchar* theName) const; 336 337 //! Returns location of the OCCT state uniform variable. GetStateLocation(OpenGl_StateVariable theVariable) const338 const OpenGl_ShaderUniformLocation& GetStateLocation (OpenGl_StateVariable theVariable) const { return myStateLocations[theVariable]; } 339 340 public: 341 342 //! Returns the value of the uniform variable from given name. 343 template<typename ValueType> GetUniform(const Handle (OpenGl_Context)& theCtx,const GLchar * theName,ValueType & theValue) const344 bool GetUniform (const Handle(OpenGl_Context)& theCtx, 345 const GLchar* theName, 346 ValueType& theValue) const 347 { 348 return GetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue); 349 } 350 351 //! Returns the value of the integer uniform variable. 352 //! Wrapper for glGetUniformiv() 353 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx, 354 GLint theLocation, 355 OpenGl_Vec4i& theValue) const; 356 357 //! Returns the value of the float uniform variable. 358 //! Wrapper for glGetUniformfv() 359 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx, 360 GLint theLocation, 361 OpenGl_Vec4& theValue) const; 362 363 public: 364 365 //! Returns the vertex attribute from given name. 366 template<typename ValueType> GetAttribute(const Handle (OpenGl_Context)& theCtx,const GLchar * theName,ValueType & theValue) const367 bool GetAttribute (const Handle(OpenGl_Context)& theCtx, 368 const GLchar* theName, 369 ValueType& theValue) const 370 { 371 return GetAttribute (theCtx, GetAttributeLocation (theCtx, theName), theValue); 372 } 373 374 //! Returns the integer vertex attribute. 375 //! Wrapper for glGetVertexAttribiv() 376 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx, 377 GLint theIndex, 378 OpenGl_Vec4i& theValue) const; 379 380 //! Returns the float vertex attribute. 381 //! Wrapper for glGetVertexAttribfv() 382 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx, 383 GLint theIndex, 384 OpenGl_Vec4& theValue) const; 385 386 public: 387 388 //! Wrapper for glBindAttribLocation() 389 Standard_EXPORT Standard_Boolean SetAttributeName (const Handle(OpenGl_Context)& theCtx, 390 GLint theIndex, 391 const GLchar* theName); 392 393 //! Wrapper for glVertexAttrib*() for attribute with the given name. 394 template<typename ValueType> SetAttribute(const Handle (OpenGl_Context)& theCtx,const GLchar * theName,const ValueType & theValue)395 bool SetAttribute (const Handle(OpenGl_Context)& theCtx, 396 const GLchar* theName, 397 const ValueType& theValue) 398 { 399 return SetAttribute (theCtx, GetAttributeLocation (theCtx, theName), theValue); 400 } 401 402 //! Wrapper for glVertexAttrib1f() 403 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx, 404 GLint theIndex, 405 GLfloat theValue); 406 407 //! Wrapper for glVertexAttrib2fv() 408 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx, 409 GLint theIndex, 410 const OpenGl_Vec2& theValue); 411 412 //! Wrapper for glVertexAttrib3fv() 413 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx, 414 GLint theIndex, 415 const OpenGl_Vec3& theValue); 416 417 //! Wrapper for glVertexAttrib4fv() 418 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx, 419 GLint theIndex, 420 const OpenGl_Vec4& theValue); 421 422 public: 423 424 //! Specifies the value of the uniform variable with given name. 425 template<typename ValueType> SetUniform(const Handle (OpenGl_Context)& theCtx,const GLchar * theName,const ValueType & theValue)426 bool SetUniform (const Handle(OpenGl_Context)& theCtx, 427 const GLchar* theName, 428 const ValueType& theValue) 429 { 430 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue); 431 } 432 433 //! Specifies the value of the integer uniform variable. 434 //! Wrapper for glUniform1i() 435 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 436 GLint theLocation, 437 GLint theValue); 438 439 //! Specifies the value of the integer uniform 2D vector. 440 //! Wrapper for glUniform2iv() 441 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 442 GLint theLocation, 443 const OpenGl_Vec2i& theValue); 444 445 //! Specifies the value of the integer uniform 3D vector. 446 //! Wrapper for glUniform3iv() 447 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 448 GLint theLocation, 449 const OpenGl_Vec3i& theValue); 450 451 //! Specifies the value of the integer uniform 4D vector. 452 //! Wrapper for glUniform4iv() 453 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 454 GLint theLocation, 455 const OpenGl_Vec4i& theValue); 456 457 public: 458 459 //! Specifies the value of the unsigned integer uniform 2D vector (uvec2). 460 //! Wrapper for glUniform2uiv() 461 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 462 GLint theLocation, 463 const OpenGl_Vec2u& theValue); 464 465 //! Specifies the value of the uvec2 uniform array 466 //! Wrapper for glUniform2uiv() 467 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 468 const GLchar* theName, 469 const GLsizei theCount, 470 const OpenGl_Vec2u* theValue); 471 472 //! Specifies the value of the uvec2 uniform array 473 //! Wrapper for glUniform2uiv() 474 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 475 GLint theLocation, 476 const GLsizei theCount, 477 const OpenGl_Vec2u* theValue); 478 479 public: 480 481 //! Specifies the value of the float uniform variable. 482 //! Wrapper for glUniform1f() 483 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 484 GLint theLocation, 485 GLfloat theValue); 486 487 //! Specifies the value of the float uniform 2D vector. 488 //! Wrapper for glUniform2fv() 489 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 490 GLint theLocation, 491 const OpenGl_Vec2& theValue); 492 493 //! Specifies the value of the float uniform 3D vector. 494 //! Wrapper for glUniform3fv() 495 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 496 GLint theLocation, 497 const OpenGl_Vec3& theValue); 498 499 //! Specifies the value of the float uniform 4D vector. 500 //! Wrapper for glUniform4fv() 501 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 502 GLint theLocation, 503 const OpenGl_Vec4& theValue); 504 505 public: 506 507 //! Specifies the value of the array of float uniform 3x3 matrices. 508 //! Wrapper over glUniformMatrix3fv(). 509 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 510 GLint theLocation, 511 GLuint theCount, 512 const NCollection_Mat3<float>* theData); 513 514 //! Specifies the value of the float uniform 4x4 matrix. 515 //! Wrapper for glUniformMatrix4fv() SetUniform(const Handle (OpenGl_Context)& theCtx,const GLchar * theName,const OpenGl_Mat4 & theValue,GLboolean theTranspose=GL_FALSE)516 bool SetUniform (const Handle(OpenGl_Context)& theCtx, 517 const GLchar* theName, 518 const OpenGl_Mat4& theValue, 519 GLboolean theTranspose = GL_FALSE) 520 { 521 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue, theTranspose); 522 } 523 524 //! Specifies the value of the float uniform 4x4 matrix. 525 //! Wrapper for glUniformMatrix4fv() 526 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 527 GLint theLocation, 528 const OpenGl_Mat4& theValue, 529 GLboolean theTranspose = GL_FALSE); 530 531 //! Specifies the value of the array of float uniform 4x4 matrices. 532 //! Wrapper over glUniformMatrix4fv(). 533 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 534 GLint theLocation, 535 GLuint theCount, 536 const OpenGl_Mat4* theData); 537 538 //! Specifies the value of the float uniform array 539 //! Wrapper over glUniform1fv() 540 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 541 GLint theLocation, 542 GLuint theCount, 543 const Standard_ShortReal* theData); 544 545 //! Specifies the value of the float2 uniform array 546 //! Wrapper over glUniform2fv() 547 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 548 GLint theLocation, 549 GLuint theCount, 550 const OpenGl_Vec2* theData); 551 552 //! Specifies the value of the float3 uniform array 553 //! Wrapper over glUniform3fv() 554 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 555 GLint theLocation, 556 GLuint theCount, 557 const OpenGl_Vec3* theData); 558 559 //! Specifies the value of the float4 uniform array 560 //! Wrapper over glUniform4fv() 561 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 562 GLint theLocation, 563 GLuint theCount, 564 const OpenGl_Vec4* theData); 565 566 //! Specifies the value of the integer uniform array 567 //! Wrapper over glUniform1iv() 568 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 569 GLint theLocation, 570 GLuint theCount, 571 const Standard_Integer* theData); 572 573 //! Specifies the value of the int2 uniform array 574 //! Wrapper over glUniform2iv() 575 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 576 GLint theLocation, 577 GLuint theCount, 578 const OpenGl_Vec2i* theData); 579 580 //! Specifies the value of the int3 uniform array 581 //! Wrapper over glUniform3iv() 582 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 583 GLint theLocation, 584 GLuint theCount, 585 const OpenGl_Vec3i* theData); 586 587 //! Specifies the value of the int4 uniform array 588 //! Wrapper over glUniform4iv() 589 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx, 590 GLint theLocation, 591 GLuint theCount, 592 const OpenGl_Vec4i* theData); 593 594 public: 595 596 //! Specifies the value of the sampler uniform variable. SetSampler(const Handle (OpenGl_Context)& theCtx,const GLchar * theName,const Graphic3d_TextureUnit theTextureUnit)597 bool SetSampler (const Handle(OpenGl_Context)& theCtx, 598 const GLchar* theName, 599 const Graphic3d_TextureUnit theTextureUnit) 600 { 601 return SetSampler (theCtx, GetUniformLocation (theCtx, theName), theTextureUnit); 602 } 603 604 //! Specifies the value of the sampler uniform variable. 605 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx, 606 GLint theLocation, 607 const Graphic3d_TextureUnit theTextureUnit); 608 609 public: 610 611 //! Update the shader program from external files (per shader stage) in the following way: 612 //! 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. 613 //! 2) If external file exists and it has the same timestamp as myDumpDate, nothing will be done and FALSE will be returned. 614 //! 3) If external file exists and it has newer timestamp than myDumpDate, shader will be recompiled and relinked and TRUE will be returned. 615 //! @param theCtx OpenGL context bound to this working thread 616 //! @param theFolder folder to store files; when unspecified, $CSF_ShadersDirectoryDump or current folder will be used instead 617 //! @param theToBeautify flag improving formatting (add extra newlines) 618 //! @param theToReset when TRUE, existing dumps will be overridden 619 Standard_EXPORT Standard_Boolean UpdateDebugDump (const Handle(OpenGl_Context)& theCtx, 620 const TCollection_AsciiString& theFolder = "", 621 Standard_Boolean theToBeautify = Standard_False, 622 Standard_Boolean theToReset = Standard_False); 623 624 protected: 625 626 //! Increments counter of users. 627 //! Used by OpenGl_ShaderManager. 628 //! @return true when resource has been restored from delayed release queue Share()629 bool Share() 630 { 631 return ++myShareCount == 1; 632 } 633 634 //! Decrements counter of users. 635 //! Used by OpenGl_ShaderManager. 636 //! @return true when there are no more users of this program has been left UnShare()637 bool UnShare() 638 { 639 return --myShareCount == 0; 640 } 641 642 //! Links the program object. 643 Standard_EXPORT Standard_Boolean link (const Handle(OpenGl_Context)& theCtx); 644 645 protected: 646 647 GLuint myProgramID; //!< Handle of OpenGL shader program 648 OpenGl_ShaderList myShaderObjects; //!< List of attached shader objects 649 Handle(Graphic3d_ShaderProgram) myProxy; //!< Proxy shader program (from application layer) 650 Standard_Integer myShareCount; //!< program users count, initialized with 1 (already shared by one user) 651 Standard_Integer myNbLightsMax; //!< length of array of light sources (THE_MAX_LIGHTS) 652 Standard_Integer myNbShadowMaps; //!< length of array of shadow maps (THE_NB_SHADOWMAPS) 653 Standard_Integer myNbClipPlanesMax; //!< length of array of clipping planes (THE_MAX_CLIP_PLANES) 654 Standard_Integer myNbFragOutputs; //!< length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS) 655 Standard_Integer myTextureSetBits;//!< texture units declared within the program, @sa Graphic3d_TextureSetBits 656 Graphic3d_RenderTransparentMethod myOitOutput; //!< flag indicating that Fragment Shader includes OIT outputs 657 Standard_Boolean myHasAlphaTest; //!< flag indicating that Fragment Shader should perform alpha-test 658 Standard_Boolean myHasTessShader; //!< flag indicating that program defines tessellation stage 659 660 protected: 661 662 Standard_Size myCurrentState[OpenGl_UniformStateType_NB]; //!< defines last modification for variables of each state type 663 664 //! Stores locations of OCCT state uniform variables. 665 OpenGl_ShaderUniformLocation myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES]; 666 667 }; 668 669 template<class T> 670 struct OpenGl_VariableSetter : public OpenGl_SetterInterface 671 { SetOpenGl_VariableSetter672 virtual void Set (const Handle(OpenGl_Context)& theCtx, 673 const Handle(Graphic3d_ShaderVariable)& theVariable, 674 OpenGl_ShaderProgram* theProgram) 675 { 676 theProgram->SetUniform (theCtx, 677 theVariable->Name().ToCString(), 678 theVariable->Value()->As<T>()); 679 } 680 }; 681 682 namespace OpenGl_HashMapInitializer 683 { 684 template<class K, class V> 685 struct MapListOfType 686 { 687 NCollection_DataMap<K, V> myDictionary; 688 MapListOfTypeOpenGl_HashMapInitializer::MapListOfType689 MapListOfType (K theKey, V theValue) 690 { 691 myDictionary.Bind (theKey, theValue); 692 } 693 operator ()OpenGl_HashMapInitializer::MapListOfType694 MapListOfType& operator() (K theKey, V theValue) 695 { 696 myDictionary.Bind (theKey, theValue); 697 return *this; 698 } 699 operator const NCollection_DataMap<K,V>&OpenGl_HashMapInitializer::MapListOfType700 operator const NCollection_DataMap<K, V>& () const 701 { 702 return myDictionary; 703 } 704 }; 705 706 template<class K, class V> CreateListOf(K theKey,V theValue)707 MapListOfType<K, V> CreateListOf (K theKey, V theValue) 708 { 709 return MapListOfType<K, V> (theKey, theValue); 710 } 711 } 712 713 #endif // _OpenGl_ShaderProgram_Header 714