Guest User

Untitled

a guest
Mar 6th, 2016
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.82 KB | None | 0 0
  1. // from https://github.com/libretro/common-shaders/blob/master/windowed/shaders/jinc2.cg
  2.  
  3. uniform float4x4 ViewProj;
  4. uniform texture2d image;
  5. uniform float4x4 color_matrix;
  6. uniform float3 color_range_min = {0.0, 0.0, 0.0};
  7. uniform float3 color_range_max = {1.0, 1.0, 1.0};
  8. uniform float2 base_dimension_i;
  9.  
  10. sampler_state textureSampler {
  11.     Filter    = MIN_MAG_MIP_POINT;
  12.     AddressU  = Clamp;
  13.     AddressV  = Clamp;
  14. };
  15.  
  16. struct VertData {
  17.     float4 pos : POSITION;
  18.     float2 uv  : TEXCOORD0;
  19. };
  20.  
  21. VertData VSDefault(VertData v_in)
  22. {
  23.     VertData vert_out;
  24.     vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
  25.     vert_out.uv  = v_in.uv;
  26.     return vert_out;
  27. }
  28.  
  29. #define JINC2_WINDOW_SINC 0.44
  30. #define JINC2_SINC 0.82
  31. #define JINC2_AR_STRENGTH 0.5
  32.  
  33. #define halfpi  1.5707963267948966192313216916398
  34. #define pi    3.1415926535897932384626433832795
  35. #define wa    JINC2_WINDOW_SINC*pi
  36. #define wb    JINC2_SINC*pi
  37.  
  38. // Calculates the distance between two points
  39. float d(float2 pt1, float2 pt2)
  40. {
  41.   float2 v = pt2 - pt1;
  42.   return sqrt(dot(v,v));
  43. }
  44.  
  45. float3 min4(float3 a, float3 b, float3 c, float3 d)
  46. {
  47.     return min(a, min(b, min(c, d)));
  48. }
  49. float3 max4(float3 a, float3 b, float3 c, float3 d)
  50. {
  51.     return max(a, max(b, max(c, d)));
  52. }
  53.  
  54. float4 resampler(float4 x)
  55.     {
  56.       float4 res;
  57.  
  58.       res = (x==float4(0.0, 0.0, 0.0, 0.0)) ?  (float4)(wa*wb)  :  sin(x*wa)*sin(x*wb)/(x*x);
  59.  
  60.       return res;
  61.     }
  62.  
  63. float4 DrawBicubic(VertData v_in)
  64. {
  65.     float2 size = 1.0/base_dimension_i;
  66.  
  67.     float3 color;
  68.   float4x4 weights;
  69.  
  70.   float2 dx = float2(1.0, 0.0);
  71.   float2 dy = float2(0.0, 1.0);
  72.  
  73.   float2 pc = v_in.uv*size;
  74.  
  75.   float2 tc = (floor(pc-float2(0.5,0.5))+float2(0.5,0.5));
  76.  
  77.   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)));
  78.   weights[1] = resampler(float4(d(pc, tc    -dx       ), d(pc, tc              ), d(pc, tc    +dx       ), d(pc, tc+2.0*dx       )));
  79.   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)));
  80.   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)));
  81.  
  82.   dx = dx/size;
  83.   dy = dy/size;
  84.   tc = tc/size;
  85.  
  86.  // reading the texels
  87.  
  88.   float3 c00 = pow(image.SampleLevel(textureSampler, tc    -dx    -dy, 0), 2.2).xyz;
  89.   float3 c10 = pow(image.SampleLevel(textureSampler, tc           -dy, 0), 2.2).xyz;
  90.   float3 c20 = pow(image.SampleLevel(textureSampler, tc    +dx    -dy, 0), 2.2).xyz;
  91.   float3 c30 = pow(image.SampleLevel(textureSampler, tc+2.0*dx    -dy, 0), 2.2).xyz;
  92.   float3 c01 = pow(image.SampleLevel(textureSampler, tc    -dx       , 0), 2.2).xyz;
  93.   float3 c11 = pow(image.SampleLevel(textureSampler, tc              , 0), 2.2).xyz;
  94.   float3 c21 = pow(image.SampleLevel(textureSampler, tc    +dx       , 0), 2.2).xyz;
  95.   float3 c31 = pow(image.SampleLevel(textureSampler, tc+2.0*dx       , 0), 2.2).xyz;
  96.   float3 c02 = pow(image.SampleLevel(textureSampler, tc    -dx    +dy, 0), 2.2).xyz;
  97.   float3 c12 = pow(image.SampleLevel(textureSampler, tc           +dy, 0), 2.2).xyz;
  98.   float3 c22 = pow(image.SampleLevel(textureSampler, tc    +dx    +dy, 0), 2.2).xyz;
  99.   float3 c32 = pow(image.SampleLevel(textureSampler, tc+2.0*dx    +dy, 0), 2.2).xyz;
  100.   float3 c03 = pow(image.SampleLevel(textureSampler, tc    -dx+2.0*dy, 0), 2.2).xyz;
  101.   float3 c13 = pow(image.SampleLevel(textureSampler, tc       +2.0*dy, 0), 2.2).xyz;
  102.   float3 c23 = pow(image.SampleLevel(textureSampler, tc    +dx+2.0*dy, 0), 2.2).xyz;
  103.   float3 c33 = pow(image.SampleLevel(textureSampler, tc+2.0*dx+2.0*dy, 0), 2.2).xyz;
  104.  
  105.   //  Get min/max samples
  106.   float3 min_sample = min4(c11, c21, c12, c22);
  107.   float3 max_sample = max4(c11, c21, c12, c22);
  108.  
  109.   color = mul(weights[0], float4x3(c00, c10, c20, c30));
  110.   color+= mul(weights[1], float4x3(c01, c11, c21, c31));
  111.   color+= mul(weights[2], float4x3(c02, c12, c22, c32));
  112.   color+= mul(weights[3], float4x3(c03, c13, c23, c33));
  113.   color = color/(dot(mul(weights, (float4)(1)), 1));
  114.  
  115.   // Anti-ringing
  116.   float3 aux = color;
  117.   color = clamp(color, min_sample, max_sample);
  118.  
  119.   color = lerp(aux, color, JINC2_AR_STRENGTH);
  120.  
  121.   // final sum and weight normalization
  122.   return pow(float4(color, 1), 1/2.2);
  123. }
  124.  
  125. float4 PSDrawBicubicRGBA(VertData v_in) : TARGET
  126. {
  127.     return DrawBicubic(v_in);
  128. }
  129.  
  130. float4 PSDrawBicubicMatrix(VertData v_in) : TARGET
  131. {
  132.     float4 rgba = DrawBicubic(v_in);
  133.     float4 yuv;
  134.  
  135.     yuv.xyz = clamp(rgba.xyz, color_range_min, color_range_max);
  136.     return saturate(mul(float4(yuv.xyz, 1.0), color_matrix));
  137. }
  138.  
  139. technique Draw
  140. {
  141.     pass
  142.     {
  143.         vertex_shader = VSDefault(v_in);
  144.         pixel_shader  = PSDrawBicubicRGBA(v_in);
  145.     }
  146. }
  147.  
  148. technique DrawMatrix
  149. {
  150.     pass
  151.     {
  152.         vertex_shader = VSDefault(v_in);
  153.         pixel_shader  = PSDrawBicubicMatrix(v_in);
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment