summaryrefslogtreecommitdiff
path: root/web/host.js
blob: aa45840e20035e75041c483d723813c868a9e676 (plain) (blame)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
// sketchbook web host: forwards input to the sketch and does what it asks:
// drawing on a 2D canvas, GPU work on WebGPU (shaders arrive as WGSL), sound
// and the microphone on Web Audio, files through a picker. It knows nothing
// about any particular sketch.
const canvas = document.querySelector('canvas');
const status = document.querySelector('#status');
const ctx = canvas.getContext('2d', {alpha: false});
const decoder = new TextDecoder();
const encoder = new TextEncoder();
const images = [null];
const byPath = new Map();
const loads = [];
let wasm, clipped = false, size = [960, 600], frameNumber = 0;

// An empty Zig slice may point anywhere, even past the memory.
const bytes = (ptr, len) => len ? new Uint8Array(wasm.memory.buffer, ptr, len) : new Uint8Array(0);
const string = (ptr, len) => decoder.decode(bytes(ptr, len));
const rgba = (r, g, b, a) => `rgba(${r},${g},${b},${a / 255})`;
const Kind = {move: 1, down: 2, up: 3, wheel: 4, keyDown: 5, keyUp: 6, text: 7, cancel: 8};

function fit() {
  const [w, h] = size;
  canvas.style.width = `min(100vw, ${w / h * 100}vh)`;
  canvas.style.height = `min(${h / w * 100}vw, 100vh)`;
  const ratio = Math.min(window.devicePixelRatio || 1, 2);
  const box = canvas.getBoundingClientRect();
  const pw = Math.round(box.width * ratio), ph = Math.round(box.height * ratio);
  if (canvas.width !== pw || canvas.height !== ph) { canvas.width = pw; canvas.height = ph; clipped = false; }
  if (clipped) { ctx.restore(); clipped = false; }
  ctx.setTransform(canvas.width / w, 0, 0, canvas.height / h, 0, 0);
  ctx.imageSmoothingEnabled = false;
}

// Hands bytes to the sketch: into memory it allocates, which the export
// it is passed to takes over. Views are made after allocating, which may
// grow (and so replace) the memory.
function give(source) {
  const view = source instanceof Float32Array ? new Uint8Array(source.buffer, source.byteOffset, source.byteLength) : source;
  const ptr = wasm.sk_alloc(view.length);
  bytes(ptr, view.length).set(view);
  return ptr;
}

// ---------------------------------------------------------------------------
// GPU: abi.gpu on WebGPU. Buffers and images by handle; shader modules and
// pipelines cached by their description; one queue submission per dispatch
// or pass, in order, as on native.

