debayer.frag 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  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. // Fast SRGB estimate. See https://mimosa-pudica.net/fast-gamma/
  23. vec3 srgb_color = (vec3(1.138) * inversesqrt(color) - vec3(0.138)) * color;
  24. // Slow SRGB estimate
  25. // vec3 srgb_color = pow(color, vec3(1.0 / 2.2));
  26. gl_FragColor = vec4(color_matrix * srgb_color, 1);
  27. }