3a9b5dc8 |
1 | #ifdef ADAPTIVE_SAMPLING |
189f85a3 |
2 | |
3a9b5dc8 |
3 | #extension GL_ARB_shader_image_load_store : require |
1d865689 |
4 | |
4eaaf9d8 |
5 | #extension GL_ARB_shader_image_size : enable |
6 | |
3a9b5dc8 |
7 | //! OpenGL image used for accumulating rendering result. |
e084dbbc |
8 | volatile restrict layout(r32f) uniform image2D uRenderImage; |
3a9b5dc8 |
9 | |
10 | //! OpenGL image storing variance of sampled pixels blocks. |
e084dbbc |
11 | volatile restrict layout(r32i) uniform iimage2D uVarianceImage; |
3a9b5dc8 |
12 | |
66d1cdc6 |
13 | //! Scale factor used to quantize visual error (float) into signed integer. |
14 | uniform float uVarianceScaleFactor; |
15 | |
16 | //! Screen space tile size. |
17 | uniform ivec2 uTileSize; |
18 | |
3a9b5dc8 |
19 | #else // ADAPTIVE_SAMPLING |
20 | |
21 | //! Input image. |
22 | uniform sampler2D uInputTexture; |
23 | |
24 | //! Ray tracing depth image. |
25 | uniform sampler2D uDepthTexture; |
26 | |
27 | #endif // ADAPTIVE_SAMPLING |
28 | |
29 | //! Number of accumulated frames. |
30 | uniform int uAccumFrames; |
31 | |
32 | //! Is debug mode enabled for importance screen sampling. |
33 | uniform int uDebugAdaptive; |
d9e72440 |
34 | |
eb85ed36 |
35 | //! Exposure value for tone mapping. |
36 | uniform float uExposure; |
37 | |
38 | #ifdef TONE_MAPPING_FILMIC |
39 | |
40 | //! White point value for filmic tone mapping. |
41 | uniform float uWhitePoint; |
42 | |
43 | #endif // TONE_MAPPING |
44 | |
189f85a3 |
45 | //! Output pixel color. |
46 | out vec4 OutColor; |
47 | |
3a9b5dc8 |
48 | //! RGB weight factors to calculate luminance. |
49 | #define LUMA vec3 (0.2126f, 0.7152f, 0.0722f) |
50 | |
eb85ed36 |
51 | // ======================================================================= |
52 | // function : ToneMappingFilmic |
53 | // purpose : |
54 | // ======================================================================= |
55 | vec4 ToneMappingFilmic(vec4 theColor, float theWhitePoint) |
56 | { |
57 | vec4 aPackColor = vec4 (theColor.rgb, theWhitePoint); |
58 | vec4 aFilmicCurve = 1.425f * aPackColor + vec4 (0.05f); |
59 | vec4 aResultColor = (aPackColor * aFilmicCurve + vec4 (0.004f)) / (aPackColor * (aFilmicCurve + vec4 (0.55f)) + vec4 (0.0491f)) - vec4 (0.0821f); |
60 | return vec4 (aResultColor.rgb / aResultColor.www, 1.0); |
61 | } |
62 | |
3a9b5dc8 |
63 | // ======================================================================= |
64 | // function : main |
65 | // purpose : |
66 | // ======================================================================= |
189f85a3 |
67 | void main (void) |
68 | { |
3a9b5dc8 |
69 | #ifndef ADAPTIVE_SAMPLING |
70 | |
189f85a3 |
71 | vec4 aColor = texelFetch (uInputTexture, ivec2 (gl_FragCoord.xy), 0); |
72 | |
3a9b5dc8 |
73 | #ifdef PATH_TRACING |
74 | float aDepth = aColor.w; // path tracing uses averaged depth |
75 | #else |
1d865689 |
76 | float aDepth = texelFetch (uDepthTexture, ivec2 (gl_FragCoord.xy), 0).r; |
3a9b5dc8 |
77 | #endif |
78 | |
1d865689 |
79 | gl_FragDepth = aDepth; |
80 | |
3a9b5dc8 |
81 | #else // ADAPTIVE_SAMPLING |
82 | |
83 | ivec2 aPixel = ivec2 (gl_FragCoord.xy); |
84 | |
85 | vec4 aColor = vec4 (0.0); |
86 | |
87 | // fetch accumulated color and total number of samples |
88 | aColor.x = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 0, |
89 | 2 * aPixel.y + 0)).x; |
90 | aColor.y = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 1, |
91 | 2 * aPixel.y + 0)).x; |
92 | aColor.z = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 1, |
93 | 2 * aPixel.y + 1)).x; |
94 | aColor.w = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 0, |
95 | 2 * aPixel.y + 1)).x; |
96 | |
97 | // calculate normalization factor |
98 | float aSampleWeight = 1.f / max (1.0, aColor.w); |
99 | |
100 | // calculate averaged depth value |
101 | gl_FragDepth = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 2, |
102 | 2 * aPixel.y + 1)).x * aSampleWeight; |
103 | |
104 | // calculate averaged radiance for all samples and even samples only |
105 | float aHalfRad = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 2, |
106 | 2 * aPixel.y + 0)).x * aSampleWeight * 2.f; |
107 | |
108 | float aAverRad = dot (aColor.rgb, LUMA) * aSampleWeight; |
109 | |
110 | // apply our 'tone mapping' operator (gamma correction and clamping) |
111 | aHalfRad = min (1.f, sqrt (aHalfRad)); |
112 | aAverRad = min (1.f, sqrt (aAverRad)); |
113 | |
114 | // calculate visual error |
115 | float anError = (aAverRad - aHalfRad) * (aAverRad - aHalfRad); |
116 | |
4eaaf9d8 |
117 | // accumulate visual error to current block; estimated error is written only |
118 | // after the first 40 samples and path length has reached 10 bounces or more |
66d1cdc6 |
119 | imageAtomicAdd (uVarianceImage, aPixel / uTileSize, |
120 | int (mix (uVarianceScaleFactor, anError * uVarianceScaleFactor, aColor.w > 40.f))); |
3a9b5dc8 |
121 | |
122 | if (uDebugAdaptive == 0) // normal rendering |
d9e72440 |
123 | { |
3a9b5dc8 |
124 | aColor = vec4 (aColor.rgb * aSampleWeight, 1.0); |
d9e72440 |
125 | } |
3a9b5dc8 |
126 | else // showing number of samples |
d9e72440 |
127 | { |
4eaaf9d8 |
128 | vec2 aRatio = vec2 (1.f, 1.f); |
4eaaf9d8 |
129 | #ifdef GL_ARB_shader_image_size |
130 | aRatio = vec2 (imageSize (uRenderImage)) / vec2 (3.f * 512.f, 2.f * 512.f); |
131 | #endif |
4eaaf9d8 |
132 | aColor = vec4 (0.5f * aColor.rgb * aSampleWeight + vec3 (0.f, sqrt (aRatio.x * aRatio.y) * aColor.w / uAccumFrames * 0.35f, 0.f), 1.0); |
d9e72440 |
133 | } |
3a9b5dc8 |
134 | |
135 | #endif // ADAPTIVE_SAMPLING |
136 | |
137 | #ifdef PATH_TRACING |
138 | |
93cdaa76 |
139 | aColor *= pow (2.0, uExposure); |
eb85ed36 |
140 | |
141 | #ifdef TONE_MAPPING_FILMIC |
142 | aColor = ToneMappingFilmic (aColor, uWhitePoint); |
143 | #endif // TONE_MAPPING |
144 | |
ba00aab7 |
145 | #ifdef THE_SHIFT_sRGB |
eb85ed36 |
146 | // apply gamma correction (we use gamma = 2) |
147 | OutColor = vec4 (sqrt (aColor.rgb), 0.f); |
ba00aab7 |
148 | #else |
149 | OutColor = vec4 (aColor.rgb, 0.f); |
150 | #endif |
3a9b5dc8 |
151 | |
152 | #else // not PATH_TRACING |
153 | |
eb85ed36 |
154 | OutColor = aColor; |
3a9b5dc8 |
155 | |
156 | #endif |
189f85a3 |
157 | } |