From 364590e05f6702bf68f9a8d80b2705dd17687fb0 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Mon, 14 Jun 2021 08:20:55 +0100 Subject: [PATCH] Use bytemuck for the push constants (#668) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Use bytemuck for the push constants * Use the released version of bytemuck https://github.com/Lokathor/bytemuck/pull/69 landed That version also works 🎉 --- Cargo.lock | 22 ++++++++++++++++++++++ examples/runners/wgpu/Cargo.toml | 1 + examples/runners/wgpu/src/graphics.rs | 12 +++++------- examples/shaders/shared/Cargo.toml | 1 + examples/shaders/shared/src/lib.rs | 4 +++- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 73eef7bd18..0b95fb3f8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -197,6 +197,26 @@ version = "3.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" +[[package]] +name = "bytemuck" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9966d2ab714d0f785dbac0a0396251a35280aeb42413281617d0209ab4898435" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e215f8c2f9f79cb53c8335e687ffd07d5bfcb6fe5fc80723762d0be46e7cc54" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "byteorder" version = "1.4.3" @@ -767,6 +787,7 @@ dependencies = [ name = "example-runner-wgpu" version = "0.4.0-alpha.9" dependencies = [ + "bytemuck", "cfg-if 1.0.0", "clap 3.0.0-beta.2", "console_error_panic_hook", @@ -2339,6 +2360,7 @@ dependencies = [ name = "shared" version = "0.4.0-alpha.9" dependencies = [ + "bytemuck", "spirv-std", ] diff --git a/examples/runners/wgpu/Cargo.toml b/examples/runners/wgpu/Cargo.toml index f76338526b..7580be0b29 100644 --- a/examples/runners/wgpu/Cargo.toml +++ b/examples/runners/wgpu/Cargo.toml @@ -23,6 +23,7 @@ wgpu = "0.7.0" winit = { version = "0.24", features = ["web-sys"] } clap = "3.0.0-beta.2" strum = { version = "0.20", default_features = false, features = ["derive"] } +bytemuck = "1.6.3" [target.'cfg(not(any(target_os = "android", target_arch = "wasm32")))'.dependencies] spirv-builder = { path = "../../../crates/spirv-builder", default-features = false, features = ["watch"] } diff --git a/examples/runners/wgpu/src/graphics.rs b/examples/runners/wgpu/src/graphics.rs index ed95fd4c47..9f19a63d65 100644 --- a/examples/runners/wgpu/src/graphics.rs +++ b/examples/runners/wgpu/src/graphics.rs @@ -23,10 +23,6 @@ mod shaders { include!(concat!(env!("OUT_DIR"), "/entry_points.rs")); } -unsafe fn any_as_u8_slice(p: &T) -> &[u8] { - ::std::slice::from_raw_parts((p as *const T) as *const u8, ::std::mem::size_of::()) -} - fn mouse_button_index(button: MouseButton) -> usize { match button { MouseButton::Left => 0, @@ -198,9 +194,11 @@ async fn run( }; rpass.set_pipeline(render_pipeline); - rpass.set_push_constants(wgpu::ShaderStage::all(), 0, unsafe { - any_as_u8_slice(&push_constants) - }); + rpass.set_push_constants( + wgpu::ShaderStage::all(), + 0, + bytemuck::bytes_of(&push_constants), + ); rpass.draw(0..3, 0..1); } diff --git a/examples/shaders/shared/Cargo.toml b/examples/shaders/shared/Cargo.toml index 88aec6cc86..e7cd8cac85 100644 --- a/examples/shaders/shared/Cargo.toml +++ b/examples/shaders/shared/Cargo.toml @@ -8,3 +8,4 @@ publish = false [dependencies] spirv-std = { path = "../../../crates/spirv-std", features = ["glam"] } +bytemuck = { version = "1.6.3", features = ["derive"] } diff --git a/examples/shaders/shared/src/lib.rs b/examples/shaders/shared/src/lib.rs index 524a6f4645..69b1dd6378 100644 --- a/examples/shaders/shared/src/lib.rs +++ b/examples/shaders/shared/src/lib.rs @@ -12,7 +12,9 @@ pub use spirv_std::glam; #[cfg(target_arch = "spirv")] use spirv_std::num_traits::Float; -#[derive(Copy, Clone)] +use bytemuck::{Pod, Zeroable}; + +#[derive(Copy, Clone, Pod, Zeroable)] #[repr(C)] pub struct ShaderConstants { pub width: u32,