---
title: "textureNumLayers(): IR builder function in typeshade/core/ir"
description: "textureNumLayers, an IR builder function in typeshade/core/ir. How many layers a 2D array texture has, as a u32."
url: https://typeshade.dev/api/texturenumlayers/
language: en
---

# `textureNumLayers()`

Function in Builtins

How many layers a 2D array texture has, as a `u32`.

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

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

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

## Syntax

```ts
const textureNumLayers: (
  tex: ReadonlyNode<TextureLoadArrayKey>,
) => Node<"u32">
```

## Parameters

**`tex` `ReadonlyNode<TextureLoadArrayKey>`**

the array-texture binding to measure.

## Return value

`Node<'u32'>`

the layer count.

## Description

[`textureDimensions`](https://typeshade.dev/api/texturedimensions/) reports the width and height only, `vec2<u32>`, for an array texture as much as for a plain one, so the layer count is this separate query. Wrap the result in [`toF32`](https://typeshade.dev/api/tof32/) for float arithmetic.

It accepts an array key only: a plain 2D or multisampled texture has no layer count, and passing one is a tsc error. An integer array texture is accepted, since the count is a property of the view and not of the texel element.

The targets spell it differently, which is why it carries its own id instead of overloading `textureDimensions`. WGSL has the dedicated `textureNumLayers(t)`. GLSL ES 3.00 has no such function and reads the third component of `textureSize(t, 0)`, exactly the component the `uvec2()` constructor behind `textureDimensions` drops. The lod argument is required there, and the layer count does not vary with lod, so `0` is always right.

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

## Examples

### Example

```ts
import { resource, textureNumLayers, toF32, texture2dArrayfT } from 'typeshade/core/ir'

const atlas = resource('atlas', texture2dArrayfT, { group: 0, binding: 1 })
const layers = toF32(textureNumLayers(atlas.node))
```

## Targets

| Target | Support | Notes |
| --- | --- | --- |
| WGSL (WebGPU) | Supported | `textureNumLayers(tex)`. |
| GLSL ES 3.00 (WebGL2) | Supported | `uint(textureSize(tex, 0).z)`. |
| 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

- [`textureNumLayers()`](https://typeshade.dev/reference/functions/textureNumLayers/)
- [`textureDimensions`](https://typeshade.dev/api/texturedimensions/)
- [`toF32`](https://typeshade.dev/api/tof32/)
- [`textureSample`](https://typeshade.dev/api/texturesample/)

## Source

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