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