Use bytemuck for the push constants (#668)

* Use bytemuck for the push constants

* Use the released version of bytemuck

https://github.com/Lokathor/bytemuck/pull/69 landed

That version also works 🎉
This commit is contained in:
Daniel McNab 2021-06-14 08:20:55 +01:00 committed by GitHub
parent 4b2011476b
commit 364590e05f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 8 deletions

22
Cargo.lock generated
View File

@ -197,6 +197,26 @@ version = "3.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" 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]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.4.3" version = "1.4.3"
@ -767,6 +787,7 @@ dependencies = [
name = "example-runner-wgpu" name = "example-runner-wgpu"
version = "0.4.0-alpha.9" version = "0.4.0-alpha.9"
dependencies = [ dependencies = [
"bytemuck",
"cfg-if 1.0.0", "cfg-if 1.0.0",
"clap 3.0.0-beta.2", "clap 3.0.0-beta.2",
"console_error_panic_hook", "console_error_panic_hook",
@ -2339,6 +2360,7 @@ dependencies = [
name = "shared" name = "shared"
version = "0.4.0-alpha.9" version = "0.4.0-alpha.9"
dependencies = [ dependencies = [
"bytemuck",
"spirv-std", "spirv-std",
] ]

View File

@ -23,6 +23,7 @@ wgpu = "0.7.0"
winit = { version = "0.24", features = ["web-sys"] } winit = { version = "0.24", features = ["web-sys"] }
clap = "3.0.0-beta.2" clap = "3.0.0-beta.2"
strum = { version = "0.20", default_features = false, features = ["derive"] } strum = { version = "0.20", default_features = false, features = ["derive"] }
bytemuck = "1.6.3"
[target.'cfg(not(any(target_os = "android", target_arch = "wasm32")))'.dependencies] [target.'cfg(not(any(target_os = "android", target_arch = "wasm32")))'.dependencies]
spirv-builder = { path = "../../../crates/spirv-builder", default-features = false, features = ["watch"] } spirv-builder = { path = "../../../crates/spirv-builder", default-features = false, features = ["watch"] }

View File

@ -23,10 +23,6 @@ mod shaders {
include!(concat!(env!("OUT_DIR"), "/entry_points.rs")); include!(concat!(env!("OUT_DIR"), "/entry_points.rs"));
} }
unsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] {
::std::slice::from_raw_parts((p as *const T) as *const u8, ::std::mem::size_of::<T>())
}
fn mouse_button_index(button: MouseButton) -> usize { fn mouse_button_index(button: MouseButton) -> usize {
match button { match button {
MouseButton::Left => 0, MouseButton::Left => 0,
@ -198,9 +194,11 @@ async fn run(
}; };
rpass.set_pipeline(render_pipeline); rpass.set_pipeline(render_pipeline);
rpass.set_push_constants(wgpu::ShaderStage::all(), 0, unsafe { rpass.set_push_constants(
any_as_u8_slice(&push_constants) wgpu::ShaderStage::all(),
}); 0,
bytemuck::bytes_of(&push_constants),
);
rpass.draw(0..3, 0..1); rpass.draw(0..3, 0..1);
} }

View File

@ -8,3 +8,4 @@ publish = false
[dependencies] [dependencies]
spirv-std = { path = "../../../crates/spirv-std", features = ["glam"] } spirv-std = { path = "../../../crates/spirv-std", features = ["glam"] }
bytemuck = { version = "1.6.3", features = ["derive"] }

View File

@ -12,7 +12,9 @@ pub use spirv_std::glam;
#[cfg(target_arch = "spirv")] #[cfg(target_arch = "spirv")]
use spirv_std::num_traits::Float; use spirv_std::num_traits::Float;
#[derive(Copy, Clone)] use bytemuck::{Pod, Zeroable};
#[derive(Copy, Clone, Pod, Zeroable)]
#[repr(C)] #[repr(C)]
pub struct ShaderConstants { pub struct ShaderConstants {
pub width: u32, pub width: u32,