Spaces:
Running
Running
Update world.js
Browse files
world.js
CHANGED
|
@@ -1,90 +1,68 @@
|
|
| 1 |
-
import wgsl from "./world.wgsl?raw";
|
| 2 |
import { createCompute } from "./gpu.js";
|
|
|
|
| 3 |
|
| 4 |
export class World {
|
| 5 |
constructor(canvas, gpu) {
|
| 6 |
this.ctx = canvas.getContext("2d");
|
| 7 |
this.gpu = gpu;
|
| 8 |
-
|
| 9 |
-
this.w = 256;
|
| 10 |
-
this.h = 256;
|
| 11 |
-
this.time = 0;
|
| 12 |
this.power = 8.0;
|
| 13 |
-
this.
|
| 14 |
|
| 15 |
-
this.image = this.ctx.createImageData(
|
| 16 |
|
| 17 |
if (gpu.mode === "gpu") this.initGPU();
|
| 18 |
-
else this.ready = true;
|
| 19 |
}
|
| 20 |
|
| 21 |
async initGPU() {
|
| 22 |
const d = this.gpu.device;
|
|
|
|
| 23 |
|
| 24 |
-
this.
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
});
|
| 28 |
-
|
| 29 |
-
this.readback = d.createBuffer({
|
| 30 |
-
size: this.w * this.h * 4 * 4,
|
| 31 |
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
|
| 32 |
-
});
|
| 33 |
-
|
| 34 |
-
this.paramBuffer = d.createBuffer({
|
| 35 |
-
size: 8,
|
| 36 |
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
| 37 |
-
});
|
| 38 |
-
|
| 39 |
-
this.pipeline = createCompute(d, wgsl);
|
| 40 |
-
this.ready = true;
|
| 41 |
}
|
| 42 |
|
| 43 |
update(dt) {
|
| 44 |
-
if (!this.ready) return;
|
| 45 |
this.time += dt;
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
d.queue.writeBuffer(this.paramBuffer, 0, new Float32Array([this.time, this.power]));
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
}
|
| 69 |
}
|
| 70 |
|
| 71 |
async render() {
|
| 72 |
-
if (
|
| 73 |
|
| 74 |
-
await this.
|
| 75 |
-
const
|
| 76 |
|
| 77 |
-
for (let i = 0; i <
|
| 78 |
-
this.image.data[i] = Math.min(255,
|
| 79 |
}
|
| 80 |
|
| 81 |
-
this.
|
| 82 |
this.ctx.putImageData(this.image, 0, 0);
|
| 83 |
}
|
| 84 |
|
| 85 |
apply(cmd) {
|
| 86 |
-
if (cmd.
|
| 87 |
-
this.power = cmd.payload.power;
|
| 88 |
-
}
|
| 89 |
}
|
| 90 |
}
|
|
|
|
|
|
|
| 1 |
import { createCompute } from "./gpu.js";
|
| 2 |
+
import shader from "./world.wgsl?raw";
|
| 3 |
|
| 4 |
export class World {
|
| 5 |
constructor(canvas, gpu) {
|
| 6 |
this.ctx = canvas.getContext("2d");
|
| 7 |
this.gpu = gpu;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
this.power = 8.0;
|
| 9 |
+
this.time = 0;
|
| 10 |
|
| 11 |
+
this.image = this.ctx.createImageData(256, 256);
|
| 12 |
|
| 13 |
if (gpu.mode === "gpu") this.initGPU();
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
async initGPU() {
|
| 17 |
const d = this.gpu.device;
|
| 18 |
+
this.pipeline = createCompute(d, shader);
|
| 19 |
|
| 20 |
+
this.param = d.createBuffer({ size: 8, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });
|
| 21 |
+
this.buf = d.createBuffer({ size: 256*256*4*4, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC });
|
| 22 |
+
this.read = d.createBuffer({ size: 256*256*4*4, usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
update(dt) {
|
|
|
|
| 26 |
this.time += dt;
|
| 27 |
+
if (this.gpu.mode !== "gpu") return;
|
| 28 |
|
| 29 |
+
const d = this.gpu.device;
|
| 30 |
+
d.queue.writeBuffer(this.param, 0, new Float32Array([this.time, this.power]));
|
|
|
|
| 31 |
|
| 32 |
+
const enc = d.createCommandEncoder();
|
| 33 |
+
const pass = enc.beginComputePass();
|
| 34 |
|
| 35 |
+
pass.setPipeline(this.pipeline);
|
| 36 |
+
pass.setBindGroup(0, d.createBindGroup({
|
| 37 |
+
layout: this.pipeline.getBindGroupLayout(0),
|
| 38 |
+
entries: [
|
| 39 |
+
{ binding: 0, resource: { buffer: this.param } },
|
| 40 |
+
{ binding: 1, resource: { buffer: this.buf } }
|
| 41 |
+
]
|
| 42 |
+
}));
|
| 43 |
|
| 44 |
+
pass.dispatchWorkgroups(32, 32);
|
| 45 |
+
pass.end();
|
| 46 |
|
| 47 |
+
enc.copyBufferToBuffer(this.buf, 0, this.read, 0, 256*256*4*4);
|
| 48 |
+
d.queue.submit([enc.finish()]);
|
|
|
|
| 49 |
}
|
| 50 |
|
| 51 |
async render() {
|
| 52 |
+
if (this.gpu.mode !== "gpu") return;
|
| 53 |
|
| 54 |
+
await this.read.mapAsync(GPUMapMode.READ);
|
| 55 |
+
const f = new Float32Array(this.read.getMappedRange());
|
| 56 |
|
| 57 |
+
for (let i = 0; i < f.length; i++) {
|
| 58 |
+
this.image.data[i] = Math.min(255, f[i] * 255);
|
| 59 |
}
|
| 60 |
|
| 61 |
+
this.read.unmap();
|
| 62 |
this.ctx.putImageData(this.image, 0, 0);
|
| 63 |
}
|
| 64 |
|
| 65 |
apply(cmd) {
|
| 66 |
+
if (cmd.payload?.power) this.power = cmd.payload.power;
|
|
|
|
|
|
|
| 67 |
}
|
| 68 |
}
|