const gpu = {
  device: null,
  buffers: [null],
  modules: new Map(),
  pipelines: new Map(),
  // Uniforms and vertices go through rings written before each submission;
  // the queue orders those writes with the work, so wrapping is safe.
  rings: {},
  spare: [],

  // Why there is no device, for the page to say.
  missing: '',

  async start() {
    if (!window.isSecureContext) return void (gpu.missing = `WebGPU needs a secure context: open the page on localhost or over https, or tell the browser to trust ${location.origin}.`);
    if (!navigator.gpu) return void (gpu.missing = 'This browser has WebGPU turned off. In Firefox: about:config, set dom.webgpu.enabled to true.');
    try {
      const adapter = await navigator.gpu.requestAdapter();
      if (!adapter) return void (gpu.missing = 'WebGPU found no usable GPU: the browser may block this one (in Firefox: about:config, gfx.webgpu.ignore-blocklist).');
      const limits = adapter.limits;
      gpu.device = await adapter.requestDevice({requiredLimits: {
        maxComputeWorkgroupStorageSize: limits.maxComputeWorkgroupStorageSize,
        maxStorageBufferBindingSize: limits.maxStorageBufferBindingSize,
        maxBufferSize: limits.maxBufferSize,
      }});
      gpu.device.addEventListener('uncapturederror', e => console.error('WebGPU:', e.error.message));
      gpu.device.lost.then(info => { console.error('WebGPU device lost:', info.message); gpu.device = null; });
    } catch (error) {
      gpu.missing = `WebGPU failed to start: ${error.message ?? error}`;
    }
  },
  // Says why, once, the first time a sketch asks for the GPU and finds none.
  absent() {
    if (gpu.missing && !gpu.told) {
      gpu.told = true;
      console.warn(gpu.missing);
      status.textContent = gpu.missing;
      status.hidden = false;
    }
    return 0;
  },

  ring(name, usage, need) {
    let r = gpu.rings[name];
    if (!r || r.size < need) {
      const size = Math.max(need, 4 << 20);
      r = gpu.rings[name] = {buffer: gpu.device.createBuffer({size, usage: usage | GPUBufferUsage.COPY_DST}), size, at: 0};
    }
    return r;
  },
  // Writes `data` into a ring, returning where.
  put(name, usage, data, align) {
    const r = gpu.ring(name, usage, data.length);
    let at = Math.ceil(r.at / align) * align;
    if (at + data.length > r.size) at = 0;
    gpu.device.queue.writeBuffer(r.buffer, at, data);
    r.at = at + data.length;
    return {buffer: r.buffer, offset: at, size: data.length};
  },
  uniforms([ptr, len]) {
    return len ? gpu.put('uniforms', GPUBufferUsage.UNIFORM, bytes(ptr, len).slice(), 256) : null;
  },

  module(shader) {
    let m = gpu.modules.get(shader.hash);
    if (!m) {
      m = gpu.device.createShaderModule({code: string(...shader.code)});
      m.getCompilationInfo().then(info => { for (const msg of info.messages) if (msg.type === 'error') console.error(`WGSL ${msg.lineNum}:${msg.linePos} ${msg.message}`); });
      gpu.modules.set(shader.hash, m);
    }
    return m;
  },

  // Bind groups 0..3 in SDL_GPU's numbering; an empty one where a later
  // one is used.
  bind(encoder, pipeline, groups) {
    for (let i = 0; i < groups.length; i++) {
      const entries = groups[i] ?? [];
      if (!entries.length && !groups.slice(i + 1).some(g => g?.length)) break;
      encoder.setBindGroup(i, gpu.device.createBindGroup({layout: pipeline.getBindGroupLayout(i), entries}));
    }
  },
  storage(ids) { return ids.map((id, binding) => ({binding, resource: {buffer: gpu.buffers[id]}})); },
  uniform(u) { return u ? [{binding: 0, resource: u}] : []; },

  newBuffer(size) {
    const usage = GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC;
    gpu.buffers.push(gpu.device.createBuffer({size: Math.ceil(size / 4) * 4, usage}));
    return gpu.buffers.length - 1;
  },

  compute(d) {
    const k = d.kernel;
    const key = `compute ${k.hash}`;
    let pipeline = gpu.pipelines.get(key);
    if (!pipeline) {
      pipeline = gpu.device.createComputePipeline({layout: 'auto', compute: {module: gpu.module(k)}});
      gpu.pipelines.set(key, pipeline);
    }
    const uniforms = gpu.uniforms(d.uniforms);
    const commands = gpu.device.createCommandEncoder();
    const pass = commands.beginComputePass();
    pass.setPipeline(pipeline);
    gpu.bind(pass, pipeline, [gpu.storage(d.readonly), gpu.storage(d.readwrite), gpu.uniform(uniforms)]);
    pass.dispatchWorkgroups(...d.groups);
    pass.end();
    gpu.device.queue.submit([commands.finish()]);
  },

  // The texture a pass renders into, and the canvas that shows it.
  target(id, w, h) {
    const entry = images[id];
    if (!entry) return null;
    if (!entry.texture || entry.texture.width !== w || entry.texture.height !== h) {
      entry.texture?.destroy();
      entry.texture = gpu.device.createTexture({size: [w, h], format: 'rgba8unorm', usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC | GPUTextureUsage.TEXTURE_BINDING});
      entry.shown = document.createElement('canvas');
      entry.shown.width = w; entry.shown.height = h;
      entry.view = entry.shown.getContext('webgpu');
      entry.view.configure({device: gpu.device, format: 'rgba8unorm', usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT, alphaMode: 'premultiplied'});
      entry.w = w; entry.h = h;
      entry.attachments = {};
    }
    return entry;
  },
  // Multisample and stencil attachments, kept per image.
  attachment(entry, kind, format, samples) {
    const key = `${kind} ${samples}`;
    if (!entry.attachments[key]) entry.attachments[key] = gpu.device.createTexture({size: [entry.w, entry.h], format, sampleCount: samples, usage: GPUTextureUsage.RENDER_ATTACHMENT});
    return entry.attachments[key];
  },
  // Copies the latest render into the image's canvas, once a frame, for
  // the 2D canvas to draw.
  present(entry) {
    if (entry.presented === frameNumber) return;
    entry.presented = frameNumber;
    const commands = gpu.device.createCommandEncoder();
    commands.copyTextureToTexture({texture: entry.texture}, {texture: entry.view.getCurrentTexture()}, [entry.w, entry.h]);
    gpu.device.queue.submit([commands.finish()]);
  },

  pipeline(p, samples, stencil) {
    const key = JSON.stringify([p.vertex.hash, p.fragment.hash, p.stride, p.attributes, p.primitive, p.blend, p.write_color, p.stencil, samples, stencil]);
    let pipeline = gpu.pipelines.get(key);
    if (pipeline) return pipeline;
    const formats = {float: 'float32', float2: 'float32x2', float3: 'float32x3', float4: 'float32x4', ubyte4_norm: 'unorm8x4'};
    const blends = {
      none: undefined,
      premultiplied: {color: {srcFactor: 'one', dstFactor: 'one-minus-src-alpha'}, alpha: {srcFactor: 'one', dstFactor: 'one-minus-src-alpha'}},
      additive: {color: {srcFactor: 'one', dstFactor: 'one'}, alpha: {srcFactor: 'one', dstFactor: 'one'}},
    };
    const dashed = s => s.replaceAll('_', '-').replace('-or-equal', '-equal').replace('-and-clamp', '-clamp').replace('-and-wrap', '-wrap');
    const face = f => ({compare: dashed(f.compare), passOp: dashed(f.pass), failOp: dashed(f.fail), depthFailOp: 'keep'});
    const keep = {compare: 'always', passOp: 'keep', failOp: 'keep', depthFailOp: 'keep'};
    pipeline = gpu.device.createRenderPipeline({
      layout: 'auto',
      vertex: {
        module: gpu.module(p.vertex),
        buffers: p.attributes.length ? [{arrayStride: p.stride, attributes: p.attributes.map(a => ({shaderLocation: a.location, offset: a.offset, format: formats[a.format]}))}] : [],
      },
      fragment: {module: gpu.module(p.fragment), targets: [{format: 'rgba8unorm', blend: blends[p.blend], writeMask: p.write_color ? GPUColorWrite.ALL : 0}]},
      primitive: {topology: dashed(p.primitive)},
      multisample: {count: samples},
      depthStencil: stencil ? {
        format: 'depth24plus-stencil8', depthWriteEnabled: false, depthCompare: 'always',
        stencilFront: p.stencil ? face(p.stencil.front) : keep, stencilBack: p.stencil ? face(p.stencil.back) : keep,
      } : undefined,
    });
    gpu.pipelines.set(key, pipeline);
    return pipeline;
  },

  pass(p) {
    const entry = gpu.target(p.image, p.width, p.height);
    if (!entry) return;
    const samples = p.samples > 1 ? 4 : 1; // WebGPU offers 1 and 4.
    const view = entry.texture.createView();
    const color = {
      view: samples > 1 ? gpu.attachment(entry, 'msaa', 'rgba8unorm', samples).createView() : view,
      resolveTarget: samples > 1 ? view : undefined,
      loadOp: p.clear ? 'clear' : 'load',
      clearValue: p.clear ? {r: p.clear[0], g: p.clear[1], b: p.clear[2], a: p.clear[3]} : undefined,
      storeOp: 'store',
    };
    const depthStencil = p.stencil ? {
      view: gpu.attachment(entry, 'stencil', 'depth24plus-stencil8', samples).createView(),
      depthLoadOp: 'clear', depthClearValue: 1, depthStoreOp: 'discard',
      stencilLoadOp: 'clear', stencilClearValue: 0, stencilStoreOp: 'discard',
    } : undefined;
    // Every draw's bytes are written before the pass is submitted.
    const draws = p.draws.map(d => ({
      d,
      vertices: d.vertices[1] ? gpu.put('vertices', GPUBufferUsage.VERTEX, bytes(...d.vertices).slice(), 4) : null,
      vertexUniforms: gpu.uniforms(d.vertex_uniforms),
      fragmentUniforms: gpu.uniforms(d.fragment_uniforms),
    }));
    const commands = gpu.device.createCommandEncoder();
    const pass = commands.beginRenderPass({colorAttachments: [color], depthStencilAttachment: depthStencil});
    for (const {d, vertices, vertexUniforms, fragmentUniforms} of draws) {
      const pipeline = gpu.pipeline(p.pipelines[d.pipeline], samples, p.stencil);
      pass.setPipeline(pipeline);
      gpu.bind(pass, pipeline, [gpu.storage(d.vertex_storage), gpu.uniform(vertexUniforms), gpu.storage(d.fragment_storage), gpu.uniform(fragmentUniforms)]);
      if (vertices) pass.setVertexBuffer(0, vertices.buffer, vertices.offset, vertices.size);
      if (p.stencil) pass.setStencilReference(d.stencil_reference);
      const s = d.scissor;
      if (s) {
        const x = Math.max(0, Math.floor(s.x)), y = Math.max(0, Math.floor(s.y));
        pass.setScissorRect(x, y, Math.max(0, Math.min(p.width, Math.ceil(s.x + s.w)) - x), Math.max(0, Math.min(p.height, Math.ceil(s.y + s.h)) - y));
      } else pass.setScissorRect(0, 0, p.width, p.height);
      pass.draw(d.vertex_count);
    }
    pass.end();
    gpu.device.queue.submit([commands.finish()]);
  },

  // Reads a buffer back; it reaches the sketch before a later frame.
  arrived: [],
  download(id, offset, size, key) {
    const staging = gpu.spare.find(b => b.size >= size) ?? gpu.device.createBuffer({size: Math.max(size, 256), usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST});
    gpu.spare = gpu.spare.filter(b => b !== staging);
    const commands = gpu.device.createCommandEncoder();
    commands.copyBufferToBuffer(gpu.buffers[id], offset, staging, 0, size);
    gpu.device.queue.submit([commands.finish()]);
    staging.mapAsync(GPUMapMode.READ, 0, size).then(() => {
      gpu.arrived.push({key, bytes: new Uint8Array(staging.getMappedRange(0, size)).slice()});
      staging.unmap();
      gpu.spare.push(staging);
    }, () => {});
  },
};

