Intrinsics1 min read

texture_levels

Queries the number of mip levels available for a texture.

Reading Time
1 min
Word Count
126
Sections
7
Try It Live

Test texture_levels in a live shader

Open the playground, start from a visual preset, and wire texture_levels into the fragment stage to see how it behaves with real values.

Open Playground

The texture_levels function returns the number of mip levels available for a texture resource. Use it when shader code needs to clamp explicit LOD selection or adapt a filter to the texture's mip chain.

Live Demo

Signature

bwsl
texture_levels :: (texture2D tex) -> int
texture_levels :: (texture3D tex) -> int
texture_levels :: (textureCube tex) -> int

Parameters

ParameterTypeDescription
textexture*Texture resource to query

Return Value

Returns the number of mip levels available for the texture.

Example

bwsl
int levels = texture_levels(resources.albedoTexture);
float lod = clamp(requestedLod, 0.0, float(levels - 1));
float4 color = sample_lod(resources.albedoTexture, uv, lod);

Clamp Explicit LOD

Use texture_levels with sample_lod when you compute LOD manually and need to keep it inside the texture's available mip range.

Compiled Output

When compiled to GLSL:

glsl
textureQueryLevels(tex)

When compiled to HLSL:

hlsl
tex.GetDimensions(..., levels)

When compiled to Metal:

metal
tex.get_num_mip_levels()

See Also