@@ -6,13 +6,12 @@ const device = root.device;
6
6
7
7
const canvas = document . querySelector ( 'canvas' ) as HTMLCanvasElement ;
8
8
const context = canvas . getContext ( 'webgpu' ) as GPUCanvasContext ;
9
- const devicePixelRatio = window . devicePixelRatio ;
10
- canvas . width = canvas . clientWidth * devicePixelRatio ;
11
- canvas . height = canvas . clientHeight * devicePixelRatio ;
9
+
12
10
const presentationFormat = navigator . gpu . getPreferredCanvasFormat ( ) ;
13
11
context . configure ( {
14
12
device,
15
13
format : presentationFormat ,
14
+ alphaMode : 'premultiplied' ,
16
15
} ) ;
17
16
18
17
let workgroupSize = 16 ;
@@ -37,6 +36,7 @@ const bindGroupLayoutCompute = tgpu.bindGroupLayout({
37
36
access : 'mutable' ,
38
37
} ,
39
38
} ) ;
39
+
40
40
const bindGroupLayoutRender = tgpu . bindGroupLayout ( {
41
41
size : {
42
42
uniform : d . vec2u ,
@@ -121,20 +121,18 @@ fn frag(@location(0) cell: f32, @builtin(position) pos: vec4f) -> @location(0) v
121
121
}
122
122
123
123
return vec4f(
124
- max(f32(cell) * pos.x / 1024, 0) ,
125
- max(f32(cell) * pos.y / 1024, 0) ,
126
- max(f32(cell) * ( 1 - pos.x / 1024), 0) ,
127
- 1.
124
+ pos.x / 2048 ,
125
+ pos.y / 2048 ,
126
+ 1 - pos.x / 2048 ,
127
+ 0.8
128
128
);
129
129
}` ,
130
130
externals : {
131
131
...bindGroupLayoutRender . bound ,
132
132
} ,
133
133
} ) ,
134
134
} ) ;
135
- let commandEncoder : GPUCommandEncoder ;
136
135
137
- // compute pipeline
138
136
const computePipeline = device . createComputePipeline ( {
139
137
layout : device . createPipelineLayout ( {
140
138
bindGroupLayouts : [ root . unwrap ( bindGroupLayoutCompute ) ] ,
@@ -159,7 +157,7 @@ const resetGameData = () => {
159
157
const length = gameWidth * gameHeight ;
160
158
const cells = Array . from ( { length } )
161
159
. fill ( 0 )
162
- . map ( ( _ , i ) => ( Math . random ( ) < 0.25 ? 1 : 0 ) ) ;
160
+ . map ( ( ) => ( Math . random ( ) < 0.25 ? 1 : 0 ) ) ;
163
161
164
162
const buffer0 = root
165
163
. createBuffer ( d . arrayOf ( d . u32 , length ) , cells )
@@ -191,14 +189,13 @@ const resetGameData = () => {
191
189
colorAttachments : [
192
190
{
193
191
view,
194
- clearValue : { r : 239 / 255 , g : 239 / 255 , b : 249 / 255 , a : 1 } ,
195
192
loadOp : 'clear' ,
196
193
storeOp : 'store' ,
197
194
} ,
198
195
] ,
199
196
} ;
200
197
201
- commandEncoder = device . createCommandEncoder ( ) ;
198
+ const commandEncoder = device . createCommandEncoder ( ) ;
202
199
const passEncoderCompute = commandEncoder . beginComputePass ( ) ;
203
200
204
201
passEncoderCompute . setPipeline ( computePipeline ) ;
@@ -271,7 +268,7 @@ const renderPipeline = device.createRenderPipeline({
271
268
272
269
export const controls = {
273
270
size : {
274
- initial : '1024 ' ,
271
+ initial : '64 ' ,
275
272
options : [ 16 , 32 , 64 , 128 , 256 , 512 , 1024 ] . map ( ( x ) => x . toString ( ) ) ,
276
273
onSelectChange : ( value : string ) => {
277
274
gameWidth = Number . parseInt ( value ) ;
@@ -306,9 +303,14 @@ export const controls = {
306
303
paused = value ;
307
304
} ,
308
305
} ,
306
+
307
+ Reset : {
308
+ onButtonClick : resetGameData ,
309
+ } ,
309
310
} ;
310
311
311
312
export function onCleanup ( ) {
313
+ paused = true ;
312
314
root . destroy ( ) ;
313
315
root . device . destroy ( ) ;
314
316
}
0 commit comments