-
Notifications
You must be signed in to change notification settings - Fork 253
/
crt-lottes.cg
332 lines (290 loc) · 9.7 KB
/
crt-lottes.cg
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//
// PUBLIC DOMAIN CRT STYLED SCAN-LINE SHADER
//
// by Timothy Lottes
//
// This is more along the style of a really good CGA arcade monitor.
// With RGB inputs instead of NTSC.
// The shadow mask example has the mask rotated 90 degrees for less chromatic aberration.
//
// Left it unoptimized to show the theory behind the algorithm.
//
// It is an example what I personally would want as a display option for pixel art games.
// Please take and use, change, or whatever.
//
// -- config -- //
#pragma parameter hardScan "hardScan" -8.0 -20.0 0.0 1.0 // default, minimum, maximum, optional step
#pragma parameter hardPix "hardPix" -3.0 -20.0 0.0 1.0
#pragma parameter warpX "warpX" 0.031 0.0 0.125 0.01
#pragma parameter warpY "warpY" 0.041 0.0 0.125 0.01
#pragma parameter maskDark "maskDark" 0.5 0.0 2.0 0.1
#pragma parameter maskLight "maskLight" 1.5 0.0 2.0 0.1
#pragma parameter scaleInLinearGamma "scaleInLinearGamma" 1.0 0.0 1.0 1.0
#pragma parameter shadowMask "shadowMask" 3.0 0.0 4.0 1.0
#pragma parameter brightboost "brightness" 1.0 0.0 2.0 0.05
#pragma parameter hardBloomPix "bloom-x soft" -1.5 -2.0 -0.5 0.1
#pragma parameter hardBloomScan "bloom-y soft" -2.0 -4.0 -1.0 0.1
#pragma parameter bloomAmount "bloom amt" 0.15 0.0 1.0 0.05
#pragma parameter shape "filter kernel shape" 2.0 0.0 10.0 0.05
#ifdef PARAMETER_UNIFORM // If the shader implementation understands #pragma parameters, this is defined.
uniform float hardScan;
uniform float hardPix;
uniform float warpX;
uniform float warpY;
uniform float maskDark;
uniform float maskLight;
uniform float scaleInLinearGamma;
uniform float shadowMask;
uniform float brightboost;
uniform float hardBloomScan;
uniform float hardBloomPix;
uniform float bloomAmount;
uniform float shape;
#else
#define hardScan -8.0
#define hardPix -3.0
#define warpX 0.031
#define warpY 0.041
#define maskDark 0.5
#define maskLight 1.5
#define scaleInLinearGamma 1
#define shadowMask 4
#define brightboost 1
#define hardBloomScan -2.0
#define hardBloomPix -1.5
#define bloomAmount 1.0/16.0
#define shape 2.0
#endif
/* COMPATIBILITY
- HLSL compilers
- Cg compilers
- FX11 compilers
*/
//Uncomment to reduce instructions with simpler linearization
//(fixes HD3000 Sandy Bridge IGP)
//#define SIMPLE_LINEAR_GAMMA
#define DO_BLOOM 1
// ------------- //
#include "../../compat_includes.inc"
uniform COMPAT_Texture2D(decal) : TEXUNIT0;
uniform float4x4 modelViewProj;
struct out_vertex
{
float4 position : COMPAT_POS;
float2 texCoord : TEXCOORD0;
#ifndef HLSL_4
float4 Color : COLOR;
#endif
};
out_vertex main_vertex(COMPAT_IN_VERTEX)
{
out_vertex OUT;
#ifdef HLSL_4
float4 position = VIN.position;
float2 texCoord = VIN.texCoord;
#else
OUT.Color = color;
#endif
OUT.position = mul(modelViewProj, position);
OUT.texCoord = texCoord;
return OUT;
}
#define warp float2(warpX,warpY)
//------------------------------------------------------------------------
// sRGB to Linear.
// Assuing using sRGB typed textures this should not be needed.
#ifdef SIMPLE_LINEAR_GAMMA
float ToLinear1(float c)
{
return c;
}
float3 ToLinear(float3 c)
{
return c;
}
float3 ToSrgb(float3 c)
{
return pow(c, 1.0 / 2.2);
}
#else
float ToLinear1(float c)
{
if (scaleInLinearGamma==0) return c;
return(c<=0.04045)?c/12.92:pow((c+0.055)/1.055,2.4);
}
float3 ToLinear(float3 c)
{
if (scaleInLinearGamma==0) return c;
return float3(ToLinear1(c.r),ToLinear1(c.g),ToLinear1(c.b));
}
// Linear to sRGB.
// Assuming using sRGB typed textures this should not be needed.
float ToSrgb1(float c)
{
if (scaleInLinearGamma==0) return c;
return(c<0.0031308?c*12.92:1.055*pow(c,0.41666)-0.055);
}
float3 ToSrgb(float3 c)
{
if (scaleInLinearGamma==0) return c;
return float3(ToSrgb1(c.r),ToSrgb1(c.g),ToSrgb1(c.b));
}
#endif
// Nearest emulated sample given floating point position and texel offset.
// Also zero's off screen.
float3 Fetch(float2 pos, float2 off, float2 texture_size){
pos=(floor(pos*texture_size.xy+off)+float2(0.5,0.5))/texture_size.xy;
#ifdef SIMPLE_LINEAR_GAMMA
return ToLinear(brightboost * pow(COMPAT_SamplePoint(decal,pos.xy).rgb, 2.2));
#else
return ToLinear(brightboost * COMPAT_SamplePoint(decal,pos.xy).rgb);
#endif
}
// Distance in emulated pixels to nearest texel.
float2 Dist(float2 pos, float2 texture_size){pos=pos*texture_size.xy;return -((pos-floor(pos))-float2(0.5, 0.5));}
// 1D Gaussian.
float Gaus(float pos,float scale){return exp2(scale*pow(abs(pos),shape));}
// 3-tap Gaussian filter along horz line.
float3 Horz3(float2 pos, float off, float2 texture_size){
float3 b=Fetch(pos,float2(-1.0,off),texture_size);
float3 c=Fetch(pos,float2( 0.0,off),texture_size);
float3 d=Fetch(pos,float2( 1.0,off),texture_size);
float dst=Dist(pos, texture_size).x;
// Convert distance to weight.
float scale=hardPix;
float wb=Gaus(dst-1.0,scale);
float wc=Gaus(dst+0.0,scale);
float wd=Gaus(dst+1.0,scale);
// Return filtered sample.
return (b*wb+c*wc+d*wd)/(wb+wc+wd);}
// 5-tap Gaussian filter along horz line.
float3 Horz5(float2 pos, float off, float2 texture_size){
float3 a=Fetch(pos,float2(-2.0,off),texture_size);
float3 b=Fetch(pos,float2(-1.0,off),texture_size);
float3 c=Fetch(pos,float2( 0.0,off),texture_size);
float3 d=Fetch(pos,float2( 1.0,off),texture_size);
float3 e=Fetch(pos,float2( 2.0,off),texture_size);
float dst=Dist(pos, texture_size).x;
// Convert distance to weight.
float scale=hardPix;
float wa=Gaus(dst-2.0,scale);
float wb=Gaus(dst-1.0,scale);
float wc=Gaus(dst+0.0,scale);
float wd=Gaus(dst+1.0,scale);
float we=Gaus(dst+2.0,scale);
// Return filtered sample.
return (a*wa+b*wb+c*wc+d*wd+e*we)/(wa+wb+wc+wd+we);}
// 7-tap Gaussian filter along horz line.
float3 Horz7(float2 pos, float off, float2 texture_size){
float3 a=Fetch(pos,float2(-3.0,off),texture_size);
float3 b=Fetch(pos,float2(-2.0,off),texture_size);
float3 c=Fetch(pos,float2(-1.0,off),texture_size);
float3 d=Fetch(pos,float2( 0.0,off),texture_size);
float3 e=Fetch(pos,float2( 1.0,off),texture_size);
float3 f=Fetch(pos,float2( 2.0,off),texture_size);
float3 g=Fetch(pos,float2( 3.0,off),texture_size);
float dst=Dist(pos, texture_size).x;
// Convert distance to weight.
float scale=hardBloomPix;
float wa=Gaus(dst-3.0,scale);
float wb=Gaus(dst-2.0,scale);
float wc=Gaus(dst-1.0,scale);
float wd=Gaus(dst+0.0,scale);
float we=Gaus(dst+1.0,scale);
float wf=Gaus(dst+2.0,scale);
float wg=Gaus(dst+3.0,scale);
// Return filtered sample.
return (a*wa+b*wb+c*wc+d*wd+e*we+f*wf+g*wg)/(wa+wb+wc+wd+we+wf+wg);}
// Return scanline weight.
float Scan(float2 pos,float off, float2 texture_size){
float dst=Dist(pos, texture_size).y;
return Gaus(dst+off,hardScan);}
// Return scanline weight for bloom.
float BloomScan(float2 pos,float off, float2 texture_size){
float dst=Dist(pos, texture_size).y;
return Gaus(dst+off,hardBloomScan);}
// Allow nearest three lines to effect pixel.
float3 Tri(float2 pos, float2 texture_size){
float3 a=Horz3(pos,-1.0, texture_size);
float3 b=Horz5(pos, 0.0, texture_size);
float3 c=Horz3(pos, 1.0, texture_size);
float wa=Scan(pos,-1.0, texture_size);
float wb=Scan(pos, 0.0, texture_size);
float wc=Scan(pos, 1.0, texture_size);
return a*wa+b*wb+c*wc;}
// Small bloom.
float3 Bloom(float2 pos, float2 texture_size){
float3 a=Horz5(pos,-2.0, texture_size);
float3 b=Horz7(pos,-1.0, texture_size);
float3 c=Horz7(pos, 0.0, texture_size);
float3 d=Horz7(pos, 1.0, texture_size);
float3 e=Horz5(pos, 2.0, texture_size);
float wa=BloomScan(pos,-2.0, texture_size);
float wb=BloomScan(pos,-1.0, texture_size);
float wc=BloomScan(pos, 0.0, texture_size);
float wd=BloomScan(pos, 1.0, texture_size);
float we=BloomScan(pos, 2.0, texture_size);
return a*wa+b*wb+c*wc+d*wd+e*we;}
// Distortion of scanlines, and end of screen alpha.
float2 Warp(float2 pos){
pos=pos*2.0-1.0;
pos*=float2(1.0+(pos.y*pos.y)*warp.x,1.0+(pos.x*pos.x)*warp.y);
return pos*0.5+0.5;}
// Shadow mask
float3 Mask(float2 pos){
float3 mask=float3(maskDark,maskDark,maskDark);
// Very compressed TV style shadow mask.
if (shadowMask == 1) {
float mask_line = maskLight;
float odd=0.0;
if(frac(pos.x/6.0)<0.5) odd = 1.0;
if(frac((pos.y+odd)/2.0)<0.5) mask_line = maskDark;
pos.x=frac(pos.x/3.0);
if(pos.x<0.333)mask.r=maskLight;
else if(pos.x<0.666)mask.g=maskLight;
else mask.b=maskLight;
mask *= mask_line;
}
// Aperture-grille.
else if (shadowMask == 2) {
pos.x=frac(pos.x/3.0);
if(pos.x<0.333)mask.r=maskLight;
else if(pos.x<0.666)mask.g=maskLight;
else mask.b=maskLight;
}
// Stretched VGA style shadow mask (same as prior shaders).
else if (shadowMask == 3) {
pos.x+=pos.y*3.0;
pos.x=frac(pos.x/6.0);
if(pos.x<0.333)mask.r=maskLight;
else if(pos.x<0.666)mask.g=maskLight;
else mask.b=maskLight;
}
// VGA style shadow mask.
else if (shadowMask == 4) {
pos.xy=floor(pos.xy*float2(1.0,0.5));
pos.x+=pos.y*3.0;
pos.x=frac(pos.x/6.0);
if(pos.x<0.333)mask.r=maskLight;
else if(pos.x<0.666)mask.g=maskLight;
else mask.b=maskLight;
}
return mask;
}
float4 crt_lottes(float2 texture_size, float2 video_size, float2 output_size, float2 tex, COMPAT_Texture2D(s0))
{
float2 pos=Warp(tex.xy*(texture_size.xy/video_size.xy))*(video_size.xy/texture_size.xy);
float3 outColor = Tri(pos, texture_size);
#ifdef DO_BLOOM
//Add Bloom
outColor.rgb+=Bloom(pos, texture_size)*bloomAmount;
#endif
if(shadowMask)
outColor.rgb*=Mask(floor(tex.xy*(texture_size.xy/video_size.xy)*output_size.xy)+float2(0.5,0.5));
return float4(ToSrgb(outColor.rgb),1.0);
}
float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
return crt_lottes(COMPAT_texture_size, COMPAT_video_size, COMPAT_output_size, VOUT.texCoord, decal);
}
COMPAT_END