// ---------------------------------------------------------------------------
// Sound: streams queued and mixed on the main thread, the microphone
// collected the same way, both at the context's rate (48 kHz if the browser
// agrees). Browsers start sound only after a click or a key.

const sound = {
  context: null,
  queues: new Map(),
  heard: [],
  microphone: null,

  start() {
    // Safari: play as a media app does, through the ringer's silent switch.
    if (navigator.audioSession) navigator.audioSession.type = 'playback';
    try { sound.context = new AudioContext({sampleRate: 48000, latencyHint: 'interactive'}); } catch { sound.context = new AudioContext(); }
    const player = sound.context.createScriptProcessor(1024, 1, 1);
    player.onaudioprocess = e => {
      sound.pulled = performance.now();
      const out = e.outputBuffer.getChannelData(0);
      out.fill(0);
      for (const q of sound.queues.values()) {
        let i = 0;
        while (i < out.length && q.chunks.length) {
          const chunk = q.chunks[0];
          const n = Math.min(out.length - i, chunk.length - q.at);
          for (let j = 0; j < n; j++) out[i + j] += chunk[q.at + j];
          i += n; q.at += n; q.queued -= n;
          if (q.at === chunk.length) { q.chunks.shift(); q.at = 0; }
        }
      }
    };
    player.connect(sound.context.destination);
    const resume = () => sound.context.resume().then(() => {
      if (sound.hinted && !gpu.told) status.hidden = true;
    });
    // On touch screens browsers count only a tap's end (or the click after
    // it) as the gesture that may start sound.
    for (const type of ['pointerdown', 'pointerup', 'touchend', 'click', 'keydown']) window.addEventListener(type, resume);
  },
  // Browsers start sound only after a gesture: say so, once, while a
  // sketch that plays or listens waits for one.
  hinted: false,
  hint() {
    if (sound.context.state === 'running' || sound.hinted || gpu.told) return;
    sound.hinted = true;
    status.textContent = 'Click, tap or press a key to start sound and the microphone.';
    status.hidden = false;
  },
  // While sound is not playing (no tap yet, the page hidden, or a browser
  // whose mixer has stopped asking for samples), the queues drain by the
  // clock as if heard, so sketches that pace themselves by what is queued
  // keep running, silently.
  pulled: 0,
  drain(seconds) {
    if (sound.context.state === 'running' && performance.now() - sound.pulled < 300) return;
    for (const q of sound.queues.values()) {
      let n = Math.round(seconds * sound.context.sampleRate);
      while (n > 0 && q.chunks.length) {
        const take = Math.min(n, q.chunks[0].length - q.at);
        q.at += take; q.queued -= take; n -= take;
        if (q.at === q.chunks[0].length) { q.chunks.shift(); q.at = 0; }
      }
    }
  },
  queue(key) {
    if (!sound.queues.has(key)) sound.queues.set(key, {chunks: [], at: 0, queued: 0});
    return sound.queues.get(key);
  },
  listen() {
    sound.hint();
    if (sound.microphone) return;
    sound.microphone = 'asked';
    if (!navigator.mediaDevices) return console.warn('The microphone needs a secure context: localhost or https.');
    navigator.mediaDevices.getUserMedia({audio: {echoCancellation: false, noiseSuppression: false, autoGainControl: false}}).then(stream => {
      const source = sound.context.createMediaStreamSource(stream);
      const recorder = sound.context.createScriptProcessor(1024, 1, 1);
      recorder.onaudioprocess = e => {
        sound.heard.push(e.inputBuffer.getChannelData(0).slice());
        e.outputBuffer.getChannelData(0).fill(0);
      };
      source.connect(recorder);
      recorder.connect(sound.context.destination);
      sound.microphone = stream;
    }, error => console.warn('No microphone:', error));
  },
};

