Spaces:
Running
Running
Update world.js
Browse files
world.js
CHANGED
|
@@ -3,35 +3,50 @@ 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.time = 0;
|
|
|
|
| 10 |
|
| 11 |
this.field = new Float32Array(this.size);
|
| 12 |
|
| 13 |
if (gpu.mode === "gpu") {
|
| 14 |
this.initGPU();
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
}
|
| 17 |
|
| 18 |
async initGPU() {
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
size: this.field.byteLength,
|
| 28 |
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
|
| 29 |
-
});
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
|
| 34 |
update(dt) {
|
|
|
|
|
|
|
| 35 |
this.time += dt;
|
| 36 |
|
| 37 |
if (this.gpu.mode === "gpu") {
|
|
@@ -47,41 +62,43 @@ export class World {
|
|
| 47 |
pass.dispatchWorkgroups(Math.ceil(this.size / 64));
|
| 48 |
pass.end();
|
| 49 |
|
| 50 |
-
encoder.copyBufferToBuffer(
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
if (this.gpu.mode === "gpu") {
|
| 57 |
-
await this.readback.mapAsync(GPUMapMode.READ);
|
| 58 |
-
new Float32Array(this.readback.getMappedRange()).set(this.field);
|
| 59 |
-
this.readback.unmap();
|
| 60 |
} else {
|
| 61 |
-
// CPU fallback
|
| 62 |
for (let i = 0; i < this.size; i++) {
|
| 63 |
-
this.field[i] = Math.sin(this.time + i * 0.
|
| 64 |
}
|
| 65 |
}
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
this.
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
this.ctx.beginPath();
|
| 79 |
-
this.ctx.arc(x,
|
| 80 |
this.ctx.fill();
|
| 81 |
}
|
| 82 |
}
|
| 83 |
|
| 84 |
apply(cmd) {
|
| 85 |
-
//
|
| 86 |
}
|
| 87 |
}
|
|
|
|
| 3 |
|
| 4 |
export class World {
|
| 5 |
constructor(canvas, gpu) {
|
| 6 |
+
this.canvas = canvas;
|
| 7 |
this.ctx = canvas.getContext("2d");
|
| 8 |
this.gpu = gpu;
|
| 9 |
+
|
| 10 |
+
this.size = 512;
|
| 11 |
this.time = 0;
|
| 12 |
+
this.ready = false;
|
| 13 |
|
| 14 |
this.field = new Float32Array(this.size);
|
| 15 |
|
| 16 |
if (gpu.mode === "gpu") {
|
| 17 |
this.initGPU();
|
| 18 |
+
} else {
|
| 19 |
+
this.ready = true;
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
| 23 |
async initGPU() {
|
| 24 |
+
try {
|
| 25 |
+
const device = this.gpu.device;
|
| 26 |
+
|
| 27 |
+
this.buffer = device.createBuffer({
|
| 28 |
+
size: this.field.byteLength,
|
| 29 |
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST
|
| 30 |
+
});
|
| 31 |
|
| 32 |
+
this.readback = device.createBuffer({
|
| 33 |
+
size: this.field.byteLength,
|
| 34 |
+
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
|
| 35 |
+
});
|
| 36 |
|
| 37 |
+
this.pipeline = await createCompute(device, wgsl);
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
this.ready = true;
|
| 40 |
+
} catch (e) {
|
| 41 |
+
console.warn("GPU init failed, falling back to CPU");
|
| 42 |
+
this.gpu.mode = "cpu";
|
| 43 |
+
this.ready = true;
|
| 44 |
+
}
|
| 45 |
}
|
| 46 |
|
| 47 |
update(dt) {
|
| 48 |
+
if (!this.ready) return;
|
| 49 |
+
|
| 50 |
this.time += dt;
|
| 51 |
|
| 52 |
if (this.gpu.mode === "gpu") {
|
|
|
|
| 62 |
pass.dispatchWorkgroups(Math.ceil(this.size / 64));
|
| 63 |
pass.end();
|
| 64 |
|
| 65 |
+
encoder.copyBufferToBuffer(
|
| 66 |
+
this.buffer,
|
| 67 |
+
0,
|
| 68 |
+
this.readback,
|
| 69 |
+
0,
|
| 70 |
+
this.field.byteLength
|
| 71 |
+
);
|
| 72 |
|
| 73 |
+
device.queue.submit([encoder.finish()]);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
} else {
|
|
|
|
| 75 |
for (let i = 0; i < this.size; i++) {
|
| 76 |
+
this.field[i] = Math.sin(this.time + i * 0.02) * 0.5 + 0.5;
|
| 77 |
}
|
| 78 |
}
|
| 79 |
+
}
|
| 80 |
|
| 81 |
+
render() {
|
| 82 |
+
if (!this.ready) return;
|
| 83 |
|
| 84 |
+
const w = this.canvas.width = this.canvas.clientWidth;
|
| 85 |
+
const h = this.canvas.height = this.canvas.clientHeight;
|
| 86 |
|
| 87 |
+
this.ctx.fillStyle = "#000";
|
| 88 |
+
this.ctx.fillRect(0, 0, w, h);
|
| 89 |
+
|
| 90 |
+
this.ctx.fillStyle = "#00ffd0";
|
| 91 |
|
| 92 |
+
for (let i = 0; i < this.size; i += 4) {
|
| 93 |
+
const x = (i / this.size) * w;
|
| 94 |
+
const r = this.field[i] * 18;
|
| 95 |
this.ctx.beginPath();
|
| 96 |
+
this.ctx.arc(x, h / 2, r, 0, Math.PI * 2);
|
| 97 |
this.ctx.fill();
|
| 98 |
}
|
| 99 |
}
|
| 100 |
|
| 101 |
apply(cmd) {
|
| 102 |
+
// PLME hooks here
|
| 103 |
}
|
| 104 |
}
|