-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Template variable support #7
Comments
@yuri-karadzhov Surely your 'template variables' are something that lies outside the scope of standard GLSL? So it would seem a bit out of scope, or I suppose there could be some way of allowing the user to define extensions to the normal grammar... but I think it's out of scope, realistically. Whatever custom pre-processor interprets It's possible that what you're talking about could be done more 'legally' with something like |
@xinaesthete Actually as shaders for webgl compiled in run time that could not be preperocessed before glslx. |
(you're wrong, you have control at runtime of what happens to the strings you feed to WebGL, which could include some pre-processing, and if you're using |
Let me explain my thought. Let's say I want to pass $x to my template at runtime (just before shader compilation), but I want to check and minify template during compilation (compilation of web project, not shaders, when shader text turn into template function or string) then there is no way to do it with glslx. Probably I will create yet another webgl parser, because no one seems to be able to support templates. |
As I understand it, your pipeline should be something like
Actually, my apologies; so the thing is that you want to serve code which is minified, but checked... I'll update in a sec... |
Sure it will like this, but I need to have a template in runtime to compile shaders dynamically. So on the client side I will have a shader factory, that I can pas template value to and it will generate shader code to me, that I compile and use in a program.
|
You could always write a post-processor to re-introduce templates to the minified code. It seems to me that it's either that, or the parser needs to have some kind of way of specifying custom extensions to the grammar. Actually, I seem to remember noticing commit comments along the lines of adding support for |
Come to think of it, maybe |
ok... thinking again, I guess you mean templates as in JS template literals |
It's from my loder for webpack https://github.com/yuri-karadzhov/glsl-template-loader, template Yeah, it seems like it's possible to define a type of |
Yeah, it could be a function or just template string. The transpiller does the job to create one or another. In source code I use |
You could maybe keep a record of what all the transformed vars are and transform them back as needed... so on the webpack side you give each substituted template a unique id, which you save as metadata in a dictionary where that id is key and your desired template string is the value, so that you can then on the client side substitute each id back with the template string, and then with the value that you want to substitute at runtime before finally passing to the compiler. I think my logic is more-or-less right, but that might be hard to parse (for you as a human). |
I can transform them to template function with the dictionary immediately after glslx check, but what if glslx inline some vars or change the name during minification? What initial values should I gave this variable to make them wotk with glslx? I mean let's say I have a template attribute vec2 a_Position;
attribute vec3 a_Color;
varying vec3 v_Color;
void main(void) {
v_Color = a_Color * $mult;
gl_Position = vec4(a_Position, 0.0, 1.0);
} Loader will transform it to a function that takes a value and returns a shader code like this: const vCode = vertShader({mult: 0.5}); Where should I move the definition of |
This is turning a bit off-topic really, but never mind. I hope it's somewhat helpful, I've spent rather longer thinking about it than I mean to... probably helps myself think about some related stuff anyway... One approach that could be sound would be to turn You raise a good point that glslx could inline vars or change names... I had feared that, to quote The Big Lebowski again, "You're entering a world of pain". Although, looking at glslx output, I see that it gives a 'renaming' value similar to the dictionary I described. So that's not such a show-stopper at all. // --- prepended template declarations, to be deleted when re-substituted(?) -- //
// making them uniforms at least stops them from being inlined...
uniform float t_mult;
attribute vec2 a_Position;
attribute vec3 a_Color;
varying vec3 v_Color;
void main(void) {
v_Color = a_Color * t_mult;
gl_Position = vec4(a_Position, 0.0, 1.0);
} glslx will turn that into {
"shaders": [
{
"name": "main",
"contents": "uniform float a;attribute vec2 b;attribute vec3 c;varying vec3 d;void main(){d=c*a,gl_Position=vec4(b,0.,1.);}"
}
],
"renaming": {
"t_mult": "a",
"a_Position": "b",
"a_Color": "c"
}
} Maybe if you make your template symbol be something like I'm afraid the devil is in the details and of course I won't be able to give you a full solution... one concern is how you deal with different glsl types... actually, in your example I'm really not intimately familiar with glslx; I just tried it and saw that it choked on |
Is it possible to add support of template variables like
$x
(variable symbol can be configurable) to work with shader templates. At the moment parser returns syntax error on symbol$
The text was updated successfully, but these errors were encountered: