Advertisement
Guest User

lcd_cgwg_2.cg

a guest
May 25th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.06 KB | None | 0 0
  1. #pragma parameter RSUBPIX_R "Colour of R subpixel: R" 1.0 0.0 1.0 0.01
  2. #pragma parameter RSUBPIX_G "Colour of R subpixel: G" 0.0 0.0 1.0 0.01
  3. #pragma parameter RSUBPIX_B "Colour of R subpixel: B" 0.0 0.0 1.0 0.01
  4. #pragma parameter GSUBPIX_R "Colour of G subpixel: R" 0.0 0.0 1.0 0.01
  5. #pragma parameter GSUBPIX_G "Colour of G subpixel: G" 1.0 0.0 1.0 0.01
  6. #pragma parameter GSUBPIX_B "Colour of G subpixel: B" 0.0 0.0 1.0 0.01
  7. #pragma parameter BSUBPIX_R "Colour of B subpixel: R" 0.0 0.0 1.0 0.01
  8. #pragma parameter BSUBPIX_G "Colour of B subpixel: G" 0.0 0.0 1.0 0.01
  9. #pragma parameter BSUBPIX_B "Colour of B subpixel: B" 1.0 0.0 1.0 0.01
  10. #pragma parameter gain      "Gain"                    1.0 0.5 2.0 0.05
  11. #pragma parameter gamma     "LCD Gamma"               3.0 0.5 5.0 0.1
  12. #pragma parameter blacklevel "Black level"            0.05 0.0 0.5 0.01
  13. #pragma parameter ambient   "Ambient"                 0.0 0.0 0.5 0.01
  14. #pragma parameter BGR       "BGR"                     0 0 1 1
  15. #ifdef PARAMETER_UNIFORM
  16. uniform float RSUBPIX_R;
  17. uniform float RSUBPIX_G;
  18. uniform float RSUBPIX_B;
  19. uniform float GSUBPIX_R;
  20. uniform float GSUBPIX_G;
  21. uniform float GSUBPIX_B;
  22. uniform float BSUBPIX_R;
  23. uniform float BSUBPIX_G;
  24. uniform float BSUBPIX_B;
  25. uniform float gain;
  26. uniform float gamma;
  27. uniform float blacklevel;
  28. uniform float ambient;
  29. uniform float BGR;
  30. #else
  31. #define RSUBPIX_R 1.0
  32. #define RSUBPIX_G 0.0
  33. #define RSUBPIX_B 0.0
  34. #define GSUBPIX_R 1.0
  35. #define GSUBPIX_G 1.0
  36. #define GSUBPIX_B 0.0
  37. #define BSUBPIX_R 0.0
  38. #define BSUBPIX_G 0.0
  39. #define BSUBPIX_B 1.0
  40. #define gain 1.0
  41. #define gamma 3.0
  42. #define blacklevel 0.05
  43. #define ambient 0.0
  44. #define BGR 0.0
  45. #endif
  46.  
  47.  
  48. #define outgamma 2.2
  49.  
  50. void main_vertex
  51. (
  52.     float4 position : POSITION,
  53.     float2 texCoord : TEXCOORD0,
  54.  
  55.     uniform float4x4 modelViewProj,
  56.  
  57.     out float4 oPosition : POSITION,
  58.     out float2 otexCoord : TEXCOORD
  59. )
  60. {
  61.     oPosition = mul(modelViewProj, position);
  62.     otexCoord = texCoord;
  63. }
  64.  
  65. struct input
  66. {
  67.   float2 video_size;
  68.   float2 texCoord_size;
  69.   float2 output_size;
  70.   float frame_count;
  71.   float frame_direction;
  72.   float frame_rotation;
  73.   float2 texture_size;
  74.   sampler2D texture : TEXUNIT0;
  75. };
  76.  
  77. struct output
  78. {
  79.   float4 col    : COLOR;
  80. };
  81.  
  82. #define fetch_offset(coord,offset) (pow(float3(gain) * texelFetchOffset(IN.texture, (coord), 0, (offset)).rgb + float3(blacklevel), float3(gamma)) + float3(ambient))
  83.  
  84. // integral of (1 - x^2 - x^4 + x^6)^2
  85. const float coeffs_x[] = float[](1.0, -2.0/3.0, -1.0/5.0, 4.0/7.0, -1.0/9.0, -2.0/11.0, 1.0/13.0);
  86. // integral of (1 - 2x^4 + x^6)^2
  87. const float coeffs_y[] = float[](1.0,      0.0, -4.0/5.0, 2.0/7.0,  4.0/9.0, -4.0/11.0, 1.0/13.0);
  88. float intsmear_func(float z, float coeffs[7])
  89. {
  90.   float z2 = z*z;
  91.   float zn = z;
  92.   float ret = 0.0;
  93.   for (int i = 0; i < 7; i++) {
  94.     ret += zn*coeffs[i];
  95.     zn *= z2;
  96.   }
  97.   return ret;
  98. }
  99. float intsmear(float x, float dx, float d, float coeffs[7])
  100. {
  101.   float zl = clamp((x-dx*0.5)/d,-1.0,1.0);
  102.   float zh = clamp((x+dx*0.5)/d,-1.0,1.0);
  103.   return d * ( intsmear_func(zh,coeffs) - intsmear_func(zl,coeffs) )/dx;
  104. }
  105.  
  106. output main_fragment(in float2 texCoord : TEXCOORD0,
  107. uniform input IN,
  108. uniform sampler2D texture : TEXUNIT0
  109. )
  110. {
  111.   float2 texelSize = 1.0 / IN.texture_size;
  112.   float2 range;
  113.   range = IN.video_size / (IN.output_size * IN.texture_size);
  114.   //float2 range = sourceSize[0].xy / (targetSize.xy * sourceSize[0].xy);
  115.  
  116.   float3 cred   = pow(float3(RSUBPIX_R, RSUBPIX_G, RSUBPIX_B), float3(outgamma));
  117.   float3 cgreen = pow(float3(GSUBPIX_R, GSUBPIX_G, GSUBPIX_B), float3(outgamma));
  118.   float3 cblue  = pow(float3(BSUBPIX_R, BSUBPIX_G, BSUBPIX_B), float3(outgamma));
  119.  
  120.   int2 tli = int2(floor(texCoord/texelSize-float2(0.4999)));
  121.  
  122.   float3 lcol, rcol;
  123.   float subpix = (texCoord.x/texelSize.x - 0.4999 - float(tli.x))*3.0;
  124.   float rsubpix = range.x/texelSize.x * 3.0;
  125.   lcol = float3(intsmear(subpix+1.0,rsubpix, 1.5, coeffs_x),
  126.               intsmear(subpix    ,rsubpix, 1.5, coeffs_x),
  127.               intsmear(subpix-1.0,rsubpix, 1.5, coeffs_x));
  128.   rcol = float3(intsmear(subpix-2.0,rsubpix, 1.5, coeffs_x),
  129.               intsmear(subpix-3.0,rsubpix, 1.5, coeffs_x),
  130.               intsmear(subpix-4.0,rsubpix, 1.5, coeffs_x));
  131.   if (BGR > 0.5) {
  132.     lcol.rgb = lcol.bgr;
  133.     rcol.rgb = rcol.bgr;
  134.   }
  135.   float tcol, bcol;
  136.   subpix = texCoord.y/texelSize.y - 0.4999 - float(tli.y);
  137.   rsubpix = range.y/texelSize.y;
  138.   tcol = intsmear(subpix    ,rsubpix, 0.63, coeffs_y);
  139.   bcol = intsmear(subpix-1.0,rsubpix, 0.63, coeffs_y);
  140.  
  141.   float3 topLeftColor     = fetch_offset(tli, int2(0,0)) * lcol * float3(tcol);
  142.   float3 bottomRightColor = fetch_offset(tli, int2(1,1)) * rcol * float3(bcol);
  143.   float3 bottomLeftColor  = fetch_offset(tli, int2(0,1)) * lcol * float3(bcol);
  144.   float3 topRightColor    = fetch_offset(tli, int2(1,0)) * rcol * float3(tcol);
  145.  
  146.   float3 averageColor = topLeftColor + bottomRightColor + bottomLeftColor + topRightColor;
  147.  
  148.   averageColor = mul(averageColor, mat3x3(cred, cgreen, cblue));
  149.  
  150.   output OUT;
  151.   OUT.col = float4(pow(averageColor,float3(1.0/outgamma)),0.0);
  152.   return OUT;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement