0028198: Add FPE signals enabling from CMake
[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
3a9b5dc8 11#ifndef ADAPTIVE_SAMPLING
383c6c9f 12 //! Input image with previously accumulated samples.
3a9b5dc8 13 uniform sampler2D uAccumTexture;
14#endif
8c820969 15
3a9b5dc8 16//! Maximum radiance that can be added to the pixel. Decreases noise
17//! level, but introduces some bias.
6e728f3b 18#define MAX_RADIANCE vec3 (50.f)
8c820969 19
fc73a202 20// =======================================================================
21// function : main
22// purpose :
23// =======================================================================
24void main (void)
25{
3a9b5dc8 26 SeedRand (uFrameRndSeed, uWinSizeX, uBlockedRngEnabled == 0 ? 1 : 16);
27
189f85a3 28#ifndef PATH_TRACING
3a9b5dc8 29
fc73a202 30 SRay aRay = GenerateRay (vPixel);
3a9b5dc8 31
189f85a3 32#else
189f85a3 33
3a9b5dc8 34 ivec2 aFragCoord = ivec2 (gl_FragCoord.xy);
189f85a3 35
3a9b5dc8 36#ifdef ADAPTIVE_SAMPLING
189f85a3 37
3a9b5dc8 38 ivec2 aTileXY = imageLoad (uOffsetImage, ivec2 (aFragCoord.x / BLOCK_SIZE,
39 aFragCoord.y / BLOCK_SIZE)).xy;
189f85a3 40
3a9b5dc8 41 ivec2 aRealBlockSize = ivec2 (min (uWinSizeX - aTileXY.x, BLOCK_SIZE),
42 min (uWinSizeY - aTileXY.y, BLOCK_SIZE));
43
44 aFragCoord.x = aTileXY.x + (aFragCoord.x % aRealBlockSize.x);
45 aFragCoord.y = aTileXY.y + (aFragCoord.y % aRealBlockSize.y);
46
47#endif // ADAPTIVE_SAMPLING
48
49 vec2 aPnt = vec2 (aFragCoord.x + RandFloat(),
50 aFragCoord.y + RandFloat());
51
52 SRay aRay = GenerateRay (aPnt / vec2 (uWinSizeX, uWinSizeY));
53
54#endif // PATH_TRACING
55
56 vec3 aInvDirect = InverseDirection (aRay.Direct);
fc73a202 57
189f85a3 58#ifdef PATH_TRACING
3a9b5dc8 59
189f85a3 60 vec4 aColor = PathTrace (aRay, aInvDirect);
61
3a9b5dc8 62 if (any (isnan (aColor.rgb)))
189f85a3 63 {
8c820969 64 aColor.rgb = ZERO;
189f85a3 65 }
66
8c820969 67 aColor.rgb = min (aColor.rgb, MAX_RADIANCE);
189f85a3 68
3a9b5dc8 69#ifdef ADAPTIVE_SAMPLING
70
71 // accumulate RGB color and depth
72 imageAtomicAdd (uRenderImage, ivec2 (3 * aFragCoord.x + 0,
73 2 * aFragCoord.y + 0), aColor.r);
74 imageAtomicAdd (uRenderImage, ivec2 (3 * aFragCoord.x + 1,
75 2 * aFragCoord.y + 0), aColor.g);
76 imageAtomicAdd (uRenderImage, ivec2 (3 * aFragCoord.x + 1,
77 2 * aFragCoord.y + 1), aColor.b);
78 imageAtomicAdd (uRenderImage, ivec2 (3 * aFragCoord.x + 2,
79 2 * aFragCoord.y + 1), aColor.w);
80
81 // accumulate number of samples
82 float aNbSamples = imageAtomicAdd (uRenderImage, ivec2 (3 * aFragCoord.x + 0,
83 2 * aFragCoord.y + 1), 1.0);
84
85 if (int (aNbSamples) % 2 == 0) // accumulate luminance for even samples only
86 {
87 imageAtomicAdd (uRenderImage, ivec2 (3 * aFragCoord.x + 2,
88 2 * aFragCoord.y + 0), dot (LUMA, aColor.rgb));
89 }
90
91 discard; // fragment should not be written to frame buffer
92
93#else
94
383c6c9f 95 if (uAccumSamples == 0)
be86ba90 96 {
97 OutColor = aColor;
98 }
99 else
100 {
383c6c9f 101 OutColor = mix (texture2D (uAccumTexture, vPixel), aColor, 1.f / (uAccumSamples + 1));
be86ba90 102 }
3a9b5dc8 103
104#endif // ADAPTIVE_SAMPLING
105
189f85a3 106#else
3a9b5dc8 107
fc73a202 108 OutColor = clamp (Radiance (aRay, aInvDirect), 0.f, 1.f);
3a9b5dc8 109
110#endif // PATH_TRACING
ee5befae 111}