LordXido commited on
Commit
709f56b
·
verified ·
1 Parent(s): c9f1ab0

Update world.js

Browse files
Files changed (1) hide show
  1. world.js +31 -53
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.ready = false;
14
 
15
- this.image = this.ctx.createImageData(this.w, this.h);
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.pixelBuffer = d.createBuffer({
25
- size: this.w * this.h * 4 * 4,
26
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC
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
- if (this.gpu.mode === "gpu") {
48
- const d = this.gpu.device;
49
- d.queue.writeBuffer(this.paramBuffer, 0, new Float32Array([this.time, this.power]));
50
 
51
- const enc = d.createCommandEncoder();
52
- const pass = enc.beginComputePass();
53
 
54
- pass.setPipeline(this.pipeline);
55
- pass.setBindGroup(0, d.createBindGroup({
56
- layout: this.pipeline.getBindGroupLayout(0),
57
- entries: [
58
- { binding: 0, resource: { buffer: this.paramBuffer } },
59
- { binding: 1, resource: { buffer: this.pixelBuffer } }
60
- ]
61
- }));
62
 
63
- pass.dispatchWorkgroups(32, 32);
64
- pass.end();
65
 
66
- enc.copyBufferToBuffer(this.pixelBuffer, 0, this.readback, 0, this.w * this.h * 4 * 4);
67
- d.queue.submit([enc.finish()]);
68
- }
69
  }
70
 
71
  async render() {
72
- if (!this.ready || this.gpu.mode !== "gpu") return;
73
 
74
- await this.readback.mapAsync(GPUMapMode.READ);
75
- const data = new Float32Array(this.readback.getMappedRange());
76
 
77
- for (let i = 0; i < data.length; i++) {
78
- this.image.data[i] = Math.min(255, data[i] * 255);
79
  }
80
 
81
- this.readback.unmap();
82
  this.ctx.putImageData(this.image, 0, 0);
83
  }
84
 
85
  apply(cmd) {
86
- if (cmd.lane === "world" && cmd.payload.power) {
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
  }