/* ====================== Based on Looki's' CRT shader, which was based on cgwg's CRT shader Copyright (C) 2010 cgwg http://filthypants.blogspot.com/2010/12/crt-pixel-shader-filter-for-snes.html ====================== */ sampler2D rubyTexture; bool useInput; bool useOutput; float fPixelWidth, fPixelHeight; float2 rubyTextureSize = 128; // Abbreviations // #define TEX2D(c) tex2D(rubyTexture, (c)) #define TEX2D(c) pow(tex2D(rubyTexture, (c)), 2.2) #define FIX(c) max(abs(c), 1e-6); #define PI 3.141592653589 // Controls the intensity of the barrel distortion used to emulate the // curvature of a CRT. 0.0 is perfectly flat, 1.0 is annoyingly // distorted, higher values are increasingly ridiculous. float distortion = 0.1; // Apply radial distortion to the given coordinate. float2 radialDistortion(float2 coord) { float2 cc = coord - 0.5; float dist = dot(cc, cc) * distortion; return (coord + cc * (1.0 + dist) * dist); } float4 ps_main( float2 texCoord : TEXCOORD0 ) : COLOR { // Here's a helpful diagram to keep in mind while trying to // understand the code: // // | | | | | // ------------------------------- // | | | | | // | 01 | 11 | 21 | 31 | <-- current scanline // | | @ | | | // ------------------------------- // | | | | | // | 02 | 12 | 22 | 32 | <-- next scanline // | | | | | // ------------------------------- // | | | | | // // Each character-cell represents a pixel on the output // surface, "@" represents the current pixel (always somewhere // in the bottom half of the current scan-line, or the top-half // of the next scanline). The grid of lines represents the // edges of the texels of the underlying texture. rubyTextureSize = 1.0 / float2(fPixelWidth,fPixelHeight); // The size of one texel, in texture-coordinates. float2 one = 1.0 / rubyTextureSize; // Texture coordinates of the texel containing the active pixel float2 xy = radialDistortion(texCoord); // Of all the pixels that are mapped onto the texel we are // currently rendering, which pixel are we currently rendering? float2 uv_ratio = frac(xy * rubyTextureSize) - 0.5; // Snap to the center of the underlying texel. xy = (floor(xy * rubyTextureSize) + 0.5) / rubyTextureSize; // Calculate Lanczos scaling coefficients describing the effect // of various neighbour texels in a scanline on the current // pixel. float4 coeffs = PI * float4(1.0 + uv_ratio.x, uv_ratio.x, 1.0 - uv_ratio.x, 2.0 - uv_ratio.x); coeffs = FIX(coeffs); // Prevent division by zero coeffs = 2.0 * sin(coeffs) * sin(coeffs / 2.0) / (coeffs * coeffs); coeffs /= dot(coeffs, 1.0); // Normalize // Calculate the effective colour of the current and next // scanlines at the horizontal location of the current pixel, // using the Lanczos coefficients above. float4 col = clamp(coeffs.x * TEX2D(xy + float2(-one.x, 0.0)) + coeffs.y * TEX2D(xy) + coeffs.z * TEX2D(xy + float2(one.x, 0.0)) + coeffs.w * TEX2D(xy + float2(2.0 * one.x, 0.0)), 0.0, 1.0); float4 col2 = clamp(coeffs.x * TEX2D(xy + float2(-one.x, one.y)) + coeffs.y * TEX2D(xy + float2(0.0, one.y)) + coeffs.z * TEX2D(xy + one) + coeffs.w * TEX2D(xy + float2(2.0 * one.x, one.y)), 0.0, 1.0); float3 mul_res = (col + col2).xyz; // dot-mask emulation: // Output pixels are alternately tinted green and magenta. float3 dotMaskWeights = lerp( float3(1.05, 0.75, 1.05), float3(0.75, 1.05, 0.75), floor(fmod(texCoord.x * rubyTextureSize.x, 2.0)) ); mul_res *= dotMaskWeights; // Convert the image gamma for display on our output device. mul_res = pow(mul_res, 1.0 / 2.2); return float4(mul_res, 1.0); } technique tech_main { pass P0 { PixelShader = compile ps_2_a ps_main(); } }