Skip to content

Instantly share code, notes, and snippets.

@ds-hwang
Created October 6, 2016 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ds-hwang/9075a5f14e60c69f7f7b162d93886289 to your computer and use it in GitHub Desktop.
Save ds-hwang/9075a5f14e60c69f7f7b162d93886289 to your computer and use it in GitHub Desktop.
opengl manual bilinear filter
// refer to Mesa sample_2d_linear() in s_texfilter.c
// https://cs.chromium.org/chromium/src/third_party/mesa/src/src/mesa/swrast/s_texfilter.c?q=sample_2d_linear&sq=package:chromium
vec4 texture2D_bilinear(sampler2D sampler, vec2 tex_coord, vec2 tex_size) {
vec2 unit_texel = 1.0 / tex_size;
vec2 unnorm_tex_coord = (tex_coord * tex_size) - vec2(0.5);
vec2 f = fract(unnorm_tex_coord);
vec2 snap_tex_coord = (floor(unnorm_tex_coord) + vec2(0.5)) / tex_size;
vec4 s1 = texture2D(sampler, snap_tex_coord);
vec4 s2 = texture2D(sampler, snap_tex_coord + vec2(unit_texel.x, 0.));
vec4 s3 = texture2D(sampler, snap_tex_coord + vec2(0., unit_texel.y));
vec4 s4 = texture2D(sampler, snap_tex_coord + unit_texel);
return mix(mix(s1, s2, f.x), mix(s3, s4, f.x), f.y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment