@ -19,28 +19,32 @@ struct State {
}
impl State {
fn new ( window : & Window ) -> Self {
async fn new ( window : & Window ) -> Self {
let size = window . inner_size ( ) ;
let surface = wgpu ::Surface ::create ( window ) ;
let adapter = wgpu ::Adapter ::request ( & wgpu ::RequestAdapterOptions {
.. Default ::default ( )
} ) . unwrap ( ) ;
let adapter = wgpu ::Adapter ::request (
& wgpu ::RequestAdapterOptions {
power_preference : wgpu ::PowerPreference ::Default ,
compatible_surface : Some ( & surface ) ,
} ,
wgpu ::BackendBit ::PRIMARY , // Vulkan + Metal + DX12 + Browser WebGPU
) . await . unwrap ( ) ;
let ( device , queue ) = adapter . request_device ( & wgpu ::DeviceDescriptor {
extensions : wgpu ::Extensions {
anisotropic_filtering : false ,
} ,
limits : Default ::default ( ) ,
} ) ;
} ) .await ;
let sc_desc = wgpu ::SwapChainDescriptor {
usage : wgpu ::TextureUsage ::OUTPUT_ATTACHMENT ,
format : wgpu ::TextureFormat ::Bgra8UnormSrgb ,
width : size . width ,
height : size . height ,
present_mode : wgpu ::PresentMode ::Vsync ,
present_mode : wgpu ::PresentMode ::Fifo ,
} ;
let swap_chain = device . create_swap_chain ( & surface , & sc_desc ) ;
@ -84,8 +88,10 @@ impl State {
} ,
] ,
depth_stencil_state : None ,
index_format : wgpu ::IndexFormat ::Uint16 ,
vertex_buffers : & [ ] ,
vertex_state : wgpu ::VertexStateDescriptor {
index_format : wgpu ::IndexFormat ::Uint16 ,
vertex_buffers : & [ ] ,
} ,
sample_count : 1 ,
sample_mask : ! 0 ,
alpha_to_coverage_enabled : false ,
@ -131,8 +137,10 @@ impl State {
} ,
] ,
depth_stencil_state : None ,
index_format : wgpu ::IndexFormat ::Uint16 ,
vertex_buffers : & [ ] ,
vertex_state : wgpu ::VertexStateDescriptor {
index_format : wgpu ::IndexFormat ::Uint16 ,
vertex_buffers : & [ ] ,
} ,
sample_count : 1 ,
sample_mask : ! 0 ,
alpha_to_coverage_enabled : false ,
@ -154,7 +162,7 @@ impl State {
}
fn resize ( & mut self , new_size : winit ::dpi ::PhysicalSize < u32 > ) {
async fn resize ( & mut self , new_size : winit ::dpi ::PhysicalSize < u32 > ) {
self . size = new_size ;
self . sc_desc . width = new_size . width ;
self . sc_desc . height = new_size . height ;
@ -180,15 +188,16 @@ impl State {
}
}
fn update ( & mut self ) {
async fn update ( & mut self ) {
}
fn render ( & mut self ) {
let frame = self . swap_chain . get_next_texture ( ) ;
async fn render ( & mut self ) {
let frame = self . swap_chain . get_next_texture ( )
. expect ( "Timeout getting texture" ) ;
let mut encoder = self . device . create_command_encoder ( & wgpu ::CommandEncoderDescriptor {
todo: 0 ,
label: Some ( "Render Encoder" ) ,
} ) ;
{
@ -230,16 +239,17 @@ fn main() {
. build ( & event_loop )
. unwrap ( ) ;
let mut state = State ::new ( & window ) ;
use futures ::executor ::block_on ;
// Since main can't be async, we're going to need to block
let mut state = block_on ( State ::new ( & window ) ) ;
event_loop . run ( move | event , _ , control_flow | {
match event {
Event ::WindowEvent {
ref event ,
window_id ,
} if window_id = = window . id ( ) = > if state . input ( event ) {
* control_flow = ControlFlow ::Wait ;
} else {
} if window_id = = window . id ( ) = > if ! state . input ( event ) {
match event {
WindowEvent ::CloseRequested = > * control_flow = ControlFlow ::Exit ,
WindowEvent ::KeyboardInput {
@ -252,26 +262,29 @@ fn main() {
virtual_keycode : Some ( VirtualKeyCode ::Escape ) ,
..
} = > * control_flow = ControlFlow ::Exit ,
_ = > * control_flow = ControlFlow ::Wait ,
_ = > { }
}
}
WindowEvent ::Resized ( physical_size ) = > {
state . resize ( * physical_size ) ;
* control_flow = ControlFlow ::Wait ;
block_on ( state . resize ( * physical_size ) ) ;
}
WindowEvent ::ScaleFactorChanged { new_inner_size , .. } = > {
state . resize ( * * new_inner_size ) ;
* control_flow = ControlFlow ::Wait ;
// new_inner_size is &mut so w have to dereference it twice
block_on ( state . resize ( * * new_inner_size ) ) ;
}
_ = > * control_flow = ControlFlow ::Wait ,
_ = > { }
}
}
Event ::RedrawRequested ( _ ) = > {
block_on ( state . update ( ) ) ;
block_on ( state . render ( ) ) ;
}
Event ::MainEventsCleared = > {
state . update ( ) ;
state . render ( ) ;
* control_flow = ControlFlow ::Wait ;
// RedrawRequested will only trigger once, unless we manually
// request it.
window . request_redraw ( ) ;
}
_ = > * control_flow = ControlFlow ::Wait ,
_ = > { }
}
} ) ;
}
}