textureSampleCompare
On this page

textureSampleCompare

Function

Depth comparison tests a reference depth against stored depths through a comparison sampler. Filtering combines the comparison results into a visibility value.

Compare examples

Raise the reference depth and observe which texels pass. The example sampler uses less-equal comparison.

Model assumptionsdepth = [[0.75, 1], [0, 0.25]]; compare = less-equal; filter = linear; level = 0; uv = vec2(0.5, 0.5)

Example 1textureSampleCompare(tex, smp, uv, 0.2)
truetruefalsetrue
Texel grid. The upper row has the higher vertical texture coordinate. Values describe one channel; comparison grids show pass or fail.
Result
0.75
Example 2textureSampleCompare(tex, smp, uv, 0.5)
truetruefalsefalse
Texel grid. The upper row has the higher vertical texture coordinate. Values describe one channel; comparison grids show pass or fail.
Result
0.5
Example 3textureSampleCompare(tex, smp, uv, 0.8)
falsetruefalsefalse
Texel grid. The upper row has the higher vertical texture coordinate. Values describe one channel; comparison grids show pass or fail.
Result
0.25

The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.

When to use it

Use it for shadow tests: compare a surface depth with the nearest depth recorded from the light.

Inputs and limits

Compare the reference with each depth before filtering. Comparing with the average depth gives a different answer.

For shader input rules and precision, consult WGSL and GLSL ES 3.00.

WGSL
textureSampleCompare(a, b, c, d)
GLSL ES 3.00
texture(a, vec3(c, d))

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Compares the reference depth ref against the shadow map at uv through the comparison sampler smp, with the implicit level of detail, in the fragment stage only; returns an f32, how much of the filter footprint passed.

Syntax

declare function textureSampleCompare(
tex: texture_depth_2d,
smp: sampler_comparison,
uv: vec2,
ref: number,
): f32
declare function textureSampleCompare(
tex: texture_depth_2d_array,
smp: sampler_comparison,
uv: vec2,
layer: number,
ref: number,
): f32
declare function textureSampleCompare(
tex: texture_depth_cube,
smp: sampler_comparison,
dir: vec3,
ref: number,
): f32
declare function textureSampleCompare(
tex: texture_depth_cube_array,
smp: sampler_comparison,
dir: vec3,
layer: number,
ref: number,
): f32

A number in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.

type f32 = number & { readonly [f32Tag]?: true }

A number you write as a type is inferred from its uses, and f32, i32 and u32 fix a width; the TypeScript constructs page carries that row with the compiler's emit.

Parameters

Return value

f32

Description

On a texture_depth_2d_array the layer comes before the reference, and on a texture_depth_cube the coordinate is a vec3 direction. Compiles to textureSampleCompare on WGSL and texture(sampler2DShadow, vec3(uv, ref)) on GLSL, where the reference folds into the coordinate.

Targets

WGSL
textureSampleCompare(a, b, c, d)
GLSL ES 3.00
texture(a, vec3(c, d))

Examples

See also

Edit this page Report a problem