diff --git a/simulator/src/main.rs b/simulator/src/main.rs index fd861ce..0a5c00d 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -5,6 +5,7 @@ use std::thread; use std::time::{Duration, Instant}; use cgmath::num_traits::{pow, ToPrimitive}; use cgmath::Rotation3; +use log::info; use pollster::FutureExt; use wgpu::util::DeviceExt; use wgpu::{Adapter, Device, Instance, PresentMode, Queue, Surface, SurfaceCapabilities, SurfaceConfiguration}; @@ -177,12 +178,20 @@ fn create_circle_vertices(segment_count: usize, radius: f32, color: [f32; 3]) -> (vertices, indices) } +struct SampleCount(u32); + +impl SampleCount { + pub fn get(&self) -> u32 { + self.0 + } +} + struct State<'a> { surface: Surface<'a>, device: Device, queue: Queue, - config: wgpu::SurfaceConfiguration, - // sample_count: u32, + config: SurfaceConfiguration, + sample_count: SampleCount, size: PhysicalSize, window: Arc, @@ -212,7 +221,7 @@ impl<'a> State<'a> { surface.configure(&device, &config); let sample_count = Self::probe_msaa_support(&device, &config); - println!("Probe: {:?}", sample_count); + info!("MSAA sample count: {}", sample_count); let globals = Globals { aspect_ratio: config.width as f32 / config.height as f32, @@ -244,7 +253,7 @@ impl<'a> State<'a> { label: Some("Global Bind Group"), }); - let render_pipeline = Self::create_render_pipeline(&device, &config, &global_bind_group_layout); + let render_pipeline = Self::create_render_pipeline(&device, &config, sample_count, &global_bind_group_layout); let mut geometries = HashMap::new(); let polygon_vertices = vec![ @@ -357,6 +366,7 @@ impl<'a> State<'a> { config, size, window: window_arc, + sample_count: SampleCount(sample_count), global_bind_group, global_buffer, @@ -371,32 +381,33 @@ impl<'a> State<'a> { } } - fn probe_msaa_support(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> u32 { - for &count in &[16, 8, 4, 2] { - let texture_desc = wgpu::TextureDescriptor { - label: Some("MSAA Probe"), - size: wgpu::Extent3d { - width: 4, - height: 4, - depth_or_array_layers: 1, - }, - mip_level_count: 1, - sample_count: count, - dimension: wgpu::TextureDimension::D2, - format: config.format, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT, - view_formats: &[], - }; + fn probe_msaa_support(device: &Device, config: &SurfaceConfiguration) -> u32 { + pollster::block_on(async { + for &count in &[16, 8, 4, 2] { + device.push_error_scope(wgpu::ErrorFilter::Validation); - // This will panic if not supported, so run this ONLY if you are confident (not ideal) - if let Ok(_) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - device.create_texture(&texture_desc); - })) { - return count; + let _ = device.create_texture(&wgpu::TextureDescriptor { + label: Some("MSAA Probe"), + size: wgpu::Extent3d { + width: 4, + height: 4, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: count, + dimension: wgpu::TextureDimension::D2, + format: config.format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], + }); + + if device.pop_error_scope().await.is_none() { + return count; + } } - } - 1 // fallback + 1 // fallback + }) } pub fn input(&mut self, event: &WindowEvent) -> bool { @@ -453,7 +464,7 @@ impl<'a> State<'a> { self.instances = updated_instances; } - fn create_render_pipeline(device: &Device, config: &wgpu::SurfaceConfiguration, global_bind_group_layout: &wgpu::BindGroupLayout) -> wgpu::RenderPipeline { + fn create_render_pipeline(device: &Device, config: &SurfaceConfiguration, sample_count: u32, global_bind_group_layout: &wgpu::BindGroupLayout) -> wgpu::RenderPipeline { let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("Shader"), source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()), @@ -486,7 +497,7 @@ impl<'a> State<'a> { compilation_options: wgpu::PipelineCompilationOptions::default(), }), primitive: wgpu::PrimitiveState { - topology: wgpu::PrimitiveTopology::TriangleList, // 1. + topology: wgpu::PrimitiveTopology::TriangleList, strip_index_format: None, front_face: wgpu::FrontFace::Ccw, cull_mode: Some(wgpu::Face::Back), @@ -499,7 +510,7 @@ impl<'a> State<'a> { }, depth_stencil: None, multisample: wgpu::MultisampleState { - count: 8, + count: sample_count, mask: !0, alpha_to_coverage_enabled: false, }, @@ -514,7 +525,7 @@ impl<'a> State<'a> { .copied() .unwrap_or(capabilities.formats[0]); - wgpu::SurfaceConfiguration { + SurfaceConfiguration { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, format: surface_format, width: size.width, @@ -581,7 +592,7 @@ impl<'a> State<'a> { depth_or_array_layers: 1, }, mip_level_count: 1, - sample_count: 8, + sample_count: self.sample_count.get(), dimension: wgpu::TextureDimension::D2, format: self.config.format, usage: wgpu::TextureUsages::RENDER_ATTACHMENT,