#[macro_use] extern crate shaderc_macro; const VERT: &[u32] = shaderc_compile_file!("examples/triangle/triangle.vert"); const FRAG: &[u32] = shaderc_compile_file!("examples/triangle/triangle.frag"); use std::borrow::Cow; use wgpu::ShaderModuleDescriptor; use winit::{ event::{Event, WindowEvent}, event_loop::{ControlFlow, EventLoop}, window::Window, }; async fn run(event_loop: EventLoop<()>, window: Window) { let size = window.inner_size(); let instance = wgpu::Instance::new(wgpu::BackendBit::all()); let surface = unsafe { instance.create_surface(&window) }; let adapter = instance .request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::default(), // Request an adapter which can render to our surface compatible_surface: Some(&surface), }) .await .expect("Failed to find an appropriate adapter"); // Create the logical device and command queue let (device, queue) = adapter .request_device( &wgpu::DeviceDescriptor { label: None, features: wgpu::Features::empty(), limits: wgpu::Limits::default(), }, None, ) .await .expect("Failed to create device"); let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[], push_constant_ranges: &[], }); let swapchain_format = adapter.get_swap_chain_preferred_format(&surface); let vert_shader = device.create_shader_module(&ShaderModuleDescriptor { label: Some("triangle.vert"), source: wgpu::ShaderSource::SpirV(Cow::Borrowed(VERT)), flags: wgpu::ShaderFlags::VALIDATION, }); let frag_shader = device.create_shader_module(&ShaderModuleDescriptor { label: Some("triangle.frag"), source: wgpu::ShaderSource::SpirV(Cow::Borrowed(FRAG)), flags: wgpu::ShaderFlags::VALIDATION, }); let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: None, layout: Some(&pipeline_layout), vertex: wgpu::VertexState { module: &vert_shader, entry_point: "main", buffers: &[], }, fragment: Some(wgpu::FragmentState { module: &frag_shader, entry_point: "main", targets: &[swapchain_format.into()], }), primitive: wgpu::PrimitiveState::default(), depth_stencil: None, multisample: wgpu::MultisampleState::default(), }); let mut sc_desc = wgpu::SwapChainDescriptor { usage: wgpu::TextureUsage::RENDER_ATTACHMENT, format: swapchain_format, width: size.width, height: size.height, present_mode: wgpu::PresentMode::Mailbox, }; let mut swap_chain = device.create_swap_chain(&surface, &sc_desc); event_loop.run(move |event, _, control_flow| { // Have the closure take ownership of the resources. // `event_loop.run` never returns, therefore we must do this to ensure // the resources are properly cleaned up. let _ = (&instance, &adapter, &pipeline_layout); *control_flow = ControlFlow::Wait; match event { Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { // Recreate the swap chain with the new size sc_desc.width = size.width; sc_desc.height = size.height; swap_chain = device.create_swap_chain(&surface, &sc_desc); } Event::RedrawRequested(_) => { let frame = swap_chain .get_current_frame() .expect("Failed to acquire next swap chain texture") .output; let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); { let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: None, color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::GREEN), store: true, }, }], depth_stencil_attachment: None, }); rpass.set_pipeline(&render_pipeline); rpass.draw(0..3, 0..1); } queue.submit(Some(encoder.finish())); } Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => *control_flow = ControlFlow::Exit, _ => {} } }); } fn main() { let event_loop = EventLoop::new(); let window = winit::window::Window::new(&event_loop).unwrap(); #[cfg(not(target_arch = "wasm32"))] { env_logger::init(); // Temporarily avoid srgb formats for the swapchain on the web pollster::block_on(run(event_loop, window)); } #[cfg(target_arch = "wasm32")] { std::panic::set_hook(Box::new(console_error_panic_hook::hook)); console_log::init().expect("could not initialize logger"); use winit::platform::web::WindowExtWebSys; // On wasm, append the canvas to the document body web_sys::window() .and_then(|win| win.document()) .and_then(|doc| doc.body()) .and_then(|body| { body.append_child(&web_sys::Element::from(window.canvas())) .ok() }) .expect("couldn't append canvas to document body"); wasm_bindgen_futures::spawn_local(run(event_loop, window)); } }