p3nGu1nZz commited on
Commit
a279d3a
·
1 Parent(s): 8288691

🛠️ Refactor pipeline initialization and state handling

Browse files
Files changed (4) hide show
  1. index.js +10 -5
  2. wgpu-config.js +2 -0
  3. wgpu-pipeline.js +12 -15
  4. wgpu-state.js +1 -1
index.js CHANGED
@@ -6,7 +6,7 @@ import { config } from './wgpu-config.js';
6
  import { createState } from './wgpu-state.js';
7
  import { initializeDevice } from './wgpu-device.js';
8
  import { CreateBuffers } from './wgpu-buffer.js';
9
- import { createPipeline } from './wgpu-pipeline.js';
10
 
11
  import { generateGlyphTextureAtlas, createTextureFromSource } from './wgpu-utility.js';
12
  import { InitializeShaders } from './wgpu-shader.js';
@@ -17,23 +17,28 @@ import { generateGlyphVerticesForText } from './wgpu-text.js';
17
  const state = createState(config);
18
 
19
  async function Main() {
 
20
  await InitializeCanvas(state);
21
  await initializeDevice(state);
22
  await InitializeShaders(state);
 
 
 
 
23
  await InitializeResources(state);
24
  GameLoop(state);
25
  }
26
 
 
 
 
 
27
  async function InitializeCanvas(state) {
28
  state.canvas.width = config.canvas.width;
29
  state.canvas.height = config.canvas.height;
30
-
31
- state.webgpu.adapter = await navigator.gpu.requestAdapter();
32
  }
33
 
34
  async function InitializeResources(state) {
35
- state.webgpu.pipeline = await createPipeline(state.webgpu.device, state.webgpu.presentationFormat, state.webgpu.vertexSize, state.webgpu.shaderCode);
36
-
37
  state.webgpu.glyphCanvas = generateGlyphTextureAtlas(CANVAS, CTX, config);
38
  document.body.appendChild(state.webgpu.glyphCanvas);
39
  state.webgpu.glyphCanvas.style.backgroundColor = '#222';
 
6
  import { createState } from './wgpu-state.js';
7
  import { initializeDevice } from './wgpu-device.js';
8
  import { CreateBuffers } from './wgpu-buffer.js';
9
+ import { InitializePipeline } from './wgpu-pipeline.js';
10
 
11
  import { generateGlyphTextureAtlas, createTextureFromSource } from './wgpu-utility.js';
12
  import { InitializeShaders } from './wgpu-shader.js';
 
17
  const state = createState(config);
18
 
19
  async function Main() {
20
+ await InitializeAdapter(state);
21
  await InitializeCanvas(state);
22
  await initializeDevice(state);
23
  await InitializeShaders(state);
24
+ await InitializePipeline(state);
25
+
26
+ state.timing.lastTime = performance.now();
27
+
28
  await InitializeResources(state);
29
  GameLoop(state);
30
  }
31
 
32
+ async function InitializeAdapter(state) {
33
+ state.webgpu.adapter = await navigator.gpu.requestAdapter();
34
+ }
35
+
36
  async function InitializeCanvas(state) {
37
  state.canvas.width = config.canvas.width;
38
  state.canvas.height = config.canvas.height;
 
 
39
  }
40
 
41
  async function InitializeResources(state) {
 
 
42
  state.webgpu.glyphCanvas = generateGlyphTextureAtlas(CANVAS, CTX, config);
43
  document.body.appendChild(state.webgpu.glyphCanvas);
44
  state.webgpu.glyphCanvas.style.backgroundColor = '#222';
wgpu-config.js CHANGED
@@ -1,3 +1,5 @@
 
 
1
  export const config = {
2
  glyphsAcrossTexture: 16,
3
  glyphWidth: 32,
 
1
+ // wgpu-config.js
2
+
3
  export const config = {
4
  glyphsAcrossTexture: 16,
5
  glyphWidth: 32,
wgpu-pipeline.js CHANGED
@@ -1,20 +1,16 @@
1
- // wgpu-pipeline.js
2
-
3
- export async function createPipeline(device, presentationFormat, vertexSize, shaderCode) {
4
- const module = device.createShaderModule({
5
- label: 'textured quad shaders',
6
- code: shaderCode,
7
- });
8
-
9
- const pipeline = device.createRenderPipeline({
10
  label: 'textured quad pipeline',
11
  layout: 'auto',
12
  vertex: {
13
- module,
 
 
 
14
  entryPoint: 'vs',
15
  buffers: [
16
  {
17
- arrayStride: vertexSize,
18
  attributes: [
19
  { shaderLocation: 0, offset: 0, format: 'float32x2' }, // pos
20
  { shaderLocation: 1, offset: 8, format: 'float32x2' }, // tex
@@ -24,10 +20,13 @@ export async function createPipeline(device, presentationFormat, vertexSize, sha
24
  ],
25
  },
26
  fragment: {
27
- module,
 
 
 
28
  entryPoint: 'fs',
29
  targets: [{
30
- format: presentationFormat,
31
  blend: {
32
  color: { srcFactor: 'one', dstFactor: 'one-minus-src-alpha', operation: 'add' },
33
  alpha: { srcFactor: 'one', dstFactor: 'one-minus-src-alpha', operation: 'add' }
@@ -35,6 +34,4 @@ export async function createPipeline(device, presentationFormat, vertexSize, sha
35
  }],
36
  },
37
  });
38
-
39
- return pipeline;
40
  }
 
1
+ export async function InitializePipeline(state) {
2
+ state.webgpu.pipeline = state.webgpu.device.createRenderPipeline({
 
 
 
 
 
 
 
3
  label: 'textured quad pipeline',
4
  layout: 'auto',
5
  vertex: {
6
+ module: state.webgpu.device.createShaderModule({
7
+ label: 'textured quad shaders',
8
+ code: state.webgpu.shaderCode,
9
+ }),
10
  entryPoint: 'vs',
11
  buffers: [
12
  {
13
+ arrayStride: state.webgpu.vertexSize,
14
  attributes: [
15
  { shaderLocation: 0, offset: 0, format: 'float32x2' }, // pos
16
  { shaderLocation: 1, offset: 8, format: 'float32x2' }, // tex
 
20
  ],
21
  },
22
  fragment: {
23
+ module: state.webgpu.device.createShaderModule({
24
+ label: 'textured quad shaders',
25
+ code: state.webgpu.shaderCode,
26
+ }),
27
  entryPoint: 'fs',
28
  targets: [{
29
+ format: state.webgpu.presentationFormat,
30
  blend: {
31
  color: { srcFactor: 'one', dstFactor: 'one-minus-src-alpha', operation: 'add' },
32
  alpha: { srcFactor: 'one', dstFactor: 'one-minus-src-alpha', operation: 'add' }
 
34
  }],
35
  },
36
  });
 
 
37
  }
wgpu-state.js CHANGED
@@ -36,7 +36,7 @@ export function createState(config) {
36
  deltaTime: 0,
37
  currentTime: 0,
38
  frameTime: 0,
39
- lastTime: performance.now()
40
  }
41
  };
42
  }
 
36
  deltaTime: 0,
37
  currentTime: 0,
38
  frameTime: 0,
39
+ lastTime: 0,
40
  }
41
  };
42
  }