lcd-grid.fs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #version 150
  2. #in red
  3. #in green
  4. #in blue
  5. #in gain
  6. #in gamma
  7. #in blacklevel
  8. #in ambient
  9. #in BGR
  10. #define outgamma 2.2
  11. uniform sampler2D source[];
  12. uniform vec4 sourceSize[];
  13. uniform vec4 targetSize;
  14. in Vertex {
  15. vec2 texCoord;
  16. };
  17. out vec4 fragColor;
  18. #define fetch_offset(coord,offset) (pow(vec3(gain) * texelFetchOffset(source[0], (coord), 0, (offset)).rgb + vec3(blacklevel), vec3(gamma)) + vec3(ambient))
  19. // integral of (1 - x^2 - x^4 + x^6)^2
  20. 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);
  21. // integral of (1 - 2x^4 + x^6)^2
  22. 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);
  23. float intsmear_func(float z, float coeffs[7])
  24. {
  25. float z2 = z*z;
  26. float zn = z;
  27. float ret = 0.0;
  28. for (int i = 0; i < 7; i++) {
  29. ret += zn*coeffs[i];
  30. zn *= z2;
  31. }
  32. return ret;
  33. }
  34. float intsmear(float x, float dx, float d, float coeffs[7])
  35. {
  36. float zl = clamp((x-dx*0.5)/d,-1.0,1.0);
  37. float zh = clamp((x+dx*0.5)/d,-1.0,1.0);
  38. return d * ( intsmear_func(zh,coeffs) - intsmear_func(zl,coeffs) )/dx;
  39. }
  40. void main()
  41. {
  42. vec2 texelSize = 1.0 / sourceSize[0].xy;
  43. vec2 range = sourceSize[0].xy / (targetSize.xy * sourceSize[0].xy);
  44. vec3 cred = pow(red, vec3(outgamma));
  45. vec3 cgreen = pow(green, vec3(outgamma));
  46. vec3 cblue = pow(blue, vec3(outgamma));
  47. ivec2 tli = ivec2(floor(texCoord/texelSize-vec2(0.4999)));
  48. vec3 lcol, rcol;
  49. float subpix = (texCoord.x/texelSize.x - 0.4999 - float(tli.x))*3.0;
  50. float rsubpix = range.x/texelSize.x * 3.0;
  51. lcol = vec3(intsmear(subpix+1.0,rsubpix, 1.5, coeffs_x),
  52. intsmear(subpix ,rsubpix, 1.5, coeffs_x),
  53. intsmear(subpix-1.0,rsubpix, 1.5, coeffs_x));
  54. rcol = vec3(intsmear(subpix-2.0,rsubpix, 1.5, coeffs_x),
  55. intsmear(subpix-3.0,rsubpix, 1.5, coeffs_x),
  56. intsmear(subpix-4.0,rsubpix, 1.5, coeffs_x));
  57. #ifdef BGR
  58. lcol.rgb = lcol.bgr;
  59. rcol.rgb = rcol.bgr;
  60. #endif
  61. float tcol, bcol;
  62. subpix = texCoord.y/texelSize.y - 0.4999 - float(tli.y);
  63. rsubpix = range.y/texelSize.y;
  64. tcol = intsmear(subpix ,rsubpix, 0.63, coeffs_y);
  65. bcol = intsmear(subpix-1.0,rsubpix, 0.63, coeffs_y);
  66. vec3 topLeftColor = fetch_offset(tli, ivec2(0,0)) * lcol * vec3(tcol);
  67. vec3 bottomRightColor = fetch_offset(tli, ivec2(1,1)) * rcol * vec3(bcol);
  68. vec3 bottomLeftColor = fetch_offset(tli, ivec2(0,1)) * lcol * vec3(bcol);
  69. vec3 topRightColor = fetch_offset(tli, ivec2(1,0)) * rcol * vec3(tcol);
  70. vec3 averageColor = topLeftColor + bottomRightColor + bottomLeftColor + topRightColor;
  71. averageColor = mat3x3(cred, cgreen, cblue) * averageColor;
  72. fragColor = vec4(pow(averageColor,vec3(1.0/outgamma)),0.0);
  73. }