Skip to content

Commit 73e7e2f

Browse files
AtlasSampling: avoid redundant square root
1 parent 7d2d86e commit 73e7e2f

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

‎Shaders/Common/public/AtlasSampling.fxh‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,13 @@ float4 SampleTextureAtlas(Texture2DArray Atlas,
8585
float fElements;
8686
Atlas.GetDimensions(f2AtlasDim.x, f2AtlasDim.y, fElements);
8787

88-
// Compute gradient lengths in pixels
89-
float fGradX = max(length(f2dUV_dx * f2AtlasDim.xy), 1e-5);
90-
float fGradY = max(length(f2dUV_dy * f2AtlasDim.xy), 1e-5);
91-
float fMaxGrad = max(fGradX, fGradY);
88+
// Compute the maximum gradient length in pixels
89+
float2 f2GradX = f2dUV_dx * f2AtlasDim;
90+
float2 f2GradY = f2dUV_dy * f2AtlasDim;
91+
float fGradXSqr = dot(f2GradX, f2GradX);
92+
float fGradYSqr = dot(f2GradY, f2GradY);
93+
float fMaxGrad = max(sqrt(max(fGradXSqr, fGradYSqr)), 1e-5);
94+
float fMinGrad = max(sqrt(min(fGradXSqr, fGradYSqr)), 1e-5); // Only used on GLES and WebGPU
9295

9396
float LOD;
9497
float UnclampedLOD;
@@ -103,8 +106,7 @@ float4 SampleTextureAtlas(Texture2DArray Atlas,
103106
{
104107
// textureQueryLod is not supported even in GLES3.2.
105108
// Follow Section 8.14 (Texture Minification) from OpenGL4.6 spec.
106-
float fMinGrad = min(fGradX, fGradY);
107-
float Aniso = min(fMaxGrad / fMinGrad, Attribs.fMaxAnisotropy);
109+
float Aniso = min(fMaxGrad / fMinGrad, Attribs.fMaxAnisotropy);
108110
UnclampedLOD = log2(fMaxGrad / Aniso);
109111
LOD = UnclampedLOD;
110112
}

0 commit comments

Comments
 (0)