1- use crate :: wgpu_renderer:: render_pipeline:: MyRenderPipeline ;
21use crate :: wgpu_renderer:: swapchain:: MySwapchainManager ;
32use anyhow:: Context ;
43use mygraphics_shaders:: ShaderConstants ;
5- use wgpu:: RenderPassDescriptor ;
6- use wgpu:: wgt:: CommandEncoderDescriptor ;
4+ use std:: sync:: Arc ;
75use winit:: event:: { Event , WindowEvent } ;
86use winit:: event_loop:: { ActiveEventLoop , ControlFlow , EventLoop } ;
97
108mod render_pipeline;
119mod renderer;
1210mod swapchain;
1311
14- pub async fn main ( ) -> anyhow:: Result < ( ) > {
12+ pub fn main ( ) -> anyhow:: Result < ( ) > {
13+ pollster:: block_on ( main_inner ( ) )
14+ }
15+
16+ pub async fn main_inner ( ) -> anyhow:: Result < ( ) > {
1517 // env_logger::init();
1618 let event_loop = EventLoop :: new ( ) ?;
1719 // FIXME(eddyb) incomplete `winit` upgrade, follow the guides in:
1820 // https://github.com/rust-windowing/winit/releases/tag/v0.30.0
1921 #[ allow( deprecated) ]
20- let window = event_loop. create_window (
21- winit:: window:: Window :: default_attributes ( )
22- . with_title ( "Rust GPU - wgpu" )
23- . with_inner_size ( winit:: dpi:: LogicalSize :: new (
24- f64:: from ( 1280 ) ,
25- f64:: from ( 720 ) ,
26- ) ) ,
27- ) ?;
22+ let window = Arc :: new (
23+ event_loop. create_window (
24+ winit:: window:: Window :: default_attributes ( )
25+ . with_title ( "Rust GPU - wgpu" )
26+ . with_inner_size ( winit:: dpi:: LogicalSize :: new (
27+ f64:: from ( 1280 ) ,
28+ f64:: from ( 720 ) ,
29+ ) ) ,
30+ ) ?,
31+ ) ;
2832
2933 let instance = wgpu:: Instance :: new ( & wgpu:: InstanceDescriptor :: from_env_or_default ( ) ) ;
30- let surface = instance. create_surface ( & window) ?;
34+ let surface = instance. create_surface ( window. clone ( ) ) ?;
3135 let adapter =
3236 wgpu:: util:: initialize_adapter_from_env_or_default ( & instance, Some ( & surface) ) . await ?;
3337
@@ -49,31 +53,20 @@ pub async fn main() -> anyhow::Result<()> {
4953 . context ( "Failed to create device" ) ?;
5054
5155 let mut swapchain = MySwapchainManager :: new ( adapter. clone ( ) , device. clone ( ) , window, surface) ;
52- let render_pipeline = MyRenderPipeline :: new ( & device, swapchain. format ( ) ) ?;
56+ let renderer = renderer :: MyRenderer :: new ( device, queue , swapchain. format ( ) ) ?;
5357
5458 let start = std:: time:: Instant :: now ( ) ;
5559 let mut event_handler =
5660 move |event : Event < _ > , event_loop_window_target : & ActiveEventLoop | match event {
57- Event :: AboutToWait => swapchain. render ( |f| {
58- let mut cmd = device. create_command_encoder ( & CommandEncoderDescriptor {
59- label : Some ( "main draw" ) ,
60- } ) ;
61- let mut rpass = cmd. begin_render_pass ( & RenderPassDescriptor {
62- label : Some ( "main renderpass" ) ,
63- color_attachments : & [ ] ,
64- depth_stencil_attachment : None ,
65- timestamp_writes : None ,
66- occlusion_query_set : None ,
67- } ) ;
68- render_pipeline. draw (
69- & mut rpass,
61+ Event :: AboutToWait => swapchain. render ( |render_target| {
62+ renderer. render (
7063 & ShaderConstants {
7164 time : start. elapsed ( ) . as_secs_f32 ( ) ,
72- width : f . texture ( ) . width ( ) ,
73- height : f . texture ( ) . height ( ) ,
65+ width : render_target . texture ( ) . width ( ) ,
66+ height : render_target . texture ( ) . height ( ) ,
7467 } ,
68+ render_target,
7569 ) ;
76- queue. submit ( std:: iter:: once ( cmd) ) ;
7770 } ) ,
7871 Event :: WindowEvent { event, .. } => {
7972 match event {
@@ -91,7 +84,6 @@ pub async fn main() -> anyhow::Result<()> {
9184 WindowEvent :: Resized ( _) => swapchain. should_recreate ( ) ,
9285 _ => { }
9386 }
94-
9587 Ok ( ( ) )
9688 }
9789 _ => {
0 commit comments