1
1
#[ path = "../framework.rs" ]
2
2
mod framework;
3
3
4
+ use zerocopy:: { AsBytes , FromBytes } ;
5
+
4
6
#[ repr( C ) ]
5
- #[ derive( Clone , Copy , zerocopy :: AsBytes , zerocopy :: FromBytes ) ]
7
+ #[ derive( Clone , Copy , AsBytes , FromBytes ) ]
6
8
struct Vertex {
7
9
_pos : [ f32 ; 4 ] ,
8
10
_tex_coord : [ f32 ; 2 ] ,
@@ -107,7 +109,10 @@ impl Example {
107
109
}
108
110
109
111
impl framework:: Example for Example {
110
- fn init ( sc_desc : & wgpu:: SwapChainDescriptor , device : & wgpu:: Device ) -> ( Self , Option < wgpu:: CommandBuffer > ) {
112
+ fn init (
113
+ sc_desc : & wgpu:: SwapChainDescriptor ,
114
+ device : & wgpu:: Device ,
115
+ ) -> ( Self , Option < wgpu:: CommandBuffer > ) {
111
116
use std:: mem;
112
117
113
118
let mut init_encoder =
@@ -116,23 +121,20 @@ impl framework::Example for Example {
116
121
// Create the vertex and index buffers
117
122
let vertex_size = mem:: size_of :: < Vertex > ( ) ;
118
123
let ( vertex_data, index_data) = create_vertices ( ) ;
119
- let vertex_buf = device
120
- . create_buffer_mapped ( vertex_data. len ( ) , wgpu:: BufferUsage :: VERTEX )
121
- . fill_from_slice ( & vertex_data) ;
122
124
123
- let index_buf = device
124
- . create_buffer_mapped ( index_data. len ( ) , wgpu:: BufferUsage :: INDEX )
125
- . fill_from_slice ( & index_data) ;
125
+ let vertex_buf =
126
+ device. create_buffer_with_data ( vertex_data. as_bytes ( ) , wgpu:: BufferUsage :: VERTEX ) ;
127
+
128
+ let index_buf =
129
+ device. create_buffer_with_data ( index_data. as_bytes ( ) , wgpu:: BufferUsage :: INDEX ) ;
126
130
127
131
// Create pipeline layout
128
132
let bind_group_layout = device. create_bind_group_layout ( & wgpu:: BindGroupLayoutDescriptor {
129
133
bindings : & [
130
134
wgpu:: BindGroupLayoutBinding {
131
135
binding : 0 ,
132
136
visibility : wgpu:: ShaderStage :: VERTEX ,
133
- ty : wgpu:: BindingType :: UniformBuffer {
134
- dynamic : false ,
135
- } ,
137
+ ty : wgpu:: BindingType :: UniformBuffer { dynamic : false } ,
136
138
} ,
137
139
wgpu:: BindGroupLayoutBinding {
138
140
binding : 1 ,
@@ -171,9 +173,8 @@ impl framework::Example for Example {
171
173
usage : wgpu:: TextureUsage :: SAMPLED | wgpu:: TextureUsage :: COPY_DST ,
172
174
} ) ;
173
175
let texture_view = texture. create_default_view ( ) ;
174
- let temp_buf = device
175
- . create_buffer_mapped ( texels. len ( ) , wgpu:: BufferUsage :: COPY_SRC )
176
- . fill_from_slice ( & texels) ;
176
+ let temp_buf =
177
+ device. create_buffer_with_data ( texels. as_slice ( ) , wgpu:: BufferUsage :: COPY_SRC ) ;
177
178
init_encoder. copy_buffer_to_texture (
178
179
wgpu:: BufferCopyView {
179
180
buffer : & temp_buf,
@@ -208,12 +209,10 @@ impl framework::Example for Example {
208
209
} ) ;
209
210
let mx_total = Self :: generate_matrix ( sc_desc. width as f32 / sc_desc. height as f32 ) ;
210
211
let mx_ref: & [ f32 ; 16 ] = mx_total. as_ref ( ) ;
211
- let uniform_buf = device
212
- . create_buffer_mapped (
213
- 16 ,
214
- wgpu:: BufferUsage :: UNIFORM | wgpu:: BufferUsage :: COPY_DST ,
215
- )
216
- . fill_from_slice ( mx_ref) ;
212
+ let uniform_buf = device. create_buffer_with_data (
213
+ mx_ref. as_bytes ( ) ,
214
+ wgpu:: BufferUsage :: UNIFORM | wgpu:: BufferUsage :: COPY_DST ,
215
+ ) ;
217
216
218
217
// Create bind group
219
218
let bind_group = device. create_bind_group ( & wgpu:: BindGroupDescriptor {
@@ -310,21 +309,28 @@ impl framework::Example for Example {
310
309
//empty
311
310
}
312
311
313
- fn resize ( & mut self , sc_desc : & wgpu:: SwapChainDescriptor , device : & wgpu:: Device ) -> Option < wgpu:: CommandBuffer > {
312
+ fn resize (
313
+ & mut self ,
314
+ sc_desc : & wgpu:: SwapChainDescriptor ,
315
+ device : & wgpu:: Device ,
316
+ ) -> Option < wgpu:: CommandBuffer > {
314
317
let mx_total = Self :: generate_matrix ( sc_desc. width as f32 / sc_desc. height as f32 ) ;
315
318
let mx_ref: & [ f32 ; 16 ] = mx_total. as_ref ( ) ;
316
319
317
- let temp_buf = device
318
- . create_buffer_mapped ( 16 , wgpu:: BufferUsage :: COPY_SRC )
319
- . fill_from_slice ( mx_ref) ;
320
+ let temp_buf =
321
+ device. create_buffer_with_data ( mx_ref. as_bytes ( ) , wgpu:: BufferUsage :: COPY_SRC ) ;
320
322
321
323
let mut encoder =
322
324
device. create_command_encoder ( & wgpu:: CommandEncoderDescriptor { todo : 0 } ) ;
323
325
encoder. copy_buffer_to_buffer ( & temp_buf, 0 , & self . uniform_buf , 0 , 64 ) ;
324
326
Some ( encoder. finish ( ) )
325
327
}
326
328
327
- fn render ( & mut self , frame : & wgpu:: SwapChainOutput , device : & wgpu:: Device ) -> wgpu:: CommandBuffer {
329
+ fn render (
330
+ & mut self ,
331
+ frame : & wgpu:: SwapChainOutput ,
332
+ device : & wgpu:: Device ,
333
+ ) -> wgpu:: CommandBuffer {
328
334
let mut encoder =
329
335
device. create_command_encoder ( & wgpu:: CommandEncoderDescriptor { todo : 0 } ) ;
330
336
{
0 commit comments