0024437: Visualization - silhouette edges based on OpenGL
[occt.git] / src / OpenGl / OpenGl_ShaderManager.cxx
CommitLineData
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#include <typeinfo>
17
e135a155 18#include <Graphic3d_TextureParams.hxx>
30f0ad28 19#include <OpenGl_AspectFace.hxx>
20#include <OpenGl_AspectLine.hxx>
21#include <OpenGl_AspectMarker.hxx>
22#include <OpenGl_AspectText.hxx>
25c35042 23#include <OpenGl_ClippingIterator.hxx>
30f0ad28 24#include <OpenGl_Context.hxx>
25#include <OpenGl_ShaderManager.hxx>
26#include <OpenGl_ShaderProgram.hxx>
98b15dbf 27#include <OpenGl_VertexBufferCompat.hxx>
0adbd30f 28#include <OpenGl_Workspace.hxx>
30f0ad28 29
ac116c22 30#include <TCollection_ExtendedString.hxx>
31
92efcf78 32IMPLEMENT_STANDARD_RTTIEXT(OpenGl_ShaderManager,Standard_Transient)
33
8625ef7e 34namespace
35{
992ed6b3 36 //! Number specifying maximum number of light sources to prepare a GLSL program with unrolled loop.
37 const Standard_Integer THE_NB_UNROLLED_LIGHTS_MAX = 32;
38
39 //! Compute the size of array storing holding light sources definition.
40 static Standard_Integer roundUpMaxLightSources (Standard_Integer theNbLights)
41 {
42 Standard_Integer aMaxLimit = THE_NB_UNROLLED_LIGHTS_MAX;
43 for (; aMaxLimit < theNbLights; aMaxLimit *= 2) {}
44 return aMaxLimit;
45 }
8625ef7e 46
47#define EOL "\n"
48
79f4f036 49//! Compute TexCoord value in Vertex Shader
50const char THE_VARY_TexCoord_Trsf[] =
51 EOL" float aRotSin = occTextureTrsf_RotationSin();"
52 EOL" float aRotCos = occTextureTrsf_RotationCos();"
53 EOL" vec2 aTex2 = (occTexCoord.xy + occTextureTrsf_Translation()) * occTextureTrsf_Scale();"
54 EOL" vec2 aCopy = aTex2;"
55 EOL" aTex2.x = aCopy.x * aRotCos - aCopy.y * aRotSin;"
56 EOL" aTex2.y = aCopy.x * aRotSin + aCopy.y * aRotCos;"
57 EOL" TexCoord = vec4(aTex2, occTexCoord.zw);";
6c6aadb1 58
fd59283a 59//! Auxiliary function to flip gl_PointCoord vertically
60#define THE_VEC2_glPointCoord "vec2 (gl_PointCoord.x, 1.0 - gl_PointCoord.y)"
61
8625ef7e 62//! Auxiliary function to transform normal
63const char THE_FUNC_transformNormal[] =
64 EOL"vec3 transformNormal (in vec3 theNormal)"
65 EOL"{"
66 EOL" vec4 aResult = occWorldViewMatrixInverseTranspose"
67 EOL" * occModelWorldMatrixInverseTranspose"
68 EOL" * vec4 (theNormal, 0.0);"
69 EOL" return normalize (aResult.xyz);"
70 EOL"}";
71
72//! Global shader variable for color definition with lighting enabled.
73const char THE_FUNC_lightDef[] =
74 EOL"vec3 Ambient;" //!< Ambient contribution of light sources
75 EOL"vec3 Diffuse;" //!< Diffuse contribution of light sources
76 EOL"vec3 Specular;"; //!< Specular contribution of light sources
77
8625ef7e 78//! Function computes contribution of isotropic point light source
79const char THE_FUNC_pointLight[] =
80 EOL"void pointLight (in int theId,"
81 EOL" in vec3 theNormal,"
82 EOL" in vec3 theView,"
83 EOL" in vec3 thePoint,"
84 EOL" in bool theIsFront)"
85 EOL"{"
86 EOL" vec3 aLight = occLight_Position (theId).xyz;"
87 EOL" if (occLight_IsHeadlight (theId) == 0)"
88 EOL" {"
5f4bd4d4 89 EOL" aLight = vec3 (occWorldViewMatrix * vec4 (aLight, 1.0));"
8625ef7e 90 EOL" }"
91 EOL" aLight -= thePoint;"
92 EOL
93 EOL" float aDist = length (aLight);"
94 EOL" aLight = aLight * (1.0 / aDist);"
95 EOL
96 EOL" float anAtten = 1.0 / (occLight_ConstAttenuation (theId)"
97 EOL" + occLight_LinearAttenuation (theId) * aDist);"
98 EOL
99 EOL" vec3 aHalf = normalize (aLight + theView);"
100 EOL
101 EOL" vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal;"
102 EOL" float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));"
103 EOL" float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));"
104 EOL
105 EOL" float aSpecl = 0.0;"
106 EOL" if (aNdotL > 0.0)"
107 EOL" {"
108 EOL" aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess());"
109 EOL" }"
110 EOL
6ef0d6f1 111 EOL" Diffuse += occLight_Diffuse (theId).rgb * aNdotL * anAtten;"
112 EOL" Specular += occLight_Specular (theId).rgb * aSpecl * anAtten;"
8625ef7e 113 EOL"}";
114
115//! Function computes contribution of spotlight source
116const char THE_FUNC_spotLight[] =
117 EOL"void spotLight (in int theId,"
118 EOL" in vec3 theNormal,"
119 EOL" in vec3 theView,"
120 EOL" in vec3 thePoint,"
121 EOL" in bool theIsFront)"
122 EOL"{"
123 EOL" vec3 aLight = occLight_Position (theId).xyz;"
124 EOL" vec3 aSpotDir = occLight_SpotDirection (theId).xyz;"
125 EOL" if (occLight_IsHeadlight (theId) == 0)"
126 EOL" {"
5f4bd4d4 127 EOL" aLight = vec3 (occWorldViewMatrix * vec4 (aLight, 1.0));"
128 EOL" aSpotDir = vec3 (occWorldViewMatrix * vec4 (aSpotDir, 0.0));"
8625ef7e 129 EOL" }"
130 EOL" aLight -= thePoint;"
131 EOL
132 EOL" float aDist = length (aLight);"
133 EOL" aLight = aLight * (1.0 / aDist);"
134 EOL
135 EOL" aSpotDir = normalize (aSpotDir);"
136 // light cone
137 EOL" float aCosA = dot (aSpotDir, -aLight);"
138 EOL" if (aCosA >= 1.0 || aCosA < cos (occLight_SpotCutOff (theId)))"
139 EOL" {"
140 EOL" return;"
141 EOL" }"
142 EOL
143 EOL" float anExponent = occLight_SpotExponent (theId);"
144 EOL" float anAtten = 1.0 / (occLight_ConstAttenuation (theId)"
145 EOL" + occLight_LinearAttenuation (theId) * aDist);"
146 EOL" if (anExponent > 0.0)"
147 EOL" {"
148 EOL" anAtten *= pow (aCosA, anExponent * 128.0);"
149 EOL" }"
150 EOL
151 EOL" vec3 aHalf = normalize (aLight + theView);"
152 EOL
153 EOL" vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal;"
154 EOL" float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));"
155 EOL" float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));"
156 EOL
157 EOL" float aSpecl = 0.0;"
158 EOL" if (aNdotL > 0.0)"
159 EOL" {"
160 EOL" aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess());"
161 EOL" }"
162 EOL
163 EOL" Diffuse += occLight_Diffuse (theId).rgb * aNdotL * anAtten;"
164 EOL" Specular += occLight_Specular (theId).rgb * aSpecl * anAtten;"
165 EOL"}";
166
167//! Function computes contribution of directional light source
168const char THE_FUNC_directionalLight[] =
169 EOL"void directionalLight (in int theId,"
170 EOL" in vec3 theNormal,"
171 EOL" in vec3 theView,"
172 EOL" in bool theIsFront)"
173 EOL"{"
174 EOL" vec3 aLight = normalize (occLight_Position (theId).xyz);"
175 EOL" if (occLight_IsHeadlight (theId) == 0)"
176 EOL" {"
5f4bd4d4 177 EOL" aLight = vec3 (occWorldViewMatrix * vec4 (aLight, 0.0));"
8625ef7e 178 EOL" }"
179 EOL
180 EOL" vec3 aHalf = normalize (aLight + theView);"
181 EOL
182 EOL" vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal;"
183 EOL" float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));"
184 EOL" float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));"
185 EOL
186 EOL" float aSpecl = 0.0;"
187 EOL" if (aNdotL > 0.0)"
188 EOL" {"
189 EOL" aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess());"
190 EOL" }"
191 EOL
192 EOL" Diffuse += occLight_Diffuse (theId).rgb * aNdotL;"
193 EOL" Specular += occLight_Specular (theId).rgb * aSpecl;"
194 EOL"}";
195
f68acbf4 196//! The same as THE_FUNC_directionalLight but for the light with zero index
197//! (avoids limitations on some mobile devices).
198const char THE_FUNC_directionalLightFirst[] =
199 EOL"void directionalLightFirst (in vec3 theNormal,"
200 EOL" in vec3 theView,"
201 EOL" in bool theIsFront)"
202 EOL"{"
203 EOL" vec3 aLight = normalize (occLightSources[1].xyz);"
204 EOL" if (occLight_IsHeadlight (0) == 0)"
205 EOL" {"
5f4bd4d4 206 EOL" aLight = vec3 (occWorldViewMatrix * vec4 (aLight, 0.0));"
f68acbf4 207 EOL" }"
208 EOL
209 EOL" vec3 aHalf = normalize (aLight + theView);"
210 EOL
211 EOL" vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal;"
212 EOL" float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));"
213 EOL" float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));"
214 EOL
215 EOL" float aSpecl = 0.0;"
216 EOL" if (aNdotL > 0.0)"
217 EOL" {"
218 EOL" aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess());"
219 EOL" }"
220 EOL
221 EOL" Diffuse += occLightSources[0].rgb * aNdotL;"
222 EOL" Specular += occLightSources[0].rgb * aSpecl;"
223 EOL"}";
224
8625ef7e 225//! Process clipping planes in Fragment Shader.
226//! Should be added at the beginning of the main() function.
1a7ece8f 227const char THE_FRAG_CLIP_PLANES_N[] =
8625ef7e 228 EOL" for (int aPlaneIter = 0; aPlaneIter < occClipPlaneCount; ++aPlaneIter)"
229 EOL" {"
230 EOL" vec4 aClipEquation = occClipPlaneEquations[aPlaneIter];"
89a929ea 231 EOL" if (dot (aClipEquation.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation.w < 0.0)"
8625ef7e 232 EOL" {"
89a929ea 233 EOL" discard;"
8625ef7e 234 EOL" }"
235 EOL" }";
236
25c35042 237//! Process chains of clipping planes in Fragment Shader.
238const char THE_FRAG_CLIP_CHAINS_N[] =
239EOL" for (int aPlaneIter = 0; aPlaneIter < occClipPlaneCount;)"
240EOL" {"
241EOL" vec4 aClipEquation = occClipPlaneEquations[aPlaneIter];"
242EOL" if (dot (aClipEquation.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation.w < 0.0)"
243EOL" {"
244EOL" if (occClipPlaneChains[aPlaneIter] == 1)"
245EOL" {"
246EOL" discard;"
247EOL" }"
248EOL" aPlaneIter += 1;"
249EOL" }"
250EOL" else"
251EOL" {"
252EOL" aPlaneIter += occClipPlaneChains[aPlaneIter];"
253EOL" }"
254EOL" }";
255
1a7ece8f 256//! Process 1 clipping plane in Fragment Shader.
257const char THE_FRAG_CLIP_PLANES_1[] =
258 EOL" vec4 aClipEquation0 = occClipPlaneEquations[0];"
259 EOL" if (dot (aClipEquation0.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation0.w < 0.0)"
260 EOL" {"
261 EOL" discard;"
262 EOL" }";
263
264//! Process 2 clipping planes in Fragment Shader.
265const char THE_FRAG_CLIP_PLANES_2[] =
266 EOL" vec4 aClipEquation0 = occClipPlaneEquations[0];"
267 EOL" vec4 aClipEquation1 = occClipPlaneEquations[1];"
268 EOL" if (dot (aClipEquation0.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation0.w < 0.0"
269 EOL" || dot (aClipEquation1.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation1.w < 0.0)"
270 EOL" {"
271 EOL" discard;"
272 EOL" }";
273
25c35042 274//! Process a chain of 2 clipping planes in Fragment Shader (3/4 section).
275const char THE_FRAG_CLIP_CHAINS_2[] =
276EOL" vec4 aClipEquation0 = occClipPlaneEquations[0];"
277EOL" vec4 aClipEquation1 = occClipPlaneEquations[1];"
278EOL" if (dot (aClipEquation0.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation0.w < 0.0"
279EOL" && dot (aClipEquation1.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation1.w < 0.0)"
280EOL" {"
281EOL" discard;"
282EOL" }";
283
2a332745 284//! Modify color for Wireframe presentation.
285const char THE_FRAG_WIREFRAME_COLOR[] =
286EOL"vec4 getFinalColor(void)"
287EOL"{"
288EOL" float aDistance = min (min (EdgeDistance[0], EdgeDistance[1]), EdgeDistance[2]);"
289EOL" bool isHollow = occWireframeColor.a < 0.0;"
290EOL" float aMixVal = smoothstep (occLineWidth - occLineFeather * 0.5, occLineWidth + occLineFeather * 0.5, aDistance);"
291EOL" vec4 aMixColor = isHollow"
292EOL" ? vec4 (getColor().rgb, 1.0 - aMixVal)" // edges only (of interior color)
293EOL" : mix (occWireframeColor, getColor(), aMixVal);" // interior + edges
294EOL" return aMixColor;"
295EOL"}";
296
6ef0d6f1 297//! Compute gl_Position vertex shader output.
298const char THE_VERT_gl_Position[] =
299EOL" gl_Position = occProjectionMatrix * occWorldViewMatrix * occModelWorldMatrix * occVertex;";
300
301//! Displace gl_Position alongside vertex normal for outline rendering.
302//! This code adds silhouette only for smooth surfaces of closed primitive, and produces visual artifacts on sharp edges.
303const char THE_VERT_gl_Position_OUTLINE[] =
304EOL" float anOutlineDisp = occOrthoScale > 0.0 ? occOrthoScale : gl_Position.w;"
305EOL" vec4 anOutlinePos = occVertex + vec4 (occNormal * (occSilhouetteThickness * anOutlineDisp), 0.0);"
306EOL" gl_Position = occProjectionMatrix * occWorldViewMatrix * occModelWorldMatrix * anOutlinePos;";
307
8613985b 308#if !defined(GL_ES_VERSION_2_0)
309
310 static const GLfloat THE_DEFAULT_AMBIENT[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
311 static const GLfloat THE_DEFAULT_SPOT_DIR[3] = { 0.0f, 0.0f, -1.0f };
312 static const GLfloat THE_DEFAULT_SPOT_EXPONENT = 0.0f;
313 static const GLfloat THE_DEFAULT_SPOT_CUTOFF = 180.0f;
314
315 //! Bind FFP light source.
992ed6b3 316 static void bindLight (const Graphic3d_CLight& theLight,
8613985b 317 const GLenum theLightGlId,
318 const OpenGl_Mat4& theModelView,
319 OpenGl_Context* theCtx)
320 {
321 // the light is a headlight?
992ed6b3 322 if (theLight.IsHeadlight())
8613985b 323 {
324 theCtx->core11->glMatrixMode (GL_MODELVIEW);
325 theCtx->core11->glLoadIdentity();
326 }
327
328 // setup light type
992ed6b3 329 const Graphic3d_Vec4& aLightColor = theLight.PackedColor();
330 switch (theLight.Type())
8613985b 331 {
332 case Graphic3d_TOLS_AMBIENT : break; // handled by separate if-clause at beginning of method
333 case Graphic3d_TOLS_DIRECTIONAL:
334 {
335 // if the last parameter of GL_POSITION, is zero, the corresponding light source is a Directional one
992ed6b3 336 const OpenGl_Vec4 anInfDir = -theLight.PackedDirection();
8613985b 337
338 // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE.
339 theCtx->core11->glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
992ed6b3 340 theCtx->core11->glLightfv (theLightGlId, GL_DIFFUSE, aLightColor.GetData());
341 theCtx->core11->glLightfv (theLightGlId, GL_SPECULAR, aLightColor.GetData());
8613985b 342 theCtx->core11->glLightfv (theLightGlId, GL_POSITION, anInfDir.GetData());
343 theCtx->core11->glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR);
344 theCtx->core11->glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT);
345 theCtx->core11->glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF);
346 break;
347 }
348 case Graphic3d_TOLS_POSITIONAL:
349 {
350 // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE
992ed6b3 351 const OpenGl_Vec4 aPosition (static_cast<float>(theLight.Position().X()), static_cast<float>(theLight.Position().Y()), static_cast<float>(theLight.Position().Z()), 1.0f);
8613985b 352 theCtx->core11->glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
992ed6b3 353 theCtx->core11->glLightfv (theLightGlId, GL_DIFFUSE, aLightColor.GetData());
354 theCtx->core11->glLightfv (theLightGlId, GL_SPECULAR, aLightColor.GetData());
8613985b 355 theCtx->core11->glLightfv (theLightGlId, GL_POSITION, aPosition.GetData());
356 theCtx->core11->glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR);
357 theCtx->core11->glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT);
358 theCtx->core11->glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF);
359 theCtx->core11->glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation());
360 theCtx->core11->glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation());
361 theCtx->core11->glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0);
362 break;
363 }
364 case Graphic3d_TOLS_SPOT:
365 {
992ed6b3 366 const OpenGl_Vec4 aPosition (static_cast<float>(theLight.Position().X()), static_cast<float>(theLight.Position().Y()), static_cast<float>(theLight.Position().Z()), 1.0f);
8613985b 367 theCtx->core11->glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
992ed6b3 368 theCtx->core11->glLightfv (theLightGlId, GL_DIFFUSE, aLightColor.GetData());
369 theCtx->core11->glLightfv (theLightGlId, GL_SPECULAR, aLightColor.GetData());
8613985b 370 theCtx->core11->glLightfv (theLightGlId, GL_POSITION, aPosition.GetData());
992ed6b3 371 theCtx->core11->glLightfv (theLightGlId, GL_SPOT_DIRECTION, theLight.PackedDirection().GetData());
8613985b 372 theCtx->core11->glLightf (theLightGlId, GL_SPOT_EXPONENT, theLight.Concentration() * 128.0f);
373 theCtx->core11->glLightf (theLightGlId, GL_SPOT_CUTOFF, (theLight.Angle() * 180.0f) / GLfloat(M_PI));
374 theCtx->core11->glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation());
375 theCtx->core11->glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation());
376 theCtx->core11->glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0f);
377 break;
378 }
379 }
380
381 // restore matrix in case of headlight
992ed6b3 382 if (theLight.IsHeadlight())
8613985b 383 {
384 theCtx->core11->glLoadMatrixf (theModelView.GetData());
385 }
386
387 glEnable (theLightGlId);
388 }
389#endif
390
d95f5ce1 391 //! Generate map key for light sources configuration.
392 static TCollection_AsciiString genLightKey (const Handle(Graphic3d_LightSet)& theLights)
393 {
394 if (theLights->NbEnabled() <= THE_NB_UNROLLED_LIGHTS_MAX)
395 {
396 return TCollection_AsciiString ("l_") + theLights->KeyEnabledLong();
397 }
398
399 const Standard_Integer aMaxLimit = roundUpMaxLightSources (theLights->NbEnabled());
400 return TCollection_AsciiString ("l_") + theLights->KeyEnabledShort() + aMaxLimit;
401 }
8625ef7e 402}
403
30f0ad28 404// =======================================================================
405// function : OpenGl_ShaderManager
406// purpose : Creates new empty shader manager
407// =======================================================================
408OpenGl_ShaderManager::OpenGl_ShaderManager (OpenGl_Context* theContext)
8613985b 409: myFfpProgram (new OpenGl_ShaderProgramFFP()),
410 myShadingModel (Graphic3d_TOSM_VERTEX),
6ef0d6f1 411 myUnlitPrograms (new OpenGl_SetOfPrograms()),
8625ef7e 412 myContext (theContext),
7c3ef2f7 413 myHasLocalOrigin (Standard_False),
e6804ff7 414 myLastView (NULL)
30f0ad28 415{
256f9ac0 416 //
30f0ad28 417}
418
419// =======================================================================
420// function : ~OpenGl_ShaderManager
421// purpose : Releases resources of shader manager
422// =======================================================================
423OpenGl_ShaderManager::~OpenGl_ShaderManager()
424{
425 myProgramList.Clear();
426}
427
05e2200b 428// =======================================================================
429// function : clear
430// purpose :
431// =======================================================================
432void OpenGl_ShaderManager::clear()
433{
434 myProgramList.Clear();
435 myLightPrograms.Nullify();
6ef0d6f1 436 myUnlitPrograms = new OpenGl_SetOfPrograms();
437 myOutlinePrograms.Nullify();
05e2200b 438 myMapOfLightPrograms.Clear();
439 myFontProgram.Nullify();
b86bb3df 440 myBlitProgram.Nullify();
98b15dbf 441 myBoundBoxProgram.Nullify();
442 myBoundBoxVertBuffer.Nullify();
f978241f 443 for (Standard_Integer aModeIter = 0; aModeIter < Graphic3d_StereoMode_NB; ++aModeIter)
444 {
445 myStereoPrograms[aModeIter].Nullify();
446 }
05e2200b 447 switchLightPrograms();
448}
449
30f0ad28 450// =======================================================================
451// function : Create
452// purpose : Creates new shader program
453// =======================================================================
8625ef7e 454Standard_Boolean OpenGl_ShaderManager::Create (const Handle(Graphic3d_ShaderProgram)& theProxy,
455 TCollection_AsciiString& theShareKey,
456 Handle(OpenGl_ShaderProgram)& theProgram)
30f0ad28 457{
392ac980 458 theProgram.Nullify();
459 if (theProxy.IsNull())
30f0ad28 460 {
8625ef7e 461 return Standard_False;
30f0ad28 462 }
392ac980 463
464 theShareKey = theProxy->GetId();
465 if (myContext->GetResource<Handle(OpenGl_ShaderProgram)> (theShareKey, theProgram))
466 {
05dd08ce 467 if (theProgram->Share())
468 {
469 myProgramList.Append (theProgram);
470 }
8625ef7e 471 return Standard_True;
392ac980 472 }
473
474 theProgram = new OpenGl_ShaderProgram (theProxy);
475 if (!theProgram->Initialize (myContext, theProxy->ShaderObjects()))
30f0ad28 476 {
392ac980 477 theProgram->Release (myContext);
478 theShareKey.Clear();
479 theProgram.Nullify();
8625ef7e 480 return Standard_False;
30f0ad28 481 }
30f0ad28 482
392ac980 483 myProgramList.Append (theProgram);
484 myContext->ShareResource (theShareKey, theProgram);
8625ef7e 485 return Standard_True;
30f0ad28 486}
487
488// =======================================================================
489// function : Unregister
490// purpose : Removes specified shader program from the manager
491// =======================================================================
392ac980 492void OpenGl_ShaderManager::Unregister (TCollection_AsciiString& theShareKey,
493 Handle(OpenGl_ShaderProgram)& theProgram)
30f0ad28 494{
495 for (OpenGl_ShaderProgramList::Iterator anIt (myProgramList); anIt.More(); anIt.Next())
496 {
497 if (anIt.Value() == theProgram)
498 {
392ac980 499 if (!theProgram->UnShare())
500 {
501 theShareKey.Clear();
502 theProgram.Nullify();
503 return;
504 }
505
30f0ad28 506 myProgramList.Remove (anIt);
507 break;
508 }
509 }
510
511 const TCollection_AsciiString anID = theProgram->myProxy->GetId();
512 if (anID.IsEmpty())
513 {
514 myContext->DelayedRelease (theProgram);
515 theProgram.Nullify();
516 }
517 else
518 {
519 theProgram.Nullify();
05dd08ce 520 myContext->ReleaseResource (anID, Standard_True);
30f0ad28 521 }
522}
523
524// =======================================================================
525// function : ShaderPrograms
526// purpose : Returns list of registered shader programs
527// =======================================================================
528const OpenGl_ShaderProgramList& OpenGl_ShaderManager::ShaderPrograms() const
529{
530 return myProgramList;
531}
532
533// =======================================================================
534// function : Empty
535// purpose : Returns true if no program objects are attached
536// =======================================================================
537Standard_Boolean OpenGl_ShaderManager::IsEmpty() const
538{
539 return myProgramList.IsEmpty();
540}
541
256f9ac0 542// =======================================================================
543// function : switchLightPrograms
544// purpose :
545// =======================================================================
546void OpenGl_ShaderManager::switchLightPrograms()
547{
992ed6b3 548 const Handle(Graphic3d_LightSet)& aLights = myLightSourceState.LightSources();
dc89236f 549 if (aLights.IsNull())
256f9ac0 550 {
6ef0d6f1 551 if (!myMapOfLightPrograms.Find ("unlit", myLightPrograms))
552 {
553 myLightPrograms = new OpenGl_SetOfShaderPrograms (myUnlitPrograms);
554 myMapOfLightPrograms.Bind ("unlit", myLightPrograms);
555 }
dc89236f 556 return;
557 }
558
d95f5ce1 559 const TCollection_AsciiString aKey = genLightKey (aLights);
256f9ac0 560 if (!myMapOfLightPrograms.Find (aKey, myLightPrograms))
561 {
562 myLightPrograms = new OpenGl_SetOfShaderPrograms();
563 myMapOfLightPrograms.Bind (aKey, myLightPrograms);
564 }
565}
566
30f0ad28 567// =======================================================================
568// function : UpdateLightSourceStateTo
569// purpose : Updates state of OCCT light sources
570// =======================================================================
992ed6b3 571void OpenGl_ShaderManager::UpdateLightSourceStateTo (const Handle(Graphic3d_LightSet)& theLights)
30f0ad28 572{
573 myLightSourceState.Set (theLights);
574 myLightSourceState.Update();
256f9ac0 575 switchLightPrograms();
576}
577
7c3ef2f7 578// =======================================================================
579// function : UpdateLightSourceState
580// purpose :
581// =======================================================================
582void OpenGl_ShaderManager::UpdateLightSourceState()
583{
584 myLightSourceState.Update();
585}
586
256f9ac0 587// =======================================================================
588// function : SetShadingModel
589// purpose :
590// =======================================================================
c357e426 591void OpenGl_ShaderManager::SetShadingModel (const Graphic3d_TypeOfShadingModel theModel)
256f9ac0 592{
dc89236f 593 if (theModel == Graphic3d_TOSM_DEFAULT)
594 {
595 throw Standard_ProgramError ("OpenGl_ShaderManager::SetShadingModel() - attempt to set invalid Shading Model!");
596 }
597
256f9ac0 598 myShadingModel = theModel;
599 switchLightPrograms();
30f0ad28 600}
601
602// =======================================================================
603// function : SetProjectionState
604// purpose : Sets new state of OCCT projection transform
605// =======================================================================
c827ea3a 606void OpenGl_ShaderManager::UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix)
30f0ad28 607{
608 myProjectionState.Set (theProjectionMatrix);
609 myProjectionState.Update();
610}
611
612// =======================================================================
613// function : SetModelWorldState
614// purpose : Sets new state of OCCT model-world transform
615// =======================================================================
c827ea3a 616void OpenGl_ShaderManager::UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix)
30f0ad28 617{
618 myModelWorldState.Set (theModelWorldMatrix);
619 myModelWorldState.Update();
620}
621
622// =======================================================================
623// function : SetWorldViewState
624// purpose : Sets new state of OCCT world-view transform
625// =======================================================================
c827ea3a 626void OpenGl_ShaderManager::UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix)
30f0ad28 627{
628 myWorldViewState.Set (theWorldViewMatrix);
629 myWorldViewState.Update();
630}
631
30f0ad28 632// =======================================================================
633// function : PushLightSourceState
634// purpose : Pushes state of OCCT light sources to the program
635// =======================================================================
636void OpenGl_ShaderManager::PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const
637{
8613985b 638 if (myLightSourceState.Index() == theProgram->ActiveState (OpenGl_LIGHT_SOURCES_STATE))
639 {
640 return;
641 }
642
643 theProgram->UpdateState (OpenGl_LIGHT_SOURCES_STATE, myLightSourceState.Index());
644 if (theProgram == myFfpProgram)
30f0ad28 645 {
8613985b 646 #if !defined(GL_ES_VERSION_2_0)
647 if (myContext->core11 == NULL)
648 {
649 return;
650 }
651
daf73ab7 652 GLenum aLightGlId = GL_LIGHT0;
daf73ab7 653 const OpenGl_Mat4 aModelView = myWorldViewState.WorldViewMatrix() * myModelWorldState.ModelWorldMatrix();
992ed6b3 654 for (Graphic3d_LightSet::Iterator aLightIt (myLightSourceState.LightSources(), Graphic3d_LightSet::IterationFilter_ExcludeDisabledAndAmbient);
655 aLightIt.More(); aLightIt.Next())
8613985b 656 {
992ed6b3 657 if (aLightGlId > GL_LIGHT7) // only 8 lights in FFP...
8613985b 658 {
992ed6b3 659 myContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_MEDIUM,
660 "Warning: light sources limit (8) has been exceeded within Fixed-function pipeline.");
661 continue;
8613985b 662 }
992ed6b3 663
664 bindLight (*aLightIt.Value(), aLightGlId, aModelView, myContext);
665 ++aLightGlId;
daf73ab7 666 }
8613985b 667
daf73ab7 668 // apply accumulated ambient color
992ed6b3 669 const Graphic3d_Vec4 anAmbient = !myLightSourceState.LightSources().IsNull()
670 ? myLightSourceState.LightSources()->AmbientColor()
671 : Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 1.0f);
daf73ab7 672 myContext->core11->glLightModelfv (GL_LIGHT_MODEL_AMBIENT, anAmbient.GetData());
8613985b 673
daf73ab7 674 // GL_LIGHTING is managed by drawers to switch between shaded / no lighting output,
675 // therefore managing the state here does not have any effect - do it just for consistency.
676 if (aLightGlId != GL_LIGHT0)
677 {
678 ::glEnable (GL_LIGHTING);
679 }
680 else
681 {
682 ::glDisable (GL_LIGHTING);
683 }
684 // switch off unused lights
685 for (; aLightGlId <= GL_LIGHT7; ++aLightGlId)
686 {
687 ::glDisable (aLightGlId);
8613985b 688 }
689 #endif
30f0ad28 690 return;
691 }
30f0ad28 692
daf73ab7 693 const Standard_Integer aNbLightsMax = theProgram->NbLightsMax();
694 const GLint anAmbientLoc = theProgram->GetStateLocation (OpenGl_OCC_LIGHT_AMBIENT);
695 if (aNbLightsMax == 0
696 && anAmbientLoc == OpenGl_ShaderProgram::INVALID_LOCATION)
01eaf654 697 {
daf73ab7 698 return;
01eaf654 699 }
700
daf73ab7 701 if (myLightTypeArray.Size() < aNbLightsMax)
702 {
703 myLightTypeArray .Resize (0, aNbLightsMax - 1, false);
704 myLightParamsArray.Resize (0, aNbLightsMax - 1, false);
705 }
706 for (Standard_Integer aLightIt = 0; aLightIt < aNbLightsMax; ++aLightIt)
707 {
708 myLightTypeArray.ChangeValue (aLightIt).Type = -1;
709 }
710
992ed6b3 711 if (myLightSourceState.LightSources().IsNull()
daf73ab7 712 || myLightSourceState.LightSources()->IsEmpty())
12381341 713 {
30f0ad28 714 theProgram->SetUniform (myContext,
12381341 715 theProgram->GetStateLocation (OpenGl_OCC_LIGHT_SOURCE_COUNT),
716 0);
30f0ad28 717 theProgram->SetUniform (myContext,
daf73ab7 718 anAmbientLoc,
12381341 719 OpenGl_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
01eaf654 720 theProgram->SetUniform (myContext,
721 theProgram->GetStateLocation (OpenGl_OCC_LIGHT_SOURCE_TYPES),
daf73ab7 722 aNbLightsMax * OpenGl_ShaderLightType::NbOfVec2i(),
723 myLightTypeArray.First().Packed());
12381341 724 return;
725 }
30f0ad28 726
12381341 727 Standard_Integer aLightsNb = 0;
992ed6b3 728 for (Graphic3d_LightSet::Iterator anIter (myLightSourceState.LightSources(), Graphic3d_LightSet::IterationFilter_ExcludeDisabledAndAmbient);
729 anIter.More(); anIter.Next())
12381341 730 {
992ed6b3 731 const Graphic3d_CLight& aLight = *anIter.Value();
732 if (aLightsNb >= aNbLightsMax)
12381341 733 {
daf73ab7 734 if (aNbLightsMax != 0)
735 {
736 myContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_MEDIUM,
737 TCollection_AsciiString("Warning: light sources limit (") + aNbLightsMax + ") has been exceeded.");
738 }
12381341 739 continue;
740 }
30f0ad28 741
daf73ab7 742 OpenGl_ShaderLightType& aLightType = myLightTypeArray.ChangeValue (aLightsNb);
743 OpenGl_ShaderLightParameters& aLightParams = myLightParamsArray.ChangeValue (aLightsNb);
992ed6b3 744 if (!aLight.IsEnabled()) // has no affect with Graphic3d_LightSet::IterationFilter_ExcludeDisabled - here just for consistency
7c3ef2f7 745 {
992ed6b3 746 // if it is desired to keep disabled light in the same order - we can replace it with a black light so that it will have no influence on result
747 aLightType.Type = -1; // Graphic3d_TOLS_AMBIENT can be used instead
748 aLightType.IsHeadlight = false;
749 aLightParams.Color = OpenGl_Vec4 (0.0f, 0.0f, 0.0f, 0.0f);
750 ++aLightsNb;
751 continue;
7c3ef2f7 752 }
992ed6b3 753
754 aLightType.Type = aLight.Type();
755 aLightType.IsHeadlight = aLight.IsHeadlight();
756 aLightParams.Color = aLight.PackedColor();
757 if (aLight.Type() == Graphic3d_TOLS_DIRECTIONAL)
7c3ef2f7 758 {
992ed6b3 759 aLightParams.Position = -aLight.PackedDirection();
760 }
761 else if (!aLight.IsHeadlight())
762 {
763 aLightParams.Position.x() = static_cast<float>(aLight.Position().X() - myLocalOrigin.X());
764 aLightParams.Position.y() = static_cast<float>(aLight.Position().Y() - myLocalOrigin.Y());
765 aLightParams.Position.z() = static_cast<float>(aLight.Position().Z() - myLocalOrigin.Z());
7c3ef2f7 766 aLightParams.Position.w() = 1.0f;
767 }
768 else
769 {
992ed6b3 770 aLightParams.Position.x() = static_cast<float>(aLight.Position().X());
771 aLightParams.Position.y() = static_cast<float>(aLight.Position().Y());
772 aLightParams.Position.z() = static_cast<float>(aLight.Position().Z());
7c3ef2f7 773 aLightParams.Position.w() = 1.0f;
774 }
775
992ed6b3 776 if (aLight.Type() == Graphic3d_TOLS_SPOT)
12381341 777 {
992ed6b3 778 aLightParams.Direction = aLight.PackedDirection();
30f0ad28 779 }
992ed6b3 780 aLightParams.Parameters = aLight.PackedParams();
12381341 781 ++aLightsNb;
782 }
30f0ad28 783
992ed6b3 784 const Graphic3d_Vec4& anAmbient = myLightSourceState.LightSources()->AmbientColor();
12381341 785 theProgram->SetUniform (myContext,
786 theProgram->GetStateLocation (OpenGl_OCC_LIGHT_SOURCE_COUNT),
787 aLightsNb);
788 theProgram->SetUniform (myContext,
daf73ab7 789 anAmbientLoc,
12381341 790 anAmbient);
01eaf654 791 theProgram->SetUniform (myContext,
792 theProgram->GetStateLocation (OpenGl_OCC_LIGHT_SOURCE_TYPES),
daf73ab7 793 aNbLightsMax * OpenGl_ShaderLightType::NbOfVec2i(),
794 myLightTypeArray.First().Packed());
12381341 795 if (aLightsNb > 0)
796 {
4fe9ad57 797 theProgram->SetUniform (myContext,
798 theProgram->GetStateLocation (OpenGl_OCC_LIGHT_SOURCE_PARAMS),
799 aLightsNb * OpenGl_ShaderLightParameters::NbOfVec4(),
daf73ab7 800 myLightParamsArray.First().Packed());
30f0ad28 801 }
30f0ad28 802}
803
804// =======================================================================
805// function : PushProjectionState
806// purpose : Pushes state of OCCT projection transform to the program
807// =======================================================================
808void OpenGl_ShaderManager::PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const
809{
810 if (myProjectionState.Index() == theProgram->ActiveState (OpenGl_PROJECTION_STATE))
811 {
812 return;
813 }
814
8613985b 815 theProgram->UpdateState (OpenGl_PROJECTION_STATE, myProjectionState.Index());
816 if (theProgram == myFfpProgram)
817 {
818 #if !defined(GL_ES_VERSION_2_0)
819 if (myContext->core11 != NULL)
820 {
821 myContext->core11->glMatrixMode (GL_PROJECTION);
822 myContext->core11->glLoadMatrixf (myProjectionState.ProjectionMatrix());
823 }
824 #endif
825 return;
826 }
827
30f0ad28 828 theProgram->SetUniform (myContext,
829 theProgram->GetStateLocation (OpenGl_OCC_PROJECTION_MATRIX),
830 myProjectionState.ProjectionMatrix());
831
832 GLint aLocation = theProgram->GetStateLocation (OpenGl_OCC_PROJECTION_MATRIX_INVERSE);
833 if (aLocation != OpenGl_ShaderProgram::INVALID_LOCATION)
834 {
835 theProgram->SetUniform (myContext, aLocation, myProjectionState.ProjectionMatrixInverse());
836 }
837
838 theProgram->SetUniform (myContext,
839 theProgram->GetStateLocation (OpenGl_OCC_PROJECTION_MATRIX_TRANSPOSE),
840 myProjectionState.ProjectionMatrix(), true);
841
842 aLocation = theProgram->GetStateLocation (OpenGl_OCC_PROJECTION_MATRIX_INVERSE_TRANSPOSE);
843 if (aLocation != OpenGl_ShaderProgram::INVALID_LOCATION)
844 {
845 theProgram->SetUniform (myContext, aLocation, myProjectionState.ProjectionMatrixInverse(), true);
846 }
30f0ad28 847}
848
849// =======================================================================
850// function : PushModelWorldState
851// purpose : Pushes state of OCCT model-world transform to the program
852// =======================================================================
853void OpenGl_ShaderManager::PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const
854{
855 if (myModelWorldState.Index() == theProgram->ActiveState (OpenGl_MODEL_WORLD_STATE))
856 {
857 return;
858 }
859
8613985b 860 theProgram->UpdateState (OpenGl_MODEL_WORLD_STATE, myModelWorldState.Index());
861 if (theProgram == myFfpProgram)
862 {
863 #if !defined(GL_ES_VERSION_2_0)
864 if (myContext->core11 != NULL)
865 {
866 const OpenGl_Mat4 aModelView = myWorldViewState.WorldViewMatrix() * myModelWorldState.ModelWorldMatrix();
867 myContext->core11->glMatrixMode (GL_MODELVIEW);
868 myContext->core11->glLoadMatrixf (aModelView.GetData());
869 theProgram->UpdateState (OpenGl_WORLD_VIEW_STATE, myWorldViewState.Index());
870 }
871 #endif
872 return;
873 }
874
30f0ad28 875 theProgram->SetUniform (myContext,
876 theProgram->GetStateLocation (OpenGl_OCC_MODEL_WORLD_MATRIX),
877 myModelWorldState.ModelWorldMatrix());
878
879 GLint aLocation = theProgram->GetStateLocation (OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE);
880 if (aLocation != OpenGl_ShaderProgram::INVALID_LOCATION)
881 {
882 theProgram->SetUniform (myContext, aLocation, myModelWorldState.ModelWorldMatrixInverse());
883 }
884
885 theProgram->SetUniform (myContext,
886 theProgram->GetStateLocation (OpenGl_OCC_MODEL_WORLD_MATRIX_TRANSPOSE),
887 myModelWorldState.ModelWorldMatrix(), true);
888
889 aLocation = theProgram->GetStateLocation (OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE_TRANSPOSE);
890 if (aLocation != OpenGl_ShaderProgram::INVALID_LOCATION)
891 {
892 theProgram->SetUniform (myContext, aLocation, myModelWorldState.ModelWorldMatrixInverse(), true);
893 }
30f0ad28 894}
895
896// =======================================================================
897// function : PushWorldViewState
898// purpose : Pushes state of OCCT world-view transform to the program
899// =======================================================================
900void OpenGl_ShaderManager::PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const
901{
902 if (myWorldViewState.Index() == theProgram->ActiveState (OpenGl_WORLD_VIEW_STATE))
903 {
904 return;
905 }
906
8613985b 907 theProgram->UpdateState (OpenGl_WORLD_VIEW_STATE, myWorldViewState.Index());
908 if (theProgram == myFfpProgram)
909 {
910 #if !defined(GL_ES_VERSION_2_0)
911 if (myContext->core11 != NULL)
912 {
913 const OpenGl_Mat4 aModelView = myWorldViewState.WorldViewMatrix() * myModelWorldState.ModelWorldMatrix();
914 myContext->core11->glMatrixMode (GL_MODELVIEW);
915 myContext->core11->glLoadMatrixf (aModelView.GetData());
916 theProgram->UpdateState (OpenGl_MODEL_WORLD_STATE, myModelWorldState.Index());
917 }
918 #endif
919 return;
920 }
921
30f0ad28 922 theProgram->SetUniform (myContext,
923 theProgram->GetStateLocation (OpenGl_OCC_WORLD_VIEW_MATRIX),
924 myWorldViewState.WorldViewMatrix());
925
926 GLint aLocation = theProgram->GetStateLocation (OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE);
927 if (aLocation != OpenGl_ShaderProgram::INVALID_LOCATION)
928 {
929 theProgram->SetUniform (myContext, aLocation, myWorldViewState.WorldViewMatrixInverse());
930 }
931
932 theProgram->SetUniform (myContext,
933 theProgram->GetStateLocation (OpenGl_OCC_WORLD_VIEW_MATRIX_TRANSPOSE),
934 myWorldViewState.WorldViewMatrix(), true);
935
936 aLocation = theProgram->GetStateLocation (OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE_TRANSPOSE);
937 if (aLocation != OpenGl_ShaderProgram::INVALID_LOCATION)
938 {
939 theProgram->SetUniform (myContext, aLocation, myWorldViewState.WorldViewMatrixInverse(), true);
940 }
30f0ad28 941}
942
943// =======================================================================
944// function : UpdateClippingState
945// purpose : Updates state of OCCT clipping planes
946// =======================================================================
947void OpenGl_ShaderManager::UpdateClippingState()
948{
949 myClippingState.Update();
950}
951
952// =======================================================================
953// function : RevertClippingState
954// purpose : Reverts state of OCCT clipping planes
955// =======================================================================
956void OpenGl_ShaderManager::RevertClippingState()
957{
958 myClippingState.Revert();
959}
960
961// =======================================================================
962// function : PushClippingState
963// purpose : Pushes state of OCCT clipping planes to the program
964// =======================================================================
965void OpenGl_ShaderManager::PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const
966{
967 if (myClippingState.Index() == theProgram->ActiveState (OpenGl_CLIP_PLANES_STATE))
968 {
969 return;
970 }
971
12381341 972 theProgram->UpdateState (OpenGl_CLIP_PLANES_STATE, myClippingState.Index());
8613985b 973 if (theProgram == myFfpProgram)
974 {
975 #if !defined(GL_ES_VERSION_2_0)
976 if (myContext->core11 == NULL)
977 {
978 return;
979 }
980
daf73ab7 981 const Standard_Integer aNbMaxPlanes = myContext->MaxClipPlanes();
982 if (myClipPlaneArrayFfp.Size() < aNbMaxPlanes)
983 {
984 myClipPlaneArrayFfp.Resize (0, aNbMaxPlanes - 1, false);
985 }
986
8613985b 987 Standard_Integer aPlaneId = 0;
988 Standard_Boolean toRestoreModelView = Standard_False;
25c35042 989 const Handle(Graphic3d_ClipPlane)& aCappedChain = myContext->Clipping().CappedChain();
8613985b 990 for (OpenGl_ClippingIterator aPlaneIter (myContext->Clipping()); aPlaneIter.More(); aPlaneIter.Next())
991 {
992 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIter.Value();
25c35042 993 if (aPlaneIter.IsDisabled()
994 || aPlane->IsChain()
995 || (aPlane == aCappedChain
996 && myContext->Clipping().IsCappingEnableAllExcept()))
8613985b 997 {
998 continue;
999 }
1000 else if (aPlaneId >= aNbMaxPlanes)
1001 {
daf73ab7 1002 myContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_MEDIUM,
8613985b 1003 TCollection_ExtendedString("Warning: clipping planes limit (") + aNbMaxPlanes + ") has been exceeded.");
1004 break;
1005 }
1006
1007 const Graphic3d_ClipPlane::Equation& anEquation = aPlane->GetEquation();
daf73ab7 1008 OpenGl_Vec4d& aPlaneEq = myClipPlaneArrayFfp.ChangeValue (aPlaneId);
8613985b 1009 aPlaneEq.x() = anEquation.x();
1010 aPlaneEq.y() = anEquation.y();
1011 aPlaneEq.z() = anEquation.z();
1012 aPlaneEq.w() = anEquation.w();
1013 if (myHasLocalOrigin)
1014 {
1015 const gp_XYZ aPos = aPlane->ToPlane().Position().Location().XYZ() - myLocalOrigin;
1016 const Standard_Real aD = -(anEquation.x() * aPos.X() + anEquation.y() * aPos.Y() + anEquation.z() * aPos.Z());
1017 aPlaneEq.w() = aD;
1018 }
1019
1020 const GLenum anFfpPlaneID = GL_CLIP_PLANE0 + aPlaneId;
1021 if (anFfpPlaneID == GL_CLIP_PLANE0)
1022 {
1023 // set either identity or pure view matrix
1024 toRestoreModelView = Standard_True;
1025 myContext->core11->glMatrixMode (GL_MODELVIEW);
1026 myContext->core11->glLoadMatrixf (myWorldViewState.WorldViewMatrix().GetData());
1027 }
1028
1029 ::glEnable (anFfpPlaneID);
1030 myContext->core11->glClipPlane (anFfpPlaneID, aPlaneEq);
1031
1032 ++aPlaneId;
1033 }
1034
1035 // switch off unused lights
1036 for (; aPlaneId < aNbMaxPlanes; ++aPlaneId)
1037 {
1038 ::glDisable (GL_CLIP_PLANE0 + aPlaneId);
1039 }
1040
1041 // restore combined model-view matrix
1042 if (toRestoreModelView)
1043 {
1044 const OpenGl_Mat4 aModelView = myWorldViewState.WorldViewMatrix() * myModelWorldState.ModelWorldMatrix();
1045 myContext->core11->glLoadMatrixf (aModelView.GetData());
1046 }
1047 #endif
1048 return;
1049 }
1050
12381341 1051 const GLint aLocEquations = theProgram->GetStateLocation (OpenGl_OCC_CLIP_PLANE_EQUATIONS);
89a929ea 1052 if (aLocEquations == OpenGl_ShaderProgram::INVALID_LOCATION)
12381341 1053 {
1054 return;
1055 }
1056
daf73ab7 1057 const Standard_Integer aNbClipPlanesMax = theProgram->NbClipPlanesMax();
25c35042 1058 const Standard_Integer aNbPlanes = Min (myContext->Clipping().NbClippingOrCappingOn(), aNbClipPlanesMax);
3202bf1e 1059 if (aNbPlanes < 1)
12381341 1060 {
25c35042 1061 theProgram->SetUniform (myContext, theProgram->GetStateLocation (OpenGl_OCC_CLIP_PLANE_COUNT), 0);
12381341 1062 return;
1063 }
1064
daf73ab7 1065 if (myClipPlaneArray.Size() < aNbClipPlanesMax)
1066 {
1067 myClipPlaneArray.Resize (0, aNbClipPlanesMax - 1, false);
25c35042 1068 myClipChainArray.Resize (0, aNbClipPlanesMax - 1, false);
daf73ab7 1069 }
1070
1071 Standard_Integer aPlaneId = 0;
25c35042 1072 const Handle(Graphic3d_ClipPlane)& aCappedChain = myContext->Clipping().CappedChain();
3202bf1e 1073 for (OpenGl_ClippingIterator aPlaneIter (myContext->Clipping()); aPlaneIter.More(); aPlaneIter.Next())
12381341 1074 {
3202bf1e 1075 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIter.Value();
1076 if (aPlaneIter.IsDisabled())
30f0ad28 1077 {
12381341 1078 continue;
30f0ad28 1079 }
25c35042 1080
1081 if (myContext->Clipping().IsCappingDisableAllExcept())
89a929ea 1082 {
25c35042 1083 // enable only specific (sub) plane
1084 if (aPlane != aCappedChain)
1085 {
1086 continue;
1087 }
1088
1089 Standard_Integer aSubPlaneIndex = 1;
1090 for (const Graphic3d_ClipPlane* aSubPlaneIter = aCappedChain.get(); aSubPlaneIter != NULL; aSubPlaneIter = aSubPlaneIter->ChainNextPlane().get(), ++aSubPlaneIndex)
1091 {
1092 if (aSubPlaneIndex == myContext->Clipping().CappedSubPlane())
1093 {
1094 addClippingPlane (aPlaneId, *aSubPlaneIter, aSubPlaneIter->GetEquation(), 1);
1095 break;
1096 }
1097 }
89a929ea 1098 break;
1099 }
25c35042 1100 else if (aPlane == aCappedChain) // && myContext->Clipping().IsCappingEnableAllExcept()
1101 {
1102 // enable sub-planes within processed Chain as reversed and ORed, excluding filtered plane
1103 if (aPlaneId + aPlane->NbChainNextPlanes() - 1 > aNbClipPlanesMax)
1104 {
1105 myContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH,
1106 TCollection_AsciiString("Error: clipping planes limit (") + aNbClipPlanesMax + ") has been exceeded.");
1107 break;
1108 }
30f0ad28 1109
25c35042 1110 Standard_Integer aSubPlaneIndex = 1;
1111 for (const Graphic3d_ClipPlane* aSubPlaneIter = aPlane.get(); aSubPlaneIter != NULL; aSubPlaneIter = aSubPlaneIter->ChainNextPlane().get(), ++aSubPlaneIndex)
1112 {
1113 if (aSubPlaneIndex != -myContext->Clipping().CappedSubPlane())
1114 {
1115 addClippingPlane (aPlaneId, *aSubPlaneIter, aSubPlaneIter->ReversedEquation(), 1);
1116 }
1117 }
1118 }
1119 else
7c3ef2f7 1120 {
25c35042 1121 // normal case
1122 if (aPlaneId + aPlane->NbChainNextPlanes() > aNbClipPlanesMax)
1123 {
1124 myContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH,
1125 TCollection_AsciiString("Error: clipping planes limit (") + aNbClipPlanesMax + ") has been exceeded.");
1126 break;
1127 }
1128 for (const Graphic3d_ClipPlane* aSubPlaneIter = aPlane.get(); aSubPlaneIter != NULL; aSubPlaneIter = aSubPlaneIter->ChainNextPlane().get())
1129 {
1130 addClippingPlane (aPlaneId, *aSubPlaneIter, aSubPlaneIter->GetEquation(), aSubPlaneIter->NbChainNextPlanes());
1131 }
7c3ef2f7 1132 }
30f0ad28 1133 }
5495fa7e 1134
25c35042 1135 theProgram->SetUniform (myContext, theProgram->GetStateLocation (OpenGl_OCC_CLIP_PLANE_COUNT), aPlaneId);
daf73ab7 1136 theProgram->SetUniform (myContext, aLocEquations, aNbClipPlanesMax, &myClipPlaneArray.First());
25c35042 1137 theProgram->SetUniform (myContext, theProgram->GetStateLocation (OpenGl_OCC_CLIP_PLANE_CHAINS), aNbClipPlanesMax, &myClipChainArray.First());
30f0ad28 1138}
1139
8613985b 1140// =======================================================================
1141// function : PushMaterialState
1142// purpose :
1143// =======================================================================
1144void OpenGl_ShaderManager::PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const
1145{
1146 if (myMaterialState.Index() == theProgram->ActiveState (OpenGl_MATERIAL_STATE))
1147 {
1148 return;
1149 }
1150
1151 const OpenGl_Material& aFrontMat = myMaterialState.FrontMaterial();
1152 const OpenGl_Material& aBackMat = myMaterialState.BackMaterial();
1153 theProgram->UpdateState (OpenGl_MATERIAL_STATE, myMaterialState.Index());
1154 if (theProgram == myFfpProgram)
1155 {
1156 #if !defined(GL_ES_VERSION_2_0)
1157 if (myContext->core11 == NULL)
1158 {
1159 return;
1160 }
1161
2a332745 1162 myContext->SetSampleAlphaToCoverage (false);
c40eb6b9 1163 if (myMaterialState.AlphaCutoff() < ShortRealLast())
1164 {
1165 glAlphaFunc (GL_GEQUAL, myMaterialState.AlphaCutoff());
1166 glEnable (GL_ALPHA_TEST);
1167 }
1168 else
1169 {
1170 glDisable (GL_ALPHA_TEST);
1171 }
1172
8613985b 1173 const GLenum aFrontFace = myMaterialState.ToDistinguish() ? GL_FRONT : GL_FRONT_AND_BACK;
1174 myContext->core11->glMaterialfv(aFrontFace, GL_AMBIENT, aFrontMat.Ambient.GetData());
1175 myContext->core11->glMaterialfv(aFrontFace, GL_DIFFUSE, aFrontMat.Diffuse.GetData());
1176 myContext->core11->glMaterialfv(aFrontFace, GL_SPECULAR, aFrontMat.Specular.GetData());
1177 myContext->core11->glMaterialfv(aFrontFace, GL_EMISSION, aFrontMat.Emission.GetData());
1178 myContext->core11->glMaterialf (aFrontFace, GL_SHININESS, aFrontMat.Shine());
1179 if (myMaterialState.ToDistinguish())
1180 {
1181 myContext->core11->glMaterialfv(GL_BACK, GL_AMBIENT, aBackMat.Ambient.GetData());
1182 myContext->core11->glMaterialfv(GL_BACK, GL_DIFFUSE, aBackMat.Diffuse.GetData());
1183 myContext->core11->glMaterialfv(GL_BACK, GL_SPECULAR, aBackMat.Specular.GetData());
1184 myContext->core11->glMaterialfv(GL_BACK, GL_EMISSION, aBackMat.Emission.GetData());
1185 myContext->core11->glMaterialf (GL_BACK, GL_SHININESS, aBackMat.Shine());
1186 }
1187 #endif
1188 return;
1189 }
1190
2a332745 1191 myContext->SetSampleAlphaToCoverage (myMaterialState.HasAlphaCutoff());
c40eb6b9 1192 theProgram->SetUniform (myContext,
1193 theProgram->GetStateLocation (OpenGl_OCCT_ALPHA_CUTOFF),
1194 myMaterialState.AlphaCutoff());
8613985b 1195 theProgram->SetUniform (myContext,
1196 theProgram->GetStateLocation (OpenGl_OCCT_TEXTURE_ENABLE),
1197 myMaterialState.ToMapTexture() ? 1 : 0);
1198 theProgram->SetUniform (myContext,
1199 theProgram->GetStateLocation (OpenGl_OCCT_DISTINGUISH_MODE),
1200 myMaterialState.ToDistinguish() ? 1 : 0);
1201
2a332745 1202 if (const OpenGl_ShaderUniformLocation aLocFront = theProgram->GetStateLocation (OpenGl_OCCT_FRONT_MATERIAL))
8613985b 1203 {
1204 theProgram->SetUniform (myContext, aLocFront, OpenGl_Material::NbOfVec4(),
1205 aFrontMat.Packed());
1206 }
2a332745 1207 if (const OpenGl_ShaderUniformLocation aLocBack = theProgram->GetStateLocation (OpenGl_OCCT_BACK_MATERIAL))
8613985b 1208 {
1209 theProgram->SetUniform (myContext, aLocBack, OpenGl_Material::NbOfVec4(),
1210 aBackMat.Packed());
1211 }
1212}
1213
a1073ae2 1214// =======================================================================
1215// function : PushOitState
1216// purpose : Pushes state of OIT uniforms to the specified program
1217// =======================================================================
1218void OpenGl_ShaderManager::PushOitState (const Handle(OpenGl_ShaderProgram)& theProgram) const
1219{
1220 if (!theProgram->IsValid())
1221 {
1222 return;
1223 }
1224
1225 if (myOitState.Index() == theProgram->ActiveState (OpenGL_OIT_STATE))
1226 {
1227 return;
1228 }
1229
1230 const GLint aLocOutput = theProgram->GetStateLocation (OpenGl_OCCT_OIT_OUTPUT);
1231 if (aLocOutput != OpenGl_ShaderProgram::INVALID_LOCATION)
1232 {
1233 theProgram->SetUniform (myContext, aLocOutput, myOitState.ToEnableWrite());
1234 }
1235
1236 const GLint aLocDepthFactor = theProgram->GetStateLocation (OpenGl_OCCT_OIT_DEPTH_FACTOR);
1237 if (aLocDepthFactor != OpenGl_ShaderProgram::INVALID_LOCATION)
1238 {
1239 theProgram->SetUniform (myContext, aLocDepthFactor, myOitState.DepthFactor());
1240 }
1241}
1242
2a332745 1243// =======================================================================
1244// function : PushInteriorState
1245// purpose :
1246// =======================================================================
1247void OpenGl_ShaderManager::PushInteriorState (const Handle(OpenGl_ShaderProgram)& theProgram,
1248 const Handle(Graphic3d_AspectFillArea3d)& theAspect) const
1249{
1250 if (theProgram.IsNull()
1251 || !theProgram->IsValid())
1252 {
1253 return;
1254 }
1255
1256 if (const OpenGl_ShaderUniformLocation aLocViewPort = theProgram->GetStateLocation (OpenGl_OCCT_VIEWPORT))
1257 {
1258 theProgram->SetUniform (myContext, aLocViewPort, OpenGl_Vec4 ((float )myContext->Viewport()[0], (float )myContext->Viewport()[1], (float )myContext->Viewport()[2], (float )myContext->Viewport()[3]));
1259 }
1260 if (const OpenGl_ShaderUniformLocation aLocLineWidth = theProgram->GetStateLocation (OpenGl_OCCT_LINE_WIDTH))
1261 {
1262 theProgram->SetUniform (myContext, aLocLineWidth, theAspect->EdgeWidth() * myContext->LineWidthScale());
1263 theProgram->SetUniform (myContext, theProgram->GetStateLocation (OpenGl_OCCT_LINE_FEATHER), myContext->LineFeather() * myContext->LineWidthScale());
1264 }
1265 if (const OpenGl_ShaderUniformLocation aLocWireframeColor = theProgram->GetStateLocation (OpenGl_OCCT_WIREFRAME_COLOR))
1266 {
1267 if (theAspect->InteriorStyle() == Aspect_IS_HOLLOW)
1268 {
1269 theProgram->SetUniform (myContext, aLocWireframeColor, OpenGl_Vec4 (-1.0f, -1.0f, -1.0f, -1.0f));
1270 }
1271 else
1272 {
1273 theProgram->SetUniform (myContext, aLocWireframeColor, theAspect->EdgeColorRGBA());
1274 }
1275 }
1276 if (const OpenGl_ShaderUniformLocation aLocQuadModeState = theProgram->GetStateLocation (OpenGl_OCCT_QUAD_MODE_STATE))
1277 {
1278 theProgram->SetUniform (myContext, aLocQuadModeState, theAspect->ToSkipFirstEdge() ? 1 : 0);
1279 }
1280}
1281
30f0ad28 1282// =======================================================================
e135a155 1283// function : PushState
30f0ad28 1284// purpose : Pushes state of OCCT graphics parameters to the program
1285// =======================================================================
1286void OpenGl_ShaderManager::PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const
1287{
8613985b 1288 const Handle(OpenGl_ShaderProgram)& aProgram = !theProgram.IsNull() ? theProgram : myFfpProgram;
1289 PushClippingState (aProgram);
1290 PushWorldViewState (aProgram);
1291 PushModelWorldState (aProgram);
1292 PushProjectionState (aProgram);
1293 PushLightSourceState (aProgram);
1294 PushMaterialState (aProgram);
a1073ae2 1295 PushOitState (aProgram);
12381341 1296}
8625ef7e 1297
1298// =======================================================================
1299// function : prepareStdProgramFont
1300// purpose :
1301// =======================================================================
1302Standard_Boolean OpenGl_ShaderManager::prepareStdProgramFont()
1303{
3b4c6945 1304 OpenGl_ShaderObject::ShaderVariableList aUniforms, aStageInOuts;
1305 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec2 TexCoord", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
1306
6ef0d6f1 1307 TCollection_AsciiString aSrcVert = TCollection_AsciiString()
1308 + EOL"void main()"
3b4c6945 1309 EOL"{"
1310 EOL" TexCoord = occTexCoord.st;"
6ef0d6f1 1311 + THE_VERT_gl_Position
1312 + EOL"}";
b990e557 1313
4e1523ef 1314 TCollection_AsciiString
cc8cbabe 1315 aSrcGetAlpha = EOL"float getAlpha(void) { return occTexture2D(occSamplerBaseColor, TexCoord.st).a; }";
4e1523ef 1316#if !defined(GL_ES_VERSION_2_0)
1317 if (myContext->core11 == NULL)
1318 {
cc8cbabe 1319 aSrcGetAlpha = EOL"float getAlpha(void) { return occTexture2D(occSamplerBaseColor, TexCoord.st).r; }";
4e1523ef 1320 }
1321#endif
1322
3b4c6945 1323 TCollection_AsciiString aSrcFrag =
1324 aSrcGetAlpha
4e1523ef 1325 + EOL"void main()"
b990e557 1326 EOL"{"
1327 EOL" vec4 aColor = occColor;"
1328 EOL" aColor.a *= getAlpha();"
1329 EOL" if (aColor.a <= 0.285) discard;"
b17e5bae 1330 EOL" occSetFragColor (aColor);"
b990e557 1331 EOL"}";
8625ef7e 1332
3b4c6945 1333 Handle(Graphic3d_ShaderProgram) aProgramSrc = new Graphic3d_ShaderProgram();
d95f5ce1 1334 defaultGlslVersion (aProgramSrc, "font", 0);
daf73ab7 1335 aProgramSrc->SetNbLightsMax (0);
1336 aProgramSrc->SetNbClipPlanesMax (0);
3b4c6945 1337 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcVert, Graphic3d_TOS_VERTEX, aUniforms, aStageInOuts));
1338 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcFrag, Graphic3d_TOS_FRAGMENT, aUniforms, aStageInOuts));
8625ef7e 1339 TCollection_AsciiString aKey;
1340 if (!Create (aProgramSrc, aKey, myFontProgram))
1341 {
1342 myFontProgram = new OpenGl_ShaderProgram(); // just mark as invalid
1343 return Standard_False;
1344 }
1345 return Standard_True;
1346}
1347
1348// =======================================================================
b86bb3df 1349// function : prepareStdProgramFboBlit
1350// purpose :
1351// =======================================================================
1352Standard_Boolean OpenGl_ShaderManager::prepareStdProgramFboBlit()
1353{
3b4c6945 1354 OpenGl_ShaderObject::ShaderVariableList aUniforms, aStageInOuts;
1355 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec2 TexCoord", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
1356
b86bb3df 1357 TCollection_AsciiString aSrcVert =
b86bb3df 1358 EOL"void main()"
1359 EOL"{"
1360 EOL" TexCoord = occVertex.zw;"
1361 EOL" gl_Position = vec4(occVertex.x, occVertex.y, 0.0, 1.0);"
1362 EOL"}";
1363
3b4c6945 1364 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("sampler2D uColorSampler", Graphic3d_TOS_FRAGMENT));
1365 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("sampler2D uDepthSampler", Graphic3d_TOS_FRAGMENT));
b86bb3df 1366 TCollection_AsciiString aSrcFrag =
b86bb3df 1367 EOL"void main()"
1368 EOL"{"
1369 EOL" gl_FragDepth = occTexture2D (uDepthSampler, TexCoord).r;"
b17e5bae 1370 EOL" occSetFragColor (occTexture2D (uColorSampler, TexCoord));"
b86bb3df 1371 EOL"}";
1372
3b4c6945 1373 Handle(Graphic3d_ShaderProgram) aProgramSrc = new Graphic3d_ShaderProgram();
b86bb3df 1374#if defined(GL_ES_VERSION_2_0)
1375 if (myContext->IsGlGreaterEqual (3, 0))
1376 {
1377 aProgramSrc->SetHeader ("#version 300 es");
1378 }
1ce0716b 1379 else if (myContext->extFragDepth)
1380 {
1381 aProgramSrc->SetHeader ("#extension GL_EXT_frag_depth : enable"
1382 EOL"#define gl_FragDepth gl_FragDepthEXT");
1383 }
b86bb3df 1384 else
1385 {
1386 // there is no way to draw into depth buffer
1387 aSrcFrag =
b86bb3df 1388 EOL"void main()"
1389 EOL"{"
b17e5bae 1390 EOL" occSetFragColor (occTexture2D (uColorSampler, TexCoord));"
b86bb3df 1391 EOL"}";
1392 }
4e1523ef 1393#else
1394 if (myContext->core32 != NULL)
1395 {
1396 aProgramSrc->SetHeader ("#version 150");
1397 }
b86bb3df 1398#endif
d95f5ce1 1399 aProgramSrc->SetId ("occt_blit");
daf73ab7 1400 aProgramSrc->SetNbLightsMax (0);
1401 aProgramSrc->SetNbClipPlanesMax (0);
3b4c6945 1402 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcVert, Graphic3d_TOS_VERTEX, aUniforms, aStageInOuts));
1403 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcFrag, Graphic3d_TOS_FRAGMENT, aUniforms, aStageInOuts));
b86bb3df 1404 TCollection_AsciiString aKey;
1405 if (!Create (aProgramSrc, aKey, myBlitProgram))
1406 {
1407 myBlitProgram = new OpenGl_ShaderProgram(); // just mark as invalid
1408 return Standard_False;
1409 }
1410
1411 myContext->BindProgram (myBlitProgram);
cc8cbabe 1412 myBlitProgram->SetSampler (myContext, "uColorSampler", Graphic3d_TextureUnit_0);
1413 myBlitProgram->SetSampler (myContext, "uDepthSampler", Graphic3d_TextureUnit_1);
b86bb3df 1414 myContext->BindProgram (NULL);
1415 return Standard_True;
1416}
1417
a1073ae2 1418// =======================================================================
1419// function : prepareStdProgramOitCompositing
1420// purpose :
1421// =======================================================================
1422Standard_Boolean OpenGl_ShaderManager::prepareStdProgramOitCompositing (const Standard_Boolean theMsaa)
1423{
1424 Handle(OpenGl_ShaderProgram)& aProgram = myOitCompositingProgram[theMsaa ? 1 : 0];
1425 Handle(Graphic3d_ShaderProgram) aProgramSrc = new Graphic3d_ShaderProgram();
1426 TCollection_AsciiString aSrcVert, aSrcFrag;
1427
3b4c6945 1428 OpenGl_ShaderObject::ShaderVariableList aUniforms, aStageInOuts;
1429 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec2 TexCoord", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
1430
a1073ae2 1431 aSrcVert =
a1073ae2 1432 EOL"void main()"
1433 EOL"{"
1434 EOL" TexCoord = occVertex.zw;"
1435 EOL" gl_Position = vec4 (occVertex.x, occVertex.y, 0.0, 1.0);"
1436 EOL"}";
1437
1438 if (!theMsaa)
1439 {
3b4c6945 1440 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("sampler2D uAccumTexture", Graphic3d_TOS_FRAGMENT));
1441 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("sampler2D uWeightTexture", Graphic3d_TOS_FRAGMENT));
a1073ae2 1442 aSrcFrag =
a1073ae2 1443 EOL"void main()"
1444 EOL"{"
1445 EOL" vec4 aAccum = occTexture2D (uAccumTexture, TexCoord);"
1446 EOL" float aWeight = occTexture2D (uWeightTexture, TexCoord).r;"
b17e5bae 1447 EOL" occSetFragColor (vec4 (aAccum.rgb / max (aWeight, 0.00001), aAccum.a));"
a1073ae2 1448 EOL"}";
1449 #if !defined(GL_ES_VERSION_2_0)
1450 if (myContext->IsGlGreaterEqual (3, 2))
1451 {
1452 aProgramSrc->SetHeader ("#version 150");
1453 }
1454 #else
1455 if (myContext->IsGlGreaterEqual (3, 0))
1456 {
1457 aProgramSrc->SetHeader ("#version 300 es");
1458 }
1459 #endif
1460 }
1461 else
1462 {
3b4c6945 1463 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("sampler2DMS uAccumTexture", Graphic3d_TOS_FRAGMENT));
1464 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("sampler2DMS uWeightTexture", Graphic3d_TOS_FRAGMENT));
a1073ae2 1465 aSrcFrag =
a1073ae2 1466 EOL"void main()"
1467 EOL"{"
e3744452 1468 EOL" ivec2 aTexel = ivec2 (vec2 (textureSize (uAccumTexture)) * TexCoord);"
a1073ae2 1469 EOL" vec4 aAccum = texelFetch (uAccumTexture, aTexel, gl_SampleID);"
1470 EOL" float aWeight = texelFetch (uWeightTexture, aTexel, gl_SampleID).r;"
b17e5bae 1471 EOL" occSetFragColor (vec4 (aAccum.rgb / max (aWeight, 0.00001), aAccum.a));"
a1073ae2 1472 EOL"}";
1473 #if !defined(GL_ES_VERSION_2_0)
1474 if (myContext->IsGlGreaterEqual (4, 0))
1475 {
1476 aProgramSrc->SetHeader ("#version 400");
1477 }
1478 #else
e3744452 1479 if (myContext->IsGlGreaterEqual (3, 2))
a1073ae2 1480 {
e3744452 1481 aProgramSrc->SetHeader ("#version 320 es");
1482 }
1483 else if (myContext->IsGlGreaterEqual (3, 0))
1484 {
1485 aProgramSrc->SetHeader ("#version 300 es"); // with GL_OES_sample_variables extension
a1073ae2 1486 }
1487 #endif
1488 }
1489
d95f5ce1 1490 aProgramSrc->SetId (theMsaa ? "occt_weight-oit-msaa" : "occt_weight-oit");
daf73ab7 1491 aProgramSrc->SetNbLightsMax (0);
1492 aProgramSrc->SetNbClipPlanesMax (0);
3b4c6945 1493 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcVert, Graphic3d_TOS_VERTEX, aUniforms, aStageInOuts));
1494 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcFrag, Graphic3d_TOS_FRAGMENT, aUniforms, aStageInOuts));
a1073ae2 1495 TCollection_AsciiString aKey;
1496 if (!Create (aProgramSrc, aKey, aProgram))
1497 {
1498 aProgram = new OpenGl_ShaderProgram(); // just mark as invalid
1499 return Standard_False;
1500 }
1501
1502 myContext->BindProgram (aProgram);
cc8cbabe 1503 aProgram->SetSampler (myContext, "uAccumTexture", Graphic3d_TextureUnit_0);
1504 aProgram->SetSampler (myContext, "uWeightTexture", Graphic3d_TextureUnit_1);
a1073ae2 1505 myContext->BindProgram (Handle(OpenGl_ShaderProgram)());
1506 return Standard_True;
1507}
1508
299e0ab9 1509// =======================================================================
1510// function : pointSpriteAlphaSrc
1511// purpose :
1512// =======================================================================
fd59283a 1513TCollection_AsciiString OpenGl_ShaderManager::pointSpriteAlphaSrc (const Standard_Integer theBits)
299e0ab9 1514{
cc8cbabe 1515 TCollection_AsciiString aSrcGetAlpha = EOL"float getAlpha(void) { return occTexture2D(occSamplerBaseColor, " THE_VEC2_glPointCoord ").a; }";
299e0ab9 1516#if !defined(GL_ES_VERSION_2_0)
fd59283a 1517 if (myContext->core11 == NULL
6ef0d6f1 1518 && (theBits & OpenGl_PO_HasTextures) == OpenGl_PO_TextureA)
299e0ab9 1519 {
cc8cbabe 1520 aSrcGetAlpha = EOL"float getAlpha(void) { return occTexture2D(occSamplerBaseColor, " THE_VEC2_glPointCoord ").r; }";
299e0ab9 1521 }
fd59283a 1522#else
1523 (void )theBits;
299e0ab9 1524#endif
299e0ab9 1525 return aSrcGetAlpha;
1526}
1527
2a332745 1528// =======================================================================
1529// function : defaultGlslVersion
1530// purpose :
1531// =======================================================================
1532int OpenGl_ShaderManager::defaultGlslVersion (const Handle(Graphic3d_ShaderProgram)& theProgram,
d95f5ce1 1533 const TCollection_AsciiString& theName,
2a332745 1534 int theBits,
1535 bool theUsesDerivates) const
1536{
1537 int aBits = theBits;
1538#if !defined(GL_ES_VERSION_2_0)
1539 if (myContext->core32 != NULL)
1540 {
1541 theProgram->SetHeader ("#version 150");
1542 }
1543 else
1544 {
1545 if ((theBits & OpenGl_PO_StippleLine) != 0)
1546 {
1547 if (myContext->IsGlGreaterEqual (3, 0))
1548 {
1549 theProgram->SetHeader ("#version 130");
1550 }
1551 else if (myContext->CheckExtension ("GL_EXT_gpu_shader4"))
1552 {
1553 theProgram->SetHeader ("#extension GL_EXT_gpu_shader4 : enable");
1554 }
1555 else
1556 {
1557 aBits = aBits & ~OpenGl_PO_StippleLine;
1558 }
1559 }
1560 }
1561 (void )theUsesDerivates;
1562#else
1563 // prefer "100 es" on OpenGL ES 3.0- devices (save the features unavailable before "300 es")
1564 // and "300 es" on OpenGL ES 3.1+ devices
1565 if (myContext->IsGlGreaterEqual (3, 1))
1566 {
1567 if ((theBits & OpenGl_PO_NeedsGeomShader) != 0)
1568 {
1569 theProgram->SetHeader (myContext->hasGeometryStage != OpenGl_FeatureInExtensions ? "#version 320 es" : "#version 310 es");
1570 }
1571 else
1572 {
1573 theProgram->SetHeader ("#version 300 es");
1574 }
1575 }
1576 else
1577 {
1578 if ((theBits & OpenGl_PO_WriteOit) != 0
1579 || (theBits & OpenGl_PO_StippleLine) != 0)
1580 {
1581 if (myContext->IsGlGreaterEqual (3, 0))
1582 {
1583 theProgram->SetHeader ("#version 300 es");
1584 }
1585 else
1586 {
1587 aBits = aBits & ~OpenGl_PO_WriteOit;
1588 aBits = aBits & ~OpenGl_PO_StippleLine;
1589 }
1590 }
1591 if (theUsesDerivates)
1592 {
1593 if (myContext->IsGlGreaterEqual (3, 0))
1594 {
1595 theProgram->SetHeader ("#version 300 es");
1596 }
1597 else if (myContext->oesStdDerivatives)
1598 {
1599 theProgram->SetHeader ("#extension GL_OES_standard_derivatives : enable");
1600 }
1601 }
1602 }
1603#endif
d95f5ce1 1604
1605 // should fit OpenGl_PO_NB
1606 char aBitsStr[64];
1607 Sprintf (aBitsStr, "%04x", aBits);
1608 theProgram->SetId (TCollection_AsciiString ("occt_") + theName + aBitsStr);
2a332745 1609 return aBits;
1610}
1611
1612// =======================================================================
1613// function : prepareGeomMainSrc
1614// purpose :
1615// =======================================================================
1616TCollection_AsciiString OpenGl_ShaderManager::prepareGeomMainSrc (OpenGl_ShaderObject::ShaderVariableList& theUnifoms,
1617 OpenGl_ShaderObject::ShaderVariableList& theStageInOuts,
1618 Standard_Integer theBits)
1619{
1620 if ((theBits & OpenGl_PO_NeedsGeomShader) == 0)
1621 {
1622 return TCollection_AsciiString();
1623 }
1624
1625 TCollection_AsciiString aSrcMainGeom =
1626 EOL"void main()"
1627 EOL"{";
1628
1629 if ((theBits & OpenGl_PO_MeshEdges) != 0)
1630 {
1631 theUnifoms.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 occViewport", Graphic3d_TOS_GEOMETRY));
1632 theUnifoms.Append (OpenGl_ShaderObject::ShaderVariable ("bool occIsQuadMode", Graphic3d_TOS_GEOMETRY));
1633 theUnifoms.Append (OpenGl_ShaderObject::ShaderVariable ("float occLineWidth", Graphic3d_TOS_GEOMETRY));
1634 theUnifoms.Append (OpenGl_ShaderObject::ShaderVariable ("float occLineWidth", Graphic3d_TOS_FRAGMENT));
1635 theUnifoms.Append (OpenGl_ShaderObject::ShaderVariable ("float occLineFeather", Graphic3d_TOS_FRAGMENT));
1636 theUnifoms.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 occWireframeColor", Graphic3d_TOS_FRAGMENT));
1637 theStageInOuts.Append(OpenGl_ShaderObject::ShaderVariable ("vec3 EdgeDistance", Graphic3d_TOS_GEOMETRY | Graphic3d_TOS_FRAGMENT));
1638
1639 aSrcMainGeom = TCollection_AsciiString()
1640 + EOL"vec3 ViewPortTransform (vec4 theVec)"
1641 EOL"{"
1642 EOL" vec3 aWinCoord = theVec.xyz / theVec.w;"
1643 EOL" aWinCoord = aWinCoord * 0.5 + 0.5;"
1644 EOL" aWinCoord.xy = aWinCoord.xy * occViewport.zw + occViewport.xy;"
1645 EOL" return aWinCoord;"
1646 EOL"}"
1647 + aSrcMainGeom
1648 + EOL" vec3 aSideA = ViewPortTransform (gl_in[2].gl_Position) - ViewPortTransform (gl_in[1].gl_Position);"
1649 EOL" vec3 aSideB = ViewPortTransform (gl_in[2].gl_Position) - ViewPortTransform (gl_in[0].gl_Position);"
1650 EOL" vec3 aSideC = ViewPortTransform (gl_in[1].gl_Position) - ViewPortTransform (gl_in[0].gl_Position);"
1651 EOL" float aQuadArea = abs (aSideB.x * aSideC.y - aSideB.y * aSideC.x);"
1652 EOL" vec3 aLenABC = vec3 (length (aSideA), length (aSideB), length (aSideC));"
1653 EOL" vec3 aHeightABC = vec3 (aQuadArea) / aLenABC;"
1654 EOL" aHeightABC = max (aHeightABC, vec3 (10.0 * occLineWidth));" // avoid shrunk presentation disappearing at distance
1655 EOL" float aQuadModeHeightC = occIsQuadMode ? occLineWidth + 1.0 : 0.0;";
1656 }
1657
1658 for (Standard_Integer aVertIter = 0; aVertIter < 3; ++aVertIter)
1659 {
1660 const TCollection_AsciiString aVertIndex (aVertIter);
1661 // pass variables from Vertex shader to Fragment shader through Geometry shader
1662 for (OpenGl_ShaderObject::ShaderVariableList::Iterator aVarListIter (theStageInOuts); aVarListIter.More(); aVarListIter.Next())
1663 {
1664 if (aVarListIter.Value().Stages == (Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT))
1665 {
1666 const TCollection_AsciiString aVarName = aVarListIter.Value().Name.Token (" ", 2);
1667 aSrcMainGeom += TCollection_AsciiString()
1668 + EOL" geomOut." + aVarName + " = geomIn[" + aVertIndex + "]." + aVarName + ";";
1669 }
1670 }
1671
1672 if ((theBits & OpenGl_PO_MeshEdges) != 0)
1673 {
1674 switch (aVertIter)
1675 {
1676 case 0: aSrcMainGeom += EOL" EdgeDistance = vec3 (aHeightABC[0], 0.0, aQuadModeHeightC);"; break;
1677 case 1: aSrcMainGeom += EOL" EdgeDistance = vec3 (0.0, aHeightABC[1], aQuadModeHeightC);"; break;
1678 case 2: aSrcMainGeom += EOL" EdgeDistance = vec3 (0.0, 0.0, aHeightABC[2]);"; break;
1679 }
1680 }
1681 aSrcMainGeom += TCollection_AsciiString()
1682 + EOL" gl_Position = gl_in[" + aVertIndex + "].gl_Position;"
1683 EOL" EmitVertex();";
1684 }
1685 aSrcMainGeom +=
1686 EOL" EndPrimitive();"
1687 EOL"}";
1688
1689 return aSrcMainGeom;
1690}
1691
b86bb3df 1692// =======================================================================
dc89236f 1693// function : prepareStdProgramUnlit
8625ef7e 1694// purpose :
1695// =======================================================================
dc89236f 1696Standard_Boolean OpenGl_ShaderManager::prepareStdProgramUnlit (Handle(OpenGl_ShaderProgram)& theProgram,
6ef0d6f1 1697 Standard_Integer theBits,
1698 Standard_Boolean theIsOutline)
8625ef7e 1699{
1700 Handle(Graphic3d_ShaderProgram) aProgramSrc = new Graphic3d_ShaderProgram();
2a332745 1701 TCollection_AsciiString aSrcVert, aSrcVertExtraMain, aSrcVertExtraFunc, aSrcGetAlpha, aSrcVertEndMain;
1702 TCollection_AsciiString aSrcFrag, aSrcFragExtraMain;
8625ef7e 1703 TCollection_AsciiString aSrcFragGetColor = EOL"vec4 getColor(void) { return occColor; }";
2a332745 1704 TCollection_AsciiString aSrcFragMainGetColor = EOL" occSetFragColor (getFinalColor());";
3b4c6945 1705 OpenGl_ShaderObject::ShaderVariableList aUniforms, aStageInOuts;
2a332745 1706
8625ef7e 1707 if ((theBits & OpenGl_PO_Point) != 0)
1708 {
1709 #if defined(GL_ES_VERSION_2_0)
1710 aSrcVertExtraMain += EOL" gl_PointSize = occPointSize;";
1711 #endif
8625ef7e 1712
6ef0d6f1 1713 if ((theBits & OpenGl_PO_HasTextures) != 0)
8625ef7e 1714 {
6ef0d6f1 1715 if ((theBits & OpenGl_PO_HasTextures) == OpenGl_PO_TextureRGB)
1716 {
1717 aSrcFragGetColor =
1718 EOL"vec4 getColor(void) { return occTexture2D(occSamplerBaseColor, " THE_VEC2_glPointCoord "); }";
1719 }
299e0ab9 1720
fd59283a 1721 aSrcGetAlpha = pointSpriteAlphaSrc (theBits);
1722
1723 #if !defined(GL_ES_VERSION_2_0)
1724 if (myContext->core11 != NULL
1725 && myContext->IsGlGreaterEqual (2, 1))
1726 {
1727 aProgramSrc->SetHeader ("#version 120"); // gl_PointCoord has been added since GLSL 1.2
1728 }
1729 #endif
1730
1731 aSrcFragMainGetColor =
1732 EOL" vec4 aColor = getColor();"
1733 EOL" aColor.a = getAlpha();"
1734 EOL" if (aColor.a <= 0.1) discard;"
b17e5bae 1735 EOL" occSetFragColor (aColor);";
fd59283a 1736 }
1737 else
1738 {
1739 aSrcFragMainGetColor =
1740 EOL" vec4 aColor = getColor();"
1741 EOL" if (aColor.a <= 0.1) discard;"
b17e5bae 1742 EOL" occSetFragColor (aColor);";
fd59283a 1743 }
8625ef7e 1744 }
6c6aadb1 1745 else
1746 {
3b4c6945 1747 if ((theBits & OpenGl_PO_TextureRGB) != 0 || (theBits & OpenGl_PO_TextureEnv) != 0)
1748 {
1749 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 TexCoord", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
1750 }
1751
6ef0d6f1 1752 if ((theBits & OpenGl_PO_HasTextures) == OpenGl_PO_TextureRGB)
6c6aadb1 1753 {
79f4f036 1754 aSrcVertExtraMain += THE_VARY_TexCoord_Trsf;
6c6aadb1 1755
1756 aSrcFragGetColor =
cc8cbabe 1757 EOL"vec4 getColor(void) { return occTexture2D(occSamplerBaseColor, TexCoord.st / TexCoord.w); }";
6c6aadb1 1758 }
e135a155 1759 else if ((theBits & OpenGl_PO_TextureEnv) != 0)
1760 {
e135a155 1761 aSrcVertExtraFunc = THE_FUNC_transformNormal;
1762
1763 aSrcVertExtraMain +=
1764 EOL" vec4 aPosition = occWorldViewMatrix * occModelWorldMatrix * occVertex;"
1765 EOL" vec3 aNormal = transformNormal (occNormal);"
1766 EOL" vec3 aReflect = reflect (normalize (aPosition.xyz), aNormal);"
1767 EOL" aReflect.z += 1.0;"
b872ca56 1768 EOL" TexCoord = vec4(aReflect.xy * inversesqrt (dot (aReflect, aReflect)) * 0.5 + vec2 (0.5), 0.0, 1.0);";
e135a155 1769
1770 aSrcFragGetColor =
cc8cbabe 1771 EOL"vec4 getColor(void) { return occTexture2D (occSamplerBaseColor, TexCoord.st); }";
e135a155 1772 }
6c6aadb1 1773 }
8625ef7e 1774 if ((theBits & OpenGl_PO_VertColor) != 0)
1775 {
3b4c6945 1776 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 VertColor", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
8625ef7e 1777 aSrcVertExtraMain += EOL" VertColor = occVertColor;";
8625ef7e 1778 aSrcFragGetColor = EOL"vec4 getColor(void) { return VertColor; }";
1779 }
daf73ab7 1780
1781 int aNbClipPlanes = 0;
1a7ece8f 1782 if ((theBits & OpenGl_PO_ClipPlanesN) != 0)
8625ef7e 1783 {
3b4c6945 1784 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 PositionWorld", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
1785 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 Position", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
8625ef7e 1786 aSrcVertExtraMain +=
1787 EOL" PositionWorld = occModelWorldMatrix * occVertex;"
1788 EOL" Position = occWorldViewMatrix * PositionWorld;";
1a7ece8f 1789
2a332745 1790 if ((theBits & OpenGl_PO_ClipPlanesN) == OpenGl_PO_ClipPlanesN)
1791 {
1792 aNbClipPlanes = Graphic3d_ShaderProgram::THE_MAX_CLIP_PLANES_DEFAULT;
1793 aSrcFragExtraMain += (theBits & OpenGl_PO_ClipChains) != 0
1794 ? THE_FRAG_CLIP_CHAINS_N
1795 : THE_FRAG_CLIP_PLANES_N;
1796 }
1797 else if ((theBits & OpenGl_PO_ClipPlanes1) != 0)
1a7ece8f 1798 {
daf73ab7 1799 aNbClipPlanes = 1;
1a7ece8f 1800 aSrcFragExtraMain += THE_FRAG_CLIP_PLANES_1;
1801 }
1802 else if ((theBits & OpenGl_PO_ClipPlanes2) != 0)
1803 {
daf73ab7 1804 aNbClipPlanes = 2;
25c35042 1805 aSrcFragExtraMain += (theBits & OpenGl_PO_ClipChains) != 0
1806 ? THE_FRAG_CLIP_CHAINS_2
1807 : THE_FRAG_CLIP_PLANES_2;
1a7ece8f 1808 }
8625ef7e 1809 }
a1073ae2 1810 if ((theBits & OpenGl_PO_WriteOit) != 0)
1811 {
b17e5bae 1812 aProgramSrc->SetNbFragmentOutputs (2);
1813 aProgramSrc->SetWeightOitOutput (true);
a1073ae2 1814 }
8625ef7e 1815
6ef0d6f1 1816 if (theIsOutline)
1817 {
1818 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("float occOrthoScale", Graphic3d_TOS_VERTEX));
1819 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("float occSilhouetteThickness", Graphic3d_TOS_VERTEX));
1820 aSrcVertEndMain = THE_VERT_gl_Position_OUTLINE;
1821 }
1822 else if ((theBits & OpenGl_PO_StippleLine) != 0)
ac116c22 1823 {
d95f5ce1 1824 const Standard_Integer aBits = defaultGlslVersion (aProgramSrc, "unlit", theBits);
2a332745 1825 if ((aBits & OpenGl_PO_StippleLine) != 0)
ac116c22 1826 {
ea298f59 1827 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("int uPattern", Graphic3d_TOS_FRAGMENT));
1828 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("float uFactor", Graphic3d_TOS_FRAGMENT));
3b4c6945 1829 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec2 ScreenSpaceCoord", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
ac116c22 1830 aSrcVertEndMain =
1831 EOL" ScreenSpaceCoord = gl_Position.xy / gl_Position.w;";
1832 aSrcFragMainGetColor =
1833 EOL" float anAngle = atan (dFdx (ScreenSpaceCoord.x), dFdy (ScreenSpaceCoord.y));"
1834 EOL" float aRotatePoint = gl_FragCoord.x * sin (anAngle) + gl_FragCoord.y * cos (anAngle);"
1835 EOL" uint aBit = uint (floor (aRotatePoint / uFactor + 0.5)) & 15U;"
1836 EOL" if ((uint (uPattern) & (1U << aBit)) == 0U) discard;"
2a332745 1837 EOL" vec4 aColor = getFinalColor();"
ac116c22 1838 EOL" if (aColor.a <= 0.1) discard;"
b17e5bae 1839 EOL" occSetFragColor (aColor);";
ac116c22 1840 }
1841 else
1842 {
3b4c6945 1843 myContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, "Warning: stipple lines in GLSL will be ignored.");
ac116c22 1844 }
1845 }
1846
8625ef7e 1847 aSrcVert =
e135a155 1848 aSrcVertExtraFunc
8625ef7e 1849 + EOL"void main()"
1850 EOL"{"
1851 + aSrcVertExtraMain
6ef0d6f1 1852 + THE_VERT_gl_Position
ac116c22 1853 + aSrcVertEndMain
1854 + EOL"}";
8625ef7e 1855
2a332745 1856 TCollection_AsciiString aSrcGeom = prepareGeomMainSrc (aUniforms, aStageInOuts, theBits);
1857 aSrcFragGetColor += (theBits & OpenGl_PO_MeshEdges) != 0
1858 ? THE_FRAG_WIREFRAME_COLOR
1859 : EOL"#define getFinalColor getColor";
1860
8625ef7e 1861 aSrcFrag =
3b4c6945 1862 aSrcFragGetColor
299e0ab9 1863 + aSrcGetAlpha
8625ef7e 1864 + EOL"void main()"
1865 EOL"{"
1866 + aSrcFragExtraMain
1867 + aSrcFragMainGetColor
1868 + EOL"}";
1869
6ef0d6f1 1870 defaultGlslVersion (aProgramSrc, theIsOutline ? "outline" : "unlit", theBits);
daf73ab7 1871 aProgramSrc->SetNbLightsMax (0);
1872 aProgramSrc->SetNbClipPlanesMax (aNbClipPlanes);
c40eb6b9 1873 aProgramSrc->SetAlphaTest ((theBits & OpenGl_PO_AlphaTest) != 0);
2a332745 1874 const Standard_Integer aNbGeomInputVerts = !aSrcGeom.IsEmpty() ? 3 : 0;
1875 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcVert, Graphic3d_TOS_VERTEX, aUniforms, aStageInOuts, "", "", aNbGeomInputVerts));
1876 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcGeom, Graphic3d_TOS_GEOMETRY, aUniforms, aStageInOuts, "geomIn", "geomOut", aNbGeomInputVerts));
1877 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcFrag, Graphic3d_TOS_FRAGMENT, aUniforms, aStageInOuts, "", "", aNbGeomInputVerts));
8625ef7e 1878 TCollection_AsciiString aKey;
1879 if (!Create (aProgramSrc, aKey, theProgram))
1880 {
1881 theProgram = new OpenGl_ShaderProgram(); // just mark as invalid
1882 return Standard_False;
1883 }
1884 return Standard_True;
1885}
1886
299e0ab9 1887// =======================================================================
1888// function : pointSpriteShadingSrc
1889// purpose :
1890// =======================================================================
1891TCollection_AsciiString OpenGl_ShaderManager::pointSpriteShadingSrc (const TCollection_AsciiString theBaseColorSrc,
1892 const Standard_Integer theBits)
1893{
1894 TCollection_AsciiString aSrcFragGetColor;
6ef0d6f1 1895 if ((theBits & OpenGl_PO_HasTextures) == OpenGl_PO_TextureA)
299e0ab9 1896 {
fd59283a 1897 aSrcFragGetColor = pointSpriteAlphaSrc (theBits) +
299e0ab9 1898 EOL"vec4 getColor(void)"
1899 EOL"{"
1900 EOL" vec4 aColor = " + theBaseColorSrc + ";"
1901 EOL" aColor.a = getAlpha();"
1902 EOL" if (aColor.a <= 0.1) discard;"
1903 EOL" return aColor;"
1904 EOL"}";
1905 }
6ef0d6f1 1906 else if ((theBits & OpenGl_PO_HasTextures) == OpenGl_PO_TextureRGB)
299e0ab9 1907 {
1908 aSrcFragGetColor = TCollection_AsciiString() +
1909 EOL"vec4 getColor(void)"
1910 EOL"{"
1911 EOL" vec4 aColor = " + theBaseColorSrc + ";"
cc8cbabe 1912 EOL" aColor = occTexture2D(occSamplerBaseColor, " THE_VEC2_glPointCoord ") * aColor;"
299e0ab9 1913 EOL" if (aColor.a <= 0.1) discard;"
1914 EOL" return aColor;"
1915 EOL"}";
1916 }
1917
1918 return aSrcFragGetColor;
1919}
1920
256f9ac0 1921// =======================================================================
1922// function : stdComputeLighting
1923// purpose :
1924// =======================================================================
daf73ab7 1925TCollection_AsciiString OpenGl_ShaderManager::stdComputeLighting (Standard_Integer& theNbLights,
1926 Standard_Boolean theHasVertColor)
256f9ac0 1927{
256f9ac0 1928 TCollection_AsciiString aLightsFunc, aLightsLoop;
daf73ab7 1929 theNbLights = 0;
992ed6b3 1930 const Handle(Graphic3d_LightSet)& aLights = myLightSourceState.LightSources();
1931 if (!aLights.IsNull())
256f9ac0 1932 {
992ed6b3 1933 theNbLights = aLights->NbEnabled();
1934 if (theNbLights <= THE_NB_UNROLLED_LIGHTS_MAX)
1935 {
1936 Standard_Integer anIndex = 0;
1937 for (Graphic3d_LightSet::Iterator aLightIter (aLights, Graphic3d_LightSet::IterationFilter_ExcludeDisabledAndAmbient);
1938 aLightIter.More(); aLightIter.Next(), ++anIndex)
1939 {
1940 switch (aLightIter.Value()->Type())
1941 {
1942 case Graphic3d_TOLS_AMBIENT:
1943 --anIndex;
1944 break; // skip ambient
1945 case Graphic3d_TOLS_DIRECTIONAL:
1946 aLightsLoop = aLightsLoop + EOL" directionalLight (" + anIndex + ", theNormal, theView, theIsFront);";
1947 break;
1948 case Graphic3d_TOLS_POSITIONAL:
1949 aLightsLoop = aLightsLoop + EOL" pointLight (" + anIndex + ", theNormal, theView, aPoint, theIsFront);";
1950 break;
1951 case Graphic3d_TOLS_SPOT:
1952 aLightsLoop = aLightsLoop + EOL" spotLight (" + anIndex + ", theNormal, theView, aPoint, theIsFront);";
1953 break;
1954 }
1955 }
1956 }
1957 else
256f9ac0 1958 {
992ed6b3 1959 theNbLights = roundUpMaxLightSources (theNbLights);
1960 bool isFirstInLoop = true;
1961 aLightsLoop = aLightsLoop +
1962 EOL" for (int anIndex = 0; anIndex < occLightSourcesCount; ++anIndex)"
1963 EOL" {"
1964 EOL" int aType = occLight_Type (anIndex);";
1965 if (aLights->NbEnabledLightsOfType (Graphic3d_TOLS_DIRECTIONAL) > 0)
256f9ac0 1966 {
992ed6b3 1967 isFirstInLoop = false;
1968 aLightsLoop +=
1969 EOL" if (aType == OccLightType_Direct)"
1970 EOL" {"
1971 EOL" directionalLight (anIndex, theNormal, theView, theIsFront);"
1972 EOL" }";
1973 }
1974 if (aLights->NbEnabledLightsOfType (Graphic3d_TOLS_POSITIONAL) > 0)
1975 {
1976 if (!isFirstInLoop)
1977 {
1978 aLightsLoop += EOL" else ";
1979 }
1980 isFirstInLoop = false;
1981 aLightsLoop +=
1982 EOL" if (aType == OccLightType_Point)"
1983 EOL" {"
1984 EOL" pointLight (anIndex, theNormal, theView, aPoint, theIsFront);"
1985 EOL" }";
1986 }
1987 if (aLights->NbEnabledLightsOfType (Graphic3d_TOLS_SPOT) > 0)
1988 {
1989 if (!isFirstInLoop)
1990 {
1991 aLightsLoop += EOL" else ";
1992 }
1993 isFirstInLoop = false;
1994 aLightsLoop +=
1995 EOL" if (aType == OccLightType_Spot)"
1996 EOL" {"
1997 EOL" spotLight (anIndex, theNormal, theView, aPoint, theIsFront);"
1998 EOL" }";
256f9ac0 1999 }
992ed6b3 2000 aLightsLoop += EOL" }";
f68acbf4 2001 }
992ed6b3 2002 if (aLights->NbEnabledLightsOfType (Graphic3d_TOLS_DIRECTIONAL) == 1
2003 && theNbLights == 1)
f68acbf4 2004 {
2005 // use the version with hard-coded first index
2006 aLightsLoop = EOL" directionalLightFirst(theNormal, theView, theIsFront);";
2007 aLightsFunc += THE_FUNC_directionalLightFirst;
2008 }
992ed6b3 2009 else if (aLights->NbEnabledLightsOfType (Graphic3d_TOLS_DIRECTIONAL) > 0)
f68acbf4 2010 {
2011 aLightsFunc += THE_FUNC_directionalLight;
2012 }
992ed6b3 2013 if (aLights->NbEnabledLightsOfType (Graphic3d_TOLS_POSITIONAL) > 0)
f68acbf4 2014 {
2015 aLightsFunc += THE_FUNC_pointLight;
2016 }
992ed6b3 2017 if (aLights->NbEnabledLightsOfType (Graphic3d_TOLS_SPOT) > 0)
f68acbf4 2018 {
2019 aLightsFunc += THE_FUNC_spotLight;
256f9ac0 2020 }
2021 }
2022
abdf0b10 2023 TCollection_AsciiString aGetMatAmbient = "theIsFront ? occFrontMaterial_Ambient() : occBackMaterial_Ambient();";
2024 TCollection_AsciiString aGetMatDiffuse = "theIsFront ? occFrontMaterial_Diffuse() : occBackMaterial_Diffuse();";
2025 if (theHasVertColor)
2026 {
2027 aGetMatAmbient = "getVertColor();";
2028 aGetMatDiffuse = "getVertColor();";
2029 }
2030
256f9ac0 2031 return TCollection_AsciiString()
2032 + THE_FUNC_lightDef
2033 + aLightsFunc
2034 + EOL
2035 EOL"vec4 computeLighting (in vec3 theNormal,"
2036 EOL" in vec3 theView,"
2037 EOL" in vec4 thePoint,"
2038 EOL" in bool theIsFront)"
2039 EOL"{"
2040 EOL" Ambient = occLightAmbient.rgb;"
2041 EOL" Diffuse = vec3 (0.0);"
2042 EOL" Specular = vec3 (0.0);"
2043 EOL" vec3 aPoint = thePoint.xyz / thePoint.w;"
2044 + aLightsLoop
363bc51b 2045 + EOL" vec4 aMatAmbient = " + aGetMatAmbient
2046 + EOL" vec4 aMatDiffuse = " + aGetMatDiffuse
2047 + EOL" vec4 aMatSpecular = theIsFront ? occFrontMaterial_Specular() : occBackMaterial_Specular();"
2048 EOL" vec4 aMatEmission = theIsFront ? occFrontMaterial_Emission() : occBackMaterial_Emission();"
2049 EOL" vec3 aColor = Ambient * aMatAmbient.rgb"
2050 EOL" + Diffuse * aMatDiffuse.rgb"
2051 EOL" + Specular * aMatSpecular.rgb"
2052 EOL" + aMatEmission.rgb;"
2053 EOL" return vec4 (aColor, aMatDiffuse.a);"
256f9ac0 2054 EOL"}";
2055}
2056
8625ef7e 2057// =======================================================================
2058// function : prepareStdProgramGouraud
2059// purpose :
2060// =======================================================================
2061Standard_Boolean OpenGl_ShaderManager::prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram,
2062 const Standard_Integer theBits)
2063{
2064 Handle(Graphic3d_ShaderProgram) aProgramSrc = new Graphic3d_ShaderProgram();
3b4c6945 2065 TCollection_AsciiString aSrcVert, aSrcVertColor, aSrcVertExtraMain;
2a332745 2066 TCollection_AsciiString aSrcFrag, aSrcFragExtraMain;
6c6aadb1 2067 TCollection_AsciiString aSrcFragGetColor = EOL"vec4 getColor(void) { return gl_FrontFacing ? FrontColor : BackColor; }";
3b4c6945 2068 OpenGl_ShaderObject::ShaderVariableList aUniforms, aStageInOuts;
2069
8625ef7e 2070 if ((theBits & OpenGl_PO_Point) != 0)
2071 {
2072 #if defined(GL_ES_VERSION_2_0)
2073 aSrcVertExtraMain += EOL" gl_PointSize = occPointSize;";
2074 #endif
299e0ab9 2075
6ef0d6f1 2076 if ((theBits & OpenGl_PO_HasTextures) != 0)
6c6aadb1 2077 {
299e0ab9 2078 #if !defined(GL_ES_VERSION_2_0)
2079 if (myContext->core11 != NULL
2080 && myContext->IsGlGreaterEqual (2, 1))
2081 {
2082 aProgramSrc->SetHeader ("#version 120"); // gl_PointCoord has been added since GLSL 1.2
2083 }
2084 #endif
2085
2086 aSrcFragGetColor = pointSpriteShadingSrc ("gl_FrontFacing ? FrontColor : BackColor", theBits);
6c6aadb1 2087 }
2088 }
2089 else
2090 {
6ef0d6f1 2091 if ((theBits & OpenGl_PO_HasTextures) == OpenGl_PO_TextureRGB)
6c6aadb1 2092 {
3b4c6945 2093 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 TexCoord", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
79f4f036 2094 aSrcVertExtraMain += THE_VARY_TexCoord_Trsf;
6c6aadb1 2095
2096 aSrcFragGetColor =
2097 EOL"vec4 getColor(void)"
2098 EOL"{"
2099 EOL" vec4 aColor = gl_FrontFacing ? FrontColor : BackColor;"
cc8cbabe 2100 EOL" return occTexture2D(occSamplerBaseColor, TexCoord.st / TexCoord.w) * aColor;"
6c6aadb1 2101 EOL"}";
2102 }
2103 }
299e0ab9 2104
2105 if ((theBits & OpenGl_PO_VertColor) != 0)
2106 {
2107 aSrcVertColor = EOL"vec4 getVertColor(void) { return occVertColor; }";
2108 }
2109
daf73ab7 2110 int aNbClipPlanes = 0;
1a7ece8f 2111 if ((theBits & OpenGl_PO_ClipPlanesN) != 0)
8625ef7e 2112 {
3b4c6945 2113 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 PositionWorld", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
2114 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 Position", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
8625ef7e 2115 aSrcVertExtraMain +=
2116 EOL" PositionWorld = aPositionWorld;"
2117 EOL" Position = aPosition;";
1a7ece8f 2118
2a332745 2119 if ((theBits & OpenGl_PO_ClipPlanesN) == OpenGl_PO_ClipPlanesN)
2120 {
2121 aNbClipPlanes = Graphic3d_ShaderProgram::THE_MAX_CLIP_PLANES_DEFAULT;
2122 aSrcFragExtraMain += (theBits & OpenGl_PO_ClipChains) != 0
2123 ? THE_FRAG_CLIP_CHAINS_N
2124 : THE_FRAG_CLIP_PLANES_N;
2125 }
2126 else if ((theBits & OpenGl_PO_ClipPlanes1) != 0)
1a7ece8f 2127 {
daf73ab7 2128 aNbClipPlanes = 1;
1a7ece8f 2129 aSrcFragExtraMain += THE_FRAG_CLIP_PLANES_1;
2130 }
2131 else if ((theBits & OpenGl_PO_ClipPlanes2) != 0)
2132 {
daf73ab7 2133 aNbClipPlanes = 2;
25c35042 2134 aSrcFragExtraMain += (theBits & OpenGl_PO_ClipChains) != 0
2135 ? THE_FRAG_CLIP_CHAINS_2
2136 : THE_FRAG_CLIP_PLANES_2;
1a7ece8f 2137 }
8625ef7e 2138 }
a1073ae2 2139 if ((theBits & OpenGl_PO_WriteOit) != 0)
2140 {
b17e5bae 2141 aProgramSrc->SetNbFragmentOutputs (2);
2142 aProgramSrc->SetWeightOitOutput (true);
a1073ae2 2143 }
8625ef7e 2144
3b4c6945 2145 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 FrontColor", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
2146 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 BackColor", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
2147
daf73ab7 2148 Standard_Integer aNbLights = 0;
2149 const TCollection_AsciiString aLights = stdComputeLighting (aNbLights, (theBits & OpenGl_PO_VertColor) != 0);
8625ef7e 2150 aSrcVert = TCollection_AsciiString()
2151 + THE_FUNC_transformNormal
2152 + EOL
abdf0b10 2153 + aSrcVertColor
256f9ac0 2154 + aLights
8625ef7e 2155 + EOL"void main()"
2156 EOL"{"
2157 EOL" vec4 aPositionWorld = occModelWorldMatrix * occVertex;"
2158 EOL" vec4 aPosition = occWorldViewMatrix * aPositionWorld;"
2159 EOL" vec3 aNormal = transformNormal (occNormal);"
2160 EOL" vec3 aView = vec3 (0.0, 0.0, 1.0);"
2161 EOL" FrontColor = computeLighting (normalize (aNormal), normalize (aView), aPosition, true);"
2162 EOL" BackColor = computeLighting (normalize (aNormal), normalize (aView), aPosition, false);"
2163 + aSrcVertExtraMain
6ef0d6f1 2164 + THE_VERT_gl_Position
2165 + EOL"}";
8625ef7e 2166
2a332745 2167 TCollection_AsciiString aSrcGeom = prepareGeomMainSrc (aUniforms, aStageInOuts, theBits);
2168 aSrcFragGetColor += (theBits & OpenGl_PO_MeshEdges) != 0
2169 ? THE_FRAG_WIREFRAME_COLOR
2170 : EOL"#define getFinalColor getColor";
2171
8625ef7e 2172 aSrcFrag = TCollection_AsciiString()
6c6aadb1 2173 + aSrcFragGetColor
8625ef7e 2174 + EOL"void main()"
2175 EOL"{"
2176 + aSrcFragExtraMain
2a332745 2177 + EOL" occSetFragColor (getFinalColor());"
a1073ae2 2178 + EOL"}";
8625ef7e 2179
d95f5ce1 2180 const TCollection_AsciiString aProgId = TCollection_AsciiString ("gouraud-") + genLightKey (myLightSourceState.LightSources()) + "-";
2181 defaultGlslVersion (aProgramSrc, aProgId, theBits);
daf73ab7 2182 aProgramSrc->SetNbLightsMax (aNbLights);
2183 aProgramSrc->SetNbClipPlanesMax (aNbClipPlanes);
c40eb6b9 2184 aProgramSrc->SetAlphaTest ((theBits & OpenGl_PO_AlphaTest) != 0);
2a332745 2185 const Standard_Integer aNbGeomInputVerts = !aSrcGeom.IsEmpty() ? 3 : 0;
2186 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcVert, Graphic3d_TOS_VERTEX, aUniforms, aStageInOuts, "", "", aNbGeomInputVerts));
2187 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcGeom, Graphic3d_TOS_GEOMETRY, aUniforms, aStageInOuts, "geomIn", "geomOut", aNbGeomInputVerts));
2188 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcFrag, Graphic3d_TOS_FRAGMENT, aUniforms, aStageInOuts, "", "", aNbGeomInputVerts));
8625ef7e 2189 TCollection_AsciiString aKey;
2190 if (!Create (aProgramSrc, aKey, theProgram))
2191 {
2192 theProgram = new OpenGl_ShaderProgram(); // just mark as invalid
2193 return Standard_False;
2194 }
2195 return Standard_True;
2196}
2197
2198// =======================================================================
2199// function : prepareStdProgramPhong
2200// purpose :
2201// =======================================================================
2202Standard_Boolean OpenGl_ShaderManager::prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
8c3237d4 2203 const Standard_Integer theBits,
2204 const Standard_Boolean theIsFlatNormal)
8625ef7e 2205{
6c6aadb1 2206 #define thePhongCompLight "computeLighting (normalize (Normal), normalize (View), Position, gl_FrontFacing)"
8c3237d4 2207 const bool isFlatNormal = theIsFlatNormal
c39bb31b 2208 && myContext->hasFlatShading != OpenGl_FeatureNotAvailable;
2209 const char* aDFdxSignReversion = "";
2210#if defined(GL_ES_VERSION_2_0)
2211 if (isFlatNormal != theIsFlatNormal)
2212 {
2213 myContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
2214 GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_MEDIUM,
2215 "Warning: flat shading requires OpenGL ES 3.0+ or GL_OES_standard_derivatives extension.");
2216 }
2217 else if (isFlatNormal
6997ff1c 2218 && myContext->Vendor().Search("qualcomm") != -1)
c39bb31b 2219 {
2220 // workaround Adreno driver bug computing reversed normal using dFdx/dFdy
2221 aDFdxSignReversion = "-";
2222 myContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_MEDIUM,
2223 "Warning: applied workaround for flat shading normal computation using dFdx/dFdy on Adreno");
2224 }
8c3237d4 2225#endif
8625ef7e 2226 Handle(Graphic3d_ShaderProgram) aProgramSrc = new Graphic3d_ShaderProgram();
3b4c6945 2227 TCollection_AsciiString aSrcVert, aSrcVertExtraFunc, aSrcVertExtraMain;
2a332745 2228 TCollection_AsciiString aSrcFrag, aSrcFragExtraOut, aSrcFragGetVertColor, aSrcFragExtraMain;
6c6aadb1 2229 TCollection_AsciiString aSrcFragGetColor = EOL"vec4 getColor(void) { return " thePhongCompLight "; }";
3b4c6945 2230 OpenGl_ShaderObject::ShaderVariableList aUniforms, aStageInOuts;
8625ef7e 2231 if ((theBits & OpenGl_PO_Point) != 0)
2232 {
2233 #if defined(GL_ES_VERSION_2_0)
2234 aSrcVertExtraMain += EOL" gl_PointSize = occPointSize;";
2235 #endif
6c6aadb1 2236
6ef0d6f1 2237 if ((theBits & OpenGl_PO_HasTextures) != 0)
6c6aadb1 2238 {
299e0ab9 2239 #if !defined(GL_ES_VERSION_2_0)
2240 if (myContext->core11 != NULL
2241 && myContext->IsGlGreaterEqual (2, 1))
2242 {
2243 aProgramSrc->SetHeader ("#version 120"); // gl_PointCoord has been added since GLSL 1.2
2244 }
2245 #endif
2246
2247 aSrcFragGetColor = pointSpriteShadingSrc (thePhongCompLight, theBits);
6c6aadb1 2248 }
2249 }
2250 else
2251 {
6ef0d6f1 2252 if ((theBits & OpenGl_PO_HasTextures) == OpenGl_PO_TextureRGB)
6c6aadb1 2253 {
3b4c6945 2254 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 TexCoord", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
79f4f036 2255 aSrcVertExtraMain += THE_VARY_TexCoord_Trsf;
6c6aadb1 2256
2257 aSrcFragGetColor =
2258 EOL"vec4 getColor(void)"
2259 EOL"{"
2260 EOL" vec4 aColor = " thePhongCompLight ";"
cc8cbabe 2261 EOL" return occTexture2D(occSamplerBaseColor, TexCoord.st / TexCoord.w) * aColor;"
6c6aadb1 2262 EOL"}";
2263 }
2264 }
2265
299e0ab9 2266 if ((theBits & OpenGl_PO_VertColor) != 0)
2267 {
3b4c6945 2268 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 VertColor", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
299e0ab9 2269 aSrcVertExtraMain += EOL" VertColor = occVertColor;";
3b4c6945 2270 aSrcFragGetVertColor = EOL"vec4 getVertColor(void) { return VertColor; }";
299e0ab9 2271 }
2272
daf73ab7 2273 int aNbClipPlanes = 0;
1a7ece8f 2274 if ((theBits & OpenGl_PO_ClipPlanesN) != 0)
8625ef7e 2275 {
2a332745 2276 if ((theBits & OpenGl_PO_ClipPlanesN) == OpenGl_PO_ClipPlanesN)
2277 {
2278 aNbClipPlanes = Graphic3d_ShaderProgram::THE_MAX_CLIP_PLANES_DEFAULT;
2279 aSrcFragExtraMain += (theBits & OpenGl_PO_ClipChains) != 0
2280 ? THE_FRAG_CLIP_CHAINS_N
2281 : THE_FRAG_CLIP_PLANES_N;
2282 }
2283 else if ((theBits & OpenGl_PO_ClipPlanes1) != 0)
1a7ece8f 2284 {
daf73ab7 2285 aNbClipPlanes = 1;
1a7ece8f 2286 aSrcFragExtraMain += THE_FRAG_CLIP_PLANES_1;
2287 }
2288 else if ((theBits & OpenGl_PO_ClipPlanes2) != 0)
2289 {
daf73ab7 2290 aNbClipPlanes = 2;
25c35042 2291 aSrcFragExtraMain += (theBits & OpenGl_PO_ClipChains) != 0
2292 ? THE_FRAG_CLIP_CHAINS_2
2293 : THE_FRAG_CLIP_PLANES_2;
1a7ece8f 2294 }
8625ef7e 2295 }
a1073ae2 2296 if ((theBits & OpenGl_PO_WriteOit) != 0)
2297 {
b17e5bae 2298 aProgramSrc->SetNbFragmentOutputs (2);
2299 aProgramSrc->SetWeightOitOutput (true);
a1073ae2 2300 }
8625ef7e 2301
3b4c6945 2302 if (isFlatNormal)
2303 {
2304 aSrcFragExtraOut += EOL"vec3 Normal;";
2305 aSrcFragExtraMain += TCollection_AsciiString()
2306 + EOL" Normal = " + aDFdxSignReversion + "normalize (cross (dFdx (Position.xyz / Position.w), dFdy (Position.xyz / Position.w)));"
2307 EOL" if (!gl_FrontFacing) { Normal = -Normal; }";
2308 }
2309 else
2310 {
2311 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec3 Normal", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
2312 aSrcVertExtraFunc += THE_FUNC_transformNormal;
2313 aSrcVertExtraMain += EOL" Normal = transformNormal (occNormal);";
2314 }
2315
2316 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 PositionWorld", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
2317 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec4 Position", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
2318 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec3 View", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
2319
8625ef7e 2320 aSrcVert = TCollection_AsciiString()
3b4c6945 2321 + aSrcVertExtraFunc
8625ef7e 2322 + EOL"void main()"
2323 EOL"{"
2324 EOL" PositionWorld = occModelWorldMatrix * occVertex;"
2325 EOL" Position = occWorldViewMatrix * PositionWorld;"
8c3237d4 2326 + EOL" View = vec3 (0.0, 0.0, 1.0);"
8625ef7e 2327 + aSrcVertExtraMain
6ef0d6f1 2328 + THE_VERT_gl_Position
2329 + EOL"}";
8625ef7e 2330
2a332745 2331 TCollection_AsciiString aSrcGeom = prepareGeomMainSrc (aUniforms, aStageInOuts, theBits);
2332 aSrcFragGetColor += (theBits & OpenGl_PO_MeshEdges) != 0
2333 ? THE_FRAG_WIREFRAME_COLOR
2334 : EOL"#define getFinalColor getColor";
2335
daf73ab7 2336 Standard_Integer aNbLights = 0;
2337 const TCollection_AsciiString aLights = stdComputeLighting (aNbLights, (theBits & OpenGl_PO_VertColor) != 0);
8625ef7e 2338 aSrcFrag = TCollection_AsciiString()
8625ef7e 2339 + EOL
6c6aadb1 2340 + aSrcFragExtraOut
2341 + aSrcFragGetVertColor
256f9ac0 2342 + aLights
6c6aadb1 2343 + aSrcFragGetColor
8625ef7e 2344 + EOL
2345 EOL"void main()"
2346 EOL"{"
2347 + aSrcFragExtraMain
2a332745 2348 + EOL" occSetFragColor (getFinalColor());"
a1073ae2 2349 + EOL"}";
8625ef7e 2350
d95f5ce1 2351 const TCollection_AsciiString aProgId = TCollection_AsciiString (theIsFlatNormal ? "flat-" : "phong-") + genLightKey (myLightSourceState.LightSources()) + "-";
2352 defaultGlslVersion (aProgramSrc, aProgId, theBits, isFlatNormal);
daf73ab7 2353 aProgramSrc->SetNbLightsMax (aNbLights);
2354 aProgramSrc->SetNbClipPlanesMax (aNbClipPlanes);
c40eb6b9 2355 aProgramSrc->SetAlphaTest ((theBits & OpenGl_PO_AlphaTest) != 0);
2a332745 2356 const Standard_Integer aNbGeomInputVerts = !aSrcGeom.IsEmpty() ? 3 : 0;
2357 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcVert, Graphic3d_TOS_VERTEX, aUniforms, aStageInOuts, "", "", aNbGeomInputVerts));
2358 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcGeom, Graphic3d_TOS_GEOMETRY, aUniforms, aStageInOuts, "geomIn", "geomOut", aNbGeomInputVerts));
2359 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcFrag, Graphic3d_TOS_FRAGMENT, aUniforms, aStageInOuts, "", "", aNbGeomInputVerts));
8625ef7e 2360 TCollection_AsciiString aKey;
2361 if (!Create (aProgramSrc, aKey, theProgram))
2362 {
2363 theProgram = new OpenGl_ShaderProgram(); // just mark as invalid
2364 return Standard_False;
2365 }
2366 return Standard_True;
2367}
2368
38a0206f 2369// =======================================================================
f978241f 2370// function : prepareStdProgramStereo
38a0206f 2371// purpose :
2372// =======================================================================
f978241f 2373Standard_Boolean OpenGl_ShaderManager::prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram,
2374 const Graphic3d_StereoMode theStereoMode)
38a0206f 2375{
2376 Handle(Graphic3d_ShaderProgram) aProgramSrc = new Graphic3d_ShaderProgram();
3b4c6945 2377 OpenGl_ShaderObject::ShaderVariableList aUniforms, aStageInOuts;
2378
2379 aStageInOuts.Append (OpenGl_ShaderObject::ShaderVariable ("vec2 TexCoord", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT));
38a0206f 2380 TCollection_AsciiString aSrcVert =
38a0206f 2381 EOL"void main()"
2382 EOL"{"
2383 EOL" TexCoord = occVertex.zw;"
2384 EOL" gl_Position = vec4(occVertex.x, occVertex.y, 0.0, 1.0);"
2385 EOL"}";
2386
f978241f 2387 TCollection_AsciiString aSrcFrag;
3b4c6945 2388 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("sampler2D uLeftSampler", Graphic3d_TOS_FRAGMENT));
2389 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("sampler2D uRightSampler", Graphic3d_TOS_FRAGMENT));
d95f5ce1 2390 const char* aName = "stereo";
f978241f 2391 switch (theStereoMode)
2392 {
2393 case Graphic3d_StereoMode_Anaglyph:
2394 {
d95f5ce1 2395 aName = "anaglyph";
3b4c6945 2396 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("mat4 uMultL", Graphic3d_TOS_FRAGMENT));
2397 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("mat4 uMultR", Graphic3d_TOS_FRAGMENT));
f978241f 2398 aSrcFrag =
0deb6f04 2399 EOL"const vec4 THE_POW_UP = vec4 (2.2, 2.2, 2.2, 1.0);"
2400 EOL"const vec4 THE_POW_DOWN = 1.0 / vec4 (2.2, 2.2, 2.2, 1.0);"
f978241f 2401 EOL
f978241f 2402 EOL"void main()"
2403 EOL"{"
2404 EOL" vec4 aColorL = occTexture2D (uLeftSampler, TexCoord);"
2405 EOL" vec4 aColorR = occTexture2D (uRightSampler, TexCoord);"
2406 EOL" aColorL = pow (aColorL, THE_POW_UP);" // normalize
2407 EOL" aColorR = pow (aColorR, THE_POW_UP);"
2408 EOL" vec4 aColor = uMultR * aColorR + uMultL * aColorL;"
b17e5bae 2409 EOL" occSetFragColor (pow (aColor, THE_POW_DOWN));"
f978241f 2410 EOL"}";
2411 break;
2412 }
2413 case Graphic3d_StereoMode_RowInterlaced:
2414 {
d95f5ce1 2415 aName = "row-interlaced";
f978241f 2416 aSrcFrag =
f978241f 2417 EOL"void main()"
2418 EOL"{"
2419 EOL" vec4 aColorL = occTexture2D (uLeftSampler, TexCoord);"
2420 EOL" vec4 aColorR = occTexture2D (uRightSampler, TexCoord);"
6e66a3fa 2421 EOL" if (int (mod (gl_FragCoord.y - 1023.5, 2.0)) != 1)"
f978241f 2422 EOL" {"
b17e5bae 2423 EOL" occSetFragColor (aColorL);"
f978241f 2424 EOL" }"
2425 EOL" else"
2426 EOL" {"
b17e5bae 2427 EOL" occSetFragColor (aColorR);"
f978241f 2428 EOL" }"
2429 EOL"}";
2430 break;
2431 }
2432 case Graphic3d_StereoMode_ColumnInterlaced:
2433 {
d95f5ce1 2434 aName = "column-interlaced";
f978241f 2435 aSrcFrag =
f978241f 2436 EOL"void main()"
2437 EOL"{"
2438 EOL" vec4 aColorL = occTexture2D (uLeftSampler, TexCoord);"
2439 EOL" vec4 aColorR = occTexture2D (uRightSampler, TexCoord);"
6e66a3fa 2440 EOL" if (int (mod (gl_FragCoord.x - 1023.5, 2.0)) == 1)"
f978241f 2441 EOL" {"
b17e5bae 2442 EOL" occSetFragColor (aColorL);"
f978241f 2443 EOL" }"
2444 EOL" else"
2445 EOL" {"
b17e5bae 2446 EOL" occSetFragColor (aColorR);"
f978241f 2447 EOL" }"
2448 EOL"}";
2449 break;
2450 }
2451 case Graphic3d_StereoMode_ChessBoard:
2452 {
d95f5ce1 2453 aName = "chessboard";
f978241f 2454 aSrcFrag =
f978241f 2455 EOL"void main()"
2456 EOL"{"
2457 EOL" vec4 aColorL = occTexture2D (uLeftSampler, TexCoord);"
2458 EOL" vec4 aColorR = occTexture2D (uRightSampler, TexCoord);"
6e66a3fa 2459 EOL" bool isEvenX = int(mod(floor(gl_FragCoord.x - 1023.5), 2.0)) != 1;"
2460 EOL" bool isEvenY = int(mod(floor(gl_FragCoord.y - 1023.5), 2.0)) == 1;"
f978241f 2461 EOL" if ((isEvenX && isEvenY) || (!isEvenX && !isEvenY))"
2462 EOL" {"
b17e5bae 2463 EOL" occSetFragColor (aColorL);"
f978241f 2464 EOL" }"
2465 EOL" else"
2466 EOL" {"
b17e5bae 2467 EOL" occSetFragColor (aColorR);"
f978241f 2468 EOL" }"
2469 EOL"}";
2470 break;
2471 }
2472 case Graphic3d_StereoMode_SideBySide:
2473 {
d95f5ce1 2474 aName = "sidebyside";
f978241f 2475 aSrcFrag =
f978241f 2476 EOL"void main()"
2477 EOL"{"
2478 EOL" vec2 aTexCoord = vec2 (TexCoord.x * 2.0, TexCoord.y);"
2479 EOL" if (TexCoord.x > 0.5)"
2480 EOL" {"
2481 EOL" aTexCoord.x -= 1.0;"
2482 EOL" }"
2483 EOL" vec4 aColorL = occTexture2D (uLeftSampler, aTexCoord);"
2484 EOL" vec4 aColorR = occTexture2D (uRightSampler, aTexCoord);"
2485 EOL" if (TexCoord.x <= 0.5)"
2486 EOL" {"
b17e5bae 2487 EOL" occSetFragColor (aColorL);"
f978241f 2488 EOL" }"
2489 EOL" else"
2490 EOL" {"
b17e5bae 2491 EOL" occSetFragColor (aColorR);"
f978241f 2492 EOL" }"
2493 EOL"}";
2494 break;
2495 }
2496 case Graphic3d_StereoMode_OverUnder:
2497 {
d95f5ce1 2498 aName = "overunder";
f978241f 2499 aSrcFrag =
f978241f 2500 EOL"void main()"
2501 EOL"{"
2502 EOL" vec2 aTexCoord = vec2 (TexCoord.x, TexCoord.y * 2.0);"
2503 EOL" if (TexCoord.y > 0.5)"
2504 EOL" {"
2505 EOL" aTexCoord.y -= 1.0;"
2506 EOL" }"
2507 EOL" vec4 aColorL = occTexture2D (uLeftSampler, aTexCoord);"
2508 EOL" vec4 aColorR = occTexture2D (uRightSampler, aTexCoord);"
2509 EOL" if (TexCoord.y <= 0.5)"
2510 EOL" {"
b17e5bae 2511 EOL" occSetFragColor (aColorL);"
f978241f 2512 EOL" }"
2513 EOL" else"
2514 EOL" {"
b17e5bae 2515 EOL" occSetFragColor (aColorR);"
f978241f 2516 EOL" }"
2517 EOL"}";
2518 break;
2519 }
2520 case Graphic3d_StereoMode_QuadBuffer:
2521 case Graphic3d_StereoMode_SoftPageFlip:
2522 default:
2523 {
2524 /*const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[Graphic3d_StereoMode_QuadBuffer];
2525 if (!aProgram.IsNull())
2526 {
2527 return aProgram->IsValid();
2528 }*/
2529 aSrcFrag =
f978241f 2530 EOL"void main()"
2531 EOL"{"
2532 EOL" vec4 aColorL = occTexture2D (uLeftSampler, TexCoord);"
2533 EOL" vec4 aColorR = occTexture2D (uRightSampler, TexCoord);"
2534 EOL" aColorL.b = 0.0;"
2535 EOL" aColorL.g = 0.0;"
2536 EOL" aColorR.r = 0.0;"
b17e5bae 2537 EOL" occSetFragColor (aColorL + aColorR);"
f978241f 2538 EOL"}";
2539 break;
2540 }
2541 }
38a0206f 2542
d95f5ce1 2543 defaultGlslVersion (aProgramSrc, aName, 0);
daf73ab7 2544 aProgramSrc->SetNbLightsMax (0);
2545 aProgramSrc->SetNbClipPlanesMax (0);
3b4c6945 2546 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcVert, Graphic3d_TOS_VERTEX, aUniforms, aStageInOuts));
2547 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcFrag, Graphic3d_TOS_FRAGMENT, aUniforms, aStageInOuts));
38a0206f 2548 TCollection_AsciiString aKey;
f978241f 2549 if (!Create (aProgramSrc, aKey, theProgram))
38a0206f 2550 {
f978241f 2551 theProgram = new OpenGl_ShaderProgram(); // just mark as invalid
38a0206f 2552 return Standard_False;
2553 }
2554
f978241f 2555 myContext->BindProgram (theProgram);
cc8cbabe 2556 theProgram->SetSampler (myContext, "uLeftSampler", Graphic3d_TextureUnit_0);
2557 theProgram->SetSampler (myContext, "uRightSampler", Graphic3d_TextureUnit_1);
38a0206f 2558 myContext->BindProgram (NULL);
2559 return Standard_True;
2560}
2561
98b15dbf 2562// =======================================================================
2563// function : prepareStdProgramBoundBox
2564// purpose :
2565// =======================================================================
2566Standard_Boolean OpenGl_ShaderManager::prepareStdProgramBoundBox()
2567{
2568 Handle(Graphic3d_ShaderProgram) aProgramSrc = new Graphic3d_ShaderProgram();
3b4c6945 2569
2570 OpenGl_ShaderObject::ShaderVariableList aUniforms, aStageInOuts;
2571 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("vec3 occBBoxCenter", Graphic3d_TOS_VERTEX));
2572 aUniforms.Append (OpenGl_ShaderObject::ShaderVariable ("vec3 occBBoxSize", Graphic3d_TOS_VERTEX));
2573
98b15dbf 2574 TCollection_AsciiString aSrcVert =
98b15dbf 2575 EOL"void main()"
2576 EOL"{"
2577 EOL" vec4 aCenter = vec4(occVertex.xyz * occBBoxSize + occBBoxCenter, 1.0);"
2578 EOL" vec4 aPos = vec4(occVertex.xyz * occBBoxSize + occBBoxCenter, 1.0);"
2579 EOL" gl_Position = occProjectionMatrix * occWorldViewMatrix * occModelWorldMatrix * aPos;"
2580 EOL"}";
2581
2582 TCollection_AsciiString aSrcFrag =
2583 EOL"void main()"
2584 EOL"{"
2585 EOL" occSetFragColor (occColor);"
2586 EOL"}";
2587
d95f5ce1 2588 defaultGlslVersion (aProgramSrc, "bndbox", 0);
98b15dbf 2589 aProgramSrc->SetNbLightsMax (0);
2590 aProgramSrc->SetNbClipPlanesMax (0);
3b4c6945 2591 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcVert, Graphic3d_TOS_VERTEX, aUniforms, aStageInOuts));
2592 aProgramSrc->AttachShader (OpenGl_ShaderObject::CreateFromSource (aSrcFrag, Graphic3d_TOS_FRAGMENT, aUniforms, aStageInOuts));
98b15dbf 2593 TCollection_AsciiString aKey;
2594 if (!Create (aProgramSrc, aKey, myBoundBoxProgram))
2595 {
2596 myBoundBoxProgram = new OpenGl_ShaderProgram(); // just mark as invalid
2597 return Standard_False;
2598 }
2599
2600 const OpenGl_Vec4 aMin (-0.5f, -0.5f, -0.5f, 1.0f);
2601 const OpenGl_Vec4 anAxisShifts[3] =
2602 {
2603 OpenGl_Vec4 (1.0f, 0.0f, 0.0f, 0.0f),
2604 OpenGl_Vec4 (0.0f, 1.0f, 0.0f, 0.0f),
2605 OpenGl_Vec4 (0.0f, 0.0f, 1.0f, 0.0f)
2606 };
2607
2608 const OpenGl_Vec4 aLookup1 (0.0f, 1.0f, 0.0f, 1.0f);
2609 const OpenGl_Vec4 aLookup2 (0.0f, 0.0f, 1.0f, 1.0f);
2610 OpenGl_Vec4 aLinesVertices[24];
2611 for (int anAxis = 0, aVertex = 0; anAxis < 3; ++anAxis)
2612 {
2613 for (int aCompIter = 0; aCompIter < 4; ++aCompIter)
2614 {
2615 aLinesVertices[aVertex++] = aMin
2616 + anAxisShifts[(anAxis + 1) % 3] * aLookup1[aCompIter]
2617 + anAxisShifts[(anAxis + 2) % 3] * aLookup2[aCompIter];
2618
2619 aLinesVertices[aVertex++] = aMin
2620 + anAxisShifts[anAxis]
2621 + anAxisShifts[(anAxis + 1) % 3] * aLookup1[aCompIter]
2622 + anAxisShifts[(anAxis + 2) % 3] * aLookup2[aCompIter];
2623 }
2624 }
2625 if (myContext->ToUseVbo())
2626 {
2627 myBoundBoxVertBuffer = new OpenGl_VertexBuffer();
2628 if (myBoundBoxVertBuffer->Init (myContext, 4, 24, aLinesVertices[0].GetData()))
2629 {
2630 myContext->ShareResource ("OpenGl_ShaderManager_BndBoxVbo", myBoundBoxVertBuffer);
2631 return Standard_True;
2632 }
2633 }
2634 myBoundBoxVertBuffer = new OpenGl_VertexBufferCompat();
2635 myBoundBoxVertBuffer->Init (myContext, 4, 24, aLinesVertices[0].GetData());
2636 myContext->ShareResource ("OpenGl_ShaderManager_BndBoxVbo", myBoundBoxVertBuffer);
2637 return Standard_True;
2638}
2639
8625ef7e 2640// =======================================================================
2641// function : bindProgramWithState
2642// purpose :
2643// =======================================================================
299e0ab9 2644Standard_Boolean OpenGl_ShaderManager::bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram)
8625ef7e 2645{
8613985b 2646 const Standard_Boolean isBound = myContext->BindProgram (theProgram);
2647 if (isBound
2648 && !theProgram.IsNull())
8625ef7e 2649 {
8613985b 2650 theProgram->ApplyVariables (myContext);
8625ef7e 2651 }
8625ef7e 2652 PushState (theProgram);
8613985b 2653 return isBound;
8625ef7e 2654}