Skip to content
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

Rusty imports #41

Merged
merged 32 commits into from
Oct 18, 2023
Merged

Rusty imports #41

merged 32 commits into from
Oct 18, 2023

Conversation

robtfm
Copy link
Collaborator

@robtfm robtfm commented Jul 30, 2023

reworks the import mechanism to support more syntax to make it more rusty, and test for item use before importing to determine which imports are modules and which are items, which allows:

  • use rust-style imports
#import bevy_pbr::{pbr_functions::{alpha_discard as discard, pbr}, mesh_bindings}
  • import partial paths:
#import part::of::path
...
path::remainder::function();

which will call to part::of::path::remainder::function

  • use fully qualified paths without importing:
// #import bevy_pbr::pbr_functions
bevy_pbr::pbr_functions::pbr()
  • use imported items without qualifying
#import bevy_pbr::pbr_functions::pbr
// or, for backwards compatibility (i think this style should be deprecated)
// #import bevy_pbr::pbr_functions pbr
...
pbr()

i've spot-checked bevy, will test more thoroughly after some feedback. would very much appreciate tests from people using the crate outside of bevy core - @DGriffin91, @NiseVoid, @Aaron1011

@robtfm
Copy link
Collaborator Author

robtfm commented Jul 31, 2023

fixed

  • "quoted" imports were not parsed correctly
  • an issue where an imported name was also used as a variable/parameter and declared with a trailing colon.
  • source offset was wrong when reporting naga-level errors in top-level shaders

added some further tests to catch regressions.

thanks @NiseVoid for testing and providing an import test case.

@cart
Copy link
Member

cart commented Aug 1, 2023

I'm on board for this direction! This is going to make a lot of people very happy.

@DGriffin91
Copy link

This looks awesome! I'm hoping to check it out this weekend.

@robtfm
Copy link
Collaborator Author

robtfm commented Aug 3, 2023

i've tested properly against the bevy examples as well now (see draft pr linked above), and fixed a couple of minor issues (mainly around reporting errors in import declarations).
edit to add: the bevy examples worked without the wgsl changes in that pr as well.

@DGriffin91
Copy link

@robtfm One odd thing I found is that when using as modules now have to have names that never overlap with variable names. Previously this would work:

#import "shaders/foo.wgsl" as foo
fn func() {
     let foo = vec3(0.0);
}

Now I get this error:

error: expected identifier, found '"'
   ┌─ shaders/glowy.wgsl:47:947let "foo.wgsl" = vec3(0.0);
   │         ^ expected identifier
   │
   = expected identifier, found '"'

But

This still works fine:

#import "shaders/foo.wgsl"::{somefunc}
fn func() {
     let somefunc = vec3(0.0);
}

I'm not sure if the as feature is expected to still be available in the future? I like being able to sometimes import files and use them without picking each thing individually. Ex:

#import "shaders/foo.wgsl" as foo
fn func() {
     let a = foo::somefunc();
}

This does still work currently, just wanted to check that you plan to continue to support this, and if so if it would be possible to avoid the var name issue?

@robtfm
Copy link
Collaborator Author

robtfm commented Aug 8, 2023

One odd thing I found is that when using as modules now have to have names that never overlap with variable names.

fixed, thanks. this happens because we (with very limited context) substitute imported identifiers directly. for imported names without quotes, the replacement is harmless - your local variable gets renamed in all the places it is used. with quoted imports the quoted name is not a valid variable name, so it failed. i've special-cased this.

there will still be potentially misleading errors if your local variables shadow imported names (either raw module/item names or as names), but it won't fail if the shader code is valid.

just wanted to check that you plan to continue to support this

yes, definitely.

Copy link
Member

@cart cart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very happy with these changes!

src/compose/mod.rs Outdated Show resolved Hide resolved
);

let input = r"
#import a::b c, d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this style should be deprecated. Maybe we should print a warning when we identify these?

Copy link
Collaborator Author

@robtfm robtfm Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i added this. i guess that also means we should do a major minor version bump. this pr is not technically breaking but existing users of bevy 0.11 with naga_oil 0.9 would get deprecated warning spam from the console if the warning is on by default (which i think it should be).

Copy link
Member

@cart cart Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think this should land in naga_oil 0.10. I agree the warning should be on by default.

src/compose/parse_imports.rs Show resolved Hide resolved
src/compose/preprocess.rs Show resolved Hide resolved
src/compose/preprocess.rs Show resolved Hide resolved
src/compose/parse_imports.rs Outdated Show resolved Hide resolved
src/compose/parse_imports.rs Outdated Show resolved Hide resolved
@cart cart merged commit ac56a00 into bevyengine:master Oct 18, 2023
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Responded
Development

Successfully merging this pull request may close these issues.

3 participants