1// PUBLIC DOMAIN CRT STYLED SCAN-LINE SHADER
2//
3//   by Timothy Lottes
4//
5// This is more along the style of a really good CGA arcade monitor.
6// With RGB inputs instead of NTSC.
7// The shadow mask example has the mask rotated 90 degrees for less chromatic aberration.
8//
9// Left it unoptimized to show the theory behind the algorithm.
10//
11// It is an example what I personally would want as a display option for pixel art games.
12// Please take and use, change, or whatever.
13
14// Parameter lines go here:
15#pragma parameter hardScan "hardScan" -8.0 -20.0 0.0 1.0
16#pragma parameter hardPix "hardPix" -3.0 -20.0 0.0 1.0
17#pragma parameter warpX "warpX" 0.031 0.0 0.125 0.01
18#pragma parameter warpY "warpY" 0.041 0.0 0.125 0.01
19#pragma parameter maskDark "maskDark" 0.5 0.0 2.0 0.1
20#pragma parameter maskLight "maskLight" 1.5 0.0 2.0 0.1
21#pragma parameter scaleInLinearGamma "scaleInLinearGamma" 1.0 0.0 1.0 1.0
22#pragma parameter shadowMask "shadowMask" 3.0 0.0 4.0 1.0
23#pragma parameter brightBoost "brightness boost" 1.0 0.0 2.0 0.05
24#pragma parameter hardBloomPix "bloom-x soft" -1.5 -2.0 -0.5 0.1
25#pragma parameter hardBloomScan "bloom-y soft" -2.0 -4.0 -1.0 0.1
26#pragma parameter bloomAmount "bloom ammount" 0.15 0.0 1.0 0.05
27#pragma parameter shape "filter kernel shape" 2.0 0.0 10.0 0.05
28
29#if defined(VERTEX)
30
31#if __VERSION__ >= 130
32#define COMPAT_VARYING out
33#define COMPAT_ATTRIBUTE in
34#define COMPAT_TEXTURE texture
35#else
36#define COMPAT_VARYING varying
37#define COMPAT_ATTRIBUTE attribute
38#define COMPAT_TEXTURE texture2D
39#endif
40
41#ifdef GL_ES
42#define COMPAT_PRECISION mediump
43#else
44#define COMPAT_PRECISION
45#endif
46
47COMPAT_ATTRIBUTE vec4 VertexCoord;
48COMPAT_ATTRIBUTE vec4 COLOR;
49COMPAT_ATTRIBUTE vec4 TexCoord;
50COMPAT_VARYING vec4 COL0;
51COMPAT_VARYING vec4 TEX0;
52
53uniform mat4 MVPMatrix;
54uniform COMPAT_PRECISION int FrameDirection;
55uniform COMPAT_PRECISION int FrameCount;
56uniform COMPAT_PRECISION vec2 OutputSize;
57uniform COMPAT_PRECISION vec2 TextureSize;
58uniform COMPAT_PRECISION vec2 InputSize;
59
60// vertex compatibility #defines
61#define vTexCoord TEX0.xy
62#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
63#define outsize vec4(OutputSize, 1.0 / OutputSize)
64
65void main()
66{
67    gl_Position = MVPMatrix * VertexCoord;
68    TEX0.xy = TexCoord.xy;
69}
70
71#elif defined(FRAGMENT)
72
73#if __VERSION__ >= 130
74#define COMPAT_VARYING in
75#define COMPAT_TEXTURE texture
76out vec4 FragColor;
77#else
78#define COMPAT_VARYING varying
79#define FragColor gl_FragColor
80#define COMPAT_TEXTURE texture2D
81#endif
82
83#ifdef GL_ES
84#ifdef GL_FRAGMENT_PRECISION_HIGH
85precision highp float;
86#else
87precision mediump float;
88#endif
89#define COMPAT_PRECISION mediump
90#else
91#define COMPAT_PRECISION
92#endif
93
94uniform COMPAT_PRECISION int FrameDirection;
95uniform COMPAT_PRECISION int FrameCount;
96uniform COMPAT_PRECISION vec2 OutputSize;
97uniform COMPAT_PRECISION vec2 TextureSize;
98uniform COMPAT_PRECISION vec2 InputSize;
99uniform sampler2D Texture;
100COMPAT_VARYING vec4 TEX0;
101
102// fragment compatibility #defines
103#define Source Texture
104#define vTexCoord TEX0.xy
105
106#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
107#define outsize vec4(OutputSize, 1.0 / OutputSize)
108
109#ifdef PARAMETER_UNIFORM
110// All parameter floats need to have COMPAT_PRECISION in front of them
111uniform COMPAT_PRECISION float hardScan;
112uniform COMPAT_PRECISION float hardPix;
113uniform COMPAT_PRECISION float warpX;
114uniform COMPAT_PRECISION float warpY;
115uniform COMPAT_PRECISION float maskDark;
116uniform COMPAT_PRECISION float maskLight;
117uniform COMPAT_PRECISION float scaleInLinearGamma;
118uniform COMPAT_PRECISION float shadowMask;
119uniform COMPAT_PRECISION float brightBoost;
120uniform COMPAT_PRECISION float hardBloomPix;
121uniform COMPAT_PRECISION float hardBloomScan;
122uniform COMPAT_PRECISION float bloomAmount;
123uniform COMPAT_PRECISION float shape;
124#else
125#define hardScan -8.0
126#define hardPix -3.0
127#define warpX 0.031
128#define warpY 0.041
129#define maskDark 0.5
130#define maskLight 1.5
131#define scaleInLinearGamma 1.0
132#define shadowMask 3.0
133#define brightBoost 1.0
134#define hardBloomPix -1.5
135#define hardBloomScan -2.0
136#define bloomAmount 0.15
137#define shape 2.0
138#endif
139
140//Uncomment to reduce instructions with simpler linearization
141//(fixes HD3000 Sandy Bridge IGP)
142//#define SIMPLE_LINEAR_GAMMA
143#define DO_BLOOM
144
145// ------------- //
146
147// sRGB to Linear.
148// Assuming using sRGB typed textures this should not be needed.
149#ifdef SIMPLE_LINEAR_GAMMA
150float ToLinear1(float c)
151{
152    return c;
153}
154vec3 ToLinear(vec3 c)
155{
156    return c;
157}
158vec3 ToSrgb(vec3 c)
159{
160    return pow(c, vec3(1.0 / 2.2));
161}
162#else
163float ToLinear1(float c)
164{
165    if (scaleInLinearGamma == 0.)
166        return c;
167
168    return(c<=0.04045) ? c/12.92 : pow((c + 0.055)/1.055, 2.4);
169}
170
171vec3 ToLinear(vec3 c)
172{
173    if (scaleInLinearGamma==0.)
174        return c;
175
176    return vec3(ToLinear1(c.r), ToLinear1(c.g), ToLinear1(c.b));
177}
178
179// Linear to sRGB.
180// Assuming using sRGB typed textures this should not be needed.
181float ToSrgb1(float c)
182{
183    if (scaleInLinearGamma == 0.)
184        return c;
185
186    return(c<0.0031308 ? c*12.92 : 1.055*pow(c, 0.41666) - 0.055);
187}
188
189vec3 ToSrgb(vec3 c)
190{
191    if (scaleInLinearGamma == 0.)
192        return c;
193
194    return vec3(ToSrgb1(c.r), ToSrgb1(c.g), ToSrgb1(c.b));
195}
196#endif
197
198// Nearest emulated sample given floating point position and texel offset.
199// Also zero's off screen.
200vec3 Fetch(vec2 pos,vec2 off){
201  pos=(floor(pos*SourceSize.xy+off)+vec2(0.5,0.5))/SourceSize.xy;
202#ifdef SIMPLE_LINEAR_GAMMA
203  return ToLinear(brightBoost * pow(COMPAT_TEXTURE(Source,pos.xy).rgb, vec3(2.2)));
204#else
205  return ToLinear(brightBoost * COMPAT_TEXTURE(Source,pos.xy).rgb);
206#endif
207}
208
209// Distance in emulated pixels to nearest texel.
210vec2 Dist(vec2 pos)
211{
212    pos = pos*SourceSize.xy;
213
214    return -((pos - floor(pos)) - vec2(0.5));
215}
216
217// 1D Gaussian.
218float Gaus(float pos, float scale)
219{
220    return exp2(scale*pow(abs(pos), shape));
221}
222
223// 3-tap Gaussian filter along horz line.
224vec3 Horz3(vec2 pos, float off)
225{
226    vec3 b    = Fetch(pos, vec2(-1.0, off));
227    vec3 c    = Fetch(pos, vec2( 0.0, off));
228    vec3 d    = Fetch(pos, vec2( 1.0, off));
229    float dst = Dist(pos).x;
230
231    // Convert distance to weight.
232    float scale = hardPix;
233    float wb = Gaus(dst-1.0,scale);
234    float wc = Gaus(dst+0.0,scale);
235    float wd = Gaus(dst+1.0,scale);
236
237    // Return filtered sample.
238    return (b*wb+c*wc+d*wd)/(wb+wc+wd);
239}
240
241// 5-tap Gaussian filter along horz line.
242vec3 Horz5(vec2 pos,float off){
243    vec3 a = Fetch(pos,vec2(-2.0, off));
244    vec3 b = Fetch(pos,vec2(-1.0, off));
245    vec3 c = Fetch(pos,vec2( 0.0, off));
246    vec3 d = Fetch(pos,vec2( 1.0, off));
247    vec3 e = Fetch(pos,vec2( 2.0, off));
248
249    float dst = Dist(pos).x;
250    // Convert distance to weight.
251    float scale = hardPix;
252    float wa = Gaus(dst - 2.0, scale);
253    float wb = Gaus(dst - 1.0, scale);
254    float wc = Gaus(dst + 0.0, scale);
255    float wd = Gaus(dst + 1.0, scale);
256    float we = Gaus(dst + 2.0, scale);
257
258    // Return filtered sample.
259    return (a*wa+b*wb+c*wc+d*wd+e*we)/(wa+wb+wc+wd+we);
260}
261
262// 7-tap Gaussian filter along horz line.
263vec3 Horz7(vec2 pos,float off)
264{
265    vec3 a = Fetch(pos, vec2(-3.0, off));
266    vec3 b = Fetch(pos, vec2(-2.0, off));
267    vec3 c = Fetch(pos, vec2(-1.0, off));
268    vec3 d = Fetch(pos, vec2( 0.0, off));
269    vec3 e = Fetch(pos, vec2( 1.0, off));
270    vec3 f = Fetch(pos, vec2( 2.0, off));
271    vec3 g = Fetch(pos, vec2( 3.0, off));
272
273    float dst = Dist(pos).x;
274    // Convert distance to weight.
275    float scale = hardBloomPix;
276    float wa = Gaus(dst - 3.0, scale);
277    float wb = Gaus(dst - 2.0, scale);
278    float wc = Gaus(dst - 1.0, scale);
279    float wd = Gaus(dst + 0.0, scale);
280    float we = Gaus(dst + 1.0, scale);
281    float wf = Gaus(dst + 2.0, scale);
282    float wg = Gaus(dst + 3.0, scale);
283
284    // Return filtered sample.
285    return (a*wa+b*wb+c*wc+d*wd+e*we+f*wf+g*wg)/(wa+wb+wc+wd+we+wf+wg);
286}
287
288// Return scanline weight.
289float Scan(vec2 pos, float off)
290{
291    float dst = Dist(pos).y;
292
293    return Gaus(dst + off, hardScan);
294}
295
296// Return scanline weight for bloom.
297float BloomScan(vec2 pos, float off)
298{
299    float dst = Dist(pos).y;
300
301    return Gaus(dst + off, hardBloomScan);
302}
303
304// Allow nearest three lines to effect pixel.
305vec3 Tri(vec2 pos)
306{
307    vec3 a = Horz3(pos,-1.0);
308    vec3 b = Horz5(pos, 0.0);
309    vec3 c = Horz3(pos, 1.0);
310
311    float wa = Scan(pos,-1.0);
312    float wb = Scan(pos, 0.0);
313    float wc = Scan(pos, 1.0);
314
315    return a*wa + b*wb + c*wc;
316}
317
318// Small bloom.
319vec3 Bloom(vec2 pos)
320{
321    vec3 a = Horz5(pos,-2.0);
322    vec3 b = Horz7(pos,-1.0);
323    vec3 c = Horz7(pos, 0.0);
324    vec3 d = Horz7(pos, 1.0);
325    vec3 e = Horz5(pos, 2.0);
326
327    float wa = BloomScan(pos,-2.0);
328    float wb = BloomScan(pos,-1.0);
329    float wc = BloomScan(pos, 0.0);
330    float wd = BloomScan(pos, 1.0);
331    float we = BloomScan(pos, 2.0);
332
333    return a*wa+b*wb+c*wc+d*wd+e*we;
334}
335
336// Distortion of scanlines, and end of screen alpha.
337vec2 Warp(vec2 pos)
338{
339    pos  = pos*2.0-1.0;
340    pos *= vec2(1.0 + (pos.y*pos.y)*warpX, 1.0 + (pos.x*pos.x)*warpY);
341
342    return pos*0.5 + 0.5;
343}
344
345// Shadow mask.
346vec3 Mask(vec2 pos)
347{
348    vec3 mask = vec3(maskDark, maskDark, maskDark);
349
350    // Very compressed TV style shadow mask.
351    if (shadowMask == 1.0)
352    {
353        float line = maskLight;
354        float odd = 0.0;
355
356        if (fract(pos.x*0.166666666) < 0.5) odd = 1.0;
357        if (fract((pos.y + odd) * 0.5) < 0.5) line = maskDark;
358
359        pos.x = fract(pos.x*0.333333333);
360
361        if      (pos.x < 0.333) mask.r = maskLight;
362        else if (pos.x < 0.666) mask.g = maskLight;
363        else                    mask.b = maskLight;
364        mask*=line;
365    }
366
367    // Aperture-grille.
368    else if (shadowMask == 2.0)
369    {
370        pos.x = fract(pos.x*0.333333333);
371
372        if      (pos.x < 0.333) mask.r = maskLight;
373        else if (pos.x < 0.666) mask.g = maskLight;
374        else                    mask.b = maskLight;
375    }
376
377    // Stretched VGA style shadow mask (same as prior shaders).
378    else if (shadowMask == 3.0)
379    {
380        pos.x += pos.y*3.0;
381        pos.x  = fract(pos.x*0.166666666);
382
383        if      (pos.x < 0.333) mask.r = maskLight;
384        else if (pos.x < 0.666) mask.g = maskLight;
385        else                    mask.b = maskLight;
386    }
387
388    // VGA style shadow mask.
389    else if (shadowMask == 4.0)
390    {
391        pos.xy  = floor(pos.xy*vec2(1.0, 0.5));
392        pos.x  += pos.y*3.0;
393        pos.x   = fract(pos.x*0.166666666);
394
395        if      (pos.x < 0.333) mask.r = maskLight;
396        else if (pos.x < 0.666) mask.g = maskLight;
397        else                    mask.b = maskLight;
398    }
399
400    return mask;
401}
402
403void main()
404{
405    vec2 pos = Warp(TEX0.xy*(TextureSize.xy/InputSize.xy))*(InputSize.xy/TextureSize.xy);
406    vec3 outColor = Tri(pos);
407
408#ifdef DO_BLOOM
409    //Add Bloom
410    outColor.rgb += Bloom(pos)*bloomAmount;
411#endif
412
413    if (shadowMask > 0.0)
414        outColor.rgb *= Mask(gl_FragCoord.xy * 1.000001);
415
416#ifdef GL_ES    /* TODO/FIXME - hacky clamp fix */
417    vec2 bordertest = (pos);
418    if ( bordertest.x > 0.0001 && bordertest.x < 0.9999 && bordertest.y > 0.0001 && bordertest.y < 0.9999)
419        outColor.rgb = outColor.rgb;
420    else
421        outColor.rgb = vec3(0.0);
422#endif
423    FragColor = vec4(ToSrgb(outColor.rgb), 1.0);
424}
425#endif
426