debayer.frag 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #ifdef GL_ES
  2. precision mediump float;
  3. #endif
  4. uniform sampler2D texture;
  5. uniform mat3 color_matrix;
  6. varying vec2 top_left_uv;
  7. varying vec2 top_right_uv;
  8. varying vec2 bottom_left_uv;
  9. varying vec2 bottom_right_uv;
  10. void main() {
  11. // Note the coordinates for texture samples need to be a varying, as the
  12. // Mali-400 has this as a fast path allowing 32-bit floats. Otherwise
  13. // they end up as 16-bit floats and that's not accurate enough.
  14. vec4 samples = vec4(
  15. texture2D(texture, top_left_uv).r,
  16. texture2D(texture, top_right_uv).r,
  17. texture2D(texture, bottom_left_uv).r,
  18. texture2D(texture, bottom_right_uv).r);
  19. // Assume BGGR for now. Currently this just takes 3 of the four samples
  20. // for each pixel, there's room here to do some better debayering.
  21. vec3 color = vec3(samples.w, (samples.y + samples.z) / 2.0, samples.x);
  22. vec3 corrected = color * color_matrix;
  23. // Fast SRGB estimate. See https://mimosa-pudica.net/fast-gamma/
  24. vec3 srgb_color = (vec3(1.138) * inversesqrt(corrected) - vec3(0.138)) * corrected;
  25. // Slow SRGB estimate
  26. // vec3 srgb_color = pow(color, vec3(1.0 / 2.2));
  27. gl_FragColor = vec4(srgb_color, 1);
  28. }