From e60e9099ca94b7557ba40f4d1ff231788631ea11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Thu, 14 Apr 2022 17:56:36 +0900 Subject: [PATCH] MSAA --- helix-ui/src/main.rs | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/helix-ui/src/main.rs b/helix-ui/src/main.rs index 61d4ebda4..41d3330a1 100644 --- a/helix-ui/src/main.rs +++ b/helix-ui/src/main.rs @@ -224,7 +224,34 @@ fn append_outline( } } +fn create_multisampled_framebuffer( + device: &wgpu::Device, + config: &wgpu::SurfaceConfiguration, + sample_count: u32, +) -> wgpu::TextureView { + let multisampled_texture_extent = wgpu::Extent3d { + width: config.width, + height: config.height, + depth_or_array_layers: 1, + }; + let multisampled_frame_descriptor = &wgpu::TextureDescriptor { + size: multisampled_texture_extent, + mip_level_count: 1, + sample_count, + dimension: wgpu::TextureDimension::D2, + format: config.format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, + label: None, + }; + + device + .create_texture(multisampled_frame_descriptor) + .create_view(&wgpu::TextureViewDescriptor::default()) +} + async fn run(event_loop: EventLoop<()>, window: Window) { + let sample_count = 4; + let size = window.inner_size(); let instance = wgpu::Instance::new(wgpu::Backends::all()); let surface = unsafe { instance.create_surface(&window) }; @@ -277,6 +304,7 @@ async fn run(event_loop: EventLoop<()>, window: Window) { // + // TODO: use size fetched before let data = View { size: [0.0, 0.0] }; let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { @@ -338,7 +366,10 @@ async fn run(event_loop: EventLoop<()>, window: Window) { }), primitive: wgpu::PrimitiveState::default(), depth_stencil: None, - multisample: wgpu::MultisampleState::default(), + multisample: wgpu::MultisampleState { + count: sample_count, + ..Default::default() + }, multiview: None, }); @@ -350,6 +381,9 @@ async fn run(event_loop: EventLoop<()>, window: Window) { present_mode: wgpu::PresentMode::Mailbox, }; + let mut multisampled_framebuffer = + create_multisampled_framebuffer(&device, &config, sample_count); + surface.configure(&device, &config); // @@ -369,6 +403,10 @@ async fn run(event_loop: EventLoop<()>, window: Window) { // Reconfigure the surface with the new size config.width = size.width; config.height = size.height; + + multisampled_framebuffer = + create_multisampled_framebuffer(&device, &config, sample_count); + surface.configure(&device, &config); // On macos the window needs to be redrawn manually after resizing window.request_redraw(); @@ -400,8 +438,8 @@ async fn run(event_loop: EventLoop<()>, window: Window) { let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: None, color_attachments: &[wgpu::RenderPassColorAttachment { - view: &view, - resolve_target: None, + view: &multisampled_framebuffer, + resolve_target: Some(&view), ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::BLACK), store: true,