use bytemuck::{Pod, Zeroable}; use wgpu::{include_wgsl, util::DeviceExt, BindGroup, Buffer, Device, Queue, RenderPipeline, Surface, SurfaceConfiguration, VertexBufferLayout}; use winit::{ event::{ElementState, Event, MouseButton, WindowEvent}, event_loop::EventLoop, window::Window }; use crate::state::State; #[repr(C)] #[derive(Clone, Copy, Zeroable, Pod, Debug)] pub struct Vertex { pub pos: [f32; 3], pub color: [f32; 4] } impl Vertex { const DESC: VertexBufferLayout<'static> = VertexBufferLayout { array_stride: std::mem::size_of::() as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x4], }; } #[repr(C)] #[derive(Clone, Copy, Zeroable, Pod, Debug)] pub struct Uniforms { // [x, y, zoom] pub camera: [f32; 3], pub darkness: f32 } impl Default for Uniforms { fn default() -> Self { Self { camera: [0., 0., 1.], darkness: 0. } } } impl Uniforms { const DESC: VertexBufferLayout<'static> = VertexBufferLayout { array_stride: std::mem::size_of::() as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32], }; } pub struct Graphics<'a> { state: State, window: &'a Window, surface_config: SurfaceConfiguration, surface: Surface<'a>, device: Device, render_pipeline: RenderPipeline, queue: Queue, vertex_buf: Buffer, index_buf: Buffer, uniforms_buf: Buffer, uniforms_bind_group: BindGroup } impl<'a> Graphics<'a> { pub async fn init(window: &'a Window, state: State) -> Self { let mut size = window.inner_size(); size.width = size.width.max(1); size.height = size.height.max(1); let instance = wgpu::Instance::default(); let surface = instance.create_surface(window).unwrap(); let adapter = instance .request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::default(), force_fallback_adapter: false, // 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, required_features: wgpu::Features::empty(), // Make sure we use the texture resolution limits from the adapter, so we can support images the size of the swapchain. required_limits: wgpu::Limits::default() .using_resolution(adapter.limits()), memory_hints: wgpu::MemoryHints::MemoryUsage, }, None, ) .await .expect("Failed to create device"); let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Vertex Buffer"), contents: &[bytemuck::cast_slice::(&state.vertices), &[0; 1024]].concat(), usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, }); let index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Index Buffer"), contents: &[bytemuck::cast_slice::(&state.indices), &[0; 1024]].concat(), usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST, }); let uniforms_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Uniforms Buffer"), contents: bytemuck::cast_slice(&[state.uniforms]), usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, }); let uniforms_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { entries: &[ wgpu::BindGroupLayoutEntry { binding: 0, visibility: wgpu::ShaderStages::VERTEX, ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Uniform, has_dynamic_offset: false, min_binding_size: None, }, count: None, } ], label: Some("Uniforms Bind Group Layout"), }); let uniforms_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &uniforms_bind_group_layout, entries: &[ wgpu::BindGroupEntry { binding: 0, resource: uniforms_buf.as_entire_binding(), } ], label: Some("Uniforms Bind Group"), }); // Load the shaders from disk let shader = device.create_shader_module(include_wgsl!("shader.wgsl")); let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[ &uniforms_bind_group_layout ], push_constant_ranges: &[], }); let swapchain_capabilities = surface.get_capabilities(&adapter); let swapchain_format = swapchain_capabilities.formats[0]; 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: &[ Vertex::DESC ], compilation_options: Default::default(), }, fragment: Some(wgpu::FragmentState { module: &shader, entry_point: "fs_main", compilation_options: Default::default(), targets: &[Some(swapchain_format.into())], }), primitive: wgpu::PrimitiveState::default(), depth_stencil: None, multisample: wgpu::MultisampleState::default(), multiview: None, cache: None, }); let surface_config = surface .get_default_config(&adapter, size.width, size.height) .unwrap(); surface.configure(&device, &surface_config); Self { state, window, surface_config, surface, device, render_pipeline, queue, vertex_buf, index_buf, uniforms_buf, uniforms_bind_group } } pub fn run(&mut self, event_loop: EventLoop<()>) { event_loop.run(move |event, target| { // 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 _ = &self; match event { Event::WindowEvent { event: WindowEvent::Resized(new_size), .. } => { // Reconfigure the surface with the new size self.surface_config.width = new_size.width.max(1); self.surface_config.height = new_size.height.max(1); self.surface.configure(&self.device, &self.surface_config); // On macos the window needs to be redrawn manually after resizing self.window.request_redraw(); }, Event::WindowEvent { event: WindowEvent::RedrawRequested, .. } => { self.update(); self.render(); }, Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => target.exit(), Event::AboutToWait => { // RedrawRequested will only trigger once unless we manually // request it. self.window.request_redraw(); }, e => self.state.input(e) } }) .unwrap(); } fn render(&self) { let frame = self.surface .get_current_texture() .expect("Failed to acquire next swap chain texture"); let view = frame .texture .create_view(&wgpu::TextureViewDescriptor::default()); let mut encoder = self.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::BLACK), store: wgpu::StoreOp::Store, }, })], depth_stencil_attachment: None, timestamp_writes: None, occlusion_query_set: None, }); rpass.set_pipeline(&self.render_pipeline); rpass.set_bind_group(0, &self.uniforms_bind_group, &[]); rpass.set_vertex_buffer(0, self.vertex_buf.slice(..)); rpass.set_index_buffer(self.index_buf.slice(..), wgpu::IndexFormat::Uint32); rpass.draw_indexed(0..self.state.indices.len() as u32, 0, 0..1); // rpass.draw(0..self.state.vertices.len() as u32, 0..1); } self.queue.submit(Some(encoder.finish())); frame.present(); } fn update(&mut self) { self.state.update(); self.queue.write_buffer(&self.vertex_buf, 0, bytemuck::cast_slice(&self.state.vertices)); self.queue.write_buffer(&self.index_buf, 0, bytemuck::cast_slice(&self.state.indices)); self.queue.write_buffer(&self.uniforms_buf, 0, bytemuck::cast_slice(&[self.state.uniforms])); } }