Testing hot-swapping renderers, which requires destroying and rebuilding the canvas after all. Fixed a few other related bugs and moved the imports into "bundle-contents.js".

This commit is contained in:
Rezmason
2025-05-08 12:52:48 -07:00
parent a1332d8f1a
commit 319b53919b
7 changed files with 84 additions and 28 deletions

View File

@@ -75,6 +75,9 @@ export const init = async (canvas) => {
};
export const formulate = async (rain, config) => {
if (rain.destroyed) {
throw new Error("Cannot formulate a destroyed rain instance.");
}
const { resize, canvas, cache, canvasContext, adapter, device } = rain;
rain.resolution = config.resolution;
resize();
@@ -197,10 +200,18 @@ export const formulate = async (rain, config) => {
rain.renderLoop = renderLoop;
};
export const destroy = ({ device, resize, doubleClick, cache, canvas }) => {
export const destroy = (rain) => {
if (rain.destroyed) {
return;
}
const { device, resize, doubleClick, cache, canvas, renderLoop } = rain;
window.removeEventListener("resize", resize);
window.removeEventListener("dblclick", doubleClick);
cache.clear();
tick.cancel(); // stop RAF
cancelAnimationFrame(renderLoop); // stop RAF
// TODO: destroy WebGPU resources
device.destroy();
rain.destroyed = true;
};
export const type = "webgpu";

View File

@@ -29,7 +29,6 @@ const makeConfigBuffer = (device, configUniforms, config, density, gridSize, gly
};
// console.table(configData);
console.log(configUniforms, configData);
return makeUniformBuffer(device, configUniforms, configData);
};