Skip to content

Commit

Permalink
Make Shader(Module)Source::Wgsl optional
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Sep 3, 2022
1 parent 90eb399 commit b9a40e2
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 15 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

env:
RUST_BACKTRACE: 1
RUST_VERSION: 1.59
RUST_VERSION: '1.60'

# We distinguish the following kinds of builds:
# - local: build for the same target as we compile on, and do local tests
Expand Down Expand Up @@ -178,6 +178,9 @@ jobs:
- name: check web
if: matrix.kind == 'web'
run: |
# build with no default features
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu --no-default-features
# build examples
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu --examples
Expand All @@ -202,6 +205,9 @@ jobs:
- name: check native
if: matrix.kind == 'local' || matrix.kind == 'other'
run: |
# check with no default features
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu -p wgpu-core -p wgpu-info -p player --no-default-features
# check with no features
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu -p wgpu-core -p wgpu-info -p player
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ For an overview of all the components in the gfx-rs ecosystem, see [the big pict

### MSRV policy

Minimum Supported Rust Version is **1.59**.
Minimum Supported Rust Version is **1.60**.
It is enforced on CI (in "/.github/workflows/ci.yml") with `RUST_VERSION` variable.
This version can only be upgraded in breaking releases.

Expand Down
6 changes: 4 additions & 2 deletions wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ license = "MIT OR Apache-2.0"
[lib]

[features]
default = []
default = ["wgsl"]
# Apply run-time checks, even in release builds. These are in addition
# to the validation carried out at public APIs in all builds.
strict_asserts = []
Expand All @@ -24,6 +24,8 @@ replay = ["serde", "wgt/replay", "arrayvec/serde", "naga/deserialize"]
# Enable serializable compute/render passes, and bundle encoders.
serial-pass = ["serde", "wgt/serde", "arrayvec/serde"]
id32 = []
# Enable `ShaderModuleSource::Wgsl`
wgsl = ["naga/wgsl-in"]
vulkan-portability = ["hal/vulkan"]

[dependencies]
Expand All @@ -47,7 +49,7 @@ thiserror = "1"
git = "https://github.com/gfx-rs/naga"
rev = "b209d911"
version = "0.9"
features = ["span", "validate", "wgsl-in"]
features = ["span", "validate"]

[dependencies.wgt]
path = "../wgpu-types"
Expand Down
4 changes: 3 additions & 1 deletion wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,9 +1189,11 @@ impl<A: HalApi> Device<A> {
&self,
self_id: id::DeviceId,
desc: &pipeline::ShaderModuleDescriptor<'a>,
source: pipeline::ShaderModuleSource<'a>,
#[cfg(feature = "wgsl")] source: pipeline::ShaderModuleSource<'a>,
#[cfg(not(feature = "wgsl"))] source: pipeline::ShaderModuleSource,
) -> Result<pipeline::ShaderModule<A>, pipeline::CreateShaderModuleError> {
let (module, source) = match source {
#[cfg(feature = "wgsl")]
pipeline::ShaderModuleSource::Wgsl(code) => {
profiling::scope!("naga::wgsl::parse_str");
let module = naga::front::wgsl::parse_str(&code).map_err(|inner| {
Expand Down
6 changes: 5 additions & 1 deletion wgpu-core/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub(crate) struct LateSizedBufferGroup {
}

#[allow(clippy::large_enum_variant)]
pub enum ShaderModuleSource<'a> {
pub enum ShaderModuleSource<#[cfg(feature = "wgsl")] 'a> {
#[cfg(feature = "wgsl")]
Wgsl(Cow<'a, str>),
Naga(naga::Module),
}
Expand Down Expand Up @@ -63,6 +64,7 @@ pub struct ShaderError<E> {
pub label: Option<String>,
pub inner: E,
}
#[cfg(feature = "wgsl")]
impl fmt::Display for ShaderError<naga::front::wgsl::ParseError> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let label = self.label.as_deref().unwrap_or_default();
Expand Down Expand Up @@ -114,6 +116,7 @@ where
//Note: `Clone` would require `WithSpan: Clone`.
#[derive(Debug, Error)]
pub enum CreateShaderModuleError {
#[cfg(feature = "wgsl")]
#[error(transparent)]
Parsing(#[from] ShaderError<naga::front::wgsl::ParseError>),
#[error("Failed to generate the backend-specific code")]
Expand All @@ -137,6 +140,7 @@ pub enum CreateShaderModuleError {
impl CreateShaderModuleError {
pub fn location(&self, source: &str) -> Option<naga::SourceLocation> {
match *self {
#[cfg(feature = "wgsl")]
CreateShaderModuleError::Parsing(ref err) => err.inner.location(source),
CreateShaderModuleError::Validation(ref err) => err.inner.location(source),
_ => None,
Expand Down
4 changes: 3 additions & 1 deletion wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ name = "water"
test = true

[features]
default = []
default = ["wgsl"]
spirv = ["naga/spv-in"]
glsl = ["naga/glsl-in"]
wgsl = ["wgc?/wgsl"]
trace = ["serde", "wgc/trace"]
replay = ["serde", "wgc/replay"]
angle = ["wgc/angle"]
Expand All @@ -90,6 +91,7 @@ vulkan-portability = ["wgc/vulkan-portability"]
package = "wgpu-core"
path = "../wgpu-core"
version = "0.13"
default-features = false
features = ["raw-window-handle"]

[target.'cfg(target_arch = "wasm32")'.dependencies.wgc]
Expand Down
24 changes: 18 additions & 6 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
CommandEncoderDescriptor, ComputePassDescriptor, ComputePipelineDescriptor,
DownlevelCapabilities, Features, Label, Limits, LoadOp, MapMode, Operations,
PipelineLayoutDescriptor, RenderBundleEncoderDescriptor, RenderPipelineDescriptor,
SamplerDescriptor, ShaderModuleDescriptor, ShaderModuleDescriptorSpirV, ShaderSource,
SurfaceStatus, TextureDescriptor, TextureFormat, TextureViewDescriptor,
SamplerDescriptor, ShaderModuleDescriptor, ShaderModuleDescriptorSpirV, SurfaceStatus,
TextureDescriptor, TextureFormat, TextureViewDescriptor,
};

use arrayvec::ArrayVec;
Expand Down Expand Up @@ -1075,6 +1075,15 @@ impl crate::Context for Context {
}
}

#[cfg_attr(
not(any(
feature = "spirv",
feature = "glsl",
feature = "wgsl",
feature = "naga"
)),
allow(unreachable_code, unused_variables)
)]
fn device_create_shader_module(
&self,
device: &Self::DeviceId,
Expand All @@ -1088,7 +1097,7 @@ impl crate::Context for Context {
};
let source = match desc.source {
#[cfg(feature = "spirv")]
ShaderSource::SpirV(ref spv) => {
crate::ShaderSource::SpirV(ref spv) => {
// Parse the given shader code and store its representation.
let options = naga::front::spv::Options {
adjust_coordinate_space: false, // we require NDC_Y_UP feature
Expand All @@ -1100,7 +1109,7 @@ impl crate::Context for Context {
wgc::pipeline::ShaderModuleSource::Naga(module)
}
#[cfg(feature = "glsl")]
ShaderSource::Glsl {
crate::ShaderSource::Glsl {
ref shader,
stage,
ref defines,
Expand All @@ -1115,9 +1124,12 @@ impl crate::Context for Context {

wgc::pipeline::ShaderModuleSource::Naga(module)
}
ShaderSource::Wgsl(ref code) => wgc::pipeline::ShaderModuleSource::Wgsl(Borrowed(code)),
#[cfg(feature = "wgsl")]
crate::ShaderSource::Wgsl(ref code) => {
wgc::pipeline::ShaderModuleSource::Wgsl(Borrowed(code))
}
#[cfg(feature = "naga")]
ShaderSource::Naga(module) => wgc::pipeline::ShaderModuleSource::Naga(module),
crate::ShaderSource::Naga(module) => wgc::pipeline::ShaderModuleSource::Naga(module),
};
let (id, error) = wgc::gfx_select!(
device.id => global.device_create_shader_module(device.id, &descriptor, source, ())
Expand Down
12 changes: 11 additions & 1 deletion wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,13 +1278,22 @@ impl crate::Context for Context {
wgt::DownlevelCapabilities::default()
}

#[cfg_attr(
not(any(
feature = "spirv",
feature = "glsl",
feature = "wgsl",
feature = "naga"
)),
allow(unreachable_code, unused_variables)
)]
fn device_create_shader_module(
&self,
device: &Self::DeviceId,
desc: crate::ShaderModuleDescriptor,
_shader_bound_checks: wgt::ShaderBoundChecks,
) -> Self::ShaderModuleId {
let mut descriptor = match desc.source {
let mut descriptor: web_sys::GpuShaderModuleDescriptor = match desc.source {
#[cfg(feature = "spirv")]
crate::ShaderSource::SpirV(ref spv) => {
use naga::{back, front, valid};
Expand Down Expand Up @@ -1336,6 +1345,7 @@ impl crate::Context for Context {
.unwrap();
web_sys::GpuShaderModuleDescriptor::new(wgsl_text.as_str())
}
#[cfg(feature = "wgsl")]
crate::ShaderSource::Wgsl(ref code) => web_sys::GpuShaderModuleDescriptor::new(code),
#[cfg(feature = "naga")]
crate::ShaderSource::Naga(module) => {
Expand Down
8 changes: 7 additions & 1 deletion wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ impl Drop for ShaderModule {
/// Any necessary shader translation (e.g. from WGSL to SPIR-V or vice versa)
/// will be done internally by wgpu.
#[non_exhaustive]
pub enum ShaderSource<'a> {
pub enum ShaderSource<#[cfg(any(feature = "spirv", feature = "glsl", feature = "wgsl"))] 'a> {
/// SPIR-V module represented as a slice of words.
///
/// See also: [`util::make_spirv`], [`include_spirv`]
Expand All @@ -837,6 +837,8 @@ pub enum ShaderSource<'a> {
defines: naga::FastHashMap<String, String>,
},
/// WGSL module as a string slice.
#[cfg(feature = "wgsl")]
#[cfg_attr(docsrs, doc(cfg(feature = "wgsl")))]
Wgsl(Cow<'a, str>),
/// Naga module.
#[cfg(feature = "naga")]
Expand All @@ -852,7 +854,11 @@ pub struct ShaderModuleDescriptor<'a> {
/// Debug label of the shader module. This will show up in graphics debuggers for easy identification.
pub label: Label<'a>,
/// Source code for the shader.
#[cfg(any(feature = "spirv", feature = "glsl", feature = "wgsl"))]
pub source: ShaderSource<'a>,
/// Source code for the shader.
#[cfg(not(any(feature = "spirv", feature = "glsl", feature = "wgsl")))]
pub source: ShaderSource,
}

/// Descriptor for a shader module given by SPIR-V binary.
Expand Down

0 comments on commit b9a40e2

Please sign in to comment.