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