p3nGu1nZz commited on
Commit
0870851
·
1 Parent(s): 99b5acd

✨ Updated state initialization and organized code

Browse files
Files changed (3) hide show
  1. index.js +2 -7
  2. wgpu-device.js +11 -5
  3. wgpu-state.js +3 -4
index.js CHANGED
@@ -21,16 +21,12 @@ async function InitializeCanvas(state) {
21
  state.canvas.height = config.canvas.height;
22
 
23
  state.webgpu.adapter = await navigator.gpu.requestAdapter();
24
- const { device, context, presentationFormat } = await initializeDevice(navigator, state.webgpu.adapter, state.canvas);
25
 
26
- if (!device) {
27
  alert('Failed to initialize WebGPU');
28
  return;
29
  }
30
-
31
- state.webgpu.device = device;
32
- state.webgpu.context = context;
33
- state.webgpu.presentationFormat = presentationFormat;
34
  }
35
 
36
  async function InitializeResources(state) {
@@ -107,4 +103,3 @@ async function Main() {
107
  }
108
 
109
  Main();
110
-
 
21
  state.canvas.height = config.canvas.height;
22
 
23
  state.webgpu.adapter = await navigator.gpu.requestAdapter();
24
+ await initializeDevice(state);
25
 
26
+ if (!state.webgpu.device) {
27
  alert('Failed to initialize WebGPU');
28
  return;
29
  }
 
 
 
 
30
  }
31
 
32
  async function InitializeResources(state) {
 
103
  }
104
 
105
  Main();
 
wgpu-device.js CHANGED
@@ -1,11 +1,15 @@
1
  // wgpu-device.js
2
 
3
- export async function initializeDevice(navigator, adapter, canvas) {
4
- const context = canvas.getContext('webgpu');
5
- const device = await adapter?.requestDevice();
 
6
  if (!device) {
7
  alert('need a browser that supports WebGPU');
8
- return { device: null, context: null, presentationFormat: null };
 
 
 
9
  }
10
 
11
  const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
@@ -14,5 +18,7 @@ export async function initializeDevice(navigator, adapter, canvas) {
14
  format: presentationFormat,
15
  });
16
 
17
- return { device, context, presentationFormat };
 
 
18
  }
 
1
  // wgpu-device.js
2
 
3
+ export async function initializeDevice(state) {
4
+ const context = state.canvas.getContext('webgpu');
5
+ const device = await state.webgpu.adapter?.requestDevice();
6
+
7
  if (!device) {
8
  alert('need a browser that supports WebGPU');
9
+ state.webgpu.device = null;
10
+ state.webgpu.context = null;
11
+ state.webgpu.presentationFormat = null;
12
+ return;
13
  }
14
 
15
  const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
 
18
  format: presentationFormat,
19
  });
20
 
21
+ state.webgpu.device = device;
22
+ state.webgpu.context = context;
23
+ state.webgpu.presentationFormat = presentationFormat;
24
  }
wgpu-state.js CHANGED
@@ -3,7 +3,10 @@
3
  export function createState(config) {
4
  return {
5
  webgpu: {
 
6
  device: null,
 
 
7
  pipeline: null,
8
  vertexBuffer: null,
9
  indexBuffer: null,
@@ -11,10 +14,7 @@ export function createState(config) {
11
  texture: null,
12
  sampler: null,
13
  bindGroup: null,
14
- context: null,
15
- presentationFormat: null,
16
  shaderCode: null,
17
- adapter: null
18
  },
19
  matrices: {
20
  uniformValues: new Float32Array(config.floatsInUniformBuffer),
@@ -40,4 +40,3 @@ export function createState(config) {
40
  }
41
  };
42
  }
43
-
 
3
  export function createState(config) {
4
  return {
5
  webgpu: {
6
+ adapter: null,
7
  device: null,
8
+ context: null,
9
+ presentationFormat: null,
10
  pipeline: null,
11
  vertexBuffer: null,
12
  indexBuffer: null,
 
14
  texture: null,
15
  sampler: null,
16
  bindGroup: null,
 
 
17
  shaderCode: null,
 
18
  },
19
  matrices: {
20
  uniformValues: new Float32Array(config.floatsInUniformBuffer),
 
40
  }
41
  };
42
  }