Skip to content

Commit ce42367

Browse files
committed
Using Webpack to manage shader code
This should make things a bit easier, though I did like fetching it before...
1 parent 7f30f58 commit ce42367

File tree

6 files changed

+29
-20
lines changed

6 files changed

+29
-20
lines changed

definitions.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module '*.wgsl';

src/renderer.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import vertShaderCode from './shaders/triangle.vert.wgsl';
2+
import fragShaderCode from './shaders/triangle.frag.wgsl';
3+
14
// 📈 Position Vertex Buffer Data
25
const positions = new Float32Array([
36
1.0, -1.0, 0.0,
47
-1.0, -1.0, 0.0,
58
0.0, 1.0, 0.0
69
]);
7-
810
// 🎨 Color Vertex Buffer Data
911
const colors = new Float32Array([
1012
1.0, 0.0, 0.0, // 🔴
@@ -82,7 +84,10 @@ export default class Renderer {
8284
// 🍱 Initialize resources to render triangle (buffers, shaders, pipeline)
8385
async initializeResources() {
8486
// 🔺 Buffers
85-
const createBuffer = (arr: Float32Array | Uint16Array, usage: number) => {
87+
const createBuffer = (
88+
arr: Float32Array | Uint16Array,
89+
usage: number
90+
) => {
8691
// 📏 Align to 4 bytes (thanks @chrimsonite)
8792
let desc = {
8893
size: (arr.byteLength + 3) & ~3,
@@ -104,21 +109,13 @@ export default class Renderer {
104109
this.indexBuffer = createBuffer(indices, GPUBufferUsage.INDEX);
105110

106111
// 🖍️ Shaders
107-
let loadShader = (shaderPath: string) =>
108-
fetch(new Request(shaderPath), {
109-
method: 'GET',
110-
mode: 'cors'
111-
}).then((res) =>
112-
res.text()
113-
);
114-
115112
const vsmDesc = {
116-
code: await loadShader('/assets/shaders/triangle.vert.wgsl')
113+
code: vertShaderCode
117114
};
118115
this.vertModule = this.device.createShaderModule(vsmDesc);
119116

120117
const fsmDesc = {
121-
code: await loadShader('/assets/shaders/triangle.frag.wgsl')
118+
code: fragShaderCode
122119
};
123120
this.fragModule = this.device.createShaderModule(fsmDesc);
124121

@@ -211,7 +208,7 @@ export default class Renderer {
211208
const depthTextureDesc: GPUTextureDescriptor = {
212209
size: [this.canvas.width, this.canvas.height, 1],
213210
dimension: '2d',
214-
format: 'depth24plus-stencil8',
211+
format: 'depth24plus-stencil8',
215212
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC
216213
};
217214

File renamed without changes.
File renamed without changes.

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"allowSyntheticDefaultImports": true
3030
},
3131
"include": [
32+
"definitions.d.ts",
3233
"src/**/*",
3334
"node_modules/@webgpu/types/**/*"
3435
],

webpack.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { argv } from 'process';
55

66
let env = process.env['NODE_ENV'];
77
let isProduction =
8-
(env && env.match(/production/)) || argv.reduce((prev, cur) => prev || cur === '--production', false);
8+
(env && env.match(/production/)) ||
9+
argv.reduce((prev, cur) => prev || cur === '--production', false);
910

1011
let config: webpack.Configuration = {
1112
context: path.join(__dirname, 'src'),
@@ -17,9 +18,8 @@ let config: webpack.Configuration = {
1718
path: path.resolve(__dirname, 'dist')
1819
},
1920
resolve: {
20-
extensions: [ '.ts', '.tsx', 'js' ],
21-
modules: [ path.resolve(__dirname, 'src'), 'node_modules' ],
22-
21+
extensions: ['.ts', '.tsx', 'js'],
22+
modules: [path.resolve(__dirname, 'src'), 'node_modules']
2323
},
2424
module: {
2525
rules: [
@@ -33,6 +33,10 @@ let config: webpack.Configuration = {
3333
isolatedModules: true
3434
}
3535
}
36+
},
37+
{
38+
test: /\.wgsl/,
39+
type: 'asset/source'
3640
}
3741
]
3842
},
@@ -41,7 +45,9 @@ let config: webpack.Configuration = {
4145
new CleanWebpackPlugin(),
4246
new webpack.DefinePlugin({
4347
'process.env': {
44-
NODE_ENV: JSON.stringify(isProduction ? 'production' : 'development')
48+
NODE_ENV: JSON.stringify(
49+
isProduction ? 'production' : 'development'
50+
)
4551
}
4652
})
4753
],
@@ -61,7 +67,9 @@ if (!argv.reduce((prev, cur) => prev || cur === '--watch', false)) {
6167

6268
if (stats.hasErrors()) {
6369
let statsJson = stats.toJson();
64-
console.log('❌' + ' · Error · ' + 'webgpu-seed failed to compile:');
70+
console.log(
71+
'❌' + ' · Error · ' + 'webgpu-seed failed to compile:'
72+
);
6573
for (let error of statsJson.errors) {
6674
console.log(error.message);
6775
}
@@ -82,7 +90,9 @@ if (!argv.reduce((prev, cur) => prev || cur === '--watch', false)) {
8290

8391
if (stats.hasErrors()) {
8492
let statsJson = stats.toJson();
85-
console.log('❌' + ' · Error · ' + 'webgpu-seed failed to compile:');
93+
console.log(
94+
'❌' + ' · Error · ' + 'webgpu-seed failed to compile:'
95+
);
8696
for (let error of statsJson.errors) {
8797
console.log(error.message);
8898
}

0 commit comments

Comments
 (0)