p3nGu1nZz commited on
Commit
1cc5c8d
1 Parent(s): effc3a3

move init for shaders

Browse files
Files changed (4) hide show
  1. index.js +6 -6
  2. wgpu-resource.js +0 -0
  3. wgpu-shader.js +5 -1
  4. wgpu-state.js +2 -1
index.js CHANGED
@@ -4,7 +4,7 @@ import { createState } from './wgpu-state.js';
4
  import { initializeTiming } from './wgpu-timing.js';
5
  import { createPipeline } from './wgpu-pipeline.js';
6
  import { generateGlyphTextureAtlas, createTextureFromSource } from './wgpu-utility.js';
7
- import { fetchShaderCode } from './wgpu-shader.js';
8
  import { GenerateVertexDataAndTexture } from './wgpu-texture.js';
9
  import { generateGlyphVerticesForText } from './wgpu-text.js';
10
  import { config } from './wgpu-config.js';
@@ -26,7 +26,8 @@ async function Main() {
26
  // Initialize timing properties
27
  initializeTiming(state);
28
 
29
- // Initialize Resources
 
30
  await InitializeResources(state);
31
 
32
  // Start the game loop
@@ -34,10 +35,9 @@ async function Main() {
34
  }
35
 
36
  async function InitializeResources(state) {
37
- const shaderCode = await fetchShaderCode('shaders.wgsl');
38
  const vertexSize = config.floatsPerVertex * 4;
39
 
40
- state.webgpu.pipeline = await createPipeline(state.webgpu.device, state.webgpu.presentationFormat, vertexSize, shaderCode);
41
 
42
  const glyphCanvas = generateGlyphTextureAtlas(CANVAS, CTX, config);
43
  document.body.appendChild(glyphCanvas);
@@ -64,10 +64,10 @@ function GameLoop(state) {
64
 
65
  Render(state);
66
 
67
- setTimeout(Tick, state.timing.frameDuration);
68
  }
69
 
70
- Tick();
71
  }
72
 
73
  function FixedUpdate(deltaTime, state) {
 
4
  import { initializeTiming } from './wgpu-timing.js';
5
  import { createPipeline } from './wgpu-pipeline.js';
6
  import { generateGlyphTextureAtlas, createTextureFromSource } from './wgpu-utility.js';
7
+ import { fetchShaderCode, InitializeShaders } from './wgpu-shader.js';
8
  import { GenerateVertexDataAndTexture } from './wgpu-texture.js';
9
  import { generateGlyphVerticesForText } from './wgpu-text.js';
10
  import { config } from './wgpu-config.js';
 
26
  // Initialize timing properties
27
  initializeTiming(state);
28
 
29
+ // Initialize Shaders and Resources
30
+ await InitializeShaders(state);
31
  await InitializeResources(state);
32
 
33
  // Start the game loop
 
35
  }
36
 
37
  async function InitializeResources(state) {
 
38
  const vertexSize = config.floatsPerVertex * 4;
39
 
40
+ state.webgpu.pipeline = await createPipeline(state.webgpu.device, state.webgpu.presentationFormat, vertexSize, state.webgpu.shaderCode);
41
 
42
  const glyphCanvas = generateGlyphTextureAtlas(CANVAS, CTX, config);
43
  document.body.appendChild(glyphCanvas);
 
64
 
65
  Render(state);
66
 
67
+ setTimeout(() => Tick(state), state.timing.frameDuration);
68
  }
69
 
70
+ Tick(state);
71
  }
72
 
73
  function FixedUpdate(deltaTime, state) {
wgpu-resource.js ADDED
File without changes
wgpu-shader.js CHANGED
@@ -3,4 +3,8 @@
3
  export async function fetchShaderCode(url) {
4
  const response = await fetch(url);
5
  return await response.text();
6
- }
 
 
 
 
 
3
  export async function fetchShaderCode(url) {
4
  const response = await fetch(url);
5
  return await response.text();
6
+ }
7
+
8
+ export async function InitializeShaders(state) {
9
+ state.webgpu.shaderCode = await fetchShaderCode('shaders.wgsl');
10
+ }
wgpu-state.js CHANGED
@@ -11,6 +11,7 @@ export function createState(config) {
11
  bindGroup: null,
12
  context: null,
13
  presentationFormat: null,
 
14
  },
15
  matrices: {
16
  uniformValues: new Float32Array(config.uniformBufferSize / 4),
@@ -21,7 +22,7 @@ export function createState(config) {
21
  width: 0,
22
  height: 0,
23
  },
24
- canvas: null,
25
  timing: {
26
  time: 0,
27
  fixedDeltaTime: 0,
 
11
  bindGroup: null,
12
  context: null,
13
  presentationFormat: null,
14
+ shaderCode: null,
15
  },
16
  matrices: {
17
  uniformValues: new Float32Array(config.uniformBufferSize / 4),
 
22
  width: 0,
23
  height: 0,
24
  },
25
+ canvas: document.querySelector('canvas'),
26
  timing: {
27
  time: 0,
28
  fixedDeltaTime: 0,