1 #ifdef ADAPTIVE_SAMPLING
3 #extension GL_ARB_shader_image_load_store : require
5 #extension GL_ARB_shader_image_size : enable
7 //! OpenGL image used for accumulating rendering result.
8 volatile restrict layout(size1x32) uniform image2D uRenderImage;
10 //! OpenGL image storing variance of sampled pixels blocks.
11 volatile restrict layout(size1x32) uniform iimage2D uVarianceImage;
13 #else // ADAPTIVE_SAMPLING
16 uniform sampler2D uInputTexture;
18 //! Ray tracing depth image.
19 uniform sampler2D uDepthTexture;
21 #endif // ADAPTIVE_SAMPLING
23 //! Number of accumulated frames.
24 uniform int uAccumFrames;
26 //! Is debug mode enabled for importance screen sampling.
27 uniform int uDebugAdaptive;
29 //! Output pixel color.
32 //! RGB weight factors to calculate luminance.
33 #define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)
35 //! Scale factor used to quantize visual error.
36 #define SCALE_FACTOR 1.0e6f
38 // =======================================================================
41 // =======================================================================
44 #ifndef ADAPTIVE_SAMPLING
46 vec4 aColor = texelFetch (uInputTexture, ivec2 (gl_FragCoord.xy), 0);
49 float aDepth = aColor.w; // path tracing uses averaged depth
51 float aDepth = texelFetch (uDepthTexture, ivec2 (gl_FragCoord.xy), 0).r;
54 gl_FragDepth = aDepth;
56 #else // ADAPTIVE_SAMPLING
58 ivec2 aPixel = ivec2 (gl_FragCoord.xy);
60 vec4 aColor = vec4 (0.0);
62 // fetch accumulated color and total number of samples
63 aColor.x = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 0,
65 aColor.y = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 1,
67 aColor.z = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 1,
69 aColor.w = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 0,
72 // calculate normalization factor
73 float aSampleWeight = 1.f / max (1.0, aColor.w);
75 // calculate averaged depth value
76 gl_FragDepth = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 2,
77 2 * aPixel.y + 1)).x * aSampleWeight;
79 // calculate averaged radiance for all samples and even samples only
80 float aHalfRad = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 2,
81 2 * aPixel.y + 0)).x * aSampleWeight * 2.f;
83 float aAverRad = dot (aColor.rgb, LUMA) * aSampleWeight;
85 // apply our 'tone mapping' operator (gamma correction and clamping)
86 aHalfRad = min (1.f, sqrt (aHalfRad));
87 aAverRad = min (1.f, sqrt (aAverRad));
89 // calculate visual error
90 float anError = (aAverRad - aHalfRad) * (aAverRad - aHalfRad);
92 // accumulate visual error to current block; estimated error is written only
93 // after the first 40 samples and path length has reached 10 bounces or more
94 imageAtomicAdd (uVarianceImage, ivec2 (aPixel / vec2 (BLOCK_SIZE)), int (mix (SCALE_FACTOR, anError * SCALE_FACTOR, aColor.w > 40.f)));
96 if (uDebugAdaptive == 0) // normal rendering
98 aColor = vec4 (aColor.rgb * aSampleWeight, 1.0);
100 else // showing number of samples
102 vec2 aRatio = vec2 (1.f, 1.f);
104 #ifdef GL_ARB_shader_image_size
105 aRatio = vec2 (imageSize (uRenderImage)) / vec2 (3.f * 512.f, 2.f * 512.f);
108 aColor = vec4 (0.5f * aColor.rgb * aSampleWeight + vec3 (0.f, sqrt (aRatio.x * aRatio.y) * aColor.w / uAccumFrames * 0.35f, 0.f), 1.0);
111 #endif // ADAPTIVE_SAMPLING
115 // apply gamma correction (we use gamma = 2)
116 OutColor = vec4 (sqrt (aColor.rgb), 0.f);
118 #else // not PATH_TRACING