Intrinsics1 min read
sample_lod
Samples a texture at an explicit mip level.
Reading Time
1 min
Word Count
175
Sections
12
Try It Live
Test sample_lod in a live shader
Open the playground, start from a visual preset, and wire sample_lod into the fragment stage to see how it behaves with real values.
Open PlaygroundLive Demo
The sample_lod function reads a texture at a specified mip level (level of detail), bypassing automatic LOD selection.
Signature
bwsl
sample_lod :: (texture2D tex, sampler samp, float2 uv, float lod) -> float4 {...}
sample_lod :: (textureCube tex, sampler samp, float3 dir, float lod) -> float4 {...}
Parameters
| Parameter | Type | Description |
|---|---|---|
tex | texture* | The texture to sample |
samp | sampler | Sampler state |
uv | float2 | Texture coordinates |
lod | float | Mip level (0 = base, higher = smaller mips) |
Return Value
Returns the texel value at the specified mip level.
Example
bwsl
pipeline BlurredReflections {
fragment {
float3 reflectDir = reflect(-viewDir, normal);
// Sample rougher mip levels based on material roughness
float roughnessLod = roughness * 8.0; // Assume 8 mip levels
float4 envColor = sample_lod(resources.envMap, resources.linearSampler, reflectDir, roughnessLod);
output.color = envColor;
}
}Common Use Cases
Roughness-Based Reflections
bwsl
// Blur reflections based on roughness
float lod = roughness * maxMipLevel;
float4 reflection = sample_lod(resources.envMap, resources.linearSampler, reflectDir, lod);Vertex Shader Sampling
bwsl
// Sample in vertex shader (no automatic LOD)
vertex {
float4 heightData = sample_lod(resources.heightMap, resources.linearSampler, input.uv, 0.0);
float3 pos = input.position;
pos.y += heightData.r * heightScale;
}Bloom Downsampling
bwsl
// Read from specific mip level in bloom chain
float4 blur1 = sample_lod(resources.bloomTex, resources.linearSampler, uv, 1.0);
float4 blur2 = sample_lod(resources.bloomTex, resources.linearSampler, uv, 2.0);
float4 combined = blur1 * 0.5 + blur2 * 0.25;LOD Debugging
bwsl
// Visualize mip levels
float4 mip0 = sample_lod(resources.tex, resources.linearSampler, uv, 0.0);
float4 mip1 = sample_lod(resources.tex, resources.linearSampler, uv, 1.0);
float4 mip2 = sample_lod(resources.tex, resources.linearSampler, uv, 2.0);Compute Shader Access
bwsl
pass "Main" {
compute "Main" [8, 8, 1] {
// Compute shaders must specify LOD
float2 uv = float2(input.global_id.xy) / textureSize;
float4 texValue = sample_lod(resources.inputTex, resources.linearSampler, uv, 0.0);
}
}LOD Values
- LOD 0.0 = full resolution (base mip)
- LOD 1.0 = half resolution
- LOD 2.0 = quarter resolution
- Fractional values blend between mip levels
Use Cases
Use sample_lod when:
- Sampling in vertex/compute shaders
- Implementing custom LOD selection
- Blurring via mip levels
- Pre-integrated lighting (roughness-based)
Compiled Output
When compiled to GLSL:
glsl
textureLod(tex, uv, lod)
When compiled to HLSL:
hlsl
tex.SampleLevel(samplerState, uv, lod)
When compiled to Metal:
metal
tex.sample(sampler, uv, level(lod))
See Also
- sample - Automatic LOD sampling
- sample_grad - Gradient-based LOD
- sample_bias - LOD with bias