// A file the user picked: decoded by the browser, handed to the sketch by
// name, then announced as the console line `<command> <name>`.
async function openFile(command, pattern) {
  const input = document.createElement('input');
  input.type = 'file';
  if (pattern) input.accept = pattern.split(';').map(e => `.${e}`).join(',');
  input.onchange = async () => {
    const file = input.files[0];
    if (!file) return;
    try {
      const decoded = await sound.context.decodeAudioData(await file.arrayBuffer());
      const mono = new Float32Array(decoded.length);
      for (let c = 0; c < decoded.numberOfChannels; c++) {
        const channel = decoded.getChannelData(c);
        for (let i = 0; i < mono.length; i++) mono[i] += channel[i] / decoded.numberOfChannels;
      }
      const name = encoder.encode(file.name);
      wasm.sk_sound(give(name), name.length, give(mono), mono.length);
      const line = encoder.encode(`${command} ${file.name}`);
      wasm.sk_command(give(line), line.length);
    } catch (error) {
      console.error(`Cannot open ${file.name}:`, error);
    }
  };
  input.click();
}

// Microphone samples and readbacks, into the sketch before each frame.
function deliver() {
  if (sound.heard.length) {
    const total = sound.heard.reduce((n, c) => n + c.length, 0);
    const all = new Float32Array(total);
    let at = 0;
    for (const chunk of sound.heard) { all.set(chunk, at); at += chunk.length; }
    sound.heard = [];
    wasm.sk_microphone(give(all), total);
  }
  for (const {key, bytes: b} of gpu.arrived) wasm.sk_download(key, give(b), b.length);
  gpu.arrived = [];
}

