30f0ad28 |
1 | // Created on: 2013-09-26 |
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 _OpenGl_ShaderManager_HeaderFile |
17 | #define _OpenGl_ShaderManager_HeaderFile |
18 | |
19 | #include <Graphic3d_ShaderProgram_Handle.hxx> |
20 | |
21 | #include <NCollection_DataMap.hxx> |
22 | #include <NCollection_Sequence.hxx> |
23 | |
24 | #include <Handle_OpenGl_ShaderManager.hxx> |
25 | #include <OpenGl_ShaderProgram.hxx> |
26 | #include <OpenGl_ShaderStates.hxx> |
8625ef7e |
27 | #include <OpenGl_AspectFace.hxx> |
28 | #include <OpenGl_AspectLine.hxx> |
29 | #include <OpenGl_AspectText.hxx> |
30 | #include <OpenGl_AspectMarker.hxx> |
31 | #include <OpenGl_Texture.hxx> |
32 | #include <Visual3d_TypeOfModel.hxx> |
30f0ad28 |
33 | |
e6804ff7 |
34 | class OpenGl_View; |
35 | |
30f0ad28 |
36 | //! List of shader programs. |
37 | typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList; |
38 | |
39 | //! Map to declare per-program states of OCCT materials. |
40 | typedef NCollection_DataMap<Handle(OpenGl_ShaderProgram), OpenGl_MaterialState> OpenGl_MaterialStates; |
41 | |
8625ef7e |
42 | //! Standard GLSL program combination bits. |
43 | enum OpenGl_ProgramOptions |
44 | { |
45 | OpenGl_PO_ClipPlanes = 0x01, //!< handle clipping planes |
46 | OpenGl_PO_Point = 0x02, //!< point marker |
47 | OpenGl_PO_VertColor = 0x04, //!< per-vertex color |
48 | OpenGl_PO_TextureRGB = 0x08, //!< handle RGB texturing |
49 | OpenGl_PO_TextureA = 0x10, //!< handle Alpha texturing |
50 | OpenGl_PO_NB = 0x20 //!< overall number of combinations |
51 | }; |
52 | |
30f0ad28 |
53 | //! This class is responsible for managing shader programs. |
54 | class OpenGl_ShaderManager : public Standard_Transient |
55 | { |
56 | friend class OpenGl_ShaderProgram; |
57 | |
58 | public: |
59 | |
60 | //! Creates new empty shader manager. |
61 | Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext); |
62 | |
63 | //! Releases resources of shader manager. |
64 | Standard_EXPORT virtual ~OpenGl_ShaderManager(); |
65 | |
392ac980 |
66 | //! Creates new shader program or re-use shared instance. |
67 | //! @param theProxy [IN] program definition |
68 | //! @param theShareKey [OUT] sharing key |
69 | //! @param theProgram [OUT] OpenGL program |
8625ef7e |
70 | //! @return true on success |
71 | Standard_EXPORT Standard_Boolean Create (const Handle(Graphic3d_ShaderProgram)& theProxy, |
72 | TCollection_AsciiString& theShareKey, |
73 | Handle(OpenGl_ShaderProgram)& theProgram); |
30f0ad28 |
74 | |
75 | //! Unregisters specified shader program. |
392ac980 |
76 | Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey, |
77 | Handle(OpenGl_ShaderProgram)& theProgram); |
30f0ad28 |
78 | |
79 | //! Returns list of registered shader programs. |
80 | Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const; |
81 | |
82 | //! Returns true if no program objects are registered in the manager. |
83 | Standard_EXPORT Standard_Boolean IsEmpty() const; |
84 | |
8625ef7e |
85 | //! Bind program for filled primitives rendering |
86 | Standard_Boolean BindProgram (const OpenGl_AspectFace* theAspect, |
87 | const Handle(OpenGl_Texture)& theTexture, |
88 | const Standard_Boolean theToLightOn, |
89 | const Standard_Boolean theHasVertColor, |
90 | const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
91 | { |
92 | if (!theCustomProgram.IsNull() |
93 | || myContext->caps->ffpEnable) |
94 | { |
95 | return bindProgramWithState (theCustomProgram, theAspect); |
96 | } |
97 | |
98 | const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor); |
99 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits); |
100 | return bindProgramWithState (aProgram, theAspect); |
101 | } |
30f0ad28 |
102 | |
8625ef7e |
103 | //! Bind program for line rendering |
104 | Standard_Boolean BindProgram (const OpenGl_AspectLine* theAspect, |
105 | const Handle(OpenGl_Texture)& theTexture, |
106 | const Standard_Boolean theToLightOn, |
107 | const Standard_Boolean theHasVertColor, |
108 | const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
109 | { |
110 | if (!theCustomProgram.IsNull() |
111 | || myContext->caps->ffpEnable) |
112 | { |
113 | return bindProgramWithState (theCustomProgram, theAspect); |
114 | } |
115 | |
116 | const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor); |
117 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits); |
118 | return bindProgramWithState (aProgram, theAspect); |
119 | } |
30f0ad28 |
120 | |
8625ef7e |
121 | //! Bind program for point rendering |
122 | Standard_Boolean BindProgram (const OpenGl_AspectMarker* theAspect, |
123 | const Handle(OpenGl_Texture)& theTexture, |
124 | const Standard_Boolean theToLightOn, |
125 | const Standard_Boolean theHasVertColor, |
126 | const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
127 | { |
128 | if (!theCustomProgram.IsNull() |
129 | || myContext->caps->ffpEnable) |
130 | { |
131 | return bindProgramWithState (theCustomProgram, theAspect); |
132 | } |
133 | |
134 | const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point; |
135 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits); |
136 | return bindProgramWithState (aProgram, theAspect); |
137 | } |
138 | |
139 | //! Bind program for rendering alpha-textured font. |
140 | Standard_Boolean BindProgram (const OpenGl_AspectText* theAspect, |
141 | const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
142 | { |
143 | if (!theCustomProgram.IsNull() |
144 | || myContext->caps->ffpEnable) |
145 | { |
146 | return bindProgramWithState (theCustomProgram, theAspect); |
147 | } |
148 | |
149 | if (myFontProgram.IsNull()) |
150 | { |
151 | prepareStdProgramFont(); |
152 | } |
153 | return bindProgramWithState (myFontProgram, theAspect); |
154 | } |
30f0ad28 |
155 | |
156 | public: |
157 | |
158 | //! Returns current state of OCCT light sources. |
159 | Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const; |
160 | |
161 | //! Updates state of OCCT light sources. |
162 | Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights); |
163 | |
164 | //! Pushes current state of OCCT light sources to specified program. |
165 | Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
166 | |
167 | public: |
168 | |
169 | //! Returns current state of OCCT projection transform. |
170 | Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const; |
171 | |
172 | //! Updates state of OCCT projection transform. |
b5ac8292 |
173 | Standard_EXPORT void UpdateProjectionStateTo (const Tmatrix3* theProjectionMatrix); |
30f0ad28 |
174 | |
175 | //! Reverts state of OCCT projection transform. |
b5ac8292 |
176 | Standard_EXPORT void RevertProjectionStateTo (const Tmatrix3* theProjectionMatrix); |
30f0ad28 |
177 | |
178 | //! Pushes current state of OCCT projection transform to specified program. |
179 | Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
180 | |
181 | public: |
182 | |
183 | //! Returns current state of OCCT model-world transform. |
184 | Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const; |
185 | |
186 | //! Updates state of OCCT model-world transform. |
b5ac8292 |
187 | Standard_EXPORT void UpdateModelWorldStateTo (const Tmatrix3* theModelWorldMatrix); |
30f0ad28 |
188 | |
189 | //! Reverts state of OCCT model-world transform. |
b5ac8292 |
190 | Standard_EXPORT void RevertModelWorldStateTo (const Tmatrix3* theModelWorldMatrix); |
30f0ad28 |
191 | |
192 | //! Pushes current state of OCCT model-world transform to specified program. |
193 | Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
194 | |
195 | public: |
196 | |
197 | //! Returns current state of OCCT world-view transform. |
198 | Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const; |
199 | |
200 | //! Updates state of OCCT world-view transform. |
b5ac8292 |
201 | Standard_EXPORT void UpdateWorldViewStateTo (const Tmatrix3* theWorldViewMatrix); |
30f0ad28 |
202 | |
203 | //! Reverts state of OCCT world-view transform. |
b5ac8292 |
204 | Standard_EXPORT void RevertWorldViewStateTo (const Tmatrix3* theWorldViewMatrix); |
30f0ad28 |
205 | |
206 | //! Pushes current state of OCCT world-view transform to specified program. |
207 | Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
208 | |
209 | public: |
210 | |
211 | //! Updates state of OCCT clipping planes. |
212 | Standard_EXPORT void UpdateClippingState(); |
213 | |
214 | //! Reverts state of OCCT clipping planes. |
215 | Standard_EXPORT void RevertClippingState(); |
216 | |
217 | //! Pushes current state of OCCT clipping planes to specified program. |
218 | Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
219 | |
220 | public: |
221 | |
222 | //! Resets state of OCCT material for all programs. |
223 | Standard_EXPORT void ResetMaterialStates(); |
224 | |
225 | //! Updates state of OCCT material for specified program. |
226 | Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram, |
227 | const OpenGl_Element* theAspect); |
228 | |
229 | //! Pushes current state of OCCT material to specified program. |
230 | Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
231 | |
232 | //! Returns current state of OCCT material for specified program. |
233 | Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
234 | |
235 | public: |
236 | |
237 | //! Pushes current state of OCCT graphics parameters to specified program. |
238 | Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
239 | |
392ac980 |
240 | public: |
241 | |
242 | //! Overwrites context |
243 | void SetContext (OpenGl_Context* theCtx) |
244 | { |
245 | myContext = theCtx; |
246 | } |
247 | |
8625ef7e |
248 | //! Sets shading model. |
249 | void SetShadingModel(const Visual3d_TypeOfModel theModel) |
250 | { |
251 | myLightPrograms = theModel == Visual3d_TOM_FRAGMENT |
252 | ? myLightPrograms = myPhongPrograms |
253 | : myLightPrograms = myGouraudPrograms; |
254 | myShadingModel = theModel; |
255 | } |
256 | |
e6804ff7 |
257 | //! Sets last view manger used with. |
258 | //! Helps to handle matrix states in multi-view configurations. |
259 | void SetLastView (const OpenGl_View* theLastView) |
260 | { |
261 | myLastView = theLastView; |
262 | } |
263 | |
264 | //! Returns true when provided view is the same as cached one. |
265 | bool IsSameView (const OpenGl_View* theView) const |
266 | { |
267 | return myLastView == theView; |
268 | } |
269 | |
30f0ad28 |
270 | protected: |
271 | |
8625ef7e |
272 | //! Define program bits. |
273 | Standard_Integer getProgramBits (const Handle(OpenGl_Texture)& theTexture, |
274 | const Standard_Boolean theHasVertColor) |
275 | { |
276 | Standard_Integer aBits = 0; |
277 | if (myContext->Clipping().IsClippingOrCappingOn()) |
278 | { |
279 | aBits |= OpenGl_PO_ClipPlanes; |
280 | } |
281 | if (!theTexture.IsNull()) |
282 | { |
283 | // GL_RED to be handled |
284 | aBits |= theTexture->GetFormat() == GL_ALPHA ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB; |
285 | } |
286 | if (theHasVertColor) |
287 | { |
288 | aBits |= OpenGl_PO_VertColor; |
289 | } |
290 | return aBits; |
291 | } |
292 | |
293 | //! Prepare standard GLSL program. |
294 | Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn, |
295 | const Standard_Integer theBits) |
296 | { |
297 | if (theToLightOn) |
298 | { |
299 | Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms[theBits]; |
300 | if (aProgram.IsNull()) |
301 | { |
302 | prepareStdProgramLight (aProgram, theBits); |
303 | } |
304 | return aProgram; |
305 | } |
306 | |
307 | Handle(OpenGl_ShaderProgram)& aProgram = myFlatPrograms[theBits]; |
308 | if (aProgram.IsNull()) |
309 | { |
310 | prepareStdProgramFlat (aProgram, theBits); |
311 | } |
312 | return aProgram; |
313 | } |
314 | |
315 | //! Prepare standard GLSL program for textured font. |
316 | Standard_EXPORT Standard_Boolean prepareStdProgramFont(); |
317 | |
318 | //! Prepare standard GLSL program without lighting. |
319 | Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram, |
320 | const Standard_Integer theBits); |
321 | |
322 | //! Prepare standard GLSL program with lighting. |
323 | Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram, |
324 | const Standard_Integer theBits) |
325 | { |
326 | return myShadingModel == Visual3d_TOM_FRAGMENT |
327 | ? prepareStdProgramPhong (theProgram, theBits) |
328 | : prepareStdProgramGouraud (theProgram, theBits); |
329 | } |
330 | |
331 | //! Prepare standard GLSL program with per-vertex lighting. |
332 | Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram, |
333 | const Standard_Integer theBits); |
334 | |
335 | //! Prepare standard GLSL program with per-pixel lighting. |
336 | Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram, |
337 | const Standard_Integer theBits); |
338 | |
339 | //! Bind specified program to current context and apply state. |
340 | Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram, |
341 | const OpenGl_Element* theAspect); |
342 | |
343 | protected: |
344 | |
345 | Visual3d_TypeOfModel myShadingModel; //!< lighting shading model |
346 | OpenGl_ShaderProgramList myProgramList; //!< The list of shader programs |
347 | Handle(OpenGl_ShaderProgram)* myLightPrograms; //!< pointer to active lighting programs matrix, depending on shading model and lights configuration |
348 | Handle(OpenGl_ShaderProgram) myGouraudPrograms[OpenGl_PO_NB]; //!< matrix with per-vertex lighting programs |
349 | Handle(OpenGl_ShaderProgram) myPhongPrograms [OpenGl_PO_NB]; //!< matrix with per-fragment lighting programs |
350 | Handle(OpenGl_ShaderProgram) myFlatPrograms [OpenGl_PO_NB]; //!< programs matrix without lighting |
351 | Handle(OpenGl_ShaderProgram) myFontProgram; //!< standard program for textured text |
352 | |
353 | OpenGl_Context* myContext; //!< OpenGL context |
354 | |
355 | protected: |
356 | |
357 | OpenGl_MaterialStates myMaterialStates; //!< Per-program state of OCCT material |
358 | OpenGl_ProjectionState myProjectionState; //!< State of OCCT projection transformation |
359 | OpenGl_ModelWorldState myModelWorldState; //!< State of OCCT model-world transformation |
360 | OpenGl_WorldViewState myWorldViewState; //!< State of OCCT world-view transformation |
361 | OpenGl_LightSourceState myClippingState; //!< State of OCCT clipping planes |
362 | OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources |
30f0ad28 |
363 | |
364 | private: |
365 | |
8625ef7e |
366 | const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with |
367 | |
368 | public: |
369 | |
370 | DEFINE_STANDARD_RTTI (OpenGl_ShaderManager) |
371 | |
30f0ad28 |
372 | }; |
373 | |
374 | #endif // _OpenGl_ShaderManager_HeaderFile |