debayer.frag 2.3 KB

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