Finished checking of multi sampling capability

This commit is contained in:
Verox001 2025-05-05 13:54:29 +02:00
parent aad4355b30
commit ddcd2333fc

View File

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