Decoupled naga for wgpu testing (#18099)

# Objective
- Allow bevy and wgpu developers to test newer versions of wgpu without
having to update naga_oil.

## Solution

- Currently bevy feeds wgsl through naga_oil to get a naga::Module that
it passes to wgpu.
- Added a way to pass wgsl through naga_oil, and then serialize the
naga::Module back into a wgsl string to feed to wgpu, allowing wgpu to
parse it using it's internal version of naga (and not the version of
naga bevy_render/naga_oil is using).

## Testing
1. Run 3d_scene (it works)
2. Run 3d_scene with `--features bevy_render/decoupled_naga` (it still
works)
3. Add the following patch to bevy/Cargo.toml, run cargo update, and
compile again (it will fail)
```toml
[patch.crates-io]
wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "2764e7a39920e23928d300e8856a672f1952da63" }
wgpu-core = { git = "https://github.com/gfx-rs/wgpu", rev = "2764e7a39920e23928d300e8856a672f1952da63" }
wgpu-hal = { git = "https://github.com/gfx-rs/wgpu", rev = "2764e7a39920e23928d300e8856a672f1952da63" }
wgpu-types = { git = "https://github.com/gfx-rs/wgpu", rev = "2764e7a39920e23928d300e8856a672f1952da63" }
```
4. Fix errors and compile again (it will work, and you didn't have to
touch naga_oil)
This commit is contained in:
JMS55 2025-03-09 14:49:48 -07:00 committed by GitHub
parent 4cd014384f
commit bb1616a212
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View File

@ -9,6 +9,18 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]
[features]
# Bevy users should _never_ turn this feature on.
#
# Bevy/wgpu developers can turn this feature on to test a newer version of wgpu without needing to also update naga_oil.
#
# When turning this feature on, you can add the following to bevy/Cargo.toml (not this file), and then run `cargo update`:
# [patch.crates-io]
# wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "..." }
# wgpu-core = { git = "https://github.com/gfx-rs/wgpu", rev = "..." }
# wgpu-hal = { git = "https://github.com/gfx-rs/wgpu", rev = "..." }
# wgpu-types = { git = "https://github.com/gfx-rs/wgpu", rev = "..." }
decoupled_naga = []
# Texture formats (require more than just image support)
basis-universal = ["bevy_image/basis-universal"]
dds = ["bevy_image/dds"]

View File

@ -349,7 +349,28 @@ impl ShaderCache {
},
)?;
ShaderSource::Naga(Cow::Owned(naga))
#[cfg(not(feature = "decoupled_naga"))]
{
ShaderSource::Naga(Cow::Owned(naga))
}
#[cfg(feature = "decoupled_naga")]
{
let mut validator = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
self.composer.capabilities,
);
let module_info = validator.validate(&naga).unwrap();
let wgsl = Cow::Owned(
naga::back::wgsl::write_string(
&naga,
&module_info,
naga::back::wgsl::WriterFlags::empty(),
)
.unwrap(),
);
ShaderSource::Wgsl(wgsl)
}
}
};