| 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" |
| 8 | " #extension GL_ARB_shader_image_size : enable\n" |
| 9 | "\n" |
| 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 | " //! Scale factor used to quantize visual error (float) into signed integer.\n" |
| 17 | " uniform float uVarianceScaleFactor;\n" |
| 18 | "\n" |
| 19 | " //! Screen space tile size.\n" |
| 20 | " uniform ivec2 uTileSize;\n" |
| 21 | "\n" |
| 22 | "#else // ADAPTIVE_SAMPLING\n" |
| 23 | "\n" |
| 24 | " //! Input image.\n" |
| 25 | " uniform sampler2D uInputTexture;\n" |
| 26 | "\n" |
| 27 | " //! Ray tracing depth image.\n" |
| 28 | " uniform sampler2D uDepthTexture;\n" |
| 29 | "\n" |
| 30 | "#endif // ADAPTIVE_SAMPLING\n" |
| 31 | "\n" |
| 32 | "//! Number of accumulated frames.\n" |
| 33 | "uniform int uAccumFrames;\n" |
| 34 | "\n" |
| 35 | "//! Is debug mode enabled for importance screen sampling.\n" |
| 36 | "uniform int uDebugAdaptive;\n" |
| 37 | "\n" |
| 38 | "//! Exposure value for tone mapping.\n" |
| 39 | "uniform float uExposure;\n" |
| 40 | "\n" |
| 41 | "#ifdef TONE_MAPPING_FILMIC\n" |
| 42 | "\n" |
| 43 | "//! White point value for filmic tone mapping.\n" |
| 44 | "uniform float uWhitePoint;\n" |
| 45 | "\n" |
| 46 | "#endif // TONE_MAPPING\n" |
| 47 | "\n" |
| 48 | "//! Output pixel color.\n" |
| 49 | "out vec4 OutColor;\n" |
| 50 | "\n" |
| 51 | "//! RGB weight factors to calculate luminance.\n" |
| 52 | "#define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)\n" |
| 53 | "\n" |
| 54 | "// =======================================================================\n" |
| 55 | "// function : ToneMappingFilmic\n" |
| 56 | "// purpose :\n" |
| 57 | "// =======================================================================\n" |
| 58 | "vec4 ToneMappingFilmic(vec4 theColor, float theWhitePoint)\n" |
| 59 | "{\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" |
| 64 | "}\n" |
| 65 | "\n" |
| 66 | "// =======================================================================\n" |
| 67 | "// function : main\n" |
| 68 | "// purpose :\n" |
| 69 | "// =======================================================================\n" |
| 70 | "void main (void)\n" |
| 71 | "{\n" |
| 72 | "#ifndef ADAPTIVE_SAMPLING\n" |
| 73 | "\n" |
| 74 | " vec4 aColor = texelFetch (uInputTexture, ivec2 (gl_FragCoord.xy), 0);\n" |
| 75 | "\n" |
| 76 | "#ifdef PATH_TRACING\n" |
| 77 | " float aDepth = aColor.w; // path tracing uses averaged depth\n" |
| 78 | "#else\n" |
| 79 | " float aDepth = texelFetch (uDepthTexture, ivec2 (gl_FragCoord.xy), 0).r;\n" |
| 80 | "#endif\n" |
| 81 | "\n" |
| 82 | " gl_FragDepth = aDepth;\n" |
| 83 | "\n" |
| 84 | "#else // ADAPTIVE_SAMPLING\n" |
| 85 | "\n" |
| 86 | " ivec2 aPixel = ivec2 (gl_FragCoord.xy);\n" |
| 87 | "\n" |
| 88 | " vec4 aColor = vec4 (0.0);\n" |
| 89 | "\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" |
| 99 | "\n" |
| 100 | " // calculate normalization factor\n" |
| 101 | " float aSampleWeight = 1.f / max (1.0, aColor.w);\n" |
| 102 | "\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" |
| 106 | "\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" |
| 110 | "\n" |
| 111 | " float aAverRad = dot (aColor.rgb, LUMA) * aSampleWeight;\n" |
| 112 | "\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" |
| 116 | "\n" |
| 117 | " // calculate visual error\n" |
| 118 | " float anError = (aAverRad - aHalfRad) * (aAverRad - aHalfRad);\n" |
| 119 | "\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" |
| 124 | "\n" |
| 125 | " if (uDebugAdaptive == 0) // normal rendering\n" |
| 126 | " {\n" |
| 127 | " aColor = vec4 (aColor.rgb * aSampleWeight, 1.0);\n" |
| 128 | " }\n" |
| 129 | " else // showing number of samples\n" |
| 130 | " {\n" |
| 131 | " vec2 aRatio = vec2 (1.f, 1.f);\n" |
| 132 | "\n" |
| 133 | "#ifdef GL_ARB_shader_image_size\n" |
| 134 | " aRatio = vec2 (imageSize (uRenderImage)) / vec2 (3.f * 512.f, 2.f * 512.f);\n" |
| 135 | "#endif\n" |
| 136 | "\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" |
| 138 | " }\n" |
| 139 | "\n" |
| 140 | "#endif // ADAPTIVE_SAMPLING\n" |
| 141 | "\n" |
| 142 | "#ifdef PATH_TRACING\n" |
| 143 | "\n" |
| 144 | " aColor *= pow (2, uExposure);\n" |
| 145 | "\n" |
| 146 | "#ifdef TONE_MAPPING_FILMIC\n" |
| 147 | " aColor = ToneMappingFilmic (aColor, uWhitePoint);\n" |
| 148 | "#endif // TONE_MAPPING\n" |
| 149 | "\n" |
| 150 | " // apply gamma correction (we use gamma = 2)\n" |
| 151 | " OutColor = vec4 (sqrt (aColor.rgb), 0.f);\n" |
| 152 | "\n" |
| 153 | "#else // not PATH_TRACING\n" |
| 154 | "\n" |
| 155 | " OutColor = aColor;\n" |
| 156 | "\n" |
| 157 | "#endif\n" |
| 158 | "}\n"; |