bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
35.75k stars 3.53k forks source link

Shader relative imports should support Rusty syntax. #11828

Open viridia opened 8 months ago

viridia commented 8 months ago

What problem does this solve or what need does it fill?

Let's say I have a number of shader assets that use a common set of utility functions, and I want to refactor the code to put the common functions in their own file. It would be nice to be able to use "Rusty" syntax for relative imports:

#import super::common::my_function

This syntax is already supported, but only for exports that are defined by the Bevy engine, not for user-created shader code.

What solution would you like?

Basically, it would be nice to extend support for "Rusty" import syntax for relative paths. If an import path in a shader begins with super::, then we know it's meant to be relative.

I recognize that there may be some complexity due to the fact that shaders accessed by relative paths might be assets, which are loaded differently than shader code defined by Bevy. But for users, having a consistent way to import symbols regardless of where those symbols are located would be a benefit.

One open issue is what to do about shaders that want to refer to other assets by absolute, rather than relative path. crate:: is probably incorrect, since the absolute path for imports is likely to be assets/ folder. It's actually worse in my case, since my shaders are located in an assets folder which in turn is located in a sub-crate, and accessed via an asset root: obsidian_ui://shaders/rounded_rect.wgsl for example.

What alternative(s) have you considered?

Supposedly you can also use import via asset path, e.g. #import "path/to/shader", but I haven't actually gotten this to work, nor have I discovered any documentation on this.

viridia commented 8 months ago

Here's a more general solution: A shader import path could be a combination of Rust and String paths. So the following would all be legal:

viridia commented 7 months ago

BTW, I have not been able to get non-Rusty asset paths to work with asset sources either. For example:

#import "obsidian_ui://shaders/rounded_box.wgsl"::sd_rounded_box

What's annoying is that this doesn't work, but doesn't fail either - it simply reports later down in the file that the symbol sd_rounded_box is not found. The fact that the import statement doesn't work is silently ignored.

infmagic2047 commented 6 months ago

BTW, I have not been able to get non-Rusty asset paths to work with asset sources either. For example:

#import "obsidian_ui://shaders/rounded_box.wgsl"::sd_rounded_box

What's annoying is that this doesn't work, but doesn't fail either - it simply reports later down in the file that the symbol sd_rounded_box is not found. The fact that the import statement doesn't work is silently ignored.

This is because naga_oil recognizes the "//" as comments. See https://github.com/bevyengine/naga_oil/issues/81.