1 #ifdef ADAPTIVE_SAMPLING
3 #extension GL_ARB_shader_image_load_store : require
5 //! OpenGL image used for accumulating rendering result.
6 volatile restrict layout(size1x32) uniform image2D uRenderImage;
8 //! OpenGL image storing variance of sampled pixels blocks.
9 volatile restrict layout(size1x32) uniform iimage2D uVarianceImage;
11 #else // ADAPTIVE_SAMPLING
14 uniform sampler2D uInputTexture;
16 //! Ray tracing depth image.
17 uniform sampler2D uDepthTexture;
19 #endif // ADAPTIVE_SAMPLING
21 //! Number of accumulated frames.
22 uniform int uAccumFrames;
24 //! Is debug mode enabled for importance screen sampling.
25 uniform int uDebugAdaptive;
27 //! Output pixel color.
30 //! RGB weight factors to calculate luminance.
31 #define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)
33 //! Scale factor used to quantize visual error.
34 #define SCALE_FACTOR 1.0e6f
36 // =======================================================================
39 // =======================================================================
42 #ifndef ADAPTIVE_SAMPLING
44 vec4 aColor = texelFetch (uInputTexture, ivec2 (gl_FragCoord.xy), 0);
47 float aDepth = aColor.w; // path tracing uses averaged depth
49 float aDepth = texelFetch (uDepthTexture, ivec2 (gl_FragCoord.xy), 0).r;
52 gl_FragDepth = aDepth;
54 #else // ADAPTIVE_SAMPLING
56 ivec2 aPixel = ivec2 (gl_FragCoord.xy);
58 vec4 aColor = vec4 (0.0);
60 // fetch accumulated color and total number of samples
61 aColor.x = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 0,
63 aColor.y = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 1,
65 aColor.z = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 1,
67 aColor.w = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 0,
70 // calculate normalization factor
71 float aSampleWeight = 1.f / max (1.0, aColor.w);
73 // calculate averaged depth value
74 gl_FragDepth = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 2,
75 2 * aPixel.y + 1)).x * aSampleWeight;
77 // calculate averaged radiance for all samples and even samples only
78 float aHalfRad = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 2,
79 2 * aPixel.y + 0)).x * aSampleWeight * 2.f;
81 float aAverRad = dot (aColor.rgb, LUMA) * aSampleWeight;
83 // apply our 'tone mapping' operator (gamma correction and clamping)
84 aHalfRad = min (1.f, sqrt (aHalfRad));
85 aAverRad = min (1.f, sqrt (aAverRad));
87 // calculate visual error
88 float anError = (aAverRad - aHalfRad) * (aAverRad - aHalfRad);
90 // accumulate visual error to current block
91 imageAtomicAdd (uVarianceImage, ivec2 (aPixel / vec2 (BLOCK_SIZE)), int (anError * SCALE_FACTOR));
93 if (uDebugAdaptive == 0) // normal rendering
95 aColor = vec4 (aColor.rgb * aSampleWeight, 1.0);
97 else // showing number of samples
99 aColor = vec4 (0.5f * aColor.rgb * aSampleWeight + vec3 (0.f, aColor.w / uAccumFrames * 0.35f, 0.f), 1.0);
102 #endif // ADAPTIVE_SAMPLING
106 // apply gamma correction (we use gamma = 2)
107 OutColor = vec4 (sqrt (aColor.rgb), 0.f);
109 #else // not PATH_TRACING