1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| use wgpu::{IndexFormat, InstanceDescriptor, PrimitiveTopology, ShaderSource}; use winit::{ event::{Event, WindowEvent}, event_loop::{ControlFlow, EventLoop}, window::Window, };
pub struct Inputs<'a> { pub source: ShaderSource<'a>, pub topology: PrimitiveTopology, pub strip_index_format: Option<IndexFormat>, }
pub async fn run(event_loop: EventLoop<()>, window: Window, inputs: Inputs<'_>, num_vertices: u32) { let size = window.inner_size(); let instance = wgpu::Instance::new(InstanceDescriptor { backends: wgpu::Backends::VULKAN, dx12_shader_compiler: wgpu::Dx12Compiler::default(), });
let surface = unsafe { instance.create_surface(&window) }.unwrap(); let adapter = instance .request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::default(), force_fallback_adapter: false, compatible_surface: Some(&surface), }) .await .expect("Failed to find an appropriate adapter");
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 swapchain_capabilities = surface.get_capabilities(&adapter); let swapchain_format = swapchain_capabilities.formats[0];
let mut config = wgpu::SurfaceConfiguration { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, format: swapchain_format, width: size.width, height: size.height, present_mode: wgpu::PresentMode::Fifo, alpha_mode: swapchain_capabilities.alpha_modes[0], view_formats: vec![], };
surface.configure(&device, &config);
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: None, source: inputs.source, });
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[], push_constant_ranges: &[], });
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: None, layout: Some(&pipeline_layout), vertex: wgpu::VertexState { module: &shader, entry_point: "vs_main", buffers: &[], }, fragment: Some(wgpu::FragmentState { module: &shader, entry_point: "fs_main", targets: &[Some(swapchain_format.into())], }), primitive: wgpu::PrimitiveState { topology: inputs.topology, strip_index_format: inputs.strip_index_format, ..Default::default() }, depth_stencil: None, multisample: wgpu::MultisampleState::default(), multiview: None, });
event_loop.set_control_flow(ControlFlow::Wait); let _ = event_loop.run(move |event, elwt| { let _ = (&instance, &adapter, &shader, &pipeline_layout); match event { Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { config.width = size.width; config.height = size.height; surface.configure(&device, &config); } Event::WindowEvent { event: WindowEvent::RedrawRequested, .. } => { let frame = surface.get_current_texture().unwrap(); let view = frame .texture .create_view(&wgpu::TextureViewDescriptor::default()); let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); { let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: None, color_attachments: &[Some(wgpu::RenderPassColorAttachment { view: &view, resolve_target: None, ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.05, g: 0.062, b: 0.08, a: 1.0, }), store: true, }, })], depth_stencil_attachment: None, }); rpass.set_pipeline(&render_pipeline); rpass.draw(0..num_vertices, 0..1); }
queue.submit(Some(encoder.finish())); frame.present(); } Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => elwt.exit(), _ => {} } }); }
|