Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // from https://github.com/libretro/common-shaders/blob/master/windowed/shaders/jinc2.cg
- uniform float4x4 ViewProj;
- uniform texture2d image;
- uniform float4x4 color_matrix;
- uniform float3 color_range_min = {0.0, 0.0, 0.0};
- uniform float3 color_range_max = {1.0, 1.0, 1.0};
- uniform float2 base_dimension_i;
- sampler_state textureSampler {
- Filter = MIN_MAG_MIP_POINT;
- AddressU = Clamp;
- AddressV = Clamp;
- };
- struct VertData {
- float4 pos : POSITION;
- float2 uv : TEXCOORD0;
- };
- VertData VSDefault(VertData v_in)
- {
- VertData vert_out;
- vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
- vert_out.uv = v_in.uv;
- return vert_out;
- }
- #define JINC2_WINDOW_SINC 0.44
- #define JINC2_SINC 0.82
- #define JINC2_AR_STRENGTH 0.5
- #define halfpi 1.5707963267948966192313216916398
- #define pi 3.1415926535897932384626433832795
- #define wa JINC2_WINDOW_SINC*pi
- #define wb JINC2_SINC*pi
- // Calculates the distance between two points
- float d(float2 pt1, float2 pt2)
- {
- float2 v = pt2 - pt1;
- return sqrt(dot(v,v));
- }
- float3 min4(float3 a, float3 b, float3 c, float3 d)
- {
- return min(a, min(b, min(c, d)));
- }
- float3 max4(float3 a, float3 b, float3 c, float3 d)
- {
- return max(a, max(b, max(c, d)));
- }
- float4 resampler(float4 x)
- {
- float4 res;
- res = (x==float4(0.0, 0.0, 0.0, 0.0)) ? (float4)(wa*wb) : sin(x*wa)*sin(x*wb)/(x*x);
- return res;
- }
- float4 DrawBicubic(VertData v_in)
- {
- float2 size = 1.0/base_dimension_i;
- float3 color;
- float4x4 weights;
- float2 dx = float2(1.0, 0.0);
- float2 dy = float2(0.0, 1.0);
- float2 pc = v_in.uv*size;
- float2 tc = (floor(pc-float2(0.5,0.5))+float2(0.5,0.5));
- weights[0] = resampler(float4(d(pc, tc -dx -dy), d(pc, tc -dy), d(pc, tc +dx -dy), d(pc, tc+2.0*dx -dy)));
- weights[1] = resampler(float4(d(pc, tc -dx ), d(pc, tc ), d(pc, tc +dx ), d(pc, tc+2.0*dx )));
- weights[2] = resampler(float4(d(pc, tc -dx +dy), d(pc, tc +dy), d(pc, tc +dx +dy), d(pc, tc+2.0*dx +dy)));
- weights[3] = resampler(float4(d(pc, tc -dx+2.0*dy), d(pc, tc +2.0*dy), d(pc, tc +dx+2.0*dy), d(pc, tc+2.0*dx+2.0*dy)));
- dx = dx/size;
- dy = dy/size;
- tc = tc/size;
- // reading the texels
- float3 c00 = pow(image.SampleLevel(textureSampler, tc -dx -dy, 0), 2.2).xyz;
- float3 c10 = pow(image.SampleLevel(textureSampler, tc -dy, 0), 2.2).xyz;
- float3 c20 = pow(image.SampleLevel(textureSampler, tc +dx -dy, 0), 2.2).xyz;
- float3 c30 = pow(image.SampleLevel(textureSampler, tc+2.0*dx -dy, 0), 2.2).xyz;
- float3 c01 = pow(image.SampleLevel(textureSampler, tc -dx , 0), 2.2).xyz;
- float3 c11 = pow(image.SampleLevel(textureSampler, tc , 0), 2.2).xyz;
- float3 c21 = pow(image.SampleLevel(textureSampler, tc +dx , 0), 2.2).xyz;
- float3 c31 = pow(image.SampleLevel(textureSampler, tc+2.0*dx , 0), 2.2).xyz;
- float3 c02 = pow(image.SampleLevel(textureSampler, tc -dx +dy, 0), 2.2).xyz;
- float3 c12 = pow(image.SampleLevel(textureSampler, tc +dy, 0), 2.2).xyz;
- float3 c22 = pow(image.SampleLevel(textureSampler, tc +dx +dy, 0), 2.2).xyz;
- float3 c32 = pow(image.SampleLevel(textureSampler, tc+2.0*dx +dy, 0), 2.2).xyz;
- float3 c03 = pow(image.SampleLevel(textureSampler, tc -dx+2.0*dy, 0), 2.2).xyz;
- float3 c13 = pow(image.SampleLevel(textureSampler, tc +2.0*dy, 0), 2.2).xyz;
- float3 c23 = pow(image.SampleLevel(textureSampler, tc +dx+2.0*dy, 0), 2.2).xyz;
- float3 c33 = pow(image.SampleLevel(textureSampler, tc+2.0*dx+2.0*dy, 0), 2.2).xyz;
- // Get min/max samples
- float3 min_sample = min4(c11, c21, c12, c22);
- float3 max_sample = max4(c11, c21, c12, c22);
- color = mul(weights[0], float4x3(c00, c10, c20, c30));
- color+= mul(weights[1], float4x3(c01, c11, c21, c31));
- color+= mul(weights[2], float4x3(c02, c12, c22, c32));
- color+= mul(weights[3], float4x3(c03, c13, c23, c33));
- color = color/(dot(mul(weights, (float4)(1)), 1));
- // Anti-ringing
- float3 aux = color;
- color = clamp(color, min_sample, max_sample);
- color = lerp(aux, color, JINC2_AR_STRENGTH);
- // final sum and weight normalization
- return pow(float4(color, 1), 1/2.2);
- }
- float4 PSDrawBicubicRGBA(VertData v_in) : TARGET
- {
- return DrawBicubic(v_in);
- }
- float4 PSDrawBicubicMatrix(VertData v_in) : TARGET
- {
- float4 rgba = DrawBicubic(v_in);
- float4 yuv;
- yuv.xyz = clamp(rgba.xyz, color_range_min, color_range_max);
- return saturate(mul(float4(yuv.xyz, 1.0), color_matrix));
- }
- technique Draw
- {
- pass
- {
- vertex_shader = VSDefault(v_in);
- pixel_shader = PSDrawBicubicRGBA(v_in);
- }
- }
- technique DrawMatrix
- {
- pass
- {
- vertex_shader = VSDefault(v_in);
- pixel_shader = PSDrawBicubicMatrix(v_in);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment