WGSL Shader Preprocessor Brittle

I tried writing a vertex shader with WGSL, like in the tutorials. However, I very quickly ran into an odd issue, which can be reproduced here

fn main brittle parsing | Babylon.js Playground (babylonjs.com)

It turns out that Babylon does some very brittle shader preprocessing (pretty please could we get a proper parser with a syntax tree) and will break as soon as there is another function that happens to start with fn main above the actual main function. fn mainImage() was the function in my case.

We could look for fn main( to make your example work, but then, writing your main function like fn main (... (with a space between n and () would fail… The regexp could still be improved, but there’s no good solution, except implementing a parser with a syntax tree like you said.

However, it’s not a small task, and would add to the size of the codebase. I’m not sure it’s something that is planned, as we already used a string parser for WebGL.

cc @sebavan for his opinion.

I d suggest to only fix the parser for now with a better regex.

I am also thinking less and less processing of the wgsl would be for the best :slight_smile: