선언
선언은 상수나 변수나 레이아웃에 이름을 붙입니다. 셋 중 무엇이냐에 따라 생성되는 코드가 있는지 없는지가 갈립니다.
상수와 변수
-
스칼라
const모듈 상수입니다. 스칼라는 선언 자리에서 값 하나로 접힙니다. module-const
TypeScriptconst GAIN: f32 = 1.5 * 2.생성된 WGSL const GAIN: f32 = 3.0;생성된 GLSL ES 3.00 const float GAIN = 3.0; -
벡터
const벡터나 배열 상수는 값을 식으로 지니고, 백엔드가 그 식을 평가합니다. palette-const
TypeScriptconst TINT: vec3 = vec3(0.2, 0.4, 0.9)생성된 WGSL const TINT: vec3<f32> = vec3<f32>(0.2, 0.4, 0.9);생성된 GLSL ES 3.00 const vec3 TINT = vec3(0.2, 0.4, 0.9); -
지역
let변경 가능한 지역 변수입니다. 타입은 표기가 정하고, 모든 타깃이 영에서 시작합니다. bitfield-bands
TypeScriptexport function band(x: f32): f32 {let w: f32w = x * 2.return w}생성된 WGSL fn band(x: f32) -> f32 {let w = (x * 2.0);return w;}생성된 GLSL ES 3.00 float band(float x) {float w = (x * 2.0);return w;} -
대입 전 읽기
거절대입하지 않은 경로에서 지역 변수를 읽으면
TS8075로 거부합니다. TypeScript가 TS2454로 보고하는 규칙과 같습니다. 편집기도 TS2454 대신TS8075를 보여 줍니다.TypeScriptexport function band(x: f32): f32 {let w: f32if (x > 0.) {w = x * 2.}return w}컴파일러 진단
TS8075 "w" is read here before it is assigned on every path. Assign it before this read, or declare it with a value (Rule 7.6).
-
모듈
let모듈 변수입니다. 호출마다 하나씩 생깁니다. private-state
TypeScriptlet seed: u32 = 7생성된 WGSL var<private> seed: u32 = 7u;생성된 GLSL ES 3.00 uint seed = 7u; -
거절var호출별 변수는
let, 모듈 상수는const입니다.TypeScriptvar k = 1.컴파일러 진단
TS8014 Top-level var is not allowed. Use `let` for a per-invocation variable or `const` for a module constant.
타입과 네임스페이스
-
enum멤버마다 모듈 상수가 하나씩 생깁니다. 이름은
Enum_Member, 타입은i32입니다.TypeScriptenum Mode {Flat,Shaded,Wire,}생성된 WGSL const Mode_Flat: i32 = 0;const Mode_Shaded: i32 = 1;const Mode_Wire: i32 = 2;생성된 GLSL ES 3.00 const int Mode_Flat = 0;const int Mode_Shaded = 1;const int Mode_Wire = 2; -
const enum같은 상수가 생깁니다. 여기서
const enum은 다르지 않습니다.TypeScriptconst enum Flag {None = 0,Both = 3,}생성된 WGSL const Flag_None: i32 = 0;const Flag_Both: i32 = 3;생성된 GLSL ES 3.00 const int Flag_None = 0;const int Flag_Both = 3; -
type별칭대상을 가리키는 다른 이름입니다. 타입이 올 수 있는 자리마다 풀립니다.
TypeScripttype Meters = f32생성된 WGSL fn deeper(d: f32) -> f32 {return (d * 2.0);}생성된 GLSL ES 3.00 float deeper(float d) {return (d * 2.0);} -
interface구조체입니다. 같은 필드를 쓴 class와 결과가 같습니다.
TypeScriptinterface Camera {view: mat4pos: vec3}생성된 WGSL struct Camera {view: mat4x4<f32>,pos: vec3<f32>,}생성된 GLSL ES 3.00 struct Camera {mat4 view;vec3 pos;}; -
구조체가 되는 클래스
구조체입니다. 필드가 호스트의 메모리 레이아웃을 정합니다. ray-class
TypeScriptclass Ray {origin: vec3dir: vec3}생성된 WGSL struct Ray {origin: vec3<f32>,dir: vec3<f32>,}생성된 GLSL ES 3.00 struct Ray {vec3 origin;vec3 dir;}; -
namespace멤버는
Ns_member로 펼쳐지고, namespace 자체는 아무것도 생성하지 않습니다.TypeScriptnamespace Palette {export const WARM: vec3 = vec3(0.9, 0.6, 0.3)export function tint(x: f32): vec3 {return WARM * x}}생성된 WGSL const Palette_WARM: vec3<f32> = vec3<f32>(0.9, 0.6, 0.3);fn Palette_tint(x: f32) -> vec3<f32> {return (Palette_WARM * x);}생성된 GLSL ES 3.00 const vec3 Palette_WARM = vec3(0.9, 0.6, 0.3);vec3 Palette_tint(float x) {return (Palette_WARM * x);}