@luma.gl/webgpu 9.0.0-alpha.2
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/README.md +1 -0
- package/dist/adapter/helpers/accessor-to-format.d.ts +1 -0
- package/dist/adapter/helpers/accessor-to-format.d.ts.map +1 -0
- package/dist/adapter/helpers/accessor-to-format.js +2 -0
- package/dist/adapter/helpers/accessor-to-format.js.map +1 -0
- package/dist/adapter/helpers/convert-texture-format.d.ts +5 -0
- package/dist/adapter/helpers/convert-texture-format.d.ts.map +1 -0
- package/dist/adapter/helpers/convert-texture-format.js +8 -0
- package/dist/adapter/helpers/convert-texture-format.js.map +1 -0
- package/dist/adapter/helpers/generate-mipmaps.d.ts +10 -0
- package/dist/adapter/helpers/generate-mipmaps.d.ts.map +1 -0
- package/dist/adapter/helpers/generate-mipmaps.js +95 -0
- package/dist/adapter/helpers/generate-mipmaps.js.map +1 -0
- package/dist/adapter/helpers/get-bind-group.d.ts +13 -0
- package/dist/adapter/helpers/get-bind-group.d.ts.map +1 -0
- package/dist/adapter/helpers/get-bind-group.js +60 -0
- package/dist/adapter/helpers/get-bind-group.js.map +1 -0
- package/dist/adapter/helpers/get-vertex-buffer-layout.d.ts +12 -0
- package/dist/adapter/helpers/get-vertex-buffer-layout.d.ts.map +1 -0
- package/dist/adapter/helpers/get-vertex-buffer-layout.js +98 -0
- package/dist/adapter/helpers/get-vertex-buffer-layout.js.map +1 -0
- package/dist/adapter/helpers/webgpu-parameters.d.ts +9 -0
- package/dist/adapter/helpers/webgpu-parameters.d.ts.map +1 -0
- package/dist/adapter/helpers/webgpu-parameters.js +134 -0
- package/dist/adapter/helpers/webgpu-parameters.js.map +1 -0
- package/dist/adapter/resources/webgpu-buffer.d.ts +18 -0
- package/dist/adapter/resources/webgpu-buffer.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-buffer.js +84 -0
- package/dist/adapter/resources/webgpu-buffer.js.map +1 -0
- package/dist/adapter/resources/webgpu-command-encoder.d.ts +45 -0
- package/dist/adapter/resources/webgpu-command-encoder.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-command-encoder.js +66 -0
- package/dist/adapter/resources/webgpu-command-encoder.js.map +1 -0
- package/dist/adapter/resources/webgpu-compute-pass.d.ts +32 -0
- package/dist/adapter/resources/webgpu-compute-pass.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-compute-pass.js +56 -0
- package/dist/adapter/resources/webgpu-compute-pass.js.map +1 -0
- package/dist/adapter/resources/webgpu-compute-pipeline.d.ts +12 -0
- package/dist/adapter/resources/webgpu-compute-pipeline.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-compute-pipeline.js +27 -0
- package/dist/adapter/resources/webgpu-compute-pipeline.js.map +1 -0
- package/dist/adapter/resources/webgpu-external-texture.d.ts +18 -0
- package/dist/adapter/resources/webgpu-external-texture.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-external-texture.js +30 -0
- package/dist/adapter/resources/webgpu-external-texture.js.map +1 -0
- package/dist/adapter/resources/webgpu-framebuffer.d.ts +29 -0
- package/dist/adapter/resources/webgpu-framebuffer.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-framebuffer.js +111 -0
- package/dist/adapter/resources/webgpu-framebuffer.js.map +1 -0
- package/dist/adapter/resources/webgpu-query.d.ts +1 -0
- package/dist/adapter/resources/webgpu-query.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-query.js +2 -0
- package/dist/adapter/resources/webgpu-query.js.map +1 -0
- package/dist/adapter/resources/webgpu-render-pass.d.ts +34 -0
- package/dist/adapter/resources/webgpu-render-pass.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-render-pass.js +92 -0
- package/dist/adapter/resources/webgpu-render-pass.js.map +1 -0
- package/dist/adapter/resources/webgpu-render-pipeline.d.ts +41 -0
- package/dist/adapter/resources/webgpu-render-pipeline.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-render-pipeline.js +148 -0
- package/dist/adapter/resources/webgpu-render-pipeline.js.map +1 -0
- package/dist/adapter/resources/webgpu-sampler.d.ts +16 -0
- package/dist/adapter/resources/webgpu-sampler.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-sampler.js +19 -0
- package/dist/adapter/resources/webgpu-sampler.js.map +1 -0
- package/dist/adapter/resources/webgpu-shader.d.ts +21 -0
- package/dist/adapter/resources/webgpu-shader.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-shader.js +64 -0
- package/dist/adapter/resources/webgpu-shader.js.map +1 -0
- package/dist/adapter/resources/webgpu-texture.d.ts +39 -0
- package/dist/adapter/resources/webgpu-texture.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-texture.js +111 -0
- package/dist/adapter/resources/webgpu-texture.js.map +1 -0
- package/dist/adapter/webgpu-canvas-context.d.ts +32 -0
- package/dist/adapter/webgpu-canvas-context.d.ts.map +1 -0
- package/dist/adapter/webgpu-canvas-context.js +95 -0
- package/dist/adapter/webgpu-canvas-context.js.map +1 -0
- package/dist/adapter/webgpu-device.d.ts +67 -0
- package/dist/adapter/webgpu-device.d.ts.map +1 -0
- package/dist/adapter/webgpu-device.js +225 -0
- package/dist/adapter/webgpu-device.js.map +1 -0
- package/dist/adapter/webgpu-types.d.ts +1 -0
- package/dist/adapter/webgpu-types.d.ts.map +1 -0
- package/dist/adapter/webgpu-types.js +2 -0
- package/dist/adapter/webgpu-types.js.map +1 -0
- package/dist/bundle.d.ts +2 -0
- package/dist/bundle.d.ts.map +1 -0
- package/dist/bundle.js +5 -0
- package/dist/bundle.js.map +1 -0
- package/dist/glsl/glsllang.d.ts +3 -0
- package/dist/glsl/glsllang.d.ts.map +1 -0
- package/dist/glsl/glsllang.js +10 -0
- package/dist/glsl/glsllang.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/init.d.ts +2 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +4 -0
- package/dist/init.js.map +1 -0
- package/package.json +36 -0
- package/src/adapter/helpers/accessor-to-format.ts +101 -0
- package/src/adapter/helpers/convert-texture-format.ts +10 -0
- package/src/adapter/helpers/generate-mipmaps.ts +107 -0
- package/src/adapter/helpers/get-bind-group.ts +82 -0
- package/src/adapter/helpers/get-vertex-buffer-layout.ts +123 -0
- package/src/adapter/helpers/webgpu-parameters.ts +226 -0
- package/src/adapter/resources/webgpu-buffer.ts +96 -0
- package/src/adapter/resources/webgpu-command-encoder.ts +111 -0
- package/src/adapter/resources/webgpu-compute-pass.ts +74 -0
- package/src/adapter/resources/webgpu-compute-pipeline.ts +34 -0
- package/src/adapter/resources/webgpu-external-texture.ts +37 -0
- package/src/adapter/resources/webgpu-framebuffer.ts +120 -0
- package/src/adapter/resources/webgpu-query.ts +43 -0
- package/src/adapter/resources/webgpu-render-pass.ts +128 -0
- package/src/adapter/resources/webgpu-render-pipeline.ts +231 -0
- package/src/adapter/resources/webgpu-sampler.ts +25 -0
- package/src/adapter/resources/webgpu-shader.ts +72 -0
- package/src/adapter/resources/webgpu-texture.ts +243 -0
- package/src/adapter/webgpu-canvas-context.ts +102 -0
- package/src/adapter/webgpu-device.ts +282 -0
- package/src/adapter/webgpu-types.ts +0 -0
- package/src/bundle.ts +4 -0
- package/src/glsl/glsllang.ts +14 -0
- package/src/index.ts +13 -0
- package/src/init.ts +4 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import {Parameters} from '@luma.gl/api';
|
|
2
|
+
|
|
3
|
+
function addDepthStencil(descriptor: GPURenderPipelineDescriptor): void {
|
|
4
|
+
descriptor.depthStencil = descriptor.depthStencil || {
|
|
5
|
+
// required, set something
|
|
6
|
+
format: 'depth24plus',
|
|
7
|
+
stencilFront: {},
|
|
8
|
+
stencilBack: {}
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Supports for luma.gl's flat parameter space
|
|
14
|
+
* Populates the corresponding sub-objects in a GPURenderPipelineDescriptor
|
|
15
|
+
*/
|
|
16
|
+
// @ts-expect-error
|
|
17
|
+
export const PARAMETER_TABLE: Record<keyof Parameters, Function> = {
|
|
18
|
+
// RASTERIZATION PARAMETERS
|
|
19
|
+
|
|
20
|
+
cullMode: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
21
|
+
descriptor.primitive.cullMode = value;
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
frontFace: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
25
|
+
descriptor.primitive.frontFace = value;
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
// DEPTH
|
|
29
|
+
|
|
30
|
+
depthWriteEnabled: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
31
|
+
addDepthStencil(descriptor);
|
|
32
|
+
descriptor.depthStencil.depthWriteEnabled = value;
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
depthCompare: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
36
|
+
addDepthStencil(descriptor);
|
|
37
|
+
descriptor.depthStencil.depthCompare = value;
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
depthFormat: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
41
|
+
addDepthStencil(descriptor);
|
|
42
|
+
descriptor.depthStencil.format = value;
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
depthBias: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
46
|
+
addDepthStencil(descriptor);
|
|
47
|
+
descriptor.depthStencil.depthBias = value;
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
depthBiasSlopeScale: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
51
|
+
addDepthStencil(descriptor);
|
|
52
|
+
descriptor.depthStencil.depthBiasSlopeScale = value;
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
depthBiasClamp: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
56
|
+
addDepthStencil(descriptor);
|
|
57
|
+
descriptor.depthStencil.depthBiasClamp = value;
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
// STENCIL
|
|
61
|
+
|
|
62
|
+
stencilReadMask: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
63
|
+
addDepthStencil(descriptor);
|
|
64
|
+
descriptor.depthStencil.stencilReadMask = value;
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
stencilWriteMask: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
68
|
+
addDepthStencil(descriptor);
|
|
69
|
+
descriptor.depthStencil.stencilWriteMask = value;
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
stencilCompare: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
73
|
+
addDepthStencil(descriptor);
|
|
74
|
+
descriptor.depthStencil.stencilFront.compare = value;
|
|
75
|
+
descriptor.depthStencil.stencilBack.compare = value;
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
stencilPassOperation: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
79
|
+
addDepthStencil(descriptor);
|
|
80
|
+
descriptor.depthStencil.stencilFront.passOp = value;
|
|
81
|
+
descriptor.depthStencil.stencilBack.passOp = value;
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
stencilFailOperation: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
85
|
+
addDepthStencil(descriptor);
|
|
86
|
+
descriptor.depthStencil.stencilFront.failOp = value;
|
|
87
|
+
descriptor.depthStencil.stencilBack.failOp = value;
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
stencilDepthFailOperation: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
91
|
+
addDepthStencil(descriptor);
|
|
92
|
+
descriptor.depthStencil.stencilFront.depthFailOp = value;
|
|
93
|
+
descriptor.depthStencil.stencilBack.depthFailOp = value;
|
|
94
|
+
},
|
|
95
|
+
|
|
96
|
+
// MULTISAMPLE
|
|
97
|
+
|
|
98
|
+
sampleCount: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
99
|
+
descriptor.multisample = descriptor.multisample || {};
|
|
100
|
+
descriptor.multisample.count = value;
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
sampleMask: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
104
|
+
descriptor.multisample = descriptor.multisample || {};
|
|
105
|
+
descriptor.multisample.mask = value;
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
sampleAlphaToCoverageEnabled: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
109
|
+
descriptor.multisample = descriptor.multisample || {};
|
|
110
|
+
descriptor.multisample.alphaToCoverageEnabled = value;
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
// COLOR
|
|
114
|
+
|
|
115
|
+
colorMask: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
116
|
+
addColorState(descriptor);
|
|
117
|
+
const targets = descriptor.fragment.targets as GPUColorTargetState[];
|
|
118
|
+
targets[0].writeMask = value;
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
blendColorOperation: (parameter: keyof Parameters, value: any, descriptor: GPURenderPipelineDescriptor) => {
|
|
122
|
+
addColorState(descriptor);
|
|
123
|
+
const targets = descriptor.fragment.targets as GPUColorTargetState[];
|
|
124
|
+
// @ts-expect-error
|
|
125
|
+
targets[0].blend = targets[0].blend || {};
|
|
126
|
+
targets[0].blend.color = targets[0].blend.color || {};
|
|
127
|
+
targets[0].blend.color.operation = value;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/*
|
|
131
|
+
blendColorSrcTarget: (parameter, value, descriptor: GPURenderPipelineDescriptor) => {
|
|
132
|
+
addColorState(descriptor);
|
|
133
|
+
targets[0].blend = targets[0].blend || {};
|
|
134
|
+
targets[0].blend.color = targets[0].blend.color || {};
|
|
135
|
+
targets[0].blend.color.srcTarget = value;
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
blendColorDstTarget: (parameter, value, descriptor: GPURenderPipelineDescriptor) => {
|
|
139
|
+
addColorState(descriptor);
|
|
140
|
+
targets[0].blend = targets[0].blend || {};
|
|
141
|
+
targets[0].blend.color = targets[0].blend.color || {};
|
|
142
|
+
targets[0].blend.color.dstTarget = value;
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
blendAlphaOperation: (parameter, value, descriptor: GPURenderPipelineDescriptor) => {
|
|
146
|
+
addColorState(descriptor);
|
|
147
|
+
targets[0].blend = targets[0].blend || {};
|
|
148
|
+
targets[0].blend.alpha = targets[0].blend.alpha || {};
|
|
149
|
+
targets[0].blend.alpha.operation = value;
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
blendAlphaSrcTarget: (parameter, value, descriptor: GPURenderPipelineDescriptor) => {
|
|
153
|
+
addColorState(descriptor);
|
|
154
|
+
targets[0].blend = targets[0].blend || {};
|
|
155
|
+
targets[0].blend.alpha = targets[0].blend.alpha || {};
|
|
156
|
+
targets[0].blend.alpha.srcTarget = value;
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
blendAlphaDstTarget: (parameter, value, descriptor: GPURenderPipelineDescriptor) => {
|
|
160
|
+
addColorState(descriptor);
|
|
161
|
+
targets[0].blend = targets[0].blend || {};
|
|
162
|
+
targets[0].blend.alpha = targets[0].blend.alpha || {};
|
|
163
|
+
targets[0].blend.alpha.dstTarget = value;
|
|
164
|
+
},
|
|
165
|
+
*/
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const DEFAULT_PIPELINE_DESCRIPTOR: GPURenderPipelineDescriptor = {
|
|
169
|
+
// depthStencil: {
|
|
170
|
+
// stencilFront: {},
|
|
171
|
+
// stencilBack: {},
|
|
172
|
+
// // depthWriteEnabled: true,
|
|
173
|
+
// // depthCompare: 'less',
|
|
174
|
+
// // format: 'depth24plus-stencil8',
|
|
175
|
+
// },
|
|
176
|
+
|
|
177
|
+
primitive: {
|
|
178
|
+
cullMode: 'back',
|
|
179
|
+
topology: 'triangle-list'
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
vertex: {
|
|
183
|
+
module: undefined,
|
|
184
|
+
entryPoint: 'main'
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
fragment: {
|
|
188
|
+
module: undefined,
|
|
189
|
+
entryPoint: 'main',
|
|
190
|
+
targets: [
|
|
191
|
+
// { format: props.color0Format || 'bgra8unorm' }
|
|
192
|
+
]
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
export function applyParametersToRenderPipelineDescriptor(
|
|
197
|
+
pipelineDescriptor: GPURenderPipelineDescriptor,
|
|
198
|
+
parameters: Parameters = {}
|
|
199
|
+
): void {
|
|
200
|
+
// Apply defaults
|
|
201
|
+
Object.assign(pipelineDescriptor, {...DEFAULT_PIPELINE_DESCRIPTOR, ...pipelineDescriptor});
|
|
202
|
+
setParameters(pipelineDescriptor, parameters);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Apply any supplied parameters
|
|
206
|
+
function setParameters(
|
|
207
|
+
pipelineDescriptor: GPURenderPipelineDescriptor,
|
|
208
|
+
parameters: Parameters
|
|
209
|
+
): void {
|
|
210
|
+
for (const [key, value] of Object.entries(parameters)) {
|
|
211
|
+
const setterFunction = PARAMETER_TABLE[key as keyof Parameters];
|
|
212
|
+
if (!setterFunction) {
|
|
213
|
+
throw new Error(`Illegal parameter ${key}`);
|
|
214
|
+
}
|
|
215
|
+
setterFunction(key, value, pipelineDescriptor);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function addColorState(descriptor: GPURenderPipelineDescriptor): void {
|
|
220
|
+
descriptor.fragment.targets = descriptor.fragment.targets || [];
|
|
221
|
+
// @ts-expect-error
|
|
222
|
+
if (descriptor.fragment.targets.length === 0) {
|
|
223
|
+
// @ts-expect-error
|
|
224
|
+
descriptor.fragment.targets.push({});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// WEBGPU Buffer implementation
|
|
2
|
+
import {Buffer, BufferProps, assert} from '@luma.gl/api';
|
|
3
|
+
import type WebGPUDevice from '../webgpu-device';
|
|
4
|
+
|
|
5
|
+
function getByteLength(props: BufferProps): number {
|
|
6
|
+
return props.byteLength || props.data?.byteLength || 0;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default class WebGPUBuffer extends Buffer {
|
|
10
|
+
readonly device: WebGPUDevice;
|
|
11
|
+
readonly handle: GPUBuffer;
|
|
12
|
+
readonly byteLength: number;
|
|
13
|
+
|
|
14
|
+
constructor(device: WebGPUDevice, props: BufferProps) {
|
|
15
|
+
super(device, props);
|
|
16
|
+
this.device = device;
|
|
17
|
+
|
|
18
|
+
this.byteLength = getByteLength(props);
|
|
19
|
+
const mapBuffer = Boolean(props.data);
|
|
20
|
+
|
|
21
|
+
this.handle = this.props.handle || this.device.handle.createBuffer({
|
|
22
|
+
size: this.byteLength,
|
|
23
|
+
// usage defaults to vertex
|
|
24
|
+
usage: this.props.usage || (GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST),
|
|
25
|
+
mappedAtCreation: this.props.mappedAtCreation || mapBuffer,
|
|
26
|
+
label: this.props.id
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
if (props.data) {
|
|
30
|
+
this._writeMapped(props.data);
|
|
31
|
+
// this.handle.writeAsync({data: props.data, map: false, unmap: false});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (mapBuffer && !props.mappedAtCreation) {
|
|
35
|
+
this.handle.unmap();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
protected createHandle(mapBuffer: boolean): GPUBuffer {
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
destroy(): void {
|
|
44
|
+
this.handle.destroy();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// WebGPU provides multiple ways to write a buffer...
|
|
48
|
+
write(data: ArrayBufferView, byteOffset = 0) {
|
|
49
|
+
this.device.handle.queue.writeBuffer(
|
|
50
|
+
this.handle,
|
|
51
|
+
byteOffset,
|
|
52
|
+
data.buffer,
|
|
53
|
+
data.byteOffset,
|
|
54
|
+
data.byteLength
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async readAsync(byteOffset: number = 0, byteLength: number = this.byteLength): Promise<ArrayBuffer> {
|
|
59
|
+
// We need MAP_READ flag, but only COPY_DST buffers can have MAP_READ flag, so we need to create a temp buffer
|
|
60
|
+
const tempBuffer = new WebGPUBuffer(this.device, {usage: Buffer.MAP_READ | Buffer.COPY_DST, byteLength});
|
|
61
|
+
|
|
62
|
+
// Now do a GPU-side copy into the temp buffer we can actually read.
|
|
63
|
+
// TODO - we are spinning up an independent command queue here, what does this mean
|
|
64
|
+
const commandEncoder = this.device.handle.createCommandEncoder();
|
|
65
|
+
commandEncoder.copyBufferToBuffer(this.handle, byteOffset, tempBuffer.handle, 0, byteLength);
|
|
66
|
+
this.device.handle.queue.submit([commandEncoder.finish()]);
|
|
67
|
+
|
|
68
|
+
// Map the temp buffer and read the data.
|
|
69
|
+
await tempBuffer.handle.mapAsync(GPUMapMode.READ, byteOffset, byteLength);
|
|
70
|
+
const arrayBuffer = tempBuffer.handle.getMappedRange().slice(0);
|
|
71
|
+
tempBuffer.handle.unmap();
|
|
72
|
+
tempBuffer.destroy();
|
|
73
|
+
|
|
74
|
+
return arrayBuffer;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
_writeMapped<TypedArray>(typedArray: TypedArray): void {
|
|
78
|
+
const arrayBuffer = this.handle.getMappedRange();
|
|
79
|
+
// @ts-expect-error
|
|
80
|
+
new typedArray.constructor(arrayBuffer).set(typedArray);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// WEBGPU API
|
|
84
|
+
|
|
85
|
+
mapAsync(mode: number, offset: number = 0, size?: number): Promise<void> {
|
|
86
|
+
return this.handle.mapAsync(mode, offset, size);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
getMappedRange(offset: number = 0, size?: number): ArrayBuffer {
|
|
90
|
+
return this.handle.getMappedRange(offset, size);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
unmap(): void {
|
|
94
|
+
this.handle.unmap();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import {CommandEncoder, CommandEncoderProps, RenderPipeline, Buffer, Texture, cast} from '@luma.gl/api';
|
|
2
|
+
import WebGPUDevice from '../webgpu-device';
|
|
3
|
+
import WEBGPUBuffer from './webgpu-buffer';
|
|
4
|
+
import WebGPUTexture from './webgpu-texture';
|
|
5
|
+
|
|
6
|
+
export default class WebGPUCommandEncoder extends CommandEncoder {
|
|
7
|
+
readonly device: WebGPUDevice;
|
|
8
|
+
readonly handle: GPUCommandEncoder;
|
|
9
|
+
|
|
10
|
+
constructor(device: WebGPUDevice, props: CommandEncoderProps) {
|
|
11
|
+
super(props);
|
|
12
|
+
this.device = device;
|
|
13
|
+
this.handle = this.handle || this.createHandle();
|
|
14
|
+
this.handle.label = this.props.id;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
protected createHandle(): GPUCommandEncoder {
|
|
18
|
+
return this.device.handle.createCommandEncoder({
|
|
19
|
+
measureExecutionTime: this.props.measureExecutionTime
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
destroy() {}
|
|
24
|
+
|
|
25
|
+
finish(options?: {id?: string}): GPUCommandBuffer {
|
|
26
|
+
return this.finish(options);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// beginRenderPass(GPURenderPassDescriptor descriptor): GPURenderPassEncoder;
|
|
30
|
+
// beginComputePass(optional GPUComputePassDescriptor descriptor = {}): GPUComputePassEncoder;
|
|
31
|
+
|
|
32
|
+
copyBufferToBuffer(options: {
|
|
33
|
+
source: Buffer,
|
|
34
|
+
sourceOffset?: number,
|
|
35
|
+
destination: Buffer,
|
|
36
|
+
destinationOffset?: number,
|
|
37
|
+
size?: number
|
|
38
|
+
}): void {
|
|
39
|
+
this.handle.copyBufferToBuffer(
|
|
40
|
+
cast<WEBGPUBuffer>(options.source).handle,
|
|
41
|
+
options.sourceOffset ?? 0,
|
|
42
|
+
cast<WEBGPUBuffer>(options.destination).handle,
|
|
43
|
+
options.destinationOffset ?? 0,
|
|
44
|
+
options.size ?? 0
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
copyBufferToTexture(options: {
|
|
49
|
+
source: Buffer,
|
|
50
|
+
offset?: number,
|
|
51
|
+
bytesPerRow: number,
|
|
52
|
+
rowsPerImage: number,
|
|
53
|
+
|
|
54
|
+
destination: Texture,
|
|
55
|
+
mipLevel?: number;
|
|
56
|
+
aspect?: 'all' | 'stencil-only' | 'depth-only',
|
|
57
|
+
|
|
58
|
+
origin?: number[] | [number, number, number],
|
|
59
|
+
extent?: number[] | [number, number, number]
|
|
60
|
+
}): void {
|
|
61
|
+
this.handle.copyBufferToTexture(
|
|
62
|
+
{
|
|
63
|
+
buffer: cast<WEBGPUBuffer>(options.source).handle,
|
|
64
|
+
offset: options.offset ?? 0,
|
|
65
|
+
bytesPerRow: options.bytesPerRow,
|
|
66
|
+
rowsPerImage: options.rowsPerImage,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
texture: cast<WebGPUTexture>(options.destination).handle,
|
|
70
|
+
mipLevel: options.mipLevel ?? 0,
|
|
71
|
+
origin: options.origin ?? {},
|
|
72
|
+
// aspect: options.aspect
|
|
73
|
+
},
|
|
74
|
+
options.extent // default depth?
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
copyTextureToBuffer(options: {
|
|
79
|
+
source: GPUImageCopyTexture,
|
|
80
|
+
destination: GPUImageCopyBuffer,
|
|
81
|
+
copySize: GPUExtent3D
|
|
82
|
+
}): void {}
|
|
83
|
+
|
|
84
|
+
copyTextureToTexture(options: {
|
|
85
|
+
source: GPUImageCopyTexture ,
|
|
86
|
+
destination: GPUImageCopyTexture,
|
|
87
|
+
copySize: GPUExtent3D
|
|
88
|
+
}): void {}
|
|
89
|
+
|
|
90
|
+
pushDebugGroup(groupLabel: string): void {
|
|
91
|
+
this.handle.pushDebugGroup(groupLabel);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
popDebugGroup(): void {
|
|
95
|
+
this.handle.popDebugGroup();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
insertDebugMarker(markerLabel: string): void {
|
|
99
|
+
this.handle.insertDebugMarker(markerLabel);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// writeTimestamp(querySet: Query, queryIndex: number): void {}
|
|
103
|
+
|
|
104
|
+
// resolveQuerySet(options: {
|
|
105
|
+
// querySet: GPUQuerySet,
|
|
106
|
+
// firstQuery: number,
|
|
107
|
+
// queryCount: number,
|
|
108
|
+
// destination: Buffer,
|
|
109
|
+
// destinationOffset?: number;
|
|
110
|
+
// }): void;
|
|
111
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {ComputePass, ComputePassProps, ComputePipeline, Buffer, Binding, cast} from '@luma.gl/api';
|
|
2
|
+
import WebGPUDevice from '../webgpu-device';
|
|
3
|
+
import WebGPUBuffer from './webgpu-buffer';
|
|
4
|
+
// import WebGPUCommandEncoder from './webgpu-command-encoder';
|
|
5
|
+
import WebGPUComputePipeline from './webgpu-compute-pipeline';
|
|
6
|
+
|
|
7
|
+
export default class WebGPUComputePass extends ComputePass {
|
|
8
|
+
readonly device: WebGPUDevice;
|
|
9
|
+
readonly handle: GPUComputePassEncoder;
|
|
10
|
+
_bindGroupLayout: GPUBindGroupLayout;
|
|
11
|
+
|
|
12
|
+
constructor(device: WebGPUDevice, props: ComputePassProps) {
|
|
13
|
+
super(device, props);
|
|
14
|
+
this.device = device;
|
|
15
|
+
|
|
16
|
+
this.handle = this.props.handle || device.commandEncoder.beginComputePass({
|
|
17
|
+
label: this.props.id,
|
|
18
|
+
// timestampWrites?: GPUComputePassTimestampWrites;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** @note no WebGPU destroy method, just gc */
|
|
23
|
+
destroy() {}
|
|
24
|
+
|
|
25
|
+
endPass(): void {
|
|
26
|
+
this.handle.endPass();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
setPipeline(pipeline: ComputePipeline): void {
|
|
30
|
+
const wgpuPipeline = cast<WebGPUComputePipeline>(pipeline);
|
|
31
|
+
this.handle.setPipeline(wgpuPipeline.handle);
|
|
32
|
+
this._bindGroupLayout = wgpuPipeline._getBindGroupLayout();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Sets an array of bindings (uniform buffers, samplers, textures, ...) */
|
|
36
|
+
setBindings(bindings: Binding[]): void {
|
|
37
|
+
throw new Error('fix me');
|
|
38
|
+
// const bindGroup = getBindGroup(this.device.handle, this._bindGroupLayout, this.props.bindings);
|
|
39
|
+
// this.handle.setBindGroup(0, bindGroup);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Dispatch work to be performed with the current ComputePipeline.
|
|
44
|
+
* @param x X dimension of the grid of workgroups to dispatch.
|
|
45
|
+
* @param y Y dimension of the grid of workgroups to dispatch.
|
|
46
|
+
* @param z Z dimension of the grid of workgroups to dispatch.
|
|
47
|
+
*/
|
|
48
|
+
dispatch(x: number, y?: number, z?: number): void {
|
|
49
|
+
this.handle.dispatch(x, y, z);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Dispatch work to be performed with the current ComputePipeline.
|
|
54
|
+
* @param indirectBuffer buffer must be a tightly packed block of three 32-bit unsigned integer values (12 bytes total), given in the same order as the arguments for dispatch()
|
|
55
|
+
* @param indirectOffset
|
|
56
|
+
*/
|
|
57
|
+
dispatchIndirect(indirectBuffer: Buffer, indirectOffset: number = 0): void {
|
|
58
|
+
this.handle.dispatchIndirect(cast<WebGPUBuffer>(indirectBuffer).handle, indirectOffset);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
pushDebugGroup(groupLabel: string): void {
|
|
62
|
+
this.handle.pushDebugGroup(groupLabel);
|
|
63
|
+
}
|
|
64
|
+
popDebugGroup(): void {
|
|
65
|
+
this.handle.popDebugGroup();
|
|
66
|
+
}
|
|
67
|
+
insertDebugMarker(markerLabel: string): void {
|
|
68
|
+
this.handle.insertDebugMarker(markerLabel);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// writeTimestamp(querySet: GPUQuerySet, queryIndex: number): void;
|
|
72
|
+
// beginPipelineStatisticsQuery(querySet: GPUQuerySet, queryIndex: number): void;
|
|
73
|
+
// endPipelineStatisticsQuery(querySet: GPUQuerySet, queryIndex: number): void;
|
|
74
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// prettier-ignore
|
|
2
|
+
import {ComputePipeline, ComputePipelineProps, cast} from '@luma.gl/api';
|
|
3
|
+
|
|
4
|
+
import WebGPUDevice from '../webgpu-device';
|
|
5
|
+
import WebGPUShader from './webgpu-shader';
|
|
6
|
+
|
|
7
|
+
// COMPUTE PIPELINE
|
|
8
|
+
|
|
9
|
+
/** Creates a new compute pipeline when parameters change */
|
|
10
|
+
export default class WebGPUComputePipeline extends ComputePipeline {
|
|
11
|
+
device: WebGPUDevice;
|
|
12
|
+
handle: GPUComputePipeline;
|
|
13
|
+
|
|
14
|
+
constructor(device: WebGPUDevice, props: ComputePipelineProps) {
|
|
15
|
+
super(device, props);
|
|
16
|
+
this.device = device;
|
|
17
|
+
|
|
18
|
+
const module = cast<WebGPUShader>(this.props.cs).handle;
|
|
19
|
+
this.handle = this.props.handle || this.device.handle.createComputePipeline({
|
|
20
|
+
label: this.props.id,
|
|
21
|
+
compute: {
|
|
22
|
+
module,
|
|
23
|
+
entryPoint: this.props.csEntryPoint,
|
|
24
|
+
// constants: this.props.csConstants
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** For internal use in render passes */
|
|
30
|
+
_getBindGroupLayout() {
|
|
31
|
+
// TODO: Cache?
|
|
32
|
+
return this.handle.getBindGroupLayout(0);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// luma.gl, MIT license
|
|
2
|
+
import {ExternalTexture, ExternalTextureProps, Sampler, SamplerProps} from '@luma.gl/api';
|
|
3
|
+
import type WebGPUDevice from '../webgpu-device';
|
|
4
|
+
import WebGPUSampler from './webgpu-sampler';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Cheap, temporary texture view for videos
|
|
8
|
+
* Only valid within same callback, destroyed automatically as a microtask.
|
|
9
|
+
*/
|
|
10
|
+
export default class WebGPUExternalTexture extends ExternalTexture {
|
|
11
|
+
readonly device: WebGPUDevice;
|
|
12
|
+
readonly handle: GPUExternalTexture;
|
|
13
|
+
sampler: WebGPUSampler;
|
|
14
|
+
|
|
15
|
+
constructor(device: WebGPUDevice, props: ExternalTextureProps) {
|
|
16
|
+
super(device, props);
|
|
17
|
+
this.device = device;
|
|
18
|
+
this.handle = this.props.handle || this.device.handle.importExternalTexture({
|
|
19
|
+
source: props.source,
|
|
20
|
+
colorSpace: props.colorSpace
|
|
21
|
+
});
|
|
22
|
+
this.sampler = null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
destroy(): void {
|
|
26
|
+
// External textures are destroyed automatically,
|
|
27
|
+
// as a microtask, instead of manually or upon garbage collection like other resources.
|
|
28
|
+
// this.handle.destroy();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Set default sampler */
|
|
32
|
+
setSampler(sampler: Sampler | SamplerProps): this {
|
|
33
|
+
// We can accept a sampler instance or set of props;
|
|
34
|
+
this.sampler = sampler instanceof WebGPUSampler ? sampler : new WebGPUSampler(this.device, sampler);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type {FramebufferProps, ColorTextureFormat} from '@luma.gl/api';
|
|
2
|
+
import {Framebuffer, Texture} from '@luma.gl/api';
|
|
3
|
+
import WebGPUDevice from '../webgpu-device';
|
|
4
|
+
// import WebGPUCanvasContext from '../webgpu-canvas-context';
|
|
5
|
+
import WEBGPUTexture from './webgpu-texture';
|
|
6
|
+
import WebGPUTexture from './webgpu-texture';
|
|
7
|
+
|
|
8
|
+
// const DEFAULT_DEPTH_STENCIL_FORMAT: DepthStencilTextureFormat = 'depth24plus';
|
|
9
|
+
|
|
10
|
+
const MAX_COLOR_ATTACHMENTS = 8;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Create new textures with correct size for all attachments.
|
|
14
|
+
* @note resize() destroys existing textures (if size has changed).
|
|
15
|
+
*/
|
|
16
|
+
export default class WebGPUFramebuffer extends Framebuffer {
|
|
17
|
+
readonly device: WebGPUDevice;
|
|
18
|
+
|
|
19
|
+
colorAttachments: WebGPUTexture[] = [];
|
|
20
|
+
depthStencilAttachment: WebGPUTexture;
|
|
21
|
+
|
|
22
|
+
/** Partial render pass descriptor. Used by WebGPURenderPass */
|
|
23
|
+
renderPassDescriptor: {
|
|
24
|
+
colorAttachments: GPURenderPassColorAttachment[];
|
|
25
|
+
depthStencilAttachment?: GPURenderPassDepthStencilAttachment;
|
|
26
|
+
} = {
|
|
27
|
+
colorAttachments: []
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
constructor(device: WebGPUDevice, props: FramebufferProps) {
|
|
31
|
+
super(device, props);
|
|
32
|
+
this.device = device;
|
|
33
|
+
|
|
34
|
+
if (props.depthStencilAttachment) {
|
|
35
|
+
this.depthStencilAttachment = this.createDepthStencilTexture(props);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (props.colorAttachments) {
|
|
39
|
+
this.colorAttachments = props.colorAttachments.map(colorAttachment => this.createColorTexture(this.props, colorAttachment));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (this.depthStencilAttachment) {
|
|
43
|
+
this.renderPassDescriptor.depthStencilAttachment = {
|
|
44
|
+
view: this.depthStencilAttachment.handle.createView(),
|
|
45
|
+
// Add default clear values
|
|
46
|
+
depthLoadValue: 1.0,
|
|
47
|
+
depthStoreOp: 'store',
|
|
48
|
+
stencilLoadValue: 0,
|
|
49
|
+
stencilStoreOp: 'store',
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (this.colorAttachments.length > 0) {
|
|
54
|
+
this.renderPassDescriptor.colorAttachments = this.colorAttachments.map(colorAttachment => ({
|
|
55
|
+
view: colorAttachment.handle.createView(),
|
|
56
|
+
loadValue: [0.0, 0.0, 0.0, 0.0],
|
|
57
|
+
storeOp: 'store'
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Create depth stencil texture */
|
|
63
|
+
private createDepthStencilTexture(props: FramebufferProps): WEBGPUTexture {
|
|
64
|
+
if (props.depthStencilAttachment instanceof WEBGPUTexture) {
|
|
65
|
+
return props.depthStencilAttachment;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (typeof props.depthStencilAttachment === 'string') {
|
|
69
|
+
return this.device._createTexture({
|
|
70
|
+
id: 'depth-stencil-attachment',
|
|
71
|
+
format: props.depthStencilAttachment,
|
|
72
|
+
width: props.width,
|
|
73
|
+
height: props.height,
|
|
74
|
+
usage: Texture.RENDER_ATTACHMENT
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
throw new Error('type');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private createColorTexture(props: FramebufferProps, texture: Texture | ColorTextureFormat): WEBGPUTexture {
|
|
82
|
+
if (texture instanceof WEBGPUTexture) {
|
|
83
|
+
return texture;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (typeof texture === 'string') {
|
|
87
|
+
return this.device._createTexture({
|
|
88
|
+
id: 'color-attachment',
|
|
89
|
+
format: texture,
|
|
90
|
+
width: props.width,
|
|
91
|
+
height: props.height,
|
|
92
|
+
usage: Texture.RENDER_ATTACHMENT
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
throw new Error('type');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Create new textures with correct size for all attachments.
|
|
101
|
+
* @note destroys existing textures.
|
|
102
|
+
*/
|
|
103
|
+
protected _resizeAttachments(width: number, height: number): void {
|
|
104
|
+
for (let i = 0; i < this.colorAttachments.length; ++i) {
|
|
105
|
+
if (this.colorAttachments[i]) {
|
|
106
|
+
const resizedTexture = this.device._createTexture({...this.colorAttachments[i].props, width, height})
|
|
107
|
+
this.colorAttachments[i].destroy();
|
|
108
|
+
this.colorAttachments[i] = resizedTexture;
|
|
109
|
+
this.renderPassDescriptor.colorAttachments[i].view = resizedTexture.handle.createView();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (this.depthStencilAttachment) {
|
|
114
|
+
const resizedTexture = this.device._createTexture({...this.depthStencilAttachment.props, width, height})
|
|
115
|
+
this.depthStencilAttachment.destroy();
|
|
116
|
+
this.depthStencilAttachment = resizedTexture;
|
|
117
|
+
this.renderPassDescriptor.depthStencilAttachment.view = resizedTexture.handle.createView();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|