p3nGu1nZz commited on
Commit
cf65f18
·
1 Parent(s): 0741357

change signature of initializeWebGPU

Browse files
Files changed (1) hide show
  1. index.js +16 -10
index.js CHANGED
@@ -1,5 +1,3 @@
1
- // index.js
2
-
3
  import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
4
  import { fetchShaderCode, generateGlyphTextureAtlas, createTextureFromSource } from './wgpu-utility.js';
5
  import { config } from './wgpu-config.js';
@@ -9,23 +7,31 @@ import { createState } from './wgpu-state.js';
9
  import { generateGlyphVerticesForText } from './wgpu-text.js';
10
 
11
  const canvas = document.querySelector('canvas');
12
- const context = canvas.getContext('webgpu');
13
- const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
14
  const state = createState(config);
15
 
16
- async function main() {
17
- const adapter = await navigator.gpu?.requestAdapter();
18
- state.device = await adapter?.requestDevice();
19
- if (!state.device) {
20
  alert('need a browser that supports WebGPU');
21
- return;
22
  }
23
 
 
24
  context.configure({
25
- device: state.device,
26
  format: presentationFormat,
27
  });
28
 
 
 
 
 
 
 
 
 
 
29
  const shaderCode = await fetchShaderCode('shaders.wgsl');
30
  const vertexSize = config.floatsPerVertex * 4;
31
 
 
 
 
1
  import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
2
  import { fetchShaderCode, generateGlyphTextureAtlas, createTextureFromSource } from './wgpu-utility.js';
3
  import { config } from './wgpu-config.js';
 
7
  import { generateGlyphVerticesForText } from './wgpu-text.js';
8
 
9
  const canvas = document.querySelector('canvas');
 
 
10
  const state = createState(config);
11
 
12
+ async function initializeWebGPU(navigator, adapter, canvas) {
13
+ const context = canvas.getContext('webgpu');
14
+ const device = await adapter?.requestDevice();
15
+ if (!device) {
16
  alert('need a browser that supports WebGPU');
17
+ return { device: null, context: null, presentationFormat: null };
18
  }
19
 
20
+ const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
21
  context.configure({
22
+ device,
23
  format: presentationFormat,
24
  });
25
 
26
+ return { device, context, presentationFormat };
27
+ }
28
+
29
+ async function main() {
30
+ const adapter = await navigator.gpu?.requestAdapter();
31
+ const { device, context, presentationFormat } = await initializeWebGPU(navigator, adapter, canvas);
32
+ if (!device) return;
33
+
34
+ state.device = device;
35
  const shaderCode = await fetchShaderCode('shaders.wgsl');
36
  const vertexSize = config.floatsPerVertex * 4;
37