---
title: "storageBuffer(): TypeShade 셰이더 API의 레이아웃 분류 함수 참조 문서"
description: "storageBuffer, TypeShade API의 레이아웃 분류에 속하는 함수입니다. 요소만으로 바인딩된 array<Element> 스토리지 버퍼를 선언합니다."
url: https://typeshade.dev/ko/api/storagebuffer/
language: ko
---

# `storageBuffer()`

함수, 레이아웃 분류

요소만으로 바인딩된 `array<Element>` 스토리지 버퍼를 선언합니다.

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

설명은 커밋 af54ddad5의 검토된 컴파일러 문서를 번역한 것입니다. 시그니처와 예제 코드는 원문을 따르며, 필요한 경우 설명에 편집상 수정이 반영됩니다.

## 구문

```ts
function storageBuffer<H extends StructHandle>(
  name: string,
  element: H,
  at: { group: number; binding: number; access: 'read' },
): StorageBuffer<ReturnType<H['of']>>;
function storageBuffer<H extends StructHandle>(
  name: string,
  element: H,
  at: { group: number; binding: number; access: 'read_write' },
): StorageBuffer<MutableView<ReturnType<H['of']>>>;
function storageBuffer<T extends ShaderType>(
  name: string,
  element: T,
  at: { group: number; binding: number; access: 'read' },
): StorageBuffer<ReadonlyNode<KeyOf<T>>>;
function storageBuffer<T extends ShaderType>(
  name: string,
  element: T,
  at: { group: number; binding: number; access: 'read_write' },
): StorageBuffer<Node<KeyOf<T>>>;
```

## 매개변수

**`name` `string`**

생성되는 바인딩 이름입니다. 호스트가 바인딩할 때 쓰는 이름이기도 합니다.

**`element` `H extends StructHandle`**

배열의 요소입니다. 구조체 핸들, 또는 스칼라 또는 벡터 타입입니다.

**`at` `{ group: number; binding: number; access: 'read' }`**

`group`과 `binding` 슬롯, 그리고 `access`(`'read'` 또는 `'read_write'`)입니다.

## 반환값

`StorageBuffer<ReturnType<H['of']>>`

