@@ -11,13 +11,13 @@ use winit::window::Window;
11
11
12
12
#[ repr( C ) ]
13
13
#[ derive( Copy , Clone , Debug , Pod , Zeroable ) ]
14
- struct Instance {
14
+ pub struct Instance {
15
15
pos : [ f32 ; 2 ] ,
16
16
size : [ f32 ; 2 ] ,
17
17
}
18
18
19
19
impl Instance {
20
- fn new ( x : f32 , y : f32 , width : f32 , height : f32 ) -> Self {
20
+ pub fn new ( x : f32 , y : f32 , width : f32 , height : f32 ) -> Self {
21
21
Self {
22
22
pos : [ x, y] ,
23
23
size : [ width, height] ,
@@ -40,11 +40,11 @@ pub struct Gpu<'window> {
40
40
}
41
41
42
42
impl < ' window > Gpu < ' window > {
43
- pub fn new ( window : Arc < Window > ) -> Gpu < ' window > {
44
- pollster:: block_on ( Gpu :: new_async ( window) )
43
+ pub fn new ( window : Arc < Window > , instances : Vec < Instance > ) -> Gpu < ' window > {
44
+ pollster:: block_on ( Gpu :: new_async ( window, instances ) )
45
45
}
46
46
47
- pub async fn new_async ( window : Arc < Window > ) -> Gpu < ' window > {
47
+ pub async fn new_async ( window : Arc < Window > , instances : Vec < Instance > ) -> Gpu < ' window > {
48
48
/*
49
49
* window
50
50
*/
@@ -100,16 +100,6 @@ impl<'window> Gpu<'window> {
100
100
range : 0 ..push_const_size,
101
101
} ;
102
102
103
- /*
104
- * rects
105
- */
106
-
107
- let rects: Vec < Instance > = vec ! [
108
- Instance :: new( 100.0 , 100.0 , 200.0 , 200.0 ) ,
109
- Instance :: new( 400.0 , 100.0 , 200.0 , 200.0 ) ,
110
- Instance :: new( 700.0 , 100.0 , 200.0 , 200.0 ) ,
111
- ] ;
112
-
113
103
/*
114
104
* vertices
115
105
*/
@@ -142,10 +132,10 @@ impl<'window> Gpu<'window> {
142
132
143
133
let instance_buffer = device. create_buffer_init ( & wgpu:: util:: BufferInitDescriptor {
144
134
label : Some ( "Instance Buffer" ) ,
145
- contents : cast_slice ( & rects ) ,
135
+ contents : cast_slice ( & instances ) ,
146
136
usage : wgpu:: BufferUsages :: VERTEX ,
147
137
} ) ;
148
- let instance_count = rects . len ( ) as u32 ;
138
+ let instance_count = instances . len ( ) as u32 ;
149
139
150
140
/*
151
141
* shader
@@ -290,4 +280,15 @@ impl<'window> Gpu<'window> {
290
280
self . queue . submit ( Some ( encoder. finish ( ) ) ) ;
291
281
frame. present ( ) ;
292
282
}
283
+
284
+ pub fn update_instance_buffer ( & mut self , instances : & [ Instance ] ) {
285
+ self . instance_buffer = self
286
+ . device
287
+ . create_buffer_init ( & wgpu:: util:: BufferInitDescriptor {
288
+ label : Some ( "Instance Buffer" ) ,
289
+ contents : cast_slice ( instances) ,
290
+ usage : wgpu:: BufferUsages :: VERTEX ,
291
+ } ) ;
292
+ self . instance_count = instances. len ( ) as u32 ;
293
+ }
293
294
}
0 commit comments