Sunday, December 6, 2026

ASTC Pixel Shader Deblocking with Mipmapping

Deblocking is standard in image and video compression, and it turns out to be simple to add to ASTC sampling too. The method is compatible with any ASTC encoder, but an encoder that optimizes for a simple pixel shader reconstruction filter can create far better-looking output. The video world figured this out decades ago: post-filtering (or out-of-loop filtering) vs. in-loop filtering.

On ASTC hardware (available on many billions of shipped devices, with 800 million more shipped per year that don't support BC1-7), adding mipmap-aware shader deblocking permits the usage of larger block sizes than you normally could use. There are 14 block sizes supported by the ASTC hardware's generative decoder, and once you add deblocking you'll be unlocking all of them - even up to 12x12 (0.89 bpp).

The primary goal with the deblocking shader was to preserve standard GPU hardware filtering: bilinear filtering, and mipmapping with trilinear or anisotropic filtering. So the texture fetches it uses are entirely standard - nothing special. Each is just filtered by the GPU hardware, as usual. (Anything else would be too slow/complex.)

I tried around 10 ASTC deblocking pixel shader variants before settling on this one. Being mipmap compatible and temporally stable was a crucial requirement.

There's the usual center sample. The shader then samples around the center sample 4 times (left/right/upper/lower, with the sampling distance taking into account the dominant mipmap level), and it then blends the output. The output blend depends on where the center sample falls relative to the block edges: samples inside each texture block have no filtering at all. The transition between edge filtering and no filtering is smooth. At block edges the low-pass filter is at maximum strength.

The mipmap part computes the dominant mipmap level that will influence the output most, which is easy using shader derivative instructions. The ASTC WxH block size in texels must be provided to the shader in constants, so it knows where it's sampling within the block footprint of the dominant mipmap level.

I really wanted to make the block edge filtering adaptive, to avoid filtering away real edges at block boundaries, but I couldn't find a way to make that temporally stable as an object rotates/scales in 3D. It was also too complex.

This filter could easily be added by the IHVs directly to the GPU texture sampling hardware.

Shader:

https://github.com/BinomialLLC/basis_universal/blob/master/shader_deblocking_glfw/bin/shader.glsl

GLSL sample:

https://github.com/BinomialLLC/basis_universal/tree/master/shader_deblocking_glfw

D3D11 sample:

https://github.com/BinomialLLC/basis_universal/tree/master/shader_deblocking_d3d11

Mathematical analysis of the Basis Universal ASTC deblocking shader:

https://chatgpt.com/share/6b158d07-0368-83e8-b79a-53fda74bed5c


Sample GLSL Deblocking Shader


#vertex

#version 330 core


layout(location = 0) in vec3 aPos;

layout(location = 1) in vec2 aUV;

uniform mat4 mvp;


out vec2 vUV;


void main() {

    vUV = aUV;

    gl_Position = mvp * vec4(aPos, 1.0);

}


#fragment

#version 330 core


uniform sampler2D tex;

uniform vec4 texSize;  // Base mip dimensions (mip 0) in xy, zw=ASTC block size of texture

uniform float maxLod;  // = number_of_mip_levels - 1

uniform vec4 const0;   // User constant 0 (keys 1-4 toggle x,y,z,w)

uniform vec4 const1;   // User constant 1 (keys 5-8 toggle x,y,z,w)


in vec2 vUV;

out vec4 fragColor;


void main() 

{

    // Note: Could use textureQueryLod() but it's not supported in WebGL. textureSize() is in WebGL 2.0 however.

    const vec3 LUMA = vec3(0.299, 0.587, 0.114);


    vec2 texDim = vec2(texSize.x, texSize.y);

    vec2 blockSize = vec2(texSize.z, texSize.w);

    

    vec2 du = dFdx(vUV);

    vec2 dv = dFdy(vUV);

    float rho = max(length(du * texSize.xy), length(dv * texSize.xy));

    float lod = clamp(log2(max(rho, 1e-8)), 0.0, maxLod); // lod index

    float mipScale = exp2(floor(lod + .5)); // 2^lod mipmap scale, snaps to dominant mipmap, 1=mip0, 2=mip1, 4=mip2, etc.

        

    vec2 texelStep = mipScale / texDim; // how to step one texel in effective mip space

    vec2 texelPos = (vUV * texDim) / mipScale; // the physical texel coord in effective mip space

    vec2 blockPos = mod(texelPos, blockSize); // the block offset in texels

            

    vec3 color;

    color = texture(tex, vUV).rgb;


    // Keep these fetches outside non-uniform control flow: texture() uses implicit

    // derivatives for LOD, which are undefined when neighboring fragments take

    // different branches. Use explicit LOD so edgeWeight can safely go to zero.

    vec3 l1 = texture(tex, vUV - vec2(texelStep.x, 0.0)).rgb;

    vec3 r1 = texture(tex, vUV + vec2(texelStep.x, 0.0)).rgb;

                    

    vec3 u1 = texture(tex, vUV - vec2(0.0, texelStep.y)).rgb;

    vec3 d1 = texture(tex, vUV + vec2(0.0, texelStep.y)).rgb;


    if (const0.x > 0.5) 

    {

        const float falloff = 1.5;

        

        float leftProx   = 1.0 - clamp(blockPos.x / falloff, 0.0, 1.0);

        float rightProx  = 1.0 - clamp((blockSize.x - blockPos.x) / falloff, 0.0, 1.0);

        float topProx    = 1.0 - clamp(blockPos.y / falloff, 0.0, 1.0);

        float bottomProx = 1.0 - clamp((blockSize.y - blockPos.y) / falloff, 0.0, 1.0);


        float horizWeight = max(leftProx, rightProx);

        float vertWeight = max(topProx, bottomProx);

        float edgeWeight = max(horizWeight, vertWeight); // overall proximity

        

        if (edgeWeight > 0.0)

        {                       

            vec3 c0 = color;

                                    

            vec3 filteredH = (l1 + c0 + r1) * (1.0/3.0);

            vec3 filteredV = (u1 + c0 + d1) * (1.0/3.0);

        

            float strengthH = horizWeight;

            float strengthV = vertWeight;


            vec3 horizColor = mix(c0, filteredH, strengthH);

            vec3 vertColor  = mix(c0, filteredV, strengthV);


            float totalW = strengthH + strengthV;

            if (totalW > 0.0)

                color = (horizColor * strengthH + vertColor * strengthV) / totalW;

        }


        // block edge vis

        if (const0.y > 0.5)         

        {

            color = vec3(edgeWeight, edgeWeight, edgeWeight);

        }

    }

            

    fragColor = vec4(color, 1.0);

}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.