summaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
Diffstat (limited to 'web')
-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: {