debayer.frag 2.2 KB

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