// 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 screen = canvas.getContext('2d', {alpha: false}); // Where 2D commands draw: the canvas, or an image's (`Command.target`). let ctx = screen, drawing = null, composite = 'source-over'; 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 === screen) { ctx.restore(); clipped = false; } screen.setTransform(canvas.width / w, 0, 0, canvas.height / h, 0, 0); screen.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 | (kind === 'stencil' && samples === 1 ? GPUTextureUsage.TEXTURE_BINDING : 0)}); 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, p.depth_test, p.depth_write, 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: p.depth_write ?? false, depthCompare: dashed(p.depth_test ?? '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 || p.depth ? { view: gpu.attachment(entry, 'stencil', 'depth24plus-stencil8', samples).createView(), depthLoadOp: 'clear', depthClearValue: 1, depthStoreOp: p.depth ? 'store' : 'discard', stencilLoadOp: 'clear', stencilClearValue: 0, stencilStoreOp: 'discard', } : undefined; entry.depthValid = p.depth; entry.depthSamples = samples; // 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 || p.depth); 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()]); }, // Fragment group 2: color texture/sampler at 0/1, optional depth at 2/3. postProcess({source, target, effect}) { if (source === target) throw new Error('FeedbackLoop'); const input = images[source]; if (!input?.texture) throw new Error('MissingImage'); if (effect.sample_depth && !input.depthValid) throw new Error('MissingDepthAttachment'); if (effect.sample_depth && input.depthSamples !== 1) throw new Error('MultisampleDepthUnsupported'); const output = gpu.target(target, input.w, input.h); if (!output) return; const key = `post ${effect.vertex.hash} ${effect.fragment.hash} ${effect.sample_depth}`; let pipeline = gpu.pipelines.get(key); if (!pipeline) { const entries = [ {binding: 0, visibility: GPUShaderStage.FRAGMENT, texture: {sampleType: 'float'}}, {binding: 1, visibility: GPUShaderStage.FRAGMENT, sampler: {type: 'non-filtering'}}, ]; if (effect.sample_depth) entries.push( {binding: 2, visibility: GPUShaderStage.FRAGMENT, texture: {sampleType: 'unfilterable-float'}}, {binding: 3, visibility: GPUShaderStage.FRAGMENT, sampler: {type: 'non-filtering'}}, ); const layouts = [[], [], entries, effect.fragment.uniform_buffers ? [{binding: 0, visibility: GPUShaderStage.FRAGMENT, buffer: {type: 'uniform'}}] : []]; pipeline = gpu.device.createRenderPipeline({ layout: gpu.device.createPipelineLayout({bindGroupLayouts: layouts.map(entries => gpu.device.createBindGroupLayout({entries}))}), vertex: {module: gpu.module(effect.vertex)}, fragment: {module: gpu.module(effect.fragment), targets: [{format: 'rgba8unorm'}]}, primitive: {topology: 'triangle-list'}, }); gpu.pipelines.set(key, pipeline); } gpu.postSampler ??= gpu.device.createSampler({minFilter: 'nearest', magFilter: 'nearest'}); const entries = [{binding: 0, resource: input.texture.createView()}, {binding: 1, resource: gpu.postSampler}]; if (effect.sample_depth) entries.push( {binding: 2, resource: input.attachments['stencil 1'].createView({aspect: 'depth-only'})}, {binding: 3, resource: gpu.postSampler}, ); const uniforms = gpu.uniforms(effect.fragment_uniforms); const commands = gpu.device.createCommandEncoder(); const pass = commands.beginRenderPass({colorAttachments: [{view: output.texture.createView(), loadOp: 'clear', storeOp: 'store'}]}); pass.setPipeline(pipeline); gpu.bind(pass, pipeline, [[], [], entries, gpu.uniform(uniforms)]); pass.draw(3); pass.end(); gpu.device.queue.submit([commands.finish()]); output.depthValid = false; }, // Reads a buffer back; it reaches the sketch before a later frame. arrived: [], download(id, offset, size, key) { gpu.read(id, offset, size, bytes => gpu.arrived.push({key, bytes})); }, read(id, offset, size, then) { 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(() => { then(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) { // `waiting`: GPU buffers on their way, in order (counted as queued). if (!sound.queues.has(key)) sound.queues.set(key, {chunks: [], waiting: [], 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 ` `. 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 = []; } // Draws `source` (or its `sw` x `sh` region at `sx`, `sy`) into `x, y, w, // h`, its colour multiplied by `r, g, b` as SDL's colour mod does, which // canvas compositing cannot do exactly where the image is translucent. So // the pixels are multiplied one by one into a scratch canvas kept per // image, and kept while the tint, region and image stay the same (an // animated alpha costs nothing: that is `globalAlpha`). A GPU pass's output // changes every frame and cannot be read cheaply: it goes through the // compositing approximation, exact where it is opaque. function drawTinted(entry, source, x, y, w, h, sx, sy, sw, sh, r, g, b) { if (r === 255 && g === 255 && b === 255) { if (sw > 0) ctx.drawImage(source, sx, sy, sw, sh, x, y, w, h); else ctx.drawImage(source, x, y, w, h); return; } const iw = sw > 0 ? Math.ceil(sw) : source.width, ih = sh > 0 ? Math.ceil(sh) : source.height; if (!iw || !ih) return; const region = sw > 0 ? [sx, sy, sw, sh] : [0, 0, source.width, source.height]; const key = `${r},${g},${b},${region},${entry.version ?? 0}`; const scratch = entry.tinted ??= document.createElement('canvas'); if (entry.texture || entry.tintKey !== key) { if (scratch.width !== iw || scratch.height !== ih) { scratch.width = iw; scratch.height = ih; } const t = scratch.getContext('2d', {willReadFrequently: !entry.texture}); t.globalCompositeOperation = 'copy'; t.drawImage(source, ...region, 0, 0, iw, ih); if (entry.texture) { t.globalCompositeOperation = 'multiply'; t.fillStyle = `rgb(${r},${g},${b})`; t.fillRect(0, 0, iw, ih); t.globalCompositeOperation = 'destination-in'; t.drawImage(source, ...region, 0, 0, iw, ih); } else { const pixels = t.getImageData(0, 0, iw, ih), d = pixels.data; for (let i = 0; i < d.length; i += 4) { d[i] = d[i] * r / 255; d[i + 1] = d[i + 1] * g / 255; d[i + 2] = d[i + 2] * b / 255; } t.putImageData(pixels, 0, 0); } entry.tintKey = entry.texture ? null : key; } ctx.drawImage(scratch, 0, 0, iw, ih, x, y, w, h); } // Command.image: as on native, a GPU pass's output scales smoothly, an // image nearest; `tint` multiplies, its alpha as `globalAlpha`. `place` // sets the transform `x, y, w, h` are drawn under. function drawImage(id, x, y, w, h, sx, sy, sw, sh, r, g, b, a, place) { const entry = images[id]; if (id && entry === drawing) return; // Drawing into itself: nothing. const ready = entry?.texture || (entry?.source && !(entry.source instanceof Image && !entry.w)); ctx.save(); if (place) ctx.transform(...place); if (!ready) { ctx.fillStyle = '#6e1e46'; ctx.fillRect(x, y, w, h); } else { if (entry.texture) gpu.present(entry); ctx.globalAlpha = a / 255; ctx.imageSmoothingEnabled = !!entry.texture; drawTinted(entry, entry.texture ? entry.shown : entry.source, x, y, w, h, sx, sy, sw, sh, r, g, b); } ctx.restore(); } 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: { // Replaces, ignoring the blend, as SDL's clear does; into an image with // its alpha too. clear(r, g, b, a) { ctx.save(); ctx.globalCompositeOperation = drawing ? 'copy' : 'source-over'; ctx.fillStyle = rgba(r, g, b, a); ctx.fillRect(0, 0, drawing ? drawing.w : size[0], drawing ? drawing.h : size[1]); ctx.restore(); }, 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, r, g, b, a) { drawImage(id, x, y, w, h, sx, sy, sw, sh, r, g, b, a, null); }, // The unit square onto the three corners, and the image into it. imageCorners(id, x0, y0, x1, y1, x2, y2, sx, sy, sw, sh, r, g, b, a) { drawImage(id, 0, 0, 1, 1, sx, sy, sw, sh, r, g, b, a, [x1 - x0, y1 - y0, x2 - x0, y2 - y0, x0, y0]); }, // Command.target: later 2D commands draw into the image's canvas (at // its pixels, 1:1), or the screen for 0. Unclips, as native does. target(id) { if (clipped) { ctx.restore(); clipped = false; } const entry = id ? images[id] : null; if (id && !entry?.ctx) return; ctx = entry ? entry.ctx : screen; drawing = entry; if (entry) { entry.version = (entry.version ?? 0) + 1; ctx.imageSmoothingEnabled = false; } ctx.globalCompositeOperation = composite; }, // Command.blend, by abi.BlendMode value. blend(mode) { composite = ['source-over', 'lighter'][mode] ?? 'source-over'; ctx.globalCompositeOperation = composite; }, // Command.shape: its subpaths filled nonzero (the triangles are for // native), one path per positively wound subpath together with the // negatively wound ones after it (its holes). One fill per shape, as // native blends once per triangle: shapes batched into one command // that overlap blend twice on both hosts. Canvas2D also slows down // badly on one path of thousands of subpaths (5000 circles: 200 ms // as one path, 9 ms as 5000). // With `colors`, each group fills in its first subpath's colour. shape(ptr, len, endsPtr, endsLen, colorsPtr, colorsLen, r, g, b, a) { if (!len || !endsLen) return; const p = new Float32Array(wasm.memory.buffer, ptr, len * 2); const ends = new Uint32Array(wasm.memory.buffer, endsPtr, endsLen); const colors = colorsLen ? new Uint8Array(wasm.memory.buffer, colorsPtr, colorsLen * 4) : null; const style = k => k < colorsLen ? rgba(colors[4 * k], colors[4 * k + 1], colors[4 * k + 2], colors[4 * k + 3]) : rgba(r, g, b, a); ctx.fillStyle = style(0); ctx.beginPath(); let start = 0, open = false; for (let k = 0; k < endsLen; k++) { const end = Math.min(ends[k], len); if (end - start >= 3) { let area = 0; for (let i = start, j = end - 1; i < end; j = i++) area += p[2 * j] * p[2 * i + 1] - p[2 * i] * p[2 * j + 1]; if (area > 0 && open) { ctx.fill('nonzero'); ctx.beginPath(); } if (area > 0 && colors) ctx.fillStyle = style(k); ctx.moveTo(p[2 * start], p[2 * start + 1]); for (let i = start + 1; i < end; i++) ctx.lineTo(p[2 * i], p[2 * i + 1]); ctx.closePath(); open = true; } start = Math.max(start, end); } if (open) ctx.fill('nonzero'); }, // Always blended alpha, as native text is. text(x, y, scale, ptr, len, r, g, b, a) { ctx.save(); ctx.globalCompositeOperation = 'source-over'; 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); ctx.restore(); }, 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); entry.version = (entry.version ?? 0) + 1; }, }, 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) { const buffer = gpu.buffers[id]; if (!gpu.device || !buffer || offset % 4 || len % 4 || offset + len > buffer.size) return 0; try { gpu.device.queue.writeBuffer(buffer, offset, bytes(ptr, len).slice()); return 1; } catch { return 0; } }, 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))); }, postProcess(ptr, len) { if (gpu.device) gpu.postProcess(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.waiting = []; q.at = 0; q.queued = 0; } if (len) { q.chunks.push(new Float32Array(wasm.memory.buffer.slice(ptr, ptr + len * 4))); q.queued += len; } }, buffer(key, id, offset, count) { if (!gpu.buffers[id]) return; sound.hint(); const q = sound.queue(key); const chunk = {samples: null}; q.waiting.push(chunk); q.queued += count; gpu.read(id, offset, count * 4, bytes => { chunk.samples = new Float32Array(bytes.buffer); while (q.waiting.length && q.waiting[0].samples) q.chunks.push(q.waiting.shift().samples); }); }, 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, // Runs one frame now, at `ms` on the page's clock, for tests that time // or compare frames without waiting for the display. frame(ms) { frameNumber++; deliver(); wasm.sk_frame(ms); }, // '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); }