duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

crt-lottes.glsl (9344B)


      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 /*
     15 [configuration]
     16 
     17 [OptionRangeFloat]
     18 GUIName = Scanline Weight
     19 OptionName = hardScan
     20 MinValue = -20.0
     21 MaxValue = 0.0
     22 StepAmount = 1.0
     23 DefaultValue = -8.0
     24 
     25 [OptionRangeFloat]
     26 GUIName = Scanline Scale
     27 OptionName = hardPix
     28 MinValue = -20.0
     29 MaxValue = 0.0
     30 StepAmount = 1.0
     31 DefaultValue = -3.0
     32 
     33 [OptionRangeFloat]
     34 GUIName = Screen Warp X
     35 OptionName = warpX
     36 MinValue = 0.0
     37 MaxValue = 0.125
     38 StepAmount = 0.01
     39 DefaultValue = 0.031
     40 
     41 [OptionRangeFloat]
     42 GUIName = Screen Warp Y
     43 OptionName = warpY
     44 MinValue = 0.0
     45 MaxValue = 0.125
     46 StepAmount = 0.01
     47 DefaultValue = 0.041
     48 
     49 [OptionRangeFloat]
     50 GUIName = Mask Dark
     51 OptionName = maskDark
     52 MinValue = 0.0
     53 MaxValue = 2.0
     54 StepAmount = 0.1
     55 DefaultValue = 0.5
     56 
     57 [OptionRangeFloat]
     58 GUIName = Mask Light
     59 OptionName = maskLight
     60 MinValue = 0.0
     61 MaxValue = 2.0
     62 StepAmount = 0.1
     63 DefaultValue = 1.5
     64 
     65 [OptionRangeInteger]
     66 GUIName = Shadow Mask Type
     67 OptionName = shadowMask
     68 MinValue = 0
     69 MaxValue = 4
     70 StepAmount = 1
     71 DefaultValue = 3
     72 
     73 [OptionRangeFloat]
     74 GUIName = Brightess Boost
     75 OptionName = brightBoost
     76 MinValue = 0.0
     77 MaxValue = 2.0
     78 StepAmount = 0.05
     79 DefaultValue = 1.0
     80 
     81 [OptionRangeFloat]
     82 GUIName = Bloom Soft X
     83 OptionName = hardBloomPix
     84 MinValue = -2.0
     85 MaxValue = -0.5
     86 StepAmount = 0.1
     87 DefaultValue = -1.5
     88 
     89 [OptionRangeFloat]
     90 GUIName = Bloom Soft Y
     91 OptionName = hardBloomScan
     92 MinValue = -4.0
     93 MaxValue = -1.0
     94 StepAmount = 0.1
     95 DefaultValue = -2.0
     96 
     97 [OptionRangeFloat]
     98 GUIName = Bloom Amount
     99 OptionName = bloomAmount
    100 MinValue = 0.0
    101 MaxValue = 1.0
    102 StepAmount = 0.05
    103 DefaultValue = 0.15
    104 
    105 [OptionRangeFloat]
    106 GUIName = Filter Kernel Shape
    107 OptionName = shape
    108 MinValue = 0.0
    109 MaxValue = 10.0
    110 StepAmount = 0.05
    111 DefaultValue = 2.0
    112 
    113 [/configuration]
    114 */
    115 
    116 //Uncomment to reduce instructions with simpler linearization
    117 //(fixes HD3000 Sandy Bridge IGP)
    118 #define SIMPLE_LINEAR_GAMMA
    119 #define DO_BLOOM
    120 
    121 // ------------- //
    122 
    123 // sRGB to Linear.
    124 // Assuming using sRGB typed textures this should not be needed.
    125 #ifdef SIMPLE_LINEAR_GAMMA
    126 float ToLinear1(float c)
    127 {
    128     return c;
    129 }
    130 float3 ToLinear(float3 c)
    131 {
    132     return c;
    133 }
    134 float3 ToSrgb(float3 c)
    135 {
    136     return pow(c, float3(1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2));
    137 }
    138 #else
    139 float ToLinear1(float c)
    140 {
    141     return(c<=0.04045) ? c/12.92 : pow((c + 0.055)/1.055, 2.4);
    142 }
    143 
    144 float3 ToLinear(float3 c)
    145 {
    146     return float3(ToLinear1(c.r), ToLinear1(c.g), ToLinear1(c.b));
    147 }
    148 
    149 // Linear to sRGB.
    150 // Assuming using sRGB typed textures this should not be needed.
    151 float ToSrgb1(float c)
    152 {
    153     return(c<0.0031308 ? c*12.92 : 1.055*pow(c, 0.41666) - 0.055);
    154 }
    155 
    156 float3 ToSrgb(float3 c)
    157 {
    158     return float3(ToSrgb1(c.r), ToSrgb1(c.g), ToSrgb1(c.b));
    159 }
    160 #endif
    161 
    162 // Nearest emulated sample given floating point position and texel offset.
    163 // Also zero's off screen.
    164 float3 Fetch(float2 pos,float2 off){
    165   pos=(floor(pos*GetResolution()+off)+float2(0.5,0.5))*GetInvResolution();
    166 #ifdef SIMPLE_LINEAR_GAMMA
    167   return ToLinear(GetOption(brightBoost) * pow(SampleLocation(pos.xy).rgb, float3(2.2, 2.2, 2.2)));
    168 #else
    169   return ToLinear(GetOption(brightBoost) * SampleLocation(pos.xy).rgb);
    170 #endif
    171 }
    172 
    173 // Distance in emulated pixels to nearest texel.
    174 float2 Dist(float2 pos)
    175 {
    176     pos = pos * (GetNativeSize() * GetWindowToViewportRatio());
    177     
    178     return -((pos - floor(pos)) - float2(0.5, 0.5));
    179 }
    180     
    181 // 1D Gaussian.
    182 float Gaus(float pos, float scale)
    183 {
    184     return exp2(scale*pow(abs(pos), GetOption(shape)));
    185 }
    186 
    187 // 3-tap Gaussian filter along horz line.
    188 float3 Horz3(float2 pos, float off)
    189 {
    190     float3 b    = Fetch(pos, float2(-1.0, off));
    191     float3 c    = Fetch(pos, float2( 0.0, off));
    192     float3 d    = Fetch(pos, float2( 1.0, off));
    193     float dst = Dist(pos).x;
    194 
    195     // Convert distance to weight.
    196     float scale = GetOption(hardPix);
    197     float wb = Gaus(dst-1.0,scale);
    198     float wc = Gaus(dst+0.0,scale);
    199     float wd = Gaus(dst+1.0,scale);
    200 
    201     // Return filtered sample.
    202     return (b*wb+c*wc+d*wd)/(wb+wc+wd);
    203 }
    204 
    205 // 5-tap Gaussian filter along horz line.
    206 float3 Horz5(float2 pos,float off){
    207     float3 a = Fetch(pos,float2(-2.0, off));
    208     float3 b = Fetch(pos,float2(-1.0, off));
    209     float3 c = Fetch(pos,float2( 0.0, off));
    210     float3 d = Fetch(pos,float2( 1.0, off));
    211     float3 e = Fetch(pos,float2( 2.0, off));
    212     
    213     float dst = Dist(pos).x;
    214     // Convert distance to weight.
    215     float scale = GetOption(hardPix);
    216     float wa = Gaus(dst - 2.0, scale);
    217     float wb = Gaus(dst - 1.0, scale);
    218     float wc = Gaus(dst + 0.0, scale);
    219     float wd = Gaus(dst + 1.0, scale);
    220     float we = Gaus(dst + 2.0, scale);
    221     
    222     // Return filtered sample.
    223     return (a*wa+b*wb+c*wc+d*wd+e*we)/(wa+wb+wc+wd+we);
    224 }
    225   
    226 // 7-tap Gaussian filter along horz line.
    227 float3 Horz7(float2 pos,float off)
    228 {
    229     float3 a = Fetch(pos, float2(-3.0, off));
    230     float3 b = Fetch(pos, float2(-2.0, off));
    231     float3 c = Fetch(pos, float2(-1.0, off));
    232     float3 d = Fetch(pos, float2( 0.0, off));
    233     float3 e = Fetch(pos, float2( 1.0, off));
    234     float3 f = Fetch(pos, float2( 2.0, off));
    235     float3 g = Fetch(pos, float2( 3.0, off));
    236 
    237     float dst = Dist(pos).x;
    238     // Convert distance to weight.
    239     float scale = GetOption(hardBloomPix);
    240     float wa = Gaus(dst - 3.0, scale);
    241     float wb = Gaus(dst - 2.0, scale);
    242     float wc = Gaus(dst - 1.0, scale);
    243     float wd = Gaus(dst + 0.0, scale);
    244     float we = Gaus(dst + 1.0, scale);
    245     float wf = Gaus(dst + 2.0, scale);
    246     float wg = Gaus(dst + 3.0, scale);
    247 
    248     // Return filtered sample.
    249     return (a*wa+b*wb+c*wc+d*wd+e*we+f*wf+g*wg)/(wa+wb+wc+wd+we+wf+wg);
    250 }
    251   
    252 // Return scanline weight.
    253 float Scan(float2 pos, float off)
    254 {
    255     float dst = Dist(pos).y;
    256 
    257     return Gaus(dst + off, GetOption(hardScan));
    258 }
    259   
    260 // Return scanline weight for bloom.
    261 float BloomScan(float2 pos, float off)
    262 {
    263     float dst = Dist(pos).y;
    264     
    265     return Gaus(dst + off, GetOption(hardBloomScan));
    266 }
    267 
    268 // Allow nearest three lines to effect pixel.
    269 float3 Tri(float2 pos)
    270 {
    271     float3 a = Horz3(pos,-1.0);
    272     float3 b = Horz5(pos, 0.0);
    273     float3 c = Horz3(pos, 1.0);
    274     
    275     float wa = Scan(pos,-1.0); 
    276     float wb = Scan(pos, 0.0);
    277     float wc = Scan(pos, 1.0);
    278     
    279     return a*wa + b*wb + c*wc;
    280 }
    281   
    282 // Small bloom.
    283 float3 Bloom(float2 pos)
    284 {
    285     float3 a = Horz5(pos,-2.0);
    286     float3 b = Horz7(pos,-1.0);
    287     float3 c = Horz7(pos, 0.0);
    288     float3 d = Horz7(pos, 1.0);
    289     float3 e = Horz5(pos, 2.0);
    290 
    291     float wa = BloomScan(pos,-2.0);
    292     float wb = BloomScan(pos,-1.0); 
    293     float wc = BloomScan(pos, 0.0);
    294     float wd = BloomScan(pos, 1.0);
    295     float we = BloomScan(pos, 2.0);
    296 
    297     return a*wa+b*wb+c*wc+d*wd+e*we;
    298 }
    299   
    300 // Distortion of scanlines, and end of screen alpha.
    301 float2 Warp(float2 pos)
    302 {
    303     pos  = pos*2.0-1.0;    
    304     pos *= float2(1.0 + (pos.y*pos.y)*GetOption(warpX), 1.0 + (pos.x*pos.x)*GetOption(warpY));
    305     
    306     return pos*0.5 + 0.5;
    307 }
    308   
    309 // Shadow mask.
    310 float3 Mask(float2 pos)
    311 {
    312     float3 mask = float3(GetOption(maskDark), GetOption(maskDark), GetOption(maskDark));
    313   
    314     // Very compressed TV style shadow mask.
    315     if (GetOption(shadowMask) == 1) 
    316     {
    317         float line_ = GetOption(maskLight);
    318         float odd = 0.0;
    319         
    320         if (fract(pos.x*0.166666666) < 0.5) odd = 1.0;
    321         if (fract((pos.y + odd) * 0.5) < 0.5) line_ = GetOption(maskDark);  
    322         
    323         pos.x = fract(pos.x*0.333333333);
    324 
    325         if      (pos.x < 0.333) mask.r = GetOption(maskLight);
    326         else if (pos.x < 0.666) mask.g = GetOption(maskLight);
    327         else                    mask.b = GetOption(maskLight);
    328         mask*=line_;  
    329     } 
    330 
    331     // Aperture-grille.
    332     else if (GetOption(shadowMask) == 2) 
    333     {
    334         pos.x = fract(pos.x*0.333333333);
    335 
    336         if      (pos.x < 0.333) mask.r = GetOption(maskLight);
    337         else if (pos.x < 0.666) mask.g = GetOption(maskLight);
    338         else                    mask.b = GetOption(maskLight);
    339     } 
    340 
    341     // Stretched VGA style shadow mask (same as prior shaders).
    342     else if (GetOption(shadowMask) == 3) 
    343     {
    344         pos.x += pos.y*3.0;
    345         pos.x  = fract(pos.x*0.166666666);
    346 
    347         if      (pos.x < 0.333) mask.r = GetOption(maskLight);
    348         else if (pos.x < 0.666) mask.g = GetOption(maskLight);
    349         else                    mask.b = GetOption(maskLight);
    350     }
    351 
    352     // VGA style shadow mask.
    353     else if (GetOption(shadowMask) == 4) 
    354     {
    355         pos.xy  = floor(pos.xy*float2(1.0, 0.5));
    356         pos.x  += pos.y*3.0;
    357         pos.x   = fract(pos.x*0.166666666);
    358 
    359         if      (pos.x < 0.333) mask.r = GetOption(maskLight);
    360         else if (pos.x < 0.666) mask.g = GetOption(maskLight);
    361         else                    mask.b = GetOption(maskLight);
    362     }
    363 
    364     return mask;
    365 }
    366 
    367 void main()
    368 {
    369     float2 pos = Warp(GetCoordinates());
    370     float3 outColor = Tri(pos);
    371 
    372 #ifdef DO_BLOOM
    373     //Add Bloom
    374     outColor.rgb += Bloom(pos)*GetOption(bloomAmount);
    375 #endif
    376 
    377     if (GetOption(shadowMask) > 0)
    378         outColor.rgb *= Mask(gl_FragCoord.xy * 1.000001);
    379     
    380     SetOutput(float4(ToSrgb(outColor.rgb), 1.0));
    381 }