Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
283 lines (278 loc) · 8.53 KB
/
Copy pathindex.js
File metadata and controls
283 lines (278 loc) · 8.53 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
// Video / image → RGBA buffer → luminance + Sobel buffer → ASCII rendering.
// setup() and frame() have no dependency on the gallery or editor.
import { WebGPUjs } from "../../../src/pipeline";
import { decodeFrame, extractFeatures, asciiDisplay } from "./shaders.js";
// CC0 sample with verified HTTPS and cross-origin access. The older Blender sample URLs return 403.
export const sampleVideo =
"https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4";
function glyphAtlas() {
if (typeof document === "undefined")
return {
width: 160,
height: 24,
buffer: new Uint8Array(160 * 24 * 4).fill(255),
};
const atlas = document.createElement("canvas");
atlas.width = 160;
atlas.height = 24;
const context = atlas.getContext("2d");
context.fillStyle = "black";
context.fillRect(0, 0, 160, 24);
context.fillStyle = "white";
context.font = "bold 22px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
Array.from(" .:-=+*#%@").forEach((glyph, i) =>
context.fillText(glyph, i * 16 + 8, 12),
);
return atlas;
}
function createInput() {
// The GPU path also works in workers/headless tests with ordinary typed texture data.
if (typeof document === "undefined")
return {
source: () => ({
width: 2,
height: 2,
buffer: new Uint8Array([
255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 255, 255, 255,
]),
}),
label: () => "Typed image input",
setActive() {},
dispose() {},
};
const video = document.createElement("video");
video.crossOrigin = "anonymous";
video.muted = true;
video.loop = true;
video.playsInline = true;
video.preload = "auto";
video.controls = true;
video.setAttribute("aria-label", "Source video");
const fallback = document.createElement("canvas");
fallback.width = 256;
fallback.height = 144;
const context = fallback.getContext("2d");
let active = true,
bitmap,
objectURL,
disposed = false,
videoLabel = "Sample video · MDN CC0 flower",
label = "Loading sample video…";
const play = () => {
if (active && !bitmap && !disposed)
video.play().catch((error) => {
if (error.name === "NotAllowedError")
label = "Press Play video to start playback";
});
};
video.addEventListener("loadeddata", () => {
label = videoLabel;
play();
});
video.addEventListener("error", () => {
label =
"Video unavailable · generated fallback. Choose a local video or image.";
});
function loadVideo(url, title = "Sample video · MDN CC0 flower") {
videoLabel = title;
label = "Loading " + title + "…";
bitmap?.close();
bitmap = undefined;
video.pause();
video.src = url;
video.load();
play();
}
loadVideo(sampleVideo);
return {
source(time) {
if (bitmap) return bitmap;
if (video.readyState >= 2) return video;
const gradient = context.createLinearGradient(0, 0, 256, 144);
gradient.addColorStop(0, "#094851");
gradient.addColorStop(1, "#f8b46c");
context.fillStyle = gradient;
context.fillRect(0, 0, 256, 144);
context.fillStyle = "#e7fff1";
context.beginPath();
context.arc(128 + Math.sin(time / 900) * 75, 72, 34, 0, Math.PI * 2);
context.fill();
return fallback;
},
label: () => label,
setActive(value) {
active = value;
if (active) play();
else video.pause();
},
mountControls(container, invalidate) {
const input = document.createElement("input");
input.type = "file";
input.accept = "video/*,image/*";
const caption = document.createElement("label");
caption.textContent = "Video or image file ";
caption.append(input);
input.addEventListener("change", async () => {
const file = input.files[0];
if (!file) return;
try {
if (objectURL) URL.revokeObjectURL(objectURL);
if (file.type.startsWith("image/")) {
video.pause();
bitmap?.close();
bitmap = await createImageBitmap(file);
label = file.name;
} else {
objectURL = URL.createObjectURL(file);
loadVideo(objectURL, file.name);
}
invalidate();
} catch (error) {
label = error.message;
invalidate();
}
});
const playButton = document.createElement("button");
playButton.textContent = "Play video";
playButton.addEventListener("click", () => {
active = true;
play();
invalidate();
});
const reset = document.createElement("button");
reset.textContent = "Use sample video";
reset.addEventListener("click", () => {
if (objectURL) URL.revokeObjectURL(objectURL);
objectURL = undefined;
input.value = "";
loadVideo(sampleVideo);
invalidate();
});
const source = document.createElement("details");
const summary = document.createElement("summary");
summary.textContent = "Source video";
source.append(summary, video);
const group = document.createElement("div");
group.className = "media-input-controls";
group.append(caption, playButton, reset, source);
container.append(group);
},
dispose() {
disposed = true;
video.pause();
video.removeAttribute("src");
video.load();
bitmap?.close();
if (objectURL) URL.revokeObjectURL(objectURL);
},
};
}
export async function setup(canvas, device, options = {}) {
const width = options.width ?? 128,
height = options.height ?? 72;
const input = createInput();
const decode = await WebGPUjs.createPipeline(decodeFrame, {
device,
computePass: {
workgroupsX: Math.ceil((width * height) / 64),
skipOutputDef: true,
},
resourceBindings: { pixels: { resource: "media-pixels" } },
});
decode.compute.buffer({
inputs: {
image: input.source(0),
pixels: new Float32Array(width * height * 4),
width,
height,
},
});
const features = await WebGPUjs.combineShaders(
extractFeatures,
{
resourceBindings: {
pixels: { resource: "media-pixels" },
features: { resource: "media-features" },
},
computePass: {
workgroupsX: Math.ceil((width * height) / 64),
skipOutputDef: true,
},
},
decode,
);
features.compute.buffer({
inputs: { features: new Float32Array(width * height * 2), width, height },
});
const display = await WebGPUjs.combineShaders(
asciiDisplay,
{
canvas,
resourceBindings: {
pixels: { resource: "media-pixels" },
features: { resource: "media-features" },
},
renderPass: {
vbos: [],
vertexCount: 3,
textures: { atlas: glyphAtlas() },
},
},
features,
);
async function frame(time, controls = {}) {
const encoder = device.createCommandEncoder();
await decode.compute.run({
commandEncoder: encoder,
inputs: { image: input.source(time), width, height },
});
await features.compute.run({
commandEncoder: encoder,
inputs: { width, height },
});
await display.render({
commandEncoder: encoder,
inputs: {
width,
height,
columns: controls.columns ?? 64,
colorize: controls.colorize ?? 1,
edgeMix: controls.edgeMix ?? 0.35,
},
});
device.queue.submit([encoder.finish()]);
}
return {
pipelines: [decode, features, display],
frame,
mountControls: input.mountControls,
setActive: input.setActive,
dispose: input.dispose,
get resultInfo() {
return `${input.label()}\nLeft: captured pixel buffer. Right: glyph atlas selected by luminance and Sobel edges. Three passes, one submission, no CPU pixel readback.`;
},
getBuffer(name = "pixels") {
const helper = name === "pixels" ? decode : features;
return helper.resources.get(
helper.compute.resources.find((resource) => resource.name === name)
.resource,
).buffer;
},
};
}
export default {
id: "video-ascii",
controls: [
["columns", "Glyph columns", 24, 96, 4, 64],
["colorize", "Original colors", 0, 1, 1, 1],
["edgeMix", "Edge emphasis", 0, 1, 0.05, 0.35],
],
title: "Video / image → buffers → ASCII",
description:
"A decoded video or image feeds an RGBA storage buffer, a luminance/Sobel compute pass and glyph-atlas rendering. Every intermediate stays on the GPU.",
category: "Media processing",
canvasSize: [1024, 576],
setup,
};