1 // This file has been automatically generated from resource file src/Shaders/Display.fs
3 static const char Shaders_Display_fs[] =
4 "#ifdef ADAPTIVE_SAMPLING\n"
6 " #extension GL_ARB_shader_image_load_store : require\n"
8 " #extension GL_ARB_shader_image_size : enable\n"
10 " //! OpenGL image used for accumulating rendering result.\n"
11 " volatile restrict layout(r32f) uniform image2D uRenderImage;\n"
13 " //! OpenGL image storing variance of sampled pixels blocks.\n"
14 " volatile restrict layout(r32i) uniform iimage2D uVarianceImage;\n"
16 " //! Scale factor used to quantize visual error (float) into signed integer.\n"
17 " uniform float uVarianceScaleFactor;\n"
19 " //! Screen space tile size.\n"
20 " uniform ivec2 uTileSize;\n"
22 "#else // ADAPTIVE_SAMPLING\n"
25 " uniform sampler2D uInputTexture;\n"
27 " //! Ray tracing depth image.\n"
28 " uniform sampler2D uDepthTexture;\n"
30 "#endif // ADAPTIVE_SAMPLING\n"
32 "//! Number of accumulated frames.\n"
33 "uniform int uAccumFrames;\n"
35 "//! Is debug mode enabled for importance screen sampling.\n"
36 "uniform int uDebugAdaptive;\n"
38 "//! Exposure value for tone mapping.\n"
39 "uniform float uExposure;\n"
41 "#ifdef TONE_MAPPING_FILMIC\n"
43 "//! White point value for filmic tone mapping.\n"
44 "uniform float uWhitePoint;\n"
46 "#endif // TONE_MAPPING\n"
48 "//! Output pixel color.\n"
49 "out vec4 OutColor;\n"
51 "//! RGB weight factors to calculate luminance.\n"
52 "#define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)\n"
54 "// =======================================================================\n"
55 "// function : ToneMappingFilmic\n"
57 "// =======================================================================\n"
58 "vec4 ToneMappingFilmic(vec4 theColor, float theWhitePoint)\n"
60 " vec4 aPackColor = vec4 (theColor.rgb, theWhitePoint);\n"
61 " vec4 aFilmicCurve = 1.425f * aPackColor + vec4 (0.05f);\n"
62 " vec4 aResultColor = (aPackColor * aFilmicCurve + vec4 (0.004f)) / (aPackColor * (aFilmicCurve + vec4 (0.55f)) + vec4 (0.0491f)) - vec4 (0.0821f);\n"
63 " return vec4 (aResultColor.rgb / aResultColor.www, 1.0);\n"
66 "// =======================================================================\n"
67 "// function : main\n"
69 "// =======================================================================\n"
72 "#ifndef ADAPTIVE_SAMPLING\n"
74 " vec4 aColor = texelFetch (uInputTexture, ivec2 (gl_FragCoord.xy), 0);\n"
76 "#ifdef PATH_TRACING\n"
77 " float aDepth = aColor.w; // path tracing uses averaged depth\n"
79 " float aDepth = texelFetch (uDepthTexture, ivec2 (gl_FragCoord.xy), 0).r;\n"
82 " gl_FragDepth = aDepth;\n"
84 "#else // ADAPTIVE_SAMPLING\n"
86 " ivec2 aPixel = ivec2 (gl_FragCoord.xy);\n"
88 " vec4 aColor = vec4 (0.0);\n"
90 " // fetch accumulated color and total number of samples\n"
91 " aColor.x = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 0,\n"
92 " 2 * aPixel.y + 0)).x;\n"
93 " aColor.y = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 1,\n"
94 " 2 * aPixel.y + 0)).x;\n"
95 " aColor.z = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 1,\n"
96 " 2 * aPixel.y + 1)).x;\n"
97 " aColor.w = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 0,\n"
98 " 2 * aPixel.y + 1)).x;\n"
100 " // calculate normalization factor\n"
101 " float aSampleWeight = 1.f / max (1.0, aColor.w);\n"
103 " // calculate averaged depth value\n"
104 " gl_FragDepth = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 2,\n"
105 " 2 * aPixel.y + 1)).x * aSampleWeight;\n"
107 " // calculate averaged radiance for all samples and even samples only\n"
108 " float aHalfRad = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 2,\n"
109 " 2 * aPixel.y + 0)).x * aSampleWeight * 2.f;\n"
111 " float aAverRad = dot (aColor.rgb, LUMA) * aSampleWeight;\n"
113 " // apply our 'tone mapping' operator (gamma correction and clamping)\n"
114 " aHalfRad = min (1.f, sqrt (aHalfRad));\n"
115 " aAverRad = min (1.f, sqrt (aAverRad));\n"
117 " // calculate visual error\n"
118 " float anError = (aAverRad - aHalfRad) * (aAverRad - aHalfRad);\n"
120 " // accumulate visual error to current block; estimated error is written only\n"
121 " // after the first 40 samples and path length has reached 10 bounces or more\n"
122 " imageAtomicAdd (uVarianceImage, aPixel / uTileSize,\n"
123 " int (mix (uVarianceScaleFactor, anError * uVarianceScaleFactor, aColor.w > 40.f)));\n"
125 " if (uDebugAdaptive == 0) // normal rendering\n"
127 " aColor = vec4 (aColor.rgb * aSampleWeight, 1.0);\n"
129 " else // showing number of samples\n"
131 " vec2 aRatio = vec2 (1.f, 1.f);\n"
133 "#ifdef GL_ARB_shader_image_size\n"
134 " aRatio = vec2 (imageSize (uRenderImage)) / vec2 (3.f * 512.f, 2.f * 512.f);\n"
137 " 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"
140 "#endif // ADAPTIVE_SAMPLING\n"
142 "#ifdef PATH_TRACING\n"
144 " aColor *= pow (2, uExposure);\n"
146 "#ifdef TONE_MAPPING_FILMIC\n"
147 " aColor = ToneMappingFilmic (aColor, uWhitePoint);\n"
148 "#endif // TONE_MAPPING\n"
150 "#ifdef THE_SHIFT_sRGB\n"
151 " // apply gamma correction (we use gamma = 2)\n"
152 " OutColor = vec4 (sqrt (aColor.rgb), 0.f);\n"
154 " OutColor = vec4 (aColor.rgb, 0.f);\n"
157 "#else // not PATH_TRACING\n"
159 " OutColor = aColor;\n"