---
title: "textureSample(): IR builder function in typeshade/core/ir"
description: "textureSample, an IR builder function in typeshade/core/ir. Sample a 2D texture, returning vec4<f32>."
url: https://typeshade.dev/api/texturesample/
language: en
---

# `textureSample()`

Function in Builtins

Sample a 2D texture, returning `vec4<f32>`.

`import { textureSample } from 'typeshade/core/ir'`

This export returns an IR node. A `"use typeshade"` file calls the builtin itself: [`textureSample()`](https://typeshade.dev/reference/functions/textureSample/)

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

## Syntax

```ts
function textureSample(
  tex: ReadonlyNode<'texture_2d<f32>'>,
  smp: ReadonlyNode<'sampler'>,
  uv: ReadonlyNode<'vec2<f32>'>,
): Node<'vec4<f32>'>;
function textureSample(
  tex: ReadonlyNode<'texture_2d_array<f32>'>,
  smp: ReadonlyNode<'sampler'>,
  uv: ReadonlyNode<'vec2<f32>'>,
  layer: ReadonlyNode<'i32' | 'u32'> | number,
): Node<'vec4<f32>'>;
```

## Parameters

**`tex` `ReadonlyNode<'texture_2d<f32>'>`**

the sampled texture binding, `resource(name, texture2dfT, at).node`.

**`smp` `ReadonlyNode<'sampler'>`**

the sampler binding to filter with.

**`uv` `ReadonlyNode<'vec2<f32>'>`**

normalised texture coordinates.

**`layer`**

which layer to read, required for an array texture and rejected otherwise.

## Return value

`Node<'vec4<f32>'>`

the filtered texel.

## Exceptions

**[`SD0109`](https://typeshade.dev/reference/errors/sd0109/)**

That makes this call fragment-only, and the fragment-only-builtin lint rule, a core rule that fires at every emit, reports SD0109 when it appears in a vertex or compute stage. `textureSampleLevel` takes the level as an argument and is legal in every stage, so it is the form a vertex or compute shader reaches for.

## Description

The level of detail is implicit: it comes from screen-space derivatives, which exist only in a fragment invocation. That makes this call fragment-only, and the `fragment-only-builtin` lint rule, a core rule that fires at every emit, reports [`SD0109`](https://typeshade.dev/reference/errors/sd0109/) when it appears in a vertex or compute stage. [`textureSampleLevel`](https://typeshade.dev/api/texturesamplelevel/) takes the level as an argument and is legal in every stage, so it is the form a vertex or compute shader reaches for.

A 2D array texture uses the same name, and the first argument’s key picks the overload: a `texture_2d_array<f32>` requires the `layer` argument, and omitting it is a tsc error. A `number` layer lifts to an `i32` literal. The targets spell the layer differently and the DSL absorbs that: WGSL takes it as its own argument, `textureSample(t, s, uv, layer)`, while GLSL ES 3.00 folds it into the coordinate, `texture(t, vec3(uv, float(layer)))`. Both spellings are core, so an array texture needs no capability on either target.

Integer texture keys (`texture_2d<u32>`, `texture_2d<i32>` and their array twins) are rejected at tsc, deliberately. Filtering is a weighted average, and interpolating integer texels has no meaning, so WGSL has no `textureSample` for them at all. GLSL’s `texture(usampler2D, …)` would compile, and accepting it would mint a construct that runs on WebGL2 and cannot be expressed on WebGPU. The surface both targets share for an integer texture is [`textureLoad`](https://typeshade.dev/api/textureload/), [`textureDimensions`](https://typeshade.dev/api/texturedimensions/) and [`textureNumLayers`](https://typeshade.dev/api/texturenumlayers/).

The CPU evaluation (`compileModule`) has no way to read a texture and returns a placeholder under its `gpuStubs` option.

## Examples

### Example

```ts
import { fn, resource, textureSample, texture2dfT, samplerT, vec2fT, vec4fT } from 'typeshade/core/ir'

const tex = resource('tex', texture2dfT, { group: 0, binding: 1 })
const smp = resource('tex_sampler', samplerT, { group: 0, binding: 2 })

const fs = fn('fs_main', { uv: vec2fT }, vec4fT, ({ uv }) => textureSample(tex.node, smp.node, uv), {
  stage: 'fragment',
})
```

## Targets

| Target | Support | Notes |
| --- | --- | --- |
| WGSL (WebGPU) | Supported | `textureSample(tex, smp, uv)`. |
| GLSL ES 3.00 (WebGL2) | Supported | `texture(tex, uv)`. |
| CPU oracle | Stub | The oracle has no texture memory and no neighbouring fragments. The call throws unless the module was compiled with `{ gpuStubs: true }`, which returns a placeholder. |

## In the guide

- [Layouts and resources](https://typeshade.dev/guide/internals/layouts-and-resources/)
- [The CPU oracle](https://typeshade.dev/guide/internals/the-cpu-oracle/)

## See also

- [`textureSample()`](https://typeshade.dev/reference/functions/textureSample/)
- [`textureSampleLevel`](https://typeshade.dev/api/texturesamplelevel/)
- [`textureLoad`](https://typeshade.dev/api/textureload/)
- [`textureDimensions`](https://typeshade.dev/api/texturedimensions/)
- [`textureNumLayers`](https://typeshade.dev/api/texturenumlayers/)
- [`resource`](https://typeshade.dev/api/resource/)

## Source

[`src/core/ir/node.ts`, line 1788](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/ir/node.ts#L1788), at commit af54ddad5
