add #pragma branching

This commit is contained in:
41666 2023-10-08 00:19:24 -04:00
parent 89759068e5
commit 3376c8a22e
3 changed files with 29 additions and 9 deletions

File diff suppressed because one or more lines are too long

View file

@ -2,9 +2,10 @@ import { WebGPUApp } from "../renderer/webgpu";
import { MeshRenderer } from "../renderer/mesh-renderer";
import plane from "../meshes/plane";
import rainbowPlane from "./rainbow-plane.wgsl";
import { Shader } from "../renderer/shader";
const app = new WebGPUApp({ fov: 20 });
const renderer = new MeshRenderer(app, plane);
console.log({ rainbowPlane });
const shader = new Shader(rainbowPlane);
app.start();

View file

@ -1,19 +1,38 @@
import { WebGPUApp } from "./webgpu";
import oopsWsgl from "./oops.wgsl";
const pragmaRegexp = new RegExp("#pragma ([a-z]+) ([a-zA-Z_0-9]+)", "g");
export class Shader {
private _module: GPUShaderModule | null = null;
constructor(private code: string[]) {}
public vertexMain: string = "main";
public fragmentMain: string = "main";
public code: string;
constructor(...code: string[]) {
this.code = code.join("\n");
// pragma preprocessing
const matches = this.code.matchAll(pragmaRegexp);
for (const match of matches || []) {
switch (match[1]) {
case "fragment":
this.fragmentMain = match[2];
break;
case "vertex":
this.vertexMain = match[2];
break;
}
}
}
module(app: WebGPUApp) {
this._module =
return (this._module =
this._module ||
(this._module = app.device.createShaderModule({
code: this.code.join("\n"),
}));
return this._module;
code: this.code,
})));
}
}
export const Oops = new Shader([oopsWsgl]);
export const Oops = new Shader(oopsWsgl);