---
title: "textureNumLayers: WGSL and GLSL function guide"
description: "A texture array contains independently addressable image layers. This query returns their count."
url: https://typeshade.dev/reference/functions/textureNumLayers/
language: en
---

# `textureNumLayers`

Function

A texture array contains independently addressable image layers. This query returns their count.

## Compare examples

The result is the array layer count. For a cube array, it counts cubes, not individual faces.

**Example 1`texture_2d_array: layers = 1`**

**Result**

`1`

**Example 2`texture_2d_array: layers = 4`**

**Result**

`4`

**Example 3`texture_cube_array: cubes = 3`**

**Result**

`3`

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 bound a layer selector or iterate over an image array.

## Inputs and limits

The texture must be an array texture. The highest valid layer index is one less than the count.

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

**WGSL**

`textureNumLayers(a)`

**GLSL ES 3.00**

`uint(textureSize(a, 0).z)`

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

Returns the number of layers of a `texture_2d_array` or a `texture_cube_array` as a `u32`.

## Syntax

```ts
declare function textureNumLayers<E extends TextureElem = f32>(tex: texture_cube_array<E>): u32
declare function textureNumLayers(tex: texture_depth_cube_array): u32
declare function textureNumLayers(tex: texture_depth_2d_array): u32
declare function textureNumLayers<E extends TextureElem = f32>(tex: texture_2d_array<E>): u32
declare function textureNumLayers<F extends StorageFormat, A extends StorageAccess>(
  tex: texture_storage_2d_array<F, A>,
): u32
```

A signature may name a type the ambient file declares for its own use. These are those declarations.

**`TextureElem`**

`type TextureElem = f32 | i32 | u32`

**`StorageFormat`**

`type StorageFormat = 'rgba8unorm' | 'rgba8snorm' | 'rgba8uint' | 'rgba8sint' | 'rgba16uint' | 'rgba16sint' | 'rgba16float' | 'r32uint' | 'r32sint' | 'r32float' | 'rg32uint' | 'rg32sint' | 'rg32float' | 'rgba32uint' | 'rgba32sint' | 'rgba32float' | 'bgra8unorm'`

**`StorageAccess`**

`type StorageAccess = 'write' | 'read' | 'read_write'`

## Parameters

- `tex` `texture_cube_array<E>``texture_depth_cube_array``texture_depth_2d_array``texture_2d_array<E>``texture_storage_2d_array<F, A>`

## Return value

`u32`

## Description

A texture with no layers is refused.

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

`textureNumLayers(a)`

**GLSL ES 3.00**

`uint(textureSize(a, 0).z)`

## Examples

- [`gallery/cube-array-gather/cube-array-gather.shade.ts`](https://typeshade.dev/guide/examples/cube-array-gather/)
- [`gallery/shadow-compare/shadow-compare.shade.ts`](https://typeshade.dev/guide/examples/shadow-compare/)

## See also

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