-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwebgpu-renderer.js
More file actions
322 lines (282 loc) · 12.4 KB
/
Copy pathwebgpu-renderer.js
File metadata and controls
322 lines (282 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright (C) <2025> Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
import { WebGPUBlur } from './webgpu-blur.js';
class WebGPURenderer {
constructor(device, segmenter, blurrer, { zeroCopy, directOutput }, {useWebNN, zeroCopyTensor}) {
console.log("createWebGPUBlurRenderer", { zeroCopy, directOutput });
this.device = device;
this.segmenter = segmenter;
this.blurrer = blurrer;
this.zeroCopy = zeroCopy;
this.directOutput = directOutput;
this.useWebNN = useWebNN;
this.zeroCopyTensor = zeroCopyTensor;
// Always use full resolution for processing, regardless of display size
this.webgpuCanvas = new OffscreenCanvas(1280, 720);
this.context = this.webgpuCanvas.getContext('webgpu');
if (!this.context) {
throw new Error('WebGPU context not available');
}
this.context.configure({
device: this.device,
format: navigator.gpu.getPreferredCanvasFormat(),
alphaMode: 'premultiplied',
usage: GPUTextureUsage.RENDER_ATTACHMENT | (directOutput ? GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_DST : 0),
});
this.segmentationWidth = 256;
this.segmentationHeight = 144;
this.downscaledImageData = new ImageData(this.segmentationWidth, this.segmentationHeight);
let downscaleShaderUrl;
if (this.useWebNN && this.zeroCopyTensor) {
downscaleShaderUrl = 'blur4/shaders/downscale-and-convert-to-rgb16float.wgsl';
} else {
downscaleShaderUrl = 'blur4/shaders/downscale-and-convert-to-rgba8unorm.wgsl';
}
this.downscaleModule = fetch(downscaleShaderUrl).then(res => res.text())
.then(code => this.device.createShaderModule({
code: code.replace(/\${(\w+)}/g, (...groups) => ({
inputTextureType: zeroCopy ? "texture_external" : "texture_2d<f32>",
}[groups[1]]))
}));
this.downscalePipeline = this.downscaleModule.then(module => this.device.createComputePipeline({
layout: 'auto',
compute: { module: module, entryPoint: 'main' },
}));
this.downscaleSampler = this.device.createSampler({
magFilter: 'linear',
minFilter: 'linear',
});
// Create a simple render pipeline to copy the compute shader's output (RGBA)
// to the canvas, which might have a different format (e.g., BGRA).
this.outputRendererVertexShader = fetch('blur4/shaders/render.vertex.wgsl').then(res => res.text())
.then(code => this.device.createShaderModule({ code }));
this.outputRendererFragmentShader = null;
this.lastDim = null;
this.renderSampler = this.device.createSampler({ magFilter: 'linear', minFilter: 'linear' });
this.resourceCache = {};
}
async getOrCreateResource(key, createFn) {
if (!this.resourceCache[key]) {
console.log("Creating new resource", key);
this.resourceCache[key] = await createFn();
}
return this.resourceCache[key];
}
getOrCreateTexture(key, { size, usage, format = 'rgba8unorm' }) {
const [width, height] = size;
const cacheKey = `${key}_${width}x${height}_${format}_${usage}`;
let texture = this.resourceCache[cacheKey];
if (!texture || texture.width !== width || texture.height !== height) {
console.log("Creating new texture", cacheKey, "with format", format, "and usage", usage);
if (texture) {
console.log("Destroying old texture", cacheKey);
texture.destroy();
}
texture = this.device.createTexture({ size, format, usage });
this.resourceCache[cacheKey] = texture;
}
return texture;
}
async getOutputRendererFragmentShader(device, width, height) {
if (!this.outputRendererFragmentShader || this.lastDim !== [width, height]) {
this.outputRendererFragmentShader = await this.device.createShaderModule({
code: await fetch('blur4/shaders/render.fragment.wgsl').then(res => res.text())
.then(code => code.replace(/\${(\w+)}/g, (...groups) => ({ width, height }[groups[1]]))),
});
this.lastDim = [width, height];
}
return this.outputRendererFragmentShader;
}
async segment(sourceTexture) {
const useInterop = this.useWebNN && this.zeroCopyTensor;
let destTexture;
if (useInterop) {
destTexture = { buffer: await this.segmenter.getInputBuffer(this.device) };
} else {
destTexture = this.getOrCreateTexture('downscaleDest', {
size: [this.segmentationWidth, this.segmentationHeight, 1],
usage: GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC | GPUTextureUsage.TEXTURE_BINDING,
format: 'rgba8unorm',
});
}
const downscaleBindGroup = this.device.createBindGroup({
layout: (await this.downscalePipeline).getBindGroupLayout(0),
entries: [
{ binding: 0, resource: this.zeroCopy ? sourceTexture : sourceTexture.createView() },
{ binding: 1, resource: this.downscaleSampler },
{ binding: 2, resource: useInterop ? destTexture : destTexture.createView() },
],
});
const commandEncoder = this.device.createCommandEncoder();
const computePass = commandEncoder.beginComputePass();
computePass.setPipeline(await this.downscalePipeline);
computePass.setBindGroup(0, downscaleBindGroup);
computePass.dispatchWorkgroups(Math.ceil(this.segmentationWidth / 8), Math.ceil(this.segmentationHeight / 8));
computePass.end();
if (useInterop) {
this.device.queue.submit([commandEncoder.finish()]);
destTexture.buffer.destroy();
await this.segmenter.segmentGPUBuffer();
const outputBuffer = await this.segmenter.getOutputBuffer();
if (!this.maskTexture) {
this.maskTexture = this.device.createTexture({
size: [this.segmentationWidth, this.segmentationHeight, 1],
format: 'r16float',
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
});
}
const copyCommandEncoder = this.device.createCommandEncoder();
copyCommandEncoder.copyBufferToTexture(
{ buffer: outputBuffer, bytesPerRow: this.segmentationWidth * 2 },
{ texture: this.maskTexture },
[this.segmentationWidth, this.segmentationHeight, 1]
);
this.device.queue.submit([copyCommandEncoder.finish()]);
outputBuffer.destroy();
return this.maskTexture;
} else {
const bufferSize = this.segmentationWidth * this.segmentationHeight * 4;
const readbackBuffer = await this.getOrCreateResource(`readbackBuffer${bufferSize}`, async () =>
this.device.createBuffer({
size: bufferSize,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
}));
commandEncoder.copyTextureToBuffer(
{ texture: destTexture },
{ buffer: readbackBuffer, bytesPerRow: this.segmentationWidth * 4 },
[this.segmentationWidth, this.segmentationHeight]
);
this.device.queue.submit([commandEncoder.finish()]);
await readbackBuffer.mapAsync(GPUMapMode.READ);
this.downscaledImageData.data.set(new Uint8Array(readbackBuffer.getMappedRange()));
readbackBuffer.unmap();
const maskImageData = await this.segmenter.segment(this.downscaledImageData);
// Upload
const maskTexture = this.getOrCreateTexture('maskTexture', {
size: [maskImageData.width, maskImageData.height, 1],
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT
});
this.device.queue.writeTexture(
{ texture: maskTexture },
maskImageData.data,
{ bytesPerRow: maskImageData.width * 4 },
[maskImageData.width, maskImageData.height, 1]
);
return maskTexture;
}
}
async render(videoFrame) {
// Import external texture.
let sourceTexture;
if (this.zeroCopy) {
sourceTexture = this.device.importExternalTexture({ source: videoFrame });
} else {
sourceTexture = this.getOrCreateTexture('sourceTexture', {
size: [videoFrame.displayWidth, videoFrame.displayHeight, 1],
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT
});
this.device.queue.copyExternalImageToTexture(
{ source: videoFrame },
{ texture: sourceTexture },
[videoFrame.displayWidth, videoFrame.displayHeight]
);
}
let maskTexture = await this.segment(sourceTexture);
// Always process at full video resolution, ignore display size
const processingWidth = videoFrame.displayWidth || 1280;
const processingHeight = videoFrame.displayHeight || 720;
// Update canvas size only if video resolution actually changed
if (this.webgpuCanvas.width !== processingWidth || this.webgpuCanvas.height !== processingHeight) {
this.webgpuCanvas.width = processingWidth;
this.webgpuCanvas.height = processingHeight;
// Reconfigure context with actual video size
this.context.configure({
device: this.device,
format: navigator.gpu.getPreferredCanvasFormat(),
alphaMode: 'premultiplied',
usage: GPUTextureUsage.RENDER_ATTACHMENT | (this.directOutput ? GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_DST : 0),
});
}
const width = this.webgpuCanvas.width;
const height = this.webgpuCanvas.height;
const canvasTexture = this.context.getCurrentTexture();
const outputTexture = this.getOrCreateTexture('outputTexture', {
size: [width, height, 1],
usage: GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_SRC | GPUTextureUsage.TEXTURE_BINDING
});
const commandEncoder = this.device.createCommandEncoder();
this.blurrer.setInputDimensions(processingWidth, processingHeight);
this.blurrer.blur(commandEncoder, sourceTexture, maskTexture, this.directOutput ? canvasTexture : outputTexture, 360);
if (!this.directOutput) {
const renderPipeline = await this.getOrCreateResource(`renderPipeline_${width}x${height}`, async () =>
this.device.createRenderPipeline({
layout: 'auto',
vertex: {
module: await this.outputRendererVertexShader,
entryPoint: 'main',
},
fragment: {
module: await this.getOutputRendererFragmentShader(this.device, width, height),
entryPoint: 'main',
targets: [{ format: navigator.gpu.getPreferredCanvasFormat() }],
},
primitive: {
topology: 'triangle-list',
},
}));
const renderBindGroup = this.device.createBindGroup({
layout: renderPipeline.getBindGroupLayout(0),
entries: [
{ binding: 0, resource: outputTexture.createView() },
{ binding: 1, resource: this.renderSampler },
],
});
// Render to canvas
const renderPass = commandEncoder.beginRenderPass({
colorAttachments: [{
view: canvasTexture.createView(),
clearValue: { r: 0, g: 0, b: 0, a: 1 },
loadOp: 'clear',
storeOp: 'store',
}],
});
renderPass.setPipeline(renderPipeline);
renderPass.setBindGroup(0, renderBindGroup);
renderPass.draw(6);
renderPass.end();
}
this.device.queue.submit([commandEncoder.finish()]);
// Create a new VideoFrame from the processed WebGPU canvas
return new VideoFrame(this.webgpuCanvas, {
timestamp: videoFrame.timestamp,
duration: videoFrame.duration
});
}
}
export async function getWebGPUDevice() {
const adapter = await navigator.gpu.requestAdapter();
if (!adapter) {
throw new Error('WebGPU adapter not available');
}
// Ensure we're compatible with directOutput
console.log("Adapter features:");
console.log([...adapter.features]);
const requiredFeatures = ['bgra8unorm-storage', 'shader-f16', 'texture-formats-tier1'];
for (const feature of requiredFeatures) {
if (!adapter.features.has(feature)) {
console.log(`${feature} is not supported`);
}
}
const device = await adapter.requestDevice({ requiredFeatures });
if (!device) {
console.error('WebGPU adapter does not support the required features:', requiredFeatures);
}
return device;
}
// WebGPU blur renderer
export async function createWebGPUBlurRenderer(device, segmenter, zeroCopy, directOutput, useWebNN, zeroCopyTensor) {
const blurrer = new WebGPUBlur(device, zeroCopy, directOutput);
await blurrer.init();
return new WebGPURenderer(device, segmenter, blurrer, { zeroCopy, directOutput }, {useWebNN, zeroCopyTensor});
}