위에서 설명한 [`StorageBuffer`](https://typeshade.dev/ko/api/storagebuffer-interface/) 핸들입니다.

## 예외

**`UnsupportedFeatureError`**

이 경로는 읽기만 지원합니다. read\_write 바인딩은 GLSL 생성 시점에 UnsupportedFeatureError를 던지므로, 쓰기가 조용히 버려지지 않습니다.

## 설명

요소는 구조체 핸들([`structDecl`](https://typeshade.dev/ko/api/structdecl/) 또는 [`ioStruct`](https://typeshade.dev/ko/api/iostruct/))이거나 스칼라 또는 벡터 셰이더 타입입니다. 핸들은 바인딩 선언, access 노드, 그리고 타입이 지정된 `.at(i)`를 도출합니다. 배열의 길이는 실행 시점에 정해지며 호스트가 바인딩하는 버퍼의 크기를 따릅니다. 그래서 개수 인자를 받지 않습니다.

구조체 요소의 경우 `.at(i)`는 타입이 지정된 필드 프록시를 반환합니다. 따라서 `buf.at(i).p0`는 `.of()`나 요소 타입 인자 없이 타입이 지정된 읽기가 됩니다. 스칼라 요소의 경우에는 요소 노드를 직접 반환합니다. `.binding`은 `module({ bindings })`에 넣거나, 핸들을 `uses`에 넘기면 요소 구조체도 함께 등록됩니다.

`access`는 요소 뷰의 쓰기 권한을 타입 수준에서 결정합니다. `'read'`는 읽기 전용 필드를 내주므로 `buf.at(i).p0.assign(...)`는 tsc 오류가 됩니다. `'read_write'`는 변경 가능한 필드를 내줍니다.

GLSL ES 3.00에서는 배열을 데이터 텍스처로 생성하고, 각 읽기를 `texelFetch`로 바꿉니다. 작성 위치에서는 달라지는 것이 없습니다. 이 GLSL 생성 경로는 읽기만 지원합니다. `read_write` 바인딩은 GLSL 생성 시점에 [`UnsupportedFeatureError`](https://typeshade.dev/ko/api/unsupportedfeatureerror/)를 던지므로, 쓰기가 조용히 버려지는 일은 없습니다. `@compute` 진입점은 WebGL2에서 패스 프로그램으로 스토리지에 씁니다([Rule 8.24](https://typeshade.dev/ko/reference/rules/8-24/)). 커널 함수는 각 루프가 배열 하나를 `i` 위치에 쓰는 경우에 스토리지에 쓸 수 있습니다([Rule 11.8](https://typeshade.dev/ko/reference/rules/11-8/)).

DSL이 검사할 수 없는 의무가 호스트에게 하나 있습니다. 데이터 텍스처에 요소와 맞는 내부 포맷을 지정하는 것입니다. `array<u32>`와 `array<vecN<u32>>`는 `usampler2D`가 되며 R32UI가 필요합니다. `array<i32>`와 `array<vecN<i32>>`는 `isampler2D`가 되며 R32I가 필요합니다. float의 경우에는 R32F가 필요합니다. 벡터 요소는 자기 std430 레인을 차지합니다. 구조체 요소는 필드가 모두 float이면 R32F가 필요합니다. `u32`나 `i32` 필드가 있으면 R32UI가 필요하며, 이때 float 필드는 `uintBitsToFloat`를 거쳐 다시 읽힙니다. 포맷이 샘플러 타입과 맞지 않는 텍스처는 불완전할 뿐이고, 이때 아무 오류도 나지 않습니다. 그 경우 `texelFetch`는 0을 반환합니다. 포맷은 `reflect(m)`에서 확인할 수 있습니다. 바인딩마다 `glslDataTexture`가 포맷을 보고합니다.

정수를 R32F 텍스처에 실어 `floatBitsToUint`로 되돌리는 방식은 대개 동작하더라도 안전하지 않습니다. GLSL ES 3.00은 구현이 비정규화 수를 모두 0으로 밀어내는 것을 허용합니다. 그런데 작은 정수는 비정규화 `f32` 비트 패턴입니다(`1u`는 1.4e-45). 따라서 이 경로에서는 스펙상 값이 사라질 수 있습니다.

## 예제

### 예제

```ts
import { storageBuffer, structDecl, u32T, vec2fT } from 'typeshade/core/ir'

const ShapeSegment = structDecl('ShapeSegment', { kind: u32T, p0: vec2fT, p1: vec2fT })
const segments = storageBuffer('segments', ShapeSegment, { group: 0, binding: 9, access: 'read' })
const seg = segments.at(i)
seg.p0 // Node<'vec2<f32>'>

const featIds = storageBuffer('feat_ids', u32T, { group: 0, binding: 0, access: 'read' })
featIds.at(i) // Node<'u32'>: an SSBO read on WGSL, a texelFetch on GLSL
```

## 관련 가이드

- [기능과 확장](https://typeshade.dev/ko/guide/internals/capabilities-extensions/)

## 함께 보기

- [`structDecl`](https://typeshade.dev/ko/api/structdecl/)
- [`ioStruct`](https://typeshade.dev/ko/api/iostruct/)
- [`UnsupportedFeatureError`](https://typeshade.dev/ko/api/unsupportedfeatureerror/)
- [`StorageBuffer`](https://typeshade.dev/ko/api/storagebuffer-interface/)
- [`reflect`](https://typeshade.dev/ko/api/reflect/)
- [`texture`](https://typeshade.dev/ko/api/texture/)
- [`arrayOf`](https://typeshade.dev/ko/api/arrayof/)
- [`length`](https://typeshade.dev/ko/api/length/)
- [`array`](https://typeshade.dev/ko/api/array/)
- [`uniform`](https://typeshade.dev/ko/api/uniform/)
- [`resource`](https://typeshade.dev/ko/api/resource/)

## 소스

[`src/core/sot.ts`, 1102행](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/sot.ts#L1102), 커밋 af54ddad5 기준
