textureSampleGrad
On this page

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

Compare examples

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

Model assumptionsuv = vec2(0.25, 0.25); filter = linear; mip = nearest; levels = [2 × 2, 1 × 1]

Example 1ddx = vec2(0.5, 0); ddy = vec2(0, 0.5)
0.75100.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 2ddx = 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 3ddx = vec2(0.25, 0); ddy = vec2(0, 0.25)
0.75100.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 and GLSL ES 3.00.

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

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 carries that row with the compiler's emit.

Parameters

Return value

vec4

Description

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

Targets

WGSL
textureSampleGrad(a, b, c, d, e)
GLSL ES 3.00
textureGrad(a, c, d, e)

Examples

See also

Edit this page Report a problem