glsl
glsl copied to clipboard
#if and #ifdef fail to parse
Any input with #if or #ifdef fails to parse with an error of ErrorKind Custom(0).
eg:
use glsl::parser::{Parse, ParseResult};
use glsl::syntax::TranslationUnit;
fn main() {
let fs = "#define USE_GLOBAL_COLOR 1
uniform vec4 color;
out vec4 out_color;
void main() {
#if USE_GLOBAL_COLOR
out_color = color;
#else
out_color = vec4(1., 0., 0., 1.);
#endif
}";
let parsed = match TranslationUnit::parse_str(fs){
ParseResult::Ok(parsed) => parsed,
ParseResult::Incomplete(_needed) =>
panic!("More data needed to parse shader"),
ParseResult::Err(err) =>
panic!("Error parsing shader: {}", err)
};
}
panics with error Custom(0). removing the ifs makes the parser work correctly
Yes, CPP is not yet supported. Only a very few subset of it is currently supported. I don’t really know whether glsl should support it, or if it should be done via a CPP crate.
I’m going to work on this. I will likely provide a new feature for parsing with options.
that would be great thanks!
Do you have a rough idea how you want to implement preprocessing? It seems difficult to fit into the current parser which doesn't seem to have a separate lexing stage.
Yes it doesn’t. At first I thought to use a different crate for this, because the CPP used is exactly the same as the C one, plus some specific pragma for GLSL.
Using an existing cpp preprocessing crate will need some modification as the GLSL preprocessor allows white space before '#'. There might be other differences as well.
See #95.
#96 is going to bring those to the next release. :)
that's great, haven't had a chance to test it yet but will report as soon as i do if i find any problem. should i close this issue?
thanks for looking into it!
Nah leave it open, we’ll close it whenever we have more test data and usage. You’re very welcome.
Feel free to tell me whenever you have time if your issue is fixed with glsl-3.0. :slightly_smiling_face:
using v4.0.0
[dependencies]
glsl = "4.0.0"
the following fails to parse
void make_drawbuffer() {
#ifdef GL_RENDERER_GEFORCE
vec3 temp = clamp(scenecol, 1.0/65530.0, 65535.0); //NaN fix on nvidia
#else
vec3 temp = clamp(scenecol, 0.0, 65535.0);
#endif
}
with error:
#ifdef GL_RENDERER_GEFORCE
^
expected '}', found #
Yes, unfortunately, those are currently only supported at the top-level of your shader.