Skip to content

Commit

Permalink
Use bytemuck for the push constants (#668)
Browse files Browse the repository at this point in the history
* Use bytemuck for the push constants

* Use the released version of bytemuck

Lokathor/bytemuck#69 landed

That version also works 🎉
  • Loading branch information
DJMcNab authored Jun 14, 2021
1 parent 4b20114 commit 364590e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/runners/wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
12 changes: 5 additions & 7 deletions examples/runners/wgpu/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ mod shaders {
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 {
match button {
MouseButton::Left => 0,
Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions examples/shaders/shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ publish = false

[dependencies]
spirv-std = { path = "../../../crates/spirv-std", features = ["glam"] }
bytemuck = { version = "1.6.3", features = ["derive"] }
4 changes: 3 additions & 1 deletion examples/shaders/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 364590e

Please sign in to comment.