-
Notifications
You must be signed in to change notification settings - Fork 44
/
shaders.go
212 lines (174 loc) · 5.67 KB
/
shaders.go
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
package main
const vertShaderSrcDef = `
attribute vec4 vPosition;
attribute vec2 vTexCoord;
varying vec2 texCoord;
void main() {
texCoord = vec2(vTexCoord.x, -vTexCoord.y);
gl_Position = vec4((vPosition.xy * 2.0) - 1.0, vPosition.zw);
}
`
const fragShaderSrcDef = `
varying vec2 texCoord;
uniform sampler2D texture;
void main() {
vec4 c = texture2D(texture, texCoord);
gl_FragColor = vec4(c.r, c.g, c.b, c.a);
}
`
const firstPassNtscArtifactVert = `
#version 120
uniform mat4 rubyMVPMatrix;
attribute vec2 rubyVertexCoord;
attribute vec2 rubyTexCoord;
varying vec2 tex_coord;
varying float chroma_mod_freq;
varying vec2 pix_no;
uniform vec2 rubyTextureSize;
uniform vec2 rubyInputSize;
uniform vec2 rubyOutputSize;
void main()
{
gl_Position = rubyMVPMatrix * vec4(rubyVertexCoord, 0.0, 1.0);
tex_coord = rubyTexCoord;
pix_no = rubyTexCoord * rubyTextureSize * (rubyOutputSize / rubyInputSize);
}
`
const firstPassNtscArtifactFrag = `
#version 120
varying vec2 tex_coord;
uniform sampler2D rubyTexture;
uniform int rubyFrameCount;
varying vec2 pix_no;
const float pi = 3.14159265;
const float chroma_mod_freq = 0.4 * pi;
const mat3 yiq_mat = mat3(
0.2989, 0.5959, 0.2115,
0.5870, -0.2744, -0.5229,
0.1140, -0.3216, 0.3114);
vec3 rgb2yiq(vec3 col)
{
return yiq_mat * col;
}
void main()
{
vec3 col = texture2D(rubyTexture, tex_coord).rgb;
vec3 yiq = rgb2yiq(col);
float chroma_phase = 0.6667 * pi * mod(pix_no.y + float(rubyFrameCount), 3.0);
float mod_phase = chroma_phase + pix_no.x * chroma_mod_freq;
float i_mod = cos(mod_phase);
float q_mod = sin(mod_phase);
// Get it in range of [0, 1] so it can fit in an RGBA framebuffer.
yiq = vec3(yiq.x, yiq.y * i_mod, yiq.z * q_mod);
gl_FragColor = vec4(yiq, 1.0);
}
`
const secondPassNtscArtifactVert = `
#version 120
uniform mat4 rubyMVPMatrix;
attribute vec2 rubyVertexCoord;
attribute vec2 rubyTexCoord;
uniform vec2 rubyTextureSize;
uniform vec2 rubyOutputSize;
varying vec2 tex_coord;
const float pi = 3.14159265;
varying vec2 pix_no;
void main()
{
gl_Position = rubyMVPMatrix * vec4(rubyVertexCoord, 0.0, 1.0);
tex_coord = rubyTexCoord;
pix_no = rubyTexCoord * rubyTextureSize;
}
`
const secondPassNtscArtifactFrag = `
#version 120
uniform sampler2D rubyTexture;
uniform vec2 rubyTextureSize;
uniform int rubyFrameCount;
varying vec2 tex_coord;
varying vec2 pix_no;
const float pi = 3.14159265;
const float chroma_mod_freq = 0.40 * pi;
const float filter[9] = float[9](
0.0019, 0.0031, -0.0108, 0.0, 0.0407,
-0.0445, -0.0807, 0.2913, 0.5982
);
vec3 fetch_offset(float offset, float one_x)
{
return texture2D(rubyTexture, tex_coord + vec2(offset * one_x, 0.0)).xyz;
}
void main()
{
float one_x = 1.0 / rubyTextureSize.x;
float chroma_phase = 0.6667 * pi * mod(pix_no.y + float(rubyFrameCount), 3.0);
float mod_phase = chroma_phase + pix_no.x * chroma_mod_freq;
float signal = 0.0;
for (int i = 0; i < 8; i++)
{
float offset = float(i);
float sums =
dot(fetch_offset(offset - 8.0, one_x), vec3(1.0)) +
dot(fetch_offset(8.0 - offset, one_x), vec3(1.0));
signal += sums * filter[i];
}
signal += dot(texture2D(rubyTexture, tex_coord).xyz, vec3(1.0)) * filter[8];
float i_mod = 2.0 * cos(mod_phase);
float q_mod = 2.0 * sin(mod_phase);
vec3 out_color = vec3(signal, signal * i_mod, signal * q_mod);
gl_FragColor = vec4(out_color, 1.0);
}
`
const thirdPassNtscArtifactVert = `
#version 120
uniform mat4 rubyMVPMatrix;
attribute vec2 rubyVertexCoord;
attribute vec2 rubyTexCoord;
varying vec2 tex_coord;
void main()
{
gl_Position = rubyMVPMatrix * vec4(rubyVertexCoord, 0.0, 1.0);
tex_coord = rubyTexCoord;
}
`
const thirdPassNtscArtifactFrag = `
#version 120
varying vec2 tex_coord;
uniform sampler2D rubyTexture;
uniform vec2 rubyTextureSize;
const float luma_filter[9] = float[9](
0.0019, 0.0052, 0.0035, -0.0163, -0.0407,
-0.0118, 0.1111, 0.2729, 0.3489
);
const float chroma_filter[9] = float[9](
0.0025, 0.0057, 0.0147, 0.0315, 0.0555,
0.0834, 0.1099, 0.1289, 0.1358
);
const mat3 yiq2rgb_mat = mat3(
1.0, 1.0, 1.0,
0.956, -0.2720, -1.1060,
0.6210, -0.6474, 1.7046);
vec3 yiq2rgb(vec3 yiq)
{
return yiq2rgb_mat * yiq;
}
vec3 fetch_offset(float offset, float one_x)
{
return texture2D(rubyTexture, tex_coord + vec2(offset * one_x, 0.0)).xyz;
}
void main()
{
float one_x = 1.0 / rubyTextureSize.x;
vec3 signal = vec3(0.0);
for (int i = 0; i < 8; i++)
{
float offset = float(i);
vec3 sums = fetch_offset(offset - 8.0, one_x) +
fetch_offset(8.0 - offset, one_x);
signal += sums * vec3(luma_filter[i], chroma_filter[i], chroma_filter[i]);
}
signal += texture2D(rubyTexture, tex_coord).xyz *
vec3(luma_filter[8], chroma_filter[8], chroma_filter[8]);
vec3 rgb = yiq2rgb(signal);
gl_FragColor = vec4(rgb, 1.0);
}
`