5a2650a6cc888365d75f9b787b034c158908b9b8
[occt.git] / src / OpenGl / OpenGl_ShaderObject.hxx
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_ShaderObject_Header
17 #define _OpenGl_ShaderObject_Header
18
19 #include <Graphic3d_ShaderObject.hxx>
20 #include <OpenGl_GlCore20.hxx>
21 #include <OpenGl_Resource.hxx>
22 #include <Quantity_Date.hxx>
23
24 //! Wrapper for OpenGL shader object.
25 class OpenGl_ShaderObject : public OpenGl_Resource
26 {
27   DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderObject, OpenGl_Resource)
28   friend class OpenGl_ShaderProgram;
29 public:
30
31   //! Non-valid shader name.
32   static const GLuint NO_SHADER = 0;
33
34 public:
35
36   //! Structure defining shader uniform or in/out variable.
37   struct ShaderVariable
38   {
39     TCollection_AsciiString Name;   //!< variable name
40     Standard_Integer        Stages; //!< active stages as Graphic3d_TypeOfShaderObject bits;
41                                     //!  for in/out variables, intermediate stages will be automatically filled
42
43     //! Create new shader variable.
44     ShaderVariable (const TCollection_AsciiString& theVarName, Standard_Integer theShaderStageBits) : Name (theVarName), Stages (theShaderStageBits) {}
45
46     //! Empty constructor.
47     ShaderVariable() : Stages (0) {}
48   };
49
50   //! List of variable of shader program.
51   typedef NCollection_Sequence<ShaderVariable> ShaderVariableList;
52
53   //! This is a preprocessor for Graphic3d_ShaderObject::CreateFromSource() function.
54   //! Creates a new shader object from specified source according to list of uniforms and in/out variables.
55   //! @param theSource      shader object source code to modify
56   //! @param theType        shader object type to create
57   //! @param theUniforms    list of uniform variables
58   //! @param theStageInOuts list of stage in/out variables
59   //! @param theInName      name of input  variables block;
60   //!                       can be empty for accessing each variable without block prefix
61   //!                       (mandatory for stages accessing both inputs and outputs)
62   //! @param theOutName     name of output variables block;
63   //!                       can be empty for accessing each variable without block prefix
64   //!                       (mandatory for stages accessing both inputs and outputs)
65   //! @param theNbGeomInputVerts number of geometry shader input vertexes
66   Standard_EXPORT static Handle(Graphic3d_ShaderObject) CreateFromSource (TCollection_AsciiString& theSource,
67                                                                           Graphic3d_TypeOfShaderObject theType,
68                                                                           const ShaderVariableList& theUniforms,
69                                                                           const ShaderVariableList& theStageInOuts,
70                                                                           const TCollection_AsciiString& theInName  = TCollection_AsciiString(),
71                                                                           const TCollection_AsciiString& theOutName = TCollection_AsciiString(),
72                                                                           Standard_Integer theNbGeomInputVerts = 0);
73
74 public:
75
76   //! Creates uninitialized shader object.
77   Standard_EXPORT OpenGl_ShaderObject (GLenum theType);
78
79   //! Releases resources of shader object.
80   Standard_EXPORT virtual ~OpenGl_ShaderObject();
81
82   //! Loads shader source code.
83   Standard_EXPORT Standard_Boolean LoadSource (const Handle(OpenGl_Context)&  theCtx,
84                                                const TCollection_AsciiString& theSource);
85
86   //! Compiles the shader object.
87   Standard_EXPORT Standard_Boolean Compile (const Handle(OpenGl_Context)& theCtx);
88
89   //! Wrapper for compiling shader object with verbose printing on error.
90   //! @param theCtx bound OpenGL context
91   //! @param theSource source code to load
92   //! @param theIsVerbose flag to print log on error
93   //! @param theToPrintSource flag to print source code on error
94   Standard_EXPORT Standard_Boolean LoadAndCompile (const Handle(OpenGl_Context)& theCtx,
95                                                    const TCollection_AsciiString& theSource,
96                                                    bool theIsVerbose = true,
97                                                    bool theToPrintSource = true);
98
99   //! Fetches information log of the last compile operation.
100   Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
101                                                  TCollection_AsciiString&      theLog);
102
103   //! Creates new empty shader object of specified type.
104   Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
105
106   //! Destroys shader object.
107   Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
108
109   //! Returns estimated GPU memory usage - not implemented.
110   virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE { return 0; }
111
112   //! Returns type of shader object.
113   GLenum Type() const { return myType; }
114
115 public:
116
117   //! Update the shader object from external file in the following way:
118   //! 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.
119   //! 2) If external file exists and it has the same timestamp as   myDumpDate, nothing will be done      and FALSE will be returned.
120   //! 3) If external file exists and it has    newer timestamp than myDumpDate, shader  will be recompiled and TRUE will be returned.
121   //! @param theCtx OpenGL context bound to this working thread
122   //! @param theId  GLSL program id to define file name
123   //! @param theFolder folder to store files
124   //! @param theToBeautify flag improving formatting (add extra newlines)
125   //! @param theToReset when TRUE, existing dumps will be overridden
126   Standard_EXPORT Standard_Boolean updateDebugDump (const Handle(OpenGl_Context)& theCtx,
127                                                     const TCollection_AsciiString& theId,
128                                                     const TCollection_AsciiString& theFolder,
129                                                     Standard_Boolean theToBeautify,
130                                                     Standard_Boolean theToReset);
131
132 protected:
133
134   Quantity_Date myDumpDate; //!< The recent date of the shader dump
135   GLenum        myType;     //!< Type of OpenGL shader object
136   GLuint        myShaderID; //!< Handle of OpenGL shader object
137
138 };
139
140 DEFINE_STANDARD_HANDLE(OpenGl_ShaderObject, OpenGl_Resource)
141
142 #endif // _OpenGl_ShaderObject_Header