1/*
2   AA shader 4.o / AA shader 4.o - filtro
3
4   Copyright (C) 2014 guest(r) - guest.r@gmail.com
5
6   This program is free software; you can redistribute it and/or
7   modify it under the terms of the GNU General Public License
8   as published by the Free Software Foundation; either version 2
9   of the License, or (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20*/
21// Modified as video aware smoothing effect for PPSSPP.
22// Some variable definitions had to be moved inside functions(and so repeated) due to glsl->hlsl auto translation failing.
23// Also auto translation fails with bool uniform, which is why u_video is defined as float.
24
25#ifdef GL_ES
26precision mediump float;
27precision mediump int;
28#endif
29
30uniform sampler2D sampler0;
31uniform float u_video;
32
33
34//===========
35varying vec2 v_texcoord0;
36
37const vec3 dt = vec3(1.0,1.0,1.0);
38
39vec3 texture2d (vec2 texcoord) {
40
41	float scale = 1.0;
42	if (u_video==1.0){
43		scale = 2.0;
44	} else {
45		scale = 7.0;
46	}
47
48	vec4 yx = vec4(1.0/480.0,1.0/272.0,-1.0/480.0,-1.0/272.0)/scale;
49	vec4 xy = vec4(2.0/480.0,2.0/272.0,-2.0/480.0,-2.0/272.0)/scale;
50
51	vec3 s00 = texture2D(sampler0, texcoord + yx.zw).xyz;
52	vec3 s20 = texture2D(sampler0, texcoord + yx.xw).xyz;
53	vec3 s22 = texture2D(sampler0, texcoord + yx.xy).xyz;
54	vec3 s02 = texture2D(sampler0, texcoord + yx.zy).xyz;
55
56	float m1=dot(abs(s00-s22),dt)+0.001;
57	float m2=dot(abs(s02-s20),dt)+0.001;
58
59	return 0.5*(m2*(s00+s22)+m1*(s02+s20))/(m1+m2);
60}
61vec3 texture2dd (vec2 texcoord) {
62
63	float scale = 1.0;
64	if (u_video==1.0){
65		scale = 2.0;
66	} else {
67		scale = 7.0;
68	}
69
70	vec4 yx = vec4(1.0/480.0,1.0/272.0,-1.0/480.0,-1.0/272.0)/scale;
71	vec4 xy = vec4(2.0/480.0,2.0/272.0,-2.0/480.0,-2.0/272.0)/scale;
72
73	vec3 c11 = texture2D(sampler0, texcoord        ).xyz;
74	vec3 c00 = texture2D(sampler0, texcoord + xy.zw).xyz;
75	vec3 c20 = texture2D(sampler0, texcoord + xy.xw).xyz;
76	vec3 c22 = texture2D(sampler0, texcoord + xy.xy).xyz;
77	vec3 c02 = texture2D(sampler0, texcoord + xy.zy).xyz;
78	vec3 s00 = texture2D(sampler0, texcoord + yx.zw).xyz;
79	vec3 s20 = texture2D(sampler0, texcoord + yx.xw).xyz;
80	vec3 s22 = texture2D(sampler0, texcoord + yx.xy).xyz;
81	vec3 s02 = texture2D(sampler0, texcoord + yx.zy).xyz;
82
83	float d1=dot(abs(c00-c22),dt)+0.001;
84	float d2=dot(abs(c20-c02),dt)+0.001;
85	float m1=dot(abs(s00-s22),dt)+0.001;
86	float m2=dot(abs(s02-s20),dt)+0.001;
87
88	vec3 t2=(d1*(c20+c02)+d2*(c00+c22))/(2.0*(d1+d2));
89
90	return 0.25*(c11+t2+(m2*(s00+s22)+m1*(s02+s20))/(m1+m2));
91}
92
93void main() {
94
95	float scale = 7.0;
96	bool filtro = false;
97
98	if (u_video==1.0){
99		scale = 2.0;
100		filtro = true;
101	} else {
102		scale = 7.0;
103		filtro = false;
104	}
105
106	// Calculating texel coordinates
107
108	vec2 size     = vec2(480.0,272.0)*scale;
109	vec2 inv_size = vec2(1.0/480.0,1.0/272.0)/scale;
110
111	vec2 OGL2Pos = v_texcoord0 * size;
112	vec2 fp = fract(OGL2Pos);
113	vec2 dx = vec2(inv_size.x,0.0);
114	vec2 dy = vec2(0.0, inv_size.y);
115	vec2 g1 = vec2(inv_size.x,inv_size.y);
116	vec2 g2 = vec2(-inv_size.x,inv_size.y);
117
118	vec2 pC4 = floor(OGL2Pos) * inv_size + 0.5*inv_size;
119
120	// Reading the texels
121	vec3 C0 = texture2d(pC4 - g1);
122	vec3 C1 = texture2d(pC4 - dy);
123	vec3 C2 = texture2d(pC4 - g2);
124	vec3 C3 = texture2d(pC4 - dx);
125	vec3 C4 = texture2d(pC4     );
126	vec3 C5 = texture2d(pC4 + dx);
127	vec3 C6 = texture2d(pC4 + g2);
128	vec3 C7 = texture2d(pC4 + dy);
129	vec3 C8 = texture2d(pC4 + g1);
130
131	vec3 ul, ur, dl, dr;
132	float m1, m2;
133
134	m1 = dot(abs(C0-C4),dt)+0.001;
135	m2 = dot(abs(C1-C3),dt)+0.001;
136	ul = (m2*(C0+C4)+m1*(C1+C3))/(m1+m2);
137
138	m1 = dot(abs(C1-C5),dt)+0.001;
139	m2 = dot(abs(C2-C4),dt)+0.001;
140	ur = (m2*(C1+C5)+m1*(C2+C4))/(m1+m2);
141
142	m1 = dot(abs(C3-C7),dt)+0.001;
143	m2 = dot(abs(C6-C4),dt)+0.001;
144	dl = (m2*(C3+C7)+m1*(C6+C4))/(m1+m2);
145
146	m1 = dot(abs(C4-C8),dt)+0.001;
147	m2 = dot(abs(C5-C7),dt)+0.001;
148	dr = (m2*(C4+C8)+m1*(C5+C7))/(m1+m2);
149
150	vec3 result = vec3(0.5*((dr*fp.x+dl*(1.0-fp.x))*fp.y+(ur*fp.x+ul*(1.0-fp.x))*(1.0-fp.y)));
151
152	if(filtro){
153		vec3 c11 = 0.5*((dr*fp.x+dl*(1.0-fp.x))*fp.y+(ur*fp.x+ul*(1.0-fp.x))*(1.0-fp.y));
154
155		inv_size = vec2(2.0/480.0,2.0/272.0)/scale;;
156
157		dx  = vec2(inv_size.x,0.0);
158		dy  = vec2(0.0,inv_size.y);
159		g1  = vec2( inv_size.x,inv_size.y);
160		g2  = vec2(-inv_size.x,inv_size.y);
161		pC4 = v_texcoord0;
162
163		C0 = texture2dd(pC4-g1);
164		C1 = texture2dd(pC4-dy);
165		C2 = texture2dd(pC4-g2);
166		C3 = texture2dd(pC4-dx);
167		C4 = texture2dd(pC4   );
168		C5 = texture2dd(pC4+dx);
169		C6 = texture2dd(pC4+g2);
170		C7 = texture2dd(pC4+dy);
171		C8 = texture2dd(pC4+g1);
172
173		vec3 mn1 = min(min(C0,C1),C2);
174		vec3 mn2 = min(min(C3,C4),C5);
175		vec3 mn3 = min(min(C6,C7),C8);
176		vec3 mx1 = max(max(C0,C1),C2);
177		vec3 mx2 = max(max(C3,C4),C5);
178		vec3 mx3 = max(max(C6,C7),C8);
179
180		mn1 = min(min(mn1,mn2),mn3);
181		mx1 = max(max(mx1,mx2),mx3);
182
183		vec3 dif1 = abs(c11-mn1) + 0.0001*dt;
184		vec3 dif2 = abs(c11-mx1) + 0.0001*dt;
185
186		float filterparam = 2.0;
187
188		dif1=vec3(pow(dif1.x,filterparam),pow(dif1.y,filterparam),pow(dif1.z,filterparam));
189		dif2=vec3(pow(dif2.x,filterparam),pow(dif2.y,filterparam),pow(dif2.z,filterparam));
190
191		c11.r = (dif1.x*mx1.x + dif2.x*mn1.x)/(dif1.x + dif2.x);
192		c11.g = (dif1.y*mx1.y + dif2.y*mn1.y)/(dif1.y + dif2.y);
193		c11.b = (dif1.z*mx1.z + dif2.z*mn1.z)/(dif1.z + dif2.z);
194
195		result = c11;
196	}
197
198
199	gl_FragColor.xyz=result.xyz;
200	gl_FragColor.a = 1.0;
201}
202