Skip to content
This repository was archived by the owner on Jun 18, 2021. It is now read-only.

Commit a2e4aa7

Browse files
committed
Update examples and tests
1 parent 88c24cf commit a2e4aa7

File tree

7 files changed

+165
-125
lines changed

7 files changed

+165
-125
lines changed

examples/cube/main.rs

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

4+
use zerocopy::{AsBytes, FromBytes};
5+
46
#[repr(C)]
5-
#[derive(Clone, Copy, zerocopy::AsBytes, zerocopy::FromBytes)]
7+
#[derive(Clone, Copy, AsBytes, FromBytes)]
68
struct Vertex {
79
_pos: [f32; 4],
810
_tex_coord: [f32; 2],
@@ -119,13 +121,12 @@ impl framework::Example for Example {
119121
// Create the vertex and index buffers
120122
let vertex_size = mem::size_of::<Vertex>();
121123
let (vertex_data, index_data) = create_vertices();
122-
let vertex_buf = device
123-
.create_buffer_mapped(vertex_data.len(), wgpu::BufferUsage::VERTEX)
124-
.fill_from_slice(&vertex_data);
125124

126-
let index_buf = device
127-
.create_buffer_mapped(index_data.len(), wgpu::BufferUsage::INDEX)
128-
.fill_from_slice(&index_data);
125+
let vertex_buf =
126+
device.create_buffer_mapped_from(vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);
127+
128+
let index_buf =
129+
device.create_buffer_mapped_from(index_data.as_bytes(), wgpu::BufferUsage::INDEX);
129130

130131
// Create pipeline layout
131132
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
@@ -172,9 +173,8 @@ impl framework::Example for Example {
172173
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
173174
});
174175
let texture_view = texture.create_default_view();
175-
let temp_buf = device
176-
.create_buffer_mapped(texels.len(), wgpu::BufferUsage::COPY_SRC)
177-
.fill_from_slice(&texels);
176+
let temp_buf =
177+
device.create_buffer_mapped_from(texels.as_slice(), wgpu::BufferUsage::COPY_SRC);
178178
init_encoder.copy_buffer_to_texture(
179179
wgpu::BufferCopyView {
180180
buffer: &temp_buf,
@@ -209,9 +209,10 @@ impl framework::Example for Example {
209209
});
210210
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
211211
let mx_ref: &[f32; 16] = mx_total.as_ref();
212-
let uniform_buf = device
213-
.create_buffer_mapped(16, wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST)
214-
.fill_from_slice(mx_ref);
212+
let uniform_buf = device.create_buffer_mapped_from(
213+
mx_ref.as_bytes(),
214+
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
215+
);
215216

216217
// Create bind group
217218
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
@@ -316,9 +317,8 @@ impl framework::Example for Example {
316317
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
317318
let mx_ref: &[f32; 16] = mx_total.as_ref();
318319

319-
let temp_buf = device
320-
.create_buffer_mapped(16, wgpu::BufferUsage::COPY_SRC)
321-
.fill_from_slice(mx_ref);
320+
let temp_buf =
321+
device.create_buffer_mapped_from(mx_ref.as_bytes(), wgpu::BufferUsage::COPY_SRC);
322322

323323
let mut encoder =
324324
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });

examples/hello-compute/main.rs

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

34
fn main() {
45
env_logger::init();
@@ -12,7 +13,8 @@ fn main() {
1213
.map(|s| u32::from_str(&s).expect("You must pass a list of positive integers!"))
1314
.collect();
1415

15-
let size = (numbers.len() * std::mem::size_of::<u32>()) as wgpu::BufferAddress;
16+
let slice_size = numbers.len() * std::mem::size_of::<u32>();
17+
let size = slice_size as wgpu::BufferAddress;
1618

1719
let adapter = wgpu::Adapter::request(
1820
&wgpu::RequestAdapterOptions {
@@ -33,12 +35,10 @@ fn main() {
3335
let cs_module =
3436
device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&cs[..])).unwrap());
3537

36-
let staging_buffer = device
37-
.create_buffer_mapped(
38-
numbers.len(),
39-
wgpu::BufferUsage::MAP_READ | wgpu::BufferUsage::COPY_DST | wgpu::BufferUsage::COPY_SRC,
40-
)
41-
.fill_from_slice(&numbers);
38+
let staging_buffer = device.create_buffer_mapped_from(
39+
numbers.as_slice().as_bytes(),
40+
wgpu::BufferUsage::MAP_READ | wgpu::BufferUsage::COPY_DST | wgpu::BufferUsage::COPY_SRC,
41+
);
4242

4343
let storage_buffer = device.create_buffer(&wgpu::BufferDescriptor {
4444
size,
@@ -93,13 +93,15 @@ fn main() {
9393

9494
queue.submit(&[encoder.finish()]);
9595

96-
staging_buffer.map_read_async(
97-
0,
98-
numbers.len(),
99-
|result: wgpu::BufferMapAsyncResult<&[u32]>| {
100-
if let Ok(mapping) = result {
101-
println!("Times: {:?}", mapping.data);
102-
}
103-
},
104-
);
96+
// FIXME: Align and use `LayoutVerified`
97+
staging_buffer.map_read_async(0, slice_size, |result| {
98+
if let Ok(mapping) = result {
99+
let times: Box<[u32]> = mapping
100+
.data
101+
.chunks_exact(4)
102+
.map(|b| u32::from_ne_bytes(b.try_into().unwrap()))
103+
.collect();
104+
println!("Times: {:?}", times);
105+
}
106+
});
105107
}

examples/mipmap/main.rs

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

4+
use zerocopy::{AsBytes, FromBytes};
5+
46
const TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8UnormSrgb;
57

68
#[repr(C)]
7-
#[derive(Clone, Copy, zerocopy::AsBytes, zerocopy::FromBytes)]
9+
#[derive(Clone, Copy, AsBytes, FromBytes)]
810
struct Vertex {
911
#[allow(dead_code)]
1012
pos: [f32; 4],
@@ -206,9 +208,8 @@ impl framework::Example for Example {
206208
// Create the vertex and index buffers
207209
let vertex_size = mem::size_of::<Vertex>();
208210
let vertex_data = create_vertices();
209-
let vertex_buf = device
210-
.create_buffer_mapped(vertex_data.len(), wgpu::BufferUsage::VERTEX)
211-
.fill_from_slice(&vertex_data);
211+
let vertex_buf =
212+
device.create_buffer_mapped_from(vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);
212213

213214
// Create pipeline layout
214215
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
@@ -258,9 +259,8 @@ impl framework::Example for Example {
258259
| wgpu::TextureUsage::COPY_DST,
259260
});
260261
let texture_view = texture.create_default_view();
261-
let temp_buf = device
262-
.create_buffer_mapped(texels.len(), wgpu::BufferUsage::COPY_SRC)
263-
.fill_from_slice(&texels);
262+
let temp_buf =
263+
device.create_buffer_mapped_from(texels.as_slice(), wgpu::BufferUsage::COPY_SRC);
264264
init_encoder.copy_buffer_to_texture(
265265
wgpu::BufferCopyView {
266266
buffer: &temp_buf,
@@ -295,9 +295,10 @@ impl framework::Example for Example {
295295
});
296296
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
297297
let mx_ref: &[f32; 16] = mx_total.as_ref();
298-
let uniform_buf = device
299-
.create_buffer_mapped(16, wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST)
300-
.fill_from_slice(mx_ref);
298+
let uniform_buf = device.create_buffer_mapped_from(
299+
mx_ref.as_bytes(),
300+
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
301+
);
301302

302303
// Create bind group
303304
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
@@ -393,9 +394,8 @@ impl framework::Example for Example {
393394
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
394395
let mx_ref: &[f32; 16] = mx_total.as_ref();
395396

396-
let temp_buf = device
397-
.create_buffer_mapped(16, wgpu::BufferUsage::COPY_SRC)
398-
.fill_from_slice(mx_ref);
397+
let temp_buf =
398+
device.create_buffer_mapped_from(mx_ref.as_bytes(), wgpu::BufferUsage::COPY_SRC);
399399

400400
let mut encoder =
401401
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });

examples/msaa-line/main.rs

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

13+
use zerocopy::{AsBytes, FromBytes};
14+
1315
#[repr(C)]
14-
#[derive(Clone, Copy, zerocopy::AsBytes, zerocopy::FromBytes)]
16+
#[derive(Clone, Copy, AsBytes, FromBytes)]
1517
struct Vertex {
1618
_pos: [f32; 2],
1719
_color: [f32; 4],
@@ -162,9 +164,8 @@ impl framework::Example for Example {
162164
});
163165
}
164166

165-
let vertex_buffer = device
166-
.create_buffer_mapped(vertex_data.len(), wgpu::BufferUsage::VERTEX)
167-
.fill_from_slice(&vertex_data);
167+
let vertex_buffer =
168+
device.create_buffer_mapped_from(vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);
168169
let vertex_count = vertex_data.len() as u32;
169170

170171
let this = Example {

examples/shadow/main.rs

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

6+
use zerocopy::{AsBytes, FromBytes};
7+
68
#[repr(C)]
7-
#[derive(Clone, Copy, zerocopy::AsBytes, zerocopy::FromBytes)]
9+
#[derive(Clone, Copy, AsBytes, FromBytes)]
810

911
struct Vertex {
1012
_pos: [i8; 4],
@@ -97,7 +99,7 @@ struct Light {
9799
}
98100

99101
#[repr(C)]
100-
#[derive(Clone, Copy, zerocopy::AsBytes, zerocopy::FromBytes)]
102+
#[derive(Clone, Copy, AsBytes, FromBytes)]
101103
struct LightRaw {
102104
proj: [[f32; 4]; 4],
103105
pos: [f32; 4],
@@ -132,14 +134,14 @@ impl Light {
132134
}
133135

134136
#[repr(C)]
135-
#[derive(Clone, Copy, zerocopy::AsBytes, zerocopy::FromBytes)]
137+
#[derive(Clone, Copy, AsBytes, FromBytes)]
136138
struct ForwardUniforms {
137139
proj: [[f32; 4]; 4],
138140
num_lights: [u32; 4],
139141
}
140142

141143
#[repr(C)]
142-
#[derive(Clone, Copy, zerocopy::AsBytes, zerocopy::FromBytes)]
144+
#[derive(Clone, Copy, AsBytes, FromBytes)]
143145
struct EntityUniforms {
144146
model: [[f32; 4]; 4],
145147
color: [f32; 4],
@@ -198,24 +200,19 @@ impl framework::Example for Example {
198200
let (cube_vertex_data, cube_index_data) = create_cube();
199201
let cube_vertex_buf = Rc::new(
200202
device
201-
.create_buffer_mapped(cube_vertex_data.len(), wgpu::BufferUsage::VERTEX)
202-
.fill_from_slice(&cube_vertex_data),
203+
.create_buffer_mapped_from(cube_vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX),
203204
);
204205

205206
let cube_index_buf = Rc::new(
206-
device
207-
.create_buffer_mapped(cube_index_data.len(), wgpu::BufferUsage::INDEX)
208-
.fill_from_slice(&cube_index_data),
207+
device.create_buffer_mapped_from(cube_index_data.as_bytes(), wgpu::BufferUsage::INDEX),
209208
);
210209

211210
let (plane_vertex_data, plane_index_data) = create_plane(7);
212211
let plane_vertex_buf = device
213-
.create_buffer_mapped(plane_vertex_data.len(), wgpu::BufferUsage::VERTEX)
214-
.fill_from_slice(&plane_vertex_data);
212+
.create_buffer_mapped_from(plane_vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);
215213

216-
let plane_index_buf = device
217-
.create_buffer_mapped(plane_index_data.len(), wgpu::BufferUsage::INDEX)
218-
.fill_from_slice(&plane_index_data);
214+
let plane_index_buf =
215+
device.create_buffer_mapped_from(plane_index_data.as_bytes(), wgpu::BufferUsage::INDEX);
219216

220217
let entity_uniform_size = mem::size_of::<EntityUniforms>() as wgpu::BufferAddress;
221218
let plane_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
@@ -534,9 +531,10 @@ impl framework::Example for Example {
534531
num_lights: [lights.len() as u32, 0, 0, 0],
535532
};
536533
let uniform_size = mem::size_of::<ForwardUniforms>() as wgpu::BufferAddress;
537-
let uniform_buf = device
538-
.create_buffer_mapped(1, wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST)
539-
.fill_from_slice(&[forward_uniforms]);
534+
let uniform_buf = device.create_buffer_mapped_from(
535+
forward_uniforms.as_bytes(),
536+
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
537+
);
540538

541539
// Create bind group
542540
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
@@ -662,9 +660,8 @@ impl framework::Example for Example {
662660
let command_buf = {
663661
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
664662
let mx_ref: &[f32; 16] = mx_total.as_ref();
665-
let temp_buf = device
666-
.create_buffer_mapped(16, wgpu::BufferUsage::COPY_SRC)
667-
.fill_from_slice(mx_ref);
663+
let temp_buf =
664+
device.create_buffer_mapped_from(mx_ref.as_bytes(), wgpu::BufferUsage::COPY_SRC);
668665

669666
let mut encoder =
670667
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
@@ -699,54 +696,68 @@ impl framework::Example for Example {
699696
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
700697

701698
{
702-
let size = mem::size_of::<EntityUniforms>() as wgpu::BufferAddress;
703-
let temp_buf_data =
704-
device.create_buffer_mapped(self.entities.len(), wgpu::BufferUsage::COPY_SRC);
705-
706-
for (i, entity) in self.entities.iter_mut().enumerate() {
699+
let size = mem::size_of::<EntityUniforms>();
700+
let temp_buf_data = device
701+
.create_buffer_mapped(self.entities.len() * size, wgpu::BufferUsage::COPY_SRC);
702+
703+
// FIXME: Align and use `LayoutVerified`
704+
for (entity, slot) in self
705+
.entities
706+
.iter_mut()
707+
.zip(temp_buf_data.data.chunks_exact_mut(size))
708+
{
707709
if entity.rotation_speed != 0.0 {
708710
let rotation =
709711
cgmath::Matrix4::from_angle_x(cgmath::Deg(entity.rotation_speed));
710712
entity.mx_world = entity.mx_world * rotation;
711713
}
712-
temp_buf_data.data[i] = EntityUniforms {
713-
model: entity.mx_world.into(),
714-
color: [
715-
entity.color.r as f32,
716-
entity.color.g as f32,
717-
entity.color.b as f32,
718-
entity.color.a as f32,
719-
],
720-
};
714+
slot.copy_from_slice(
715+
EntityUniforms {
716+
model: entity.mx_world.into(),
717+
color: [
718+
entity.color.r as f32,
719+
entity.color.g as f32,
720+
entity.color.b as f32,
721+
entity.color.a as f32,
722+
],
723+
}
724+
.as_bytes(),
725+
);
721726
}
722727

723728
let temp_buf = temp_buf_data.finish();
724729

725730
for (i, entity) in self.entities.iter().enumerate() {
726731
encoder.copy_buffer_to_buffer(
727732
&temp_buf,
728-
i as wgpu::BufferAddress * size,
733+
(i * size) as wgpu::BufferAddress,
729734
&entity.uniform_buf,
730735
0,
731-
size,
736+
size as wgpu::BufferAddress,
732737
);
733738
}
734739
}
735740

736741
if self.lights_are_dirty {
737742
self.lights_are_dirty = false;
738-
let size = (self.lights.len() * mem::size_of::<LightRaw>()) as wgpu::BufferAddress;
743+
let size = mem::size_of::<LightRaw>();
744+
let total_size = size * self.lights.len();
739745
let temp_buf_data =
740-
device.create_buffer_mapped(self.lights.len(), wgpu::BufferUsage::COPY_SRC);
741-
for (i, light) in self.lights.iter().enumerate() {
742-
temp_buf_data.data[i] = light.to_raw();
746+
device.create_buffer_mapped(total_size, wgpu::BufferUsage::COPY_SRC);
747+
// FIXME: Align and use `LayoutVerified`
748+
for (light, slot) in self
749+
.lights
750+
.iter()
751+
.zip(temp_buf_data.data.chunks_exact_mut(size))
752+
{
753+
slot.copy_from_slice(light.to_raw().as_bytes());
743754
}
744755
encoder.copy_buffer_to_buffer(
745756
&temp_buf_data.finish(),
746757
0,
747758
&self.light_uniform_buf,
748759
0,
749-
size,
760+
total_size as wgpu::BufferAddress,
750761
);
751762
}
752763

0 commit comments

Comments
 (0)