diff options
Diffstat (limited to 'web/host.js')
| -rw-r--r-- | web/host.js | 143 |
1 files changed, 127 insertions, 16 deletions
diff --git a/web/host.js b/web/host.js index 2c410f7..8213dbd 100644 --- a/web/host.js +++ b/web/host.js @@ -176,7 +176,7 @@ const gpu = { // 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}); + 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 @@ -190,7 +190,7 @@ const gpu = { }, 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]); + 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'}; @@ -212,7 +212,7 @@ const gpu = { primitive: {topology: dashed(p.primitive)}, multisample: {count: samples}, depthStencil: stencil ? { - format: 'depth24plus-stencil8', depthWriteEnabled: false, depthCompare: 'always', + 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, }); @@ -232,11 +232,13 @@ const gpu = { 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 ? { + const depthStencil = p.stencil || p.depth ? { view: gpu.attachment(entry, 'stencil', 'depth24plus-stencil8', samples).createView(), - depthLoadOp: 'clear', depthClearValue: 1, depthStoreOp: 'discard', + 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, @@ -247,7 +249,7 @@ const gpu = { 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); + 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); @@ -263,16 +265,65 @@ const gpu = { 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(() => { - gpu.arrived.push({key, bytes: new Uint8Array(staging.getMappedRange(0, size)).slice()}); + then(new Uint8Array(staging.getMappedRange(0, size)).slice()); staging.unmap(); gpu.spare.push(staging); }, () => {}); @@ -344,7 +395,8 @@ const sound = { } }, queue(key) { - if (!sound.queues.has(key)) sound.queues.set(key, {chunks: [], at: 0, queued: 0}); + // `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() { @@ -467,8 +519,49 @@ function drawImage(id, x, y, w, h, sx, sy, sw, sh, r, g, b, a, place) { ctx.restore(); } +// Text (docs/text.md): sketchbook's faces in the page's fonts, in their +// weight and style, and a sketch's own font a FontFace (`fonts`, its bytes +// kept to give the same face for the same bytes). 13 px at scale 1. +const faceFonts = [['', 'monospace'], ['bold', 'monospace'], ['italic', 'monospace'], ['', 'sans-serif'], ['500', 'sans-serif'], ['bold', 'sans-serif'], ['', 'monospace'], ['bold', 'monospace'], ['italic', 'monospace'], ['500', 'monospace']]; +const fonts = []; +const fontOf = (face, scale) => { + const [style, family] = face < 10 ? faceFonts[face] : ['', fonts[face - 10]?.family ?? 'monospace']; + return `${style} ${13 * scale}px ${family}`.trim(); +}; +const measurer = document.createElement('canvas').getContext('2d'); + const imports = { host: { + // A run's width, ascent and line height, and each byte's x (a byte inside + // a codepoint at its codepoint's): the prefixes measured, so drawn is + // measured, kerning included. + textRun(face, scale, ptr, len, xsPtr, xsLen, outPtr) { + measurer.font = fontOf(face, scale); + const all = bytes(ptr, len).slice(); + const m = measurer.measureText(decoder.decode(all)); + new Float32Array(wasm.memory.buffer, outPtr, 3).set([m.width, m.fontBoundingBoxAscent, m.fontBoundingBoxAscent + m.fontBoundingBoxDescent]); + if (!xsLen) return; + const xs = new Float32Array(wasm.memory.buffer, xsPtr, xsLen); + let x = 0; + for (let i = 0; i < len; i++) { + if ((all[i] & 0xc0) !== 0x80) x = i ? measurer.measureText(decoder.decode(all.subarray(0, i))).width : 0; + xs[i] = x; + } + }, + font(ptr, len) { + const data = bytes(ptr, len).slice(); + const known = fonts.findIndex(f => f.data.length === data.length && f.data.every((b, i) => b === data[i])); + if (known >= 0) return 10 + known; + if (fonts.length === 54) return -1; + const family = `sketchbook-font-${fonts.length}`; + try { + const face = new FontFace(family, data); + document.fonts.add(face); + face.load().catch(err => console.warn(`font ${family}: ${err}`)); + } catch { return -1; } + fonts.push({family, data}); + return 10 + fonts.length - 1; + }, log(ptr, len) { console.log(string(ptr, len)); }, loadImage(ptr, len) { const path = string(ptr, len); @@ -563,15 +656,15 @@ const imports = { if (open) ctx.fill('nonzero'); }, // Always blended alpha, as native text is. - text(x, y, scale, ptr, len, r, g, b, a) { + // A run from its line box's top left, in the face textRun measures it in. + text(x, y, scale, face, 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.font = fontOf(face, scale); + ctx.textBaseline = 'alphabetic'; + const s = string(ptr, len); + ctx.fillText(s, x, y + ctx.measureText(s).fontBoundingBoxAscent); ctx.restore(); }, clip(enabled, x, y, w, h) { @@ -596,9 +689,15 @@ const imports = { 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()); }, + 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: { @@ -606,9 +705,21 @@ const imports = { 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 (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(), }, |
