UNPKG

8.34 kBTypeScriptView Raw
1/// <reference types="@webgpu/types" />
2import 'reflect-metadata';
3import { Component, ComponentManager } from './ComponentManager';
4import { FrameGraphSystem } from './components/framegraph/System';
5import { GeometryComponent } from './components/geometry/GeometryComponent';
6import { GeometrySystem } from './components/geometry/System';
7import { MaterialComponent } from './components/material/MaterialComponent';
8import { MaterialSystem } from './components/material/System';
9import { CullableComponent } from './components/mesh/CullableComponent';
10import { MeshComponent } from './components/mesh/MeshComponent';
11import { MeshSystem } from './components/mesh/System';
12import { IView } from './components/renderer';
13import { PixelPickingPass } from './components/renderer/passes/PixelPickingPass';
14import { RendererSystem } from './components/renderer/System';
15import { HierarchyComponent } from './components/scenegraph/HierarchyComponent';
16import { SceneGraphSystem } from './components/scenegraph/System';
17import { TransformComponent } from './components/scenegraph/TransformComponent';
18import { createEntity } from './Entity';
19import { IDENTIFIER } from './identifier';
20import { container, createWorldContainer, lazyInject, lazyMultiInject } from './inversify.config';
21import { generateAABBFromVertices } from './utils/aabb';
22import { isSafari } from './utils/isSafari';
23/**
24 * inspired by Entitas' Systems
25 * @see https://github.com/sschmid/Entitas-CSharp/wiki/Systems
26 */
27export interface ISystem {
28 /**
29 * in a similar way to Unity's `Start()`, we can do some initialization works:
30 * * create global entities
31 * * init event listeners
32 */
33 initialize?(): Promise<void>;
34 /**
35 * in a similar way to Unity's `Update()`, run once per frame
36 */
37 execute?(views: IView[]): Promise<void>;
38 /**
39 * run at the end of each frame
40 */
41 cleanup?(): void;
42 /**
43 * run once at the end of your program
44 */
45 tearDown?(): void;
46}
47declare type Entity = number;
48declare enum AST_TOKEN_TYPES {
49 Void = "Void",
50 Boolean = "Boolean",
51 Float = "Float",
52 Uint32 = "Uint32",
53 Int32 = "Int32",
54 Vector = "Vector",
55 Vector2Float = "vec2<f32>",
56 Vector3Float = "vec3<f32>",
57 Vector4Float = "vec4<f32>",
58 Vector2Boolean = "vec2<bool>",
59 Vector3Boolean = "vec3<bool>",
60 Vector4Boolean = "vec4<bool>",
61 Vector2Uint = "vec2<u32>",
62 Vector3Uint = "vec3<u32>",
63 Vector4Uint = "vec4<u32>",
64 Vector2Int = "vec2<i32>",
65 Vector3Int = "vec3<i32>",
66 Vector4Int = "vec4<i32>",
67 Matrix = "Matrix",
68 Matrix3x3Float = "mat3x3<f32>",
69 Matrix4x4Float = "mat4x4<i32>",
70 Struct = "Struct",
71 FloatArray = "Float[]",
72 Vector4FloatArray = "vec4<f32>[]"
73}
74declare enum AST_NODE_TYPES {
75 Program = "Program",
76 Identifier = "Identifier",
77 VariableDeclaration = "VariableDeclaration",
78 BlockStatement = "BlockStatement",
79 ReturnStatement = "ReturnStatement",
80 FunctionDeclaration = "FunctionDeclaration",
81 VariableDeclarator = "VariableDeclarator",
82 AssignmentExpression = "AssignmentExpression",
83 LogicalExpression = "LogicalExpression",
84 BinaryExpression = "BinaryExpression",
85 ArrayExpression = "ArrayExpression",
86 UnaryExpression = "UnaryExpression",
87 UpdateExpression = "UpdateExpression",
88 FunctionExpression = "FunctionExpression",
89 MemberExpression = "MemberExpression",
90 ConditionalExpression = "ConditionalExpression",
91 ExpressionStatement = "ExpressionStatement",
92 CallExpression = "CallExpression",
93 NumThreadStatement = "NumThreadStatement",
94 StorageStatement = "StorageStatement",
95 DoWhileStatement = "DoWhileStatement",
96 WhileStatement = "WhileStatement",
97 ForStatement = "ForStatement",
98 BreakStatement = "BreakStatement",
99 ContinueStatement = "ContinueStatement",
100 IfStatement = "IfStatement",
101 ImportedFunctionStatement = "ImportedFunctionStatement"
102}
103declare enum STORAGE_CLASS {
104 Input = "Input",
105 Output = "Output",
106 Uniform = "Uniform",
107 Workgroup = "Workgroup",
108 UniformConstant = "UniformConstant",
109 Image = "Image",
110 StorageBuffer = "StorageBuffer",
111 Private = "Private",
112 Function = "Function"
113}
114declare type TypedArrayConstructor = Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor;
115declare type DataType = AST_TOKEN_TYPES.Uint32 | AST_TOKEN_TYPES.Int32 | AST_TOKEN_TYPES.Boolean | AST_TOKEN_TYPES.Float | AST_TOKEN_TYPES.Vector2Float | AST_TOKEN_TYPES.Vector3Float | AST_TOKEN_TYPES.Vector4Float | AST_TOKEN_TYPES.Vector2Int | AST_TOKEN_TYPES.Vector3Int | AST_TOKEN_TYPES.Vector4Int | AST_TOKEN_TYPES.Vector2Uint | AST_TOKEN_TYPES.Vector3Uint | AST_TOKEN_TYPES.Vector4Uint | AST_TOKEN_TYPES.Vector2Boolean | AST_TOKEN_TYPES.Vector3Boolean | AST_TOKEN_TYPES.Vector4Boolean | AST_TOKEN_TYPES.Matrix3x3Float | AST_TOKEN_TYPES.Matrix4x4Float | AST_TOKEN_TYPES.FloatArray | AST_TOKEN_TYPES.Vector4FloatArray | AST_TOKEN_TYPES.Void;
116interface GLSLContext {
117 /**
118 * 程序名
119 */
120 name: string;
121 shader?: string;
122 /**
123 * size of thread grid
124 * 即 WebGL 2 Compute 中的 dispatchCompute
125 * 或者 WebGPU 中的 dispatch
126 */
127 dispatch: [number, number, number];
128 /**
129 * size of each thread group
130 * Compute Shader 中的 local_size_x/y/z
131 */
132 threadGroupSize: [number, number, number];
133 /**
134 * 迭代次数,例如布局运算中需要迭代很多次才能到达稳定
135 */
136 maxIteration: number;
137 /**
138 * 是否需要 pingpong,如果存在输入和输出为同一个的情况
139 */
140 needPingpong: boolean;
141 /**
142 * 目前仅支持单一输出,受限于 WebGL 实现
143 */
144 output: {
145 name: string;
146 size?: [number, number];
147 textureSize?: [number, number];
148 length?: number;
149 typedArrayConstructor?: TypedArrayConstructor;
150 gpuBuffer?: GPUBuffer;
151 outputElementsPerTexel?: number;
152 };
153 /**
154 * 常量,可分成编译时和运行时两类:
155 * 1. 编译时即可确定值
156 * 2. 运行时:例如循环长度需要为常量,但在编译时又无法确定
157 * TODO 支持定义函数,例如 tensorflow 中的 DIV_CEIL
158 */
159 defines: Array<{
160 name: string;
161 type: DataType;
162 value: number;
163 runtime: boolean;
164 }>;
165 globalDeclarations: Array<{
166 name: string;
167 type: DataType;
168 value: string;
169 shared: boolean;
170 }>;
171 uniforms: Array<{
172 name: string;
173 type: DataType;
174 data?: number | number[] | Float32Array | Uint8Array | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array;
175 size?: [number, number];
176 storageClass: STORAGE_CLASS;
177 readonly: boolean;
178 writeonly: boolean;
179 isReferer?: boolean;
180 }>;
181}
182/**
183 * 根据目标平台生成 Shader 代码
184 * * WebGL GLSL 1.0
185 * * WebGPU Chrome/Edge GLSL 4.5 & WGSL @see https://gpuweb.github.io/gpuweb/wgsl.html
186 * * Safari WHLSL (maybe deprecated)
187 */
188declare enum Target {
189 GLSL100 = "GLSL100",
190 GLSL450 = "GLSL450",
191 WGSL = "WGSL"
192}
193declare const DefineValuePlaceholder = "__DefineValuePlaceholder__";
194interface KernelBundle {
195 shaders: {
196 [Target.WGSL]: string;
197 [Target.GLSL450]: string;
198 [Target.GLSL100]: string;
199 };
200 context?: GLSLContext;
201 toString(): string;
202}
203export * from './ComponentManager';
204export * from './services';
205export * from './shape';
206export * from './components/renderer';
207export * from './components/material/interface';
208export * from './components/mesh/interface';
209export * from './components/renderer';
210export { container, createWorldContainer, lazyInject, lazyMultiInject, createEntity, Component, ComponentManager, Entity, IDENTIFIER, FrameGraphSystem, GeometrySystem, RendererSystem, MaterialSystem, MeshSystem, SceneGraphSystem, CullableComponent, MeshComponent, TransformComponent, MaterialComponent, GeometryComponent, HierarchyComponent, isSafari, generateAABBFromVertices, PixelPickingPass, AST_TOKEN_TYPES, AST_NODE_TYPES, STORAGE_CLASS, Target, DefineValuePlaceholder, GLSLContext, DataType, KernelBundle, };