@luma.gl/engine 9.4.0 → 10.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/animation-loop/animation-loop.d.ts +12 -5
- package/dist/animation-loop/animation-loop.d.ts.map +1 -1
- package/dist/animation-loop/animation-loop.js +22 -7
- package/dist/animation-loop/animation-loop.js.map +1 -1
- package/dist/animation-loop/animation-props.d.ts +3 -1
- package/dist/animation-loop/animation-props.d.ts.map +1 -1
- package/dist/animation-loop/make-animation-loop.d.ts.map +1 -1
- package/dist/animation-loop/make-animation-loop.js +3 -1
- package/dist/animation-loop/make-animation-loop.js.map +1 -1
- package/dist/compute/computation.d.ts +12 -0
- package/dist/compute/computation.d.ts.map +1 -1
- package/dist/compute/computation.js +85 -22
- package/dist/compute/computation.js.map +1 -1
- package/dist/compute/kernel.d.ts +54 -0
- package/dist/compute/kernel.d.ts.map +1 -0
- package/dist/compute/kernel.js +103 -0
- package/dist/compute/kernel.js.map +1 -0
- package/dist/compute/render-kernel.d.ts +65 -0
- package/dist/compute/render-kernel.d.ts.map +1 -0
- package/dist/compute/render-kernel.js +129 -0
- package/dist/compute/render-kernel.js.map +1 -0
- package/dist/controls/orbit-controls.d.ts +9 -2
- package/dist/controls/orbit-controls.d.ts.map +1 -1
- package/dist/controls/orbit-controls.js +55 -8
- package/dist/controls/orbit-controls.js.map +1 -1
- package/dist/dist.dev.js +502 -134
- package/dist/dist.min.js +23 -23
- package/dist/index.cjs +504 -117
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/model/model.d.ts +13 -2
- package/dist/model/model.d.ts.map +1 -1
- package/dist/model/model.js +137 -66
- package/dist/model/model.js.map +1 -1
- package/dist/passes/shader-pass-renderer.d.ts +12 -12
- package/dist/passes/shader-pass-renderer.d.ts.map +1 -1
- package/dist/passes/shader-pass-renderer.js +17 -17
- package/dist/passes/shader-pass-renderer.js.map +1 -1
- package/package.json +4 -4
- package/src/animation-loop/animation-loop.ts +36 -14
- package/src/animation-loop/animation-props.ts +4 -1
- package/src/animation-loop/make-animation-loop.ts +3 -1
- package/src/compute/computation.ts +83 -30
- package/src/compute/kernel.ts +146 -0
- package/src/compute/render-kernel.ts +192 -0
- package/src/controls/orbit-controls.ts +61 -9
- package/src/index.ts +12 -0
- package/src/model/model.ts +149 -87
- package/src/passes/shader-pass-renderer.ts +28 -25
|
@@ -77,6 +77,36 @@ export type ComputationProps = Omit<ComputePipelineProps, 'shader'> & {
|
|
|
77
77
|
* - accepts modules and performs shader transpilation
|
|
78
78
|
*/
|
|
79
79
|
export class Computation {
|
|
80
|
+
/**
|
|
81
|
+
* Creates a computation while allowing the backend to compile its pipeline asynchronously.
|
|
82
|
+
*
|
|
83
|
+
* Use this factory when several independent computations can be prepared together. Calling the
|
|
84
|
+
* constructor remains the synchronous compatibility path.
|
|
85
|
+
*/
|
|
86
|
+
static async createAsync(device: Device, props: ComputationProps): Promise<Computation> {
|
|
87
|
+
const ownsCompilation = !PipelineFactory.getAsyncCompilation(device);
|
|
88
|
+
const asyncCompilation = ownsCompilation
|
|
89
|
+
? PipelineFactory.beginAsyncCompilation(device)
|
|
90
|
+
: PipelineFactory.getAsyncCompilation(device)!;
|
|
91
|
+
let computation: Computation;
|
|
92
|
+
try {
|
|
93
|
+
computation = new Computation(device, props);
|
|
94
|
+
} finally {
|
|
95
|
+
if (ownsCompilation) PipelineFactory.endAsyncCompilation(device, asyncCompilation);
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
if (ownsCompilation) {
|
|
99
|
+
await Promise.all(asyncCompilation);
|
|
100
|
+
} else {
|
|
101
|
+
await computation._pipelineInitialization;
|
|
102
|
+
}
|
|
103
|
+
return computation;
|
|
104
|
+
} catch (error) {
|
|
105
|
+
computation.destroy();
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
80
110
|
static defaultProps: Required<ComputationProps> = {
|
|
81
111
|
...ComputePipeline.defaultProps,
|
|
82
112
|
id: 'unnamed',
|
|
@@ -110,7 +140,7 @@ export class Computation {
|
|
|
110
140
|
bindings: Record<string, Binding> = {};
|
|
111
141
|
|
|
112
142
|
/** The underlying GPU pipeline. */
|
|
113
|
-
pipeline
|
|
143
|
+
pipeline!: ComputePipeline;
|
|
114
144
|
/** Assembled compute shader source */
|
|
115
145
|
source: string;
|
|
116
146
|
/** the underlying compiled compute shader */
|
|
@@ -129,6 +159,7 @@ export class Computation {
|
|
|
129
159
|
private props: Required<ComputationProps>;
|
|
130
160
|
|
|
131
161
|
private _destroyed = false;
|
|
162
|
+
private _pipelineInitialization?: Promise<ComputePipeline>;
|
|
132
163
|
|
|
133
164
|
constructor(device: Device, props: ComputationProps) {
|
|
134
165
|
if (device.type !== 'webgpu') {
|
|
@@ -209,7 +240,13 @@ export class Computation {
|
|
|
209
240
|
|
|
210
241
|
// Create the pipeline
|
|
211
242
|
// @note order is important
|
|
212
|
-
|
|
243
|
+
const asyncCompilation = PipelineFactory.getAsyncCompilation(this.device);
|
|
244
|
+
if (asyncCompilation) {
|
|
245
|
+
this._pipelineInitialization = this._updatePipelineAsync();
|
|
246
|
+
asyncCompilation.push(this._pipelineInitialization);
|
|
247
|
+
} else {
|
|
248
|
+
this.pipeline = this._updatePipeline();
|
|
249
|
+
}
|
|
213
250
|
|
|
214
251
|
// Apply any dynamic settings that will not trigger pipeline change
|
|
215
252
|
if (props.bindings) {
|
|
@@ -219,8 +256,8 @@ export class Computation {
|
|
|
219
256
|
|
|
220
257
|
destroy(): void {
|
|
221
258
|
if (this._destroyed) return;
|
|
222
|
-
this.pipelineFactory.release(this.pipeline);
|
|
223
|
-
this.shaderFactory.release(this.shader);
|
|
259
|
+
if (this.pipeline) this.pipelineFactory.release(this.pipeline);
|
|
260
|
+
if (this.shader) this.shaderFactory.release(this.shader);
|
|
224
261
|
this._uniformStore.destroy();
|
|
225
262
|
this._destroyed = true;
|
|
226
263
|
}
|
|
@@ -266,7 +303,6 @@ export class Computation {
|
|
|
266
303
|
// Any caching needs to be done inside the pipeline functions
|
|
267
304
|
this.pipeline.setBindings(this.bindings);
|
|
268
305
|
computePass.setPipeline(this.pipeline);
|
|
269
|
-
// @ts-expect-error ComputePass implementations expose binding application internally.
|
|
270
306
|
computePass.setBindings({});
|
|
271
307
|
}
|
|
272
308
|
|
|
@@ -345,37 +381,54 @@ export class Computation {
|
|
|
345
381
|
}
|
|
346
382
|
|
|
347
383
|
_updatePipeline(): ComputePipeline {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
prevShader = this.shader;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
this._pipelineNeedsUpdate = false;
|
|
359
|
-
|
|
360
|
-
this.shader = this.shaderFactory.createShader({
|
|
361
|
-
id: `${this.id}-fragment`,
|
|
362
|
-
stage: 'compute',
|
|
363
|
-
source: this.source,
|
|
364
|
-
debugShaders: this.props.debugShaders
|
|
365
|
-
});
|
|
366
|
-
|
|
367
|
-
this.pipeline = this.pipelineFactory.createComputePipeline({
|
|
368
|
-
...this.props,
|
|
369
|
-
shader: this.shader
|
|
370
|
-
});
|
|
384
|
+
const update = this._preparePipelineUpdate();
|
|
385
|
+
if (update) {
|
|
386
|
+
this.pipeline = this.pipelineFactory.createComputePipeline(update.props);
|
|
387
|
+
this._finishPipelineUpdate(update.previousShader);
|
|
388
|
+
}
|
|
389
|
+
return this.pipeline;
|
|
390
|
+
}
|
|
371
391
|
|
|
372
|
-
|
|
373
|
-
|
|
392
|
+
/** Creates or replaces the pipeline through the backend's asynchronous compilation path. */
|
|
393
|
+
async _updatePipelineAsync(): Promise<ComputePipeline> {
|
|
394
|
+
const update = this._preparePipelineUpdate();
|
|
395
|
+
if (update) {
|
|
396
|
+
try {
|
|
397
|
+
this.pipeline = await this.pipelineFactory.createComputePipelineAsync(update.props);
|
|
398
|
+
} catch (error) {
|
|
399
|
+
this.shaderFactory.release(this.shader);
|
|
400
|
+
this.shader = update.previousShader!;
|
|
401
|
+
this._pipelineNeedsUpdate = 'asynchronous pipeline creation failed';
|
|
402
|
+
throw error;
|
|
374
403
|
}
|
|
404
|
+
this._finishPipelineUpdate(update.previousShader);
|
|
375
405
|
}
|
|
376
406
|
return this.pipeline;
|
|
377
407
|
}
|
|
378
408
|
|
|
409
|
+
private _preparePipelineUpdate(): {
|
|
410
|
+
props: ComputePipelineProps;
|
|
411
|
+
previousShader: Shader | null;
|
|
412
|
+
} | null {
|
|
413
|
+
if (!this._pipelineNeedsUpdate) return null;
|
|
414
|
+
const previousShader = this.pipeline ? this.shader : null;
|
|
415
|
+
if (this.pipeline) {
|
|
416
|
+
log.log(1, `Model ${this.id}: Recreating pipeline because "${this._pipelineNeedsUpdate}".`)();
|
|
417
|
+
}
|
|
418
|
+
this._pipelineNeedsUpdate = false;
|
|
419
|
+
this.shader = this.shaderFactory.createShader({
|
|
420
|
+
id: `${this.id}-fragment`,
|
|
421
|
+
stage: 'compute',
|
|
422
|
+
source: this.source,
|
|
423
|
+
debugShaders: this.props.debugShaders
|
|
424
|
+
});
|
|
425
|
+
return {props: {...this.props, shader: this.shader}, previousShader};
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
private _finishPipelineUpdate(previousShader: Shader | null): void {
|
|
429
|
+
if (previousShader) this.shaderFactory.release(previousShader);
|
|
430
|
+
}
|
|
431
|
+
|
|
379
432
|
/** Throttle draw call logging */
|
|
380
433
|
_lastLogTime = 0;
|
|
381
434
|
_logOpen = false;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {Binding, ComputePipelineProps, Shader} from '@luma.gl/core';
|
|
6
|
+
import {
|
|
7
|
+
Buffer,
|
|
8
|
+
ComputePass,
|
|
9
|
+
ComputePipeline,
|
|
10
|
+
Device,
|
|
11
|
+
PipelineFactory,
|
|
12
|
+
ShaderFactory
|
|
13
|
+
} from '@luma.gl/core';
|
|
14
|
+
|
|
15
|
+
/** Properties for a lightweight immutable WebGPU compute kernel. */
|
|
16
|
+
export type KernelProps = Omit<ComputePipelineProps, 'shader'> & {
|
|
17
|
+
/** WGSL compute shader source. Shader assembly belongs to higher-level APIs. */
|
|
18
|
+
source: string;
|
|
19
|
+
/** Factory used to create/reuse the compute pipeline. */
|
|
20
|
+
pipelineFactory?: PipelineFactory;
|
|
21
|
+
/** Factory used to create/reuse the compute shader. */
|
|
22
|
+
shaderFactory?: ShaderFactory;
|
|
23
|
+
/** Show shader source in browser debugging UI. */
|
|
24
|
+
debugShaders?: 'never' | 'errors' | 'warnings' | 'always';
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** Bindings and workgroup dimensions for one direct dispatch. */
|
|
28
|
+
export type KernelDispatchOptions = {
|
|
29
|
+
bindings?: Record<string, Binding>;
|
|
30
|
+
x: number;
|
|
31
|
+
y?: number;
|
|
32
|
+
z?: number;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Bindings and GPU-written dispatch record for one indirect dispatch. */
|
|
36
|
+
export type KernelDispatchIndirectOptions = {
|
|
37
|
+
bindings?: Record<string, Binding>;
|
|
38
|
+
indirectBuffer: Buffer;
|
|
39
|
+
indirectOffset?: number;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Lightweight executable WebGPU compute program.
|
|
44
|
+
*
|
|
45
|
+
* `Kernel` deliberately does not assemble shadertools modules or own ShaderInputs/uniform state.
|
|
46
|
+
* Higher-level APIs may assemble WGSL before constructing a kernel. Bindings are supplied per
|
|
47
|
+
* dispatch so a kernel remains reusable and does not retain mutable resource binding state.
|
|
48
|
+
*/
|
|
49
|
+
export class Kernel {
|
|
50
|
+
/** Creates a kernel while allowing the backend to compile its pipeline asynchronously. */
|
|
51
|
+
static async createAsync(device: Device, props: KernelProps): Promise<Kernel> {
|
|
52
|
+
const ownsCompilation = !PipelineFactory.getAsyncCompilation(device);
|
|
53
|
+
const asyncCompilation = ownsCompilation
|
|
54
|
+
? PipelineFactory.beginAsyncCompilation(device)
|
|
55
|
+
: PipelineFactory.getAsyncCompilation(device)!;
|
|
56
|
+
let kernel: Kernel;
|
|
57
|
+
try {
|
|
58
|
+
kernel = new Kernel(device, props);
|
|
59
|
+
} finally {
|
|
60
|
+
if (ownsCompilation) PipelineFactory.endAsyncCompilation(device, asyncCompilation);
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
if (ownsCompilation) {
|
|
64
|
+
await Promise.all(asyncCompilation);
|
|
65
|
+
} else {
|
|
66
|
+
await kernel._pipelineInitialization;
|
|
67
|
+
}
|
|
68
|
+
return kernel;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
kernel.destroy();
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
readonly device: Device;
|
|
76
|
+
readonly id: string;
|
|
77
|
+
readonly source: string;
|
|
78
|
+
readonly pipelineFactory: PipelineFactory;
|
|
79
|
+
readonly shaderFactory: ShaderFactory;
|
|
80
|
+
|
|
81
|
+
shader: Shader;
|
|
82
|
+
pipeline!: ComputePipeline;
|
|
83
|
+
|
|
84
|
+
private _pipelineInitialization?: Promise<ComputePipeline>;
|
|
85
|
+
private _destroyed = false;
|
|
86
|
+
|
|
87
|
+
constructor(device: Device, props: KernelProps) {
|
|
88
|
+
if (device.type !== 'webgpu') {
|
|
89
|
+
throw new Error('Kernel is only supported in WebGPU');
|
|
90
|
+
}
|
|
91
|
+
const {source, pipelineFactory, shaderFactory, debugShaders, ...pipelineProps} = props;
|
|
92
|
+
|
|
93
|
+
this.device = device;
|
|
94
|
+
this.id = props.id ?? 'kernel';
|
|
95
|
+
this.source = source;
|
|
96
|
+
this.pipelineFactory = pipelineFactory ?? PipelineFactory.getDefaultPipelineFactory(device);
|
|
97
|
+
this.shaderFactory = shaderFactory ?? ShaderFactory.getDefaultShaderFactory(device);
|
|
98
|
+
this.shader = this.shaderFactory.createShader({
|
|
99
|
+
id: `${this.id}-compute`,
|
|
100
|
+
stage: 'compute',
|
|
101
|
+
source: this.source,
|
|
102
|
+
debugShaders
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const computePipelineProps: ComputePipelineProps = {...pipelineProps, shader: this.shader};
|
|
106
|
+
|
|
107
|
+
const asyncCompilation = PipelineFactory.getAsyncCompilation(device);
|
|
108
|
+
if (asyncCompilation) {
|
|
109
|
+
const pipelineInitialization =
|
|
110
|
+
this.pipelineFactory.createComputePipelineAsync(computePipelineProps);
|
|
111
|
+
this._pipelineInitialization = pipelineInitialization;
|
|
112
|
+
asyncCompilation.push(
|
|
113
|
+
pipelineInitialization.then(pipeline => {
|
|
114
|
+
this.pipeline = pipeline;
|
|
115
|
+
return pipeline;
|
|
116
|
+
})
|
|
117
|
+
);
|
|
118
|
+
} else {
|
|
119
|
+
this.pipeline = this.pipelineFactory.createComputePipeline(computePipelineProps);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Applies per-dispatch bindings and records a direct compute dispatch. */
|
|
124
|
+
dispatch(computePass: ComputePass, options: KernelDispatchOptions): void {
|
|
125
|
+
this._setPipeline(computePass, options.bindings);
|
|
126
|
+
computePass.dispatch(options.x, options.y, options.z);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Applies per-dispatch bindings and records a GPU-driven indirect compute dispatch. */
|
|
130
|
+
dispatchIndirect(computePass: ComputePass, options: KernelDispatchIndirectOptions): void {
|
|
131
|
+
this._setPipeline(computePass, options.bindings);
|
|
132
|
+
computePass.dispatchIndirect(options.indirectBuffer, options.indirectOffset ?? 0);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
destroy(): void {
|
|
136
|
+
if (this._destroyed) return;
|
|
137
|
+
if (this.pipeline) this.pipelineFactory.release(this.pipeline);
|
|
138
|
+
this.shaderFactory.release(this.shader);
|
|
139
|
+
this._destroyed = true;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private _setPipeline(computePass: ComputePass, bindings: Record<string, Binding> = {}): void {
|
|
143
|
+
computePass.setPipeline(this.pipeline);
|
|
144
|
+
computePass.setBindings(bindings);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
Binding,
|
|
7
|
+
RenderPass,
|
|
8
|
+
RenderPassDrawOptions,
|
|
9
|
+
RenderPipelineProps,
|
|
10
|
+
Shader,
|
|
11
|
+
VertexArray
|
|
12
|
+
} from '@luma.gl/core';
|
|
13
|
+
import {Buffer, Device, PipelineFactory, RenderPipeline, ShaderFactory} from '@luma.gl/core';
|
|
14
|
+
|
|
15
|
+
/** Properties for a lightweight immutable WebGPU render kernel. */
|
|
16
|
+
export type RenderKernelProps = Omit<RenderPipelineProps, 'vs' | 'fs'> & {
|
|
17
|
+
/** Already assembled WGSL vertex shader source. */
|
|
18
|
+
vertexSource: string;
|
|
19
|
+
/** Already assembled WGSL fragment shader source. Omit for vertex/depth-only pipelines. */
|
|
20
|
+
fragmentSource?: string;
|
|
21
|
+
/** Factory used to create/reuse the render pipeline. */
|
|
22
|
+
pipelineFactory?: PipelineFactory;
|
|
23
|
+
/** Factory used to create/reuse the render shaders. */
|
|
24
|
+
shaderFactory?: ShaderFactory;
|
|
25
|
+
/** Show shader source in browser debugging UI. */
|
|
26
|
+
debugShaders?: 'never' | 'errors' | 'warnings' | 'always';
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Per-draw state that deliberately remains outside the immutable render kernel. */
|
|
30
|
+
export type RenderKernelDrawOptions = RenderPassDrawOptions & {
|
|
31
|
+
/** Complete resource binding set for this draw. */
|
|
32
|
+
bindings?: Record<string, Binding>;
|
|
33
|
+
/** Vertex/index bindings consumed by this draw. */
|
|
34
|
+
vertexArray: VertexArray;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Per-indirect-draw state shared by indexed and non-indexed indirect rendering. */
|
|
38
|
+
export type RenderKernelIndirectDrawOptions = {
|
|
39
|
+
/** Complete resource binding set for this draw. */
|
|
40
|
+
bindings?: Record<string, Binding>;
|
|
41
|
+
/** Vertex/index bindings consumed by this draw. */
|
|
42
|
+
vertexArray: VertexArray;
|
|
43
|
+
/** Buffer containing WebGPU indirect draw arguments. */
|
|
44
|
+
indirectBuffer: Buffer;
|
|
45
|
+
/** Byte offset of the indirect draw record. */
|
|
46
|
+
indirectOffset?: number;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Lightweight executable WebGPU render program.
|
|
51
|
+
*
|
|
52
|
+
* `RenderKernel` owns compiled shaders and a cached render pipeline only. Shader assembly,
|
|
53
|
+
* uniforms, geometry ownership, draw counts, command graphs and scene/model policy stay above it.
|
|
54
|
+
* Bindings and vertex arrays are supplied per draw so the kernel does not retain mutable draw state.
|
|
55
|
+
*/
|
|
56
|
+
export class RenderKernel {
|
|
57
|
+
/** Creates a render kernel while allowing the backend to compile its pipeline asynchronously. */
|
|
58
|
+
static async createAsync(device: Device, props: RenderKernelProps): Promise<RenderKernel> {
|
|
59
|
+
const ownsCompilation = !PipelineFactory.getAsyncCompilation(device);
|
|
60
|
+
const asyncCompilation = ownsCompilation
|
|
61
|
+
? PipelineFactory.beginAsyncCompilation(device)
|
|
62
|
+
: PipelineFactory.getAsyncCompilation(device)!;
|
|
63
|
+
let kernel: RenderKernel;
|
|
64
|
+
try {
|
|
65
|
+
kernel = new RenderKernel(device, props);
|
|
66
|
+
} finally {
|
|
67
|
+
if (ownsCompilation) PipelineFactory.endAsyncCompilation(device, asyncCompilation);
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
if (ownsCompilation) {
|
|
71
|
+
await Promise.all(asyncCompilation);
|
|
72
|
+
} else {
|
|
73
|
+
await kernel._pipelineInitialization;
|
|
74
|
+
}
|
|
75
|
+
return kernel;
|
|
76
|
+
} catch (error) {
|
|
77
|
+
kernel.destroy();
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
readonly device: Device;
|
|
83
|
+
readonly id: string;
|
|
84
|
+
readonly vertexSource: string;
|
|
85
|
+
readonly fragmentSource?: string;
|
|
86
|
+
readonly pipelineFactory: PipelineFactory;
|
|
87
|
+
readonly shaderFactory: ShaderFactory;
|
|
88
|
+
|
|
89
|
+
readonly vertexShader: Shader;
|
|
90
|
+
readonly fragmentShader: Shader | null;
|
|
91
|
+
pipeline!: RenderPipeline;
|
|
92
|
+
|
|
93
|
+
private _pipelineInitialization?: Promise<RenderPipeline>;
|
|
94
|
+
private _destroyed = false;
|
|
95
|
+
|
|
96
|
+
constructor(device: Device, props: RenderKernelProps) {
|
|
97
|
+
if (device.type !== 'webgpu') {
|
|
98
|
+
throw new Error('RenderKernel is only supported in WebGPU');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const {
|
|
102
|
+
vertexSource,
|
|
103
|
+
fragmentSource,
|
|
104
|
+
pipelineFactory,
|
|
105
|
+
shaderFactory,
|
|
106
|
+
debugShaders,
|
|
107
|
+
...pipelineProps
|
|
108
|
+
} = props;
|
|
109
|
+
|
|
110
|
+
this.device = device;
|
|
111
|
+
this.id = props.id ?? 'render-kernel';
|
|
112
|
+
this.vertexSource = vertexSource;
|
|
113
|
+
this.fragmentSource = fragmentSource;
|
|
114
|
+
this.pipelineFactory = pipelineFactory ?? PipelineFactory.getDefaultPipelineFactory(device);
|
|
115
|
+
this.shaderFactory = shaderFactory ?? ShaderFactory.getDefaultShaderFactory(device);
|
|
116
|
+
|
|
117
|
+
this.vertexShader = this.shaderFactory.createShader({
|
|
118
|
+
id: `${this.id}-vertex`,
|
|
119
|
+
stage: 'vertex',
|
|
120
|
+
source: this.vertexSource,
|
|
121
|
+
debugShaders
|
|
122
|
+
});
|
|
123
|
+
this.fragmentShader = this.fragmentSource
|
|
124
|
+
? this.shaderFactory.createShader({
|
|
125
|
+
id: `${this.id}-fragment`,
|
|
126
|
+
stage: 'fragment',
|
|
127
|
+
source: this.fragmentSource,
|
|
128
|
+
debugShaders
|
|
129
|
+
})
|
|
130
|
+
: null;
|
|
131
|
+
|
|
132
|
+
const renderPipelineProps: RenderPipelineProps = {
|
|
133
|
+
...pipelineProps,
|
|
134
|
+
vs: this.vertexShader,
|
|
135
|
+
fs: this.fragmentShader
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const asyncCompilation = PipelineFactory.getAsyncCompilation(device);
|
|
139
|
+
if (asyncCompilation) {
|
|
140
|
+
const pipelineInitialization =
|
|
141
|
+
this.pipelineFactory.createRenderPipelineAsync(renderPipelineProps);
|
|
142
|
+
this._pipelineInitialization = pipelineInitialization;
|
|
143
|
+
asyncCompilation.push(
|
|
144
|
+
pipelineInitialization.then(pipeline => {
|
|
145
|
+
this.pipeline = pipeline;
|
|
146
|
+
return pipeline;
|
|
147
|
+
})
|
|
148
|
+
);
|
|
149
|
+
} else {
|
|
150
|
+
this.pipeline = this.pipelineFactory.createRenderPipeline(renderPipelineProps);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Applies per-draw bindings/vertex state and records a direct draw. */
|
|
155
|
+
draw(renderPass: RenderPass, options: RenderKernelDrawOptions): boolean {
|
|
156
|
+
const {bindings = {}, vertexArray, ...drawOptions} = options;
|
|
157
|
+
this._setPipeline(renderPass, bindings, vertexArray);
|
|
158
|
+
return renderPass.draw(drawOptions);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Applies per-draw state and records a non-indexed indirect draw. */
|
|
162
|
+
drawIndirect(renderPass: RenderPass, options: RenderKernelIndirectDrawOptions): void {
|
|
163
|
+
const {bindings = {}, vertexArray, indirectBuffer, indirectOffset = 0} = options;
|
|
164
|
+
this._setPipeline(renderPass, bindings, vertexArray);
|
|
165
|
+
renderPass.drawIndirect(indirectBuffer, indirectOffset);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Applies per-draw state and records an indexed indirect draw. */
|
|
169
|
+
drawIndexedIndirect(renderPass: RenderPass, options: RenderKernelIndirectDrawOptions): void {
|
|
170
|
+
const {bindings = {}, vertexArray, indirectBuffer, indirectOffset = 0} = options;
|
|
171
|
+
this._setPipeline(renderPass, bindings, vertexArray);
|
|
172
|
+
renderPass.drawIndexedIndirect(indirectBuffer, indirectOffset);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
destroy(): void {
|
|
176
|
+
if (this._destroyed) return;
|
|
177
|
+
if (this.pipeline) this.pipelineFactory.release(this.pipeline);
|
|
178
|
+
this.shaderFactory.release(this.vertexShader);
|
|
179
|
+
if (this.fragmentShader) this.shaderFactory.release(this.fragmentShader);
|
|
180
|
+
this._destroyed = true;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private _setPipeline(
|
|
184
|
+
renderPass: RenderPass,
|
|
185
|
+
bindings: Record<string, Binding>,
|
|
186
|
+
vertexArray: VertexArray
|
|
187
|
+
): void {
|
|
188
|
+
renderPass.setPipeline(this.pipeline);
|
|
189
|
+
renderPass.setBindings(bindings);
|
|
190
|
+
renderPass.setVertexArray(vertexArray);
|
|
191
|
+
}
|
|
192
|
+
}
|