1
0
Fork 0
fgdata/Shaders/HDR/smaa-neighborhood-blending.frag

102 lines
3.7 KiB
GLSL
Raw Normal View History

/**
* Adaptation of SMAA (Enhanced Subpixel Morphological Antialiasing)
* for FlightGear.
* See http://www.iryoku.com/smaa/ for details.
* Licensed under the MIT license, see below.
*/
/**
* Copyright (C) 2013 Jorge Jimenez (jorge@iryoku.com)
* Copyright (C) 2013 Jose I. Echevarria (joseignacioechevarria@gmail.com)
* Copyright (C) 2013 Belen Masia (bmasia@unizar.es)
* Copyright (C) 2013 Fernando Navarro (fernandn@microsoft.com)
* Copyright (C) 2013 Diego Gutierrez (diegog@unizar.es)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to
* do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. As clarification, there
* is no requirement that the copyright notice and permission be included in
* binary distributions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#version 330 core
#define mad(a, b, c) (a * b + c)
out vec4 fragColor;
in vec2 texCoord;
in vec4 vOffset;
uniform sampler2D color_tex;
uniform sampler2D blend_tex;
uniform vec4 fg_Viewport;
#define SMAA_RT_METRICS vec4(1.0 / fg_Viewport.z, 1.0 / fg_Viewport.w, fg_Viewport.z, fg_Viewport.w)
/**
* Conditional move:
*/
void SMAAMovc(bvec2 cond, inout vec2 variable, vec2 value) {
if (cond.x) variable.x = value.x;
if (cond.y) variable.y = value.y;
}
void SMAAMovc(bvec4 cond, inout vec4 variable, vec4 value) {
SMAAMovc(cond.xy, variable.xy, value.xy);
SMAAMovc(cond.zw, variable.zw, value.zw);
}
//-----------------------------------------------------------------------------
// Neighborhood Blending Pixel Shader (Third Pass)
void main()
{
vec4 color;
// Fetch the blending weights for current pixel:
vec4 a;
a.x = texture(blend_tex, vOffset.xy).a; // Right
a.y = texture(blend_tex, vOffset.zw).g; // Top
a.wz = texture(blend_tex, texCoord).xz; // Bottom / Left
// Is there any blending weight with a value greater than 0.0?
if (dot(a, vec4(1.0, 1.0, 1.0, 1.0)) < 1e-5) {
color = textureLod(color_tex, texCoord, 0.0);
} else {
bool h = max(a.x, a.z) > max(a.y, a.w); // max(horizontal) > max(vertical)
// Calculate the blending offsets:
vec4 blendingOffset = vec4(0.0, a.y, 0.0, a.w);
vec2 blendingWeight = a.yw;
SMAAMovc(bvec4(h, h, h, h), blendingOffset, vec4(a.x, 0.0, a.z, 0.0));
SMAAMovc(bvec2(h, h), blendingWeight, a.xz);
blendingWeight /= dot(blendingWeight, vec2(1.0, 1.0));
// Calculate the texture coordinates:
vec4 blendingCoord = mad(blendingOffset, vec4(SMAA_RT_METRICS.xy, -SMAA_RT_METRICS.xy), texCoord.xyxy);
// We exploit bilinear filtering to mix current pixel with the chosen
// neighbor:
color = blendingWeight.x * textureLod(color_tex, blendingCoord.xy, 0.0);
color += blendingWeight.y * textureLod(color_tex, blendingCoord.zw, 0.0);
}
fragColor = color;
}