debayer.frag 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. // Some crude blacklevel correction to make the preview a bit nicer, this should be an uniform
  23. vec3 corrected = color - 0.02;
  24. // Apply the color matrices
  25. //vec3 corrected = color_matrix * color2;
  26. // Fast SRGB estimate. See https://mimosa-pudica.net/fast-gamma/
  27. vec3 srgb_color = (vec3(1.138) * inversesqrt(corrected) - vec3(0.138)) * corrected;
  28. // Slow SRGB estimate
  29. // vec3 srgb_color = pow(color, vec3(1.0 / 2.2));
  30. gl_FragColor = vec4(srgb_color, 1);
  31. }