0031313: Foundation Classes - Dump improvement for classes
[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 theId  GLSL program id to define file name
92   //! @param theSource source code to load
93   //! @param theIsVerbose flag to print log on error
94   //! @param theToPrintSource flag to print source code on error
95   Standard_EXPORT Standard_Boolean LoadAndCompile (const Handle(OpenGl_Context)& theCtx,
96                                                    const TCollection_AsciiString& theId,
97                                                    const TCollection_AsciiString& theSource,
98                                                    bool theIsVerbose = true,
99                                                    bool theToPrintSource = true);
100
101   //! Print source code of this shader object to messenger.
102   Standard_EXPORT void DumpSourceCode (const Handle(OpenGl_Context)& theCtx,
103                                        const TCollection_AsciiString& theId,
104                                        const TCollection_AsciiString& theSource) const;
105
106   //! Fetches information log of the last compile operation.
107   Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
108                                                  TCollection_AsciiString&      theLog);
109
110   //! Creates new empty shader object of specified type.
111   Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
112
113   //! Destroys shader object.
114   Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
115
116   //! Returns estimated GPU memory usage - not implemented.
117   virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE { return 0; }
118
119   //! Returns type of shader object.
120   GLenum Type() const { return myType; }
121
122 public:
123
124   //! Update the shader object from external file in the following way:
125   //! 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.
126   //! 2) If external file exists and it has the same timestamp as   myDumpDate, nothing will be done      and FALSE will be returned.
127   //! 3) If external file exists and it has    newer timestamp than myDumpDate, shader  will be recompiled and TRUE will be returned.
128   //! @param theCtx OpenGL context bound to this working thread
129   //! @param theId  GLSL program id to define file name
130   //! @param theFolder folder to store files
131   //! @param theToBeautify flag improving formatting (add extra newlines)
132   //! @param theToReset when TRUE, existing dumps will be overridden
133   Standard_EXPORT Standard_Boolean updateDebugDump (const Handle(OpenGl_Context)& theCtx,
134                                                     const TCollection_AsciiString& theId,
135                                                     const TCollection_AsciiString& theFolder,
136                                                     Standard_Boolean theToBeautify,
137                                                     Standard_Boolean theToReset);
138
139 protected:
140
141   Quantity_Date myDumpDate; //!< The recent date of the shader dump
142   GLenum        myType;     //!< Type of OpenGL shader object
143   GLuint        myShaderID; //!< Handle of OpenGL shader object
144
145 };
146
147 DEFINE_STANDARD_HANDLE(OpenGl_ShaderObject, OpenGl_Resource)
148
149 #endif // _OpenGl_ShaderObject_Header