p3nGu1nZz commited on
Commit
b27f287
1 Parent(s): 1614d87

Modularize State and Timing Initialization

Browse files
Files changed (3) hide show
  1. index.js +4 -33
  2. wgpu-buffer.js +26 -0
  3. wgpu-timing.js +14 -0
index.js CHANGED
@@ -8,6 +8,8 @@ import { GenerateVertexDataAndTexture } from './wgpu-texture.js';
8
  import { generateGlyphVerticesForText } from './wgpu-text.js';
9
  import { config } from './wgpu-config.js';
10
  import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
 
 
11
 
12
  // Canvas element for rendering
13
  const canvas = document.querySelector('canvas');
@@ -26,10 +28,7 @@ async function Main() {
26
  state.webgpu.presentationFormat = presentationFormat;
27
 
28
  // Initialize timing properties
29
- state.timing.fixedDeltaTime = 1 / 60;
30
- state.timing.maxFrameTime = 0.25;
31
- state.timing.targetFps = 60;
32
- state.timing.frameDuration = 1000 / 60;
33
 
34
  // Initialize Resources
35
  await InitializeResources();
@@ -48,40 +47,12 @@ async function InitializeResources() {
48
  document.body.appendChild(glyphCanvas);
49
  glyphCanvas.style.backgroundColor = '#222';
50
 
51
- CreateBuffers();
52
 
53
  GenerateVertexDataAndTexture(state, glyphCanvas, generateGlyphVerticesForText, COLORS, config, createTextureFromSource);
54
  }
55
 
56
- function CreateBuffers() {
57
- const vertexBufferSize = config.maxGlyphs * config.vertsPerGlyph * config.floatsPerVertex * 4;
58
- state.webgpu.vertexBuffer = state.webgpu.device.createBuffer({
59
- label: 'vertices',
60
- size: vertexBufferSize,
61
- usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
62
- });
63
-
64
- state.webgpu.indexBuffer = state.webgpu.device.createBuffer({
65
- label: 'indices',
66
- size: config.maxGlyphs * config.vertsPerGlyph * 4,
67
- usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
68
- });
69
-
70
- const indices = GenerateIndices(config.maxGlyphs);
71
- state.webgpu.device.queue.writeBuffer(state.webgpu.indexBuffer, 0, new Uint32Array(indices));
72
- }
73
-
74
- function GenerateIndices(maxGlyphs) {
75
- return Array.from({ length: maxGlyphs * 6 }, (_, i) => {
76
- const ndx = Math.floor(i / 6) * 4;
77
- return (i % 6 < 3 ? [ndx, ndx + 1, ndx + 2] : [ndx + 2, ndx + 1, ndx + 3])[i % 3];
78
- });
79
- }
80
-
81
  function GameLoop() {
82
- state.timing.lastTime = performance.now();
83
- state.timing.accumulator = 0;
84
-
85
  function Tick() {
86
  state.timing.currentTime = performance.now();
87
  state.timing.frameTime = (state.timing.currentTime - state.timing.lastTime) / 1000;
 
8
  import { generateGlyphVerticesForText } from './wgpu-text.js';
9
  import { config } from './wgpu-config.js';
10
  import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
11
+ import { CreateBuffers } from './wgpu-buffer.js';
12
+ import { initializeTiming } from './wgpu-timing.js';
13
 
14
  // Canvas element for rendering
15
  const canvas = document.querySelector('canvas');
 
28
  state.webgpu.presentationFormat = presentationFormat;
29
 
30
  // Initialize timing properties
31
+ initializeTiming(state);
 
 
 
32
 
33
  // Initialize Resources
34
  await InitializeResources();
 
47
  document.body.appendChild(glyphCanvas);
48
  glyphCanvas.style.backgroundColor = '#222';
49
 
50
+ CreateBuffers(state, config);
51
 
52
  GenerateVertexDataAndTexture(state, glyphCanvas, generateGlyphVerticesForText, COLORS, config, createTextureFromSource);
53
  }
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  function GameLoop() {
 
 
 
56
  function Tick() {
57
  state.timing.currentTime = performance.now();
58
  state.timing.frameTime = (state.timing.currentTime - state.timing.lastTime) / 1000;
wgpu-buffer.js ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // wgpu-buffer.js
2
+
3
+ export function CreateBuffers(state, config) {
4
+ const vertexBufferSize = config.maxGlyphs * config.vertsPerGlyph * config.floatsPerVertex * 4;
5
+ state.webgpu.vertexBuffer = state.webgpu.device.createBuffer({
6
+ label: 'vertices',
7
+ size: vertexBufferSize,
8
+ usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
9
+ });
10
+
11
+ state.webgpu.indexBuffer = state.webgpu.device.createBuffer({
12
+ label: 'indices',
13
+ size: config.maxGlyphs * config.vertsPerGlyph * 4,
14
+ usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
15
+ });
16
+
17
+ const indices = GenerateIndices(config.maxGlyphs);
18
+ state.webgpu.device.queue.writeBuffer(state.webgpu.indexBuffer, 0, new Uint32Array(indices));
19
+ }
20
+
21
+ export function GenerateIndices(maxGlyphs) {
22
+ return Array.from({ length: maxGlyphs * 6 }, (_, i) => {
23
+ const ndx = Math.floor(i / 6) * 4;
24
+ return (i % 6 < 3 ? [ndx, ndx + 1, ndx + 2] : [ndx + 2, ndx + 1, ndx + 3])[i % 3];
25
+ });
26
+ }
wgpu-timing.js ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // wgpu-timing.js
2
+
3
+ export function initializeTiming(state) {
4
+ state.timing.fixedDeltaTime = 1 / 60;
5
+ state.timing.maxFrameTime = 0.25;
6
+ state.timing.targetFps = 60;
7
+ state.timing.frameDuration = 1000 / 60;
8
+ state.timing.lastTime = performance.now();
9
+ state.timing.accumulator = 0;
10
+ state.timing.currentTime = 0;
11
+ state.timing.frameTime = 0;
12
+ state.timing.deltaTime = 0;
13
+ state.timing.time = 0;
14
+ }