@@ -13,7 +13,7 @@ fn main() -> Result<(), Box<dyn Error>> {
1313 . build ( & event_loop)
1414 . unwrap ( ) ;
1515
16- let instance = wgpu:: Instance :: new ( wgpu:: InstanceDescriptor :: default ( ) ) ;
16+ let instance = wgpu:: Instance :: new ( & wgpu:: InstanceDescriptor :: default ( ) ) ;
1717 let surface = instance. create_surface ( & window) ?;
1818
1919 // Initialize GPU
@@ -28,7 +28,7 @@ fn main() -> Result<(), Box<dyn Error>> {
2828 . expect ( "Request adapter" ) ;
2929
3030 adapter
31- . request_device ( & wgpu:: DeviceDescriptor :: default ( ) , None )
31+ . request_device ( & wgpu:: DeviceDescriptor :: default ( ) )
3232 . await
3333 . expect ( "Request device" )
3434 } ) ;
@@ -65,137 +65,140 @@ fn main() -> Result<(), Box<dyn Error>> {
6565 // Render loop
6666 window. request_redraw ( ) ;
6767
68- event_loop. run ( move |event, elwt| {
69- match event {
70- winit:: event:: Event :: WindowEvent {
71- event : winit:: event:: WindowEvent :: CloseRequested ,
72- ..
73- } => elwt. exit ( ) ,
74- winit:: event:: Event :: WindowEvent {
75- event : winit:: event:: WindowEvent :: Resized ( new_size) ,
76- ..
77- } => {
78- size = new_size;
79-
80- surface. configure (
81- & device,
82- & wgpu:: SurfaceConfiguration {
83- usage : wgpu:: TextureUsages :: RENDER_ATTACHMENT ,
84- format : render_format,
85- width : size. width ,
86- height : size. height ,
87- present_mode : wgpu:: PresentMode :: AutoVsync ,
88- alpha_mode : CompositeAlphaMode :: Auto ,
89- view_formats : vec ! [ ] ,
90- desired_maximum_frame_latency : 2 ,
91- } ,
92- ) ;
93- }
94- winit:: event:: Event :: WindowEvent {
95- event : winit:: event:: WindowEvent :: RedrawRequested ,
96- ..
97- } => {
98- // Get a command encoder for the current frame
99- let mut encoder = device. create_command_encoder (
100- & wgpu:: CommandEncoderDescriptor {
101- label : Some ( "Redraw" ) ,
102- } ,
103- ) ;
104-
105- // Get the next frame
106- let frame =
107- surface. get_current_texture ( ) . expect ( "Get next frame" ) ;
108- let view = & frame
109- . texture
110- . create_view ( & wgpu:: TextureViewDescriptor :: default ( ) ) ;
111-
112- // Clear frame
113- {
114- let _ = encoder. begin_render_pass (
115- & wgpu:: RenderPassDescriptor {
116- label : Some ( "Render pass" ) ,
117- color_attachments : & [ Some (
118- wgpu:: RenderPassColorAttachment {
119- view,
120- resolve_target : None ,
121- ops : wgpu:: Operations {
122- load : wgpu:: LoadOp :: Clear (
123- wgpu:: Color {
124- r : 0.4 ,
125- g : 0.4 ,
126- b : 0.4 ,
127- a : 1.0 ,
128- } ,
129- ) ,
130- store : wgpu:: StoreOp :: Store ,
131- } ,
132- } ,
133- ) ] ,
134- depth_stencil_attachment : None ,
135- timestamp_writes : None ,
136- occlusion_query_set : None ,
68+ event_loop
69+ . run ( move |event, elwt| {
70+ match event {
71+ winit:: event:: Event :: WindowEvent {
72+ event : winit:: event:: WindowEvent :: CloseRequested ,
73+ ..
74+ } => elwt. exit ( ) ,
75+ winit:: event:: Event :: WindowEvent {
76+ event : winit:: event:: WindowEvent :: Resized ( new_size) ,
77+ ..
78+ } => {
79+ size = new_size;
80+
81+ surface. configure (
82+ & device,
83+ & wgpu:: SurfaceConfiguration {
84+ usage : wgpu:: TextureUsages :: RENDER_ATTACHMENT ,
85+ format : render_format,
86+ width : size. width ,
87+ height : size. height ,
88+ present_mode : wgpu:: PresentMode :: AutoVsync ,
89+ alpha_mode : CompositeAlphaMode :: Auto ,
90+ view_formats : vec ! [ ] ,
91+ desired_maximum_frame_latency : 2 ,
13792 } ,
13893 ) ;
13994 }
95+ winit:: event:: Event :: WindowEvent {
96+ event : winit:: event:: WindowEvent :: RedrawRequested ,
97+ ..
98+ } => {
99+ // Get a command encoder for the current frame
100+ let mut encoder = device. create_command_encoder (
101+ & wgpu:: CommandEncoderDescriptor {
102+ label : Some ( "Redraw" ) ,
103+ } ,
104+ ) ;
140105
141- glyph_brush. queue ( Section {
142- screen_position : ( 30.0 , 30.0 ) ,
143- bounds : ( size. width as f32 , size. height as f32 ) ,
144- text : vec ! [ Text :: new( "Hello wgpu_glyph!" )
145- . with_color( [ 0.0 , 0.0 , 0.0 , 1.0 ] )
146- . with_scale( 40.0 ) ] ,
147- ..Section :: default ( )
148- } ) ;
149-
150- // Draw the text!
151- glyph_brush
152- . draw_queued (
153- & device,
154- & mut staging_belt,
155- & mut encoder,
156- view,
157- size. width ,
158- size. height ,
159- )
160- . expect ( "Draw queued" ) ;
161-
162- glyph_brush. queue ( Section {
163- screen_position : ( 30.0 , 90.0 ) ,
164- bounds : ( size. width as f32 , size. height as f32 ) ,
165- text : vec ! [ Text :: new( "Hello wgpu_glyph!" )
166- . with_color( [ 1.0 , 1.0 , 1.0 , 1.0 ] )
167- . with_scale( 40.0 ) ] ,
168- ..Section :: default ( )
169- } ) ;
170-
171- // Draw the text!
172- glyph_brush
173- . draw_queued_with_transform_and_scissoring (
174- & device,
175- & mut staging_belt,
176- & mut encoder,
177- view,
178- wgpu_glyph:: orthographic_projection (
106+ // Get the next frame
107+ let frame =
108+ surface. get_current_texture ( ) . expect ( "Get next frame" ) ;
109+ let view = & frame
110+ . texture
111+ . create_view ( & wgpu:: TextureViewDescriptor :: default ( ) ) ;
112+
113+ // Clear frame
114+ {
115+ let _ = encoder. begin_render_pass (
116+ & wgpu:: RenderPassDescriptor {
117+ label : Some ( "Render pass" ) ,
118+ color_attachments : & [ Some (
119+ wgpu:: RenderPassColorAttachment {
120+ view,
121+ resolve_target : None ,
122+ ops : wgpu:: Operations {
123+ load : wgpu:: LoadOp :: Clear (
124+ wgpu:: Color {
125+ r : 0.4 ,
126+ g : 0.4 ,
127+ b : 0.4 ,
128+ a : 1.0 ,
129+ } ,
130+ ) ,
131+ store : wgpu:: StoreOp :: Store ,
132+ } ,
133+ depth_slice : None ,
134+ } ,
135+ ) ] ,
136+ depth_stencil_attachment : None ,
137+ timestamp_writes : None ,
138+ occlusion_query_set : None ,
139+ } ,
140+ ) ;
141+ }
142+
143+ glyph_brush. queue ( Section {
144+ screen_position : ( 30.0 , 30.0 ) ,
145+ bounds : ( size. width as f32 , size. height as f32 ) ,
146+ text : vec ! [ Text :: new( "Hello wgpu_glyph!" )
147+ . with_color( [ 0.0 , 0.0 , 0.0 , 1.0 ] )
148+ . with_scale( 40.0 ) ] ,
149+ ..Section :: default ( )
150+ } ) ;
151+
152+ // Draw the text!
153+ glyph_brush
154+ . draw_queued (
155+ & device,
156+ & mut staging_belt,
157+ & mut encoder,
158+ view,
179159 size. width ,
180160 size. height ,
181- ) ,
182- Region {
183- x : 40 ,
184- y : 105 ,
185- width : 200 ,
186- height : 15 ,
187- } ,
188- )
189- . expect ( "Draw queued" ) ;
190-
191- // Submit the work!
192- staging_belt. finish ( ) ;
193- queue. submit ( Some ( encoder. finish ( ) ) ) ;
194- frame. present ( ) ;
195- // Recall unused staging buffers
196- staging_belt. recall ( ) ;
161+ )
162+ . expect ( "Draw queued" ) ;
163+
164+ glyph_brush. queue ( Section {
165+ screen_position : ( 30.0 , 90.0 ) ,
166+ bounds : ( size. width as f32 , size. height as f32 ) ,
167+ text : vec ! [ Text :: new( "Hello wgpu_glyph!" )
168+ . with_color( [ 1.0 , 1.0 , 1.0 , 1.0 ] )
169+ . with_scale( 40.0 ) ] ,
170+ ..Section :: default ( )
171+ } ) ;
172+
173+ // Draw the text!
174+ glyph_brush
175+ . draw_queued_with_transform_and_scissoring (
176+ & device,
177+ & mut staging_belt,
178+ & mut encoder,
179+ view,
180+ wgpu_glyph:: orthographic_projection (
181+ size. width ,
182+ size. height ,
183+ ) ,
184+ Region {
185+ x : 40 ,
186+ y : 105 ,
187+ width : 200 ,
188+ height : 15 ,
189+ } ,
190+ )
191+ . expect ( "Draw queued" ) ;
192+
193+ // Submit the work!
194+ staging_belt. finish ( ) ;
195+ queue. submit ( Some ( encoder. finish ( ) ) ) ;
196+ frame. present ( ) ;
197+ // Recall unused staging buffers
198+ staging_belt. recall ( ) ;
199+ }
200+ _ => { }
197201 }
198- _ => { }
199- }
200- } ) . map_err ( Into :: into)
202+ } )
203+ . map_err ( Into :: into)
201204}
0 commit comments