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