0028129: Visualization, Path Tracing - Improve interactivity in "steady" rendering...
[occt.git] / src / Shaders / Display.fs
CommitLineData
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.
8 volatile restrict layout(size1x32) uniform image2D uRenderImage;
9
10 //! OpenGL image storing variance of sampled pixels blocks.
11 volatile restrict layout(size1x32) uniform iimage2D uVarianceImage;
12
13#else // ADAPTIVE_SAMPLING
14
15 //! Input image.
16 uniform sampler2D uInputTexture;
17
18 //! Ray tracing depth image.
19 uniform sampler2D uDepthTexture;
20
21#endif // ADAPTIVE_SAMPLING
22
23//! Number of accumulated frames.
24uniform int uAccumFrames;
25
26//! Is debug mode enabled for importance screen sampling.
27uniform int uDebugAdaptive;
d9e72440 28
189f85a3 29//! Output pixel color.
30out vec4 OutColor;
31
3a9b5dc8 32//! RGB weight factors to calculate luminance.
33#define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)
34
35//! Scale factor used to quantize visual error.
36#define SCALE_FACTOR 1.0e6f
37
38// =======================================================================
39// function : main
40// purpose :
41// =======================================================================
189f85a3 42void main (void)
43{
3a9b5dc8 44#ifndef ADAPTIVE_SAMPLING
45
189f85a3 46 vec4 aColor = texelFetch (uInputTexture, ivec2 (gl_FragCoord.xy), 0);
47
3a9b5dc8 48#ifdef PATH_TRACING
49 float aDepth = aColor.w; // path tracing uses averaged depth
50#else
1d865689 51 float aDepth = texelFetch (uDepthTexture, ivec2 (gl_FragCoord.xy), 0).r;
3a9b5dc8 52#endif
53
1d865689 54 gl_FragDepth = aDepth;
55
3a9b5dc8 56#else // ADAPTIVE_SAMPLING
57
58 ivec2 aPixel = ivec2 (gl_FragCoord.xy);
59
60 vec4 aColor = vec4 (0.0);
61
62 // fetch accumulated color and total number of samples
63 aColor.x = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 0,
64 2 * aPixel.y + 0)).x;
65 aColor.y = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 1,
66 2 * aPixel.y + 0)).x;
67 aColor.z = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 1,
68 2 * aPixel.y + 1)).x;
69 aColor.w = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 0,
70 2 * aPixel.y + 1)).x;
71
72 // calculate normalization factor
73 float aSampleWeight = 1.f / max (1.0, aColor.w);
74
75 // calculate averaged depth value
76 gl_FragDepth = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 2,
77 2 * aPixel.y + 1)).x * aSampleWeight;
78
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;
82
83 float aAverRad = dot (aColor.rgb, LUMA) * aSampleWeight;
84
85 // apply our 'tone mapping' operator (gamma correction and clamping)
86 aHalfRad = min (1.f, sqrt (aHalfRad));
87 aAverRad = min (1.f, sqrt (aAverRad));
88
89 // calculate visual error
90 float anError = (aAverRad - aHalfRad) * (aAverRad - aHalfRad);
91
4eaaf9d8 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)));
3a9b5dc8 95
96 if (uDebugAdaptive == 0) // normal rendering
d9e72440 97 {
3a9b5dc8 98 aColor = vec4 (aColor.rgb * aSampleWeight, 1.0);
d9e72440 99 }
3a9b5dc8 100 else // showing number of samples
d9e72440 101 {
4eaaf9d8 102 vec2 aRatio = vec2 (1.f, 1.f);
103
104#ifdef GL_ARB_shader_image_size
105 aRatio = vec2 (imageSize (uRenderImage)) / vec2 (3.f * 512.f, 2.f * 512.f);
106#endif
107
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);
d9e72440 109 }
3a9b5dc8 110
111#endif // ADAPTIVE_SAMPLING
112
113#ifdef PATH_TRACING
114
115 // apply gamma correction (we use gamma = 2)
116 OutColor = vec4 (sqrt (aColor.rgb), 0.f);
117
118#else // not PATH_TRACING
119
120 OutColor = aColor;
121
122#endif
189f85a3 123}