const imports = {
  host: {
    log(ptr, len) { console.log(string(ptr, len)); },
    loadImage(ptr, len) {
      const path = string(ptr, len);
      if (byPath.has(path)) return byPath.get(path);
      const entry = {source: new Image(), w: 0, h: 0};
      loads.push(new Promise(resolve => {
        entry.source.onload = () => { entry.w = entry.source.naturalWidth; entry.h = entry.source.naturalHeight; resolve(); };
        entry.source.onerror = () => { console.warn(`missing image ${path}`); entry.source = null; resolve(); };
      }));
      entry.source.src = path;
      images.push(entry);
      byPath.set(path, images.length - 1);
      return images.length - 1;
    },
    clock: () => performance.now(),
    imageWidth: id => images[id]?.w ?? 0,
    imageHeight: id => images[id]?.h ?? 0,
    newImage(w, h) {
      const source = document.createElement('canvas');
      source.width = w; source.height = h;
      images.push({source, w, h, ctx: source.getContext('2d')});
      return images.length - 1;
    },
    freeImage(id) {
      if (!images[id]) return;
      images[id].texture?.destroy();
      for (const t of Object.values(images[id].attachments ?? {})) t.destroy();
      images[id] = null;
    },
    openFile(command, commandLen, pattern, patternLen) { openFile(string(command, commandLen), string(pattern, patternLen)); },
  },
  canvas: {
    clear(r, g, b, a) { ctx.fillStyle = rgba(r, g, b, a); ctx.fillRect(0, 0, size[0], size[1]); },
    fill(x, y, w, h, r, g, b, a) { ctx.fillStyle = rgba(r, g, b, a); ctx.fillRect(x, y, w, h); },
    image(id, x, y, w, h, sx, sy, sw, sh, alpha) {
      const entry = images[id];
      if (entry?.texture) {
        // GPU renders scale smoothly, as on native.
        gpu.present(entry);
        ctx.globalAlpha = alpha / 255;
        ctx.imageSmoothingEnabled = true;
        if (sw > 0) ctx.drawImage(entry.shown, sx, sy, sw, sh, x, y, w, h); else ctx.drawImage(entry.shown, x, y, w, h);
        ctx.imageSmoothingEnabled = false;
        ctx.globalAlpha = 1;
        return;
      }
      if (!entry?.source || (entry.source instanceof Image && !entry.w)) { ctx.fillStyle = '#6e1e46'; ctx.fillRect(x, y, w, h); return; }
      ctx.globalAlpha = alpha / 255;
      if (sw > 0) ctx.drawImage(entry.source, sx, sy, sw, sh, x, y, w, h); else ctx.drawImage(entry.source, x, y, w, h);
      ctx.globalAlpha = 1;
    },
    text(x, y, scale, ptr, len, r, g, b, a) {
      ctx.fillStyle = rgba(r, g, b, a);
      // The native font is a fixed 8px cell: the same advance, and a size
      // whose capitals fill the cell as native ones do.
      ctx.font = `${10 * scale}px monospace`;
      ctx.textBaseline = 'top';
      for (const [i, ch] of Array.from(string(ptr, len)).entries()) ctx.fillText(ch, x + i * 8 * scale, y);
    },
    clip(enabled, x, y, w, h) {
      if (clipped) { ctx.restore(); clipped = false; }
      if (enabled) { ctx.save(); ctx.beginPath(); ctx.rect(x, y, w, h); ctx.clip(); clipped = true; }
    },
    canvas(w, h) { if (size[0] !== w || size[1] !== h) { size = [w, h]; } fit(); },
    cursor(kind) { canvas.style.cursor = ['none', 'default', 'pointer'][kind] ?? 'default'; },
    pixels(id, w, h, ptr, len, premultiplied) {
      const entry = images[id];
      if (!entry?.ctx || len < w * h * 4) return;
      const data = new Uint8ClampedArray(wasm.memory.buffer.slice(ptr, ptr + w * h * 4));
      if (premultiplied) for (let i = 0; i < data.length; i += 4) {
        const a = data[i + 3];
        if (a && a < 255) { data[i] = data[i] * 255 / a; data[i + 1] = data[i + 1] * 255 / a; data[i + 2] = data[i + 2] * 255 / a; }
      }
      entry.ctx.putImageData(new ImageData(data, w, h), 0, 0);
    },
  },
  gpu: {
    available: () => gpu.device ? 1 : gpu.absent(),
    newBuffer: size => gpu.device ? gpu.newBuffer(size) : 0,
    freeBuffer(id) { gpu.buffers[id]?.destroy(); gpu.buffers[id] = null; },
    write(id, offset, ptr, len) { if (gpu.buffers[id]) gpu.device.queue.writeBuffer(gpu.buffers[id], offset, bytes(ptr, len).slice()); },
    compute(ptr, len) { if (gpu.device) gpu.compute(JSON.parse(string(ptr, len))); },
    pass(ptr, len) { if (gpu.device) gpu.pass(JSON.parse(string(ptr, len))); },
    download(id, offset, size, key) { if (gpu.buffers[id]) gpu.download(id, offset, size, key); },
  },
  audio: {
    rate: () => sound.context.sampleRate,
    push(key, ptr, len, clear) {
      if (len) sound.hint();
      const q = sound.queue(key);
      if (clear) { q.chunks = []; q.at = 0; q.queued = 0; }
      if (len) { q.chunks.push(new Float32Array(wasm.memory.buffer.slice(ptr, ptr + len * 4))); q.queued += len; }
    },
    queued: key => sound.queues.get(key)?.queued ?? 0,
    listen: () => sound.listen(),
  },
};

