---
title: "textureSampleCompare: WGSL and GLSL function guide"
description: "Depth comparison tests a reference depth against stored depths through a comparison sampler. Filtering combines the comparison results into a visibility value."
url: https://typeshade.dev/reference/functions/textureSampleCompare/
language: en
---

# `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 assumptions**`depth = [[0.75, 1], [0, 0.25]]; compare = less-equal; filter = linear; level = 0; uv = vec2(0.5, 0.5)`

**Example 1`textureSampleCompare(tex, smp, uv, 0.2)`**

`true` `true` `false` `true`

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 2`textureSampleCompare(tex, smp, uv, 0.5)`**

`true` `true` `false` `false`

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 3`textureSampleCompare(tex, smp, uv, 0.8)`**

`false` `true` `false` `false`

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](https://www.w3.org/TR/WGSL/) and [GLSL ES 3.00](https://registry.khronos.org/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf).

## Related functions

- [`textureSample`](https://typeshade.dev/reference/functions/textureSample/)
- [`textureLoad`](https://typeshade.dev/reference/functions/textureLoad/)
- [`textureSampleLevel`](https://typeshade.dev/reference/functions/textureSampleLevel/)
- [`textureSampleBias`](https://typeshade.dev/reference/functions/textureSampleBias/)
- [`textureSampleGrad`](https://typeshade.dev/reference/functions/textureSampleGrad/)

**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

```ts
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](https://typeshade.dev/guide/language/from-typescript/) carries that row with the compiler's emit.

## Parameters

- `tex` `texture_depth_2d``texture_depth_2d_array``texture_depth_cube``texture_depth_cube_array`
- `smp` `sampler_comparison`
- `uv` optional `vec2`
- `dir` optional `vec3`
- `ref` `number`
- `layer` optional `number`

## 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.

The CPU oracle has no texture memory and no neighbouring fragments. The call throws unless the module was compiled with `{ gpuStubs: true }`, which returns a placeholder.

## Targets

**WGSL**

`textureSampleCompare(a, b, c, d)`

**GLSL ES 3.00**

`texture(a, vec3(c, d))`

## Examples

- [`gallery/cube-array-gather/cube-array-gather.shade.ts`](https://typeshade.dev/guide/examples/cube-array-gather/)
- [`gallery/cube-env/cube-env.shade.ts`](https://typeshade.dev/guide/examples/cube-env/)
- [`gallery/shadow-compare/shadow-compare.shade.ts`](https://typeshade.dev/guide/examples/shadow-compare/)

## See also

- [Functions](https://typeshade.dev/reference/functions/)
- [Builtin table](https://typeshade.dev/guide/language/builtins/#textures)
