---
title: "constDecl(): TypeShade 셰이더 API의 레이아웃 분류 함수 참조 문서"
description: "constDecl, TypeShade API의 레이아웃 분류에 속하는 함수입니다. 모듈 수준 스칼라 상수를 선언하고, 그 타입이 지정된 참조를 한 번의 호출로 함께 받습니다."
url: https://typeshade.dev/ko/api/constdecl/
language: ko
---

# `constDecl()`

함수, 레이아웃 분류

모듈 수준 스칼라 상수를 선언하고, 그 타입이 지정된 참조를 한 번의 호출로 함께 받습니다.

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

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

## 구문

```ts
function constDecl<T extends ShaderType>(
  name: string,
  type: T,
  values: { readonly wgsl: number; readonly cpu: number },
): ConstHandle<T>
```

## 매개변수

**`name` `string`**

생성되는 상수 이름입니다.

**`type` `T extends ShaderType`**

상수의 스칼라 타입입니다.

**`values` `{ readonly wgsl: number; readonly cpu: number }`**

`wgsl`은 WGSL이 생성하는 리터럴이고, `cpu`는 CPU 오라클이 쓰는 값입니다.

## 반환값

`ConstHandle<T>`

[`ConstHandle`](https://typeshade.dev/ko/api/consthandle/)이며, `.decl`과 `.node`를 가집니다.

## 설명

WGSL은 `values.wgsl`을 리터럴로 생성합니다. 이 표기는 손으로 쓴 `ConstDecl`이 쓰는 표기와 같다(예: `PI = 3.14159265`). CPU 오라클은 같은 모듈을 CPU에서 배정밀도로 실행하며, `values.cpu`를 JS의 배정밀도 전체 정밀도로 계산합니다. 그래서 WGSL 리터럴을 일부러 짧게 적더라도 패리티 검사의 정밀도는 낮아지지 않습니다.

[`ConstHandle`](https://typeshade.dev/ko/api/consthandle/)을 반환합니다. 그 `.decl`은 `module({ consts })`에 넣거나, 더 흔하게는 `module({ uses })`에 넣습니다. 이 옵션은 자신에게 넘긴 핸들의 `.decl`을 모읍니다. `.node`는 호출 지점에서 쓰는 타입이 지정된 참조입니다. 그래서 이름을 바꾸거나 잘못 적은 상수는 쓰이는 모든 곳에서 `tsc` 오류가 됩니다. `vec4<f32>` 색상이나 `array<vec4<f32>, N>` 팔레트처럼 벡터, 행렬 또는 배열 타입인 상수에는 [`constExpr`](https://typeshade.dev/ko/api/constexpr/)를 씁니다. 이 함수는 WGSL/CPU 값 쌍 대신 상수 폴딩이 가능한 리터럴 노드 하나를 받습니다.

## 예제

### 예제

```ts
const TAU = constDecl('TAU', f32T, { wgsl: 6.2831853, cpu: Math.PI * 2 })
const g = fn('g', { t: f32T }, ({ t }) => t.mul(TAU.node))
const m = module({ consts: [TAU.decl], funcs: [g] })
```

## 함께 보기

- [`ConstHandle`](https://typeshade.dev/ko/api/consthandle/)
- [`constExpr`](https://typeshade.dev/ko/api/constexpr/)
- [`array`](https://typeshade.dev/ko/api/array/)

## 소스

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