0029337: Visualization, TKOpenGl - visual artifacts on Intel Broadwell GPU
[occt.git] / src / Graphic3d / Graphic3d_ShaderProgram.hxx
CommitLineData
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
4a535d3f 19#include <Graphic3d_ShaderAttribute.hxx>
494782f6 20#include <Graphic3d_ShaderObject.hxx>
30f0ad28 21#include <Graphic3d_ShaderVariable.hxx>
c04c30b3 22#include <Graphic3d_TextureParams.hxx>
30f0ad28 23#include <NCollection_Sequence.hxx>
24
25//! List of shader objects.
26typedef NCollection_Sequence<Handle(Graphic3d_ShaderObject)> Graphic3d_ShaderObjectList;
27
28//! List of custom uniform shader variables.
29typedef NCollection_Sequence<Handle(Graphic3d_ShaderVariable)> Graphic3d_ShaderVariableList;
30
4a535d3f 31//! List of custom vertex shader attrubures
32typedef NCollection_Sequence<Handle(Graphic3d_ShaderAttribute)> Graphic3d_ShaderAttributeList;
33
30f0ad28 34//! This class is responsible for managing shader programs.
35class Graphic3d_ShaderProgram : public Standard_Transient
36{
daf73ab7 37 DEFINE_STANDARD_RTTIEXT(Graphic3d_ShaderProgram, Standard_Transient)
38public:
39
40 //! Default value of THE_MAX_LIGHTS macros within GLSL program (see Declarations.glsl).
41 static const Standard_Integer THE_MAX_LIGHTS_DEFAULT = 8;
42
43 //! Default value of THE_MAX_CLIP_PLANES macros within GLSL program (see Declarations.glsl).
44 static const Standard_Integer THE_MAX_CLIP_PLANES_DEFAULT = 8;
392ac980 45
b17e5bae 46 //! Default value of THE_NB_FRAG_OUTPUTS macros within GLSL program (see Declarations.glsl).
47 static const Standard_Integer THE_NB_FRAG_OUTPUTS = 1;
48
392ac980 49public:
50
30f0ad28 51 //! Creates new empty program object.
52 Standard_EXPORT Graphic3d_ShaderProgram();
53
54 //! Releases resources of program object.
55 Standard_EXPORT virtual ~Graphic3d_ShaderProgram();
56
30f0ad28 57 //! Checks if the program object is valid or not.
58 Standard_EXPORT virtual Standard_Boolean IsDone() const;
59
60 //! Returns unique ID used to manage resource in graphic driver.
61 const TCollection_AsciiString& GetId() const { return myID; }
62
b86bb3df 63 //! Returns GLSL header (version code and extensions).
64 const TCollection_AsciiString& Header() const { return myHeader; }
65
66 //! Setup GLSL header containing language version code and used extensions.
67 //! Will be prepended to the very beginning of the source code.
68 //! Example:
69 //! @code
70 //! #version 300 es
71 //! #extension GL_ARB_bindless_texture : require
72 //! @endcode
73 void SetHeader (const TCollection_AsciiString& theHeader) { myHeader = theHeader; }
74
daf73ab7 75 //! Append line to GLSL header.
76 void AppendToHeader (const TCollection_AsciiString& theHeaderLine)
77 {
78 if (!myHeader.IsEmpty())
79 {
80 myHeader += "\n";
81 }
82 myHeader += theHeaderLine;
83 }
84
85 //! Return the length of array of light sources (THE_MAX_LIGHTS),
86 //! to be used for initialization occLightSources.
87 //! Default value is THE_MAX_LIGHTS_DEFAULT.
88 Standard_Integer NbLightsMax() const { return myNbLightsMax; }
89
90 //! Specify the length of array of light sources (THE_MAX_LIGHTS).
91 void SetNbLightsMax (Standard_Integer theNbLights) { myNbLightsMax = theNbLights; }
92
93 //! Return the length of array of clipping planes (THE_MAX_CLIP_PLANES),
94 //! to be used for initialization occClipPlaneEquations.
95 //! Default value is THE_MAX_CLIP_PLANES_DEFAULT.
96 Standard_Integer NbClipPlanesMax() const { return myNbClipPlanesMax; }
97
98 //! Specify the length of array of clipping planes (THE_MAX_CLIP_PLANES).
99 void SetNbClipPlanesMax (Standard_Integer theNbPlanes) { myNbClipPlanesMax = theNbPlanes; }
100
30f0ad28 101 //! Attaches shader object to the program object.
102 Standard_EXPORT Standard_Boolean AttachShader (const Handle(Graphic3d_ShaderObject)& theShader);
103
104 //! Detaches shader object from the program object.
105 Standard_EXPORT Standard_Boolean DetachShader (const Handle(Graphic3d_ShaderObject)& theShader);
106
107 //! Returns list of attached shader objects.
108 const Graphic3d_ShaderObjectList& ShaderObjects() const { return myShaderObjects; }
109
4a535d3f 110 //! The list of currently pushed but not applied custom uniform variables.
111 //! This list is automatically cleared after applying to GLSL program.
30f0ad28 112 const Graphic3d_ShaderVariableList& Variables() const { return myVariables; }
113
4a535d3f 114 //! Return the list of custom vertex attributes.
115 const Graphic3d_ShaderAttributeList& VertexAttributes() const { return myAttributes; }
116
117 //! Assign the list of custom vertex attributes.
118 //! Should be done before GLSL program initialization.
119 Standard_EXPORT void SetVertexAttributes (const Graphic3d_ShaderAttributeList& theAttributes);
120
b17e5bae 121 //! Returns the number (1+) of Fragment Shader outputs to be written to
122 //! (more than 1 can be in case of multiple draw buffers); 1 by default.
123 Standard_Integer NbFragmentOutputs() const { return myNbFragOutputs; }
124
125 //! Sets the number of Fragment Shader outputs to be written to.
126 //! Should be done before GLSL program initialization.
127 void SetNbFragmentOutputs (const Standard_Integer theNbOutputs) { myNbFragOutputs = theNbOutputs; }
128
129 //! Return true if Fragment Shader color should output the weighted OIT coverage; FALSE by default.
130 Standard_Boolean HasWeightOitOutput() const { return myHasWeightOitOutput; }
131
132 //! Set if Fragment Shader color should output the weighted OIT coverage.
133 //! Note that weighted OIT also requires at least 2 Fragment Outputs (color + coverage).
134 void SetWeightOitOutput (Standard_Boolean theOutput) { myHasWeightOitOutput = theOutput; }
135
30f0ad28 136 //! Pushes custom uniform variable to the program.
4a535d3f 137 //! The list of pushed variables is automatically cleared after applying to GLSL program.
138 //! Thus after program recreation even unchanged uniforms should be pushed anew.
30f0ad28 139 template<class T>
140 Standard_Boolean PushVariable (const TCollection_AsciiString& theName,
141 const T& theValue);
142
143 //! Removes all custom uniform variables from the program.
144 Standard_EXPORT void ClearVariables();
145
4a535d3f 146 //! Pushes float uniform.
147 Standard_Boolean PushVariableFloat (const TCollection_AsciiString& theName, const float theValue) { return PushVariable (theName, theValue); }
148
149 //! Pushes vec2 uniform.
150 Standard_Boolean PushVariableVec2 (const TCollection_AsciiString& theName, const Graphic3d_Vec2& theValue) { return PushVariable (theName, theValue); }
151
152 //! Pushes vec3 uniform.
153 Standard_Boolean PushVariableVec3 (const TCollection_AsciiString& theName, const Graphic3d_Vec3& theValue) { return PushVariable (theName, theValue); }
154
155 //! Pushes vec4 uniform.
156 Standard_Boolean PushVariableVec4 (const TCollection_AsciiString& theName, const Graphic3d_Vec4& theValue) { return PushVariable (theName, theValue); }
157
158 //! Pushes int uniform.
159 Standard_Boolean PushVariableInt (const TCollection_AsciiString& theName, const int theValue) { return PushVariable (theName, theValue); }
160
161 //! Pushes vec2i uniform.
162 Standard_Boolean PushVariableVec2i (const TCollection_AsciiString& theName, const Graphic3d_Vec2i& theValue) { return PushVariable (theName, theValue); }
163
164 //! Pushes vec3i uniform.
165 Standard_Boolean PushVariableVec3i (const TCollection_AsciiString& theName, const Graphic3d_Vec3i& theValue) { return PushVariable (theName, theValue); }
166
167 //! Pushes vec4i uniform.
168 Standard_Boolean PushVariableVec4i (const TCollection_AsciiString& theName, const Graphic3d_Vec4i& theValue) { return PushVariable (theName, theValue); }
169
30f0ad28 170public:
171
392ac980 172 //! The path to GLSL programs determined from CSF_ShadersDirectory or CASROOT environment variables.
173 //! @return the root folder with default GLSL programs.
174 Standard_EXPORT static const TCollection_AsciiString& ShadersFolder();
175
30f0ad28 176private:
177
4a535d3f 178 TCollection_AsciiString myID; //!< the unique identifier of program object
179 Graphic3d_ShaderObjectList myShaderObjects; //!< the list of attached shader objects
180 Graphic3d_ShaderVariableList myVariables; //!< the list of custom uniform variables
181 Graphic3d_ShaderAttributeList myAttributes; //!< the list of custom vertex attributes
182 TCollection_AsciiString myHeader; //!< GLSL header with version code and used extensions
daf73ab7 183 Standard_Integer myNbLightsMax; //!< length of array of light sources (THE_MAX_LIGHTS)
184 Standard_Integer myNbClipPlanesMax; //!< length of array of clipping planes (THE_MAX_CLIP_PLANES)
b17e5bae 185 Standard_Integer myNbFragOutputs; //!< length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS)
186 Standard_Boolean myHasWeightOitOutput; //!< flag indicating that Fragment Shader includes weighted OIT coverage
30f0ad28 187
188};
189
494782f6 190DEFINE_STANDARD_HANDLE (Graphic3d_ShaderProgram, Standard_Transient)
191
30f0ad28 192// =======================================================================
193// function : PushVariable
194// purpose : Pushes custom uniform variable to the program
195// =======================================================================
196template<class T> inline
197Standard_Boolean Graphic3d_ShaderProgram::PushVariable (const TCollection_AsciiString& theName,
198 const T& theValue)
199{
200 Handle(Graphic3d_ShaderVariable) aVariable = Graphic3d_ShaderVariable::Create (theName, theValue);
201 if (aVariable.IsNull() || !aVariable->IsDone())
202 {
203 return Standard_False;
204 }
205
206 myVariables.Append (aVariable);
207 return Standard_True;
208}
209
210#endif