debayer.frag 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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
  11. main()
  12. {
  13. // Note the coordinates for texture samples need to be a varying, as the
  14. // Mali-400 has this as a fast path allowing 32-bit floats. Otherwise
  15. // they end up as 16-bit floats and that's not accurate enough.
  16. vec4 samples = vec4(texture2D(texture, top_left_uv).r,
  17. texture2D(texture, top_right_uv).r,
  18. texture2D(texture, bottom_left_uv).r,
  19. texture2D(texture, bottom_right_uv).r);
  20. // Assume BGGR for now. Currently this just takes 3 of the four samples
  21. // for each pixel, there's room here to do some better debayering.
  22. vec3 color = vec3(samples.w, (samples.y + samples.z) / 2.0, samples.x);
  23. // Some crude blacklevel correction to make the preview a bit nicer, this
  24. // should be an uniform
  25. vec3 corrected = color - 0.02;
  26. // Apply the color matrices
  27. // vec3 corrected = color_matrix * color2;
  28. // Fast SRGB estimate. See https://mimosa-pudica.net/fast-gamma/
  29. vec3 srgb_color =
  30. (vec3(1.138) * inversesqrt(corrected) - vec3(0.138)) * corrected;
  31. // Slow SRGB estimate
  32. // vec3 srgb_color = pow(color, vec3(1.0 / 2.2));
  33. gl_FragColor = vec4(srgb_color, 1);
  34. }