Mercurial > repos > blastem
annotate shaders/crt.f.glsl @ 2486:e8eba0cd5444
Implement turbo/slow for Pico and Copera
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 14 Apr 2024 12:30:32 -0700 |
parents | f3cca4b3f17a |
children |
rev | line source |
---|---|
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 /* Subtle CRT shader usable in fullscreen - Anaƫl Seghezzi [anael(at)maratis3d.com] |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 This shader is free software distributed under the terms of the GNU General Public |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 License version 3 or higher. This gives you the right to redistribute and/or |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 modify the program as long as you follow the terms of the license. See the file |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 COPYING for full license details. |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 */ |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 #define M_PI 3.14159265358979323846 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 uniform sampler2D textures[2]; |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
11 uniform mediump float width, height; |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
12 uniform mediump vec2 texsize; |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
13 varying mediump vec2 texcoord; |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
14 varying mediump vec2 screencoord; |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
17 mediump float nrand(vec2 n) { |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 return fract(sin(dot(n.xy, vec2(12.9898, 78.233))) * 43758.5453); |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 } |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
21 mediump float scanline(vec2 texco) |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 { |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
23 return (1.0 - abs(cos(texco.y * texsize.y * M_PI))); |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 } |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
26 mediump vec2 sharp_coord(mediump vec2 texco, mediump vec2 dim, mediump vec2 sharpness) |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 { |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
28 mediump vec2 texcoif = texco * dim; |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
29 mediump vec2 texcoi = floor(texcoif); |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
30 mediump vec2 mu = (texcoif - 0.5) - texcoi; |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
31 mediump vec2 mub = pow(abs(mu) * 2.0, sharpness) * sign(mu) * 0.5; |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 return (texcoi + mub + 0.5) / dim; |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 } |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 void main() |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 { |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
37 mediump float v = 1.0 / texsize.y; |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
38 mediump float yforce = 0.175; |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
39 mediump float vign = length(screencoord); |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 // monitor deformation |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
42 mediump vec2 monitorcoord = (screencoord + screencoord * vign * 0.025); |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 if (monitorcoord.x < -1.0 || monitorcoord.y < -1.0 || monitorcoord.x > 1.0 || monitorcoord.y > 1.0) { |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 gl_FragColor = vec4(0.0); |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 return; |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 } |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
49 mediump vec2 texco = monitorcoord * vec2(0.5*width/texsize.x, -0.5 * height/texsize.y) + vec2(0.5*width/texsize.x, 0.5*height/texsize.y); |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 // mask |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
52 mediump float maskx = 1.0 - pow(abs(monitorcoord.x), 200.0); |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
53 mediump float masky = 1.0 - pow(abs(-monitorcoord.y), 200.0); |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
54 mediump float mask = clamp(maskx * masky, 0.0, 1.0); |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 // sharp texcoord |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
57 mediump vec2 texco_sharp0 = sharp_coord(texco, texsize, vec2(4.0, 8.0)); |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
58 mediump vec2 texco_sharp1 = sharp_coord(texco - vec2(0.0, 0.5 / texsize.y), texsize, vec2(4.0, 8.0)); |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
60 mediump vec4 src0 = texture2D(textures[0], texco_sharp0); |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
61 mediump vec4 src1 = texture2D(textures[1], texco_sharp1); |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 // interlace mix |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
64 mediump float interlace = cos((texco.y * 2.0 * texsize.y) * M_PI); |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
65 mediump vec4 src_mix = mix(src0, src1, interlace * 0.5 + 0.5); |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 // blur |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
68 mediump vec4 src_blur = mix(texture2D(textures[0], texco), texture2D(textures[1], texco), 0.5); |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 #ifdef NO_SCANLINE |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 gl_FragColor = (src_mix * 0.95 + (src_blur * (1.6 - vign * 0.4) * 0.1)) * mask; |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 #else |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 // multisample scanline with grain |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 // TODO: offset grain with time (needs a "frame" uniform) |
1977
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
77 mediump float cosy; |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
78 cosy = scanline(texco + vec2(0.125, v * (nrand(texcoord + vec2(0.0, 512.0/texsize.y)) * 0.25) + 512.0*0.3333/texsize.y)); |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
79 cosy += scanline(texco + vec2(0.25, v * (nrand(texcoord + vec2(0.0, 1024.0/texsize.y)) * 0.25) + 512.0*0.25/texsize.y)); |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
80 cosy += scanline(texco + vec2(0.50, v * (nrand(texcoord + vec2(0.0, 1536.0/texsize.y)) * 0.25) + 512.0*0.6666/texsize.y)); |
f3cca4b3f17a
Allow use of NPOT textures as a config option. Useful for some mobile GPUs
Michael Pavone <pavone@retrodev.com>
parents:
1439
diff
changeset
|
81 cosy += scanline(texco + vec2(0.75, v * (nrand(texcoord + vec2(0.0, 2048.0/texsize.y)) * 0.25) + 512.0*0.75/texsize.y)); |
1392
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 cosy *= 0.25; |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 // final scanline + burn |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 gl_FragColor = ((src_mix * ((1.0 - yforce) + cosy * yforce)) + (src_blur * (1.6 - vign * 0.4) * 0.1)) * mask; |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
87 #endif |
4942da159194
Added a "subtle" CRT shader contributed by Anael Seghezzi
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 } |