@@ -3,8 +3,10 @@ use std::{mem, ops::Range, rc::Rc};
3
3
#[ path = "../framework.rs" ]
4
4
mod framework;
5
5
6
+ use zerocopy:: { AsBytes , FromBytes } ;
7
+
6
8
#[ repr( C ) ]
7
- #[ derive( Clone , Copy , zerocopy :: AsBytes , zerocopy :: FromBytes ) ]
9
+ #[ derive( Clone , Copy , AsBytes , FromBytes ) ]
8
10
9
11
struct Vertex {
10
12
_pos : [ i8 ; 4 ] ,
@@ -97,7 +99,7 @@ struct Light {
97
99
}
98
100
99
101
#[ repr( C ) ]
100
- #[ derive( Clone , Copy , zerocopy :: AsBytes , zerocopy :: FromBytes ) ]
102
+ #[ derive( Clone , Copy , AsBytes , FromBytes ) ]
101
103
struct LightRaw {
102
104
proj : [ [ f32 ; 4 ] ; 4 ] ,
103
105
pos : [ f32 ; 4 ] ,
@@ -132,14 +134,14 @@ impl Light {
132
134
}
133
135
134
136
#[ repr( C ) ]
135
- #[ derive( Clone , Copy , zerocopy :: AsBytes , zerocopy :: FromBytes ) ]
137
+ #[ derive( Clone , Copy , AsBytes , FromBytes ) ]
136
138
struct ForwardUniforms {
137
139
proj : [ [ f32 ; 4 ] ; 4 ] ,
138
140
num_lights : [ u32 ; 4 ] ,
139
141
}
140
142
141
143
#[ repr( C ) ]
142
- #[ derive( Clone , Copy , zerocopy :: AsBytes , zerocopy :: FromBytes ) ]
144
+ #[ derive( Clone , Copy , AsBytes , FromBytes ) ]
143
145
struct EntityUniforms {
144
146
model : [ [ f32 ; 4 ] ; 4 ] ,
145
147
color : [ f32 ; 4 ] ,
@@ -198,24 +200,19 @@ impl framework::Example for Example {
198
200
let ( cube_vertex_data, cube_index_data) = create_cube ( ) ;
199
201
let cube_vertex_buf = Rc :: new (
200
202
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 ) ,
203
204
) ;
204
205
205
206
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 ) ,
209
208
) ;
210
209
211
210
let ( plane_vertex_data, plane_index_data) = create_plane ( 7 ) ;
212
211
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 ) ;
215
213
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 ) ;
219
216
220
217
let entity_uniform_size = mem:: size_of :: < EntityUniforms > ( ) as wgpu:: BufferAddress ;
221
218
let plane_uniform_buf = device. create_buffer ( & wgpu:: BufferDescriptor {
@@ -534,9 +531,10 @@ impl framework::Example for Example {
534
531
num_lights : [ lights. len ( ) as u32 , 0 , 0 , 0 ] ,
535
532
} ;
536
533
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
+ ) ;
540
538
541
539
// Create bind group
542
540
let bind_group = device. create_bind_group ( & wgpu:: BindGroupDescriptor {
@@ -662,9 +660,8 @@ impl framework::Example for Example {
662
660
let command_buf = {
663
661
let mx_total = Self :: generate_matrix ( sc_desc. width as f32 / sc_desc. height as f32 ) ;
664
662
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 ) ;
668
665
669
666
let mut encoder =
670
667
device. create_command_encoder ( & wgpu:: CommandEncoderDescriptor { todo : 0 } ) ;
@@ -699,54 +696,68 @@ impl framework::Example for Example {
699
696
device. create_command_encoder ( & wgpu:: CommandEncoderDescriptor { todo : 0 } ) ;
700
697
701
698
{
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
+ {
707
709
if entity. rotation_speed != 0.0 {
708
710
let rotation =
709
711
cgmath:: Matrix4 :: from_angle_x ( cgmath:: Deg ( entity. rotation_speed ) ) ;
710
712
entity. mx_world = entity. mx_world * rotation;
711
713
}
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
+ ) ;
721
726
}
722
727
723
728
let temp_buf = temp_buf_data. finish ( ) ;
724
729
725
730
for ( i, entity) in self . entities . iter ( ) . enumerate ( ) {
726
731
encoder. copy_buffer_to_buffer (
727
732
& temp_buf,
728
- i as wgpu:: BufferAddress * size ,
733
+ ( i * size ) as wgpu:: BufferAddress ,
729
734
& entity. uniform_buf ,
730
735
0 ,
731
- size,
736
+ size as wgpu :: BufferAddress ,
732
737
) ;
733
738
}
734
739
}
735
740
736
741
if self . lights_are_dirty {
737
742
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 ( ) ;
739
745
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 ( ) ) ;
743
754
}
744
755
encoder. copy_buffer_to_buffer (
745
756
& temp_buf_data. finish ( ) ,
746
757
0 ,
747
758
& self . light_uniform_buf ,
748
759
0 ,
749
- size ,
760
+ total_size as wgpu :: BufferAddress ,
750
761
) ;
751
762
}
752
763
0 commit comments