Efficient Bicubic filtering code in GLSL?

前端 未结 8 1823
感动是毒
感动是毒 2021-01-30 06:11

I\'m wondering if anyone has complete, working, and efficient code to do bicubic texture filtering in glsl. There is this:

http://www.codeproject.com/Articles/236394/Bi-

8条回答
  •  爱一瞬间的悲伤
    2021-01-30 06:16

    (EDIT)

    • Cubic() is a cubic spline function

      Example:

    cubic spline

    • Texscale is sampling window size coefficient. You can start with 1.0 value.

    vec4 filter(sampler2D texture, vec2 texcoord, vec2 texscale)
    {
        float fx = fract(texcoord.x);
        float fy = fract(texcoord.y);
        texcoord.x -= fx;
        texcoord.y -= fy;
    
        vec4 xcubic = cubic(fx);
        vec4 ycubic = cubic(fy);
    
        vec4 c = vec4(texcoord.x - 0.5, texcoord.x + 1.5, texcoord.y -
    0.5, texcoord.y + 1.5);
        vec4 s = vec4(xcubic.x + xcubic.y, xcubic.z + xcubic.w, ycubic.x +
    ycubic.y, ycubic.z + ycubic.w);
        vec4 offset = c + vec4(xcubic.y, xcubic.w, ycubic.y, ycubic.w) /
    s;
    
        vec4 sample0 = texture2D(texture, vec2(offset.x, offset.z) *
    texscale);
        vec4 sample1 = texture2D(texture, vec2(offset.y, offset.z) *
    texscale);
        vec4 sample2 = texture2D(texture, vec2(offset.x, offset.w) *
    texscale);
        vec4 sample3 = texture2D(texture, vec2(offset.y, offset.w) *
    texscale);
    
        float sx = s.x / (s.x + s.y);
        float sy = s.z / (s.z + s.w);
    
        return mix(
            mix(sample3, sample2, sx),
            mix(sample1, sample0, sx), sy);
    }
    

    Source

提交回复
热议问题