From 81f2f56067f34918eb994b5fd3eb3194e4f7abc8 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Thu, 29 Oct 2020 23:14:01 +0100 Subject: [PATCH 1/5] Mock up shader compilationception --- Cargo.lock | 3 ++- examples/example-runner/Cargo.toml | 4 +--- examples/example-runner/src/main.rs | 2 +- examples/example-shader/Cargo.toml | 7 +++++++ examples/{example-runner => example-shader}/build.rs | 4 +++- examples/example-shader/src/lib.rs | 3 +++ examples/wgpu-example-runner/build.rs | 10 ---------- spirv-builder/src/lib.rs | 6 ++++++ 8 files changed, 23 insertions(+), 16 deletions(-) rename examples/{example-runner => example-shader}/build.rs (73%) delete mode 100644 examples/wgpu-example-runner/build.rs diff --git a/Cargo.lock b/Cargo.lock index 9b91608536..540d3f9b4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -595,7 +595,7 @@ dependencies = [ "ash-molten", "ash-window", "cfg-if 1.0.0", - "spirv-builder", + "example-shader", "structopt", "winit 0.23.0", ] @@ -614,6 +614,7 @@ dependencies = [ name = "example-shader" version = "0.1.0" dependencies = [ + "spirv-builder", "spirv-std", ] diff --git a/examples/example-runner/Cargo.toml b/examples/example-runner/Cargo.toml index 071c9059b9..157bf41cc3 100644 --- a/examples/example-runner/Cargo.toml +++ b/examples/example-runner/Cargo.toml @@ -11,9 +11,7 @@ ash-window = "0.5" cfg-if = "1.0.0" structopt = "0.3.20" winit = "0.23.0" +example-shader = { path = "../example-shader" } [target.'cfg(target_os = "macos")'.dependencies] ash-molten = { git = "https://github.com/EmbarkStudios/ash-molten", branch = "moltenvk-1.1.0" } - -[build-dependencies] -spirv-builder = { path = "../../spirv-builder" } diff --git a/examples/example-runner/src/main.rs b/examples/example-runner/src/main.rs index 285632eae5..4fe1a7c7f4 100644 --- a/examples/example-runner/src/main.rs +++ b/examples/example-runner/src/main.rs @@ -855,7 +855,7 @@ fn main() { base.device .bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0) .unwrap(); - let mut spv_file = Cursor::new(&include_bytes!(env!("example_shader.spv"))[..]); + let mut spv_file = Cursor::new(example_shader::COMPILED_SHADER); let code = read_spv(&mut spv_file).expect("Failed to read spv file"); let shader_info = vk::ShaderModuleCreateInfo::builder().code(&code); diff --git a/examples/example-shader/Cargo.toml b/examples/example-shader/Cargo.toml index 1a723f2a98..22d4b0bd47 100644 --- a/examples/example-shader/Cargo.toml +++ b/examples/example-shader/Cargo.toml @@ -10,3 +10,10 @@ crate-type = ["dylib"] [dependencies] spirv-std = { path = "../../spirv-std" } + +[build-dependencies] +spirv-builder = { path = "../../spirv-builder" } + +[features] +default = ["std"] +std = [] diff --git a/examples/example-runner/build.rs b/examples/example-shader/build.rs similarity index 73% rename from examples/example-runner/build.rs rename to examples/example-shader/build.rs index 5f7b5107d3..f90697e250 100644 --- a/examples/example-runner/build.rs +++ b/examples/example-shader/build.rs @@ -3,6 +3,8 @@ use std::error::Error; fn main() -> Result<(), Box> { // This will set the env var `example-shader.spv` to a spir-v file that can be include!()'d - SpirvBuilder::new("../example-shader").build()?; + SpirvBuilder::new(".") + .spirv_version(1, 0) + .build()?; Ok(()) } diff --git a/examples/example-shader/src/lib.rs b/examples/example-shader/src/lib.rs index a74b0e8102..8b53d6df61 100644 --- a/examples/example-shader/src/lib.rs +++ b/examples/example-shader/src/lib.rs @@ -8,6 +8,9 @@ use core::f32::consts::PI; use spirv_std::{Input, Mat4, MathExt, Output, Vec2, Vec3, Vec4}; +#[cfg(feature = "std")] +pub const COMPILED_SHADER: &'static [u8] = include_bytes!(env!("example_shader.spv")); + const DEPOLARIZATION_FACTOR: f32 = 0.035; const LUMINANCE: f32 = 1.0; const MIE_COEFFICIENT: f32 = 0.005; diff --git a/examples/wgpu-example-runner/build.rs b/examples/wgpu-example-runner/build.rs deleted file mode 100644 index 0243f6c665..0000000000 --- a/examples/wgpu-example-runner/build.rs +++ /dev/null @@ -1,10 +0,0 @@ -use spirv_builder::SpirvBuilder; -use std::error::Error; - -fn main() -> Result<(), Box> { - // This will set the env var `wgpu-example-shader.spv` to a spir-v file that can be include!()'d - SpirvBuilder::new("../wgpu-example-shader") - .spirv_version(1, 0) - .build()?; - Ok(()) -} diff --git a/spirv-builder/src/lib.rs b/spirv-builder/src/lib.rs index fe12897890..e5334e8315 100644 --- a/spirv-builder/src/lib.rs +++ b/spirv-builder/src/lib.rs @@ -71,6 +71,10 @@ impl SpirvBuilder { /// you usually don't have to inspect the path, as the environment variable will already be /// set. pub fn build(self) -> Result { + if std::env::var("TARGETING_SPIRV").is_ok() { + return Ok(PathBuf::new()); + } + let spirv_module = invoke_rustc(&self)?; let env_var = spirv_module.file_name().unwrap().to_str().unwrap(); if self.print_metadata { @@ -155,11 +159,13 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result { "build-std=core", "--target", "spirv-unknown-unknown", + "--no-default-features", "--release", ]) .stderr(Stdio::inherit()) .current_dir(&builder.path_to_crate) .env("RUSTFLAGS", rustflags) + .env("TARGETING_SPIRV", "1") .output() .expect("failed to execute cargo build"); if build.status.success() { From 0d384145e6ab9770dd6a7cf5e9f3278dbb0ae951 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Thu, 29 Oct 2020 23:26:06 +0100 Subject: [PATCH 2/5] Use target_arch --- examples/example-shader/Cargo.toml | 4 ---- examples/example-shader/src/lib.rs | 2 +- spirv-builder/src/lib.rs | 1 - 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/examples/example-shader/Cargo.toml b/examples/example-shader/Cargo.toml index 22d4b0bd47..e13d4ba9d6 100644 --- a/examples/example-shader/Cargo.toml +++ b/examples/example-shader/Cargo.toml @@ -13,7 +13,3 @@ spirv-std = { path = "../../spirv-std" } [build-dependencies] spirv-builder = { path = "../../spirv-builder" } - -[features] -default = ["std"] -std = [] diff --git a/examples/example-shader/src/lib.rs b/examples/example-shader/src/lib.rs index 8b53d6df61..c3df3199ac 100644 --- a/examples/example-shader/src/lib.rs +++ b/examples/example-shader/src/lib.rs @@ -8,7 +8,7 @@ use core::f32::consts::PI; use spirv_std::{Input, Mat4, MathExt, Output, Vec2, Vec3, Vec4}; -#[cfg(feature = "std")] +#[cfg(not(target_arch = "spirv"))] pub const COMPILED_SHADER: &'static [u8] = include_bytes!(env!("example_shader.spv")); const DEPOLARIZATION_FACTOR: f32 = 0.035; diff --git a/spirv-builder/src/lib.rs b/spirv-builder/src/lib.rs index e5334e8315..1848e9a11e 100644 --- a/spirv-builder/src/lib.rs +++ b/spirv-builder/src/lib.rs @@ -159,7 +159,6 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result { "build-std=core", "--target", "spirv-unknown-unknown", - "--no-default-features", "--release", ]) .stderr(Stdio::inherit()) From 2770f6098f1f094f648663067cfcbb1be460b2a1 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Thu, 29 Oct 2020 23:34:13 +0100 Subject: [PATCH 3/5] Add no-spirv-build feature --- examples/example-runner-cpu/Cargo.toml | 2 +- examples/example-shader/Cargo.toml | 3 +++ examples/example-shader/build.rs | 4 ++++ examples/example-shader/src/lib.rs | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/example-runner-cpu/Cargo.toml b/examples/example-runner-cpu/Cargo.toml index cc7897c435..7db9f010f3 100644 --- a/examples/example-runner-cpu/Cargo.toml +++ b/examples/example-runner-cpu/Cargo.toml @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0" [dependencies] minifb = "0.19.1" # bring in the shader as natively compiled code -example-shader = { path = "../example-shader" } +example-shader = { path = "../example-shader", features = ["no-spirv-build"] } # for math types, likely get from glam directly later instead spirv-std = { path = "../../spirv-std" } diff --git a/examples/example-shader/Cargo.toml b/examples/example-shader/Cargo.toml index e13d4ba9d6..7cf984e93c 100644 --- a/examples/example-shader/Cargo.toml +++ b/examples/example-shader/Cargo.toml @@ -13,3 +13,6 @@ spirv-std = { path = "../../spirv-std" } [build-dependencies] spirv-builder = { path = "../../spirv-builder" } + +[features] +no-spirv-build = [] diff --git a/examples/example-shader/build.rs b/examples/example-shader/build.rs index f90697e250..0cd55853a9 100644 --- a/examples/example-shader/build.rs +++ b/examples/example-shader/build.rs @@ -2,6 +2,10 @@ use spirv_builder::SpirvBuilder; use std::error::Error; fn main() -> Result<(), Box> { + if std::env::var("CARGO_FEATURE_NO_SPIRV_BUILD").is_ok() { + return Ok(()); + } + // This will set the env var `example-shader.spv` to a spir-v file that can be include!()'d SpirvBuilder::new(".") .spirv_version(1, 0) diff --git a/examples/example-shader/src/lib.rs b/examples/example-shader/src/lib.rs index c3df3199ac..64e1deb5f2 100644 --- a/examples/example-shader/src/lib.rs +++ b/examples/example-shader/src/lib.rs @@ -8,7 +8,7 @@ use core::f32::consts::PI; use spirv_std::{Input, Mat4, MathExt, Output, Vec2, Vec3, Vec4}; -#[cfg(not(target_arch = "spirv"))] +#[cfg(all(not(target_arch = "spirv"), not(feature = "no-spirv-build")))] pub const COMPILED_SHADER: &'static [u8] = include_bytes!(env!("example_shader.spv")); const DEPOLARIZATION_FACTOR: f32 = 0.035; From 9fc15c1a675869de5dbf8606895a79f050827f66 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Thu, 29 Oct 2020 23:42:48 +0100 Subject: [PATCH 4/5] Change no-spirv-build to be build-spirv --- examples/example-runner-cpu/Cargo.toml | 2 +- examples/example-shader/Cargo.toml | 5 +++-- examples/example-shader/build.rs | 8 ++------ examples/example-shader/src/lib.rs | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/examples/example-runner-cpu/Cargo.toml b/examples/example-runner-cpu/Cargo.toml index 7db9f010f3..51a3e9b451 100644 --- a/examples/example-runner-cpu/Cargo.toml +++ b/examples/example-runner-cpu/Cargo.toml @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0" [dependencies] minifb = "0.19.1" # bring in the shader as natively compiled code -example-shader = { path = "../example-shader", features = ["no-spirv-build"] } +example-shader = { path = "../example-shader", default-features = false } # for math types, likely get from glam directly later instead spirv-std = { path = "../../spirv-std" } diff --git a/examples/example-shader/Cargo.toml b/examples/example-shader/Cargo.toml index 7cf984e93c..0c508f1d8b 100644 --- a/examples/example-shader/Cargo.toml +++ b/examples/example-shader/Cargo.toml @@ -12,7 +12,8 @@ crate-type = ["dylib"] spirv-std = { path = "../../spirv-std" } [build-dependencies] -spirv-builder = { path = "../../spirv-builder" } +spirv-builder = { path = "../../spirv-builder", optional = true } [features] -no-spirv-build = [] +default = ["build-spirv"] +build-spirv = ["spirv-builder"] diff --git a/examples/example-shader/build.rs b/examples/example-shader/build.rs index 0cd55853a9..0a7f1a528f 100644 --- a/examples/example-shader/build.rs +++ b/examples/example-shader/build.rs @@ -1,13 +1,9 @@ -use spirv_builder::SpirvBuilder; use std::error::Error; fn main() -> Result<(), Box> { - if std::env::var("CARGO_FEATURE_NO_SPIRV_BUILD").is_ok() { - return Ok(()); - } - // This will set the env var `example-shader.spv` to a spir-v file that can be include!()'d - SpirvBuilder::new(".") + #[cfg(feature = "build-spirv")] + spirv_builder::SpirvBuilder::new(".") .spirv_version(1, 0) .build()?; Ok(()) diff --git a/examples/example-shader/src/lib.rs b/examples/example-shader/src/lib.rs index 64e1deb5f2..613c1ddf53 100644 --- a/examples/example-shader/src/lib.rs +++ b/examples/example-shader/src/lib.rs @@ -8,7 +8,7 @@ use core::f32::consts::PI; use spirv_std::{Input, Mat4, MathExt, Output, Vec2, Vec3, Vec4}; -#[cfg(all(not(target_arch = "spirv"), not(feature = "no-spirv-build")))] +#[cfg(all(not(target_arch = "spirv"), feature = "build-spirv"))] pub const COMPILED_SHADER: &'static [u8] = include_bytes!(env!("example_shader.spv")); const DEPOLARIZATION_FACTOR: f32 = 0.035; From a337310e0c4cbb710a7015a440b3743bb92e17df Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Thu, 29 Oct 2020 23:51:48 +0100 Subject: [PATCH 5/5] Do the same for wgpu-example-shader/runner --- Cargo.lock | 3 ++- examples/wgpu-example-runner/Cargo.toml | 4 +--- examples/wgpu-example-runner/src/main.rs | 3 ++- examples/wgpu-example-shader/Cargo.toml | 7 +++++++ examples/wgpu-example-shader/build.rs | 10 ++++++++++ examples/wgpu-example-shader/src/lib.rs | 3 +++ 6 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 examples/wgpu-example-shader/build.rs diff --git a/Cargo.lock b/Cargo.lock index 540d3f9b4e..372f312d10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2901,10 +2901,10 @@ dependencies = [ "console_error_panic_hook", "console_log", "futures", - "spirv-builder", "wasm-bindgen-futures", "web-sys", "wgpu", + "wgpu-example-shader", "wgpu-subscriber", "winit 0.22.2", ] @@ -2913,6 +2913,7 @@ dependencies = [ name = "wgpu-example-shader" version = "0.1.0" dependencies = [ + "spirv-builder", "spirv-std", ] diff --git a/examples/wgpu-example-runner/Cargo.toml b/examples/wgpu-example-runner/Cargo.toml index 400f8cac68..bd1b6053d8 100644 --- a/examples/wgpu-example-runner/Cargo.toml +++ b/examples/wgpu-example-runner/Cargo.toml @@ -9,9 +9,7 @@ license = "MIT OR Apache-2.0" wgpu = "0.6.0" futures = { version = "0.3", default-features = false, features = ["std", "executor"] } winit = { version = "0.22.1", features = ["web-sys"] } - -[build-dependencies] -spirv-builder = { path = "../../spirv-builder" } +wgpu-example-shader = { path = "../wgpu-example-shader" } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] wgpu-subscriber = "0.1.0" diff --git a/examples/wgpu-example-runner/src/main.rs b/examples/wgpu-example-runner/src/main.rs index 4c7538dbd7..6cc0f6ccaa 100644 --- a/examples/wgpu-example-runner/src/main.rs +++ b/examples/wgpu-example-runner/src/main.rs @@ -31,7 +31,8 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu:: .expect("Failed to create device"); // Load the shaders from disk - let module = device.create_shader_module(wgpu::include_spirv!(env!("wgpu_example_shader.spv"))); + let shader = wgpu::util::make_spirv(wgpu_example_shader::COMPILED_SHADER); + let module = device.create_shader_module(shader); let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, diff --git a/examples/wgpu-example-shader/Cargo.toml b/examples/wgpu-example-shader/Cargo.toml index c07da10088..065004a7ca 100644 --- a/examples/wgpu-example-shader/Cargo.toml +++ b/examples/wgpu-example-shader/Cargo.toml @@ -10,3 +10,10 @@ crate-type = ["dylib"] [dependencies] spirv-std = { path = "../../spirv-std" } + +[build-dependencies] +spirv-builder = { path = "../../spirv-builder", optional = true } + +[features] +default = ["build-spirv"] +build-spirv = ["spirv-builder"] diff --git a/examples/wgpu-example-shader/build.rs b/examples/wgpu-example-shader/build.rs new file mode 100644 index 0000000000..2e8822f3dc --- /dev/null +++ b/examples/wgpu-example-shader/build.rs @@ -0,0 +1,10 @@ +use std::error::Error; + +fn main() -> Result<(), Box> { + // This will set the env var `wgpu-example-shader.spv` to a spir-v file that can be include!()'d + #[cfg(feature = "build-spirv")] + spirv_builder::SpirvBuilder::new(".") + .spirv_version(1, 0) + .build()?; + Ok(()) +} diff --git a/examples/wgpu-example-shader/src/lib.rs b/examples/wgpu-example-shader/src/lib.rs index 5b80b99b35..3e6c5de62e 100644 --- a/examples/wgpu-example-shader/src/lib.rs +++ b/examples/wgpu-example-shader/src/lib.rs @@ -5,6 +5,9 @@ use spirv_std::{Input, Output, Vec4}; +#[cfg(all(not(target_arch = "spirv"), feature = "build-spirv"))] +pub const COMPILED_SHADER: &'static [u8] = include_bytes!(env!("wgpu_example_shader.spv")); + #[allow(unused_attributes)] #[spirv(fragment)] pub fn main_fs(mut output: Output) {