debayer.frag 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifdef GL_ES
  2. precision highp float;
  3. #endif
  4. uniform sampler2D texture;
  5. uniform mat3 color_matrix;
  6. uniform float inv_gamma;
  7. #ifdef BITS_10
  8. uniform float row_length;
  9. uniform float padding_ratio;
  10. #endif
  11. varying vec2 top_left_uv;
  12. varying vec2 top_right_uv;
  13. varying vec2 bottom_left_uv;
  14. varying vec2 bottom_right_uv;
  15. #ifdef BITS_10
  16. vec2
  17. skip_5th_pixel(vec2 uv)
  18. {
  19. vec2 new_uv = uv;
  20. new_uv.x *= 0.8;
  21. new_uv.x += floor(uv.x * row_length / 5.0) / row_length;
  22. // Crop out padding
  23. new_uv.x *= padding_ratio;
  24. return new_uv;
  25. }
  26. #endif
  27. void
  28. main()
  29. {
  30. // Note the coordinates for texture samples need to be a varying, as the
  31. // Mali-400 has this as a fast path allowing 32-bit floats. Otherwise
  32. // they end up as 16-bit floats and that's not accurate enough.
  33. #ifdef BITS_10
  34. vec4 samples = vec4(texture2D(texture, skip_5th_pixel(top_left_uv)).r,
  35. texture2D(texture, skip_5th_pixel(top_right_uv)).r,
  36. texture2D(texture, skip_5th_pixel(bottom_left_uv)).r,
  37. texture2D(texture, skip_5th_pixel(bottom_right_uv)).r);
  38. #else
  39. vec4 samples = vec4(texture2D(texture, top_left_uv).r,
  40. texture2D(texture, top_right_uv).r,
  41. texture2D(texture, bottom_left_uv).r,
  42. texture2D(texture, bottom_right_uv).r);
  43. #endif
  44. #if defined(CFA_BGGR)
  45. vec3 color = vec3(samples.w, (samples.y + samples.z) / 2.0, samples.x);
  46. #elif defined(CFA_GBRG)
  47. vec3 color = vec3(samples.z, (samples.x + samples.w) / 2.0, samples.y);
  48. #elif defined(CFA_GRBG)
  49. vec3 color = vec3(samples.y, (samples.x + samples.w) / 2.0, samples.z);
  50. #else
  51. vec3 color = vec3(samples.x, (samples.y + samples.z) / 2.0, samples.w);
  52. #endif
  53. // Some crude blacklevel correction to make the preview a bit nicer, this
  54. // should be an uniform
  55. vec3 corrected = color - 0.02;
  56. /*
  57. // Fast SRGB estimate. See https://mimosa-pudica.net/fast-gamma/
  58. vec3 gamma_color =
  59. (vec3(1.138) * inversesqrt(corrected) - vec3(0.138)) * corrected;
  60. */
  61. color = color * color_matrix;
  62. vec3 gamma_color = pow(color, vec3(inv_gamma));
  63. gl_FragColor = vec4(gamma_color, 1);
  64. }