sharp-bilinear.fs 855 B

123456789101112131415161718192021222324252627282930313233
  1. #version 150
  2. // Sharp Bilinear
  3. // Author: Themaister
  4. // License: Public Domain
  5. uniform sampler2D source[];
  6. uniform vec4 sourceSize[];
  7. uniform vec4 targetSize;
  8. in Vertex {
  9. vec2 texCoord;
  10. };
  11. out vec4 fragColor;
  12. void main() {
  13. float prescale = floor(targetSize.y / sourceSize[0].y);
  14. vec2 texel = texCoord.xy * sourceSize[0].xy;
  15. vec2 texel_floored = floor(texel);
  16. vec2 s = fract(texel);
  17. float region_range = 0.5 - 0.5 / prescale;
  18. // Figure out where in the texel to sample to get correct pre-scaled bilinear.
  19. // Uses the hardware bilinear interpolator to avoid having to sample 4 times manually.
  20. vec2 center_dist = s - 0.5;
  21. vec2 f = (center_dist - clamp(center_dist, -region_range, region_range)) * prescale + 0.5;
  22. vec2 mod_texel = texel_floored + f;
  23. fragColor = texture(source[0], mod_texel / sourceSize[0].xy);;
  24. }