ProjectArcade
133 строки · 5.4 Кб
1/*
2Hyllian's 5xBR v3.5a Shader
3
4Copyright (C) 2011 Hyllian/Jararaca - sergiogdb@gmail.com
5
6This program is free software; you can redistribute it and/or
7modify it under the terms of the GNU General Public License
8as published by the Free Software Foundation; either version 2
9of the License, or (at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20*/
21#ifdef GL_ES
22precision mediump float;
23precision mediump int;
24#endif
25
26uniform sampler2D sampler0;
27uniform vec2 u_texelDelta;
28uniform vec2 u_pixelDelta;
29varying vec2 v_texcoord0;
30
31const float coef = 2.0;
32const vec3 rgbw = vec3(16.163, 23.351, 8.4772);
33
34const vec4 Ao = vec4( 1.0, -1.0, -1.0, 1.0 );
35const vec4 Bo = vec4( 1.0, 1.0, -1.0,-1.0 );
36const vec4 Co = vec4( 1.5, 0.5, -0.5, 0.5 );
37const vec4 Ax = vec4( 1.0, -1.0, -1.0, 1.0 );
38const vec4 Bx = vec4( 0.5, 2.0, -0.5,-2.0 );
39const vec4 Cx = vec4( 1.0, 1.0, -0.5, 0.0 );
40const vec4 Ay = vec4( 1.0, -1.0, -1.0, 1.0 );
41const vec4 By = vec4( 2.0, 0.5, -2.0,-0.5 );
42const vec4 Cy = vec4( 2.0, 0.0, -1.0, 0.5 );
43
44
45vec4 df(vec4 A, vec4 B) {
46return abs(A-B);
47}
48
49vec4 weighted_distance(vec4 a, vec4 b, vec4 c, vec4 d, vec4 e, vec4 f, vec4 g, vec4 h) {
50return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
51}
52
53
54void main(){
55
56bool upscale = u_texelDelta.x > (1.6 * u_pixelDelta.x);
57vec3 res = texture2D(sampler0, v_texcoord0.xy).xyz;
58
59// Let's skip the whole scaling if output size smaller than 1.6x of input size
60if (upscale) {
61
62bvec4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
63bvec4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
64bvec4 nc; // new_color
65bvec4 fx, fx_left, fx_up; // inequations of straight lines.
66
67vec2 pS = 1.0 / u_texelDelta.xy;
68vec2 fp = fract(v_texcoord0.xy*pS.xy);
69vec2 TexCoord_0 = v_texcoord0.xy-fp*u_pixelDelta.xy;
70vec2 dx = vec2(u_texelDelta.x,0.0);
71vec2 dy = vec2(0.0,u_texelDelta.y);
72vec2 y2 = dy + dy; vec2 x2 = dx + dx;
73
74vec3 A = texture2D(sampler0, TexCoord_0 -dx -dy ).xyz;
75vec3 B = texture2D(sampler0, TexCoord_0 -dy ).xyz;
76vec3 C = texture2D(sampler0, TexCoord_0 +dx -dy ).xyz;
77vec3 D = texture2D(sampler0, TexCoord_0 -dx ).xyz;
78vec3 E = texture2D(sampler0, TexCoord_0 ).xyz;
79vec3 F = texture2D(sampler0, TexCoord_0 +dx ).xyz;
80vec3 G = texture2D(sampler0, TexCoord_0 -dx +dy ).xyz;
81vec3 H = texture2D(sampler0, TexCoord_0 +dy ).xyz;
82vec3 I = texture2D(sampler0, TexCoord_0 +dx +dy ).xyz;
83vec3 A1 = texture2D(sampler0, TexCoord_0 -dx -y2).xyz;
84vec3 C1 = texture2D(sampler0, TexCoord_0 +dx -y2).xyz;
85vec3 A0 = texture2D(sampler0, TexCoord_0 -x2 -dy).xyz;
86vec3 G0 = texture2D(sampler0, TexCoord_0 -x2 +dy).xyz;
87vec3 C4 = texture2D(sampler0, TexCoord_0 +x2 -dy).xyz;
88vec3 I4 = texture2D(sampler0, TexCoord_0 +x2 +dy).xyz;
89vec3 G5 = texture2D(sampler0, TexCoord_0 -dx +y2).xyz;
90vec3 I5 = texture2D(sampler0, TexCoord_0 +dx +y2).xyz;
91vec3 B1 = texture2D(sampler0, TexCoord_0 -y2).xyz;
92vec3 D0 = texture2D(sampler0, TexCoord_0 -x2 ).xyz;
93vec3 H5 = texture2D(sampler0, TexCoord_0 +y2).xyz;
94vec3 F4 = texture2D(sampler0, TexCoord_0 +x2 ).xyz;
95
96vec4 b = vec4(dot(B ,rgbw), dot(D ,rgbw), dot(H ,rgbw), dot(F ,rgbw));
97vec4 c = vec4(dot(C ,rgbw), dot(A ,rgbw), dot(G ,rgbw), dot(I ,rgbw));
98vec4 d = vec4(b.y, b.z, b.w, b.x);
99vec4 e = vec4(dot(E,rgbw));
100vec4 f = vec4(b.w, b.x, b.y, b.z);
101vec4 g = vec4(c.z, c.w, c.x, c.y);
102vec4 h = vec4(b.z, b.w, b.x, b.y);
103vec4 i = vec4(c.w, c.x, c.y, c.z);
104vec4 i4 = vec4(dot(I4,rgbw), dot(C1,rgbw), dot(A0,rgbw), dot(G5,rgbw));
105vec4 i5 = vec4(dot(I5,rgbw), dot(C4,rgbw), dot(A1,rgbw), dot(G0,rgbw));
106vec4 h5 = vec4(dot(H5,rgbw), dot(F4,rgbw), dot(B1,rgbw), dot(D0,rgbw));
107vec4 f4 = vec4(h5.y, h5.z, h5.w, h5.x);
108
109// These inequations define the line below which interpolation occurs.
110fx = greaterThan(Ao*fp.y+Bo*fp.x,Co);
111fx_left = greaterThan(Ax*fp.y+Bx*fp.x,Cx);
112fx_up = greaterThan(Ay*fp.y+By*fp.x,Cy);
113
114interp_restriction_lv1 = bvec4(vec4(notEqual(e,f))*vec4(notEqual(e,h)));
115interp_restriction_lv2_left = bvec4(vec4(notEqual(e,g))*vec4(notEqual(d,g)));
116interp_restriction_lv2_up = bvec4(vec4(notEqual(e,c))*vec4(notEqual(b,c)));
117
118edr = bvec4(vec4(lessThan(weighted_distance( e, c, g, i, h5, f4, h, f), weighted_distance( h, d, i5, f, i4, b, e, i)))*vec4(interp_restriction_lv1));
119edr_left = bvec4(vec4(lessThanEqual(coef*df(f,g),df(h,c)))*vec4(interp_restriction_lv2_left));
120edr_up = bvec4(vec4(greaterThanEqual(df(f,g),coef*df(h,c)))*vec4(interp_restriction_lv2_up));
121
122nc.x = ( edr.x && (fx.x || edr_left.x && fx_left.x || edr_up.x && fx_up.x) );
123nc.y = ( edr.y && (fx.y || edr_left.y && fx_left.y || edr_up.y && fx_up.y) );
124nc.z = ( edr.z && (fx.z || edr_left.z && fx_left.z || edr_up.z && fx_up.z) );
125nc.w = ( edr.w && (fx.w || edr_left.w && fx_left.w || edr_up.w && fx_up.w) );
126
127px = lessThanEqual(df(e,f),df(e,h));
128
129res = nc.x ? px.x ? F : H : nc.y ? px.y ? B : F : nc.z ? px.z ? D : B : nc.w ? px.w ? H : D : E;
130}
131gl_FragColor.rgb = res;
132gl_FragColor.a = 1.0;
133}
134