armory/Shaders/smaa_neighborhood_blend/smaa_neighborhood_blend.frag.glsl

93 lines
2.5 KiB
Plaintext
Raw Normal View History

2016-06-14 12:33:11 +02:00
#version 450
#include "compiled.inc"
2018-02-18 19:10:14 +01:00
2016-06-14 23:32:44 +02:00
uniform sampler2D colorTex;
uniform sampler2D blendTex;
2016-07-31 23:25:33 +02:00
#ifdef _Veloc
uniform sampler2D sveloc;
#endif
2016-06-14 12:33:11 +02:00
2016-06-21 13:29:27 +02:00
uniform vec2 screenSizeInv;
2016-06-14 12:33:11 +02:00
in vec2 texCoord;
2016-06-14 23:32:44 +02:00
in vec4 offset;
2016-10-12 17:52:27 +02:00
out vec4 fragColor;
2016-06-14 12:33:11 +02:00
//-----------------------------------------------------------------------------
// Neighborhood Blending Pixel Shader (Third Pass)
2018-12-01 12:48:29 +01:00
vec4 textureLodA(sampler2D tex, vec2 coords, float lod) {
2020-05-11 09:03:13 +02:00
#ifdef _InvY
2018-12-01 12:48:29 +01:00
coords.y = 1.0 - coords.y;
#endif
return textureLod(tex, coords, lod);
}
2016-06-14 23:32:44 +02:00
2018-12-01 12:48:29 +01:00
vec4 SMAANeighborhoodBlendingPS(vec2 texcoord, vec4 offset) {
2016-10-17 17:39:40 +02:00
// Fetch the blending weights for current pixel:
vec4 a;
2018-12-06 15:23:08 +01:00
a.x = textureLod(blendTex, offset.xy, 0.0).a; // Right
a.y = textureLod(blendTex, offset.zw, 0.0).g; // Top
a.wz = textureLod(blendTex, texcoord, 0.0).xz; // Bottom / Left
2016-10-17 17:39:40 +02:00
// Is there any blending weight with a value greater than 0.0?
//SMAA_BRANCH
if (dot(a, vec4(1.0, 1.0, 1.0, 1.0)) < 1e-5) {
vec4 color = textureLod(colorTex, texcoord, 0.0);
2016-06-14 12:33:11 +02:00
2016-07-31 23:25:33 +02:00
#ifdef _Veloc
2016-10-17 17:39:40 +02:00
vec2 velocity = textureLod(sveloc, texCoord, 0.0).rg;
// Pack velocity into the alpha channel:
color.a = sqrt(5.0 * length(velocity));
2016-07-31 23:25:33 +02:00
#endif
2016-10-17 17:39:40 +02:00
return color;
}
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;
2020-05-11 09:03:13 +02:00
2018-12-01 12:48:29 +01:00
if (h) {
blendingOffset.x = a.x;
blendingOffset.y = 0.0;
blendingOffset.z = a.z;
blendingOffset.w = 0.0;
blendingWeight.x = a.x;
blendingWeight.y = a.z;
}
2020-05-11 09:03:13 +02:00
2016-10-17 17:39:40 +02:00
blendingWeight /= dot(blendingWeight, vec2(1.0, 1.0));
// Calculate the texture coordinates:
2020-05-11 09:03:13 +02:00
#ifdef _InvY
2018-12-01 12:48:29 +01:00
vec2 tc = vec2(texcoord.x, 1.0 - texcoord.y);
#else
vec2 tc = texcoord;
#endif
vec4 blendingCoord = blendingOffset * vec4(screenSizeInv.xy, -screenSizeInv.xy) + tc.xyxy;
2016-10-17 17:39:40 +02:00
// We exploit bilinear filtering to mix current pixel with the chosen
// neighbor:
2018-12-01 12:48:29 +01:00
vec4 color = blendingWeight.x * textureLodA(colorTex, blendingCoord.xy, 0.0);
color += blendingWeight.y * textureLodA(colorTex, blendingCoord.zw, 0.0);
2016-06-14 12:33:11 +02:00
2016-07-31 23:25:33 +02:00
#ifdef _Veloc
2016-10-17 17:39:40 +02:00
// Antialias velocity for proper reprojection in a later stage:
2018-12-01 12:48:29 +01:00
vec2 velocity = blendingWeight.x * textureLodA(sveloc, blendingCoord.xy, 0.0).rg;
velocity += blendingWeight.y * textureLodA(sveloc, blendingCoord.zw, 0.0).rg;
2016-06-14 12:33:11 +02:00
2016-10-17 17:39:40 +02:00
// Pack velocity into the alpha channel:
color.a = sqrt(5.0 * length(velocity));
2016-07-31 23:25:33 +02:00
#endif
2016-10-17 17:39:40 +02:00
return color;
}
return vec4(0.0);
2016-06-14 12:33:11 +02:00
}
void main() {
2018-12-01 12:48:29 +01:00
fragColor = SMAANeighborhoodBlendingPS(texCoord, offset);
2016-06-14 12:33:11 +02:00
}