0030476: Visualization, Path Tracing - Adaptive Screen Sampling leads to unstable...
[occt.git] / src / Shaders / Shaders_RaytraceRender_fs.pxx
1 // This file has been automatically generated from resource file src/Shaders/RaytraceRender.fs
2
3 static const char Shaders_RaytraceRender_fs[] =
4   "out vec4 OutColor;\n"
5   "\n"
6   "// Seed for random number generator (generated on CPU).\n"
7   "uniform int uFrameRndSeed;\n"
8   "\n"
9   "//! Enables/disables using of single RNG seed for 16x16 image\n"
10   "//! blocks. Increases performance up to 4x, but the noise has\n"
11   "//! become structured. Can be used fo final rendering.\n"
12   "uniform int uBlockedRngEnabled;\n"
13   "\n"
14   "//! Number of previously rendered frames (used in non-ISS mode).\n"
15   "uniform int uAccumSamples;\n"
16   "\n"
17   "#ifndef ADAPTIVE_SAMPLING\n"
18   "  //! Input image with previously accumulated samples.\n"
19   "  uniform sampler2D uAccumTexture;\n"
20   "#endif\n"
21   "\n"
22   "//! Maximum radiance that can be added to the pixel.\n"
23   "//! Decreases noise level, but introduces some bias.\n"
24   "uniform float uMaxRadiance = 50.f;\n"
25   "\n"
26   "#ifdef ADAPTIVE_SAMPLING\n"
27   "//! Wrapper over imageLoad()+imageStore() having similar syntax as imageAtomicAdd().\n"
28   "//! Modifies one component of 3Wx2H uRenderImage:\n"
29   "//! |RGL| Red, Green, Luminance\n"
30   "//! |SBH| Samples, Blue, Hit time transformed into OpenGL NDC space\n"
31   "//! Returns previous value of the component.\n"
32   "float addRenderImageComp (in ivec2 theFrag, in ivec2 theComp, in float theVal)\n"
33   "{\n"
34   "  ivec2 aCoord = ivec2 (3 * theFrag.x + theComp.x,\n"
35   "                        2 * theFrag.y + theComp.y);\n"
36   "#ifdef ADAPTIVE_SAMPLING_ATOMIC\n"
37   "  return imageAtomicAdd (uRenderImage, aCoord, theVal);\n"
38   "#else\n"
39   "  float aVal = imageLoad (uRenderImage, aCoord).x;\n"
40   "  imageStore (uRenderImage, aCoord, vec4 (aVal + theVal));\n"
41   "  return aVal;\n"
42   "#endif\n"
43   "}\n"
44   "#endif\n"
45   "\n"
46   "// =======================================================================\n"
47   "// function : main\n"
48   "// purpose  :\n"
49   "// =======================================================================\n"
50   "void main (void)\n"
51   "{\n"
52   "  SeedRand (uFrameRndSeed, uWinSizeX, uBlockedRngEnabled == 0 ? 1 : 16);\n"
53   "\n"
54   "#ifndef PATH_TRACING\n"
55   "\n"
56   "  SRay aRay = GenerateRay (vPixel);\n"
57   "\n"
58   "#else\n"
59   "\n"
60   "  ivec2 aFragCoord = ivec2 (gl_FragCoord.xy);\n"
61   "\n"
62   "#ifdef ADAPTIVE_SAMPLING\n"
63   "\n"
64   "#ifdef ADAPTIVE_SAMPLING_ATOMIC\n"
65   "  ivec2 aTileXY = imageLoad (uOffsetImage, aFragCoord / uTileSize).xy * uTileSize;\n"
66   "  if (aTileXY.x < 0) { discard; }\n"
67   "\n"
68   "  ivec2 aRealBlockSize = ivec2 (min (uWinSizeX - aTileXY.x, uTileSize.x),\n"
69   "                                min (uWinSizeY - aTileXY.y, uTileSize.y));\n"
70   "\n"
71   "  aFragCoord.x = aTileXY.x + (aFragCoord.x % aRealBlockSize.x);\n"
72   "  aFragCoord.y = aTileXY.y + (aFragCoord.y % aRealBlockSize.y);\n"
73   "#else\n"
74   "  int aNbTileSamples = imageAtomicAdd (uTilesImage, aFragCoord / uTileSize, int(-1));\n"
75   "  if (aNbTileSamples <= 0)\n"
76   "  {\n"
77   "    discard;\n"
78   "  }\n"
79   "#endif\n"
80   "\n"
81   "#endif // ADAPTIVE_SAMPLING\n"
82   "\n"
83   "  vec2 aPnt = vec2 (aFragCoord.x + RandFloat(),\n"
84   "                    aFragCoord.y + RandFloat());\n"
85   "\n"
86   "  SRay aRay = GenerateRay (aPnt / vec2 (uWinSizeX, uWinSizeY));\n"
87   "\n"
88   "#endif // PATH_TRACING\n"
89   "\n"
90   "  vec3 aInvDirect = InverseDirection (aRay.Direct);\n"
91   "\n"
92   "#ifdef PATH_TRACING\n"
93   "\n"
94   "#ifndef ADAPTIVE_SAMPLING\n"
95   "\n"
96   "  vec4 aColor = PathTrace (aRay, aInvDirect, uAccumSamples);\n"
97   "\n"
98   "#else\n"
99   "\n"
100   "  float aNbSamples = addRenderImageComp (aFragCoord, ivec2 (0, 1), 1.0);\n"
101   "  vec4 aColor = PathTrace (aRay, aInvDirect, int (aNbSamples));\n"
102   "\n"
103   "#endif\n"
104   "\n"
105   "  if (any (isnan (aColor.rgb)))\n"
106   "  {\n"
107   "    aColor.rgb = ZERO;\n"
108   "  }\n"
109   "\n"
110   "  aColor.rgb = min (aColor.rgb, vec3 (uMaxRadiance));\n"
111   "\n"
112   "#ifdef ADAPTIVE_SAMPLING\n"
113   "\n"
114   "  // accumulate RGB color and depth\n"
115   "  addRenderImageComp (aFragCoord, ivec2 (0, 0), aColor.r);\n"
116   "  addRenderImageComp (aFragCoord, ivec2 (1, 0), aColor.g);\n"
117   "  addRenderImageComp (aFragCoord, ivec2 (1, 1), aColor.b);\n"
118   "  addRenderImageComp (aFragCoord, ivec2 (2, 1), aColor.w);\n"
119   "\n"
120   "  if (int (aNbSamples) % 2 == 0) // accumulate luminance for even samples only\n"
121   "  {\n"
122   "    addRenderImageComp (aFragCoord, ivec2 (2, 0), dot (LUMA, aColor.rgb));\n"
123   "  }\n"
124   "\n"
125   "#else\n"
126   "\n"
127   "  if (uAccumSamples == 0)\n"
128   "  {\n"
129   "    OutColor = aColor;\n"
130   "  }\n"
131   "  else\n"
132   "  {\n"
133   "    OutColor = mix (texture2D (uAccumTexture, vPixel), aColor, 1.f / (uAccumSamples + 1));\n"
134   "  }\n"
135   "\n"
136   "#endif // ADAPTIVE_SAMPLING\n"
137   "\n"
138   "#else\n"
139   "\n"
140   "  OutColor = clamp (Radiance (aRay, aInvDirect), 0.f, 1.f);\n"
141   "\n"
142   "#endif // PATH_TRACING\n"
143   "}\n";