---
title: "textureGatherCompare: GPU shader function examples"
description: "Depth comparison tests a reference depth against stored depths through a comparison sampler. Gather returns each comparison separately."
url: https://typeshade.dev/reference/functions/textureGatherCompare/
language: en
---

# `textureGatherCompare`

Function

Depth comparison tests a reference depth against stored depths through a comparison sampler. Gather returns each comparison separately.

## 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`textureGatherCompare(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**

`vec4(1, 1, 1, 0)`

**Example 2`textureGatherCompare(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**

`vec4(1, 1, 0, 0)`

**Example 3`textureGatherCompare(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**

`vec4(0, 1, 0, 0)`

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

The four outputs follow gather order and are not averaged. This compiler emits this operation for WebGPU only.

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/)

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

Compares the reference depth `ref` against the four texels a linear filter would blend at the coordinate on mip level 0, through the comparison sampler `smp`, and returns the four results (0 or 1 each) as a `vec4` in the order (umin,vmax), (umax,vmax), (umax,vmin), (umin,vmin), in any stage.

## Syntax

```ts
declare function textureGatherCompare(
  tex: texture_depth_2d,
  smp: sampler_comparison,
  uv: vec2,
  ref: number,
): vec4
declare function textureGatherCompare(
  tex: texture_depth_2d_array,
  smp: sampler_comparison,
  uv: vec2,
  layer: number,
  ref: number,
): vec4
declare function textureGatherCompare(
  tex: texture_depth_cube,
  smp: sampler_comparison,
  dir: vec3,
  ref: number,
): vec4
declare function textureGatherCompare(
  tex: texture_depth_cube_array,
  smp: sampler_comparison,
  dir: vec3,
  layer: number,
  ref: number,
): vec4
```

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

`vec4`

## Description

On a `texture_depth_2d_array` or a `texture_depth_cube_array` the layer comes before the reference. Compiles to `textureGatherCompare` on WGSL; its GLSL lowering is not supported yet ([Rule 10.5](https://typeshade.dev/reference/rules/10-5/)), so a module using it emits WGSL alone.

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

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

**GLSL ES 3.00**

`textureGatherCompare` is not supported yet on the WebGL2 tier (Rule 10.5)

## Examples

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

## See also

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