Mercurial > repos > blastem
annotate shaders/ntsc.f.glsl @ 2457:9da3de58410d
Fix libretro build
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 21 Feb 2024 20:25:06 -0800 |
parents | 9f3008f91bec |
children |
rev | line source |
---|---|
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 //****************************************************************************** |
2417
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
2 //***************** FOR PARAMETERS TO ADJUST SEE BELOW THIS! ******************* |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
3 //****************************************************************************** |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
4 // NTSC composite simulator for BlastEm, now with comb filter |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 // Shader by Sik, based on BlastEm's default shader |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 // |
2410
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
7 // Now with gamma correction (NTSC = 2.5 gamma, sRGB = 2.2 gamma*) |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
8 // *sorta, sRGB isn't exactly a gamma curve, but close enough |
2343
49bd818ec9d8
Updated NTSC shader from Sik
Michael Pavone <pavone@retrodev.com>
parents:
2329
diff
changeset
|
9 // |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 // It works by converting from RGB to YIQ and then encoding it into NTSC, then |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 // trying to decode it back. The lossy nature of the encoding process results in |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 // the rainbow effect. It also accounts for the differences between H40 and H32 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 // mode as it computes the exact colorburst cycle length. |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 // |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 // This shader tries to work around the inability to keep track of previous |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 // pixels by sampling seven points (in 0.25 colorburst cycle intervals), that |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 // seems to be enough to give decent filtering (four samples are used for |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 // low-pass filtering, but we need seven because decoding chroma also requires |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 // four samples so we're filtering over overlapping samples... just see the |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 // comments in the I/Q code to understand). |
2410
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
21 // |
2417
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
22 // The comb filter works by comparing against the previous scanline (which means |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
23 // sampling twice). This is done at the composite signal step, i.e. before the |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
24 // filtering to decode back YIQ is performed. |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
25 // |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
26 // Thanks to Tulio Adriano for helping compare against real hardware on a CRT. |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 //****************************************************************************** |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 |
2417
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
29 // How strong is the comb filter when reducing crosstalk between Y and IQ. |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
30 // 0% (0.0) means no separation at all, 100% (1.0) means complete filtering. |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
31 // 80% seems to approximate a model 1, while 90% is closer to a model 2 or 32X. |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
32 const mediump float comb_strength = 0.8; |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
33 |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
34 // Gamma of the TV to simulate. |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
35 const mediump float gamma_correction = 2.5; |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
36 |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
37 //****************************************************************************** |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
38 |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
39 // Parameters coming from BlastEm |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
40 uniform mediump float width; // Screen width (depends on video mode) |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
41 uniform sampler2D textures[2]; // Raw display for each field |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
42 uniform mediump vec2 texsize; // Texture size |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
43 varying mediump vec2 texcoord; // Texture coordinate of current pixel |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
44 uniform int curfield; // Even or odd field? |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
45 uniform int scanlines; // Enable scanlines? |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 // Converts from RGB to YIQ |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 mediump vec3 rgba2yiq(vec4 rgba) |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 { |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 return vec3( |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 rgba[0] * 0.3 + rgba[1] * 0.59 + rgba[2] * 0.11, |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 rgba[0] * 0.599 + rgba[1] * -0.2773 + rgba[2] * -0.3217, |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 rgba[0] * 0.213 + rgba[1] * -0.5251 + rgba[2] * 0.3121 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 ); |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 } |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 // Encodes YIQ into composite |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 mediump float yiq2raw(vec3 yiq, float phase) |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 { |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 return yiq[0] + yiq[1] * sin(phase) + yiq[2] * cos(phase); |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 } |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 // Converts from YIQ to RGB |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 mediump vec4 yiq2rgba(vec3 yiq) |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 { |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 return vec4( |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 yiq[0] + yiq[1] * 0.9469 + yiq[2] * 0.6236, |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 yiq[0] - yiq[1] * 0.2748 - yiq[2] * 0.6357, |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 yiq[0] - yiq[1] * 1.1 + yiq[2] * 1.7, |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 1.0 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 ); |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 } |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 void main() |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 { |
2410
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
76 // The coordinate of the pixel we're supposed to access |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
77 // In interlaced mode, the entire screen is shifted down by half a scanline, |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
78 // y_offset is used to account for this. |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
79 mediump float y_offset = float(curfield) * -0.5 / texsize.y; |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
80 mediump float x = texcoord.x; |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
81 mediump float y = texcoord.y + y_offset; |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 // Horizontal distance of half a colorburst cycle |
2410
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
84 mediump float factorX = (1.0 / texsize.x) / 170.667 * 0.5 * width; |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 |
2410
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
86 // sRGB approximates a gamma ramp of 2.2 while NTSC has a gamma of 2.5 |
2343
49bd818ec9d8
Updated NTSC shader from Sik
Michael Pavone <pavone@retrodev.com>
parents:
2329
diff
changeset
|
87 // Use this value to do gamma correction of every RGB value |
2417
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
88 mediump float gamma = gamma_correction / 2.2; |
2343
49bd818ec9d8
Updated NTSC shader from Sik
Michael Pavone <pavone@retrodev.com>
parents:
2329
diff
changeset
|
89 |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 // Where we store the sampled pixels. |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
91 // [0] = current pixel |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
92 // [1] = 1/4 colorburst cycles earlier |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
93 // [2] = 2/4 colorburst cycles earlier |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 // [3] = 3/4 colorburst cycles earlier |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
95 // [4] = 1 colorburst cycle earlier |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 // [5] = 1 1/4 colorburst cycles earlier |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 // [6] = 1 2/4 colorburst cycles earlier |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
98 mediump float phase[7]; // Colorburst phase (in radians) |
2417
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
99 mediump float raw_y[7]; // Luma isolated from raw composite signal |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
100 mediump float raw_iq[7]; // Chroma isolated from raw composite signal |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
102 // Sample all the pixels we're going to use |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
103 for (int n = 0; n < 7; n++, x -= factorX * 0.5) { |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
104 // Compute colorburst phase at this point |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
105 phase[n] = x / factorX * 3.1415926; |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
106 |
2417
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
107 // Y coordinate one scanline above |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
108 // Apparently GLSL doesn't allow a vec2 with a full expression for |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
109 // texture samplers? Whatever, putting it here (also makes the code |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
110 // below a bit easier to read I guess?) |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
111 mediump float y_above = y - texcoord.y / texsize.y; |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
112 |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
113 // Get the raw composite data for this scanline |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
114 mediump float raw1 = yiq2raw(rgba2yiq( |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
115 texture2D(textures[curfield], vec2(x, y)) |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
116 ), phase[n]); |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
117 |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
118 // Get the raw composite data for scanline above |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
119 // Note that the colorburst phase is shifted 180° |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
120 mediump float raw2 = yiq2raw(rgba2yiq( |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
121 texture2D(textures[curfield], vec2(x, y_above)) |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
122 ), phase[n] + 3.1415926); |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
123 |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
124 // Comb filter: isolate Y and IQ using the above two. |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
125 // Luma is obtained by adding the two scanlines, chroma will cancel out |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
126 // because chroma will be on opposite phases. |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
127 // Chroma is then obtained by cancelling this scanline from the luma |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
128 // to reduce the crosstalk. We don't cancel it entirely though since the |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
129 // filtering isn't perfect (which is why the rainbow leaks a bit). |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
130 raw_y[n] = (raw1 + raw2) * 0.5; |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
131 raw_iq[n] = raw1 - (raw1 + raw2) * (comb_strength * 0.5); |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
132 } |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
133 |
2410
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
134 // Decode Y by averaging over the last whole sampled cycle (effectively |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
135 // filtering anything above the colorburst frequency) |
2417
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
136 mediump float y_mix = (raw_y[0] + raw_y[1] + raw_y[2] + raw_y[3]) * 0.25; |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
137 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
138 // Decode I and Q (see page below to understand what's going on) |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
139 // https://codeandlife.com/2012/10/09/composite-video-decoding-theory-and-practice/ |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
140 // |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
141 // Retrieving I and Q out of the raw signal is done like this |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
142 // (use sin for I and cos for Q): |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
143 // |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
144 // 0.5 * raw[0] * sin(phase[0]) + 0.5 * raw[1] * sin(phase[1]) + |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
145 // 0.5 * raw[2] * sin(phase[2]) + 0.5 * raw[3] * sin(phase[3]) |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
146 // |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
147 // i.e. multiply each of the sampled quarter cycles against the reference |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
148 // wave and average them (actually double that because for some reason |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
149 // that's needed to get the correct scale, hence 0.5 instead of 0.25) |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
150 // |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
151 // That turns out to be blocky tho, so we opt to filter down the chroma... |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
152 // which requires doing the above *four* times if we do it the same way as |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
153 // we did for luminance (note that 0.125 = 1/4 of 0.5): |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
154 // |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
155 // 0.125 * raw[0] * sin(phase[0]) + 0.125 * raw[1] * sin(phase[1]) + |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
156 // 0.125 * raw[2] * sin(phase[2]) + 0.125 * raw[3] * sin(phase[3]) + |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
157 // 0.125 * raw[1] * sin(phase[1]) + 0.125 * raw[2] * sin(phase[2]) + |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
158 // 0.125 * raw[3] * sin(phase[3]) + 0.125 * raw[4] * sin(phase[4]) + |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
159 // 0.125 * raw[2] * sin(phase[2]) + 0.125 * raw[3] * sin(phase[3]) + |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
160 // 0.125 * raw[4] * sin(phase[4]) + 0.125 * raw[5] * sin(phase[5]) + |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
161 // 0.125 * raw[3] * sin(phase[3]) + 0.125 * raw[4] * sin(phase[4]) + |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
162 // 0.125 * raw[5] * sin(phase[5]) + 0.125 * raw[6] * sin(phase[6]) |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
163 // |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
164 // There are a lot of repeated values there that could be merged into one, |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
165 // what you see below is the resulting simplification. |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
166 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
167 mediump float i_mix = |
2417
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
168 0.125 * raw_iq[0] * sin(phase[0]) + |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
169 0.25 * raw_iq[1] * sin(phase[1]) + |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
170 0.375 * raw_iq[2] * sin(phase[2]) + |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
171 0.5 * raw_iq[3] * sin(phase[3]) + |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
172 0.375 * raw_iq[4] * sin(phase[4]) + |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
173 0.25 * raw_iq[5] * sin(phase[5]) + |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
174 0.125 * raw_iq[6] * sin(phase[6]); |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
175 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
176 mediump float q_mix = |
2417
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
177 0.125 * raw_iq[0] * cos(phase[0]) + |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
178 0.25 * raw_iq[1] * cos(phase[1]) + |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
179 0.375 * raw_iq[2] * cos(phase[2]) + |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
180 0.5 * raw_iq[3] * cos(phase[3]) + |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
181 0.375 * raw_iq[4] * cos(phase[4]) + |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
182 0.25 * raw_iq[5] * cos(phase[5]) + |
9f3008f91bec
Updated Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2410
diff
changeset
|
183 0.125 * raw_iq[6] * cos(phase[6]); |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
184 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
185 // Convert YIQ back to RGB and output it |
2343
49bd818ec9d8
Updated NTSC shader from Sik
Michael Pavone <pavone@retrodev.com>
parents:
2329
diff
changeset
|
186 gl_FragColor = pow(yiq2rgba(vec3(y_mix, i_mix, q_mix)), |
49bd818ec9d8
Updated NTSC shader from Sik
Michael Pavone <pavone@retrodev.com>
parents:
2329
diff
changeset
|
187 vec4(gamma, gamma, gamma, 1.0)); |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
188 |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
189 // If you're curious to see what the raw composite signal looks like, |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
190 // comment out the above and uncomment the line below instead |
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
191 //gl_FragColor = vec4(raw[0], raw[0], raw[0], 1.0); |
2410
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
192 |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
193 // Basic scanlines effect. This is done by multiplying the color against a |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
194 // "half sine" wave so the center is brighter and a narrow outer area in |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
195 // each scanline is noticeable darker. |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
196 // The weird constant in the middle line is 1-sqrt(2)/4 and it's used to |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
197 // make sure that the average multiplied value across the whole screen is 1 |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
198 // to preserve the original brightness. |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
199 if (scanlines != 0) { |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
200 mediump float mult = sin(y * texsize.y * 3.1415926); |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
201 mult = abs(mult) * 0.5 + 0.646446609; |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
202 gl_FragColor *= vec4(mult, mult, mult, 1.0); |
f1574b22d5d9
Update Sik's NTSC shader
Michael Pavone <pavone@retrodev.com>
parents:
2343
diff
changeset
|
203 } |
2329
06d5e9b08bdb
Add NTSC composite shader by Sik
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
204 } |