From 0f78bd3e6bc7fce41f4facb526f019a721b71ae7 Mon Sep 17 00:00:00 2001 From: Viktor Zoutman Date: Fri, 13 Nov 2020 19:37:30 +0100 Subject: [PATCH] Added clap to switch between shaders (#240) --- Cargo.lock | 105 ++++++++++++++++++++++++++++++- examples/runners/wgpu/Cargo.toml | 2 + examples/runners/wgpu/build.rs | 11 +++- examples/runners/wgpu/src/lib.rs | 33 ++++++++-- 4 files changed, 142 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0fb589c9a9..77482521bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -203,11 +203,43 @@ dependencies = [ "atty", "bitflags", "strsim 0.8.0", - "textwrap", + "textwrap 0.11.0", "unicode-width", "vec_map", ] +[[package]] +name = "clap" +version = "3.0.0-beta.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "indexmap", + "lazy_static", + "os_str_bytes", + "strsim 0.10.0", + "termcolor", + "textwrap 0.12.1", + "unicode-width", + "vec_map", +] + +[[package]] +name = "clap_derive" +version = "3.0.0-beta.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "370f715b81112975b1b69db93e0b56ea4cd4e5002ac43b2da8474106a54096a1" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2 1.0.24", + "quote 1.0.7", + "syn 1.0.48", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -603,11 +635,13 @@ name = "example-runner-wgpu" version = "0.1.0" dependencies = [ "cfg-if 1.0.0", + "clap 3.0.0-beta.2", "console_error_panic_hook", "console_log", "futures", "ndk-glue", "spirv-builder", + "strum", "wasm-bindgen-futures", "web-sys", "wgpu", @@ -963,6 +997,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "hashbrown" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" + [[package]] name = "heck" version = "0.3.1" @@ -987,6 +1027,16 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" +[[package]] +name = "indexmap" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55e2e4c765aa53a0424761bf9f41aa7a6ac1efa87238f59560640e27fca028f2" +dependencies = [ + "autocfg 1.0.1", + "hashbrown", +] + [[package]] name = "inplace_it" version = "0.3.2" @@ -1451,6 +1501,12 @@ dependencies = [ "sdl2", ] +[[package]] +name = "os_str_bytes" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85" + [[package]] name = "output_vt100" version = "0.1.2" @@ -2204,13 +2260,19 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "structopt" version = "0.3.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "126d630294ec449fae0b16f964e35bf3c74f940da9dca17ee9b905f7b3112eb8" dependencies = [ - "clap", + "clap 2.33.3", "lazy_static", "structopt-derive", ] @@ -2228,6 +2290,27 @@ dependencies = [ "syn 1.0.48", ] +[[package]] +name = "strum" +version = "0.19.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b89a286a7e3b5720b9a477b23253bc50debac207c8d21505f8e70b36792f11b5" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.19.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e61bb0be289045cb80bfce000512e32d09f8337e54c186725da381377ad1f8d5" +dependencies = [ + "heck", + "proc-macro2 1.0.24", + "quote 1.0.7", + "syn 1.0.48", +] + [[package]] name = "syn" version = "0.15.44" @@ -2276,6 +2359,15 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "termcolor" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +dependencies = [ + "winapi-util", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -2285,6 +2377,15 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "textwrap" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789" +dependencies = [ + "unicode-width", +] + [[package]] name = "thiserror" version = "1.0.22" diff --git a/examples/runners/wgpu/Cargo.toml b/examples/runners/wgpu/Cargo.toml index 62ae3580bc..f9655869e5 100644 --- a/examples/runners/wgpu/Cargo.toml +++ b/examples/runners/wgpu/Cargo.toml @@ -20,6 +20,8 @@ cfg-if = "1.0.0" futures = { version = "0.3", default-features = false, features = ["std", "executor"] } wgpu = "0.6.0" winit = { version = "0.23", features = ["web-sys"] } +clap = "3.0.0-beta.2" +strum = { version = "0.19", default_features = false, features = ["derive"] } [build-dependencies] spirv-builder = { path = "../../../crates/spirv-builder", default-features = false } diff --git a/examples/runners/wgpu/build.rs b/examples/runners/wgpu/build.rs index 437f95e628..55f95cc3cc 100644 --- a/examples/runners/wgpu/build.rs +++ b/examples/runners/wgpu/build.rs @@ -1,10 +1,15 @@ use spirv_builder::SpirvBuilder; use std::error::Error; -fn main() -> Result<(), Box> { - // This will set the env var `sky-shader.spv` to a spir-v file that can be include!()'d - SpirvBuilder::new("../../shaders/sky-shader") +fn build_shader(path_to_create: &str) -> Result<(), Box> { + SpirvBuilder::new(path_to_create) .spirv_version(1, 0) .build()?; Ok(()) } + +fn main() -> Result<(), Box> { + build_shader("../../shaders/sky-shader")?; + build_shader("../../shaders/simplest-shader")?; + Ok(()) +} diff --git a/examples/runners/wgpu/src/lib.rs b/examples/runners/wgpu/src/lib.rs index fdc101435f..f0e9ae33f2 100644 --- a/examples/runners/wgpu/src/lib.rs +++ b/examples/runners/wgpu/src/lib.rs @@ -1,14 +1,36 @@ +use clap::Clap; +use strum::{Display, EnumString}; use winit::{ event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent}, event_loop::{ControlFlow, EventLoop}, window::Window, }; -fn shader_module() -> wgpu::ShaderModuleSource<'static> { - wgpu::include_spirv!(env!("sky_shader.spv")) +#[derive(EnumString, Display)] +enum RustGPUShader { + Simplest, + Sky, } -async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::TextureFormat) { +fn shader_module(shader: RustGPUShader) -> wgpu::ShaderModuleSource<'static> { + match shader { + RustGPUShader::Simplest => wgpu::include_spirv!(env!("simplest_shader.spv")), + RustGPUShader::Sky => wgpu::include_spirv!(env!("sky_shader.spv")), + } +} + +#[derive(Clap)] +struct Options { + #[clap(short, long, default_value = "Sky")] + shader: RustGPUShader, +} + +async fn run( + options: Options, + event_loop: EventLoop<()>, + window: Window, + swapchain_format: wgpu::TextureFormat, +) { let size = window.inner_size(); let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY); @@ -43,7 +65,7 @@ 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(shader_module()); + let module = device.create_shader_module(shader_module(options.shader)); let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, @@ -166,6 +188,8 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu:: #[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))] pub fn main() { + let options: Options = Options::parse(); + let event_loop = EventLoop::new(); let window = winit::window::WindowBuilder::new() .with_title("Rust GPU - wgpu") @@ -196,6 +220,7 @@ pub fn main() { } else { wgpu_subscriber::initialize_default_subscriber(None); futures::executor::block_on(run( + options, event_loop, window, if cfg!(target_os = "android") {