diff --git a/assets/shaders/5xBR-DU.fsh b/assets/shaders/5xBR-DU.fsh index 31eb1713f1..f5d9b46197 100644 --- a/assets/shaders/5xBR-DU.fsh +++ b/assets/shaders/5xBR-DU.fsh @@ -29,6 +29,9 @@ precision mediump float; precision mediump int; #endif +#define X_FACTOR 0.7991 // Default: 0.7991, for non-stretched use 0.9981 +#define Y_FACTOR 0.8221 // Default: 0.8221 + #define CornerA 0 //ON:1/OFF:0 / A, B, C, D are just different variants of corner rounding #define CornerB 0 //ON:1/OFF:0 / activate only one #define CornerD 0 //ON:1/OFF:0 @@ -83,25 +86,25 @@ float c_df(vec3 c1, vec3 c2) { void main() { // PSOne SFA3MAX factor I used 0.81 for widescreen, but 1.04 for normal - float sampleMod = 0.802;//0.81; // Reduce for lower res textures, increase for higher res textures - - - float scaleFactor = 480.0 * u_pixelDelta.x * sampleMod; // increase or decrease to match textures - bool upscale = scaleFactor < 0.5; - vec3 res = texture2D(sampler0, v_texcoord0.xy).xyz; + //float sampleMod = 0.802;//0.81; // Reduce for lower res textures, increase for higher res textures - // Let's skip the whole scaling if output size smaller than 1.6x of input size - if (upscale) { + + float sampleModX = X_FACTOR; //0.7991 + float sampleModY = Y_FACTOR; //0.8221 + vec2 scaleFactor = vec2((480.0 * u_pixelDelta.x * sampleModX),(270.0 * u_pixelDelta.y * sampleModY)); + + + vec3 res = texture2D(sampler0, v_texcoord0.xy).xyz; vec4 edri, edr, edr_l, edr_u, px; // px = pixel, edr = edge detection rule vec4 irlv0, irlv1, irlv2l, irlv2u; vec4 fx, fx_l, fx_u; // inequations of straight lines. - vec2 pS = 1.0 / (u_pixelDelta.xy / scaleFactor); + vec2 pS = vec2(1.0 / (u_pixelDelta.x / scaleFactor.x), 1.0 / (u_pixelDelta.y / scaleFactor.y)); vec2 fp = fract(v_texcoord0.xy*pS.xy); - vec2 TexCoord_0 = v_texcoord0.xy-fp*(u_pixelDelta.xy / scaleFactor);// / scaleFactor);//vec2(scaleFactor, scaleFactor).xy); - vec2 dx = vec2(u_pixelDelta.x / scaleFactor,0.0); - vec2 dy = vec2(0.0,u_pixelDelta.y / scaleFactor); + vec2 TexCoord_0 = v_texcoord0.xy-fp.xy*vec2(u_pixelDelta.x / scaleFactor.x, u_pixelDelta.y / scaleFactor.y).xy; + vec2 dx = vec2(u_pixelDelta.x / scaleFactor.x,0.0); + vec2 dy = vec2(0.0,u_pixelDelta.y / scaleFactor.y); vec2 y2 = dy + dy; vec2 x2 = dx + dx; vec4 delta = vec4(1.0/XBR_SCALE, 1.0/XBR_SCALE, 1.0/XBR_SCALE, 1.0/XBR_SCALE); @@ -215,7 +218,7 @@ void main() { res2 = mix(res2, mix(D, H, px.w), maximos.w); res = mix(res1, res2, step(c_df(E, res1), c_df(E, res2))); - } + gl_FragColor.xyz = res; gl_FragColor.a = 1.0; } diff --git a/assets/shaders/5xBR-DUns.fsh b/assets/shaders/5xBR-DUns.fsh new file mode 100644 index 0000000000..bb1b620aa5 --- /dev/null +++ b/assets/shaders/5xBR-DUns.fsh @@ -0,0 +1,224 @@ +/* + Hyllian's xBR-lv2 Shader Accuracy (tweak by guest.r) + + Copyright (C) 2011-2015 Hyllian - sergiogdb@gmail.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of 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. + + 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. + + Incorporates some of the ideas from SABR shader. Thanks to Joshua Street. +*/ + +#ifdef GL_ES +precision mediump float; +precision mediump int; +#endif + +#define X_FACTOR 0.9981 // Default: 0.7991, for non-stretched use 0.9981 +#define Y_FACTOR 0.8221 // Default: 0.8221 + +#define CornerA 0 //ON:1/OFF:0 / A, B, C, D are just different variants of corner rounding +#define CornerB 0 //ON:1/OFF:0 / activate only one +#define CornerD 0 //ON:1/OFF:0 +// CornerC //used as default if none of the above is defined + +const float XBR_SCALE = 3.0; +const float lv2_cf = 2.0; + +const float coef = 2.0; +const vec3 rgbw = vec3(14.352, 28.176, 5.472); +const vec4 eq_threshold = vec4(15.0, 15.0, 15.0, 15.0); + +const vec4 Ao = vec4( 1.0, -1.0, -1.0, 1.0 ); +const vec4 Bo = vec4( 1.0, 1.0, -1.0,-1.0 ); +const vec4 Co = vec4( 1.5, 0.5, -0.5, 0.5 ); +const vec4 Ax = vec4( 1.0, -1.0, -1.0, 1.0 ); +const vec4 Bx = vec4( 0.5, 2.0, -0.5,-2.0 ); +const vec4 Cx = vec4( 1.0, 1.0, -0.5, 0.0 ); +const vec4 Ay = vec4( 1.0, -1.0, -1.0, 1.0 ); +const vec4 By = vec4( 2.0, 0.5, -2.0,-0.5 ); +const vec4 Cy = vec4( 2.0, 0.0, -1.0, 0.5 ); +const vec4 Ci = vec4(0.25, 0.25, 0.25, 0.25); + +uniform sampler2D sampler0; +uniform vec2 u_pixelDelta; +varying vec2 v_texcoord0; + +// Difference between vector components. +vec4 df(vec4 A, vec4 B) { + return vec4(abs(A-B)); +} + +// Compare two vectors and return their components are different. +vec4 diff(vec4 A, vec4 B) { + return vec4(notEqual(A, B)); +} + +// Determine if two vector components are equal based on a threshold. +vec4 eq(vec4 A, vec4 B) { + return (step(df(A, B), eq_threshold)); +} + +// Determine if two vector components are NOT equal based on a threshold. +vec4 neq(vec4 A, vec4 B) { + return (vec4(1.0, 1.0, 1.0, 1.0) - eq(A, B)); +} + +float c_df(vec3 c1, vec3 c2) { + vec3 df = abs(c1 - c2); + return df.r + df.g + df.b; +} + +void main() { +// PSOne SFA3MAX factor I used 0.81 for widescreen, but 1.04 for normal + //float sampleMod = 0.802;//0.81; // Reduce for lower res textures, increase for higher res textures + + + float sampleModX = X_FACTOR; //0.7991 + float sampleModY = Y_FACTOR; //0.8221 + vec2 scaleFactor = vec2((480.0 * u_pixelDelta.x * sampleModX),(270.0 * u_pixelDelta.y * sampleModY)); + + + vec3 res = texture2D(sampler0, v_texcoord0.xy).xyz; + + vec4 edri, edr, edr_l, edr_u, px; // px = pixel, edr = edge detection rule + vec4 irlv0, irlv1, irlv2l, irlv2u; + vec4 fx, fx_l, fx_u; // inequations of straight lines. + + vec2 pS = vec2(1.0 / (u_pixelDelta.x / scaleFactor.x), 1.0 / (u_pixelDelta.y / scaleFactor.y)); + vec2 fp = fract(v_texcoord0.xy*pS.xy); + vec2 TexCoord_0 = v_texcoord0.xy-fp.xy*vec2(u_pixelDelta.x / scaleFactor.x, u_pixelDelta.y / scaleFactor.y).xy; + vec2 dx = vec2(u_pixelDelta.x / scaleFactor.x,0.0); + vec2 dy = vec2(0.0,u_pixelDelta.y / scaleFactor.y); + vec2 y2 = dy + dy; vec2 x2 = dx + dx; + + vec4 delta = vec4(1.0/XBR_SCALE, 1.0/XBR_SCALE, 1.0/XBR_SCALE, 1.0/XBR_SCALE); + vec4 delta_l = vec4(0.5/XBR_SCALE, 1.0/XBR_SCALE, 0.5/XBR_SCALE, 1.0/XBR_SCALE); + vec4 delta_u = delta_l.yxwz; + + vec3 A = texture2D(sampler0, TexCoord_0 -dx -dy ).xyz; + vec3 B = texture2D(sampler0, TexCoord_0 -dy ).xyz; + vec3 C = texture2D(sampler0, TexCoord_0 +dx -dy ).xyz; + vec3 D = texture2D(sampler0, TexCoord_0 -dx ).xyz; + vec3 E = texture2D(sampler0, TexCoord_0 ).xyz; + vec3 F = texture2D(sampler0, TexCoord_0 +dx ).xyz; + vec3 G = texture2D(sampler0, TexCoord_0 -dx +dy ).xyz; + vec3 H = texture2D(sampler0, TexCoord_0 +dy ).xyz; + vec3 I = texture2D(sampler0, TexCoord_0 +dx +dy ).xyz; + vec3 A1 = texture2D(sampler0, TexCoord_0 -dx -y2).xyz; + vec3 C1 = texture2D(sampler0, TexCoord_0 +dx -y2).xyz; + vec3 A0 = texture2D(sampler0, TexCoord_0 -x2 -dy).xyz; + vec3 G0 = texture2D(sampler0, TexCoord_0 -x2 +dy).xyz; + vec3 C4 = texture2D(sampler0, TexCoord_0 +x2 -dy).xyz; + vec3 I4 = texture2D(sampler0, TexCoord_0 +x2 +dy).xyz; + vec3 G5 = texture2D(sampler0, TexCoord_0 -dx +y2).xyz; + vec3 I5 = texture2D(sampler0, TexCoord_0 +dx +y2).xyz; + vec3 B1 = texture2D(sampler0, TexCoord_0 -y2).xyz; + vec3 D0 = texture2D(sampler0, TexCoord_0 -x2 ).xyz; + vec3 H5 = texture2D(sampler0, TexCoord_0 +y2).xyz; + vec3 F4 = texture2D(sampler0, TexCoord_0 +x2 ).xyz; + + vec4 b = vec4(dot(B ,rgbw), dot(D ,rgbw), dot(H ,rgbw), dot(F ,rgbw)); + vec4 c = vec4(dot(C ,rgbw), dot(A ,rgbw), dot(G ,rgbw), dot(I ,rgbw)); + vec4 d = b.yzwx; + vec4 e = vec4(dot(E,rgbw)); + vec4 f = b.wxyz; + vec4 g = c.zwxy; + vec4 h = b.zwxy; + vec4 i = c.wxyz; + vec4 i4 = vec4(dot(I4,rgbw), dot(C1,rgbw), dot(A0,rgbw), dot(G5,rgbw)); + vec4 i5 = vec4(dot(I5,rgbw), dot(C4,rgbw), dot(A1,rgbw), dot(G0,rgbw)); + vec4 h5 = vec4(dot(H5,rgbw), dot(F4,rgbw), dot(B1,rgbw), dot(D0,rgbw)); + vec4 f4 = h5.yzwx; + + // These inequations define the line below which interpolation occurs. + fx = (Ao*fp.y+Bo*fp.x); + fx_l = (Ax*fp.y+Bx*fp.x); + fx_u = (Ay*fp.y+By*fp.x); + irlv1 = irlv0 = diff(e,f) * diff(e,h); +#if(CornerA==0) // A takes priority skipping other corners + #define SMOOTH_TIPS + // Corner C also default if no other ones used + irlv1 = (irlv0 * ( neq(f,b) * neq(f,c) + neq(h,d) * neq(h,g) + eq(e,i) * (neq(f,f4) * neq(f,i4) + neq(h,h5) * neq(h,i5)) + eq(e,g) + eq(e,c)) ); + int select1 = 0; +#if(CornerB==1) // Corner B + irlv1 = (irlv0 * ( neq(f,b) * neq(h,d) + eq(e,i) * neq(f,i4) * neq(h,i5) + eq(e,g) + eq(e,c) ) ); + select1 = 1; +#endif +#if(CornerD==1) // Corner D + if (select1==0) { + vec4 c1 = i4.yzwx; + vec4 g0 = i5.wxyz; + irlv1 = (irlv0 * ( neq(f,b) * neq(h,d) + eq(e,i) * neq(f,i4) * neq(h,i5) + eq(e,g) + eq(e,c) ) * (diff(f,f4) * diff(f,i) + diff(h,h5) * diff(h,i) + diff(h,g) + diff(f,c) + eq(b,c1) * eq(d,g0))); + } +#endif +#endif + irlv2l = diff(e,g) * diff(d,g); + irlv2u = diff(e,c) * diff(b,c); + + vec4 fx45i = clamp((fx + delta -Co - Ci) / (2.0*delta ), 0.0, 1.0); + vec4 fx45 = clamp((fx + delta -Co ) / (2.0*delta ), 0.0, 1.0); + vec4 fx30 = clamp((fx_l + delta_l -Cx ) / (2.0*delta_l), 0.0, 1.0); + vec4 fx60 = clamp((fx_u + delta_u -Cy ) / (2.0*delta_u), 0.0, 1.0); + vec4 w1, w2; + + w1.x = dot(abs(E-C),rgbw) + dot(abs(E-G),rgbw) + dot(abs(I-H5),rgbw) + dot(abs(I-F4),rgbw) + 4.0*dot(abs(H-F),rgbw); + w1.y = dot(abs(E-A),rgbw) + dot(abs(E-I),rgbw) + dot(abs(C-F4),rgbw) + dot(abs(C-B1),rgbw) + 4.0*dot(abs(F-B),rgbw); + w1.z = dot(abs(E-G),rgbw) + dot(abs(E-C),rgbw) + dot(abs(A-B1),rgbw) + dot(abs(A-D0),rgbw) + 4.0*dot(abs(B-D),rgbw); + w1.w = dot(abs(E-I),rgbw) + dot(abs(E-A),rgbw) + dot(abs(G-D0),rgbw) + dot(abs(G-H5),rgbw) + 4.0*dot(abs(D-H),rgbw); + w2.x = dot(abs(H-D),rgbw) + dot(abs(H-I5),rgbw) + dot(abs(F-I4),rgbw) + dot(abs(F-B),rgbw) + 4.0*dot(abs(E-I),rgbw); + w2.y = dot(abs(F-H),rgbw) + dot(abs(F-C4),rgbw) + dot(abs(B-C1),rgbw) + dot(abs(B-D),rgbw) + 4.0*dot(abs(E-C),rgbw); + w2.z = dot(abs(B-F),rgbw) + dot(abs(B-A1),rgbw) + dot(abs(D-A0),rgbw) + dot(abs(D-H),rgbw) + 4.0*dot(abs(E-A),rgbw); + w2.w = dot(abs(D-B),rgbw) + dot(abs(D-G0),rgbw) + dot(abs(H-G5),rgbw) + dot(abs(H-F),rgbw) + 4.0*dot(abs(E-G),rgbw); + + edri = step(w1, w2) * irlv0; + edr = step(w1 + vec4(0.1, 0.1, 0.1, 0.1), w2) * step(vec4(0.5, 0.5, 0.5, 0.5), irlv1); + + w1.x = dot(abs(F-G),rgbw); w1.y = dot(abs(B-I),rgbw); w1.z = dot(abs(D-C),rgbw); w1.w = dot(abs(H-A),rgbw); + w2.x = dot(abs(H-C),rgbw); w2.y = dot(abs(F-A),rgbw); w2.z = dot(abs(B-G),rgbw); w2.w = dot(abs(D-I),rgbw); + + edr_l = step( lv2_cf*w1, w2 ) * irlv2l * edr; + edr_u = step( lv2_cf*w2, w1 ) * irlv2u * edr; + + fx45 = edr * fx45; + fx30 = edr_l * fx30; + fx60 = edr_u * fx60; + fx45i = edri * fx45i; + + w1.x = dot(abs(E-F),rgbw); w1.y = dot(abs(E-B),rgbw); w1.z = dot(abs(E-D),rgbw); w1.w = dot(abs(E-H),rgbw); + w2.x = dot(abs(E-H),rgbw); w2.y = dot(abs(E-F),rgbw); w2.z = dot(abs(E-B),rgbw); w2.w = dot(abs(E-D),rgbw); + + px = step(w1, w2); +#ifdef SMOOTH_TIPS + vec4 maximos = max(max(fx30, fx60), max(fx45, fx45i)); +#else + vec4 maximos = max(max(fx30, fx60), fx45); +#endif + vec3 res1 = E; + res1 = mix(res1, mix(H, F, px.x), maximos.x); + res1 = mix(res1, mix(B, D, px.z), maximos.z); + + vec3 res2 = E; + res2 = mix(res2, mix(F, B, px.y), maximos.y); + res2 = mix(res2, mix(D, H, px.w), maximos.w); + + res = mix(res1, res2, step(c_df(E, res1), c_df(E, res2))); + + gl_FragColor.xyz = res; + gl_FragColor.a = 1.0; +} diff --git a/assets/shaders/defaultshaders.ini b/assets/shaders/defaultshaders.ini index 345aa325d3..333c250c6d 100644 --- a/assets/shaders/defaultshaders.ini +++ b/assets/shaders/defaultshaders.ini @@ -116,4 +116,15 @@ Name=xBRZ Freescale DU Scaler for SFA3MAX Author=DeSmuME team based on Hyllian xBR work, conversion and tweaks by LunaMoo Fragment=xBRZf-DU.fsh Vertex=5xBR.vsh +OutputResolution=True[5xBR-DUns] +Name=5xBR DU Scaler for SFA3MAX(NS) +Author=Hyllian tweak by guest.r another tweak by LunaMoo +Fragment=5xBR-DUns.fsh +Vertex=5xBR.vsh +OutputResolution=True +[xBRZf-DUns] +Name=xBRZ Freescale DU Scaler for SFA3MAX(NS) +Author=DeSmuME team based on Hyllian xBR work, conversion and tweaks by LunaMoo +Fragment=xBRZf-DUns.fsh +Vertex=5xBR.vsh OutputResolution=True diff --git a/assets/shaders/xBRZf-DU.fsh b/assets/shaders/xBRZf-DU.fsh index c02f98cc73..39c6b66395 100644 --- a/assets/shaders/xBRZf-DU.fsh +++ b/assets/shaders/xBRZf-DU.fsh @@ -48,12 +48,8 @@ precision mediump float; precision mediump int; #endif -// compatibility #defines -#define Source Texture -#define vTexCoord TEX0.xy - -#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize -#define OutSize vec4(OutputSize, 1.0 / OutputSize) +#define X_FACTOR 0.7991 // Default: 0.7991, for non-stretched use 1.0 +#define Y_FACTOR 0.831 // Default: 0.831 #define BLEND_NONE 0 #define BLEND_NORMAL 1 @@ -102,8 +98,6 @@ float get_left_ratio(vec2 center, vec2 origin, vec2 direction, vec2 scale) #define eq(a,b) (a == b) #define neq(a,b) (a != b) -//#define P(x,y) COMPAT_TEXTURE(Source, coord + SourceSize.zw * vec2(x, y)).rgb - void main() { //--------------------------------------- @@ -112,13 +106,14 @@ void main() // x|D|E|F|x // x|G|H|I|x // -|x|x|x|- - float sampleMod = 0.791;//0.829; - float scaleFactor = 480.0 * u_pixelDelta.x * sampleMod; - vec2 scale = 1.0 / (u_pixelDelta.xy / scaleFactor); - vec2 pos = fract(v_texcoord0.xy*scale.xy)- vec2(0.5, 0.5); - vec2 coord = v_texcoord0.xy-pos*(u_pixelDelta.xy / scaleFactor); - vec2 dx = vec2(u_pixelDelta.x / scaleFactor,0.0); - vec2 dy = vec2(0.0,u_pixelDelta.y / scaleFactor); + float sampleModX = X_FACTOR; //0.7991 + float sampleModY = Y_FACTOR; //0.831 + vec2 scaleFactor = vec2((480.0 * u_pixelDelta.x * sampleModX),(270.0 * u_pixelDelta.y * sampleModY)); + vec2 scale = vec2(1.0 / (u_pixelDelta.x / scaleFactor.x), 1.0 / (u_pixelDelta.y / scaleFactor.y)); + vec2 pos = fract(v_texcoord0.xy*scale.xy)- vec2(0.5*sampleModX, 0.5*sampleModY); + vec2 coord = v_texcoord0.xy-pos.xy*vec2(u_pixelDelta.x / scaleFactor.x, u_pixelDelta.y / scaleFactor.y).xy; + vec2 dx = vec2(u_pixelDelta.x / scaleFactor.x,0.0); + vec2 dy = vec2(0.0,u_pixelDelta.y / scaleFactor.y); vec2 y2 = dy + dy; vec2 x2 = dx + dx; /* vec3 A = P(-1.,-1.); diff --git a/assets/shaders/xBRZf-DUns.fsh b/assets/shaders/xBRZf-DUns.fsh new file mode 100644 index 0000000000..afc344f83c --- /dev/null +++ b/assets/shaders/xBRZf-DUns.fsh @@ -0,0 +1,329 @@ +// Enter your shader in this window +// xBRZ freescale +// based on : + +// 4xBRZ shader - Copyright (C) 2014-2016 DeSmuME team +// +// This file is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 2 of the License, or +// (at your option) any later version. +// +// This file is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with the this software. If not, see . + + +/* + Hyllian's xBR-vertex code and texel mapping + + Copyright (C) 2011/2016 Hyllian - sergiogdb@gmail.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of 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. + + 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. + +*/ + +#ifdef GL_ES +precision mediump float; +precision mediump int; +#endif + +#define X_FACTOR 1.0 // Default: 0.7991, for non-stretched use 1.0 +#define Y_FACTOR 0.831 // Default: 0.831 + +#define BLEND_NONE 0 +#define BLEND_NORMAL 1 +#define BLEND_DOMINANT 2 +#define LUMINANCE_WEIGHT 1.0 +#define EQUAL_COLOR_TOLERANCE 30.0/255.0 +#define STEEP_DIRECTION_THRESHOLD 2.2 +#define DOMINANT_DIRECTION_THRESHOLD 3.6 + +uniform sampler2D sampler0; +uniform vec2 u_pixelDelta; +varying vec2 v_texcoord0; + + +float DistYCbCr(vec3 pixA, vec3 pixB) +{ + const vec3 w = vec3(0.2627, 0.6780, 0.0593); + const float scaleB = 0.5 / (1.0 - w.b); + const float scaleR = 0.5 / (1.0 - w.r); + vec3 diff = pixA - pixB; + float Y = dot(diff.rgb, w); + float Cb = scaleB * (diff.b - Y); + float Cr = scaleR * (diff.r - Y); + + return sqrt(((LUMINANCE_WEIGHT * Y) * (LUMINANCE_WEIGHT * Y)) + (Cb * Cb) + (Cr * Cr)); +} + +bool IsPixEqual(const vec3 pixA, const vec3 pixB) +{ + return (DistYCbCr(pixA, pixB) < EQUAL_COLOR_TOLERANCE); +} + +float get_left_ratio(vec2 center, vec2 origin, vec2 direction, vec2 scale) +{ + vec2 P0 = center - origin; + vec2 proj = direction * (dot(P0, direction) / dot(direction, direction)); + vec2 distv = P0 - proj; + vec2 orth = vec2(-direction.y, direction.x); + float side = sign(dot(P0, orth)); + float v = side * length(distv * scale); + +// return step(0, v); + return smoothstep(-sqrt(2.0)/2.0, sqrt(2.0)/2.0, v); +} + +#define eq(a,b) (a == b) +#define neq(a,b) (a != b) + +void main() +{ + //--------------------------------------- + // Input Pixel Mapping: -|x|x|x|- + // x|A|B|C|x + // x|D|E|F|x + // x|G|H|I|x + // -|x|x|x|- + float sampleModX = X_FACTOR; //0.7991 + float sampleModY = Y_FACTOR; //0.831 + vec2 scaleFactor = vec2((480.0 * u_pixelDelta.x * sampleModX),(270.0 * u_pixelDelta.y * sampleModY)); + vec2 scale = vec2(1.0 / (u_pixelDelta.x / scaleFactor.x), 1.0 / (u_pixelDelta.y / scaleFactor.y)); + vec2 pos = fract(v_texcoord0.xy*scale.xy)- vec2(0.5*sampleModX, 0.5*sampleModY); + vec2 coord = v_texcoord0.xy-pos.xy*vec2(u_pixelDelta.x / scaleFactor.x, u_pixelDelta.y / scaleFactor.y).xy; + vec2 dx = vec2(u_pixelDelta.x / scaleFactor.x,0.0); + vec2 dy = vec2(0.0,u_pixelDelta.y / scaleFactor.y); + vec2 y2 = dy + dy; vec2 x2 = dx + dx; +/* + vec3 A = P(-1.,-1.); + vec3 B = P( 0.,-1.); + vec3 C = P( 1.,-1.); + vec3 D = P(-1., 0.); + vec3 E = P( 0., 0.); + vec3 F = P( 1., 0.); + vec3 G = P(-1., 1.); + vec3 H = P( 0., 1.); + vec3 I = P( 1., 1.); +*/ + vec3 A = texture2D(sampler0, coord -dx -dy ).xyz; + vec3 B = texture2D(sampler0, coord -dy ).xyz; + vec3 C = texture2D(sampler0, coord +dx -dy ).xyz; + vec3 D = texture2D(sampler0, coord -dx ).xyz; + vec3 E = texture2D(sampler0, coord ).xyz; + vec3 F = texture2D(sampler0, coord +dx ).xyz; + vec3 G = texture2D(sampler0, coord -dx +dy ).xyz; + vec3 H = texture2D(sampler0, coord +dy ).xyz; + vec3 I = texture2D(sampler0, coord +dx +dy ).xyz; + + vec3 A1 = texture2D(sampler0, coord -dx -y2).xyz; //-1,-2 + vec3 C1 = texture2D(sampler0, coord +dx -y2).xyz; //1,-2 + vec3 A0 = texture2D(sampler0, coord -x2 -dy).xyz; //-2,-1 + vec3 G0 = texture2D(sampler0, coord -x2 +dy).xyz; //-2,1 + vec3 C4 = texture2D(sampler0, coord +x2 -dy).xyz; //2,-1 + vec3 I4 = texture2D(sampler0, coord +x2 +dy).xyz; //2,1 + vec3 G5 = texture2D(sampler0, coord -dx +y2).xyz; //-1,2 + vec3 I5 = texture2D(sampler0, coord +dx +y2).xyz; //1,2 + vec3 B1 = texture2D(sampler0, coord -y2).xyz; //0,-2 + vec3 D0 = texture2D(sampler0, coord -x2 ).xyz; //-2,0 + vec3 H5 = texture2D(sampler0, coord +y2).xyz; //0,2 + vec3 F4 = texture2D(sampler0, coord +x2 ).xyz; //2,0 + + // blendResult Mapping: x|y| + // w|z| + ivec4 blendResult = ivec4(BLEND_NONE,BLEND_NONE,BLEND_NONE,BLEND_NONE); + + // Preprocess corners + // Pixel Tap Mapping: -|-|-|-|- + // -|-|B|C|- + // -|D|E|F|x + // -|G|H|I|x + // -|-|x|x|- + if (!((eq(E,F) && eq(H,I)) || (eq(E,H) && eq(F,I)))) + { + float dist_H_F = DistYCbCr(G, E) + DistYCbCr(E, C) + DistYCbCr(H5, I) + DistYCbCr(I, F4) + (4.0 * DistYCbCr(H, F)); + float dist_E_I = DistYCbCr(D, H) + DistYCbCr(H, I5) + DistYCbCr(B, F) + DistYCbCr(F, I4) + (4.0 * DistYCbCr(E, I)); + bool dominantGradient = (DOMINANT_DIRECTION_THRESHOLD * dist_H_F) < dist_E_I; + blendResult.z = ((dist_H_F < dist_E_I) && neq(E,F) && neq(E,H)) ? ((dominantGradient) ? BLEND_DOMINANT : BLEND_NORMAL) : BLEND_NONE; + } + + + // Pixel Tap Mapping: -|-|-|-|- + // -|A|B|-|- + // x|D|E|F|- + // x|G|H|I|- + // -|x|x|-|- + if (!((eq(D,E) && eq(G,H)) || (eq(D,G) && eq(E,H)))) + { + float dist_G_E = DistYCbCr(G0, D) + DistYCbCr(D, B) + DistYCbCr(G5, H) + DistYCbCr(H, F) + (4.0 * DistYCbCr(G, E)); + float dist_D_H = DistYCbCr(D0, G) + DistYCbCr(G, H5) + DistYCbCr(A, E) + DistYCbCr(E, I) + (4.0 * DistYCbCr(D, H)); + bool dominantGradient = (DOMINANT_DIRECTION_THRESHOLD * dist_D_H) < dist_G_E; + blendResult.w = ((dist_G_E > dist_D_H) && neq(E,D) && neq(E,H)) ? ((dominantGradient) ? BLEND_DOMINANT : BLEND_NORMAL) : BLEND_NONE; + } + + // Pixel Tap Mapping: -|-|x|x|- + // -|A|B|C|x + // -|D|E|F|x + // -|-|H|I|- + // -|-|-|-|- + if (!((eq(B,C) && eq(E,F)) || (eq(B,E) && eq(C,F)))) + { + float dist_E_C = DistYCbCr(D, B) + DistYCbCr(B, C1) + DistYCbCr(H, F) + DistYCbCr(F, C4) + (4.0 * DistYCbCr(E, C)); + float dist_B_F = DistYCbCr(A, E) + DistYCbCr(E, I) + DistYCbCr(B1, C) + DistYCbCr(C, F4) + (4.0 * DistYCbCr(B, F)); + bool dominantGradient = (DOMINANT_DIRECTION_THRESHOLD * dist_B_F) < dist_E_C; + blendResult.y = ((dist_E_C > dist_B_F) && neq(E,B) && neq(E,F)) ? ((dominantGradient) ? BLEND_DOMINANT : BLEND_NORMAL) : BLEND_NONE; + } + + // Pixel Tap Mapping: -|x|x|-|- + // x|A|B|C|- + // x|D|E|F|- + // -|G|H|-|- + // -|-|-|-|- + if (!((eq(A,B) && eq(D,E)) || (eq(A,D) && eq(B,E)))) + { + float dist_D_B = DistYCbCr(D0, A) + DistYCbCr(A, B1) + DistYCbCr(G, E) + DistYCbCr(E, C) + (4.0 * DistYCbCr(D, B)); + float dist_A_E = DistYCbCr(A0, D) + DistYCbCr(D, H) + DistYCbCr(A1, B) + DistYCbCr(B, F) + (4.0 * DistYCbCr(A, E)); + bool dominantGradient = (DOMINANT_DIRECTION_THRESHOLD * dist_D_B) < dist_A_E; + blendResult.x = ((dist_D_B < dist_A_E) && neq(E,D) && neq(E,B)) ? ((dominantGradient) ? BLEND_DOMINANT : BLEND_NORMAL) : BLEND_NONE; + } + + vec3 res = E; + + // Pixel Tap Mapping: -|-|-|-|- + // -|-|B|C|- + // -|D|E|F|x + // -|G|H|I|x + // -|-|x|x|- + if(blendResult.z != BLEND_NONE) + { + float dist_F_G = DistYCbCr(F, G); + float dist_H_C = DistYCbCr(H, C); + bool doLineBlend = (blendResult.z == BLEND_DOMINANT || + !((blendResult.y != BLEND_NONE && !IsPixEqual(E, G)) || (blendResult.w != BLEND_NONE && !IsPixEqual(E, C)) || + (IsPixEqual(G, H) && IsPixEqual(H, I) && IsPixEqual(I, F) && IsPixEqual(F, C) && !IsPixEqual(E, I)))); + + vec2 origin = vec2(0.0, 1.0 / sqrt(2.0)); + vec2 direction = vec2(1.0, -1.0); + if(doLineBlend) + { + bool haveShallowLine = (STEEP_DIRECTION_THRESHOLD * dist_F_G <= dist_H_C) && neq(E,G) && neq(D,G); + bool haveSteepLine = (STEEP_DIRECTION_THRESHOLD * dist_H_C <= dist_F_G) && neq(E,C) && neq(B,C); + origin = haveShallowLine? vec2(0.0, 0.25) : vec2(0.0, 0.5); + direction.x += haveShallowLine? 1.0: 0.0; + direction.y -= haveSteepLine? 1.0: 0.0; + } + + vec3 blendPix = mix(H,F, step(DistYCbCr(E, F), DistYCbCr(E, H))); + res = mix(res, blendPix, get_left_ratio(pos, origin, direction, scale)); + } + + // Pixel Tap Mapping: -|-|-|-|- + // -|A|B|-|- + // x|D|E|F|- + // x|G|H|I|- + // -|x|x|-|- + if(blendResult.w != BLEND_NONE) + { + float dist_H_A = DistYCbCr(H, A); + float dist_D_I = DistYCbCr(D, I); + bool doLineBlend = (blendResult.w == BLEND_DOMINANT || + !((blendResult.z != BLEND_NONE && !IsPixEqual(E, A)) || (blendResult.x != BLEND_NONE && !IsPixEqual(E, I)) || + (IsPixEqual(A, D) && IsPixEqual(D, G) && IsPixEqual(G, H) && IsPixEqual(H, I) && !IsPixEqual(E, G)))); + + vec2 origin = vec2(-1.0 / sqrt(2.0), 0.0); + vec2 direction = vec2(1.0, 1.0); + if(doLineBlend) + { + bool haveShallowLine = (STEEP_DIRECTION_THRESHOLD * dist_H_A <= dist_D_I) && neq(E,A) && neq(B,A); + bool haveSteepLine = (STEEP_DIRECTION_THRESHOLD * dist_D_I <= dist_H_A) && neq(E,I) && neq(F,I); + origin = haveShallowLine? vec2(-0.25, 0.0) : vec2(-0.5, 0.0); + direction.y += haveShallowLine? 1.0: 0.0; + direction.x += haveSteepLine? 1.0: 0.0; + } + origin = origin; + direction = direction; + + vec3 blendPix = mix(H,D, step(DistYCbCr(E, D), DistYCbCr(E, H))); + res = mix(res, blendPix, get_left_ratio(pos, origin, direction, scale)); + } + + // Pixel Tap Mapping: -|-|x|x|- + // -|A|B|C|x + // -|D|E|F|x + // -|-|H|I|- + // -|-|-|-|- + if(blendResult.y != BLEND_NONE) + { + float dist_B_I = DistYCbCr(B, I); + float dist_F_A = DistYCbCr(F, A); + bool doLineBlend = (blendResult.y == BLEND_DOMINANT || + !((blendResult.x != BLEND_NONE && !IsPixEqual(E, I)) || (blendResult.z != BLEND_NONE && !IsPixEqual(E, A)) || + (IsPixEqual(I, F) && IsPixEqual(F, C) && IsPixEqual(C, B) && IsPixEqual(B, A) && !IsPixEqual(E, C)))); + + vec2 origin = vec2(1.0 / sqrt(2.0), 0.0); + vec2 direction = vec2(-1.0, -1.0); + + if(doLineBlend) + { + bool haveShallowLine = (STEEP_DIRECTION_THRESHOLD * dist_B_I <= dist_F_A) && neq(E,I) && neq(H,I); + bool haveSteepLine = (STEEP_DIRECTION_THRESHOLD * dist_F_A <= dist_B_I) && neq(E,A) && neq(D,A); + origin = haveShallowLine? vec2(0.25, 0.0) : vec2(0.5, 0.0); + direction.y -= haveShallowLine? 1.0: 0.0; + direction.x -= haveSteepLine? 1.0: 0.0; + } + + vec3 blendPix = mix(F,B, step(DistYCbCr(E, B), DistYCbCr(E, F))); + res = mix(res, blendPix, get_left_ratio(pos, origin, direction, scale)); + } + + // Pixel Tap Mapping: -|x|x|-|- + // x|A|B|C|- + // x|D|E|F|- + // -|G|H|-|- + // -|-|-|-|- + if(blendResult.x != BLEND_NONE) + { + float dist_D_C = DistYCbCr(D, C); + float dist_B_G = DistYCbCr(B, G); + bool doLineBlend = (blendResult.x == BLEND_DOMINANT || + !((blendResult.w != BLEND_NONE && !IsPixEqual(E, C)) || (blendResult.y != BLEND_NONE && !IsPixEqual(E, G)) || + (IsPixEqual(C, B) && IsPixEqual(B, A) && IsPixEqual(A, D) && IsPixEqual(D, G) && !IsPixEqual(E, A)))); + + vec2 origin = vec2(0.0, -1.0 / sqrt(2.0)); + vec2 direction = vec2(-1.0, 1.0); + if(doLineBlend) + { + bool haveShallowLine = (STEEP_DIRECTION_THRESHOLD * dist_D_C <= dist_B_G) && neq(E,C) && neq(F,C); + bool haveSteepLine = (STEEP_DIRECTION_THRESHOLD * dist_B_G <= dist_D_C) && neq(E,G) && neq(H,G); + origin = haveShallowLine? vec2(0.0, -0.25) : vec2(0.0, -0.5); + direction.x -= haveShallowLine? 1.0: 0.0; + direction.y += haveSteepLine? 1.0: 0.0; + } + + vec3 blendPix = mix(D,B, step(DistYCbCr(E, B), DistYCbCr(E, D))); + res = mix(res, blendPix, get_left_ratio(pos, origin, direction, scale)); + } + + gl_FragColor.rgb = res; + gl_FragColor.a = 1.0; +} \ No newline at end of file