| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <!--
- Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
- All rights reserved.
- Code licensed under the BSD License.
- http://www.anki3d.org/LICENSE
- -->
- <shaderProgram>
- <mutators>
- <mutator name="SHARPEN" values="0 1 2"/> <!-- 0: disabled, 1: vertical, 2: horizontal -->
- <mutator name="VARIANCE_CLIPPING" values="0 1"/>
- <mutator name="TONEMAP_FIX" values="0 1"/>
- <mutator name="YCBCR" values="0 1"/>
- </mutators>
- <inputs>
- <input name="VARIANCE_CLIPPING_GAMMA" type="float" const="1"/>
- <input name="BLEND_FACTOR" type="float" const="1"/>
- </inputs>
- <shaders>
- <shader type="vert">
- <source><![CDATA[
- #include "shaders/QuadVert.glsl"
- ]]></source>
- </shader>
- <shader type="frag">
- <source><![CDATA[
- #include "shaders/Functions.glsl"
- #include "shaders/Pack.glsl"
- #include "shaders/Tonemapping.glsl"
- layout(location = 0) in vec2 in_uv;
- layout(location = 0) out vec3 out_color;
- layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_depthRt;
- layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_inputRt;
- layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_historyRt;
- layout(ANKI_UBO_BINDING(0, 0), std140, row_major) uniform u0_
- {
- mat4 u_prevViewProjMatMulInvViewProjMat;
- };
- #if TONEMAP_FIX
- #define TONEMAPPING_SET 0
- #define TONEMAPPING_BINDING 1
- #include "shaders/TonemappingResources.glsl"
- #endif
- #if YCBCR
- #define sample(s, uv) rgbToYCbCr(textureLod(s, uv, 0.0).rgb)
- #define sampleOffset(s, uv, x, y) rgbToYCbCr(textureLodOffset(s, uv, 0.0, ivec2(x, y)).rgb)
- #else
- #define sample(s, uv) textureLod(s, uv, 0.0).rgb
- #define sampleOffset(s, uv, x, y) textureLodOffset(s, uv, 0.0, ivec2(x, y)).rgb
- #endif
- vec3 sharpen()
- {
- vec3 center = sample(u_inputRt, in_uv);
- #if SHARPEN == 1
- vec3 near = sampleOffset(u_inputRt, in_uv, 1, 0) + sampleOffset(u_inputRt, in_uv, -1, 0);
- #else
- vec3 near = sampleOffset(u_inputRt, in_uv, 0, 1) + sampleOffset(u_inputRt, in_uv, 0, -1);
- #endif
- near *= 0.5;
- float sharpness = 1.0;
- return center + (center - near) * sharpness;
- }
- void main()
- {
- float depth = textureLod(u_depthRt, in_uv, 0.0).r;
- // Get prev uv coords
- vec4 v4 = u_prevViewProjMatMulInvViewProjMat * vec4(UV_TO_NDC(in_uv), depth, 1.0);
- vec2 oldUv = NDC_TO_UV(v4.xy / v4.w);
- // Read textures
- vec3 historyCol = sample(u_historyRt, oldUv);
- #if SHARPEN > 0
- vec3 crntCol = sharpen();
- #else
- vec3 crntCol = sample(u_inputRt, in_uv);
- #endif
- // Remove ghosting by clamping the history color to neighbour's AABB
- vec3 near0 = sampleOffset(u_inputRt, in_uv, 1, 0);
- vec3 near1 = sampleOffset(u_inputRt, in_uv, 0, 1);
- vec3 near2 = sampleOffset(u_inputRt, in_uv, -1, 0);
- vec3 near3 = sampleOffset(u_inputRt, in_uv, 0, -1);
- #if VARIANCE_CLIPPING
- vec3 m1 = crntCol + near0 + near1 + near2 + near3;
- vec3 m2 = crntCol * crntCol + near0 * near0 + near1 * near1 + near2 * near2 + near3 * near3;
- vec3 mu = m1 / 5.0;
- vec3 sigma = sqrt(m2 / 5.0 - mu * mu);
- vec3 boxMin = mu - VARIANCE_CLIPPING_GAMMA * sigma;
- vec3 boxMax = mu + VARIANCE_CLIPPING_GAMMA * sigma;
- #else
- vec3 boxMin = min(crntCol, min(near0, min(near1, min(near2, near3))));
- vec3 boxMax = max(crntCol, max(near0, max(near1, max(near2, near3))));
- #endif
- historyCol = clamp(historyCol, boxMin, boxMax);
- // Remove jitter (T. Lottes)
- #if YCBCR
- float lum0 = crntCol.r;
- float lum1 = historyCol.r;
- float maxLum = boxMax.r;
- #elif TONEMAP_FIX
- float lum0 = computeLuminance(tonemap(crntCol, u_exposureThreshold0));
- float lum1 = computeLuminance(tonemap(historyCol, u_exposureThreshold0));
- //float maxLum = computeLuminance(tonemap(boxMax, u_exposureThreshold0));
- float maxLum = 1.0;
- #else
- float lum0 = computeLuminance(crntCol);
- float lum1 = computeLuminance(historyCol);
- float maxLum = computeLuminance(boxMax);
- #endif
- float diff = abs(lum0 - lum1) / max(lum0, max(lum1, maxLum + EPSILON));
- diff = 1.0 - diff;
- diff = diff * diff;
- float feedback = mix(0.0, BLEND_FACTOR, diff);
- // Write result
- #if YCBCR
- out_color = yCbCrToRgb(mix(historyCol, crntCol, feedback));
- #else
- out_color = mix(historyCol, crntCol, feedback);
- #endif
- }
- ]]></source>
- </shader>
- </shaders>
- </shaderProgram>
|