Spaces:
Running
Running
✨ Updated state initialization for canvas
Browse files- index.js +5 -14
- wgpu-state.js +4 -0
index.js
CHANGED
@@ -16,28 +16,23 @@ import { InitializeShaders } from './wgpu-shader.js';
|
|
16 |
import { GenerateVertexDataAndTexture } from './wgpu-texture.js';
|
17 |
import { generateGlyphVerticesForText } from './wgpu-text.js';
|
18 |
|
19 |
-
// Initialize Canvas function
|
20 |
async function InitializeCanvas(state) {
|
21 |
-
|
22 |
-
canvas.
|
23 |
-
canvas.height = config.canvas.height;
|
24 |
-
document.body.appendChild(canvas);
|
25 |
|
26 |
-
|
27 |
-
const { device, context, presentationFormat } = await initializeDevice(navigator, adapter, canvas);
|
28 |
|
29 |
if (!device) {
|
30 |
alert('Failed to initialize WebGPU');
|
31 |
return;
|
32 |
}
|
33 |
|
34 |
-
state.canvas = canvas;
|
35 |
state.webgpu.device = device;
|
36 |
state.webgpu.context = context;
|
37 |
state.webgpu.presentationFormat = presentationFormat;
|
38 |
}
|
39 |
|
40 |
-
// Initialize Resources function
|
41 |
async function InitializeResources(state) {
|
42 |
const vertexSize = config.floatsPerVertex * 4;
|
43 |
|
@@ -51,12 +46,10 @@ async function InitializeResources(state) {
|
|
51 |
GenerateVertexDataAndTexture(state, glyphCanvas, generateGlyphVerticesForText, COLORS, config, createTextureFromSource);
|
52 |
}
|
53 |
|
54 |
-
// Fixed Update function
|
55 |
function FixedUpdate(state) {
|
56 |
state.timing.time += state.timing.fixedDeltaTime;
|
57 |
}
|
58 |
|
59 |
-
// Render function
|
60 |
function Render(state) {
|
61 |
const fov = 60 * Math.PI / 180;
|
62 |
const aspect = state.canvas.clientWidth / state.canvas.clientHeight;
|
@@ -83,7 +76,6 @@ function Render(state) {
|
|
83 |
state.webgpu.device.queue.submit([encoder.finish()]);
|
84 |
}
|
85 |
|
86 |
-
// Game Loop function
|
87 |
function GameLoop(state) {
|
88 |
function Tick() {
|
89 |
state.timing.currentTime = performance.now();
|
@@ -104,7 +96,6 @@ function GameLoop(state) {
|
|
104 |
Tick();
|
105 |
}
|
106 |
|
107 |
-
// Main function
|
108 |
const state = createState(config);
|
109 |
|
110 |
async function Main() {
|
@@ -115,5 +106,5 @@ async function Main() {
|
|
115 |
GameLoop(state);
|
116 |
}
|
117 |
|
118 |
-
// Start the main function
|
119 |
Main();
|
|
|
|
16 |
import { GenerateVertexDataAndTexture } from './wgpu-texture.js';
|
17 |
import { generateGlyphVerticesForText } from './wgpu-text.js';
|
18 |
|
|
|
19 |
async function InitializeCanvas(state) {
|
20 |
+
state.canvas.width = config.canvas.width;
|
21 |
+
state.canvas.height = config.canvas.height;
|
|
|
|
|
22 |
|
23 |
+
state.webgpu.adapter = await navigator.gpu.requestAdapter();
|
24 |
+
const { device, context, presentationFormat } = await initializeDevice(navigator, state.webgpu.adapter, state.canvas);
|
25 |
|
26 |
if (!device) {
|
27 |
alert('Failed to initialize WebGPU');
|
28 |
return;
|
29 |
}
|
30 |
|
|
|
31 |
state.webgpu.device = device;
|
32 |
state.webgpu.context = context;
|
33 |
state.webgpu.presentationFormat = presentationFormat;
|
34 |
}
|
35 |
|
|
|
36 |
async function InitializeResources(state) {
|
37 |
const vertexSize = config.floatsPerVertex * 4;
|
38 |
|
|
|
46 |
GenerateVertexDataAndTexture(state, glyphCanvas, generateGlyphVerticesForText, COLORS, config, createTextureFromSource);
|
47 |
}
|
48 |
|
|
|
49 |
function FixedUpdate(state) {
|
50 |
state.timing.time += state.timing.fixedDeltaTime;
|
51 |
}
|
52 |
|
|
|
53 |
function Render(state) {
|
54 |
const fov = 60 * Math.PI / 180;
|
55 |
const aspect = state.canvas.clientWidth / state.canvas.clientHeight;
|
|
|
76 |
state.webgpu.device.queue.submit([encoder.finish()]);
|
77 |
}
|
78 |
|
|
|
79 |
function GameLoop(state) {
|
80 |
function Tick() {
|
81 |
state.timing.currentTime = performance.now();
|
|
|
96 |
Tick();
|
97 |
}
|
98 |
|
|
|
99 |
const state = createState(config);
|
100 |
|
101 |
async function Main() {
|
|
|
106 |
GameLoop(state);
|
107 |
}
|
108 |
|
|
|
109 |
Main();
|
110 |
+
|
wgpu-state.js
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
export function createState(config) {
|
2 |
return {
|
3 |
webgpu: {
|
@@ -12,6 +14,7 @@ export function createState(config) {
|
|
12 |
context: null,
|
13 |
presentationFormat: null,
|
14 |
shaderCode: null,
|
|
|
15 |
},
|
16 |
matrices: {
|
17 |
uniformValues: new Float32Array(config.floatsInUniformBuffer),
|
@@ -37,3 +40,4 @@ export function createState(config) {
|
|
37 |
}
|
38 |
};
|
39 |
}
|
|
|
|
1 |
+
// wgpu-state.js
|
2 |
+
|
3 |
export function createState(config) {
|
4 |
return {
|
5 |
webgpu: {
|
|
|
14 |
context: null,
|
15 |
presentationFormat: null,
|
16 |
shaderCode: null,
|
17 |
+
adapter: null
|
18 |
},
|
19 |
matrices: {
|
20 |
uniformValues: new Float32Array(config.floatsInUniformBuffer),
|
|
|
40 |
}
|
41 |
};
|
42 |
}
|
43 |
+
|