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