Created
October 6, 2016 18:24
-
-
Save ds-hwang/9075a5f14e60c69f7f7b162d93886289 to your computer and use it in GitHub Desktop.
opengl manual bilinear filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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