Skip to content

Commit

Permalink
Added GLSL to ShaderSource behind Feature lock (#2055)
Browse files Browse the repository at this point in the history
* Added GLSL to ShaderSource behind Feature lock

* Used full path for Naga's FastHashMap and ShaderStage rather than add them as a pub include

* changed spirv and glsl web feature to web-shader-translation. Now requires glsl or spirv to be choosen along with the web-shader-translation to unlock their usage

* Changed the Description of the Glsl enum

* Added README update

* Made With with

* added catch all that panics if web-shader-translation is not enabled with spirv or glsl

* added Compiler condition to prevent the catch all unless glsl or spirv exists without web-shader-translation

* We decided to use [target.'cfg(target_arch = wasm32)'.dependencies.naga] within cargo to include wgsl-out in the Web backend unconditionally, which removed the need for a panic

* Removed wasm targets section from readme as it is no longer needed

* Named Glsl fields to make them more understandable as to what they are for

* Fixed the enum usage for GLSL and Added documentation for the fields
  • Loading branch information
Andrew Wheeler(Genusis) authored Oct 12, 2021
1 parent 5441556 commit 1759722
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ test = true
[features]
default = []
spirv = ["naga/spv-in"]
spirv-web = ["spirv", "naga/wgsl-out"]
glsl = ["naga/glsl-in"]
trace = ["serde", "wgc/trace"]
replay = ["serde", "wgc/replay"]
webgl = ["wgc"]
Expand Down Expand Up @@ -146,6 +146,10 @@ optional = true
version = "0.7"
features = ["wgsl-in"]

[target.'cfg(target_arch = "wasm32")'.dependencies.naga]
version = "0.7"
features = ["wgsl-out"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2.76" # remember to change version in wiki as well
web-sys = { version = "0.3.53", features = [
Expand Down
2 changes: 1 addition & 1 deletion wgpu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ Users can run the [naga](https://github.com/gfx-rs/naga) binary in the following
cargo run -- <input.spv> <output.wgsl>
```

In addition, SPIR-V can be used by enabling the `spirv` feature, or the `spirv-web` feature for wasm targets, at the cost of slightly increased build times.
In addition, SPIR-V can be used by enabling the `spirv` feature and GLSL can be enabled by enabling the `glsl` feature at the cost of slightly increased build times.
16 changes: 16 additions & 0 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,22 @@ impl crate::Context for Context {
let module = parser.parse().unwrap();
wgc::pipeline::ShaderModuleSource::Naga(module)
}
#[cfg(feature = "glsl")]
ShaderSource::Glsl {
ref shader,
stage,
ref defines,
} => {
// Parse the given shader code and store its representation.
let options = naga::front::glsl::Options {
stage,
defines: defines.clone(),
};
let mut parser = naga::front::glsl::Parser::default();
let module = parser.parse(&options, shader).unwrap();

wgc::pipeline::ShaderModuleSource::Naga(module)
}
ShaderSource::Wgsl(ref code) => wgc::pipeline::ShaderModuleSource::Wgsl(Borrowed(code)),
};
let (id, error) = wgc::gfx_select!(
Expand Down
25 changes: 24 additions & 1 deletion wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ impl crate::Context for Context {
_shader_bound_checks: wgt::ShaderBoundChecks,
) -> Self::ShaderModuleId {
let mut descriptor = match desc.source {
#[cfg(feature = "spirv-web")]
#[cfg(feature = "spirv")]
crate::ShaderSource::SpirV(ref spv) => {
use naga::{back, front, valid};

Expand All @@ -1214,6 +1214,29 @@ impl crate::Context for Context {
let wgsl_text = back::wgsl::write_string(&spv_module, &spv_module_info).unwrap();
web_sys::GpuShaderModuleDescriptor::new(wgsl_text.as_str())
}
#[cfg(feature = "glsl")]
ShaderSource::Glsl {
ref shader,
stage,
ref defines,
} => {
// Parse the given shader code and store its representation.
let options = naga::front::glsl::Options {
stage,
defines: defines.clone(),
};
let mut parser = naga::front::glsl::Parser::default();
let glsl_module = parser.parse(&options, shader).unwrap();

let mut validator = valid::Validator::new(
valid::ValidationFlags::all(),
valid::Capabilities::all(),
);
let glsl_module_info = validator.validate(&glsl_module).unwrap();

let wgsl_text = back::wgsl::write_string(&glsl_module, &glsl_module_info).unwrap();
web_sys::GpuShaderModuleDescriptor::new(wgsl_text.as_str())
}
crate::ShaderSource::Wgsl(ref code) => web_sys::GpuShaderModuleDescriptor::new(code),
};
if let Some(label) = desc.label {
Expand Down
15 changes: 15 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,21 @@ pub enum ShaderSource<'a> {
/// is passed to `gfx-rs` and `spirv_cross` for translation.
#[cfg(feature = "spirv")]
SpirV(Cow<'a, [u32]>),
/// GSLS module as a string slice.
///
/// wgpu will attempt to parse and validate it. The module will get
/// passed to wgpu-core where it will translate it to the required languages.
///
/// Note: GLSL is not yet fully supported and must be a direct ShaderStage.
#[cfg(feature = "glsl")]
Glsl {
/// The shaders code
shader: Cow<'a, str>,
/// Stage in which the GLSL shader is for example: naga::ShaderStage::Vertex
stage: naga::ShaderStage,
/// Defines to unlock configured shader features
defines: naga::FastHashMap<String, String>,
},
/// WGSL module as a string slice.
///
/// wgpu-rs will parse it and use for validation. It will attempt
Expand Down

0 comments on commit 1759722

Please sign in to comment.