30f0ad28 |
1 | // Created on: 2013-09-20 |
2 | // Created by: Denis BOGOLEPOV |
d5f74e42 |
3 | // Copyright (c) 2013-2014 OPEN CASCADE SAS |
30f0ad28 |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
30f0ad28 |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
30f0ad28 |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
30f0ad28 |
15 | |
16 | #ifndef _Graphic3d_ShaderProgram_HeaderFile |
17 | #define _Graphic3d_ShaderProgram_HeaderFile |
18 | |
494782f6 |
19 | #include <Graphic3d_ShaderObject.hxx> |
30f0ad28 |
20 | #include <Graphic3d_ShaderVariable.hxx> |
c04c30b3 |
21 | #include <Graphic3d_TextureParams.hxx> |
30f0ad28 |
22 | #include <NCollection_Sequence.hxx> |
23 | |
24 | //! List of shader objects. |
25 | typedef NCollection_Sequence<Handle(Graphic3d_ShaderObject)> Graphic3d_ShaderObjectList; |
26 | |
27 | //! List of custom uniform shader variables. |
28 | typedef NCollection_Sequence<Handle(Graphic3d_ShaderVariable)> Graphic3d_ShaderVariableList; |
29 | |
30 | //! This class is responsible for managing shader programs. |
31 | class Graphic3d_ShaderProgram : public Standard_Transient |
32 | { |
392ac980 |
33 | |
34 | public: |
35 | |
36 | //! The list of built-in GLSL programs |
37 | enum ShaderName |
38 | { |
39 | ShaderName_UNKNOWN, //!< undefined program |
40 | ShaderName_Phong //!< per-pixel lighting (Phong shading) |
41 | }; |
42 | |
30f0ad28 |
43 | public: |
44 | |
45 | //! Creates new empty program object. |
46 | Standard_EXPORT Graphic3d_ShaderProgram(); |
47 | |
392ac980 |
48 | //! Creates program object from pre-defined shaders. |
49 | //! Raises Standard_Failure exception if shader resources are unavailable. |
50 | Standard_EXPORT Graphic3d_ShaderProgram (const Graphic3d_ShaderProgram::ShaderName theName); |
51 | |
30f0ad28 |
52 | //! Releases resources of program object. |
53 | Standard_EXPORT virtual ~Graphic3d_ShaderProgram(); |
54 | |
55 | //! Releases resources of program object. |
56 | Standard_EXPORT void Destroy() const; |
57 | |
58 | //! Checks if the program object is valid or not. |
59 | Standard_EXPORT virtual Standard_Boolean IsDone() const; |
60 | |
61 | //! Returns unique ID used to manage resource in graphic driver. |
62 | const TCollection_AsciiString& GetId() const { return myID; } |
63 | |
b86bb3df |
64 | //! Returns GLSL header (version code and extensions). |
65 | const TCollection_AsciiString& Header() const { return myHeader; } |
66 | |
67 | //! Setup GLSL header containing language version code and used extensions. |
68 | //! Will be prepended to the very beginning of the source code. |
69 | //! Example: |
70 | //! @code |
71 | //! #version 300 es |
72 | //! #extension GL_ARB_bindless_texture : require |
73 | //! @endcode |
74 | void SetHeader (const TCollection_AsciiString& theHeader) { myHeader = theHeader; } |
75 | |
30f0ad28 |
76 | //! Attaches shader object to the program object. |
77 | Standard_EXPORT Standard_Boolean AttachShader (const Handle(Graphic3d_ShaderObject)& theShader); |
78 | |
79 | //! Detaches shader object from the program object. |
80 | Standard_EXPORT Standard_Boolean DetachShader (const Handle(Graphic3d_ShaderObject)& theShader); |
81 | |
82 | //! Returns list of attached shader objects. |
83 | const Graphic3d_ShaderObjectList& ShaderObjects() const { return myShaderObjects; } |
84 | |
85 | //! Returns list of custom uniform variables. |
86 | const Graphic3d_ShaderVariableList& Variables() const { return myVariables; } |
87 | |
88 | //! Pushes custom uniform variable to the program. |
89 | template<class T> |
90 | Standard_Boolean PushVariable (const TCollection_AsciiString& theName, |
91 | const T& theValue); |
92 | |
93 | //! Removes all custom uniform variables from the program. |
94 | Standard_EXPORT void ClearVariables(); |
95 | |
392ac980 |
96 | public: |
97 | |
98 | //! The path to GLSL programs determined from CSF_ShadersDirectory or CASROOT environment variables. |
99 | //! @return the root folder with default GLSL programs. |
100 | Standard_EXPORT static const TCollection_AsciiString& ShadersFolder(); |
101 | |
30f0ad28 |
102 | public: |
103 | |
ec357c5c |
104 | DEFINE_STANDARD_RTTI (Graphic3d_ShaderProgram, Standard_Transient) |
30f0ad28 |
105 | |
106 | private: |
107 | |
108 | TCollection_AsciiString myID; //!< The unique identifier of program object. |
109 | Graphic3d_ShaderObjectList myShaderObjects; //!< the list of attached shader objects. |
110 | Graphic3d_ShaderVariableList myVariables; //!< the list of custom uniform variables. |
b86bb3df |
111 | TCollection_AsciiString myHeader; //!< GLSL header with version code and used extensions |
30f0ad28 |
112 | |
113 | }; |
114 | |
494782f6 |
115 | DEFINE_STANDARD_HANDLE (Graphic3d_ShaderProgram, Standard_Transient) |
116 | |
30f0ad28 |
117 | // ======================================================================= |
118 | // function : PushVariable |
119 | // purpose : Pushes custom uniform variable to the program |
120 | // ======================================================================= |
121 | template<class T> inline |
122 | Standard_Boolean Graphic3d_ShaderProgram::PushVariable (const TCollection_AsciiString& theName, |
123 | const T& theValue) |
124 | { |
125 | Handle(Graphic3d_ShaderVariable) aVariable = Graphic3d_ShaderVariable::Create (theName, theValue); |
126 | if (aVariable.IsNull() || !aVariable->IsDone()) |
127 | { |
128 | return Standard_False; |
129 | } |
130 | |
131 | myVariables.Append (aVariable); |
132 | return Standard_True; |
133 | } |
134 | |
135 | #endif |