krafix chews through SMAA shaders.

This commit is contained in:
Lubos Lenco 2016-06-14 23:32:44 +02:00
parent ebae263779
commit f5eb1e8165
10 changed files with 511 additions and 323 deletions

View file

@ -4,7 +4,23 @@
precision mediump float;
#endif
#define SMAA_MAX_SEARCH_STEPS_DIAG 8
#define SMAA_AREATEX_MAX_DISTANCE 16
#define SMAA_AREATEX_MAX_DISTANCE_DIAG 20
#define SMAA_AREATEX_PIXEL_SIZE (1.0 / vec2(160.0, 560.0))
#define SMAA_AREATEX_SUBTEX_SIZE (1.0 / 7.0)
#define SMAA_SEARCHTEX_SIZE vec2(66.0, 33.0)
#define SMAA_SEARCHTEX_PACKED_SIZE vec2(64.0, 16.0)
#define SMAA_CORNER_ROUNDING 25
#define SMAA_CORNER_ROUNDING_NORM (float(SMAA_CORNER_ROUNDING) / 100.0)
#define SMAA_AREATEX_SELECT(sample) sample.rg
#define SMAA_SEARCHTEX_SELECT(sample) sample.r
#define SMAA_RT_METRICS vec4(1.0 / 800.0, 1.0 / 600.0, 800.0, 600.0)
#define SMAASampleLevelZeroOffset(tex, coord, offset) textureLod(tex, coord + offset * SMAA_RT_METRICS.xy, 0.0)
#define mad(a, b, c) (a * b + c)
#define saturate(a) clamp(a, 0.0, 1.0)
#define round(a) floor(a + 0.5)
uniform sampler2D colorTex;
uniform sampler2D edgesTex;
@ -14,13 +30,27 @@ uniform sampler2D searchTex;
in vec2 texCoord;
in vec2 pixcoord;
in vec4 offset[3];
// in vec4 offset[3];
in vec4 offset0;
in vec4 offset1;
in vec4 offset2;
// Blending Weight Calculation Pixel Shader (Second Pass)
vec2 cdw_end;
//-----------------------------------------------------------------------------
// Diagonal Search Functions
// Conditional move:
// void SMAAMovc(bvec2 cond, inout vec2 variable, vec2 value) {
// /*SMAA_FLATTEN*/ if (cond.x) variable.x = value.x;
// /*SMAA_FLATTEN*/ 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);
// }
// #if !defined(SMAA_DISABLE_DIAG_DETECTION)
/**
* Allows to decode two binary values from a bilinear-filtered access.
@ -51,37 +81,96 @@ vec4 SMAADecodeDiagBilinearAccess(vec4 e) {
/**
* These functions allows to perform diagonal pattern searches.
*/
vec2 SMAASearchDiag1(sampler2D edgesTex, vec2 texcoord, vec2 dir, out vec2 e) {
vec2 SMAASearchDiag1(/*sampler2D edgesTex,*/ vec2 texcoord, vec2 dir/*, out vec2 e*/) {
vec4 coord = vec4(texcoord, -1.0, 1.0);
vec3 t = vec3(SMAA_RT_METRICS.xy, 1.0);
while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) &&
coord.w > 0.9) {
if (coord.w <= 0.9) return coord.zw; //
if (coord.z >= float(SMAA_MAX_SEARCH_STEPS_DIAG - 1)) return coord.zw; //
// while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) && coord.w > 0.9) {
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
e = textureLod(edgesTex, coord.xy, 0.0).rg;
coord.w = dot(e, vec2(0.5, 0.5));
}
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
// Waiting for loops
if (coord.w <= 0.9) return coord.zw; //
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
if (coord.w <= 0.9) return coord.zw; //
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
if (coord.w <= 0.9) return coord.zw; //
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
if (coord.w <= 0.9) return coord.zw; //
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
if (coord.w <= 0.9) return coord.zw; //
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
if (coord.w <= 0.9) return coord.zw; //
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
// }
return coord.zw;
}
vec2 SMAASearchDiag2(sampler2D edgesTex, vec2 texcoord, vec2 dir, out vec2 e) {
vec2 SMAASearchDiag2(/*sampler2D edgesTex,*/ vec2 texcoord, vec2 dir/*, out vec2 e*/) {
vec4 coord = vec4(texcoord, -1.0, 1.0);
coord.x += 0.25 * SMAA_RT_METRICS.x; // See @SearchDiag2Optimization
vec3 t = vec3(SMAA_RT_METRICS.xy, 1.0);
while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) &&
coord.w > 0.9) {
if (coord.w <= 0.9) return coord.zw; //
if (coord.z >= float(SMAA_MAX_SEARCH_STEPS_DIAG - 1)) return coord.zw; //
// while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) && coord.w > 0.9) {
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
// @SearchDiag2Optimization
// Fetch both edges at once using bilinear filtering:
e = textureLod(edgesTex, coord.xy, 0.0).rg;
e = SMAADecodeDiagBilinearAccess(e);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
cdw_end /*e*/ = SMAADecodeDiagBilinearAccess(cdw_end /*e*/);
// Non-optimized version:
// e.g = SMAASampleLevelZero(edgesTex, coord.xy).g;
// e.r = SMAASampleLevelZeroOffset(edgesTex, coord.xy, ivec2(1, 0)).r;
coord.w = dot(e, vec2(0.5, 0.5));
}
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
// Waiting for loops
if (coord.w <= 0.9) return coord.zw; //
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
cdw_end /*e*/ = SMAADecodeDiagBilinearAccess(cdw_end /*e*/);
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
if (coord.w <= 0.9) return coord.zw; //
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
cdw_end /*e*/ = SMAADecodeDiagBilinearAccess(cdw_end /*e*/);
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
if (coord.w <= 0.9) return coord.zw; //
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
cdw_end /*e*/ = SMAADecodeDiagBilinearAccess(cdw_end /*e*/);
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
if (coord.w <= 0.9) return coord.zw; //
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
cdw_end /*e*/ = SMAADecodeDiagBilinearAccess(cdw_end /*e*/);
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
if (coord.w <= 0.9) return coord.zw; //
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
cdw_end /*e*/ = SMAADecodeDiagBilinearAccess(cdw_end /*e*/);
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
if (coord.w <= 0.9) return coord.zw; //
coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz);
cdw_end /*e*/ = textureLod(edgesTex, coord.xy, 0.0).rg;
cdw_end /*e*/ = SMAADecodeDiagBilinearAccess(cdw_end /*e*/);
coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5));
//}
return coord.zw;
}
@ -89,7 +178,7 @@ vec2 SMAASearchDiag2(sampler2D edgesTex, vec2 texcoord, vec2 dir, out vec2 e) {
* Similar to SMAAArea, this calculates the area corresponding to a certain
* diagonal distance and crossing edges 'e'.
*/
vec2 SMAAAreaDiag(sampler2D areaTex, vec2 dist, vec2 e, float offset) {
vec2 SMAAAreaDiag(/*sampler2D areaTex,*/ vec2 dist, vec2 e, float offset) {
vec2 texcoord = mad(vec2(SMAA_AREATEX_MAX_DISTANCE_DIAG, SMAA_AREATEX_MAX_DISTANCE_DIAG), e, dist);
// We do a scale and bias for mapping to texel space:
@ -108,18 +197,19 @@ vec2 SMAAAreaDiag(sampler2D areaTex, vec2 dist, vec2 e, float offset) {
/**
* This searches for diagonal patterns and returns the corresponding weights.
*/
vec2 SMAACalculateDiagWeights(sampler2D edgesTex, sampler2D areaTex, vec2 texcoord, vec2 e, vec4 subsampleIndices) {
vec2 SMAACalculateDiagWeights(/*sampler2D edgesTex, sampler2D areaTex,*/ vec2 texcoord, vec2 e, vec4 subsampleIndices) {
vec2 weights = vec2(0.0, 0.0);
// Search for the line ends:
vec4 d;
vec2 end;
if (e.r > 0.0) {
d.xz = SMAASearchDiag1(edgesTex, texcoord, vec2(-1.0, 1.0), end);
d.x += float(end.y > 0.9);
} else
d.xz = SMAASearchDiag1(/*edgesTex,*/ texcoord, vec2(-1.0, 1.0)/*, cdw_end*/);
d.x += float(cdw_end.y > 0.9);
}
else {
d.xz = vec2(0.0, 0.0);
d.yw = SMAASearchDiag1(edgesTex, texcoord, vec2(1.0, -1.0), end);
}
d.yw = SMAASearchDiag1(/*edgesTex,*/ texcoord, vec2(1.0, -1.0)/*, cdw_end*/);
//SMAA_BRANCH
if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3
@ -143,19 +233,25 @@ vec2 SMAACalculateDiagWeights(sampler2D edgesTex, sampler2D areaTex, vec2 texcoo
vec2 cc = mad(vec2(2.0, 2.0), c.xz, c.yw);
// Remove the crossing edge if we didn't found the end of the line:
SMAAMovc(bool2(step(0.9, d.zw)), cc, vec2(0.0, 0.0));
// SMAAMovc(bvec2(step(0.9, d.zw)), cc, vec2(0.0, 0.0));
float a1condx = step(0.9, d.z);
float a1condy = step(0.9, d.w);
if (a1condx == 1.0) cc.x = 0.0;
if (a1condy == 1.0) cc.y = 0.0;
// Fetch the areas for this line:
weights += SMAAAreaDiag(areaTex, d.xy, cc, subsampleIndices.z);
weights += SMAAAreaDiag(/*areaTex,*/ d.xy, cc, subsampleIndices.z);
}
// Search for the line ends:
d.xz = SMAASearchDiag2(edgesTex, texcoord, vec2(-1.0, -1.0), end);
d.xz = SMAASearchDiag2(/*edgesTex,*/ texcoord, vec2(-1.0, -1.0)/*, cdw_end*/);
if (SMAASampleLevelZeroOffset(edgesTex, texcoord, ivec2(1, 0)).r > 0.0) {
d.yw = SMAASearchDiag2(edgesTex, texcoord, vec2(1.0, 1.0), end);
d.y += float(end.y > 0.9);
} else
d.yw = SMAASearchDiag2(/*edgesTex,*/ texcoord, vec2(1.0, 1.0)/*, cdw_end*/);
d.y += float(cdw_end.y > 0.9);
}
else {
d.yw = vec2(0.0, 0.0);
}
// SMAA_BRANCH
if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3
@ -168,10 +264,14 @@ vec2 SMAACalculateDiagWeights(sampler2D edgesTex, sampler2D areaTex, vec2 texcoo
vec2 cc = mad(vec2(2.0, 2.0), c.xz, c.yw);
// Remove the crossing edge if we didn't found the end of the line:
SMAAMovc(bool2(step(0.9, d.zw)), cc, vec2(0.0, 0.0));
// SMAAMovc(bvec2(step(0.9, d.zw)), cc, vec2(0.0, 0.0));
float a1condx = step(0.9, d.z);
float a1condy = step(0.9, d.w);
if (a1condx == 1.0) cc.x = 0.0;
if (a1condy == 1.0) cc.y = 0.0;
// Fetch the areas for this line:
weights += SMAAAreaDiag(areaTex, d.xy, cc, subsampleIndices.w).gr;
weights += SMAAAreaDiag(/*areaTex,*/ d.xy, cc, subsampleIndices.w).gr;
}
return weights;
@ -187,15 +287,15 @@ vec2 SMAACalculateDiagWeights(sampler2D edgesTex, sampler2D areaTex, vec2 texcoo
* @PSEUDO_GATHER4), and adds 0, 1 or 2, depending on which edges and
* crossing edges are active.
*/
float SMAASearchLength(sampler2D searchTex, vec2 e, float offset) {
float SMAASearchLength(/*sampler2D searchTex,*/ vec2 e, float offset) {
// The texture is flipped vertically, with left and right cases taking half
// of the space horizontally:
vec2 scale = SMAA_SEARCHTEX_SIZE * vec2(0.5, -1.0);
vec2 bias = SMAA_SEARCHTEX_SIZE * vec2(offset, 1.0);
// Scale and bias to access texel centers:
scale += vec2(-1.0, 1.0);
bias += vec2( 0.5, -0.5);
scale += vec2(-1.0, 1.0);
bias += vec2( 0.5, -0.5);
// Convert from pixel coordinates to texcoords:
// (We use SMAA_SEARCHTEX_PACKED_SIZE because the texture is cropped)
@ -209,7 +309,11 @@ float SMAASearchLength(sampler2D searchTex, vec2 e, float offset) {
/**
* Horizontal/vertical search functions for the 2nd pass.
*/
float SMAASearchXLeft(sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end) {
float endLoopXLeft(vec2 texcoord, vec2 e) {
float offset = mad(-(255.0 / 127.0), SMAASearchLength(/*searchTex,*/ e, 0.0), 3.25);
return mad(SMAA_RT_METRICS.x, offset, texcoord.x);
}
float SMAASearchXLeft(/*sampler2D edgesTex, sampler2D searchTex,*/ vec2 texcoord, float end) {
/**
* @PSEUDO_GATHER4
* This texcoord has been offset by (-0.25, -0.125) in the vertex shader to
@ -218,14 +322,55 @@ float SMAASearchXLeft(sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, fl
* which edges are active from the four fetched ones.
*/
vec2 e = vec2(0.0, 1.0);
while (texcoord.x > end &&
e.g > 0.8281 && // Is there some edge not activated?
e.r == 0.0) { // Or is there a crossing edge that breaks the line?
if (texcoord.x <= end) return endLoopXLeft(texcoord, e); //
if (e.g <= 0.8281) return endLoopXLeft(texcoord, e);
if (e.r != 0.0) return endLoopXLeft(texcoord, e);
// while (texcoord.x > end &&
// e.g > 0.8281 && // Is there some edge not activated?
// e.r == 0.0) { // Or is there a crossing edge that breaks the line?
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
}
// Waiting for loops
if (texcoord.x <= end) return endLoopXLeft(texcoord, e); //
if (e.g <= 0.8281) return endLoopXLeft(texcoord, e);
if (e.r != 0.0) return endLoopXLeft(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.x <= end) return endLoopXLeft(texcoord, e); //
if (e.g <= 0.8281) return endLoopXLeft(texcoord, e);
if (e.r != 0.0) return endLoopXLeft(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.x <= end) return endLoopXLeft(texcoord, e); //
if (e.g <= 0.8281) return endLoopXLeft(texcoord, e);
if (e.r != 0.0) return endLoopXLeft(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.x <= end) return endLoopXLeft(texcoord, e); //
if (e.g <= 0.8281) return endLoopXLeft(texcoord, e);
if (e.r != 0.0) return endLoopXLeft(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.x <= end) return endLoopXLeft(texcoord, e); //
if (e.g <= 0.8281) return endLoopXLeft(texcoord, e);
if (e.r != 0.0) return endLoopXLeft(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.x <= end) return endLoopXLeft(texcoord, e); //
if (e.g <= 0.8281) return endLoopXLeft(texcoord, e);
if (e.r != 0.0) return endLoopXLeft(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.x <= end) return endLoopXLeft(texcoord, e); //
if (e.g <= 0.8281) return endLoopXLeft(texcoord, e);
if (e.r != 0.0) return endLoopXLeft(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
// }
float offset = mad(-(255.0 / 127.0), SMAASearchLength(searchTex, e, 0.0), 3.25);
float offset = mad(-(255.0 / 127.0), SMAASearchLength(/*searchTex,*/ e, 0.0), 3.25);
return mad(SMAA_RT_METRICS.x, offset, texcoord.x);
// Non-optimized version:
@ -237,43 +382,181 @@ float SMAASearchXLeft(sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, fl
// Disambiguate the length added by the last step:
// texcoord.x += 2.0 * SMAA_RT_METRICS.x; // Undo last step
// texcoord.x -= SMAA_RT_METRICS.x * (255.0 / 127.0) * SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.0);
// texcoord.x -= SMAA_RT_METRICS.x * (255.0 / 127.0) * SMAASearchLength(/*searchTex,*/ e, 0.0);
// return mad(SMAA_RT_METRICS.x, offset, texcoord.x);
}
float SMAASearchXRight(sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end) {
float endLoopXRight(vec2 texcoord, vec2 e) {
float offset = mad(-(255.0 / 127.0), SMAASearchLength(/*searchTex,*/ e, 0.5), 3.25);
return mad(-SMAA_RT_METRICS.x, offset, texcoord.x);
}
float SMAASearchXRight(/*sampler2D edgesTex, sampler2D searchTex,*/ vec2 texcoord, float end) {
vec2 e = vec2(0.0, 1.0);
while (texcoord.x < end &&
e.g > 0.8281 && // Is there some edge not activated?
e.r == 0.0) { // Or is there a crossing edge that breaks the line?
if (texcoord.x >= end) return endLoopXRight(texcoord, e); //
if (e.g <= 0.8281) return endLoopXRight(texcoord, e);
if (e.r != 0.0) return endLoopXRight(texcoord, e);
// while (texcoord.x < end &&
// e.g > 0.8281 && // Is there some edge not activated?
// e.r == 0.0) { // Or is there a crossing edge that breaks the line?
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
}
float offset = mad(-(255.0 / 127.0), SMAASearchLength(searchTex, e, 0.5), 3.25);
// Waiting for loops
if (texcoord.x >= end) return endLoopXRight(texcoord, e); //
if (e.g <= 0.8281) return endLoopXRight(texcoord, e);
if (e.r != 0.0) return endLoopXRight(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.x >= end) return endLoopXRight(texcoord, e); //
if (e.g <= 0.8281) return endLoopXRight(texcoord, e);
if (e.r != 0.0) return endLoopXRight(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.x >= end) return endLoopXRight(texcoord, e); //
if (e.g <= 0.8281) return endLoopXRight(texcoord, e);
if (e.r != 0.0) return endLoopXRight(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.x >= end) return endLoopXRight(texcoord, e); //
if (e.g <= 0.8281) return endLoopXRight(texcoord, e);
if (e.r != 0.0) return endLoopXRight(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.x >= end) return endLoopXRight(texcoord, e); //
if (e.g <= 0.8281) return endLoopXRight(texcoord, e);
if (e.r != 0.0) return endLoopXRight(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.x >= end) return endLoopXRight(texcoord, e); //
if (e.g <= 0.8281) return endLoopXRight(texcoord, e);
if (e.r != 0.0) return endLoopXRight(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.x >= end) return endLoopXRight(texcoord, e); //
if (e.g <= 0.8281) return endLoopXRight(texcoord, e);
if (e.r != 0.0) return endLoopXRight(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
// }
float offset = mad(-(255.0 / 127.0), SMAASearchLength(/*searchTex,*/ e, 0.5), 3.25);
return mad(-SMAA_RT_METRICS.x, offset, texcoord.x);
}
float SMAASearchYUp(sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end) {
float endLoopYUp(vec2 texcoord, vec2 e) {
float offset = mad(-(255.0 / 127.0), SMAASearchLength(/*searchTex,*/ e.gr, 0.0), 3.25);
return mad(SMAA_RT_METRICS.y, offset, texcoord.y);
}
float SMAASearchYUp(/*sampler2D edgesTex, sampler2D searchTex,*/ vec2 texcoord, float end) {
vec2 e = vec2(1.0, 0.0);
while (texcoord.y > end &&
e.r > 0.8281 && // Is there some edge not activated?
e.g == 0.0) { // Or is there a crossing edge that breaks the line?
if (texcoord.y <= end) return endLoopYUp(texcoord, e); //
if (e.r <= 0.8281) return endLoopYUp(texcoord, e);
if (e.g != 0.0) return endLoopYUp(texcoord, e);
// while (texcoord.y > end &&
// e.r > 0.8281 && // Is there some edge not activated?
// e.g == 0.0) { // Or is there a crossing edge that breaks the line?
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
}
float offset = mad(-(255.0 / 127.0), SMAASearchLength(searchTex, e.gr, 0.0), 3.25);
// Waiting for loops
if (texcoord.y <= end) return endLoopYUp(texcoord, e); //
if (e.r <= 0.8281) return endLoopYUp(texcoord, e);
if (e.g != 0.0) return endLoopYUp(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.y <= end) return endLoopYUp(texcoord, e); //
if (e.r <= 0.8281) return endLoopYUp(texcoord, e);
if (e.g != 0.0) return endLoopYUp(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.y <= end) return endLoopYUp(texcoord, e); //
if (e.r <= 0.8281) return endLoopYUp(texcoord, e);
if (e.g != 0.0) return endLoopYUp(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.y <= end) return endLoopYUp(texcoord, e); //
if (e.r <= 0.8281) return endLoopYUp(texcoord, e);
if (e.g != 0.0) return endLoopYUp(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.y <= end) return endLoopYUp(texcoord, e); //
if (e.r <= 0.8281) return endLoopYUp(texcoord, e);
if (e.g != 0.0) return endLoopYUp(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.y <= end) return endLoopYUp(texcoord, e); //
if (e.r <= 0.8281) return endLoopYUp(texcoord, e);
if (e.g != 0.0) return endLoopYUp(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.y <= end) return endLoopYUp(texcoord, e); //
if (e.r <= 0.8281) return endLoopYUp(texcoord, e);
if (e.g != 0.0) return endLoopYUp(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(-vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
// }
float offset = mad(-(255.0 / 127.0), SMAASearchLength(/*searchTex,*/ e.gr, 0.0), 3.25);
return mad(SMAA_RT_METRICS.y, offset, texcoord.y);
}
float SMAASearchYDown(sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end) {
float endLoopYDown(vec2 texcoord, vec2 e) {
float offset = mad(-(255.0 / 127.0), SMAASearchLength(/*searchTex,*/ e.gr, 0.5), 3.25);
return mad(-SMAA_RT_METRICS.y, offset, texcoord.y);
}
float SMAASearchYDown(/*sampler2D edgesTex, sampler2D searchTex,*/ vec2 texcoord, float end) {
vec2 e = vec2(1.0, 0.0);
while (texcoord.y < end &&
e.r > 0.8281 && // Is there some edge not activated?
e.g == 0.0) { // Or is there a crossing edge that breaks the line?
if (texcoord.y >= end) return endLoopYDown(texcoord, e); //
if (e.r <= 0.8281) return endLoopYDown(texcoord, e);
if (e.g != 0.0) return endLoopYDown(texcoord, e);
// while (texcoord.y < end &&
// e.r > 0.8281 && // Is there some edge not activated?
// e.g == 0.0) { // Or is there a crossing edge that breaks the line?
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
}
float offset = mad(-(255.0 / 127.0), SMAASearchLength(searchTex, e.gr, 0.5), 3.25);
// Waiting for loops
if (texcoord.y >= end) return endLoopYDown(texcoord, e); //
if (e.r <= 0.8281) return endLoopYDown(texcoord, e);
if (e.g != 0.0) return endLoopYDown(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.y >= end) return endLoopYDown(texcoord, e); //
if (e.r <= 0.8281) return endLoopYDown(texcoord, e);
if (e.g != 0.0) return endLoopYDown(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.y >= end) return endLoopYDown(texcoord, e); //
if (e.r <= 0.8281) return endLoopYDown(texcoord, e);
if (e.g != 0.0) return endLoopYDown(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.y >= end) return endLoopYDown(texcoord, e); //
if (e.r <= 0.8281) return endLoopYDown(texcoord, e);
if (e.g != 0.0) return endLoopYDown(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.y >= end) return endLoopYDown(texcoord, e); //
if (e.r <= 0.8281) return endLoopYDown(texcoord, e);
if (e.g != 0.0) return endLoopYDown(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.y >= end) return endLoopYDown(texcoord, e); //
if (e.r <= 0.8281) return endLoopYDown(texcoord, e);
if (e.g != 0.0) return endLoopYDown(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
if (texcoord.y >= end) return endLoopYDown(texcoord, e); //
if (e.r <= 0.8281) return endLoopYDown(texcoord, e);
if (e.g != 0.0) return endLoopYDown(texcoord, e);
e = textureLod(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
// }
float offset = mad(-(255.0 / 127.0), SMAASearchLength(/*searchTex,*/ e.gr, 0.5), 3.25);
return mad(-SMAA_RT_METRICS.y, offset, texcoord.y);
}
@ -281,7 +564,7 @@ float SMAASearchYDown(sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, fl
* Ok, we have the distance and both crossing edges. So, what are the areas
* at each side of current edge?
*/
vec2 SMAAArea(sampler2D areaTex, vec2 dist, float e1, float e2, float offset) {
vec2 SMAAArea(/*sampler2D areaTex,*/ vec2 dist, float e1, float e2, float offset) {
// Rounding prevents precision errors of bilinear filtering:
vec2 texcoord = mad(vec2(SMAA_AREATEX_MAX_DISTANCE, SMAA_AREATEX_MAX_DISTANCE), round(4.0 * vec2(e1, e2)), dist);
@ -298,7 +581,7 @@ vec2 SMAAArea(sampler2D areaTex, vec2 dist, float e1, float e2, float offset) {
//-----------------------------------------------------------------------------
// Corner Detection Functions
void SMAADetectHorizontalCornerPattern(sampler2D edgesTex, inout vec2 weights, vec4 texcoord, vec2 d) {
vec2 /*void*/ SMAADetectHorizontalCornerPattern(/*sampler2D edgesTex,*/ /*inout*/ vec2 weights, vec4 texcoord, vec2 d) {
// #if !defined(SMAA_DISABLE_CORNER_DETECTION)
vec2 leftRight = step(d.xy, d.yx);
vec2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * leftRight;
@ -312,10 +595,11 @@ void SMAADetectHorizontalCornerPattern(sampler2D edgesTex, inout vec2 weights, v
factor.y -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, ivec2(1, -2)).r;
weights *= saturate(factor);
return weights; //
// #endif
}
void SMAADetectVerticalCornerPattern(sampler2D edgesTex, inout vec2 weights, vec4 texcoord, vec2 d) {
vec2 /*void*/ SMAADetectVerticalCornerPattern(/*sampler2D edgesTex,*/ /*inout*/ vec2 weights, vec4 texcoord, vec2 d) {
//#if !defined(SMAA_DISABLE_CORNER_DETECTION)
vec2 leftRight = step(d.xy, d.yx);
vec2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * leftRight;
@ -329,12 +613,13 @@ void SMAADetectVerticalCornerPattern(sampler2D edgesTex, inout vec2 weights, vec
factor.y -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, ivec2(-2, 1)).g;
weights *= saturate(factor);
return weights; //
// #endif
}
vec4 SMAABlendingWeightCalculationPS(vec2 texcoord, vec2 pixcoord, vec4 offset[3],
sampler2D edgesTex, sampler2D areaTex, sampler2D searchTex,
vec4 SMAABlendingWeightCalculationPS(vec2 texcoord, vec2 pixcoord, /*vec4 offset[3],*/
/*sampler2D edgesTex, sampler2D areaTex, sampler2D searchTex,*/
vec4 subsampleIndices) { // Just pass zero for SMAA 1x, see @SUBSAMPLE_INDICES.
vec4 weights = vec4(0.0, 0.0, 0.0, 0.0);
@ -345,7 +630,7 @@ vec4 SMAABlendingWeightCalculationPS(vec2 texcoord, vec2 pixcoord, vec4 offset[3
//#if !defined(SMAA_DISABLE_DIAG_DETECTION)
// Diagonals have both north and west edges, so searching for them in
// one of the boundaries is enough.
weights.rg = SMAACalculateDiagWeights(edgesTex, areaTex, texcoord, e, subsampleIndices);
weights.rg = SMAACalculateDiagWeights(/*edgesTex, areaTex,*/ texcoord, e, subsampleIndices);
// We give priority to diagonals, so if we find a diagonal we skip
// horizontal/vertical processing.
@ -357,8 +642,8 @@ vec4 SMAABlendingWeightCalculationPS(vec2 texcoord, vec2 pixcoord, vec4 offset[3
// Find the distance to the left:
vec3 coords;
coords.x = SMAASearchXLeft(edgesTex, searchTex, offset[0].xy, offset[2].x);
coords.y = offset[1].y; // offset[1].y = texcoord.y - 0.25 * SMAA_RT_METRICS.y (@CROSSING_OFFSET)
coords.x = SMAASearchXLeft(/*edgesTex, searchTex,*/ offset0.xy, offset2.x);
coords.y = offset1.y; // offset[1].y = texcoord.y - 0.25 * SMAA_RT_METRICS.y (@CROSSING_OFFSET)
d.x = coords.x;
// Now fetch the left crossing edges, two at a time using bilinear
@ -367,7 +652,7 @@ vec4 SMAABlendingWeightCalculationPS(vec2 texcoord, vec2 pixcoord, vec4 offset[3
float e1 = textureLod(edgesTex, coords.xy, 0.0).r;
// Find the distance to the right:
coords.z = SMAASearchXRight(edgesTex, searchTex, offset[0].zw, offset[2].y);
coords.z = SMAASearchXRight(/*edgesTex, searchTex,*/ offset0.zw, offset2.y);
d.y = coords.z;
// We want the distances to be in pixel units (doing this here allow to
@ -379,20 +664,22 @@ vec4 SMAABlendingWeightCalculationPS(vec2 texcoord, vec2 pixcoord, vec4 offset[3
vec2 sqrt_d = sqrt(d);
// Fetch the right crossing edges:
float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.zy, int2(1, 0)).r;
float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.zy, ivec2(1, 0)).r;
// Ok, we know how this pattern looks like, now it is time for getting
// the actual area:
weights.rg = SMAAArea(areaTex, sqrt_d, e1, e2, subsampleIndices.y);
weights.rg = SMAAArea(/*areaTex,*/ sqrt_d, e1, e2, subsampleIndices.y);
// Fix corners:
coords.y = texcoord.y;
SMAADetectHorizontalCornerPattern(edgesTex, weights.rg, coords.xyzy, d);
weights.rg = SMAADetectHorizontalCornerPattern(/*edgesTex,*/ weights.rg, coords.xyzy, d);
//#if !defined(SMAA_DISABLE_DIAG_DETECTION)
} else
}
else {
e.r = 0.0; // Skip vertical processing.
// #endif
}
//#endif
}
//SMAA_BRANCH
@ -401,15 +688,15 @@ vec4 SMAABlendingWeightCalculationPS(vec2 texcoord, vec2 pixcoord, vec4 offset[3
// Find the distance to the top:
vec3 coords;
coords.y = SMAASearchYUp(edgesTex, searchTex, offset[1].xy, offset[2].z);
coords.x = offset[0].x; // offset[1].x = texcoord.x - 0.25 * SMAA_RT_METRICS.x;
coords.y = SMAASearchYUp(/*edgesTex, searchTex,*/ offset1.xy, offset2.z);
coords.x = offset0.x; // offset[1].x = texcoord.x - 0.25 * SMAA_RT_METRICS.x;
d.x = coords.y;
// Fetch the top crossing edges:
float e1 = textureLod(edgesTex, coords.xy, 0.0).g;
// Find the distance to the bottom:
coords.z = SMAASearchYDown(edgesTex, searchTex, offset[1].zw, offset[2].w);
coords.z = SMAASearchYDown(/*edgesTex, searchTex,*/ offset1.zw, offset2.w);
d.y = coords.z;
// We want the distances to be in pixel units:
@ -420,22 +707,20 @@ vec4 SMAABlendingWeightCalculationPS(vec2 texcoord, vec2 pixcoord, vec4 offset[3
vec2 sqrt_d = sqrt(d);
// Fetch the bottom crossing edges:
float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.xz, int2(0, 1)).g;
float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.xz, ivec2(0, 1)).g;
// Get the area for this direction:
weights.ba = SMAAArea(areaTex, sqrt_d, e1, e2, subsampleIndices.x);
weights.ba = SMAAArea(/*areaTex,*/ sqrt_d, e1, e2, subsampleIndices.x);
// Fix corners:
coords.x = texcoord.x;
SMAADetectVerticalCornerPattern(edgesTex, weights.ba, coords.xyxz, d);
weights.ba = SMAADetectVerticalCornerPattern(/*edgesTex,*/ weights.ba, coords.xyxz, d);
}
return weights;
}
void main() {
vec4 col = SMAABlendingWeightCalculationPS(texCoord, pixcoord, offset,
edgesTex, areaTex, searchTex, vec4(0.0));
gl_FragColor = vec4(col);
gl_FragColor = SMAABlendingWeightCalculationPS(texCoord, pixcoord, /*offset,*/
/*edgesTex, areaTex, searchTex,*/ vec4(0.0));
}

View file

@ -5,14 +5,16 @@ precision highp float;
#endif
#define SMAA_RT_METRICS vec4(1.0 / 800.0, 1.0 / 600.0, 800.0, 600.0)
#define mad(a, b, c) (a * b + c)
#define SMAA_MAX_SEARCH_STEPS 16
in vec2 pos;
out vec2 texCoord;
out vec2 pixcoord;
out vec4 offset[3];
// out vec4 offset[3];
out vec4 offset0;
out vec4 offset1;
out vec4 offset2;
const vec2 madd = vec2(0.5, 0.5);
@ -25,13 +27,13 @@ void main() {
pixcoord = texCoord * SMAA_RT_METRICS.zw;
// We will use these offsets for the searches later on (see @PSEUDO_GATHER4):
offset[0] = mad(SMAA_RT_METRICS.xyxy, vec4(-0.25, -0.125, 1.25, -0.125), texCoord.xyxy);
offset[1] = mad(SMAA_RT_METRICS.xyxy, vec4(-0.125, -0.25, -0.125, 1.25), texCoord.xyxy);
offset0 = SMAA_RT_METRICS.xyxy * vec4(-0.25, -0.125, 1.25, -0.125) + texCoord.xyxy;
offset1 = SMAA_RT_METRICS.xyxy * vec4(-0.125, -0.25, -0.125, 1.25) + texCoord.xyxy;
// And these for the searches, they indicate the ends of the loops:
offset[2] = mad(SMAA_RT_METRICS.xxyy,
vec4(-2.0, 2.0, -2.0, 2.0) * float(SMAA_MAX_SEARCH_STEPS),
vec4(offset[0].xz, offset[1].yw));
offset2 = SMAA_RT_METRICS.xxyy *
(vec4(-2.0, 2.0, -2.0, 2.0) * float(SMAA_MAX_SEARCH_STEPS)) +
vec4(offset0.xz, offset1.yw);
// }
gl_Position = vec4(pos.xy, 0.0, 1.0);

View file

@ -1,28 +1,84 @@
/**
* 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.
*/
/**
* _______ ___ ___ ___ ___
* / || \/ | / \ / \
* | (---- | \ / | / ^ \ / ^ \
* \ \ | |\/| | / /_\ \ / /_\ \
* ----) | | | | | / _____ \ / _____ \
* |_______/ |__| |__| /__/ \__\ /__/ \__\
*
* E N H A N C E D
* S U B P I X E L M O R P H O L O G I C A L A N T I A L I A S I N G
*
* http://www.iryoku.com/smaa/
*/
#version 450
#ifdef GL_ES
precision mediump float;
#endif
// #define SMAASampleLevelZeroOffset(tex, coord, offset) textureLod(tex, vec4(coord + offset * SMAA_RT_METRICS.xy, 0.0, 0.0))
#define SMAASampleLevelZeroOffset(tex, coord, offset) textureLod(tex, coord + offset * SMAA_RT_METRICS.xy, 0.0)
#define saturate(a) clamp(a, 0.0, 1.0)
#define mad(a, b, c) (a * b + c)
#define SMAA_THRESHOLD 0.1
#define SMAA_DEPTH_THRESHOLD (0.1 * SMAA_THRESHOLD) // For depth edge detection, depends on the depth range of the scene
#define SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 2.0
uniform sampler2D colorTex;
in vec2 texCoord;
in vec4 offset[3];
// in vec4 offset[3];
in vec4 offset0;
in vec4 offset1;
in vec4 offset2;
// Misc functions
// Gathers current pixel, and the top-left neighbors.
// vec3 SMAAGatherNeighbours(vec2 texcoord/*, vec4 offset[3], sampler2D tex*/) {
// float P = texture(tex, texcoord).r;
// float Pleft = texture(tex, offset0.xy).r;
// float Ptop = texture(tex, offset0.zw).r;
// return vec3(P, Pleft, Ptop);
// }
// Edge Detection Pixel Shaders (First Pass)
// Adjusts the threshold by means of predication.
// vec2 SMAACalculatePredicatedThreshold(vec2 texcoord, vec4 offset[3], sampler2D predicationTex) {
// vec3 neighbours = SMAAGatherNeighbours(texcoord, offset, predicationTex);
// vec2 delta = abs(neighbours.xx - neighbours.yz);
// vec2 edges = step(SMAA_PREDICATION_THRESHOLD, delta);
// return SMAA_PREDICATION_SCALE * SMAA_THRESHOLD * (1.0 - SMAA_PREDICATION_STRENGTH * edges);
// }
/**
* Luma Edge Detection
*
* IMPORTANT NOTICE: luma edge detection requires gamma-corrected colors, and
* thus 'colorTex' should be a non-sRGB texture.
*/
vec2 SMAALumaEdgeDetectionPS(vec2 texcoord, vec4 offset[3], sampler2D colorTex
// Luma Edge Detection
// IMPORTANT NOTICE: luma edge detection requires gamma-corrected colors, and
// thus 'colorTex' should be a non-sRGB texture.
vec2 SMAALumaEdgeDetectionPS(vec2 texcoord/*, vec4 offset[3], sampler2D colorTex*/
//#if SMAA_PREDICATION
//, sampler2D predicationTex
//#endif
@ -38,8 +94,8 @@ vec2 SMAALumaEdgeDetectionPS(vec2 texcoord, vec4 offset[3], sampler2D colorTex
vec3 weights = vec3(0.2126, 0.7152, 0.0722);
float L = dot(texture(colorTex, texcoord).rgb, weights);
float Lleft = dot(texture(colorTex, offset[0].xy).rgb, weights);
float Ltop = dot(texture(colorTex, offset[0].zw).rgb, weights);
float Lleft = dot(texture(colorTex, offset0.xy).rgb, weights);
float Ltop = dot(texture(colorTex, offset0.zw).rgb, weights);
// We do the usual threshold:
vec4 delta;
@ -51,16 +107,16 @@ vec2 SMAALumaEdgeDetectionPS(vec2 texcoord, vec4 offset[3], sampler2D colorTex
discard;
// Calculate right and bottom deltas:
float Lright = dot(texture(colorTex, offset[1].xy).rgb, weights);
float Lbottom = dot(texture(colorTex, offset[1].zw).rgb, weights);
float Lright = dot(texture(colorTex, offset1.xy).rgb, weights);
float Lbottom = dot(texture(colorTex, offset1.zw).rgb, weights);
delta.zw = abs(L - vec2(Lright, Lbottom));
// Calculate the maximum delta in the direct neighborhood:
vec2 maxDelta = max(delta.xy, delta.zw);
// Calculate left-left and top-top deltas:
float Lleftleft = dot(texture(colorTex, offset[2].xy).rgb, weights);
float Ltoptop = dot(texture(colorTex, offset[2].zw).rgb, weights);
float Lleftleft = dot(texture(colorTex, offset2.xy).rgb, weights);
float Ltoptop = dot(texture(colorTex, offset2.zw).rgb, weights);
delta.zw = abs(vec2(Lleft, Ltop) - vec2(Lleftleft, Ltoptop));
// Calculate the final maximum delta:
@ -73,13 +129,10 @@ vec2 SMAALumaEdgeDetectionPS(vec2 texcoord, vec4 offset[3], sampler2D colorTex
return edges;
}
/**
* Color Edge Detection
*
* IMPORTANT NOTICE: color edge detection requires gamma-corrected colors, and
* thus 'colorTex' should be a non-sRGB texture.
*/
vec2 SMAAColorEdgeDetectionPS(vec2 texcoord, vec4 offset[3], sampler2D colorTex
// Color Edge Detection
// IMPORTANT NOTICE: color edge detection requires gamma-corrected colors, and
// thus 'colorTex' should be a non-sRGB texture.
vec2 SMAAColorEdgeDetectionPS(vec2 texcoord/*, vec4 offset[3], sampler2D colorTex*/
//#if SMAA_PREDICATION
//, sampler2D predicationTex
//#endif
@ -95,11 +148,11 @@ vec2 SMAAColorEdgeDetectionPS(vec2 texcoord, vec4 offset[3], sampler2D colorTex
vec4 delta;
vec3 C = texture(colorTex, texcoord).rgb;
vec3 Cleft = texture(colorTex, offset[0].xy).rgb;
vec3 Cleft = texture(colorTex, offset0.xy).rgb;
vec3 t = abs(C - Cleft);
delta.x = max(max(t.r, t.g), t.b);
vec3 Ctop = texture(colorTex, offset[0].zw).rgb;
vec3 Ctop = texture(colorTex, offset0.zw).rgb;
t = abs(C - Ctop);
delta.y = max(max(t.r, t.g), t.b);
@ -111,11 +164,11 @@ vec2 SMAAColorEdgeDetectionPS(vec2 texcoord, vec4 offset[3], sampler2D colorTex
discard;
// Calculate right and bottom deltas:
vec3 Cright = texture(colorTex, offset[1].xy).rgb;
vec3 Cright = texture(colorTex, offset1.xy).rgb;
t = abs(C - Cright);
delta.z = max(max(t.r, t.g), t.b);
vec3 Cbottom = texture(colorTex, offset[1].zw).rgb;
vec3 Cbottom = texture(colorTex, offset1.zw).rgb;
t = abs(C - Cbottom);
delta.w = max(max(t.r, t.g), t.b);
@ -123,11 +176,11 @@ vec2 SMAAColorEdgeDetectionPS(vec2 texcoord, vec4 offset[3], sampler2D colorTex
vec2 maxDelta = max(delta.xy, delta.zw);
// Calculate left-left and top-top deltas:
vec3 Cleftleft = texture(colorTex, offset[2].xy).rgb;
vec3 Cleftleft = texture(colorTex, offset2.xy).rgb;
t = abs(C - Cleftleft);
delta.z = max(max(t.r, t.g), t.b);
vec3 Ctoptop = texture(colorTex, offset[2].zw).rgb;
vec3 Ctoptop = texture(colorTex, offset2.zw).rgb;
t = abs(C - Ctoptop);
delta.w = max(max(t.r, t.g), t.b);
@ -141,21 +194,19 @@ vec2 SMAAColorEdgeDetectionPS(vec2 texcoord, vec4 offset[3], sampler2D colorTex
return edges;
}
/**
* Depth Edge Detection
*/
vec2 SMAADepthEdgeDetectionPS(vec2 texcoord, vec4 offset[3], sampler2D depthTex) {
vec3 neighbours = SMAAGatherNeighbours(texcoord, offset, depthTex);
vec2 delta = abs(neighbours.xx - vec2(neighbours.y, neighbours.z));
vec2 edges = step(SMAA_DEPTH_THRESHOLD, delta);
// Depth Edge Detection
// vec2 SMAADepthEdgeDetectionPS(vec2 texcoord, /*vec4 offset[3],*/ sampler2D depthTex) {
// vec3 neighbours = SMAAGatherNeighbours(texcoord, /*offset,*/ depthTex);
// vec2 delta = abs(neighbours.xx - vec2(neighbours.y, neighbours.z));
// vec2 edges = step(SMAA_DEPTH_THRESHOLD, delta);
if (dot(edges, vec2(1.0, 1.0)) == 0.0)
discard;
// if (dot(edges, vec2(1.0, 1.0)) == 0.0)
// discard;
return edges;
}
// return edges;
// }
void main() {
vec2 edges = SMAALumaEdgeDetectionPS(texCoord, offset, colorTex);
gl_FragColor.rg = edges;
// gl_FragColor.rg = SMAALumaEdgeDetectionPS(texCoord/*, offset, colorTex*/);
gl_FragColor.rg = SMAAColorEdgeDetectionPS(texCoord/*, offset, colorTex*/);
}

View file

@ -5,12 +5,14 @@ precision highp float;
#endif
#define SMAA_RT_METRICS vec4(1.0 / 800.0, 1.0 / 600.0, 800.0, 600.0)
#define mad(a, b, c) (a * b + c)
in vec2 pos;
out vec2 texCoord;
out vec4 offset[3];
// out vec4 offset[3];
out vec4 offset0;
out vec4 offset1;
out vec4 offset2;
const vec2 madd = vec2(0.5, 0.5);
@ -20,9 +22,9 @@ void main() {
// Edge Detection Vertex Shader
//void SMAAEdgeDetectionVS(vec2 texcoord, out vec4 offset[3]) {
offset[0] = mad(SMAA_RT_METRICS.xyxy, vec4(-1.0, 0.0, 0.0, -1.0), texCoord.xyxy);
offset[1] = mad(SMAA_RT_METRICS.xyxy, vec4( 1.0, 0.0, 0.0, 1.0), texCoord.xyxy);
offset[2] = mad(SMAA_RT_METRICS.xyxy, vec4(-2.0, 0.0, 0.0, -2.0), texCoord.xyxy);
offset0 = SMAA_RT_METRICS.xyxy * vec4(-1.0, 0.0, 0.0, -1.0) + texCoord.xyxy;
offset1 = SMAA_RT_METRICS.xyxy * vec4( 1.0, 0.0, 0.0, 1.0) + texCoord.xyxy;
offset2 = SMAA_RT_METRICS.xyxy * vec4(-2.0, 0.0, 0.0, -2.0) + texCoord.xyxy;
//}
gl_Position = vec4(pos.xy, 0.0, 1.0);

View file

@ -1,138 +0,0 @@
/**
* 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.
*/
/**
* _______ ___ ___ ___ ___
* / || \/ | / \ / \
* | (---- | \ / | / ^ \ / ^ \
* \ \ | |\/| | / /_\ \ / /_\ \
* ----) | | | | | / _____ \ / _____ \
* |_______/ |__| |__| /__/ \__\ /__/ \__\
*
* E N H A N C E D
* S U B P I X E L M O R P H O L O G I C A L A N T I A L I A S I N G
*
* http://www.iryoku.com/smaa/
*/
#version 450
#ifdef GL_ES
precision mediump float;
#endif
#define SMAA_RT_METRICS vec4(1.0 / 800.0, 1.0 / 600.0, 800.0, 600.0)
// #define SMAA_GLSL_3
#define SMAA_PRESET_HIGH
// #include "SMAA.h"
// #define SMAA_AREATEX_SELECT(sample) sample.rg
// #define SMAA_SEARCHTEX_SELECT(sample) sample.r
// #define SMAA_DECODE_VELOCITY(sample) sample.rg
// #if defined(SMAA_PRESET_LOW)
// #define SMAA_THRESHOLD 0.15
// #define SMAA_MAX_SEARCH_STEPS 4
// #define SMAA_DISABLE_DIAG_DETECTION
// #define SMAA_DISABLE_CORNER_DETECTION
// #elif defined(SMAA_PRESET_MEDIUM)
// #define SMAA_THRESHOLD 0.1
// #define SMAA_MAX_SEARCH_STEPS 8
// #define SMAA_DISABLE_DIAG_DETECTION
// #define SMAA_DISABLE_CORNER_DETECTION
// #elif defined(SMAA_PRESET_HIGH)
#define SMAA_THRESHOLD 0.1
#define SMAA_DEPTH_THRESHOLD (0.1 * SMAA_THRESHOLD) // For depth edge detection, depends on the depth range of the scene
#define SMAA_MAX_SEARCH_STEPS 16
// Define SMAA_DISABLE_DIAG_DETECTION to disable diagonal processing
#define SMAA_MAX_SEARCH_STEPS_DIAG 8
// Define SMAA_DISABLE_CORNER_DETECTION to disable corner processing
#define SMAA_CORNER_ROUNDING 25
// If there is an neighbor edge that has SMAA_LOCAL_CONTRAST_FACTOR times bigger contrast than current edge, current edge will be discarded
#define SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 2.0
// Predicated thresholding allows to better preserve texture details and to improve performance
#define SMAA_PREDICATION 0
// Threshold to be used in the additional predication buffer
#define SMAA_PREDICATION_THRESHOLD 0.01
// How much to scale the global threshold used for luma or color edge detection when using predication
#define SMAA_PREDICATION_SCALE 2.0
// How much to locally decrease the threshold
#define SMAA_PREDICATION_STRENGTH 0.4
// Temporal reprojection allows to remove ghosting artifacts when using temporal supersampling
#define SMAA_REPROJECTION 0
// SMAA_REPROJECTION_WEIGHT_SCALE controls the velocity weighting
#define SMAA_REPROJECTION_WEIGHT_SCALE 30.0
// #elif defined(SMAA_PRESET_ULTRA)
// #define SMAA_THRESHOLD 0.05
// #define SMAA_MAX_SEARCH_STEPS 32
// #define SMAA_MAX_SEARCH_STEPS_DIAG 16
// #define SMAA_CORNER_ROUNDING 25
// #endif
// Non-Configurable Defines
#define SMAA_AREATEX_MAX_DISTANCE 16
#define SMAA_AREATEX_MAX_DISTANCE_DIAG 20
#define SMAA_AREATEX_PIXEL_SIZE (1.0 / vec2(160.0, 560.0))
#define SMAA_AREATEX_SUBTEX_SIZE (1.0 / 7.0)
#define SMAA_SEARCHTEX_SIZE vec2(66.0, 33.0)
#define SMAA_SEARCHTEX_PACKED_SIZE vec2(64.0, 16.0)
#define SMAA_CORNER_ROUNDING_NORM (float(SMAA_CORNER_ROUNDING) / 100.0)
#define SMAA_FLATTEN
#define SMAA_BRANCH
// #define lerp(a, b, t) mix(a, b, t)
// #define saturate(a) clamp(a, 0.0, 1.0)
// #define mad(a, b, c) (a * b + c)
uniform sampler2D tex;
in vec2 texCoord;
//-----------------------------------------------------------------------------
// Misc functions
// Gathers current pixel, and the top-left neighbors.
vec3 SMAAGatherNeighbours(vec2 texcoord, vec4 offset[3], sampler2D tex) {
float P = texture(tex, texcoord).r;
float Pleft = texture(tex, offset[0].xy).r;
float Ptop = texture(tex, offset[0].zw).r;
return vec3(P, Pleft, Ptop);
}
// Adjusts the threshold by means of predication.
vec2 SMAACalculatePredicatedThreshold(vec2 texcoord, vec4 offset[3], sampler2D predicationTex) {
vec3 neighbours = SMAAGatherNeighbours(texcoord, offset, predicationTex);
vec2 delta = abs(neighbours.xx - neighbours.yz);
vec2 edges = step(SMAA_PREDICATION_THRESHOLD, delta);
return SMAA_PREDICATION_SCALE * SMAA_THRESHOLD * (1.0 - SMAA_PREDICATION_STRENGTH * edges);
}
void main() {
// gl_FragColor = vec4(col);
}

View file

@ -1,18 +0,0 @@
#version 450
#ifdef GL_ES
precision highp float;
#endif
in vec2 pos;
out vec2 texCoord;
const vec2 madd = vec2(0.5, 0.5);
void main() {
// Scale vertex attribute to [0-1] range
texCoord = pos.xy * madd + madd;
gl_Position = vec4(pos.xy, 0.0, 1.0);
}

View file

@ -4,11 +4,10 @@
precision mediump float;
#endif
uniform sampler2D tex;
uniform sampler2D colorTex;
in vec2 texCoord;
// Separate Multisamples Pixel Shader (Optional Pass)
#ifdef SMAALoad
@ -23,7 +22,6 @@ void SMAASeparatePS(vec4 position,
}
#endif
void main() {
// gl_FragColor = vec4(col);
}

View file

@ -4,13 +4,11 @@
precision mediump float;
#endif
uniform sampler2D tex;
uniform sampler2D colorTex;
in vec2 texCoord;
// Temporal Resolve Pixel Shader (Optional Pass)
vec4 SMAAResolvePS(vec2 texcoord, sampler2D currentColorTex, sampler2D previousColorTex
//#if SMAA_REPROJECTION
//, sampler2D velocityTex
@ -41,7 +39,6 @@ vec4 SMAAResolvePS(vec2 texcoord, sampler2D currentColorTex, sampler2D previousC
// #endif
}
void main() {
// gl_FragColor = vec4(col);
}

View file

@ -6,25 +6,26 @@ precision mediump float;
#define SMAA_RT_METRICS vec4(1.0 / 800.0, 1.0 / 600.0, 800.0, 600.0)
uniform sampler2D tex;
uniform sampler2D colorTex;
uniform sampler2D blendTex;
in vec2 texCoord;
in vec2 offset;
in vec4 offset;
//-----------------------------------------------------------------------------
// Neighborhood Blending Pixel Shader (Third Pass)
// Conditional move:
void SMAAMovc(bvec2 cond, inout vec2 variable, vec2 value) {
/*SMAA_FLATTEN*/ if (cond.x) variable.x = value.x;
/*SMAA_FLATTEN*/ 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);
}
// void SMAAMovc(bvec2 cond, inout vec2 variable, vec2 value) {
// /*SMAA_FLATTEN*/ if (cond.x) variable.x = value.x;
// /*SMAA_FLATTEN*/ 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);
//}
vec4 SMAANeighborhoodBlendingPS(vec2 texcoord, vec4 offset, sampler2D colorTex, sampler2D blendTex
vec4 SMAANeighborhoodBlendingPS(vec2 texcoord, vec4 offset/*, sampler2D colorTex, sampler2D blendTex*/
//#if SMAA_REPROJECTION
//, sampler2D velocityTex
//#endif
@ -54,12 +55,21 @@ vec4 SMAANeighborhoodBlendingPS(vec2 texcoord, vec4 offset, sampler2D colorTex,
// 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);
//SMAAMovc(bvec4(h, h, h, h), blendingOffset, vec4(a.x, 0.0, a.z, 0.0));
if (h) blendingOffset.x = a.x;
if (h) blendingOffset.y = 0.0;
if (h) blendingOffset.z = a.z;
if (h) blendingOffset.w = 0.0;
// SMAAMovc(bvec2(h, h), blendingWeight, a.xz);
if (h) blendingWeight.x = a.x;
if (h) blendingWeight.y = a.z;
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);
vec4 blendingCoord = blendingOffset * vec4(SMAA_RT_METRICS.xy, -SMAA_RT_METRICS.xy) + texcoord.xyxy;
// We exploit bilinear filtering to mix current pixel with the chosen
// neighbor:
@ -77,9 +87,9 @@ vec4 SMAANeighborhoodBlendingPS(vec2 texcoord, vec4 offset, sampler2D colorTex,
return color;
}
return vec4(0.0);
}
void main() {
vec4 col = SMAANeighborhoodBlendingPS(texCoord, offset, colorTex, blendTex);
gl_FragColor = col;
gl_FragColor = SMAANeighborhoodBlendingPS(texCoord, offset/*, colorTex, blendTex*/);
}

View file

@ -5,7 +5,6 @@ precision highp float;
#endif
#define SMAA_RT_METRICS vec4(1.0 / 800.0, 1.0 / 600.0, 800.0, 600.0)
#define mad(a, b, c) (a * b + c)
in vec2 pos;
@ -20,7 +19,7 @@ void main() {
// Neighborhood Blending Vertex Shader
//void SMAANeighborhoodBlendingVS(vec2 texcoord, out vec4 offset) {
offset = mad(SMAA_RT_METRICS.xyxy, vec4( 1.0, 0.0, 0.0, 1.0), texCoord.xyxy);
offset = SMAA_RT_METRICS.xyxy * vec4( 1.0, 0.0, 0.0, 1.0) + texCoord.xyxy;
//}
gl_Position = vec4(pos.xy, 0.0, 1.0);