// DOM `code` to USB HID usage, the numbering recordings and sketches use.
const hid = code => {
  if (/^Key[A-Z]$/.test(code)) return code.charCodeAt(3) - 61;
  if (/^Digit[0-9]$/.test(code)) return code[5] === '0' ? 39 : code.charCodeAt(5) - 19;
  return {Enter: 40, Escape: 41, Backspace: 42, Tab: 43, Space: 44, Minus: 45, Equal: 46, BracketLeft: 47, BracketRight: 48,
    Backslash: 49, Semicolon: 51, Quote: 52, Backquote: 53, Comma: 54, Period: 55, Slash: 56, Home: 74, PageUp: 75,
    Delete: 76, End: 77, PageDown: 78, ArrowRight: 79, ArrowLeft: 80, ArrowDown: 81, ArrowUp: 82, NumpadEnter: 88,
    ControlLeft: 224, ShiftLeft: 225, AltLeft: 226, MetaLeft: 227, ControlRight: 228, ShiftRight: 229, AltRight: 230, MetaRight: 231}[code] ?? 0;
};
const floatBits = f => new Uint32Array(new Float32Array([f]).buffer)[0];

try {
  await gpu.start();
  sound.start();
  const response = await fetch('sketch.wasm');
  if (!response.ok) throw new Error('The sketch could not be loaded.');
  wasm = (await WebAssembly.instantiate(await response.arrayBuffer(), imports)).instance.exports;
  // The room for the canvas, which responsive sketches fill.
  const room = () => wasm.sk_window(window.visualViewport?.width ?? window.innerWidth, window.visualViewport?.height ?? window.innerHeight);
  room();
  window.addEventListener('resize', room);
  window.visualViewport?.addEventListener('resize', room);
  wasm.sk_init();
  wasm.sk_frame(0);
  await Promise.all(loads);
  if (!gpu.told && !(sound.hinted && sound.context.state !== 'running')) status.hidden = true;

  // `flags`: for pointer events, the modifiers held (abi.Event.withModifiers).
  const send = (kind, x = 0, y = 0, code = 0, value = 0, flags = 0) => wasm.sk_event(kind, code, x, y, value, flags);
  const held = e => ((e.shiftKey ? 1 : 0) | (e.ctrlKey ? 2 : 0) | (e.altKey ? 4 : 0) | (e.metaKey ? 8 : 0)) << 4;
  const point = e => {
    const r = canvas.getBoundingClientRect();
    return [(e.clientX - r.left) * size[0] / r.width, (e.clientY - r.top) * size[1] / r.height];
  };
  // Mouse and pen: the pointer, one at a time.
  let pointer = null;
  // Touch: one finger is the mouse's left button. Two fingers pan, sent as
  // the wheel (60 px a notch), and pinch, sent as the wheel with ctrl in its
  // flags (one notch per 20 % of spread), the way browsers report a
  // trackpad's pinch; both around the point between the fingers. Once two fingers are down, the
  // rest of the touch is a gesture until every finger lifts.
  const touches = new Map();
  let gesture = null;
  const measure = () => {
    const [a, b] = [...touches.values()];
    return {mid: [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2], spread: Math.hypot(a[0] - b[0], a[1] - b[1])};
  };
  const ctrl = 2 << 4;
  // Synthetic pointers (tests) cannot be captured; real ones always can.
  const capture = id => { try { canvas.setPointerCapture(id); } catch {} };
  canvas.addEventListener('pointerdown', e => {
    e.preventDefault();
    if (e.pointerType === 'touch') {
      capture(e.pointerId);
      touches.set(e.pointerId, point(e));
      if (touches.size === 1 && !gesture) {
        send(Kind.move, ...point(e));
        send(Kind.down, ...point(e), 1);
      } else if (touches.size === 2) {
        if (!gesture) send(Kind.cancel);
        gesture = measure();
      }
      return;
    }
    if (pointer !== null) return;
    pointer = e.pointerId; capture(pointer);
    send(Kind.down, ...point(e), e.button + 1, 0, held(e));
  });
  canvas.addEventListener('pointermove', e => {
    if (e.pointerType !== 'touch') return send(Kind.move, ...point(e), 0, 0, held(e));
    if (!touches.has(e.pointerId)) return;
    touches.set(e.pointerId, point(e));
    if (!gesture) return send(Kind.move, ...point(e));
    if (touches.size < 2) return;
    const now = measure();
    const pan = now.mid[1] - gesture.mid[1];
    if (pan) send(Kind.wheel, ...now.mid, 0, floatBits(pan / 60));
    if (gesture.spread > 0 && now.spread > 0 && now.spread !== gesture.spread) {
      send(Kind.wheel, ...now.mid, 0, floatBits(Math.log(now.spread / gesture.spread) / Math.log(1.2)), ctrl);
    }
    gesture = now;
  });
  const lift = e => {
    if (e.pointerType === 'touch') {
      if (!touches.delete(e.pointerId)) return;
      if (!gesture) send(e.type === 'pointercancel' ? Kind.cancel : Kind.up, ...point(e), 1);
      else if (touches.size === 0) gesture = null;
      return;
    }
    if (e.pointerId !== pointer) return;
    if (e.type === 'pointercancel') send(Kind.cancel); else send(Kind.up, ...point(e), e.button + 1, 0, held(e));
    const id = pointer; pointer = null;
    if (canvas.hasPointerCapture(id)) canvas.releasePointerCapture(id);
  };
  canvas.addEventListener('pointerup', lift);
  canvas.addEventListener('pointercancel', lift);
  canvas.addEventListener('contextmenu', e => e.preventDefault());
  const cancel = () => { pointer = null; touches.clear(); gesture = null; send(Kind.cancel); };
  canvas.addEventListener('lostpointercapture', e => { if (e.pointerId === pointer) cancel(); });
  canvas.addEventListener('pointerleave', e => { if (pointer === null && e.pointerType !== 'touch') send(Kind.move, -1, -1); });
  // A trackpad's pinch arrives as the wheel with ctrlKey set.
  canvas.addEventListener('wheel', e => { e.preventDefault(); send(Kind.wheel, ...point(e), 0, floatBits(-Math.sign(e.deltaY)), held(e)); }, {passive: false});
  window.addEventListener('keydown', e => {
    // Keys the sketch may use, not the page.
    if (['Space', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'PageUp', 'PageDown', 'Home', 'End', 'Backspace'].includes(e.code)) e.preventDefault();
    send(Kind.keyDown, 0, 0, hid(e.code), 0);
    if (e.key.length === 1 && !e.ctrlKey && !e.metaKey) send(Kind.text, 0, 0, 0, e.key.codePointAt(0));
  });
  window.addEventListener('keyup', e => send(Kind.keyUp, 0, 0, hid(e.code), 0));
  window.addEventListener('blur', cancel);
  window.addEventListener('resize', fit);
  document.addEventListener('visibilitychange', () => { if (document.hidden) cancel(); });

  // Tests read state by field path, like the native probe.
  window.sketch = {
    field(path) {
      const encoded = encoder.encode(path);
      new Uint8Array(wasm.memory.buffer, wasm.sk_buffer(), encoded.length).set(encoded);
      return wasm.sk_field(encoded.length);
    },
    // Runs a console line; with `sound`, opens sounds without the picker.
    command(line) { const encoded = encoder.encode(line); wasm.sk_command(give(encoded), encoded.length); },
    sound(name, samples) { const encoded = encoder.encode(name); wasm.sk_sound(give(encoded), encoded.length, give(samples), samples.length); },
    size: () => size,
    // 'running', or 'suspended' until a tap or key starts sound.
    audio: () => sound.context.state,
  };

  let previous = performance.now();
  const frame = ms => {
    try {
      frameNumber++;
      sound.drain(Math.min((ms - previous) / 1000, 0.25));
      previous = ms;
      deliver();
      wasm.sk_frame(ms);
      requestAnimationFrame(frame);
    } catch (error) {
      status.textContent = `The sketch stopped (${error?.message ?? error}). Reload to start again.`; status.hidden = false; console.error(error);
    }
  };
  requestAnimationFrame(frame);

  // Built as an installable app (a manifest in the page): keep an offline copy.
  if (document.querySelector('link[rel="manifest"]') && navigator.serviceWorker) {
    navigator.serviceWorker.register('sw.js').catch(error => console.warn('No offline copy:', error));
  }
} catch (error) {
  status.textContent = `The sketch could not start (${error?.message ?? error}). Reload to try again.`;
  console.error(error);
}