2df52815a12229069d9ca8be007589d8830a5623
[occt.git] / src / Shaders / Shaders_RaytraceBase_fs.pxx
1 // This file has been automatically generated from resource file src/Shaders/RaytraceBase.fs
2
3 static const char Shaders_RaytraceBase_fs[] =
4   "#ifdef ADAPTIVE_SAMPLING\n"
5   "  #extension GL_ARB_shader_image_load_store : require\n"
6   "  #extension GL_NV_shader_atomic_float : require\n"
7   "#endif\n"
8   "\n"
9   "#ifdef USE_TEXTURES\n"
10   "  #extension GL_ARB_bindless_texture : require\n"
11   "#endif\n"
12   "\n"
13   "//! Normalized pixel coordinates.\n"
14   "in vec2 vPixel;\n"
15   "\n"
16   "//! Sub-pixel offset in X direction for FSAA.\n"
17   "uniform float uOffsetX = 0.f;\n"
18   "//! Sub-pixel offset in Y direction for FSAA.\n"
19   "uniform float uOffsetY = 0.f;\n"
20   "\n"
21   "//! Origin of viewing ray in left-top corner.\n"
22   "uniform vec3 uOriginLT;\n"
23   "//! Origin of viewing ray in left-bottom corner.\n"
24   "uniform vec3 uOriginLB;\n"
25   "//! Origin of viewing ray in right-top corner.\n"
26   "uniform vec3 uOriginRT;\n"
27   "//! Origin of viewing ray in right-bottom corner.\n"
28   "uniform vec3 uOriginRB;\n"
29   "\n"
30   "//! Width of the rendering window.\n"
31   "uniform int uWinSizeX;\n"
32   "//! Height of the rendering window.\n"
33   "uniform int uWinSizeY;\n"
34   "\n"
35   "//! Direction of viewing ray in left-top corner.\n"
36   "uniform vec3 uDirectLT;\n"
37   "//! Direction of viewing ray in left-bottom corner.\n"
38   "uniform vec3 uDirectLB;\n"
39   "//! Direction of viewing ray in right-top corner.\n"
40   "uniform vec3 uDirectRT;\n"
41   "//! Direction of viewing ray in right-bottom corner.\n"
42   "uniform vec3 uDirectRB;\n"
43   "\n"
44   "//! Inverse model-view-projection matrix.\n"
45   "uniform mat4 uUnviewMat;\n"
46   "\n"
47   "//! Model-view-projection matrix.\n"
48   "uniform mat4 uViewMat;\n"
49   "\n"
50   "//! Texture buffer of data records of bottom-level BVH nodes.\n"
51   "uniform isamplerBuffer uSceneNodeInfoTexture;\n"
52   "//! Texture buffer of minimum points of bottom-level BVH nodes.\n"
53   "uniform samplerBuffer uSceneMinPointTexture;\n"
54   "//! Texture buffer of maximum points of bottom-level BVH nodes.\n"
55   "uniform samplerBuffer uSceneMaxPointTexture;\n"
56   "//! Texture buffer of transformations of high-level BVH nodes.\n"
57   "uniform samplerBuffer uSceneTransformTexture;\n"
58   "\n"
59   "//! Texture buffer of vertex coords.\n"
60   "uniform samplerBuffer uGeometryVertexTexture;\n"
61   "//! Texture buffer of vertex normals.\n"
62   "uniform samplerBuffer uGeometryNormalTexture;\n"
63   "#ifdef USE_TEXTURES\n"
64   "  //! Texture buffer of per-vertex UV-coordinates.\n"
65   "  uniform samplerBuffer uGeometryTexCrdTexture;\n"
66   "#endif\n"
67   "//! Texture buffer of triangle indices.\n"
68   "uniform isamplerBuffer uGeometryTriangTexture;\n"
69   "\n"
70   "//! Texture buffer of material properties.\n"
71   "uniform samplerBuffer uRaytraceMaterialTexture;\n"
72   "//! Texture buffer of light source properties.\n"
73   "uniform samplerBuffer uRaytraceLightSrcTexture;\n"
74   "//! Environment map texture.\n"
75   "uniform sampler2D uEnvironmentMapTexture;\n"
76   "\n"
77   "//! Total number of light sources.\n"
78   "uniform int uLightCount;\n"
79   "//! Intensity of global ambient light.\n"
80   "uniform vec4 uGlobalAmbient;\n"
81   "\n"
82   "//! Enables/disables hard shadows.\n"
83   "uniform int uShadowsEnabled;\n"
84   "//! Enables/disables specular reflections.\n"
85   "uniform int uReflectEnabled;\n"
86   "//! Enables/disables spherical environment map.\n"
87   "uniform int uSphereMapEnabled;\n"
88   "//! Enables/disables environment map background.\n"
89   "uniform int uSphereMapForBack;\n"
90   "\n"
91   "//! Radius of bounding sphere of the scene.\n"
92   "uniform float uSceneRadius;\n"
93   "//! Scene epsilon to prevent self-intersections.\n"
94   "uniform float uSceneEpsilon;\n"
95   "\n"
96   "#ifdef USE_TEXTURES\n"
97   "  //! Unique 64-bit handles of OpenGL textures.\n"
98   "  uniform uvec2 uTextureSamplers[MAX_TEX_NUMBER];\n"
99   "#endif\n"
100   "\n"
101   "#ifdef ADAPTIVE_SAMPLING\n"
102   "  //! OpenGL image used for accumulating rendering result.\n"
103   "  volatile restrict layout(size1x32) uniform image2D  uRenderImage;\n"
104   "\n"
105   "  //! OpenGL image storing offsets of sampled pixels blocks.\n"
106   "  coherent restrict layout(size2x32) uniform iimage2D uOffsetImage;\n"
107   "#endif\n"
108   "\n"
109   "//! Top color of gradient background.\n"
110   "uniform vec4 uBackColorTop = vec4 (0.0);\n"
111   "//! Bottom color of gradient background.\n"
112   "uniform vec4 uBackColorBot = vec4 (0.0);\n"
113   "\n"
114   "/////////////////////////////////////////////////////////////////////////////////////////\n"
115   "// Specific data types\n"
116   "\n"
117   "//! Stores ray parameters.\n"
118   "struct SRay\n"
119   "{\n"
120   "  vec3 Origin;\n"
121   "\n"
122   "  vec3 Direct;\n"
123   "};\n"
124   "\n"
125   "//! Stores intersection parameters.\n"
126   "struct SIntersect\n"
127   "{\n"
128   "  float Time;\n"
129   "\n"
130   "  vec2 UV;\n"
131   "\n"
132   "  vec3 Normal;\n"
133   "};\n"
134   "\n"
135   "/////////////////////////////////////////////////////////////////////////////////////////\n"
136   "// Some useful constants\n"
137   "\n"
138   "#define MAXFLOAT 1e15f\n"
139   "\n"
140   "#define SMALL vec3 (exp2 (-80.0f))\n"
141   "\n"
142   "#define ZERO vec3 (0.0f, 0.0f, 0.0f)\n"
143   "#define UNIT vec3 (1.0f, 1.0f, 1.0f)\n"
144   "\n"
145   "#define AXIS_X vec3 (1.0f, 0.0f, 0.0f)\n"
146   "#define AXIS_Y vec3 (0.0f, 1.0f, 0.0f)\n"
147   "#define AXIS_Z vec3 (0.0f, 0.0f, 1.0f)\n"
148   "\n"
149   "#define M_PI   3.141592653f\n"
150   "#define M_2_PI 6.283185307f\n"
151   "#define M_PI_2 1.570796327f\n"
152   "\n"
153   "#define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)\n"
154   "\n"
155   "// =======================================================================\n"
156   "// function : MatrixRowMultiplyDir\n"
157   "// purpose  : Multiplies a vector by matrix\n"
158   "// =======================================================================\n"
159   "vec3 MatrixRowMultiplyDir (in vec3 v,\n"
160   "                           in vec4 m0,\n"
161   "                           in vec4 m1,\n"
162   "                           in vec4 m2)\n"
163   "{\n"
164   "  return vec3 (dot (m0.xyz, v),\n"
165   "               dot (m1.xyz, v),\n"
166   "               dot (m2.xyz, v));\n"
167   "}\n"
168   "\n"
169   "//! 32-bit state of random number generator.\n"
170   "uint RandState;\n"
171   "\n"
172   "// =======================================================================\n"
173   "// function : SeedRand\n"
174   "// purpose  : Applies hash function by Thomas Wang to randomize seeds\n"
175   "//            (see http://www.burtleburtle.net/bob/hash/integer.html)\n"
176   "// =======================================================================\n"
177   "void SeedRand (in int theSeed, in int theSizeX, in int theRadius)\n"
178   "{\n"
179   "  RandState = uint (int (gl_FragCoord.y) / theRadius * theSizeX + int (gl_FragCoord.x) / theRadius + theSeed);\n"
180   "\n"
181   "  RandState = (RandState + 0x479ab41du) + (RandState <<  8);\n"
182   "  RandState = (RandState ^ 0xe4aa10ceu) ^ (RandState >>  5);\n"
183   "  RandState = (RandState + 0x9942f0a6u) - (RandState << 14);\n"
184   "  RandState = (RandState ^ 0x5aedd67du) ^ (RandState >>  3);\n"
185   "  RandState = (RandState + 0x17bea992u) + (RandState <<  7);\n"
186   "}\n"
187   "\n"
188   "// =======================================================================\n"
189   "// function : RandInt\n"
190   "// purpose  : Generates integer using Xorshift algorithm by G. Marsaglia\n"
191   "// =======================================================================\n"
192   "uint RandInt()\n"
193   "{\n"
194   "  RandState ^= (RandState << 13);\n"
195   "  RandState ^= (RandState >> 17);\n"
196   "  RandState ^= (RandState <<  5);\n"
197   "\n"
198   "  return RandState;\n"
199   "}\n"
200   "\n"
201   "// =======================================================================\n"
202   "// function : RandFloat\n"
203   "// purpose  : Generates a random float in 0 <= x < 1 range\n"
204   "// =======================================================================\n"
205   "float RandFloat()\n"
206   "{\n"
207   "  return float (RandInt()) * (1.f / 4294967296.f);\n"
208   "}\n"
209   "\n"
210   "// =======================================================================\n"
211   "// function : MatrixColMultiplyPnt\n"
212   "// purpose  : Multiplies a vector by matrix\n"
213   "// =======================================================================\n"
214   "vec3 MatrixColMultiplyPnt (in vec3 v,\n"
215   "                           in vec4 m0,\n"
216   "                           in vec4 m1,\n"
217   "                           in vec4 m2,\n"
218   "                           in vec4 m3)\n"
219   "{\n"
220   "  return vec3 (m0.x * v.x + m1.x * v.y + m2.x * v.z + m3.x,\n"
221   "               m0.y * v.x + m1.y * v.y + m2.y * v.z + m3.y,\n"
222   "               m0.z * v.x + m1.z * v.y + m2.z * v.z + m3.z);\n"
223   "}\n"
224   "\n"
225   "// =======================================================================\n"
226   "// function : MatrixColMultiplyDir\n"
227   "// purpose  : Multiplies a vector by matrix\n"
228   "// =======================================================================\n"
229   "vec3 MatrixColMultiplyDir (in vec3 v,\n"
230   "                           in vec4 m0,\n"
231   "                           in vec4 m1,\n"
232   "                           in vec4 m2)\n"
233   "{\n"
234   "  return vec3 (m0.x * v.x + m1.x * v.y + m2.x * v.z,\n"
235   "               m0.y * v.x + m1.y * v.y + m2.y * v.z,\n"
236   "               m0.z * v.x + m1.z * v.y + m2.z * v.z);\n"
237   "}\n"
238   "\n"
239   "//=======================================================================\n"
240   "// function : InverseDirection\n"
241   "// purpose  : Returns safely inverted direction of the given one\n"
242   "//=======================================================================\n"
243   "vec3 InverseDirection (in vec3 theInput)\n"
244   "{\n"
245   "  vec3 anInverse = 1.f / max (abs (theInput), SMALL);\n"
246   "\n"
247   "  return mix (-anInverse, anInverse, step (ZERO, theInput));\n"
248   "}\n"
249   "\n"
250   "//=======================================================================\n"
251   "// function : BackgroundColor\n"
252   "// purpose  : Returns color of gradient background\n"
253   "//=======================================================================\n"
254   "vec4 BackgroundColor()\n"
255   "{\n"
256   "#ifdef ADAPTIVE_SAMPLING\n"
257   "\n"
258   "  ivec2 aFragCoord = ivec2 (gl_FragCoord.xy);\n"
259   "\n"
260   "  ivec2 aTileXY = imageLoad (uOffsetImage, ivec2 (aFragCoord.x / BLOCK_SIZE,\n"
261   "                                                  aFragCoord.y / BLOCK_SIZE)).xy;\n"
262   "\n"
263   "  aTileXY.y += aFragCoord.y % min (uWinSizeY - aTileXY.y, BLOCK_SIZE);\n"
264   "\n"
265   "  return mix (uBackColorBot, uBackColorTop, float (aTileXY.y) / uWinSizeY);\n"
266   "\n"
267   "#else\n"
268   "\n"
269   "  return mix (uBackColorBot, uBackColorTop, vPixel.y);\n"
270   "\n"
271   "#endif\n"
272   "}\n"
273   "\n"
274   "/////////////////////////////////////////////////////////////////////////////////////////\n"
275   "// Functions for compute ray-object intersection\n"
276   "\n"
277   "// =======================================================================\n"
278   "// function : GenerateRay\n"
279   "// purpose  :\n"
280   "// =======================================================================\n"
281   "SRay GenerateRay (in vec2 thePixel)\n"
282   "{\n"
283   "  vec3 aP0 = mix (uOriginLB, uOriginRB, thePixel.x);\n"
284   "  vec3 aP1 = mix (uOriginLT, uOriginRT, thePixel.x);\n"
285   "\n"
286   "  vec3 aD0 = mix (uDirectLB, uDirectRB, thePixel.x);\n"
287   "  vec3 aD1 = mix (uDirectLT, uDirectRT, thePixel.x);\n"
288   "\n"
289   "  vec3 aDirection = normalize (mix (aD0, aD1, thePixel.y));\n"
290   "\n"
291   "  return SRay (mix (aP0, aP1, thePixel.y), aDirection);\n"
292   "}\n"
293   "\n"
294   "// =======================================================================\n"
295   "// function : IntersectSphere\n"
296   "// purpose  : Computes ray-sphere intersection\n"
297   "// =======================================================================\n"
298   "float IntersectSphere (in SRay theRay, in float theRadius)\n"
299   "{\n"
300   "  float aDdotD = dot (theRay.Direct, theRay.Direct);\n"
301   "  float aDdotO = dot (theRay.Direct, theRay.Origin);\n"
302   "  float aOdotO = dot (theRay.Origin, theRay.Origin);\n"
303   "\n"
304   "  float aD = aDdotO * aDdotO - aDdotD * (aOdotO - theRadius * theRadius);\n"
305   "\n"
306   "  if (aD > 0.0f)\n"
307   "  {\n"
308   "    float aTime = (sqrt (aD) - aDdotO) * (1.0f / aDdotD);\n"
309   "    \n"
310   "    return aTime > 0.0f ? aTime : MAXFLOAT;\n"
311   "  }\n"
312   "\n"
313   "  return MAXFLOAT;\n"
314   "}\n"
315   "\n"
316   "// =======================================================================\n"
317   "// function : IntersectTriangle\n"
318   "// purpose  : Computes ray-triangle intersection (branchless version)\n"
319   "// =======================================================================\n"
320   "void IntersectTriangle (in SRay theRay,\n"
321   "                        in vec3 thePnt0,\n"
322   "                        in vec3 thePnt1,\n"
323   "                        in vec3 thePnt2,\n"
324   "                        out vec3 theUVT,\n"
325   "                        out vec3 theNorm)\n"
326   "{\n"
327   "  vec3 aToTrg = thePnt0 - theRay.Origin;\n"
328   "\n"
329   "  vec3 aEdge0 = thePnt1 - thePnt0;\n"
330   "  vec3 aEdge1 = thePnt0 - thePnt2;\n"
331   "\n"
332   "  theNorm = cross (aEdge1, aEdge0);\n"
333   "\n"
334   "  vec3 theVect = cross (theRay.Direct, aToTrg);\n"
335   "\n"
336   "  theUVT = vec3 (dot (theNorm, aToTrg),\n"
337   "                 dot (theVect, aEdge1),\n"
338   "                 dot (theVect, aEdge0)) * (1.f / dot (theNorm, theRay.Direct));\n"
339   "\n"
340   "  theUVT.x = any (lessThan (theUVT, ZERO)) || (theUVT.y + theUVT.z) > 1.f ? MAXFLOAT : theUVT.x;\n"
341   "}\n"
342   "\n"
343   "#define EMPTY_ROOT ivec4(0)\n"
344   "\n"
345   "//! Utility structure containing information about\n"
346   "//! currently traversing sub-tree of scene's BVH.\n"
347   "struct SSubTree\n"
348   "{\n"
349   "  //! Transformed ray.\n"
350   "  SRay  TrsfRay;\n"
351   "\n"
352   "  //! Inversed ray direction.\n"
353   "  vec3  Inverse;\n"
354   "\n"
355   "  //! Parameters of sub-root node.\n"
356   "  ivec4 SubData;\n"
357   "};\n"
358   "\n"
359   "#define MATERIAL_AMBN(index) (18 * index + 0)\n"
360   "#define MATERIAL_DIFF(index) (18 * index + 1)\n"
361   "#define MATERIAL_SPEC(index) (18 * index + 2)\n"
362   "#define MATERIAL_EMIS(index) (18 * index + 3)\n"
363   "#define MATERIAL_REFL(index) (18 * index + 4)\n"
364   "#define MATERIAL_REFR(index) (18 * index + 5)\n"
365   "#define MATERIAL_TRAN(index) (18 * index + 6)\n"
366   "#define MATERIAL_TRS1(index) (18 * index + 7)\n"
367   "#define MATERIAL_TRS2(index) (18 * index + 8)\n"
368   "#define MATERIAL_TRS3(index) (18 * index + 9)\n"
369   "\n"
370   "#define TRS_OFFSET(treelet) treelet.SubData.x\n"
371   "#define BVH_OFFSET(treelet) treelet.SubData.y\n"
372   "#define VRT_OFFSET(treelet) treelet.SubData.z\n"
373   "#define TRG_OFFSET(treelet) treelet.SubData.w\n"
374   "\n"
375   "//! Identifies the absence of intersection.\n"
376   "#define INALID_HIT ivec4 (-1)\n"
377   "\n"
378   "//! Global stack shared between traversal functions.\n"
379   "int Stack[STACK_SIZE];\n"
380   "\n"
381   "// =======================================================================\n"
382   "// function : pop\n"
383   "// purpose  :\n"
384   "// =======================================================================\n"
385   "int pop (inout int theHead)\n"
386   "{\n"
387   "  int aData = Stack[theHead];\n"
388   "\n"
389   "  int aMask = aData >> 26;\n"
390   "  int aNode = aMask & 0x3;\n"
391   "\n"
392   "  aMask >>= 2;\n"
393   "\n"
394   "  if ((aMask & 0x3) == aNode)\n"
395   "  {\n"
396   "    --theHead;\n"
397   "  }\n"
398   "  else\n"
399   "  {\n"
400   "    aMask |= (aMask << 2) & 0x30;\n"
401   "\n"
402   "    Stack[theHead] = (aData & 0x03FFFFFF) | (aMask << 26);\n"
403   "  }\n"
404   "\n"
405   "  return (aData & 0x03FFFFFF) + aNode;\n"
406   "}\n"
407   "\n"
408   "// =======================================================================\n"
409   "// function : SceneNearestHit\n"
410   "// purpose  : Finds intersection with nearest scene triangle\n"
411   "// =======================================================================\n"
412   "ivec4 SceneNearestHit (in SRay theRay, in vec3 theInverse, inout SIntersect theHit, out int theTrsfId)\n"
413   "{\n"
414   "  ivec4 aTriIndex = INALID_HIT;\n"
415   "\n"
416   "  int aNode =  0; // node to traverse\n"
417   "  int aHead = -1; // pointer of stack\n"
418   "  int aStop = -1; // BVH level switch\n"
419   "\n"
420   "  SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
421   "\n"
422   "  for (bool toContinue = true; toContinue; /* none */)\n"
423   "  {\n"
424   "    ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);\n"
425   "\n"
426   "    if (aData.x == 0) // if inner node\n"
427   "    {\n"
428   "      aData.y += BVH_OFFSET (aSubTree);\n"
429   "\n"
430   "      vec4 aHitTimes = vec4 (MAXFLOAT,\n"
431   "                             MAXFLOAT,\n"
432   "                             MAXFLOAT,\n"
433   "                             MAXFLOAT);\n"
434   "\n"
435   "      vec3 aRayOriginInverse = -aSubTree.TrsfRay.Origin * aSubTree.Inverse;\n"
436   "\n"
437   "      vec3 aNodeMin0 = texelFetch (uSceneMinPointTexture, aData.y +                0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
438   "      vec3 aNodeMin1 = texelFetch (uSceneMinPointTexture, aData.y +                1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
439   "      vec3 aNodeMin2 = texelFetch (uSceneMinPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
440   "      vec3 aNodeMin3 = texelFetch (uSceneMinPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
441   "      vec3 aNodeMax0 = texelFetch (uSceneMaxPointTexture, aData.y +                0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
442   "      vec3 aNodeMax1 = texelFetch (uSceneMaxPointTexture, aData.y +                1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
443   "      vec3 aNodeMax2 = texelFetch (uSceneMaxPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
444   "      vec3 aNodeMax3 = texelFetch (uSceneMaxPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
445   "\n"
446   "      vec3 aTimeMax = max (aNodeMin0, aNodeMax0);\n"
447   "      vec3 aTimeMin = min (aNodeMin0, aNodeMax0);\n"
448   "\n"
449   "      float aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
450   "      float aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
451   "\n"
452   "      aHitTimes.x = mix (MAXFLOAT, aTimeEnter,\n"
453   "        aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f);\n"
454   "\n"
455   "      aTimeMax = max (aNodeMin1, aNodeMax1);\n"
456   "      aTimeMin = min (aNodeMin1, aNodeMax1);\n"
457   "\n"
458   "      aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
459   "      aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
460   "\n"
461   "      aHitTimes.y = mix (MAXFLOAT, aTimeEnter,\n"
462   "        aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f);\n"
463   "\n"
464   "      aTimeMax = max (aNodeMin2, aNodeMax2);\n"
465   "      aTimeMin = min (aNodeMin2, aNodeMax2);\n"
466   "\n"
467   "      aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
468   "      aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
469   "\n"
470   "      aHitTimes.z = mix (MAXFLOAT, aTimeEnter,\n"
471   "        aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f && aData.z > 1);\n"
472   "\n"
473   "      aTimeMax = max (aNodeMin3, aNodeMax3);\n"
474   "      aTimeMin = min (aNodeMin3, aNodeMax3);\n"
475   "\n"
476   "      aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
477   "      aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
478   "\n"
479   "      aHitTimes.w = mix (MAXFLOAT, aTimeEnter,\n"
480   "        aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f && aData.z > 2);\n"
481   "\n"
482   "      ivec4 aChildren = ivec4 (0, 1, 2, 3);\n"
483   "\n"
484   "      aChildren.xy = aHitTimes.y < aHitTimes.x ? aChildren.yx : aChildren.xy;\n"
485   "      aHitTimes.xy = aHitTimes.y < aHitTimes.x ? aHitTimes.yx : aHitTimes.xy;\n"
486   "      aChildren.zw = aHitTimes.w < aHitTimes.z ? aChildren.wz : aChildren.zw;\n"
487   "      aHitTimes.zw = aHitTimes.w < aHitTimes.z ? aHitTimes.wz : aHitTimes.zw;\n"
488   "      aChildren.xz = aHitTimes.z < aHitTimes.x ? aChildren.zx : aChildren.xz;\n"
489   "      aHitTimes.xz = aHitTimes.z < aHitTimes.x ? aHitTimes.zx : aHitTimes.xz;\n"
490   "      aChildren.yw = aHitTimes.w < aHitTimes.y ? aChildren.wy : aChildren.yw;\n"
491   "      aHitTimes.yw = aHitTimes.w < aHitTimes.y ? aHitTimes.wy : aHitTimes.yw;\n"
492   "      aChildren.yz = aHitTimes.z < aHitTimes.y ? aChildren.zy : aChildren.yz;\n"
493   "      aHitTimes.yz = aHitTimes.z < aHitTimes.y ? aHitTimes.zy : aHitTimes.yz;\n"
494   "\n"
495   "      if (aHitTimes.x != MAXFLOAT)\n"
496   "      {\n"
497   "        int aHitMask = (aHitTimes.w != MAXFLOAT ? aChildren.w : aChildren.z) << 2\n"
498   "                     | (aHitTimes.z != MAXFLOAT ? aChildren.z : aChildren.y);\n"
499   "\n"
500   "        if (aHitTimes.y != MAXFLOAT)\n"
501   "          Stack[++aHead] = aData.y | (aHitMask << 2 | aChildren.y) << 26;\n"
502   "\n"
503   "        aNode = aData.y + aChildren.x;\n"
504   "      }\n"
505   "      else\n"
506   "      {\n"
507   "        toContinue = (aHead >= 0);\n"
508   "\n"
509   "        if (aHead == aStop) // go to top-level BVH\n"
510   "        {\n"
511   "          aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
512   "        }\n"
513   "\n"
514   "        if (aHead >= 0)\n"
515   "          aNode = pop (aHead);\n"
516   "      }\n"
517   "    }\n"
518   "    else if (aData.x < 0) // leaf node (contains triangles)\n"
519   "    {\n"
520   "      vec3 aNormal;\n"
521   "      vec3 aTimeUV;\n"
522   "\n"
523   "      for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)\n"
524   "      {\n"
525   "        ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + TRG_OFFSET (aSubTree));\n"
526   "\n"
527   "        vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x += VRT_OFFSET (aSubTree)).xyz;\n"
528   "        vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y += VRT_OFFSET (aSubTree)).xyz;\n"
529   "        vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z += VRT_OFFSET (aSubTree)).xyz;\n"
530   "\n"
531   "        IntersectTriangle (aSubTree.TrsfRay, aPoint0, aPoint1, aPoint2, aTimeUV, aNormal);\n"
532   "\n"
533   "        if (aTimeUV.x < theHit.Time)\n"
534   "        {\n"
535   "          aTriIndex = aTriangle;\n"
536   "\n"
537   "          theTrsfId = TRS_OFFSET (aSubTree);\n"
538   "\n"
539   "          theHit = SIntersect (aTimeUV.x, aTimeUV.yz, aNormal);\n"
540   "        }\n"
541   "      }\n"
542   "\n"
543   "      toContinue = (aHead >= 0);\n"
544   "\n"
545   "      if (aHead == aStop) // go to top-level BVH\n"
546   "      {\n"
547   "        aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
548   "      }\n"
549   "\n"
550   "      if (aHead >= 0)\n"
551   "        aNode = pop (aHead);\n"
552   "    }\n"
553   "    else if (aData.x > 0) // switch node\n"
554   "    {\n"
555   "      aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root\n"
556   "\n"
557   "      vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);\n"
558   "      vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);\n"
559   "      vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);\n"
560   "      vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);\n"
561   "\n"
562   "      aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,\n"
563   "                                                      aInvTransf0,\n"
564   "                                                      aInvTransf1,\n"
565   "                                                      aInvTransf2);\n"
566   "\n"
567   "      aSubTree.Inverse = mix (-UNIT, UNIT, step (ZERO, aSubTree.TrsfRay.Direct)) /\n"
568   "        max (abs (aSubTree.TrsfRay.Direct), SMALL);\n"
569   "\n"
570   "      aSubTree.TrsfRay.Origin = MatrixColMultiplyPnt (theRay.Origin,\n"
571   "                                                      aInvTransf0,\n"
572   "                                                      aInvTransf1,\n"
573   "                                                      aInvTransf2,\n"
574   "                                                      aInvTransf3);\n"
575   "\n"
576   "      aNode = BVH_OFFSET (aSubTree); // go to sub-root node\n"
577   "\n"
578   "      aStop = aHead; // store current stack pointer\n"
579   "    }\n"
580   "  }\n"
581   "\n"
582   "  return aTriIndex;\n"
583   "}\n"
584   "\n"
585   "// =======================================================================\n"
586   "// function : SceneAnyHit\n"
587   "// purpose  : Finds intersection with any scene triangle\n"
588   "// =======================================================================\n"
589   "float SceneAnyHit (in SRay theRay, in vec3 theInverse, in float theDistance)\n"
590   "{\n"
591   "  float aFactor = 1.f;\n"
592   "\n"
593   "  int aNode =  0; // node to traverse\n"
594   "  int aHead = -1; // pointer of stack\n"
595   "  int aStop = -1; // BVH level switch\n"
596   "\n"
597   "  SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
598   "\n"
599   "  for (bool toContinue = true; toContinue; /* none */)\n"
600   "  {\n"
601   "    ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);\n"
602   "\n"
603   "    if (aData.x == 0) // if inner node\n"
604   "    {\n"
605   "      aData.y += BVH_OFFSET (aSubTree);\n"
606   "\n"
607   "      vec4 aHitTimes = vec4 (MAXFLOAT,\n"
608   "                             MAXFLOAT,\n"
609   "                             MAXFLOAT,\n"
610   "                             MAXFLOAT);\n"
611   "\n"
612   "      vec3 aRayOriginInverse = -aSubTree.TrsfRay.Origin * aSubTree.Inverse;\n"
613   "\n"
614   "      vec3 aNodeMin0 = texelFetch (uSceneMinPointTexture, aData.y +                0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
615   "      vec3 aNodeMin1 = texelFetch (uSceneMinPointTexture, aData.y +                1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
616   "      vec3 aNodeMin2 = texelFetch (uSceneMinPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
617   "      vec3 aNodeMin3 = texelFetch (uSceneMinPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
618   "      vec3 aNodeMax0 = texelFetch (uSceneMaxPointTexture, aData.y +                0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
619   "      vec3 aNodeMax1 = texelFetch (uSceneMaxPointTexture, aData.y +                1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
620   "      vec3 aNodeMax2 = texelFetch (uSceneMaxPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
621   "      vec3 aNodeMax3 = texelFetch (uSceneMaxPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
622   "\n"
623   "      vec3 aTimeMax = max (aNodeMin0, aNodeMax0);\n"
624   "      vec3 aTimeMin = min (aNodeMin0, aNodeMax0);\n"
625   "\n"
626   "      float aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
627   "      float aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
628   "\n"
629   "      aHitTimes.x = mix (MAXFLOAT, aTimeEnter,\n"
630   "        aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f);\n"
631   "\n"
632   "      aTimeMax = max (aNodeMin1, aNodeMax1);\n"
633   "      aTimeMin = min (aNodeMin1, aNodeMax1);\n"
634   "\n"
635   "      aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
636   "      aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
637   "\n"
638   "      aHitTimes.y = mix (MAXFLOAT, aTimeEnter,\n"
639   "        aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f);\n"
640   "\n"
641   "      aTimeMax = max (aNodeMin2, aNodeMax2);\n"
642   "      aTimeMin = min (aNodeMin2, aNodeMax2);\n"
643   "\n"
644   "      aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
645   "      aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
646   "\n"
647   "      aHitTimes.z = mix (MAXFLOAT, aTimeEnter,\n"
648   "        aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f && aData.z > 1);\n"
649   "\n"
650   "      aTimeMax = max (aNodeMin3, aNodeMax3);\n"
651   "      aTimeMin = min (aNodeMin3, aNodeMax3);\n"
652   "\n"
653   "      aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
654   "      aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
655   "\n"
656   "      aHitTimes.w = mix (MAXFLOAT, aTimeEnter,\n"
657   "        aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f && aData.z > 2);\n"
658   "\n"
659   "      ivec4 aChildren = ivec4 (0, 1, 2, 3);\n"
660   "\n"
661   "      aChildren.xy = aHitTimes.y < aHitTimes.x ? aChildren.yx : aChildren.xy;\n"
662   "      aHitTimes.xy = aHitTimes.y < aHitTimes.x ? aHitTimes.yx : aHitTimes.xy;\n"
663   "      aChildren.zw = aHitTimes.w < aHitTimes.z ? aChildren.wz : aChildren.zw;\n"
664   "      aHitTimes.zw = aHitTimes.w < aHitTimes.z ? aHitTimes.wz : aHitTimes.zw;\n"
665   "      aChildren.xz = aHitTimes.z < aHitTimes.x ? aChildren.zx : aChildren.xz;\n"
666   "      aHitTimes.xz = aHitTimes.z < aHitTimes.x ? aHitTimes.zx : aHitTimes.xz;\n"
667   "      aChildren.yw = aHitTimes.w < aHitTimes.y ? aChildren.wy : aChildren.yw;\n"
668   "      aHitTimes.yw = aHitTimes.w < aHitTimes.y ? aHitTimes.wy : aHitTimes.yw;\n"
669   "      aChildren.yz = aHitTimes.z < aHitTimes.y ? aChildren.zy : aChildren.yz;\n"
670   "      aHitTimes.yz = aHitTimes.z < aHitTimes.y ? aHitTimes.zy : aHitTimes.yz;\n"
671   "\n"
672   "      if (aHitTimes.x != MAXFLOAT)\n"
673   "      {\n"
674   "        int aHitMask = (aHitTimes.w != MAXFLOAT ? aChildren.w : aChildren.z) << 2\n"
675   "                     | (aHitTimes.z != MAXFLOAT ? aChildren.z : aChildren.y);\n"
676   "\n"
677   "        if (aHitTimes.y != MAXFLOAT)\n"
678   "          Stack[++aHead] = aData.y | (aHitMask << 2 | aChildren.y) << 26;\n"
679   "\n"
680   "        aNode = aData.y + aChildren.x;\n"
681   "      }\n"
682   "      else\n"
683   "      {\n"
684   "        toContinue = (aHead >= 0);\n"
685   "\n"
686   "        if (aHead == aStop) // go to top-level BVH\n"
687   "        {\n"
688   "          aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
689   "        }\n"
690   "\n"
691   "        if (aHead >= 0)\n"
692   "          aNode = pop (aHead);\n"
693   "      }\n"
694   "    }\n"
695   "    else if (aData.x < 0) // leaf node\n"
696   "    {\n"
697   "      vec3 aNormal;\n"
698   "      vec3 aTimeUV;\n"
699   "\n"
700   "      for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)\n"
701   "      {\n"
702   "        ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + TRG_OFFSET (aSubTree));\n"
703   "\n"
704   "        vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x += VRT_OFFSET (aSubTree)).xyz;\n"
705   "        vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y += VRT_OFFSET (aSubTree)).xyz;\n"
706   "        vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z += VRT_OFFSET (aSubTree)).xyz;\n"
707   "\n"
708   "        IntersectTriangle (aSubTree.TrsfRay, aPoint0, aPoint1, aPoint2, aTimeUV, aNormal);\n"
709   "\n"
710   "#ifdef TRANSPARENT_SHADOWS\n"
711   "        if (aTimeUV.x < theDistance)\n"
712   "        {\n"
713   "          aFactor *= 1.f - texelFetch (uRaytraceMaterialTexture, MATERIAL_TRAN (aTriangle.w)).x;\n"
714   "        }\n"
715   "#else\n"
716   "        if (aTimeUV.x < theDistance)\n"
717   "        {\n"
718   "          aFactor = 0.f;\n"
719   "        }\n"
720   "#endif\n"
721   "      }\n"
722   "\n"
723   "      toContinue = (aHead >= 0) && (aFactor > 0.1f);\n"
724   "\n"
725   "      if (aHead == aStop) // go to top-level BVH\n"
726   "      {\n"
727   "        aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
728   "      }\n"
729   "\n"
730   "      if (aHead >= 0)\n"
731   "        aNode = pop (aHead);\n"
732   "    }\n"
733   "    else if (aData.x > 0) // switch node\n"
734   "    {\n"
735   "      aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root\n"
736   "\n"
737   "      vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);\n"
738   "      vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);\n"
739   "      vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);\n"
740   "      vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);\n"
741   "\n"
742   "      aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,\n"
743   "                                                      aInvTransf0,\n"
744   "                                                      aInvTransf1,\n"
745   "                                                      aInvTransf2);\n"
746   "\n"
747   "      aSubTree.TrsfRay.Origin = MatrixColMultiplyPnt (theRay.Origin,\n"
748   "                                                      aInvTransf0,\n"
749   "                                                      aInvTransf1,\n"
750   "                                                      aInvTransf2,\n"
751   "                                                      aInvTransf3);\n"
752   "\n"
753   "      aSubTree.Inverse = mix (-UNIT, UNIT, step (ZERO, aSubTree.TrsfRay.Direct)) / max (abs (aSubTree.TrsfRay.Direct), SMALL);\n"
754   "\n"
755   "      aNode = BVH_OFFSET (aSubTree); // go to sub-root node\n"
756   "\n"
757   "      aStop = aHead; // store current stack pointer\n"
758   "    }\n"
759   "  }\n"
760   "\n"
761   "  return aFactor;\n"
762   "}\n"
763   "\n"
764   "#define PI 3.1415926f\n"
765   "\n"
766   "// =======================================================================\n"
767   "// function : Latlong\n"
768   "// purpose  : Converts world direction to environment texture coordinates\n"
769   "// =======================================================================\n"
770   "vec2 Latlong (in vec3 thePoint, in float theRadius)\n"
771   "{\n"
772   "  float aPsi = acos (-thePoint.z / theRadius);\n"
773   "\n"
774   "  float aPhi = atan (thePoint.y, thePoint.x) + PI;\n"
775   "\n"
776   "  return vec2 (aPhi * 0.1591549f,\n"
777   "               aPsi * 0.3183098f);\n"
778   "}\n"
779   "\n"
780   "// =======================================================================\n"
781   "// function : SmoothNormal\n"
782   "// purpose  : Interpolates normal across the triangle\n"
783   "// =======================================================================\n"
784   "vec3 SmoothNormal (in vec2 theUV, in ivec4 theTriangle)\n"
785   "{\n"
786   "  vec3 aNormal0 = texelFetch (uGeometryNormalTexture, theTriangle.x).xyz;\n"
787   "  vec3 aNormal1 = texelFetch (uGeometryNormalTexture, theTriangle.y).xyz;\n"
788   "  vec3 aNormal2 = texelFetch (uGeometryNormalTexture, theTriangle.z).xyz;\n"
789   "\n"
790   "  return normalize (aNormal1 * theUV.x +\n"
791   "                    aNormal2 * theUV.y +\n"
792   "                    aNormal0 * (1.0f - theUV.x - theUV.y));\n"
793   "}\n"
794   "\n"
795   "#define POLYGON_OFFSET_UNIT 0.f\n"
796   "#define POLYGON_OFFSET_FACTOR 1.f\n"
797   "#define POLYGON_OFFSET_SCALE 0.006f\n"
798   "\n"
799   "// =======================================================================\n"
800   "// function : PolygonOffset\n"
801   "// purpose  : Computes OpenGL polygon offset\n"
802   "// =======================================================================\n"
803   "float PolygonOffset (in vec3 theNormal, in vec3 thePoint)\n"
804   "{\n"
805   "  vec4 aProjectedNorm = vec4 (theNormal, -dot (theNormal, thePoint)) * uUnviewMat;\n"
806   "\n"
807   "  float aPolygonOffset = POLYGON_OFFSET_UNIT;\n"
808   "\n"
809   "  if (aProjectedNorm.z * aProjectedNorm.z > 1e-20f)\n"
810   "  {\n"
811   "    aProjectedNorm.xy *= 1.f / aProjectedNorm.z;\n"
812   "\n"
813   "    aPolygonOffset += POLYGON_OFFSET_FACTOR * max (abs (aProjectedNorm.x),\n"
814   "                                                   abs (aProjectedNorm.y));\n"
815   "  }\n"
816   "\n"
817   "  return aPolygonOffset;\n"
818   "}\n"
819   "\n"
820   "// =======================================================================\n"
821   "// function : SmoothUV\n"
822   "// purpose  : Interpolates UV coordinates across the triangle\n"
823   "// =======================================================================\n"
824   "#ifdef USE_TEXTURES\n"
825   "vec2 SmoothUV (in vec2 theUV, in ivec4 theTriangle)\n"
826   "{\n"
827   "  vec2 aTexCrd0 = texelFetch (uGeometryTexCrdTexture, theTriangle.x).st;\n"
828   "  vec2 aTexCrd1 = texelFetch (uGeometryTexCrdTexture, theTriangle.y).st;\n"
829   "  vec2 aTexCrd2 = texelFetch (uGeometryTexCrdTexture, theTriangle.z).st;\n"
830   "\n"
831   "  return aTexCrd1 * theUV.x +\n"
832   "         aTexCrd2 * theUV.y +\n"
833   "         aTexCrd0 * (1.0f - theUV.x - theUV.y);\n"
834   "}\n"
835   "#endif\n"
836   "\n"
837   "// =======================================================================\n"
838   "// function : FetchEnvironment\n"
839   "// purpose  :\n"
840   "// =======================================================================\n"
841   "vec4 FetchEnvironment (in vec2 theTexCoord)\n"
842   "{\n"
843   "  return uSphereMapEnabled == 0 ?\n"
844   "    vec4 (0.f, 0.f, 0.f, 1.f) : textureLod (uEnvironmentMapTexture, theTexCoord, 0.f);\n"
845   "}\n"
846   "\n"
847   "// =======================================================================\n"
848   "// function : Refract\n"
849   "// purpose  : Computes refraction ray (also handles TIR)\n"
850   "// =======================================================================\n"
851   "#ifndef PATH_TRACING\n"
852   "vec3 Refract (in vec3 theInput,\n"
853   "              in vec3 theNormal,\n"
854   "              in float theRefractIndex,\n"
855   "              in float theInvRefractIndex)\n"
856   "{\n"
857   "  float aNdotI = dot (theInput, theNormal);\n"
858   "\n"
859   "  float anIndex = aNdotI < 0.0f\n"
860   "                ? theInvRefractIndex\n"
861   "                : theRefractIndex;\n"
862   "\n"
863   "  float aSquare = anIndex * anIndex * (1.0f - aNdotI * aNdotI);\n"
864   "\n"
865   "  if (aSquare > 1.0f)\n"
866   "  {\n"
867   "    return reflect (theInput, theNormal);\n"
868   "  }\n"
869   "\n"
870   "  float aNdotT = sqrt (1.0f - aSquare);\n"
871   "\n"
872   "  return normalize (anIndex * theInput -\n"
873   "    (anIndex * aNdotI + (aNdotI < 0.0f ? aNdotT : -aNdotT)) * theNormal);\n"
874   "}\n"
875   "#endif\n"
876   "\n"
877   "#define MIN_SLOPE 0.0001f\n"
878   "#define EPS_SCALE 8.0000f\n"
879   "\n"
880   "#define THRESHOLD vec3 (0.1f)\n"
881   "\n"
882   "#define INVALID_BOUNCES 1000\n"
883   "\n"
884   "#define LIGHT_POS(index) (2 * index + 1)\n"
885   "#define LIGHT_PWR(index) (2 * index + 0)\n"
886   "\n"
887   "// =======================================================================\n"
888   "// function : Radiance\n"
889   "// purpose  : Computes color along the given ray\n"
890   "// =======================================================================\n"
891   "#ifndef PATH_TRACING\n"
892   "vec4 Radiance (in SRay theRay, in vec3 theInverse)\n"
893   "{\n"
894   "  vec3 aResult = vec3 (0.0f);\n"
895   "  vec4 aWeight = vec4 (1.0f);\n"
896   "\n"
897   "  int aTrsfId;\n"
898   "\n"
899   "  float aRaytraceDepth = MAXFLOAT;\n"
900   "\n"
901   "  for (int aDepth = 0; aDepth < NB_BOUNCES; ++aDepth)\n"
902   "  {\n"
903   "    SIntersect aHit = SIntersect (MAXFLOAT, vec2 (ZERO), ZERO);\n"
904   "\n"
905   "    ivec4 aTriIndex = SceneNearestHit (theRay, theInverse, aHit, aTrsfId);\n"
906   "\n"
907   "    if (aTriIndex.x == -1)\n"
908   "    {\n"
909   "      vec4 aColor = vec4 (0.0);\n"
910   "\n"
911   "      if (bool(uSphereMapForBack) || aWeight.w == 0.0f /* reflection */)\n"
912   "      {\n"
913   "        float aTime = IntersectSphere (theRay, uSceneRadius);\n"
914   "\n"
915   "        aColor = FetchEnvironment (Latlong (\n"
916   "          theRay.Direct * aTime + theRay.Origin, uSceneRadius));\n"
917   "      }\n"
918   "      else\n"
919   "      {\n"
920   "        aColor = BackgroundColor();\n"
921   "      }\n"
922   "\n"
923   "      aResult += aWeight.xyz * aColor.xyz; aWeight.w *= aColor.w;\n"
924   "\n"
925   "      break; // terminate path\n"
926   "    }\n"
927   "\n"
928   "    vec3 aInvTransf0 = texelFetch (uSceneTransformTexture, aTrsfId + 0).xyz;\n"
929   "    vec3 aInvTransf1 = texelFetch (uSceneTransformTexture, aTrsfId + 1).xyz;\n"
930   "    vec3 aInvTransf2 = texelFetch (uSceneTransformTexture, aTrsfId + 2).xyz;\n"
931   "\n"
932   "    aHit.Normal = normalize (vec3 (dot (aInvTransf0, aHit.Normal),\n"
933   "                                   dot (aInvTransf1, aHit.Normal),\n"
934   "                                   dot (aInvTransf2, aHit.Normal)));\n"
935   "\n"
936   "    theRay.Origin += theRay.Direct * aHit.Time; // intersection point\n"
937   "\n"
938   "    // Evaluate depth on first hit\n"
939   "    if (aDepth == 0)\n"
940   "    {\n"
941   "      vec4 aNDCPoint = uViewMat * vec4 (theRay.Origin, 1.f);\n"
942   "\n"
943   "      float aPolygonOffset = PolygonOffset (aHit.Normal, theRay.Origin);\n"
944   "      aRaytraceDepth = (aNDCPoint.z / aNDCPoint.w + aPolygonOffset * POLYGON_OFFSET_SCALE) * 0.5f + 0.5f;\n"
945   "    }\n"
946   "\n"
947   "    vec3 aNormal = SmoothNormal (aHit.UV, aTriIndex);\n"
948   "\n"
949   "    aNormal = normalize (vec3 (dot (aInvTransf0, aNormal),\n"
950   "                               dot (aInvTransf1, aNormal),\n"
951   "                               dot (aInvTransf2, aNormal)));\n"
952   "\n"
953   "    vec3 aAmbient  = texelFetch (\n"
954   "      uRaytraceMaterialTexture, MATERIAL_AMBN (aTriIndex.w)).rgb;\n"
955   "    vec4 aDiffuse  = texelFetch (\n"
956   "      uRaytraceMaterialTexture, MATERIAL_DIFF (aTriIndex.w));\n"
957   "    vec4 aSpecular = texelFetch (\n"
958   "      uRaytraceMaterialTexture, MATERIAL_SPEC (aTriIndex.w));\n"
959   "    vec4 aOpacity  = texelFetch (\n"
960   "      uRaytraceMaterialTexture, MATERIAL_TRAN (aTriIndex.w));\n"
961   "\n"
962   "#ifdef USE_TEXTURES\n"
963   "    if (aDiffuse.w >= 0.f)\n"
964   "    {\n"
965   "      vec4 aTexCoord = vec4 (SmoothUV (aHit.UV, aTriIndex), 0.f, 1.f);\n"
966   "\n"
967   "      vec4 aTrsfRow1 = texelFetch (\n"
968   "        uRaytraceMaterialTexture, MATERIAL_TRS1 (aTriIndex.w));\n"
969   "      vec4 aTrsfRow2 = texelFetch (\n"
970   "        uRaytraceMaterialTexture, MATERIAL_TRS2 (aTriIndex.w));\n"
971   "\n"
972   "      aTexCoord.st = vec2 (dot (aTrsfRow1, aTexCoord),\n"
973   "                           dot (aTrsfRow2, aTexCoord));\n"
974   "\n"
975   "      vec4 aTexColor = textureLod (\n"
976   "        sampler2D (uTextureSamplers[int(aDiffuse.w)]), aTexCoord.st, 0.f);\n"
977   "\n"
978   "      aDiffuse.rgb *= aTexColor.rgb;\n"
979   "      aAmbient.rgb *= aTexColor.rgb;\n"
980   "\n"
981   "      // keep refractive index untouched (Z component)\n"
982   "      aOpacity.xy = vec2 (aTexColor.w * aOpacity.x, 1.0f - aTexColor.w * aOpacity.x);\n"
983   "    }\n"
984   "#endif\n"
985   "\n"
986   "    vec3 aEmission = texelFetch (\n"
987   "      uRaytraceMaterialTexture, MATERIAL_EMIS (aTriIndex.w)).rgb;\n"
988   "\n"
989   "    float aGeomFactor = dot (aNormal, theRay.Direct);\n"
990   "\n"
991   "    aResult.xyz += aWeight.xyz * aOpacity.x * (\n"
992   "      uGlobalAmbient.xyz * aAmbient * max (abs (aGeomFactor), 0.5f) + aEmission);\n"
993   "\n"
994   "    vec3 aSidedNormal = mix (aNormal, -aNormal, step (0.0f, aGeomFactor));\n"
995   "\n"
996   "    for (int aLightIdx = 0; aLightIdx < uLightCount; ++aLightIdx)\n"
997   "    {\n"
998   "      vec4 aLight = texelFetch (\n"
999   "        uRaytraceLightSrcTexture, LIGHT_POS (aLightIdx));\n"
1000   "\n"
1001   "      float aDistance = MAXFLOAT;\n"
1002   "\n"
1003   "      if (aLight.w != 0.0f) // point light source\n"
1004   "      {\n"
1005   "        aDistance = length (aLight.xyz -= theRay.Origin);\n"
1006   "\n"
1007   "        aLight.xyz *= 1.0f / aDistance;\n"
1008   "      }\n"
1009   "\n"
1010   "      float aLdotN = dot (aLight.xyz, aSidedNormal);\n"
1011   "\n"
1012   "      if (aLdotN > 0.0f) // first check if light source is important\n"
1013   "      {\n"
1014   "        float aVisibility = 1.0f;\n"
1015   "\n"
1016   "        if (bool(uShadowsEnabled))\n"
1017   "        {\n"
1018   "          SRay aShadow = SRay (theRay.Origin, aLight.xyz);\n"
1019   "\n"
1020   "          aShadow.Origin += uSceneEpsilon * (aLight.xyz +\n"
1021   "            mix (-aHit.Normal, aHit.Normal, step (0.0f, dot (aHit.Normal, aLight.xyz))));\n"
1022   "\n"
1023   "          vec3 aInverse = 1.0f / max (abs (aLight.xyz), SMALL);\n"
1024   "\n"
1025   "          aVisibility = SceneAnyHit (\n"
1026   "            aShadow, mix (-aInverse, aInverse, step (ZERO, aLight.xyz)), aDistance);\n"
1027   "        }\n"
1028   "\n"
1029   "        if (aVisibility > 0.0f)\n"
1030   "        {\n"
1031   "          vec3 aIntensity = min (UNIT, vec3 (texelFetch (\n"
1032   "            uRaytraceLightSrcTexture, LIGHT_PWR (aLightIdx))));\n"
1033   "\n"
1034   "          float aRdotV = dot (reflect (aLight.xyz, aSidedNormal), theRay.Direct);\n"
1035   "\n"
1036   "          aResult.xyz += aWeight.xyz * (aOpacity.x * aVisibility) * aIntensity *\n"
1037   "            (aDiffuse.xyz * aLdotN + aSpecular.xyz * pow (max (0.f, aRdotV), aSpecular.w));\n"
1038   "        }\n"
1039   "      }\n"
1040   "    }\n"
1041   "\n"
1042   "    if (aOpacity.x != 1.0f)\n"
1043   "    {\n"
1044   "      aWeight *= aOpacity.y;\n"
1045   "\n"
1046   "      if (aOpacity.z != 1.0f)\n"
1047   "      {\n"
1048   "        theRay.Direct = Refract (theRay.Direct, aNormal, aOpacity.z, aOpacity.w);\n"
1049   "      }\n"
1050   "    }\n"
1051   "    else\n"
1052   "    {\n"
1053   "      aWeight *= bool(uReflectEnabled) ?\n"
1054   "        texelFetch (uRaytraceMaterialTexture, MATERIAL_REFL (aTriIndex.w)) : vec4 (0.0f);\n"
1055   "\n"
1056   "      vec3 aReflect = reflect (theRay.Direct, aNormal);\n"
1057   "\n"
1058   "      if (dot (aReflect, aHit.Normal) * dot (theRay.Direct, aHit.Normal) > 0.0f)\n"
1059   "      {\n"
1060   "        aReflect = reflect (theRay.Direct, aHit.Normal);\n"
1061   "      }\n"
1062   "\n"
1063   "      theRay.Direct = aReflect;\n"
1064   "    }\n"
1065   "\n"
1066   "    if (all (lessThanEqual (aWeight.xyz, THRESHOLD)))\n"
1067   "    {\n"
1068   "      aDepth = INVALID_BOUNCES;\n"
1069   "    }\n"
1070   "    else if (aOpacity.x == 1.0f || aOpacity.z != 1.0f) // if no simple transparency\n"
1071   "    {\n"
1072   "      theRay.Origin += aHit.Normal * mix (\n"
1073   "        -uSceneEpsilon, uSceneEpsilon, step (0.0f, dot (aHit.Normal, theRay.Direct)));\n"
1074   "\n"
1075   "      theInverse = 1.0f / max (abs (theRay.Direct), SMALL);\n"
1076   "\n"
1077   "      theInverse = mix (-theInverse, theInverse, step (ZERO, theRay.Direct));\n"
1078   "    }\n"
1079   "\n"
1080   "    theRay.Origin += theRay.Direct * uSceneEpsilon;\n"
1081   "  }\n"
1082   "\n"
1083   "  gl_FragDepth = aRaytraceDepth;\n"
1084   "\n"
1085   "  return vec4 (aResult.x,\n"
1086   "               aResult.y,\n"
1087   "               aResult.z,\n"
1088   "               aWeight.w);\n"
1089   "}\n"
1090   "#endif\n";