summaryrefslogtreecommitdiff
path: root/web/host.js
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-16 22:02:43 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit1cb36ee39a8bbc2cf61c28003670d760841d6d3e (patch)
treed7d01eb5b3a40920a8f71d091a106561ca56b282 /web/host.js
parent6933f99ddb2d7103515d26838496309a807bba26 (diff)
downloadsketchbook-1cb36ee39a8bbc2cf61c28003670d760841d6d3e.tar.gz
sketchbook-1cb36ee39a8bbc2cf61c28003670d760841d6d3e.zip
gpu: add a post-process pass with optional depth sampling, and describe the executor pardes shares
`f.postProcess(source, target, .{...})` records a fullscreen pass that samples a completed render into a distinct target image: colour at fragment binding 0 and, with `sample_depth`, depth at 1, which needs the source's latest pass to have depth and one sample (missing depth, multisampled depth and feedback loops are rejected). The scene format's version 4 carries it, the executor records it on the caller's command buffer and owns the sampler, the web host does the same with its texture and sampler pairs, and native profiling reports the work as `postprocess`. The README says how to use it. docs/pardes-rendering.md describes the executor as pardes uses it for addon scenes: a borrowed SDL GPU device, `record` into the caller's command buffer, `run`, `output` and `postProcess`. The working copy as it stood on 2026-11-16; message written by Claude from the diff, at the author's request.
Diffstat (limited to 'web/host.js')
-rw-r--r--web/host.js53
1 files changed, 51 insertions, 2 deletions
diff --git a/web/host.js b/web/host.js
index 7559dfc..efc335d 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
@@ -234,9 +234,11 @@ const gpu = {
};
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,
@@ -263,6 +265,52 @@ 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) {
@@ -604,6 +652,7 @@ const imports = {
},
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: {