-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCRT-Hyllian-3D.frag
292 lines (211 loc) · 10.3 KB
/
CRT-Hyllian-3D.frag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#version 330 core
/*
Hyllian's CRT Shader
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.
*/
// Uncomment to enable anti-ringing to horizontal filter.
//#define ANTI_RINGING
// Comment next line if you don't desire the phosphor effect.
//#define PHOSPHOR
// Uncomment to enable adjustment of red and green saturation.
//#define RED_GREEN_CONTROL
#define InputGamma 2.4
#define OutputGamma 2.2
#define RED_BOOST 1.0
#define GREEN_BOOST 1.0
#define SCANLINES_STRENGTH 0.72
#define BEAM_MIN_WIDTH 0.86
#define BEAM_MAX_WIDTH 1.0
#define COLOR_BOOST 1.5
#define CRT_TV_BLUE_TINT 1.0
#define GAMMA_IN(color) pow(color, vec3(InputGamma, InputGamma, InputGamma))
#define GAMMA_OUT(color) pow(color, vec3(1.0 / OutputGamma, 1.0 / OutputGamma, 1.0 / OutputGamma))
const vec3 dtt = vec3(65536,255,1);
// Horizontal cubic filter.
// Some known filters use these values:
// B = 0.0, C = 0.0 => Hermite cubic filter.
// B = 1.0, C = 0.0 => Cubic B-Spline filter.
// B = 0.0, C = 0.5 => Catmull-Rom Spline filter. This is the default used in this shader.
// B = C = 1.0/3.0 => Mitchell-Netravali cubic filter.
// B = 0.3782, C = 0.3109 => Robidoux filter.
// B = 0.2620, C = 0.3690 => Robidoux Sharp filter.
// B = 0.36, C = 0.28 => My best config for ringing elimination in pixel art (Hyllian).
// For more info, see: http://www.imagemagick.org/Usage/img_diagrams/cubic_survey.gif
// Change these params to configure the horizontal filter.
const float B = 0.0;
const float C = 0.5;
/*
const mat4 invX = mat4( (-B - 6.0*C)/6.0, (3.0*B + 12.0*C)/6.0, (-3.0*B - 6.0*C)/6.0, B/6.0,
(12.0 - 9.0*B - 6.0*C)/6.0, (-18.0 + 12.0*B + 6.0*C)/6.0, 0.0, (6.0 - 2.0*B)/6.0,
-(12.0 - 9.0*B - 6.0*C)/6.0, (18.0 - 15.0*B - 12.0*C)/6.0, (3.0*B + 6.0*C)/6.0, B/6.0,
(B + 6.0*C)/6.0, -C, 0.0, 0.0);
*/
const mat4 invX = mat4( (-B - 6.0*C)/6.0, (12.0 - 9.0*B - 6.0*C)/6.0, -(12.0 - 9.0*B - 6.0*C)/6.0, (B + 6.0*C)/6.0,
(3.0*B + 12.0*C)/6.0, (-18.0 + 12.0*B + 6.0*C)/6.0, (18.0 - 15.0*B - 12.0*C)/6.0, -C,
(-3.0*B - 6.0*C)/6.0, 0.0, (3.0*B + 6.0*C)/6.0, 0.0,
B/6.0, (6.0 - 2.0*B)/6.0, B/6.0, 0.0);
/*
const static float4x4 invX = float4x4( (-B - 6.0*C)/6.0, (3.0*B + 12.0*C)/6.0, (-3.0*B - 6.0*C)/6.0, B/6.0,
(12.0 - 9.0*B - 6.0*C)/6.0, (-18.0 + 12.0*B + 6.0*C)/6.0, 0.0, (6.0 - 2.0*B)/6.0,
-(12.0 - 9.0*B - 6.0*C)/6.0, (18.0 - 15.0*B - 12.0*C)/6.0, (3.0*B + 6.0*C)/6.0, B/6.0,
(B + 6.0*C)/6.0, -C, 0.0, 0.0);
*/
const float halfpi = 1.5707963267948966192313216916398;
const float pi = 3.1415926535897932384626433832795;
const float JINC2_WINDOW_SINC = 0.42;
const float JINC2_SINC = 0.92;
const float wa = JINC2_WINDOW_SINC*pi;
const float wb = JINC2_SINC*pi;
const float JINC2_AR_STRENGTH = 0.8;
/*
const vec2 OGLSize = vec2( 4096.0, 2048.0 );
const vec2 OGLInvSize = vec2( 1.0/4096.0, 1.0/2048.0 );
const vec2 dx = vec2( 1.0/4096.0, 0.0 );
const vec2 dy = vec2( 0.0, 1.0/2048.0 );
*/
/*
const vec2 OGLSize = vec2( 1024.0, 512.0);
const vec2 OGLInvSize = vec2( 0.0009765625, 0.001953125);
const vec2 dx = vec2( 0.0009765625, 0.0);
const vec2 dy = vec2( 0.0, 0.001953125 );
*/
//uniform vec4 OGL2InvSize;
//uniform vec4 OGL2Size;
float reduce(vec3 A)
{
return dot(A, dtt);
}
// Calculates the distance between two points
float d(vec2 pt1, vec2 pt2)
{
vec2 v = pt2 - pt1;
return sqrt(dot(v,v));
}
vec3 min4(vec3 a, vec3 b, vec3 c, vec3 d)
{
return min(a, min(b, min(c, d)));
}
vec3 max4(vec3 a, vec3 b, vec3 c, vec3 d)
{
return max(a, max(b, max(c, d)));
}
vec4 resampler(vec4 x)
{
vec4 res;
res = (x==vec4(0.0, 0.0, 0.0, 0.0)) ? vec4(wa*wb) : sin(x*wa)*sin(x*wb)/(x*x);
return res;
}
uniform sampler2D OGL2Texture;
in vec2 OGL2Size;
in vec2 texCoord;
out vec4 fragColor;
void main()
{
vec3 color, E;
vec2 OGL2InvSize = 1.0/OGL2Size;
vec2 dx = vec2(OGL2InvSize.x,0.0);
vec2 dy = vec2(0.0, OGL2InvSize.y);
vec2 pix_coord = texCoord.xy*OGL2Size+vec2(-0.5,0.5);
vec2 tex = (floor(texCoord.xy*OGL2Size) + vec2(0.5, 0.5))/OGL2Size;
vec2 tc = (floor(pix_coord)+vec2(0.5,0.5))/OGL2Size;
vec2 fp = fract(pix_coord);
vec3 c00 = GAMMA_IN(texture(OGL2Texture, tc - dx - dy).xyz);
vec3 c01 = GAMMA_IN(texture(OGL2Texture, tc - dy).xyz);
vec3 c02 = GAMMA_IN(texture(OGL2Texture, tc + dx - dy).xyz);
vec3 c03 = GAMMA_IN(texture(OGL2Texture, tc + 2.0*dx - dy).xyz);
vec3 c10 = GAMMA_IN(texture(OGL2Texture, tc - dx).xyz);
vec3 c11 = GAMMA_IN(texture(OGL2Texture, tc ).xyz);
vec3 c12 = GAMMA_IN(texture(OGL2Texture, tc + dx).xyz);
vec3 c13 = GAMMA_IN(texture(OGL2Texture, tc + 2.0*dx).xyz);
color = E = GAMMA_IN(texture(OGL2Texture, texCoord.xy).xyz);
vec3 F0 = texture(OGL2Texture, tex +dx+0.25*dx +0.25*dy).xyz;
vec3 F1 = texture(OGL2Texture, tex +dx+0.25*dx -0.25*dy).xyz;
vec3 F2 = texture(OGL2Texture, tex +dx-0.25*dx -0.25*dy).xyz;
vec3 F3 = texture(OGL2Texture, tex +dx-0.25*dx +0.25*dy).xyz;
vec3 H0 = texture(OGL2Texture, tex +0.25*dx +0.25*dy+dy).xyz;
vec3 H1 = texture(OGL2Texture, tex +0.25*dx -0.25*dy+dy).xyz;
vec3 H2 = texture(OGL2Texture, tex -0.25*dx -0.25*dy+dy).xyz;
vec3 H3 = texture(OGL2Texture, tex -0.25*dx +0.25*dy+dy).xyz;
float f0 = reduce(F0);
float f1 = reduce(F1);
float f2 = reduce(F2);
float f3 = reduce(F3);
float h0 = reduce(H0);
float h1 = reduce(H1);
float h2 = reduce(H2);
float h3 = reduce(H3);
bool block_3d = f0==f1 && f1==f2 && f2==f3 && h0==h1 && h1==h2 && h2==h3;
#ifdef ANTI_RINGING
// Get min/max samples
vec3 min_sample = min(min(c01,c11), min(c02,c12));
vec3 max_sample = max(max(c01,c11), max(c02,c12));
// vec3 min_sample = min(min(c01,c11), min(c02,c12)) + (c10-c11)*(c12-c13);
// vec3 max_sample = max(max(c01,c11), max(c02,c12)) - (c10-c11)*(c12-c13);
#endif
mat4x3 color_matrix0 = mat4x3(c00, c01, c02, c03);
mat4x3 color_matrix1 = mat4x3(c10, c11, c12, c13);
vec4 lobes = vec4(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0);
vec4 invX_Px = invX * lobes;
vec3 color0 = color_matrix0 * invX_Px;
vec3 color1 = color_matrix1 * invX_Px;
/*
vec4 invX_Px = vec4(dot(invX[0], poli), dot(invX[1], poli), dot(invX[2], poli), dot(invX[3], poli));
vec3 color0 = vec3(dot(color_matrix0[0], invX_Px), dot(color_matrix0[1], invX_Px), dot(color_matrix0[2], invX_Px));
vec3 color1 = vec3(dot(color_matrix1[0], invX_Px), dot(color_matrix1[1], invX_Px), dot(color_matrix1[2], invX_Px));
*/
/*
float4x3 color_matrix0 = float4x3(c00, c01, c02, c03);
float4x3 color_matrix1 = float4x3(c10, c11, c12, c13);
float4 invX_Px = mul(invX, float4(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0));
float3 color0 = mul(invX_Px, color_matrix0);
float3 color1 = mul(invX_Px, color_matrix1);
*/
#ifdef ANTI_RINGING
// Anti-ringing
color0 = clamp(color0, min_sample, max_sample);
color1 = clamp(color1, min_sample, max_sample);
#endif
float pos0 = fp.y;
float pos1 = 1 - fp.y;
vec3 lum0 = mix(vec3(BEAM_MIN_WIDTH), vec3(BEAM_MAX_WIDTH), color0);
vec3 lum1 = mix(vec3(BEAM_MIN_WIDTH), vec3(BEAM_MAX_WIDTH), color1);
vec3 d0 = clamp(pos0/(lum0+0.0000001), 0.0, 1.0);
vec3 d1 = clamp(pos1/(lum1+0.0000001), 0.0, 1.0);
d0 = exp(-10.0*SCANLINES_STRENGTH*d0*d0);
d1 = exp(-10.0*SCANLINES_STRENGTH*d1*d1);
color = clamp(color0*d0+color1*d1, 0.0, 1.0);
color *= COLOR_BOOST;
#ifdef RED_GREEN_CONTROL
color.rgb *= vec3(RED_BOOST, GREEN_BOOST, CRT_TV_BLUE_TINT);
#else
color.b *= CRT_TV_BLUE_TINT;
#endif
#ifdef PHOSPHOR
float mod_factor = VAR.texCoord.x * IN.output_size.x * IN.texture_size.x / IN.video_size.x;
vec3 dotMaskWeights = mix(
vec3(1.0, 0.7, 1.0),
vec3(0.7, 1.0, 0.7),
floor(fmod(mod_factor, 2.0))
);
color.rgb *= dotMaskWeights;
#endif
color = block_3d ? color : E;
color = GAMMA_OUT(color);
// final sum and weight normalization
fragColor.xyz = color;
}