1 // This file has been automatically generated from resource file src/Shaders/RaytraceBase.fs
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"
9 "#ifdef USE_TEXTURES\n"
10 " #extension GL_ARB_bindless_texture : require\n"
13 "//! Normalized pixel coordinates.\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"
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"
30 "//! Width of the rendering window.\n"
31 "uniform int uWinSizeX;\n"
32 "//! Height of the rendering window.\n"
33 "uniform int uWinSizeY;\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"
44 "//! Inverse model-view-projection matrix.\n"
45 "uniform mat4 uUnviewMat;\n"
47 "//! Model-view-projection matrix.\n"
48 "uniform mat4 uViewMat;\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"
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"
67 "//! Texture buffer of triangle indices.\n"
68 "uniform isamplerBuffer uGeometryTriangTexture;\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"
77 "//! Total number of light sources.\n"
78 "uniform int uLightCount;\n"
79 "//! Intensity of global ambient light.\n"
80 "uniform vec4 uGlobalAmbient;\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"
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"
96 "#ifdef USE_TEXTURES\n"
97 " //! Unique 64-bit handles of OpenGL textures.\n"
98 " uniform uvec2 uTextureSamplers[MAX_TEX_NUMBER];\n"
101 "#ifdef ADAPTIVE_SAMPLING\n"
102 " //! OpenGL image used for accumulating rendering result.\n"
103 " volatile restrict layout(size1x32) uniform image2D uRenderImage;\n"
105 " //! OpenGL image storing offsets of sampled pixels blocks.\n"
106 " coherent restrict layout(size2x32) uniform iimage2D uOffsetImage;\n"
108 " //! Screen space tile size.\n"
109 " uniform ivec2 uTileSize;\n"
112 "//! Top color of gradient background.\n"
113 "uniform vec4 uBackColorTop = vec4 (0.0);\n"
114 "//! Bottom color of gradient background.\n"
115 "uniform vec4 uBackColorBot = vec4 (0.0);\n"
117 "//! Aperture radius of camera used for depth-of-field\n"
118 "uniform float uApertureRadius = 0.f;\n"
120 "//! Focal distance of camera used for depth-of field\n"
121 "uniform float uFocalPlaneDist = 10.f;\n"
123 "//! Camera position used for projective mode\n"
124 "uniform vec3 uEyeOrig;\n"
126 "//! Camera view direction used for projective mode\n"
127 "uniform vec3 uEyeView;\n"
129 "//! Camera's screen vertical direction used for projective mode\n"
130 "uniform vec3 uEyeVert;\n"
132 "//! Camera's screen horizontal direction used for projective mode\n"
133 "uniform vec3 uEyeSide;\n"
135 "//! Camera's screen size used for projective mode\n"
136 "uniform vec2 uEyeSize;\n"
138 "/////////////////////////////////////////////////////////////////////////////////////////\n"
139 "// Specific data types\n"
141 "//! Stores ray parameters.\n"
149 "//! Stores intersection parameters.\n"
150 "struct SIntersect\n"
159 "/////////////////////////////////////////////////////////////////////////////////////////\n"
160 "// Some useful constants\n"
162 "#define MAXFLOAT 1e15f\n"
164 "#define SMALL vec3 (exp2 (-80.0f))\n"
166 "#define ZERO vec3 (0.0f, 0.0f, 0.0f)\n"
167 "#define UNIT vec3 (1.0f, 1.0f, 1.0f)\n"
169 "#define AXIS_X vec3 (1.0f, 0.0f, 0.0f)\n"
170 "#define AXIS_Y vec3 (0.0f, 1.0f, 0.0f)\n"
171 "#define AXIS_Z vec3 (0.0f, 0.0f, 1.0f)\n"
173 "#define M_PI 3.141592653f\n"
174 "#define M_2_PI 6.283185307f\n"
175 "#define M_PI_2 1.570796327f\n"
177 "#define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)\n"
179 "// =======================================================================\n"
180 "// function : MatrixRowMultiplyDir\n"
181 "// purpose : Multiplies a vector by matrix\n"
182 "// =======================================================================\n"
183 "vec3 MatrixRowMultiplyDir (in vec3 v,\n"
188 " return vec3 (dot (m0.xyz, v),\n"
189 " dot (m1.xyz, v),\n"
190 " dot (m2.xyz, v));\n"
193 "//! 32-bit state of random number generator.\n"
196 "// =======================================================================\n"
197 "// function : SeedRand\n"
198 "// purpose : Applies hash function by Thomas Wang to randomize seeds\n"
199 "// (see http://www.burtleburtle.net/bob/hash/integer.html)\n"
200 "// =======================================================================\n"
201 "void SeedRand (in int theSeed, in int theSizeX, in int theRadius)\n"
203 " RandState = uint (int (gl_FragCoord.y) / theRadius * theSizeX + int (gl_FragCoord.x) / theRadius + theSeed);\n"
205 " RandState = (RandState + 0x479ab41du) + (RandState << 8);\n"
206 " RandState = (RandState ^ 0xe4aa10ceu) ^ (RandState >> 5);\n"
207 " RandState = (RandState + 0x9942f0a6u) - (RandState << 14);\n"
208 " RandState = (RandState ^ 0x5aedd67du) ^ (RandState >> 3);\n"
209 " RandState = (RandState + 0x17bea992u) + (RandState << 7);\n"
212 "// =======================================================================\n"
213 "// function : RandInt\n"
214 "// purpose : Generates integer using Xorshift algorithm by G. Marsaglia\n"
215 "// =======================================================================\n"
218 " RandState ^= (RandState << 13);\n"
219 " RandState ^= (RandState >> 17);\n"
220 " RandState ^= (RandState << 5);\n"
222 " return RandState;\n"
225 "// =======================================================================\n"
226 "// function : RandFloat\n"
227 "// purpose : Generates a random float in 0 <= x < 1 range\n"
228 "// =======================================================================\n"
229 "float RandFloat()\n"
231 " return float (RandInt()) * (1.f / 4294967296.f);\n"
234 "// =======================================================================\n"
235 "// function : MatrixColMultiplyPnt\n"
236 "// purpose : Multiplies a vector by matrix\n"
237 "// =======================================================================\n"
238 "vec3 MatrixColMultiplyPnt (in vec3 v,\n"
244 " return vec3 (m0.x * v.x + m1.x * v.y + m2.x * v.z + m3.x,\n"
245 " m0.y * v.x + m1.y * v.y + m2.y * v.z + m3.y,\n"
246 " m0.z * v.x + m1.z * v.y + m2.z * v.z + m3.z);\n"
249 "// =======================================================================\n"
250 "// function : MatrixColMultiplyDir\n"
251 "// purpose : Multiplies a vector by matrix\n"
252 "// =======================================================================\n"
253 "vec3 MatrixColMultiplyDir (in vec3 v,\n"
258 " return vec3 (m0.x * v.x + m1.x * v.y + m2.x * v.z,\n"
259 " m0.y * v.x + m1.y * v.y + m2.y * v.z,\n"
260 " m0.z * v.x + m1.z * v.y + m2.z * v.z);\n"
263 "//=======================================================================\n"
264 "// function : InverseDirection\n"
265 "// purpose : Returns safely inverted direction of the given one\n"
266 "//=======================================================================\n"
267 "vec3 InverseDirection (in vec3 theInput)\n"
269 " vec3 anInverse = 1.f / max (abs (theInput), SMALL);\n"
271 " return mix (-anInverse, anInverse, step (ZERO, theInput));\n"
274 "//=======================================================================\n"
275 "// function : BackgroundColor\n"
276 "// purpose : Returns color of gradient background\n"
277 "//=======================================================================\n"
278 "vec4 BackgroundColor()\n"
280 "#ifdef ADAPTIVE_SAMPLING\n"
282 " ivec2 aFragCoord = ivec2 (gl_FragCoord.xy);\n"
284 " ivec2 aTileXY = imageLoad (uOffsetImage, aFragCoord / uTileSize).xy * uTileSize;\n"
286 " aTileXY.y += aFragCoord.y % min (uWinSizeY - aTileXY.y, uTileSize.y);\n"
288 " return mix (uBackColorBot, uBackColorTop, float (aTileXY.y) / uWinSizeY);\n"
292 " return mix (uBackColorBot, uBackColorTop, vPixel.y);\n"
297 "/////////////////////////////////////////////////////////////////////////////////////////\n"
298 "// Functions for compute ray-object intersection\n"
300 "//=======================================================================\n"
301 "// function : sampleUniformDisk\n"
303 "//=======================================================================\n"
304 "vec2 sampleUniformDisk ()\n"
308 " float aKsi1 = 2.f * RandFloat () - 1.f;\n"
309 " float aKsi2 = 2.f * RandFloat () - 1.f;\n"
311 " if (aKsi1 > -aKsi2)\n"
313 " if (aKsi1 > aKsi2)\n"
314 " aPoint = vec2 (aKsi1, (M_PI / 4.f) * (0.f + aKsi2 / aKsi1));\n"
316 " aPoint = vec2 (aKsi2, (M_PI / 4.f) * (2.f - aKsi1 / aKsi2));\n"
320 " if (aKsi1 < aKsi2)\n"
321 " aPoint = vec2 (-aKsi1, (M_PI / 4.f) * (4.f + aKsi2 / aKsi1));\n"
323 " aPoint = vec2 (-aKsi2, (M_PI / 4.f) * (6.f - aKsi1 / aKsi2));\n"
326 " return vec2 (sin (aPoint.y), cos (aPoint.y)) * aPoint.x;\n"
329 "// =======================================================================\n"
330 "// function : GenerateRay\n"
332 "// =======================================================================\n"
333 "SRay GenerateRay (in vec2 thePixel)\n"
335 "#ifndef DEPTH_OF_FIELD\n"
337 " vec3 aP0 = mix (uOriginLB, uOriginRB, thePixel.x);\n"
338 " vec3 aP1 = mix (uOriginLT, uOriginRT, thePixel.x);\n"
340 " vec3 aD0 = mix (uDirectLB, uDirectRB, thePixel.x);\n"
341 " vec3 aD1 = mix (uDirectLT, uDirectRT, thePixel.x);\n"
343 " vec3 aDirection = normalize (mix (aD0, aD1, thePixel.y));\n"
345 " return SRay (mix (aP0, aP1, thePixel.y), aDirection);\n"
349 " vec2 aPixel = uEyeSize * (thePixel - vec2 (0.5f)) * 2.f;\n"
351 " vec2 aAperturePnt = sampleUniformDisk () * uApertureRadius;\n"
353 " vec3 aLocalDir = normalize (vec3 (\n"
354 " aPixel * uFocalPlaneDist - aAperturePnt, uFocalPlaneDist));\n"
356 " vec3 aOrigin = uEyeOrig +\n"
357 " uEyeSide * aAperturePnt.x +\n"
358 " uEyeVert * aAperturePnt.y;\n"
360 " vec3 aDirect = uEyeView * aLocalDir.z +\n"
361 " uEyeSide * aLocalDir.x +\n"
362 " uEyeVert * aLocalDir.y;\n"
364 " return SRay (aOrigin, aDirect);\n"
369 "// =======================================================================\n"
370 "// function : IntersectSphere\n"
371 "// purpose : Computes ray-sphere intersection\n"
372 "// =======================================================================\n"
373 "float IntersectSphere (in SRay theRay, in float theRadius)\n"
375 " float aDdotD = dot (theRay.Direct, theRay.Direct);\n"
376 " float aDdotO = dot (theRay.Direct, theRay.Origin);\n"
377 " float aOdotO = dot (theRay.Origin, theRay.Origin);\n"
379 " float aD = aDdotO * aDdotO - aDdotD * (aOdotO - theRadius * theRadius);\n"
383 " float aTime = (sqrt (aD) - aDdotO) * (1.0f / aDdotD);\n"
385 " return aTime > 0.0f ? aTime : MAXFLOAT;\n"
388 " return MAXFLOAT;\n"
391 "// =======================================================================\n"
392 "// function : IntersectTriangle\n"
393 "// purpose : Computes ray-triangle intersection (branchless version)\n"
394 "// =======================================================================\n"
395 "void IntersectTriangle (in SRay theRay,\n"
396 " in vec3 thePnt0,\n"
397 " in vec3 thePnt1,\n"
398 " in vec3 thePnt2,\n"
399 " out vec3 theUVT,\n"
400 " out vec3 theNorm)\n"
402 " vec3 aToTrg = thePnt0 - theRay.Origin;\n"
404 " vec3 aEdge0 = thePnt1 - thePnt0;\n"
405 " vec3 aEdge1 = thePnt0 - thePnt2;\n"
407 " theNorm = cross (aEdge1, aEdge0);\n"
409 " vec3 theVect = cross (theRay.Direct, aToTrg);\n"
411 " theUVT = vec3 (dot (theNorm, aToTrg),\n"
412 " dot (theVect, aEdge1),\n"
413 " dot (theVect, aEdge0)) * (1.f / dot (theNorm, theRay.Direct));\n"
415 " theUVT.x = any (lessThan (theUVT, ZERO)) || (theUVT.y + theUVT.z) > 1.f ? MAXFLOAT : theUVT.x;\n"
418 "#define EMPTY_ROOT ivec4(0)\n"
420 "//! Utility structure containing information about\n"
421 "//! currently traversing sub-tree of scene's BVH.\n"
424 " //! Transformed ray.\n"
427 " //! Inversed ray direction.\n"
430 " //! Parameters of sub-root node.\n"
434 "#define MATERIAL_AMBN(index) (19 * index + 0)\n"
435 "#define MATERIAL_DIFF(index) (19 * index + 1)\n"
436 "#define MATERIAL_SPEC(index) (19 * index + 2)\n"
437 "#define MATERIAL_EMIS(index) (19 * index + 3)\n"
438 "#define MATERIAL_REFL(index) (19 * index + 4)\n"
439 "#define MATERIAL_REFR(index) (19 * index + 5)\n"
440 "#define MATERIAL_TRAN(index) (19 * index + 6)\n"
441 "#define MATERIAL_TRS1(index) (19 * index + 7)\n"
442 "#define MATERIAL_TRS2(index) (19 * index + 8)\n"
443 "#define MATERIAL_TRS3(index) (19 * index + 9)\n"
445 "#define TRS_OFFSET(treelet) treelet.SubData.x\n"
446 "#define BVH_OFFSET(treelet) treelet.SubData.y\n"
447 "#define VRT_OFFSET(treelet) treelet.SubData.z\n"
448 "#define TRG_OFFSET(treelet) treelet.SubData.w\n"
450 "//! Identifies the absence of intersection.\n"
451 "#define INALID_HIT ivec4 (-1)\n"
453 "//! Global stack shared between traversal functions.\n"
454 "int Stack[STACK_SIZE];\n"
456 "// =======================================================================\n"
457 "// function : pop\n"
459 "// =======================================================================\n"
460 "int pop (inout int theHead)\n"
462 " int aData = Stack[theHead];\n"
464 " int aMask = aData >> 26;\n"
465 " int aNode = aMask & 0x3;\n"
469 " if ((aMask & 0x3) == aNode)\n"
475 " aMask |= (aMask << 2) & 0x30;\n"
477 " Stack[theHead] = (aData & 0x03FFFFFF) | (aMask << 26);\n"
480 " return (aData & 0x03FFFFFF) + aNode;\n"
483 "// =======================================================================\n"
484 "// function : SceneNearestHit\n"
485 "// purpose : Finds intersection with nearest scene triangle\n"
486 "// =======================================================================\n"
487 "ivec4 SceneNearestHit (in SRay theRay, in vec3 theInverse, inout SIntersect theHit, out int theTrsfId)\n"
489 " ivec4 aTriIndex = INALID_HIT;\n"
491 " int aNode = 0; // node to traverse\n"
492 " int aHead = -1; // pointer of stack\n"
493 " int aStop = -1; // BVH level switch\n"
495 " SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
497 " for (bool toContinue = true; toContinue; /* none */)\n"
499 " ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);\n"
501 " if (aData.x == 0) // if inner node\n"
503 " aData.y += BVH_OFFSET (aSubTree);\n"
505 " vec4 aHitTimes = vec4 (MAXFLOAT,\n"
510 " vec3 aRayOriginInverse = -aSubTree.TrsfRay.Origin * aSubTree.Inverse;\n"
512 " vec3 aNodeMin0 = texelFetch (uSceneMinPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
513 " vec3 aNodeMin1 = texelFetch (uSceneMinPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
514 " vec3 aNodeMin2 = texelFetch (uSceneMinPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
515 " vec3 aNodeMin3 = texelFetch (uSceneMinPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
516 " vec3 aNodeMax0 = texelFetch (uSceneMaxPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
517 " vec3 aNodeMax1 = texelFetch (uSceneMaxPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
518 " vec3 aNodeMax2 = texelFetch (uSceneMaxPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
519 " vec3 aNodeMax3 = texelFetch (uSceneMaxPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
521 " vec3 aTimeMax = max (aNodeMin0, aNodeMax0);\n"
522 " vec3 aTimeMin = min (aNodeMin0, aNodeMax0);\n"
524 " float aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
525 " float aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
527 " aHitTimes.x = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;\n"
529 " aTimeMax = max (aNodeMin1, aNodeMax1);\n"
530 " aTimeMin = min (aNodeMin1, aNodeMax1);\n"
532 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
533 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
535 " aHitTimes.y = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;\n"
537 " aTimeMax = max (aNodeMin2, aNodeMax2);\n"
538 " aTimeMin = min (aNodeMin2, aNodeMax2);\n"
540 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
541 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
543 " aHitTimes.z = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f && aData.z > 1) ? aTimeEnter : MAXFLOAT;\n"
545 " aTimeMax = max (aNodeMin3, aNodeMax3);\n"
546 " aTimeMin = min (aNodeMin3, aNodeMax3);\n"
548 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
549 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
551 " aHitTimes.w = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f && aData.z > 2) ? aTimeEnter : MAXFLOAT;\n"
553 " ivec4 aChildren = ivec4 (0, 1, 2, 3);\n"
555 " aChildren.xy = aHitTimes.y < aHitTimes.x ? aChildren.yx : aChildren.xy;\n"
556 " aHitTimes.xy = aHitTimes.y < aHitTimes.x ? aHitTimes.yx : aHitTimes.xy;\n"
557 " aChildren.zw = aHitTimes.w < aHitTimes.z ? aChildren.wz : aChildren.zw;\n"
558 " aHitTimes.zw = aHitTimes.w < aHitTimes.z ? aHitTimes.wz : aHitTimes.zw;\n"
559 " aChildren.xz = aHitTimes.z < aHitTimes.x ? aChildren.zx : aChildren.xz;\n"
560 " aHitTimes.xz = aHitTimes.z < aHitTimes.x ? aHitTimes.zx : aHitTimes.xz;\n"
561 " aChildren.yw = aHitTimes.w < aHitTimes.y ? aChildren.wy : aChildren.yw;\n"
562 " aHitTimes.yw = aHitTimes.w < aHitTimes.y ? aHitTimes.wy : aHitTimes.yw;\n"
563 " aChildren.yz = aHitTimes.z < aHitTimes.y ? aChildren.zy : aChildren.yz;\n"
564 " aHitTimes.yz = aHitTimes.z < aHitTimes.y ? aHitTimes.zy : aHitTimes.yz;\n"
566 " if (aHitTimes.x != MAXFLOAT)\n"
568 " int aHitMask = (aHitTimes.w != MAXFLOAT ? aChildren.w : aChildren.z) << 2\n"
569 " | (aHitTimes.z != MAXFLOAT ? aChildren.z : aChildren.y);\n"
571 " if (aHitTimes.y != MAXFLOAT)\n"
572 " Stack[++aHead] = aData.y | (aHitMask << 2 | aChildren.y) << 26;\n"
574 " aNode = aData.y + aChildren.x;\n"
578 " toContinue = (aHead >= 0);\n"
580 " if (aHead == aStop) // go to top-level BVH\n"
582 " aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
586 " aNode = pop (aHead);\n"
589 " else if (aData.x < 0) // leaf node (contains triangles)\n"
594 " for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)\n"
596 " ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + TRG_OFFSET (aSubTree));\n"
598 " vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x += VRT_OFFSET (aSubTree)).xyz;\n"
599 " vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y += VRT_OFFSET (aSubTree)).xyz;\n"
600 " vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z += VRT_OFFSET (aSubTree)).xyz;\n"
602 " IntersectTriangle (aSubTree.TrsfRay, aPoint0, aPoint1, aPoint2, aTimeUV, aNormal);\n"
604 " if (aTimeUV.x < theHit.Time)\n"
606 " aTriIndex = aTriangle;\n"
608 " theTrsfId = TRS_OFFSET (aSubTree);\n"
610 " theHit = SIntersect (aTimeUV.x, aTimeUV.yz, aNormal);\n"
614 " toContinue = (aHead >= 0);\n"
616 " if (aHead == aStop) // go to top-level BVH\n"
618 " aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
622 " aNode = pop (aHead);\n"
624 " else if (aData.x > 0) // switch node\n"
626 " aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root\n"
628 " vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);\n"
629 " vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);\n"
630 " vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);\n"
631 " vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);\n"
633 " aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,\n"
638 " aSubTree.Inverse = mix (-UNIT, UNIT, step (ZERO, aSubTree.TrsfRay.Direct)) /\n"
639 " max (abs (aSubTree.TrsfRay.Direct), SMALL);\n"
641 " aSubTree.TrsfRay.Origin = MatrixColMultiplyPnt (theRay.Origin,\n"
647 " aNode = BVH_OFFSET (aSubTree); // go to sub-root node\n"
649 " aStop = aHead; // store current stack pointer\n"
653 " return aTriIndex;\n"
656 "// =======================================================================\n"
657 "// function : SceneAnyHit\n"
658 "// purpose : Finds intersection with any scene triangle\n"
659 "// =======================================================================\n"
660 "float SceneAnyHit (in SRay theRay, in vec3 theInverse, in float theDistance)\n"
662 " float aFactor = 1.f;\n"
664 " int aNode = 0; // node to traverse\n"
665 " int aHead = -1; // pointer of stack\n"
666 " int aStop = -1; // BVH level switch\n"
668 " SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
670 " for (bool toContinue = true; toContinue; /* none */)\n"
672 " ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);\n"
674 " if (aData.x == 0) // if inner node\n"
676 " aData.y += BVH_OFFSET (aSubTree);\n"
678 " vec4 aHitTimes = vec4 (MAXFLOAT,\n"
683 " vec3 aRayOriginInverse = -aSubTree.TrsfRay.Origin * aSubTree.Inverse;\n"
685 " vec3 aNodeMin0 = texelFetch (uSceneMinPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
686 " vec3 aNodeMin1 = texelFetch (uSceneMinPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
687 " vec3 aNodeMin2 = texelFetch (uSceneMinPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
688 " vec3 aNodeMin3 = texelFetch (uSceneMinPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
689 " vec3 aNodeMax0 = texelFetch (uSceneMaxPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
690 " vec3 aNodeMax1 = texelFetch (uSceneMaxPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
691 " vec3 aNodeMax2 = texelFetch (uSceneMaxPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
692 " vec3 aNodeMax3 = texelFetch (uSceneMaxPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
694 " vec3 aTimeMax = max (aNodeMin0, aNodeMax0);\n"
695 " vec3 aTimeMin = min (aNodeMin0, aNodeMax0);\n"
697 " float aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
698 " float aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
700 " aHitTimes.x = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;\n"
702 " aTimeMax = max (aNodeMin1, aNodeMax1);\n"
703 " aTimeMin = min (aNodeMin1, aNodeMax1);\n"
705 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
706 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
708 " aHitTimes.y = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;\n"
710 " aTimeMax = max (aNodeMin2, aNodeMax2);\n"
711 " aTimeMin = min (aNodeMin2, aNodeMax2);\n"
713 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
714 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
716 " aHitTimes.z = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f && aData.z > 1) ? aTimeEnter : MAXFLOAT;\n"
718 " aTimeMax = max (aNodeMin3, aNodeMax3);\n"
719 " aTimeMin = min (aNodeMin3, aNodeMax3);\n"
721 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
722 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
724 " aHitTimes.w = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f && aData.z > 2) ? aTimeEnter : MAXFLOAT;\n"
726 " ivec4 aChildren = ivec4 (0, 1, 2, 3);\n"
728 " aChildren.xy = aHitTimes.y < aHitTimes.x ? aChildren.yx : aChildren.xy;\n"
729 " aHitTimes.xy = aHitTimes.y < aHitTimes.x ? aHitTimes.yx : aHitTimes.xy;\n"
730 " aChildren.zw = aHitTimes.w < aHitTimes.z ? aChildren.wz : aChildren.zw;\n"
731 " aHitTimes.zw = aHitTimes.w < aHitTimes.z ? aHitTimes.wz : aHitTimes.zw;\n"
732 " aChildren.xz = aHitTimes.z < aHitTimes.x ? aChildren.zx : aChildren.xz;\n"
733 " aHitTimes.xz = aHitTimes.z < aHitTimes.x ? aHitTimes.zx : aHitTimes.xz;\n"
734 " aChildren.yw = aHitTimes.w < aHitTimes.y ? aChildren.wy : aChildren.yw;\n"
735 " aHitTimes.yw = aHitTimes.w < aHitTimes.y ? aHitTimes.wy : aHitTimes.yw;\n"
736 " aChildren.yz = aHitTimes.z < aHitTimes.y ? aChildren.zy : aChildren.yz;\n"
737 " aHitTimes.yz = aHitTimes.z < aHitTimes.y ? aHitTimes.zy : aHitTimes.yz;\n"
739 " if (aHitTimes.x != MAXFLOAT)\n"
741 " int aHitMask = (aHitTimes.w != MAXFLOAT ? aChildren.w : aChildren.z) << 2\n"
742 " | (aHitTimes.z != MAXFLOAT ? aChildren.z : aChildren.y);\n"
744 " if (aHitTimes.y != MAXFLOAT)\n"
745 " Stack[++aHead] = aData.y | (aHitMask << 2 | aChildren.y) << 26;\n"
747 " aNode = aData.y + aChildren.x;\n"
751 " toContinue = (aHead >= 0);\n"
753 " if (aHead == aStop) // go to top-level BVH\n"
755 " aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
759 " aNode = pop (aHead);\n"
762 " else if (aData.x < 0) // leaf node\n"
767 " for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)\n"
769 " ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + TRG_OFFSET (aSubTree));\n"
771 " vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x += VRT_OFFSET (aSubTree)).xyz;\n"
772 " vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y += VRT_OFFSET (aSubTree)).xyz;\n"
773 " vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z += VRT_OFFSET (aSubTree)).xyz;\n"
775 " IntersectTriangle (aSubTree.TrsfRay, aPoint0, aPoint1, aPoint2, aTimeUV, aNormal);\n"
777 "#ifdef TRANSPARENT_SHADOWS\n"
778 " if (aTimeUV.x < theDistance)\n"
780 " aFactor *= 1.f - texelFetch (uRaytraceMaterialTexture, MATERIAL_TRAN (aTriangle.w)).x;\n"
783 " if (aTimeUV.x < theDistance)\n"
790 " toContinue = (aHead >= 0) && (aFactor > 0.1f);\n"
792 " if (aHead == aStop) // go to top-level BVH\n"
794 " aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
798 " aNode = pop (aHead);\n"
800 " else if (aData.x > 0) // switch node\n"
802 " aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root\n"
804 " vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);\n"
805 " vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);\n"
806 " vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);\n"
807 " vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);\n"
809 " aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,\n"
814 " aSubTree.TrsfRay.Origin = MatrixColMultiplyPnt (theRay.Origin,\n"
820 " aSubTree.Inverse = mix (-UNIT, UNIT, step (ZERO, aSubTree.TrsfRay.Direct)) / max (abs (aSubTree.TrsfRay.Direct), SMALL);\n"
822 " aNode = BVH_OFFSET (aSubTree); // go to sub-root node\n"
824 " aStop = aHead; // store current stack pointer\n"
831 "#define PI 3.1415926f\n"
833 "// =======================================================================\n"
834 "// function : Latlong\n"
835 "// purpose : Converts world direction to environment texture coordinates\n"
836 "// =======================================================================\n"
837 "vec2 Latlong (in vec3 thePoint, in float theRadius)\n"
839 " float aPsi = acos (-thePoint.z / theRadius);\n"
841 " float aPhi = atan (thePoint.y, thePoint.x) + PI;\n"
843 " return vec2 (aPhi * 0.1591549f,\n"
844 " aPsi * 0.3183098f);\n"
847 "// =======================================================================\n"
848 "// function : SmoothNormal\n"
849 "// purpose : Interpolates normal across the triangle\n"
850 "// =======================================================================\n"
851 "vec3 SmoothNormal (in vec2 theUV, in ivec4 theTriangle)\n"
853 " vec3 aNormal0 = texelFetch (uGeometryNormalTexture, theTriangle.x).xyz;\n"
854 " vec3 aNormal1 = texelFetch (uGeometryNormalTexture, theTriangle.y).xyz;\n"
855 " vec3 aNormal2 = texelFetch (uGeometryNormalTexture, theTriangle.z).xyz;\n"
857 " return normalize (aNormal1 * theUV.x +\n"
858 " aNormal2 * theUV.y +\n"
859 " aNormal0 * (1.0f - theUV.x - theUV.y));\n"
862 "#define POLYGON_OFFSET_UNIT 0.f\n"
863 "#define POLYGON_OFFSET_FACTOR 1.f\n"
864 "#define POLYGON_OFFSET_SCALE 0.006f\n"
866 "// =======================================================================\n"
867 "// function : PolygonOffset\n"
868 "// purpose : Computes OpenGL polygon offset\n"
869 "// =======================================================================\n"
870 "float PolygonOffset (in vec3 theNormal, in vec3 thePoint)\n"
872 " vec4 aProjectedNorm = vec4 (theNormal, -dot (theNormal, thePoint)) * uUnviewMat;\n"
874 " float aPolygonOffset = POLYGON_OFFSET_UNIT;\n"
876 " if (aProjectedNorm.z * aProjectedNorm.z > 1e-20f)\n"
878 " aProjectedNorm.xy *= 1.f / aProjectedNorm.z;\n"
880 " aPolygonOffset += POLYGON_OFFSET_FACTOR * max (abs (aProjectedNorm.x),\n"
881 " abs (aProjectedNorm.y));\n"
884 " return aPolygonOffset;\n"
887 "// =======================================================================\n"
888 "// function : SmoothUV\n"
889 "// purpose : Interpolates UV coordinates across the triangle\n"
890 "// =======================================================================\n"
891 "#ifdef USE_TEXTURES\n"
892 "vec2 SmoothUV (in vec2 theUV, in ivec4 theTriangle)\n"
894 " vec2 aTexCrd0 = texelFetch (uGeometryTexCrdTexture, theTriangle.x).st;\n"
895 " vec2 aTexCrd1 = texelFetch (uGeometryTexCrdTexture, theTriangle.y).st;\n"
896 " vec2 aTexCrd2 = texelFetch (uGeometryTexCrdTexture, theTriangle.z).st;\n"
898 " return aTexCrd1 * theUV.x +\n"
899 " aTexCrd2 * theUV.y +\n"
900 " aTexCrd0 * (1.0f - theUV.x - theUV.y);\n"
904 "// =======================================================================\n"
905 "// function : FetchEnvironment\n"
907 "// =======================================================================\n"
908 "vec4 FetchEnvironment (in vec2 theTexCoord)\n"
910 " return uSphereMapEnabled == 0 ?\n"
911 " vec4 (0.f, 0.f, 0.f, 1.f) : textureLod (uEnvironmentMapTexture, theTexCoord, 0.f);\n"
914 "// =======================================================================\n"
915 "// function : Refract\n"
916 "// purpose : Computes refraction ray (also handles TIR)\n"
917 "// =======================================================================\n"
918 "#ifndef PATH_TRACING\n"
919 "vec3 Refract (in vec3 theInput,\n"
920 " in vec3 theNormal,\n"
921 " in float theRefractIndex,\n"
922 " in float theInvRefractIndex)\n"
924 " float aNdotI = dot (theInput, theNormal);\n"
926 " float anIndex = aNdotI < 0.0f\n"
927 " ? theInvRefractIndex\n"
928 " : theRefractIndex;\n"
930 " float aSquare = anIndex * anIndex * (1.0f - aNdotI * aNdotI);\n"
932 " if (aSquare > 1.0f)\n"
934 " return reflect (theInput, theNormal);\n"
937 " float aNdotT = sqrt (1.0f - aSquare);\n"
939 " return normalize (anIndex * theInput -\n"
940 " (anIndex * aNdotI + (aNdotI < 0.0f ? aNdotT : -aNdotT)) * theNormal);\n"
944 "#define MIN_SLOPE 0.0001f\n"
945 "#define EPS_SCALE 8.0000f\n"
947 "#define THRESHOLD vec3 (0.1f)\n"
949 "#define INVALID_BOUNCES 1000\n"
951 "#define LIGHT_POS(index) (2 * index + 1)\n"
952 "#define LIGHT_PWR(index) (2 * index + 0)\n"
954 "// =======================================================================\n"
955 "// function : Radiance\n"
956 "// purpose : Computes color along the given ray\n"
957 "// =======================================================================\n"
958 "#ifndef PATH_TRACING\n"
959 "vec4 Radiance (in SRay theRay, in vec3 theInverse)\n"
961 " vec3 aResult = vec3 (0.0f);\n"
962 " vec4 aWeight = vec4 (1.0f);\n"
966 " float aRaytraceDepth = MAXFLOAT;\n"
968 " for (int aDepth = 0; aDepth < NB_BOUNCES; ++aDepth)\n"
970 " SIntersect aHit = SIntersect (MAXFLOAT, vec2 (ZERO), ZERO);\n"
972 " ivec4 aTriIndex = SceneNearestHit (theRay, theInverse, aHit, aTrsfId);\n"
974 " if (aTriIndex.x == -1)\n"
976 " vec4 aColor = vec4 (0.0);\n"
978 " if (bool(uSphereMapForBack) || aWeight.w == 0.0f /* reflection */)\n"
980 " float aTime = IntersectSphere (theRay, uSceneRadius);\n"
982 " aColor = FetchEnvironment (Latlong (\n"
983 " theRay.Direct * aTime + theRay.Origin, uSceneRadius));\n"
987 " aColor = BackgroundColor();\n"
990 " aResult += aWeight.xyz * aColor.xyz; aWeight.w *= aColor.w;\n"
992 " break; // terminate path\n"
995 " vec3 aInvTransf0 = texelFetch (uSceneTransformTexture, aTrsfId + 0).xyz;\n"
996 " vec3 aInvTransf1 = texelFetch (uSceneTransformTexture, aTrsfId + 1).xyz;\n"
997 " vec3 aInvTransf2 = texelFetch (uSceneTransformTexture, aTrsfId + 2).xyz;\n"
999 " aHit.Normal = normalize (vec3 (dot (aInvTransf0, aHit.Normal),\n"
1000 " dot (aInvTransf1, aHit.Normal),\n"
1001 " dot (aInvTransf2, aHit.Normal)));\n"
1003 " theRay.Origin += theRay.Direct * aHit.Time; // intersection point\n"
1005 " // Evaluate depth on first hit\n"
1006 " if (aDepth == 0)\n"
1008 " vec4 aNDCPoint = uViewMat * vec4 (theRay.Origin, 1.f);\n"
1010 " float aPolygonOffset = PolygonOffset (aHit.Normal, theRay.Origin);\n"
1011 " aRaytraceDepth = (aNDCPoint.z / aNDCPoint.w + aPolygonOffset * POLYGON_OFFSET_SCALE) * 0.5f + 0.5f;\n"
1014 " vec3 aNormal = SmoothNormal (aHit.UV, aTriIndex);\n"
1016 " aNormal = normalize (vec3 (dot (aInvTransf0, aNormal),\n"
1017 " dot (aInvTransf1, aNormal),\n"
1018 " dot (aInvTransf2, aNormal)));\n"
1020 " vec3 aAmbient = texelFetch (\n"
1021 " uRaytraceMaterialTexture, MATERIAL_AMBN (aTriIndex.w)).rgb;\n"
1022 " vec4 aDiffuse = texelFetch (\n"
1023 " uRaytraceMaterialTexture, MATERIAL_DIFF (aTriIndex.w));\n"
1024 " vec4 aSpecular = texelFetch (\n"
1025 " uRaytraceMaterialTexture, MATERIAL_SPEC (aTriIndex.w));\n"
1026 " vec4 aOpacity = texelFetch (\n"
1027 " uRaytraceMaterialTexture, MATERIAL_TRAN (aTriIndex.w));\n"
1029 "#ifdef USE_TEXTURES\n"
1030 " if (aDiffuse.w >= 0.f)\n"
1032 " vec4 aTexCoord = vec4 (SmoothUV (aHit.UV, aTriIndex), 0.f, 1.f);\n"
1034 " vec4 aTrsfRow1 = texelFetch (\n"
1035 " uRaytraceMaterialTexture, MATERIAL_TRS1 (aTriIndex.w));\n"
1036 " vec4 aTrsfRow2 = texelFetch (\n"
1037 " uRaytraceMaterialTexture, MATERIAL_TRS2 (aTriIndex.w));\n"
1039 " aTexCoord.st = vec2 (dot (aTrsfRow1, aTexCoord),\n"
1040 " dot (aTrsfRow2, aTexCoord));\n"
1042 " vec4 aTexColor = textureLod (\n"
1043 " sampler2D (uTextureSamplers[int(aDiffuse.w)]), aTexCoord.st, 0.f);\n"
1045 " aDiffuse.rgb *= aTexColor.rgb;\n"
1046 " aAmbient.rgb *= aTexColor.rgb;\n"
1048 " // keep refractive index untouched (Z component)\n"
1049 " aOpacity.xy = vec2 (aTexColor.w * aOpacity.x, 1.0f - aTexColor.w * aOpacity.x);\n"
1053 " vec3 aEmission = texelFetch (\n"
1054 " uRaytraceMaterialTexture, MATERIAL_EMIS (aTriIndex.w)).rgb;\n"
1056 " float aGeomFactor = dot (aNormal, theRay.Direct);\n"
1058 " aResult.xyz += aWeight.xyz * aOpacity.x * (\n"
1059 " uGlobalAmbient.xyz * aAmbient * max (abs (aGeomFactor), 0.5f) + aEmission);\n"
1061 " vec3 aSidedNormal = mix (aNormal, -aNormal, step (0.0f, aGeomFactor));\n"
1063 " for (int aLightIdx = 0; aLightIdx < uLightCount; ++aLightIdx)\n"
1065 " vec4 aLight = texelFetch (\n"
1066 " uRaytraceLightSrcTexture, LIGHT_POS (aLightIdx));\n"
1068 " float aDistance = MAXFLOAT;\n"
1070 " if (aLight.w != 0.0f) // point light source\n"
1072 " aDistance = length (aLight.xyz -= theRay.Origin);\n"
1074 " aLight.xyz *= 1.0f / aDistance;\n"
1077 " float aLdotN = dot (aLight.xyz, aSidedNormal);\n"
1079 " if (aLdotN > 0.0f) // first check if light source is important\n"
1081 " float aVisibility = 1.0f;\n"
1083 " if (bool(uShadowsEnabled))\n"
1085 " SRay aShadow = SRay (theRay.Origin, aLight.xyz);\n"
1087 " aShadow.Origin += uSceneEpsilon * (aLight.xyz +\n"
1088 " mix (-aHit.Normal, aHit.Normal, step (0.0f, dot (aHit.Normal, aLight.xyz))));\n"
1090 " vec3 aInverse = 1.0f / max (abs (aLight.xyz), SMALL);\n"
1092 " aVisibility = SceneAnyHit (\n"
1093 " aShadow, mix (-aInverse, aInverse, step (ZERO, aLight.xyz)), aDistance);\n"
1096 " if (aVisibility > 0.0f)\n"
1098 " vec3 aIntensity = min (UNIT, vec3 (texelFetch (\n"
1099 " uRaytraceLightSrcTexture, LIGHT_PWR (aLightIdx))));\n"
1101 " float aRdotV = dot (reflect (aLight.xyz, aSidedNormal), theRay.Direct);\n"
1103 " aResult.xyz += aWeight.xyz * (aOpacity.x * aVisibility) * aIntensity *\n"
1104 " (aDiffuse.xyz * aLdotN + aSpecular.xyz * pow (max (0.f, aRdotV), aSpecular.w));\n"
1109 " if (aOpacity.x != 1.0f)\n"
1111 " aWeight *= aOpacity.y;\n"
1113 " if (aOpacity.z != 1.0f)\n"
1115 " theRay.Direct = Refract (theRay.Direct, aNormal, aOpacity.z, aOpacity.w);\n"
1120 " aWeight *= bool(uReflectEnabled) ?\n"
1121 " texelFetch (uRaytraceMaterialTexture, MATERIAL_REFL (aTriIndex.w)) : vec4 (0.0f);\n"
1123 " vec3 aReflect = reflect (theRay.Direct, aNormal);\n"
1125 " if (dot (aReflect, aHit.Normal) * dot (theRay.Direct, aHit.Normal) > 0.0f)\n"
1127 " aReflect = reflect (theRay.Direct, aHit.Normal);\n"
1130 " theRay.Direct = aReflect;\n"
1133 " if (all (lessThanEqual (aWeight.xyz, THRESHOLD)))\n"
1135 " aDepth = INVALID_BOUNCES;\n"
1137 " else if (aOpacity.x == 1.0f || aOpacity.z != 1.0f) // if no simple transparency\n"
1139 " theRay.Origin += aHit.Normal * mix (\n"
1140 " -uSceneEpsilon, uSceneEpsilon, step (0.0f, dot (aHit.Normal, theRay.Direct)));\n"
1142 " theInverse = 1.0f / max (abs (theRay.Direct), SMALL);\n"
1144 " theInverse = mix (-theInverse, theInverse, step (ZERO, theRay.Direct));\n"
1147 " theRay.Origin += theRay.Direct * uSceneEpsilon;\n"
1150 " gl_FragDepth = aRaytraceDepth;\n"
1152 " return vec4 (aResult.x,\n"