0028488: VIS - fix compilation with VTK 8.2
[occt.git] / src / Shaders / RaytraceRender.fs
CommitLineData
fc73a202 1out vec4 OutColor;
2
3a9b5dc8 3// Seed for random number generator (generated on CPU).
189f85a3 4uniform int uFrameRndSeed;
5
3a9b5dc8 6//! Enables/disables using of single RNG seed for 16x16 image
7//! blocks. Increases performance up to 4x, but the noise has
8//! become structured. Can be used fo final rendering.
9uniform int uBlockedRngEnabled;
189f85a3 10
4eaaf9d8 11//! Number of previously rendered frames (used in non-ISS mode).
12uniform int uAccumSamples;
13
3a9b5dc8 14#ifndef ADAPTIVE_SAMPLING
383c6c9f 15 //! Input image with previously accumulated samples.
3a9b5dc8 16 uniform sampler2D uAccumTexture;
17#endif
8c820969 18
b09447ed 19//! Maximum radiance that can be added to the pixel.
20//! Decreases noise level, but introduces some bias.
21uniform float uMaxRadiance = 50.f;
8c820969 22
fc73a202 23// =======================================================================
24// function : main
25// purpose :
26// =======================================================================
27void main (void)
28{
3a9b5dc8 29 SeedRand (uFrameRndSeed, uWinSizeX, uBlockedRngEnabled == 0 ? 1 : 16);
30
189f85a3 31#ifndef PATH_TRACING
3a9b5dc8 32
fc73a202 33 SRay aRay = GenerateRay (vPixel);
3a9b5dc8 34
189f85a3 35#else
189f85a3 36
3a9b5dc8 37 ivec2 aFragCoord = ivec2 (gl_FragCoord.xy);
189f85a3 38
3a9b5dc8 39#ifdef ADAPTIVE_SAMPLING
189f85a3 40
3a9b5dc8 41 ivec2 aTileXY = imageLoad (uOffsetImage, ivec2 (aFragCoord.x / BLOCK_SIZE,
42 aFragCoord.y / BLOCK_SIZE)).xy;
189f85a3 43
3a9b5dc8 44 ivec2 aRealBlockSize = ivec2 (min (uWinSizeX - aTileXY.x, BLOCK_SIZE),
45 min (uWinSizeY - aTileXY.y, BLOCK_SIZE));
46
47 aFragCoord.x = aTileXY.x + (aFragCoord.x % aRealBlockSize.x);
48 aFragCoord.y = aTileXY.y + (aFragCoord.y % aRealBlockSize.y);
49
50#endif // ADAPTIVE_SAMPLING
51
52 vec2 aPnt = vec2 (aFragCoord.x + RandFloat(),
53 aFragCoord.y + RandFloat());
54
55 SRay aRay = GenerateRay (aPnt / vec2 (uWinSizeX, uWinSizeY));
56
57#endif // PATH_TRACING
58
59 vec3 aInvDirect = InverseDirection (aRay.Direct);
fc73a202 60
189f85a3 61#ifdef PATH_TRACING
3a9b5dc8 62
4eaaf9d8 63#ifndef ADAPTIVE_SAMPLING
64
65 vec4 aColor = PathTrace (aRay, aInvDirect, uAccumSamples);
66
67#else
68
69 float aNbSamples = imageAtomicAdd (uRenderImage, ivec2 (3 * aFragCoord.x + 0,
70 2 * aFragCoord.y + 1), 1.0);
71
72 vec4 aColor = PathTrace (aRay, aInvDirect, int (aNbSamples));
73
74#endif
189f85a3 75
3a9b5dc8 76 if (any (isnan (aColor.rgb)))
189f85a3 77 {
8c820969 78 aColor.rgb = ZERO;
189f85a3 79 }
80
b09447ed 81 aColor.rgb = min (aColor.rgb, vec3 (uMaxRadiance));
189f85a3 82
3a9b5dc8 83#ifdef ADAPTIVE_SAMPLING
84
85 // accumulate RGB color and depth
86 imageAtomicAdd (uRenderImage, ivec2 (3 * aFragCoord.x + 0,
87 2 * aFragCoord.y + 0), aColor.r);
88 imageAtomicAdd (uRenderImage, ivec2 (3 * aFragCoord.x + 1,
89 2 * aFragCoord.y + 0), aColor.g);
90 imageAtomicAdd (uRenderImage, ivec2 (3 * aFragCoord.x + 1,
91 2 * aFragCoord.y + 1), aColor.b);
92 imageAtomicAdd (uRenderImage, ivec2 (3 * aFragCoord.x + 2,
93 2 * aFragCoord.y + 1), aColor.w);
94
3a9b5dc8 95 if (int (aNbSamples) % 2 == 0) // accumulate luminance for even samples only
96 {
97 imageAtomicAdd (uRenderImage, ivec2 (3 * aFragCoord.x + 2,
98 2 * aFragCoord.y + 0), dot (LUMA, aColor.rgb));
99 }
100
3a9b5dc8 101#else
102
383c6c9f 103 if (uAccumSamples == 0)
be86ba90 104 {
105 OutColor = aColor;
106 }
107 else
108 {
383c6c9f 109 OutColor = mix (texture2D (uAccumTexture, vPixel), aColor, 1.f / (uAccumSamples + 1));
be86ba90 110 }
3a9b5dc8 111
112#endif // ADAPTIVE_SAMPLING
113
189f85a3 114#else
3a9b5dc8 115
fc73a202 116 OutColor = clamp (Radiance (aRay, aInvDirect), 0.f, 1.f);
3a9b5dc8 117
118#endif // PATH_TRACING
ee5befae 119}