Guest User

5 tap ASTC texture deblocking filter

a guest
Feb 26th, 2026
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #vertex
  2. #version 330 core
  3.  
  4. layout(location = 0) in vec3 aPos;
  5. layout(location = 1) in vec2 aUV;
  6.  
  7. uniform mat4 mvp;
  8.  
  9. out vec2 vUV;
  10.  
  11. void main() {
  12.     vUV = aUV;
  13.     gl_Position = mvp * vec4(aPos, 1.0);
  14. }
  15.  
  16. #fragment
  17. #version 330 core
  18.  
  19. uniform sampler2D tex;
  20. uniform vec4 texSize;  // Base mip dimensions (mip 0) in xy, zw=ASTC block size of texture
  21. uniform float maxLod;  // = number_of_mip_levels - 1
  22.  
  23. uniform vec4 const0;   // User constant 0 (keys 1-4 toggle x,y,z,w)
  24. uniform vec4 const1;   // User constant 1 (keys 5-8 toggle x,y,z,w)
  25.  
  26. in vec2 vUV;
  27. out vec4 fragColor;
  28.  
  29. void main()
  30. {
  31.     // Note: Could use textureQueryLod() but it's not supported in WebGL. textureSize() is in WebGL 2.0 however.
  32.  
  33.     vec2 blockSize = vec2(texSize.z, texSize.w);
  34.    
  35.     vec2 du = dFdx(vUV);
  36.     vec2 dv = dFdy(vUV);
  37.     float rho = max(length(du * texSize.xy), length(dv * texSize.xy));
  38.    
  39.     //float lod = max(0.0, log2(max(rho, 1e-8))); // lod index
  40.     float lod = clamp(log2(max(rho, 1e-8)), 0.0, maxLod);
  41.    
  42.     float mipScale = exp2(floor(lod + .5)); // 2^lod mipmap scale, snaps to dominant mipmap
  43.    
  44.     vec2 texDim = vec2(texSize.x, texSize.y);
  45.     vec2 texelStep = 1.0 / texDim;
  46.     vec2 texelPos = (vUV * texDim) / mipScale;
  47.     vec2 blockPos = mod(texelPos, blockSize);
  48.    
  49.     vec3 color;
  50.     color = texture(tex, vUV).rgb;
  51.    
  52.     if (const0.x > 0.5)
  53.     {
  54.         float falloff = 1.0;
  55.        
  56.         float leftProx = 1.0 - clamp(blockPos.x / falloff, 0.0, 1.0);
  57.         float rightProx = 1.0 - clamp((blockSize.x - 1.0 - blockPos.x) / falloff, 0.0, 1.0);
  58.         float topProx = 1.0 - clamp(blockPos.y / falloff, 0.0, 1.0);
  59.         float bottomProx = 1.0 - clamp((blockSize.y - 1.0 - blockPos.y) / falloff, 0.0, 1.0);
  60.  
  61.         float horizWeight = max(leftProx, rightProx);
  62.         float vertWeight = max(topProx, bottomProx);
  63.         float edgeWeight = max(horizWeight, vertWeight); // overall proximity
  64.                
  65.         vec3 c0 = color; //texture(tex, vUV).rgb;
  66.  
  67.         //vec3 l2 = texture(tex, vUV - vec2(2 * texelStep.x * mipScale, 0.0)).rgb;      
  68.         vec3 l1 = texture(tex, vUV - vec2(texelStep.x * mipScale, 0.0)).rgb;
  69.         vec3 r1 = texture(tex, vUV + vec2(texelStep.x * mipScale, 0.0)).rgb;
  70.         //vec3 r2 = texture(tex, vUV + vec2(2 * texelStep.x * mipScale, 0.0)).rgb;
  71.        
  72.         //vec3 u2 = texture(tex, vUV - vec2(0.0, 2  * texelStep.y * mipScale)).rgb;
  73.         vec3 u1 = texture(tex, vUV - vec2(0.0, texelStep.y * mipScale)).rgb;
  74.         vec3 d1 = texture(tex, vUV + vec2(0.0, texelStep.y * mipScale)).rgb;
  75.         //vec3 d2 = texture(tex, vUV + vec2(0.0, 2 * texelStep.y * mipScale)).rgb;
  76.        
  77.         //vec3 filteredH = (l2 + 2 * l1 + 3 * c0 + 2 * r1 + r2) / 9.0;
  78.         //vec3 filteredV = (u2 + 2 * u1 + 3 * c0 + 2 * d1 + d2) / 9.0;
  79.        
  80.         //vec3 filteredH = (l2 + 2 * l1 + 2 * c0 + 2 * r1 + r2) / 8.0;
  81.         //vec3 filteredV = (u2 + 2 * u1 + 2 * c0 + 2 * d1 + d2) / 8.0;
  82.        
  83.         //vec3 filteredH = (l1 + 2.0*c0 + r1) * 0.25;
  84.         //vec3 filteredV = (u1 + 2.0*c0 + d1) * 0.25;
  85.        
  86.         vec3 filteredH = (l1 + c0 + r1) * (1.0/3.0);
  87.         vec3 filteredV = (u1 + c0 + d1) * (1.0/3.0);
  88.        
  89.         float smoothH = 1.0;
  90.         float smoothV = 1.0;
  91.        
  92.         if (edgeWeight > 0.0)
  93.         {  
  94.             float strengthH = smoothH * horizWeight;
  95.             float strengthV = smoothV * vertWeight;
  96.  
  97.             vec3 horizColor = mix(c0, filteredH, strengthH);
  98.             vec3 vertColor  = mix(c0, filteredV, strengthV);
  99.  
  100.             float totalW = strengthH + strengthV;
  101.             if (totalW > 0.0)
  102.                 color = (horizColor * strengthH + vertColor * strengthV) / totalW;
  103.         }
  104.  
  105.         // block edge vis
  106.         if (const0.y > 0.5)        
  107.         {
  108.             color = vec3(edgeWeight, edgeWeight, edgeWeight);
  109.         }
  110.     }
  111.            
  112.     fragColor = vec4(color, 1.0);
  113. }
  114.  
Tags: Shader
Advertisement
Add Comment
Please, Sign In to add comment