bevyengine/naga_oil

Define in module produce error

patriciogonzalezvivo opened this issue · 2 comments

Inside a GLSL module I'm doing

#ifndef FNC_ADD
#define FNC_ADD
float add(in add a, in add b) {
    return a + b;
}
#endif

Produce the following error

ComposerError { 
    inner: DefineInModule(100), 
    source: Constructing { 
        path: "_module.glsl", 
        source: "..."
    }
}

The line that produce the error in question is

...
#define FNC_ADD
...

Is there a way of using the following pattern without errors?

#ifndef SOME_DEFINE
#define SOME_DEFINE
...
#endif
robtfm commented

generally no, there's way of doing that. but it is not necessary either: if you include the same module multiple times you still only get one copy of it in the final output, so this kind of construct isn't necessary.

naga_oil generally works more like rust than like a c-preprocessor. #defines are like rust's #[cfg(..)]. you can specify them directly in shaders but only in the entry-point / top level, otherwise things get very confusing - internally modules are compiled once per ShaderDef set, so allowing modules to also define new ShaderDefs is not really tractable. normally they are specified externally and passed to Composer::make_naga_module.

also fyi - using error.emit_to_string(&composer) will give you more readable errors.

Ahh. Got it!
Thanks so much again for the promptly replay. Also thanks for the tip about how to get better error messages.