add #pragma branching
This commit is contained in:
parent
89759068e5
commit
3376c8a22e
3 changed files with 29 additions and 9 deletions
File diff suppressed because one or more lines are too long
|
@ -2,9 +2,10 @@ import { WebGPUApp } from "../renderer/webgpu";
|
||||||
import { MeshRenderer } from "../renderer/mesh-renderer";
|
import { MeshRenderer } from "../renderer/mesh-renderer";
|
||||||
import plane from "../meshes/plane";
|
import plane from "../meshes/plane";
|
||||||
import rainbowPlane from "./rainbow-plane.wgsl";
|
import rainbowPlane from "./rainbow-plane.wgsl";
|
||||||
|
import { Shader } from "../renderer/shader";
|
||||||
|
|
||||||
const app = new WebGPUApp({ fov: 20 });
|
const app = new WebGPUApp({ fov: 20 });
|
||||||
|
|
||||||
const renderer = new MeshRenderer(app, plane);
|
const renderer = new MeshRenderer(app, plane);
|
||||||
console.log({ rainbowPlane });
|
const shader = new Shader(rainbowPlane);
|
||||||
app.start();
|
app.start();
|
||||||
|
|
|
@ -1,19 +1,38 @@
|
||||||
import { WebGPUApp } from "./webgpu";
|
import { WebGPUApp } from "./webgpu";
|
||||||
import oopsWsgl from "./oops.wgsl";
|
import oopsWsgl from "./oops.wgsl";
|
||||||
|
|
||||||
|
const pragmaRegexp = new RegExp("#pragma ([a-z]+) ([a-zA-Z_0-9]+)", "g");
|
||||||
|
|
||||||
export class Shader {
|
export class Shader {
|
||||||
private _module: GPUShaderModule | null = null;
|
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) {
|
module(app: WebGPUApp) {
|
||||||
this._module =
|
return (this._module =
|
||||||
this._module ||
|
this._module ||
|
||||||
(this._module = app.device.createShaderModule({
|
(this._module = app.device.createShaderModule({
|
||||||
code: this.code.join("\n"),
|
code: this.code,
|
||||||
}));
|
})));
|
||||||
|
|
||||||
return this._module;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Oops = new Shader([oopsWsgl]);
|
export const Oops = new Shader(oopsWsgl);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue