Skip to content

Commit f984eb1

Browse files
bors[bot]StarArawn
andauthored
[rs] Merge gfx-rs#259
259: Remove zerocopy and replace with bytemuck. r=kvark a=StarArawn fixes gfx-rs#146 I've removed `zerocopy` from the examples and replaced it with `bytemuck`. I ran all of the examples/tests and everything ran great in vulkan on my windows box. Co-authored-by: StarToaster <[email protected]>
2 parents 32cf9d2 + f2d4295 commit f984eb1

File tree

9 files changed

+93
-67
lines changed

9 files changed

+93
-67
lines changed

wgpu/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ log = "0.4"
5555
png = "0.15"
5656
winit = "0.22"
5757
rand = "0.7.2"
58-
zerocopy = "0.3"
58+
bytemuck = "1"
5959
futures = "0.3"
6060

6161
[[example]]

wgpu/examples/boids/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ extern crate rand;
77
mod framework;
88

99
use std::fmt::Write;
10-
use zerocopy::AsBytes;
1110

1211
use wgpu::vertex_attr_array;
1312

@@ -165,7 +164,7 @@ impl framework::Example for Example {
165164

166165
let vertex_buffer_data = [-0.01f32, -0.02, 0.01, -0.02, 0.00, 0.02];
167166
let vertices_buffer = device.create_buffer_with_data(
168-
vertex_buffer_data.as_bytes(),
167+
bytemuck::bytes_of(&vertex_buffer_data),
169168
wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST,
170169
);
171170

@@ -182,7 +181,7 @@ impl framework::Example for Example {
182181
]
183182
.to_vec();
184183
let sim_param_buffer = device.create_buffer_with_data(
185-
sim_param_data.as_bytes(),
184+
bytemuck::cast_slice(&sim_param_data),
186185
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
187186
);
188187

@@ -204,7 +203,7 @@ impl framework::Example for Example {
204203
let mut particle_bind_groups = Vec::<wgpu::BindGroup>::new();
205204
for _i in 0..2 {
206205
particle_buffers.push(device.create_buffer_with_data(
207-
initial_particle_data.as_bytes(),
206+
bytemuck::cast_slice(&initial_particle_data),
208207
wgpu::BufferUsage::VERTEX
209208
| wgpu::BufferUsage::STORAGE
210209
| wgpu::BufferUsage::COPY_DST,

wgpu/examples/cube/main.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#[path = "../framework.rs"]
22
mod framework;
33

4-
use zerocopy::{AsBytes, FromBytes};
4+
use bytemuck::{Pod, Zeroable};
55

66
#[repr(C)]
7-
#[derive(Clone, Copy, AsBytes, FromBytes)]
7+
#[derive(Clone, Copy)]
88
struct Vertex {
99
_pos: [f32; 4],
1010
_tex_coord: [f32; 2],
1111
}
1212

13+
unsafe impl Pod for Vertex {}
14+
unsafe impl Zeroable for Vertex {}
15+
1316
fn vertex(pos: [i8; 3], tc: [i8; 2]) -> Vertex {
1417
Vertex {
1518
_pos: [pos[0] as f32, pos[1] as f32, pos[2] as f32, 1.0],
@@ -122,11 +125,13 @@ impl framework::Example for Example {
122125
let vertex_size = mem::size_of::<Vertex>();
123126
let (vertex_data, index_data) = create_vertices();
124127

125-
let vertex_buf =
126-
device.create_buffer_with_data(vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);
128+
let vertex_buf = device.create_buffer_with_data(
129+
bytemuck::cast_slice(&vertex_data),
130+
wgpu::BufferUsage::VERTEX,
131+
);
127132

128-
let index_buf =
129-
device.create_buffer_with_data(index_data.as_bytes(), wgpu::BufferUsage::INDEX);
133+
let index_buf = device
134+
.create_buffer_with_data(bytemuck::cast_slice(&index_data), wgpu::BufferUsage::INDEX);
130135

131136
// Create pipeline layout
132137
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
@@ -208,7 +213,7 @@ impl framework::Example for Example {
208213
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
209214
let mx_ref: &[f32; 16] = mx_total.as_ref();
210215
let uniform_buf = device.create_buffer_with_data(
211-
mx_ref.as_bytes(),
216+
bytemuck::cast_slice(mx_ref),
212217
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
213218
);
214219

@@ -318,8 +323,8 @@ impl framework::Example for Example {
318323
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
319324
let mx_ref: &[f32; 16] = mx_total.as_ref();
320325

321-
let temp_buf =
322-
device.create_buffer_with_data(mx_ref.as_bytes(), wgpu::BufferUsage::COPY_SRC);
326+
let temp_buf = device
327+
.create_buffer_with_data(bytemuck::cast_slice(mx_ref), wgpu::BufferUsage::COPY_SRC);
323328

324329
let mut encoder =
325330
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

wgpu/examples/hello-compute/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::{convert::TryInto as _, str::FromStr};
2-
use zerocopy::AsBytes as _;
32

43
async fn run() {
54
let numbers = if std::env::args().len() == 1 {
@@ -45,7 +44,7 @@ async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {
4544
device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&cs[..])).unwrap());
4645

4746
let staging_buffer = device.create_buffer_with_data(
48-
numbers.as_slice().as_bytes(),
47+
bytemuck::cast_slice(&numbers),
4948
wgpu::BufferUsage::MAP_READ | wgpu::BufferUsage::COPY_DST | wgpu::BufferUsage::COPY_SRC,
5049
);
5150

wgpu/examples/mipmap/main.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
#[path = "../framework.rs"]
22
mod framework;
33

4-
use zerocopy::{AsBytes, FromBytes};
4+
use bytemuck::{Pod, Zeroable};
55

66
use wgpu::vertex_attr_array;
77

88
const TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8UnormSrgb;
99

1010
#[repr(C)]
11-
#[derive(Clone, Copy, AsBytes, FromBytes)]
11+
#[derive(Clone, Copy)]
1212
struct Vertex {
1313
#[allow(dead_code)]
1414
pos: [f32; 4],
1515
}
1616

17+
unsafe impl Pod for Vertex {}
18+
unsafe impl Zeroable for Vertex {}
19+
1720
fn create_vertices() -> Vec<Vertex> {
1821
vec![
1922
Vertex {
@@ -215,8 +218,10 @@ impl framework::Example for Example {
215218
// Create the vertex and index buffers
216219
let vertex_size = mem::size_of::<Vertex>();
217220
let vertex_data = create_vertices();
218-
let vertex_buf =
219-
device.create_buffer_with_data(vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);
221+
let vertex_buf = device.create_buffer_with_data(
222+
bytemuck::cast_slice(&vertex_data),
223+
wgpu::BufferUsage::VERTEX,
224+
);
220225

221226
// Create pipeline layout
222227
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
@@ -301,7 +306,7 @@ impl framework::Example for Example {
301306
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
302307
let mx_ref: &[f32; 16] = mx_total.as_ref();
303308
let uniform_buf = device.create_buffer_with_data(
304-
mx_ref.as_bytes(),
309+
bytemuck::cast_slice(mx_ref),
305310
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
306311
);
307312

@@ -398,8 +403,8 @@ impl framework::Example for Example {
398403
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
399404
let mx_ref: &[f32; 16] = mx_total.as_ref();
400405

401-
let temp_buf =
402-
device.create_buffer_with_data(mx_ref.as_bytes(), wgpu::BufferUsage::COPY_SRC);
406+
let temp_buf = device
407+
.create_buffer_with_data(bytemuck::cast_slice(mx_ref), wgpu::BufferUsage::COPY_SRC);
403408

404409
let mut encoder =
405410
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

wgpu/examples/msaa-line/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@
1010
#[path = "../framework.rs"]
1111
mod framework;
1212

13-
use zerocopy::{AsBytes, FromBytes};
13+
use bytemuck::{Pod, Zeroable};
1414

1515
use wgpu::vertex_attr_array;
1616

1717
#[repr(C)]
18-
#[derive(Clone, Copy, AsBytes, FromBytes)]
18+
#[derive(Clone, Copy)]
1919
struct Vertex {
2020
_pos: [f32; 2],
2121
_color: [f32; 4],
2222
}
2323

24+
unsafe impl Pod for Vertex {}
25+
unsafe impl Zeroable for Vertex {}
26+
2427
struct Example {
2528
vs_module: wgpu::ShaderModule,
2629
fs_module: wgpu::ShaderModule,
@@ -157,8 +160,10 @@ impl framework::Example for Example {
157160
});
158161
}
159162

160-
let vertex_buffer =
161-
device.create_buffer_with_data(vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);
163+
let vertex_buffer = device.create_buffer_with_data(
164+
bytemuck::cast_slice(&vertex_data),
165+
wgpu::BufferUsage::VERTEX,
166+
);
162167
let vertex_count = vertex_data.len() as u32;
163168

164169
let this = Example {

wgpu/examples/shadow/main.rs

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ use std::{mem, ops::Range, rc::Rc};
33
#[path = "../framework.rs"]
44
mod framework;
55

6-
use zerocopy::{AsBytes, FromBytes};
6+
use bytemuck::{Pod, Zeroable};
77

88
use wgpu::vertex_attr_array;
99

1010
#[repr(C)]
11-
#[derive(Clone, Copy, AsBytes, FromBytes)]
11+
#[derive(Clone, Copy)]
1212

1313
struct Vertex {
1414
_pos: [i8; 4],
1515
_normal: [i8; 4],
1616
}
1717

18+
unsafe impl Pod for Vertex {}
19+
unsafe impl Zeroable for Vertex {}
20+
1821
fn vertex(pos: [i8; 3], nor: [i8; 3]) -> Vertex {
1922
Vertex {
2023
_pos: [pos[0], pos[1], pos[2], 1],
@@ -101,13 +104,16 @@ struct Light {
101104
}
102105

103106
#[repr(C)]
104-
#[derive(Clone, Copy, AsBytes, FromBytes)]
107+
#[derive(Clone, Copy)]
105108
struct LightRaw {
106109
proj: [[f32; 4]; 4],
107110
pos: [f32; 4],
108111
color: [f32; 4],
109112
}
110113

114+
unsafe impl Pod for LightRaw {}
115+
unsafe impl Zeroable for LightRaw {}
116+
111117
impl Light {
112118
fn to_raw(&self) -> LightRaw {
113119
use cgmath::{Deg, EuclideanSpace, Matrix4, PerspectiveFov, Point3, Vector3};
@@ -136,19 +142,25 @@ impl Light {
136142
}
137143

138144
#[repr(C)]
139-
#[derive(Clone, Copy, AsBytes, FromBytes)]
145+
#[derive(Clone, Copy)]
140146
struct ForwardUniforms {
141147
proj: [[f32; 4]; 4],
142148
num_lights: [u32; 4],
143149
}
144150

151+
unsafe impl Pod for ForwardUniforms {}
152+
unsafe impl Zeroable for ForwardUniforms {}
153+
145154
#[repr(C)]
146-
#[derive(Clone, Copy, AsBytes, FromBytes)]
155+
#[derive(Clone, Copy)]
147156
struct EntityUniforms {
148157
model: [[f32; 4]; 4],
149158
color: [f32; 4],
150159
}
151160

161+
unsafe impl Pod for EntityUniforms {}
162+
unsafe impl Zeroable for EntityUniforms {}
163+
152164
#[repr(C)]
153165
struct ShadowUniforms {
154166
proj: [[f32; 4]; 4],
@@ -200,20 +212,26 @@ impl framework::Example for Example {
200212
// Create the vertex and index buffers
201213
let vertex_size = mem::size_of::<Vertex>();
202214
let (cube_vertex_data, cube_index_data) = create_cube();
203-
let cube_vertex_buf = Rc::new(
204-
device.create_buffer_with_data(cube_vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX),
205-
);
215+
let cube_vertex_buf = Rc::new(device.create_buffer_with_data(
216+
bytemuck::cast_slice(&cube_vertex_data),
217+
wgpu::BufferUsage::VERTEX,
218+
));
206219

207-
let cube_index_buf = Rc::new(
208-
device.create_buffer_with_data(cube_index_data.as_bytes(), wgpu::BufferUsage::INDEX),
209-
);
220+
let cube_index_buf = Rc::new(device.create_buffer_with_data(
221+
bytemuck::cast_slice(&cube_index_data),
222+
wgpu::BufferUsage::INDEX,
223+
));
210224

211225
let (plane_vertex_data, plane_index_data) = create_plane(7);
212-
let plane_vertex_buf =
213-
device.create_buffer_with_data(plane_vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);
226+
let plane_vertex_buf = device.create_buffer_with_data(
227+
bytemuck::cast_slice(&plane_vertex_data),
228+
wgpu::BufferUsage::VERTEX,
229+
);
214230

215-
let plane_index_buf =
216-
device.create_buffer_with_data(plane_index_data.as_bytes(), wgpu::BufferUsage::INDEX);
231+
let plane_index_buf = device.create_buffer_with_data(
232+
bytemuck::cast_slice(&plane_index_data),
233+
wgpu::BufferUsage::INDEX,
234+
);
217235

218236
let entity_uniform_size = mem::size_of::<EntityUniforms>() as wgpu::BufferAddress;
219237
let plane_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
@@ -535,7 +553,7 @@ impl framework::Example for Example {
535553
};
536554
let uniform_size = mem::size_of::<ForwardUniforms>() as wgpu::BufferAddress;
537555
let uniform_buf = device.create_buffer_with_data(
538-
forward_uniforms.as_bytes(),
556+
bytemuck::bytes_of(&forward_uniforms),
539557
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
540558
);
541559

@@ -666,8 +684,8 @@ impl framework::Example for Example {
666684
let command_buf = {
667685
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
668686
let mx_ref: &[f32; 16] = mx_total.as_ref();
669-
let temp_buf =
670-
device.create_buffer_with_data(mx_ref.as_bytes(), wgpu::BufferUsage::COPY_SRC);
687+
let temp_buf = device
688+
.create_buffer_with_data(bytemuck::cast_slice(mx_ref), wgpu::BufferUsage::COPY_SRC);
671689

672690
let mut encoder =
673691
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
@@ -720,18 +738,15 @@ impl framework::Example for Example {
720738
cgmath::Matrix4::from_angle_x(cgmath::Deg(entity.rotation_speed));
721739
entity.mx_world = entity.mx_world * rotation;
722740
}
723-
slot.copy_from_slice(
724-
EntityUniforms {
725-
model: entity.mx_world.into(),
726-
color: [
727-
entity.color.r as f32,
728-
entity.color.g as f32,
729-
entity.color.b as f32,
730-
entity.color.a as f32,
731-
],
732-
}
733-
.as_bytes(),
734-
);
741+
slot.copy_from_slice(bytemuck::bytes_of(&EntityUniforms {
742+
model: entity.mx_world.into(),
743+
color: [
744+
entity.color.r as f32,
745+
entity.color.g as f32,
746+
entity.color.b as f32,
747+
entity.color.a as f32,
748+
],
749+
}));
735750
}
736751

737752
let temp_buf = temp_buf_data.finish();
@@ -762,7 +777,7 @@ impl framework::Example for Example {
762777
.iter()
763778
.zip(temp_buf_data.data.chunks_exact_mut(size))
764779
{
765-
slot.copy_from_slice(light.to_raw().as_bytes());
780+
slot.copy_from_slice(bytemuck::bytes_of(&light.to_raw()));
766781
}
767782
encoder.copy_buffer_to_buffer(
768783
&temp_buf_data.finish(),

0 commit comments

Comments
 (0)