glsl icon indicating copy to clipboard operation
glsl copied to clipboard

#if and #ifdef fail to parse

Open arturoc opened this issue 6 years ago • 13 comments

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

arturoc avatar Apr 20 '19 11:04 arturoc

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.

hadronized avatar Apr 22 '19 00:04 hadronized

I’m going to work on this. I will likely provide a new feature for parsing with options.

hadronized avatar Jul 19 '19 17:07 hadronized

that would be great thanks!

arturoc avatar Jul 19 '19 18:07 arturoc

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.

jrmuizel avatar Sep 13 '19 02:09 jrmuizel

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.

hadronized avatar Sep 13 '19 08:09 hadronized

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.

jrmuizel avatar Sep 13 '19 13:09 jrmuizel

See #95.

hadronized avatar Oct 25 '19 15:10 hadronized

#96 is going to bring those to the next release. :)

hadronized avatar Nov 12 '19 13:11 hadronized

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!

arturoc avatar Nov 12 '19 15:11 arturoc

Nah leave it open, we’ll close it whenever we have more test data and usage. You’re very welcome.

hadronized avatar Nov 12 '19 15:11 hadronized

Feel free to tell me whenever you have time if your issue is fixed with glsl-3.0. :slightly_smiling_face:

hadronized avatar Nov 14 '19 18:11 hadronized

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 #

Strum355 avatar Jan 07 '20 14:01 Strum355

Yes, unfortunately, those are currently only supported at the top-level of your shader.

hadronized avatar Jan 07 '20 23:01 hadronized