p3nGu1nZz commited on
Commit
6af9568
·
1 Parent(s): 60ea8d4

🌟 feat: Modularize WebGPU initialization

Browse files
Files changed (2) hide show
  1. index.js +31 -10
  2. wgpu-device.js +3 -3
index.js CHANGED
@@ -1,5 +1,5 @@
1
  import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
2
- import { initializeWebGPU } from './wgpu-device.js';
3
  import { createState } from './wgpu-state.js';
4
  import { initializeTiming } from './wgpu-timing.js';
5
  import { createPipeline } from './wgpu-pipeline.js';
@@ -11,19 +11,35 @@ import { config } from './wgpu-config.js';
11
  import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
12
  import { CreateBuffers } from './wgpu-buffer.js';
13
 
14
- const canvas = document.createElement('canvas');
15
- canvas.width = config.canvas.width;
16
- canvas.height = config.canvas.height;
17
- document.body.appendChild(canvas);
 
 
 
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  const state = createState(config);
20
- state.canvas = canvas;
21
 
22
  async function Main() {
23
- const adapter = await navigator.gpu?.requestAdapter();
24
- const { device, context, presentationFormat } = await initializeWebGPU(navigator, adapter, state.canvas);
25
- if (!device) return;
26
-
27
  state.webgpu.device = device;
28
  state.webgpu.context = context;
29
  state.webgpu.presentationFormat = presentationFormat;
@@ -36,6 +52,7 @@ async function Main() {
36
  GameLoop(state);
37
  }
38
 
 
39
  async function InitializeResources(state) {
40
  const vertexSize = config.floatsPerVertex * 4;
41
 
@@ -50,6 +67,7 @@ async function InitializeResources(state) {
50
  GenerateVertexDataAndTexture(state, glyphCanvas, generateGlyphVerticesForText, COLORS, config, createTextureFromSource);
51
  }
52
 
 
53
  function GameLoop(state) {
54
  function Tick(state) {
55
  state.timing.currentTime = performance.now();
@@ -71,10 +89,12 @@ function GameLoop(state) {
71
  Tick(state);
72
  }
73
 
 
74
  function FixedUpdate(state) {
75
  state.timing.time += state.timing.fixedDeltaTime;
76
  }
77
 
 
78
  function Render(state) {
79
  const fov = 60 * Math.PI / 180;
80
  const aspect = state.canvas.clientWidth / state.canvas.clientHeight;
@@ -96,4 +116,5 @@ function Render(state) {
96
  state.webgpu.device.queue.submit([encoder.finish()]);
97
  }
98
 
 
99
  Main();
 
1
  import { mat4 } from 'https://webgpufundamentals.org/3rdparty/wgpu-matrix.module.js';
2
+ import { initializeDevice } from './wgpu-device.js';
3
  import { createState } from './wgpu-state.js';
4
  import { initializeTiming } from './wgpu-timing.js';
5
  import { createPipeline } from './wgpu-pipeline.js';
 
11
  import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
12
  import { CreateBuffers } from './wgpu-buffer.js';
13
 
14
+ // Initialize Canvas function
15
+ async function InitializeCanvas(config) {
16
+ // Create and configure the canvas element
17
+ const canvas = document.createElement('canvas');
18
+ canvas.width = config.canvas.width;
19
+ canvas.height = config.canvas.height;
20
+ document.body.appendChild(canvas);
21
 
22
+ // Request WebGPU adapter and device
23
+ const adapter = await navigator.gpu.requestAdapter();
24
+
25
+ // Use the existing initializeDevice function
26
+ const { device, context, presentationFormat } = await initializeDevice(navigator, adapter, canvas);
27
+
28
+ if (!device) {
29
+ alert('Failed to initialize WebGPU');
30
+ return;
31
+ }
32
+
33
+ // Return essential components for further setup
34
+ return { canvas, device, context, presentationFormat };
35
+ }
36
+
37
+ // Main function
38
  const state = createState(config);
 
39
 
40
  async function Main() {
41
+ const { canvas, device, context, presentationFormat } = await InitializeCanvas(config);
42
+ state.canvas = canvas;
 
 
43
  state.webgpu.device = device;
44
  state.webgpu.context = context;
45
  state.webgpu.presentationFormat = presentationFormat;
 
52
  GameLoop(state);
53
  }
54
 
55
+ // Initialize Resources function
56
  async function InitializeResources(state) {
57
  const vertexSize = config.floatsPerVertex * 4;
58
 
 
67
  GenerateVertexDataAndTexture(state, glyphCanvas, generateGlyphVerticesForText, COLORS, config, createTextureFromSource);
68
  }
69
 
70
+ // Game Loop function
71
  function GameLoop(state) {
72
  function Tick(state) {
73
  state.timing.currentTime = performance.now();
 
89
  Tick(state);
90
  }
91
 
92
+ // Fixed Update function
93
  function FixedUpdate(state) {
94
  state.timing.time += state.timing.fixedDeltaTime;
95
  }
96
 
97
+ // Render function
98
  function Render(state) {
99
  const fov = 60 * Math.PI / 180;
100
  const aspect = state.canvas.clientWidth / state.canvas.clientHeight;
 
116
  state.webgpu.device.queue.submit([encoder.finish()]);
117
  }
118
 
119
+ // Start the main function
120
  Main();
wgpu-device.js CHANGED
@@ -1,6 +1,6 @@
1
- // wgpu-devices.js
2
 
3
- export async function initializeWebGPU(navigator, adapter, canvas) {
4
  const context = canvas.getContext('webgpu');
5
  const device = await adapter?.requestDevice();
6
  if (!device) {
@@ -15,4 +15,4 @@ export async function initializeWebGPU(navigator, adapter, canvas) {
15
  });
16
 
17
  return { device, context, presentationFormat };
18
- }
 
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) {
 
15
  });
16
 
17
  return { device, context, presentationFormat };
18
+ }