---
title: "textureSampleGrad / textureGrad: shader function guide"
description: "Gradient sampling chooses texture detail from coordinate changes supplied by the caller."
url: https://typeshade.dev/reference/functions/textureSampleGrad/
language: en
---

# `textureSampleGrad`

Function

Gradient sampling chooses texture detail from coordinate changes supplied by the caller.

**Related names in other languages**

These related operations can differ in arguments, support, and semantics. The foreign names are not accepted TypeShade aliases. Check the target forms and restrictions.

- GLSL: `textureGrad`

[Targets](https://typeshade.dev/reference/functions/textureSampleGrad/#targets)

## Compare examples

Compare small and large gradients at the same coordinate. A larger footprint selects coarser detail.

**Model assumptions**`uv = vec2(0.25, 0.25); filter = linear; mip = nearest; levels = [2 × 2, 1 × 1]`

**Example 1`ddx = vec2(0.5, 0); ddy = vec2(0, 0.5)`**

`0.75` `1` `0` `0.25`

Texel grid. The upper row has the higher vertical texture coordinate. Values describe one channel; comparison grids show pass or fail.

**Result**

`0`

**Example 2`ddx = vec2(1, 0); ddy = vec2(0, 1)`**

`0.5`

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`ddx = vec2(0.25, 0); ddy = vec2(0, 0.25)`**

`0.75` `1` `0` `0.25`

Texel grid. The upper row has the higher vertical texture coordinate. Values describe one channel; comparison grids show pass or fail.

**Result**

`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 when coordinates wrap or branch but filtering still needs the original smooth coordinate gradients.

## Inputs and limits

The model uses isotropic gradients on a square texture with nearest mip selection. Real filtering also depends on the sampler and footprint anisotropy.

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/)
- [`textureGather`](https://typeshade.dev/reference/functions/textureGather/)

**WGSL**

`textureSampleGrad(a, b, c, d, e)`

**GLSL ES 3.00**

`textureGrad(a, c, d, e)`

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

Samples a float texture through the sampler `smp` with the explicit gradients `ddx` and `ddy`, which have the width of the coordinate, in any stage.

## Syntax

```ts
declare function textureSampleGrad(
  tex: texture_2d<f32>,
  smp: sampler,
  uv: vec2,
  ddx: vec2,
  ddy: vec2,
): vec4
declare function textureSampleGrad(
  tex: texture_2d_array<f32>,
  smp: sampler,
  uv: vec2,
  layer: number,
  ddx: vec2,
  ddy: vec2,
): vec4
declare function textureSampleGrad(
  tex: texture_cube<f32>,
  smp: sampler,
  dir: vec3,
  ddx: vec3,
  ddy: vec3,
): vec4
declare function textureSampleGrad(
  tex: texture_3d<f32>,
  smp: sampler,
  coord: vec3,
  ddx: vec3,
  ddy: vec3,
): vec4
declare function textureSampleGrad(
  tex: texture_cube_array<f32>,
  smp: sampler,
  dir: vec3,
  layer: number,
  ddx: vec3,
  ddy: vec3,
): 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_2d<f32>``texture_2d_array<f32>``texture_cube<f32>``texture_3d<f32>``texture_cube_array<f32>`
- `smp` `sampler`
- `uv` optional `vec2`
- `dir` optional `vec3`
- `coord` optional `vec3`
- `ddx` `vec2``vec3`
- `layer` optional `number`
- `ddy` `vec2``vec3`

## Return value

`vec4`

## Description

On a `texture_2d_array` the layer comes before the gradients. Compiles to `textureSampleGrad` on WGSL and `textureGrad` on GLSL.

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

`textureSampleGrad(a, b, c, d, e)`

**GLSL ES 3.00**

`textureGrad(a, c, d, e)`

## Examples

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

## See also

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