---
title: "textureSample / texture: WGSL and GLSL function guide"
description: "Filtered sampling looks up a texture at normalized coordinates. A sampler controls interpolation and addressing; nearby fragments determine the mip level."
url: https://typeshade.dev/reference/functions/textureSample/
language: en
---

# `textureSample`

Function

Filtered sampling looks up a texture at normalized coordinates. A sampler controls interpolation and addressing; nearby fragments determine the mip level.

**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: `texture`
- GLSL: `texture2D`

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

## Compare examples

Compare a texel centre with the centre of the grid. Linear filtering blends the surrounding texels.

**Model assumptions**`filter = linear; address = clamp-to-edge; level = 0`

**Example 1`uv = vec2(0.25, 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`

**Example 2`uv = vec2(0.5, 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.5`

**Example 3`uv = vec2(0.75, 0.75)`**

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

`1`

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 to put an image on a surface while reducing aliasing as the image gets smaller.

## Inputs and limits

This model uses a scalar channel, linear filtering, clamp-to-edge addressing, and the base mip. Real implicit sampling chooses detail from fragment derivatives.

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

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

**WGSL**

`textureSample(a, b, c)`

**GLSL ES 3.00**

`texture(a, c)`

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 implicit level of detail, in the fragment stage only: at `uv` on a `texture_2d`, by a `vec3` direction on a `texture_cube`, by a `vec3` coordinate on a `texture_3d`, by one `f32` on a `texture_1d`, and on an array texture the argument after the coordinate picks the layer.

## Syntax

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

## Return value

`vec4`

## Description

Compiles to `textureSample` on WGSL and `texture` on GLSL, where the layer is folded into a `vec3` coordinate; the GLSL lowering of a `texture_1d` or a `texture_cube_array` is not supported yet ([Rule 10.5](https://typeshade.dev/reference/rules/10-5/)), so a module using one emits WGSL alone and is not supported yet on the WebGL2 tier.

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

`textureSample(a, b, c)`

**GLSL ES 3.00**

`texture(a, c)`

## 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/sample-branch/sample-branch.shade.ts`](https://typeshade.dev/guide/examples/sample-branch/)
- [`gallery/textured-quad/textured-quad.shade.ts`](https://typeshade.dev/guide/examples/textured-quad/)

## See also

- [Functions](https://typeshade.dev/reference/functions/)
- [Builtin table](https://typeshade.dev/guide/language/builtins/#textures)
- [`textureSample()` in `typeshade/core/ir`](https://typeshade.dev/api/texturesample/)
