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"
7 "#ifdef ADAPTIVE_SAMPLING_ATOMIC\n"
8 " #extension GL_NV_shader_atomic_float : require\n"
11 "#ifdef USE_TEXTURES\n"
12 " #extension GL_ARB_bindless_texture : require\n"
15 "//! Normalized pixel coordinates.\n"
18 "//! Sub-pixel offset in X direction for FSAA.\n"
19 "uniform float uOffsetX = 0.f;\n"
20 "//! Sub-pixel offset in Y direction for FSAA.\n"
21 "uniform float uOffsetY = 0.f;\n"
23 "//! Origin of viewing ray in left-top corner.\n"
24 "uniform vec3 uOriginLT;\n"
25 "//! Origin of viewing ray in left-bottom corner.\n"
26 "uniform vec3 uOriginLB;\n"
27 "//! Origin of viewing ray in right-top corner.\n"
28 "uniform vec3 uOriginRT;\n"
29 "//! Origin of viewing ray in right-bottom corner.\n"
30 "uniform vec3 uOriginRB;\n"
32 "//! Width of the rendering window.\n"
33 "uniform int uWinSizeX;\n"
34 "//! Height of the rendering window.\n"
35 "uniform int uWinSizeY;\n"
37 "//! Direction of viewing ray in left-top corner.\n"
38 "uniform vec3 uDirectLT;\n"
39 "//! Direction of viewing ray in left-bottom corner.\n"
40 "uniform vec3 uDirectLB;\n"
41 "//! Direction of viewing ray in right-top corner.\n"
42 "uniform vec3 uDirectRT;\n"
43 "//! Direction of viewing ray in right-bottom corner.\n"
44 "uniform vec3 uDirectRB;\n"
46 "//! Inverse model-view-projection matrix.\n"
47 "uniform mat4 uUnviewMat;\n"
49 "//! Model-view-projection matrix.\n"
50 "uniform mat4 uViewMat;\n"
52 "//! Texture buffer of data records of bottom-level BVH nodes.\n"
53 "uniform isamplerBuffer uSceneNodeInfoTexture;\n"
54 "//! Texture buffer of minimum points of bottom-level BVH nodes.\n"
55 "uniform samplerBuffer uSceneMinPointTexture;\n"
56 "//! Texture buffer of maximum points of bottom-level BVH nodes.\n"
57 "uniform samplerBuffer uSceneMaxPointTexture;\n"
58 "//! Texture buffer of transformations of high-level BVH nodes.\n"
59 "uniform samplerBuffer uSceneTransformTexture;\n"
61 "//! Texture buffer of vertex coords.\n"
62 "uniform samplerBuffer uGeometryVertexTexture;\n"
63 "//! Texture buffer of vertex normals.\n"
64 "uniform samplerBuffer uGeometryNormalTexture;\n"
65 "#ifdef USE_TEXTURES\n"
66 " //! Texture buffer of per-vertex UV-coordinates.\n"
67 " uniform samplerBuffer uGeometryTexCrdTexture;\n"
69 "//! Texture buffer of triangle indices.\n"
70 "uniform isamplerBuffer uGeometryTriangTexture;\n"
72 "//! Texture buffer of material properties.\n"
73 "uniform samplerBuffer uRaytraceMaterialTexture;\n"
74 "//! Texture buffer of light source properties.\n"
75 "uniform samplerBuffer uRaytraceLightSrcTexture;\n"
76 "//! Environment map texture.\n"
77 "uniform sampler2D uEnvironmentMapTexture;\n"
79 "//! Total number of light sources.\n"
80 "uniform int uLightCount;\n"
81 "//! Intensity of global ambient light.\n"
82 "uniform vec4 uGlobalAmbient;\n"
84 "//! Enables/disables hard shadows.\n"
85 "uniform int uShadowsEnabled;\n"
86 "//! Enables/disables specular reflections.\n"
87 "uniform int uReflectEnabled;\n"
88 "//! Enables/disables spherical environment map.\n"
89 "uniform int uSphereMapEnabled;\n"
90 "//! Enables/disables environment map background.\n"
91 "uniform int uSphereMapForBack;\n"
93 "//! Radius of bounding sphere of the scene.\n"
94 "uniform float uSceneRadius;\n"
95 "//! Scene epsilon to prevent self-intersections.\n"
96 "uniform float uSceneEpsilon;\n"
98 "#ifdef USE_TEXTURES\n"
99 " //! Unique 64-bit handles of OpenGL textures.\n"
100 " uniform uvec2 uTextureSamplers[MAX_TEX_NUMBER];\n"
103 "#ifdef ADAPTIVE_SAMPLING\n"
104 " //! OpenGL image used for accumulating rendering result.\n"
105 " volatile restrict layout(r32f) uniform image2D uRenderImage;\n"
107 "#ifdef ADAPTIVE_SAMPLING_ATOMIC\n"
108 " //! OpenGL image storing offsets of sampled pixels blocks.\n"
109 " coherent restrict layout(rg32i) uniform iimage2D uOffsetImage;\n"
111 " //! OpenGL image defining per-tile amount of samples.\n"
112 " volatile restrict layout(r32i) uniform iimage2D uTilesImage;\n"
115 " //! Screen space tile size.\n"
116 " uniform ivec2 uTileSize;\n"
119 "//! Top color of gradient background.\n"
120 "uniform vec4 uBackColorTop = vec4 (0.0);\n"
121 "//! Bottom color of gradient background.\n"
122 "uniform vec4 uBackColorBot = vec4 (0.0);\n"
124 "//! Aperture radius of camera used for depth-of-field\n"
125 "uniform float uApertureRadius = 0.f;\n"
127 "//! Focal distance of camera used for depth-of field\n"
128 "uniform float uFocalPlaneDist = 10.f;\n"
130 "//! Camera position used for projective mode\n"
131 "uniform vec3 uEyeOrig;\n"
133 "//! Camera view direction used for projective mode\n"
134 "uniform vec3 uEyeView;\n"
136 "//! Camera's screen vertical direction used for projective mode\n"
137 "uniform vec3 uEyeVert;\n"
139 "//! Camera's screen horizontal direction used for projective mode\n"
140 "uniform vec3 uEyeSide;\n"
142 "//! Camera's screen size used for projective mode\n"
143 "uniform vec2 uEyeSize;\n"
145 "/////////////////////////////////////////////////////////////////////////////////////////\n"
146 "// Specific data types\n"
148 "//! Stores ray parameters.\n"
156 "//! Stores intersection parameters.\n"
157 "struct SIntersect\n"
166 "/////////////////////////////////////////////////////////////////////////////////////////\n"
167 "// Some useful constants\n"
169 "#define MAXFLOAT 1e15f\n"
171 "#define SMALL vec3 (exp2 (-80.0f))\n"
173 "#define ZERO vec3 (0.0f, 0.0f, 0.0f)\n"
174 "#define UNIT vec3 (1.0f, 1.0f, 1.0f)\n"
176 "#define AXIS_X vec3 (1.0f, 0.0f, 0.0f)\n"
177 "#define AXIS_Y vec3 (0.0f, 1.0f, 0.0f)\n"
178 "#define AXIS_Z vec3 (0.0f, 0.0f, 1.0f)\n"
180 "#define M_PI 3.141592653f\n"
181 "#define M_2_PI 6.283185307f\n"
182 "#define M_PI_2 1.570796327f\n"
184 "#define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)\n"
186 "// =======================================================================\n"
187 "// function : MatrixRowMultiplyDir\n"
188 "// purpose : Multiplies a vector by matrix\n"
189 "// =======================================================================\n"
190 "vec3 MatrixRowMultiplyDir (in vec3 v,\n"
195 " return vec3 (dot (m0.xyz, v),\n"
196 " dot (m1.xyz, v),\n"
197 " dot (m2.xyz, v));\n"
200 "//! 32-bit state of random number generator.\n"
203 "// =======================================================================\n"
204 "// function : SeedRand\n"
205 "// purpose : Applies hash function by Thomas Wang to randomize seeds\n"
206 "// (see http://www.burtleburtle.net/bob/hash/integer.html)\n"
207 "// =======================================================================\n"
208 "void SeedRand (in int theSeed, in int theSizeX, in int theRadius)\n"
210 " RandState = uint (int (gl_FragCoord.y) / theRadius * theSizeX + int (gl_FragCoord.x) / theRadius + theSeed);\n"
212 " RandState = (RandState + 0x479ab41du) + (RandState << 8);\n"
213 " RandState = (RandState ^ 0xe4aa10ceu) ^ (RandState >> 5);\n"
214 " RandState = (RandState + 0x9942f0a6u) - (RandState << 14);\n"
215 " RandState = (RandState ^ 0x5aedd67du) ^ (RandState >> 3);\n"
216 " RandState = (RandState + 0x17bea992u) + (RandState << 7);\n"
219 "// =======================================================================\n"
220 "// function : RandInt\n"
221 "// purpose : Generates integer using Xorshift algorithm by G. Marsaglia\n"
222 "// =======================================================================\n"
225 " RandState ^= (RandState << 13);\n"
226 " RandState ^= (RandState >> 17);\n"
227 " RandState ^= (RandState << 5);\n"
229 " return RandState;\n"
232 "// =======================================================================\n"
233 "// function : RandFloat\n"
234 "// purpose : Generates a random float in 0 <= x < 1 range\n"
235 "// =======================================================================\n"
236 "float RandFloat()\n"
238 " return float (RandInt()) * (1.f / 4294967296.f);\n"
241 "// =======================================================================\n"
242 "// function : MatrixColMultiplyPnt\n"
243 "// purpose : Multiplies a vector by matrix\n"
244 "// =======================================================================\n"
245 "vec3 MatrixColMultiplyPnt (in vec3 v,\n"
251 " return vec3 (m0.x * v.x + m1.x * v.y + m2.x * v.z + m3.x,\n"
252 " m0.y * v.x + m1.y * v.y + m2.y * v.z + m3.y,\n"
253 " m0.z * v.x + m1.z * v.y + m2.z * v.z + m3.z);\n"
256 "// =======================================================================\n"
257 "// function : MatrixColMultiplyDir\n"
258 "// purpose : Multiplies a vector by matrix\n"
259 "// =======================================================================\n"
260 "vec3 MatrixColMultiplyDir (in vec3 v,\n"
265 " return vec3 (m0.x * v.x + m1.x * v.y + m2.x * v.z,\n"
266 " m0.y * v.x + m1.y * v.y + m2.y * v.z,\n"
267 " m0.z * v.x + m1.z * v.y + m2.z * v.z);\n"
270 "//=======================================================================\n"
271 "// function : InverseDirection\n"
272 "// purpose : Returns safely inverted direction of the given one\n"
273 "//=======================================================================\n"
274 "vec3 InverseDirection (in vec3 theInput)\n"
276 " vec3 anInverse = 1.f / max (abs (theInput), SMALL);\n"
278 " return mix (-anInverse, anInverse, step (ZERO, theInput));\n"
281 "//=======================================================================\n"
282 "// function : BackgroundColor\n"
283 "// purpose : Returns color of gradient background\n"
284 "//=======================================================================\n"
285 "vec4 BackgroundColor()\n"
287 "#ifdef ADAPTIVE_SAMPLING_ATOMIC\n"
289 " ivec2 aFragCoord = ivec2 (gl_FragCoord.xy);\n"
291 " ivec2 aTileXY = imageLoad (uOffsetImage, aFragCoord / uTileSize).xy * uTileSize;\n"
293 " aTileXY.y += aFragCoord.y % min (uWinSizeY - aTileXY.y, uTileSize.y);\n"
295 " return mix (uBackColorBot, uBackColorTop, float (aTileXY.y) / uWinSizeY);\n"
299 " return mix (uBackColorBot, uBackColorTop, vPixel.y);\n"
304 "/////////////////////////////////////////////////////////////////////////////////////////\n"
305 "// Functions for compute ray-object intersection\n"
307 "//=======================================================================\n"
308 "// function : sampleUniformDisk\n"
310 "//=======================================================================\n"
311 "vec2 sampleUniformDisk ()\n"
315 " float aKsi1 = 2.f * RandFloat () - 1.f;\n"
316 " float aKsi2 = 2.f * RandFloat () - 1.f;\n"
318 " if (aKsi1 > -aKsi2)\n"
320 " if (aKsi1 > aKsi2)\n"
321 " aPoint = vec2 (aKsi1, (M_PI / 4.f) * (0.f + aKsi2 / aKsi1));\n"
323 " aPoint = vec2 (aKsi2, (M_PI / 4.f) * (2.f - aKsi1 / aKsi2));\n"
327 " if (aKsi1 < aKsi2)\n"
328 " aPoint = vec2 (-aKsi1, (M_PI / 4.f) * (4.f + aKsi2 / aKsi1));\n"
330 " aPoint = vec2 (-aKsi2, (M_PI / 4.f) * (6.f - aKsi1 / aKsi2));\n"
333 " return vec2 (sin (aPoint.y), cos (aPoint.y)) * aPoint.x;\n"
336 "// =======================================================================\n"
337 "// function : GenerateRay\n"
339 "// =======================================================================\n"
340 "SRay GenerateRay (in vec2 thePixel)\n"
342 "#ifndef DEPTH_OF_FIELD\n"
344 " vec3 aP0 = mix (uOriginLB, uOriginRB, thePixel.x);\n"
345 " vec3 aP1 = mix (uOriginLT, uOriginRT, thePixel.x);\n"
347 " vec3 aD0 = mix (uDirectLB, uDirectRB, thePixel.x);\n"
348 " vec3 aD1 = mix (uDirectLT, uDirectRT, thePixel.x);\n"
350 " vec3 aDirection = normalize (mix (aD0, aD1, thePixel.y));\n"
352 " return SRay (mix (aP0, aP1, thePixel.y), aDirection);\n"
356 " vec2 aPixel = uEyeSize * (thePixel - vec2 (0.5f)) * 2.f;\n"
358 " vec2 aAperturePnt = sampleUniformDisk () * uApertureRadius;\n"
360 " vec3 aLocalDir = normalize (vec3 (\n"
361 " aPixel * uFocalPlaneDist - aAperturePnt, uFocalPlaneDist));\n"
363 " vec3 aOrigin = uEyeOrig +\n"
364 " uEyeSide * aAperturePnt.x +\n"
365 " uEyeVert * aAperturePnt.y;\n"
367 " vec3 aDirect = uEyeView * aLocalDir.z +\n"
368 " uEyeSide * aLocalDir.x +\n"
369 " uEyeVert * aLocalDir.y;\n"
371 " return SRay (aOrigin, aDirect);\n"
376 "// =======================================================================\n"
377 "// function : IntersectSphere\n"
378 "// purpose : Computes ray-sphere intersection\n"
379 "// =======================================================================\n"
380 "float IntersectSphere (in SRay theRay, in float theRadius)\n"
382 " float aDdotD = dot (theRay.Direct, theRay.Direct);\n"
383 " float aDdotO = dot (theRay.Direct, theRay.Origin);\n"
384 " float aOdotO = dot (theRay.Origin, theRay.Origin);\n"
386 " float aD = aDdotO * aDdotO - aDdotD * (aOdotO - theRadius * theRadius);\n"
390 " float aTime = (sqrt (aD) - aDdotO) * (1.0f / aDdotD);\n"
392 " return aTime > 0.0f ? aTime : MAXFLOAT;\n"
395 " return MAXFLOAT;\n"
398 "// =======================================================================\n"
399 "// function : IntersectTriangle\n"
400 "// purpose : Computes ray-triangle intersection (branchless version)\n"
401 "// =======================================================================\n"
402 "void IntersectTriangle (in SRay theRay,\n"
403 " in vec3 thePnt0,\n"
404 " in vec3 thePnt1,\n"
405 " in vec3 thePnt2,\n"
406 " out vec3 theUVT,\n"
407 " out vec3 theNorm)\n"
409 " vec3 aToTrg = thePnt0 - theRay.Origin;\n"
411 " vec3 aEdge0 = thePnt1 - thePnt0;\n"
412 " vec3 aEdge1 = thePnt0 - thePnt2;\n"
414 " theNorm = cross (aEdge1, aEdge0);\n"
416 " vec3 theVect = cross (theRay.Direct, aToTrg);\n"
418 " theUVT = vec3 (dot (theNorm, aToTrg),\n"
419 " dot (theVect, aEdge1),\n"
420 " dot (theVect, aEdge0)) * (1.f / dot (theNorm, theRay.Direct));\n"
422 " theUVT.x = any (lessThan (theUVT, ZERO)) || (theUVT.y + theUVT.z) > 1.f ? MAXFLOAT : theUVT.x;\n"
425 "#define EMPTY_ROOT ivec4(0)\n"
427 "//! Utility structure containing information about\n"
428 "//! currently traversing sub-tree of scene's BVH.\n"
431 " //! Transformed ray.\n"
434 " //! Inversed ray direction.\n"
437 " //! Parameters of sub-root node.\n"
441 "#define MATERIAL_AMBN(index) (19 * index + 0)\n"
442 "#define MATERIAL_DIFF(index) (19 * index + 1)\n"
443 "#define MATERIAL_SPEC(index) (19 * index + 2)\n"
444 "#define MATERIAL_EMIS(index) (19 * index + 3)\n"
445 "#define MATERIAL_REFL(index) (19 * index + 4)\n"
446 "#define MATERIAL_REFR(index) (19 * index + 5)\n"
447 "#define MATERIAL_TRAN(index) (19 * index + 6)\n"
448 "#define MATERIAL_TRS1(index) (19 * index + 7)\n"
449 "#define MATERIAL_TRS2(index) (19 * index + 8)\n"
450 "#define MATERIAL_TRS3(index) (19 * index + 9)\n"
452 "#define TRS_OFFSET(treelet) treelet.SubData.x\n"
453 "#define BVH_OFFSET(treelet) treelet.SubData.y\n"
454 "#define VRT_OFFSET(treelet) treelet.SubData.z\n"
455 "#define TRG_OFFSET(treelet) treelet.SubData.w\n"
457 "//! Identifies the absence of intersection.\n"
458 "#define INALID_HIT ivec4 (-1)\n"
460 "//! Global stack shared between traversal functions.\n"
461 "int Stack[STACK_SIZE];\n"
463 "// =======================================================================\n"
464 "// function : pop\n"
466 "// =======================================================================\n"
467 "int pop (inout int theHead)\n"
469 " int aData = Stack[theHead];\n"
471 " int aMask = aData >> 26;\n"
472 " int aNode = aMask & 0x3;\n"
476 " if ((aMask & 0x3) == aNode)\n"
482 " aMask |= (aMask << 2) & 0x30;\n"
484 " Stack[theHead] = (aData & 0x03FFFFFF) | (aMask << 26);\n"
487 " return (aData & 0x03FFFFFF) + aNode;\n"
490 "// =======================================================================\n"
491 "// function : SceneNearestHit\n"
492 "// purpose : Finds intersection with nearest scene triangle\n"
493 "// =======================================================================\n"
494 "ivec4 SceneNearestHit (in SRay theRay, in vec3 theInverse, inout SIntersect theHit, out int theTrsfId)\n"
496 " ivec4 aTriIndex = INALID_HIT;\n"
498 " int aNode = 0; // node to traverse\n"
499 " int aHead = -1; // pointer of stack\n"
500 " int aStop = -1; // BVH level switch\n"
502 " SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
504 " for (bool toContinue = true; toContinue; /* none */)\n"
506 " ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);\n"
508 " if (aData.x == 0) // if inner node\n"
510 " aData.y += BVH_OFFSET (aSubTree);\n"
512 " vec4 aHitTimes = vec4 (MAXFLOAT,\n"
517 " vec3 aRayOriginInverse = -aSubTree.TrsfRay.Origin * aSubTree.Inverse;\n"
519 " vec3 aNodeMin0 = texelFetch (uSceneMinPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
520 " vec3 aNodeMin1 = texelFetch (uSceneMinPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
521 " vec3 aNodeMin2 = texelFetch (uSceneMinPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
522 " vec3 aNodeMin3 = texelFetch (uSceneMinPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
523 " vec3 aNodeMax0 = texelFetch (uSceneMaxPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
524 " vec3 aNodeMax1 = texelFetch (uSceneMaxPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
525 " vec3 aNodeMax2 = texelFetch (uSceneMaxPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
526 " vec3 aNodeMax3 = texelFetch (uSceneMaxPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
528 " vec3 aTimeMax = max (aNodeMin0, aNodeMax0);\n"
529 " vec3 aTimeMin = min (aNodeMin0, aNodeMax0);\n"
531 " float aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
532 " float aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
534 " aHitTimes.x = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;\n"
536 " aTimeMax = max (aNodeMin1, aNodeMax1);\n"
537 " aTimeMin = min (aNodeMin1, aNodeMax1);\n"
539 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
540 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
542 " aHitTimes.y = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;\n"
544 " aTimeMax = max (aNodeMin2, aNodeMax2);\n"
545 " aTimeMin = min (aNodeMin2, aNodeMax2);\n"
547 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
548 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
550 " aHitTimes.z = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f && aData.z > 1) ? aTimeEnter : MAXFLOAT;\n"
552 " aTimeMax = max (aNodeMin3, aNodeMax3);\n"
553 " aTimeMin = min (aNodeMin3, aNodeMax3);\n"
555 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
556 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
558 " aHitTimes.w = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f && aData.z > 2) ? aTimeEnter : MAXFLOAT;\n"
560 " ivec4 aChildren = ivec4 (0, 1, 2, 3);\n"
562 " aChildren.xy = aHitTimes.y < aHitTimes.x ? aChildren.yx : aChildren.xy;\n"
563 " aHitTimes.xy = aHitTimes.y < aHitTimes.x ? aHitTimes.yx : aHitTimes.xy;\n"
564 " aChildren.zw = aHitTimes.w < aHitTimes.z ? aChildren.wz : aChildren.zw;\n"
565 " aHitTimes.zw = aHitTimes.w < aHitTimes.z ? aHitTimes.wz : aHitTimes.zw;\n"
566 " aChildren.xz = aHitTimes.z < aHitTimes.x ? aChildren.zx : aChildren.xz;\n"
567 " aHitTimes.xz = aHitTimes.z < aHitTimes.x ? aHitTimes.zx : aHitTimes.xz;\n"
568 " aChildren.yw = aHitTimes.w < aHitTimes.y ? aChildren.wy : aChildren.yw;\n"
569 " aHitTimes.yw = aHitTimes.w < aHitTimes.y ? aHitTimes.wy : aHitTimes.yw;\n"
570 " aChildren.yz = aHitTimes.z < aHitTimes.y ? aChildren.zy : aChildren.yz;\n"
571 " aHitTimes.yz = aHitTimes.z < aHitTimes.y ? aHitTimes.zy : aHitTimes.yz;\n"
573 " if (aHitTimes.x != MAXFLOAT)\n"
575 " int aHitMask = (aHitTimes.w != MAXFLOAT ? aChildren.w : aChildren.z) << 2\n"
576 " | (aHitTimes.z != MAXFLOAT ? aChildren.z : aChildren.y);\n"
578 " if (aHitTimes.y != MAXFLOAT)\n"
579 " Stack[++aHead] = aData.y | (aHitMask << 2 | aChildren.y) << 26;\n"
581 " aNode = aData.y + aChildren.x;\n"
585 " toContinue = (aHead >= 0);\n"
587 " if (aHead == aStop) // go to top-level BVH\n"
589 " aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
593 " aNode = pop (aHead);\n"
596 " else if (aData.x < 0) // leaf node (contains triangles)\n"
601 " for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)\n"
603 " ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + TRG_OFFSET (aSubTree));\n"
605 " vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x += VRT_OFFSET (aSubTree)).xyz;\n"
606 " vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y += VRT_OFFSET (aSubTree)).xyz;\n"
607 " vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z += VRT_OFFSET (aSubTree)).xyz;\n"
609 " IntersectTriangle (aSubTree.TrsfRay, aPoint0, aPoint1, aPoint2, aTimeUV, aNormal);\n"
611 " if (aTimeUV.x < theHit.Time)\n"
613 " aTriIndex = aTriangle;\n"
615 " theTrsfId = TRS_OFFSET (aSubTree);\n"
617 " theHit = SIntersect (aTimeUV.x, aTimeUV.yz, aNormal);\n"
621 " toContinue = (aHead >= 0);\n"
623 " if (aHead == aStop) // go to top-level BVH\n"
625 " aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
629 " aNode = pop (aHead);\n"
631 " else if (aData.x > 0) // switch node\n"
633 " aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root\n"
635 " vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);\n"
636 " vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);\n"
637 " vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);\n"
638 " vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);\n"
640 " aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,\n"
645 " aSubTree.Inverse = mix (-UNIT, UNIT, step (ZERO, aSubTree.TrsfRay.Direct)) /\n"
646 " max (abs (aSubTree.TrsfRay.Direct), SMALL);\n"
648 " aSubTree.TrsfRay.Origin = MatrixColMultiplyPnt (theRay.Origin,\n"
654 " aNode = BVH_OFFSET (aSubTree); // go to sub-root node\n"
656 " aStop = aHead; // store current stack pointer\n"
660 " return aTriIndex;\n"
663 "// =======================================================================\n"
664 "// function : SceneAnyHit\n"
665 "// purpose : Finds intersection with any scene triangle\n"
666 "// =======================================================================\n"
667 "float SceneAnyHit (in SRay theRay, in vec3 theInverse, in float theDistance)\n"
669 " float aFactor = 1.f;\n"
671 " int aNode = 0; // node to traverse\n"
672 " int aHead = -1; // pointer of stack\n"
673 " int aStop = -1; // BVH level switch\n"
675 " SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
677 " for (bool toContinue = true; toContinue; /* none */)\n"
679 " ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);\n"
681 " if (aData.x == 0) // if inner node\n"
683 " aData.y += BVH_OFFSET (aSubTree);\n"
685 " vec4 aHitTimes = vec4 (MAXFLOAT,\n"
690 " vec3 aRayOriginInverse = -aSubTree.TrsfRay.Origin * aSubTree.Inverse;\n"
692 " vec3 aNodeMin0 = texelFetch (uSceneMinPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
693 " vec3 aNodeMin1 = texelFetch (uSceneMinPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
694 " vec3 aNodeMin2 = texelFetch (uSceneMinPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
695 " vec3 aNodeMin3 = texelFetch (uSceneMinPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
696 " vec3 aNodeMax0 = texelFetch (uSceneMaxPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
697 " vec3 aNodeMax1 = texelFetch (uSceneMaxPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
698 " vec3 aNodeMax2 = texelFetch (uSceneMaxPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
699 " vec3 aNodeMax3 = texelFetch (uSceneMaxPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
701 " vec3 aTimeMax = max (aNodeMin0, aNodeMax0);\n"
702 " vec3 aTimeMin = min (aNodeMin0, aNodeMax0);\n"
704 " float aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
705 " float aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
707 " aHitTimes.x = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;\n"
709 " aTimeMax = max (aNodeMin1, aNodeMax1);\n"
710 " aTimeMin = min (aNodeMin1, aNodeMax1);\n"
712 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
713 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
715 " aHitTimes.y = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;\n"
717 " aTimeMax = max (aNodeMin2, aNodeMax2);\n"
718 " aTimeMin = min (aNodeMin2, aNodeMax2);\n"
720 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
721 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
723 " aHitTimes.z = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f && aData.z > 1) ? aTimeEnter : MAXFLOAT;\n"
725 " aTimeMax = max (aNodeMin3, aNodeMax3);\n"
726 " aTimeMin = min (aNodeMin3, aNodeMax3);\n"
728 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
729 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
731 " aHitTimes.w = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f && aData.z > 2) ? aTimeEnter : MAXFLOAT;\n"
733 " ivec4 aChildren = ivec4 (0, 1, 2, 3);\n"
735 " aChildren.xy = aHitTimes.y < aHitTimes.x ? aChildren.yx : aChildren.xy;\n"
736 " aHitTimes.xy = aHitTimes.y < aHitTimes.x ? aHitTimes.yx : aHitTimes.xy;\n"
737 " aChildren.zw = aHitTimes.w < aHitTimes.z ? aChildren.wz : aChildren.zw;\n"
738 " aHitTimes.zw = aHitTimes.w < aHitTimes.z ? aHitTimes.wz : aHitTimes.zw;\n"
739 " aChildren.xz = aHitTimes.z < aHitTimes.x ? aChildren.zx : aChildren.xz;\n"
740 " aHitTimes.xz = aHitTimes.z < aHitTimes.x ? aHitTimes.zx : aHitTimes.xz;\n"
741 " aChildren.yw = aHitTimes.w < aHitTimes.y ? aChildren.wy : aChildren.yw;\n"
742 " aHitTimes.yw = aHitTimes.w < aHitTimes.y ? aHitTimes.wy : aHitTimes.yw;\n"
743 " aChildren.yz = aHitTimes.z < aHitTimes.y ? aChildren.zy : aChildren.yz;\n"
744 " aHitTimes.yz = aHitTimes.z < aHitTimes.y ? aHitTimes.zy : aHitTimes.yz;\n"
746 " if (aHitTimes.x != MAXFLOAT)\n"
748 " int aHitMask = (aHitTimes.w != MAXFLOAT ? aChildren.w : aChildren.z) << 2\n"
749 " | (aHitTimes.z != MAXFLOAT ? aChildren.z : aChildren.y);\n"
751 " if (aHitTimes.y != MAXFLOAT)\n"
752 " Stack[++aHead] = aData.y | (aHitMask << 2 | aChildren.y) << 26;\n"
754 " aNode = aData.y + aChildren.x;\n"
758 " toContinue = (aHead >= 0);\n"
760 " if (aHead == aStop) // go to top-level BVH\n"
762 " aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
766 " aNode = pop (aHead);\n"
769 " else if (aData.x < 0) // leaf node\n"
774 " for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)\n"
776 " ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + TRG_OFFSET (aSubTree));\n"
778 " vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x += VRT_OFFSET (aSubTree)).xyz;\n"
779 " vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y += VRT_OFFSET (aSubTree)).xyz;\n"
780 " vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z += VRT_OFFSET (aSubTree)).xyz;\n"
782 " IntersectTriangle (aSubTree.TrsfRay, aPoint0, aPoint1, aPoint2, aTimeUV, aNormal);\n"
784 "#ifdef TRANSPARENT_SHADOWS\n"
785 " if (aTimeUV.x < theDistance)\n"
787 " aFactor *= 1.f - texelFetch (uRaytraceMaterialTexture, MATERIAL_TRAN (aTriangle.w)).x;\n"
790 " if (aTimeUV.x < theDistance)\n"
797 " toContinue = (aHead >= 0) && (aFactor > 0.1f);\n"
799 " if (aHead == aStop) // go to top-level BVH\n"
801 " aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
805 " aNode = pop (aHead);\n"
807 " else if (aData.x > 0) // switch node\n"
809 " aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root\n"
811 " vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);\n"
812 " vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);\n"
813 " vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);\n"
814 " vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);\n"
816 " aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,\n"
821 " aSubTree.TrsfRay.Origin = MatrixColMultiplyPnt (theRay.Origin,\n"
827 " aSubTree.Inverse = mix (-UNIT, UNIT, step (ZERO, aSubTree.TrsfRay.Direct)) / max (abs (aSubTree.TrsfRay.Direct), SMALL);\n"
829 " aNode = BVH_OFFSET (aSubTree); // go to sub-root node\n"
831 " aStop = aHead; // store current stack pointer\n"
838 "#define PI 3.1415926f\n"
840 "// =======================================================================\n"
841 "// function : Latlong\n"
842 "// purpose : Converts world direction to environment texture coordinates\n"
843 "// =======================================================================\n"
844 "vec2 Latlong (in vec3 thePoint, in float theRadius)\n"
846 " float aPsi = acos (-thePoint.z / theRadius);\n"
848 " float aPhi = atan (thePoint.y, thePoint.x) + PI;\n"
850 " return vec2 (aPhi * 0.1591549f,\n"
851 " aPsi * 0.3183098f);\n"
854 "// =======================================================================\n"
855 "// function : SmoothNormal\n"
856 "// purpose : Interpolates normal across the triangle\n"
857 "// =======================================================================\n"
858 "vec3 SmoothNormal (in vec2 theUV, in ivec4 theTriangle)\n"
860 " vec3 aNormal0 = texelFetch (uGeometryNormalTexture, theTriangle.x).xyz;\n"
861 " vec3 aNormal1 = texelFetch (uGeometryNormalTexture, theTriangle.y).xyz;\n"
862 " vec3 aNormal2 = texelFetch (uGeometryNormalTexture, theTriangle.z).xyz;\n"
864 " return normalize (aNormal1 * theUV.x +\n"
865 " aNormal2 * theUV.y +\n"
866 " aNormal0 * (1.0f - theUV.x - theUV.y));\n"
869 "#define POLYGON_OFFSET_UNIT 0.f\n"
870 "#define POLYGON_OFFSET_FACTOR 1.f\n"
871 "#define POLYGON_OFFSET_SCALE 0.006f\n"
873 "// =======================================================================\n"
874 "// function : PolygonOffset\n"
875 "// purpose : Computes OpenGL polygon offset\n"
876 "// =======================================================================\n"
877 "float PolygonOffset (in vec3 theNormal, in vec3 thePoint)\n"
879 " vec4 aProjectedNorm = vec4 (theNormal, -dot (theNormal, thePoint)) * uUnviewMat;\n"
881 " float aPolygonOffset = POLYGON_OFFSET_UNIT;\n"
883 " if (aProjectedNorm.z * aProjectedNorm.z > 1e-20f)\n"
885 " aProjectedNorm.xy *= 1.f / aProjectedNorm.z;\n"
887 " aPolygonOffset += POLYGON_OFFSET_FACTOR * max (abs (aProjectedNorm.x),\n"
888 " abs (aProjectedNorm.y));\n"
891 " return aPolygonOffset;\n"
894 "// =======================================================================\n"
895 "// function : SmoothUV\n"
896 "// purpose : Interpolates UV coordinates across the triangle\n"
897 "// =======================================================================\n"
898 "#ifdef USE_TEXTURES\n"
899 "vec2 SmoothUV (in vec2 theUV, in ivec4 theTriangle)\n"
901 " vec2 aTexCrd0 = texelFetch (uGeometryTexCrdTexture, theTriangle.x).st;\n"
902 " vec2 aTexCrd1 = texelFetch (uGeometryTexCrdTexture, theTriangle.y).st;\n"
903 " vec2 aTexCrd2 = texelFetch (uGeometryTexCrdTexture, theTriangle.z).st;\n"
905 " return aTexCrd1 * theUV.x +\n"
906 " aTexCrd2 * theUV.y +\n"
907 " aTexCrd0 * (1.0f - theUV.x - theUV.y);\n"
911 "// =======================================================================\n"
912 "// function : FetchEnvironment\n"
914 "// =======================================================================\n"
915 "vec4 FetchEnvironment (in vec2 theTexCoord)\n"
917 " return uSphereMapEnabled == 0 ?\n"
918 " vec4 (0.f, 0.f, 0.f, 1.f) : textureLod (uEnvironmentMapTexture, theTexCoord, 0.f);\n"
921 "// =======================================================================\n"
922 "// function : Refract\n"
923 "// purpose : Computes refraction ray (also handles TIR)\n"
924 "// =======================================================================\n"
925 "#ifndef PATH_TRACING\n"
926 "vec3 Refract (in vec3 theInput,\n"
927 " in vec3 theNormal,\n"
928 " in float theRefractIndex,\n"
929 " in float theInvRefractIndex)\n"
931 " float aNdotI = dot (theInput, theNormal);\n"
933 " float anIndex = aNdotI < 0.0f\n"
934 " ? theInvRefractIndex\n"
935 " : theRefractIndex;\n"
937 " float aSquare = anIndex * anIndex * (1.0f - aNdotI * aNdotI);\n"
939 " if (aSquare > 1.0f)\n"
941 " return reflect (theInput, theNormal);\n"
944 " float aNdotT = sqrt (1.0f - aSquare);\n"
946 " return normalize (anIndex * theInput -\n"
947 " (anIndex * aNdotI + (aNdotI < 0.0f ? aNdotT : -aNdotT)) * theNormal);\n"
951 "#define MIN_SLOPE 0.0001f\n"
952 "#define EPS_SCALE 8.0000f\n"
954 "#define THRESHOLD vec3 (0.1f)\n"
956 "#define INVALID_BOUNCES 1000\n"
958 "#define LIGHT_POS(index) (2 * index + 1)\n"
959 "#define LIGHT_PWR(index) (2 * index + 0)\n"
961 "// =======================================================================\n"
962 "// function : Radiance\n"
963 "// purpose : Computes color along the given ray\n"
964 "// =======================================================================\n"
965 "#ifndef PATH_TRACING\n"
966 "vec4 Radiance (in SRay theRay, in vec3 theInverse)\n"
968 " vec3 aResult = vec3 (0.0f);\n"
969 " vec4 aWeight = vec4 (1.0f);\n"
973 " float aRaytraceDepth = MAXFLOAT;\n"
975 " for (int aDepth = 0; aDepth < NB_BOUNCES; ++aDepth)\n"
977 " SIntersect aHit = SIntersect (MAXFLOAT, vec2 (ZERO), ZERO);\n"
979 " ivec4 aTriIndex = SceneNearestHit (theRay, theInverse, aHit, aTrsfId);\n"
981 " if (aTriIndex.x == -1)\n"
983 " vec4 aColor = vec4 (0.0);\n"
985 " if (bool(uSphereMapForBack) || aWeight.w == 0.0f /* reflection */)\n"
987 " float aTime = IntersectSphere (theRay, uSceneRadius);\n"
989 " aColor = FetchEnvironment (Latlong (\n"
990 " theRay.Direct * aTime + theRay.Origin, uSceneRadius));\n"
994 " aColor = BackgroundColor();\n"
997 " aResult += aWeight.xyz * aColor.xyz; aWeight.w *= aColor.w;\n"
999 " break; // terminate path\n"
1002 " vec3 aInvTransf0 = texelFetch (uSceneTransformTexture, aTrsfId + 0).xyz;\n"
1003 " vec3 aInvTransf1 = texelFetch (uSceneTransformTexture, aTrsfId + 1).xyz;\n"
1004 " vec3 aInvTransf2 = texelFetch (uSceneTransformTexture, aTrsfId + 2).xyz;\n"
1006 " aHit.Normal = normalize (vec3 (dot (aInvTransf0, aHit.Normal),\n"
1007 " dot (aInvTransf1, aHit.Normal),\n"
1008 " dot (aInvTransf2, aHit.Normal)));\n"
1010 " theRay.Origin += theRay.Direct * aHit.Time; // intersection point\n"
1012 " // Evaluate depth on first hit\n"
1013 " if (aDepth == 0)\n"
1015 " vec4 aNDCPoint = uViewMat * vec4 (theRay.Origin, 1.f);\n"
1017 " float aPolygonOffset = PolygonOffset (aHit.Normal, theRay.Origin);\n"
1018 " aRaytraceDepth = (aNDCPoint.z / aNDCPoint.w + aPolygonOffset * POLYGON_OFFSET_SCALE) * 0.5f + 0.5f;\n"
1021 " vec3 aNormal = SmoothNormal (aHit.UV, aTriIndex);\n"
1023 " aNormal = normalize (vec3 (dot (aInvTransf0, aNormal),\n"
1024 " dot (aInvTransf1, aNormal),\n"
1025 " dot (aInvTransf2, aNormal)));\n"
1027 " vec3 aAmbient = texelFetch (\n"
1028 " uRaytraceMaterialTexture, MATERIAL_AMBN (aTriIndex.w)).rgb;\n"
1029 " vec4 aDiffuse = texelFetch (\n"
1030 " uRaytraceMaterialTexture, MATERIAL_DIFF (aTriIndex.w));\n"
1031 " vec4 aSpecular = texelFetch (\n"
1032 " uRaytraceMaterialTexture, MATERIAL_SPEC (aTriIndex.w));\n"
1033 " vec4 aOpacity = texelFetch (\n"
1034 " uRaytraceMaterialTexture, MATERIAL_TRAN (aTriIndex.w));\n"
1036 "#ifdef USE_TEXTURES\n"
1037 " if (aDiffuse.w >= 0.f)\n"
1039 " vec4 aTexCoord = vec4 (SmoothUV (aHit.UV, aTriIndex), 0.f, 1.f);\n"
1041 " vec4 aTrsfRow1 = texelFetch (\n"
1042 " uRaytraceMaterialTexture, MATERIAL_TRS1 (aTriIndex.w));\n"
1043 " vec4 aTrsfRow2 = texelFetch (\n"
1044 " uRaytraceMaterialTexture, MATERIAL_TRS2 (aTriIndex.w));\n"
1046 " aTexCoord.st = vec2 (dot (aTrsfRow1, aTexCoord),\n"
1047 " dot (aTrsfRow2, aTexCoord));\n"
1049 " vec4 aTexColor = textureLod (\n"
1050 " sampler2D (uTextureSamplers[int(aDiffuse.w)]), aTexCoord.st, 0.f);\n"
1052 " aDiffuse.rgb *= aTexColor.rgb;\n"
1053 " aAmbient.rgb *= aTexColor.rgb;\n"
1055 " // keep refractive index untouched (Z component)\n"
1056 " aOpacity.xy = vec2 (aTexColor.w * aOpacity.x, 1.0f - aTexColor.w * aOpacity.x);\n"
1060 " vec3 aEmission = texelFetch (\n"
1061 " uRaytraceMaterialTexture, MATERIAL_EMIS (aTriIndex.w)).rgb;\n"
1063 " float aGeomFactor = dot (aNormal, theRay.Direct);\n"
1065 " aResult.xyz += aWeight.xyz * aOpacity.x * (\n"
1066 " uGlobalAmbient.xyz * aAmbient * max (abs (aGeomFactor), 0.5f) + aEmission);\n"
1068 " vec3 aSidedNormal = mix (aNormal, -aNormal, step (0.0f, aGeomFactor));\n"
1070 " for (int aLightIdx = 0; aLightIdx < uLightCount; ++aLightIdx)\n"
1072 " vec4 aLight = texelFetch (\n"
1073 " uRaytraceLightSrcTexture, LIGHT_POS (aLightIdx));\n"
1075 " float aDistance = MAXFLOAT;\n"
1077 " if (aLight.w != 0.0f) // point light source\n"
1079 " aDistance = length (aLight.xyz -= theRay.Origin);\n"
1081 " aLight.xyz *= 1.0f / aDistance;\n"
1084 " float aLdotN = dot (aLight.xyz, aSidedNormal);\n"
1086 " if (aLdotN > 0.0f) // first check if light source is important\n"
1088 " float aVisibility = 1.0f;\n"
1090 " if (bool(uShadowsEnabled))\n"
1092 " SRay aShadow = SRay (theRay.Origin, aLight.xyz);\n"
1094 " aShadow.Origin += uSceneEpsilon * (aLight.xyz +\n"
1095 " mix (-aHit.Normal, aHit.Normal, step (0.0f, dot (aHit.Normal, aLight.xyz))));\n"
1097 " vec3 aInverse = 1.0f / max (abs (aLight.xyz), SMALL);\n"
1099 " aVisibility = SceneAnyHit (\n"
1100 " aShadow, mix (-aInverse, aInverse, step (ZERO, aLight.xyz)), aDistance);\n"
1103 " if (aVisibility > 0.0f)\n"
1105 " vec3 aIntensity = min (UNIT, vec3 (texelFetch (\n"
1106 " uRaytraceLightSrcTexture, LIGHT_PWR (aLightIdx))));\n"
1108 " float aRdotV = dot (reflect (aLight.xyz, aSidedNormal), theRay.Direct);\n"
1110 " aResult.xyz += aWeight.xyz * (aOpacity.x * aVisibility) * aIntensity *\n"
1111 " (aDiffuse.xyz * aLdotN + aSpecular.xyz * pow (max (0.f, aRdotV), aSpecular.w));\n"
1116 " if (aOpacity.x != 1.0f)\n"
1118 " aWeight *= aOpacity.y;\n"
1120 " if (aOpacity.z != 1.0f)\n"
1122 " theRay.Direct = Refract (theRay.Direct, aNormal, aOpacity.z, aOpacity.w);\n"
1127 " aWeight *= bool(uReflectEnabled) ?\n"
1128 " texelFetch (uRaytraceMaterialTexture, MATERIAL_REFL (aTriIndex.w)) : vec4 (0.0f);\n"
1130 " vec3 aReflect = reflect (theRay.Direct, aNormal);\n"
1132 " if (dot (aReflect, aHit.Normal) * dot (theRay.Direct, aHit.Normal) > 0.0f)\n"
1134 " aReflect = reflect (theRay.Direct, aHit.Normal);\n"
1137 " theRay.Direct = aReflect;\n"
1140 " if (all (lessThanEqual (aWeight.xyz, THRESHOLD)))\n"
1142 " aDepth = INVALID_BOUNCES;\n"
1144 " else if (aOpacity.x == 1.0f || aOpacity.z != 1.0f) // if no simple transparency\n"
1146 " theRay.Origin += aHit.Normal * mix (\n"
1147 " -uSceneEpsilon, uSceneEpsilon, step (0.0f, dot (aHit.Normal, theRay.Direct)));\n"
1149 " theInverse = 1.0f / max (abs (theRay.Direct), SMALL);\n"
1151 " theInverse = mix (-theInverse, theInverse, step (ZERO, theRay.Direct));\n"
1154 " theRay.Origin += theRay.Direct * uSceneEpsilon;\n"
1157 " gl_FragDepth = aRaytraceDepth;\n"
1159 " return vec4 (aResult.x,\n"