From db716b3bdf039b38fe7dcb17776cae7803d47d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 5 Nov 2019 05:26:20 +0100 Subject: [PATCH 1/6] Apply HiDPI to text, images, and clip primitives Quads are a bit trickier to handle. We may need to change the shaders a bit. --- core/src/rectangle.rs | 13 ++++++ native/src/renderer/windowed.rs | 9 +++- wgpu/src/renderer.rs | 53 ++++++++++++++++------- wgpu/src/renderer/target.rs | 16 ++++++- wgpu/src/transformation.rs | 5 +++ winit/src/application.rs | 74 +++++++++++++++------------------ 6 files changed, 112 insertions(+), 58 deletions(-) diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index 95c2570c55..c319167764 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -28,3 +28,16 @@ impl Rectangle { && point.y <= self.y + self.height } } + +impl std::ops::Mul for Rectangle { + type Output = Self; + + fn mul(self, scale: f32) -> Self { + Self { + x: (self.x as f32 * scale).round() as u32, + y: (self.y as f32 * scale).round() as u32, + width: (self.width as f32 * scale).round() as u32, + height: (self.height as f32 * scale).round() as u32, + } + } +} diff --git a/native/src/renderer/windowed.rs b/native/src/renderer/windowed.rs index 6e4ae61120..6d0419d201 100644 --- a/native/src/renderer/windowed.rs +++ b/native/src/renderer/windowed.rs @@ -22,8 +22,15 @@ pub trait Target { window: &W, width: u16, height: u16, + dpi: f32, renderer: &Self::Renderer, ) -> Self; - fn resize(&mut self, width: u16, height: u16, renderer: &Self::Renderer); + fn resize( + &mut self, + width: u16, + height: u16, + dpi: f32, + renderer: &Self::Renderer, + ); } diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 060f07a3a1..7ac74e9349 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -98,6 +98,7 @@ impl Renderer { log::debug!("Drawing"); let (width, height) = target.dimensions(); + let dpi = target.dpi(); let transformation = target.transformation(); let frame = target.next_frame(); @@ -137,7 +138,7 @@ impl Renderer { self.draw_overlay(overlay, &mut layers); for layer in layers { - self.flush(transformation, &layer, &mut encoder, &frame.view); + self.flush(dpi, transformation, &layer, &mut encoder, &frame.view); } self.queue.submit(&[encoder.finish()]); @@ -190,7 +191,10 @@ impl Renderer { layer.text.push(Section { text: &content, - screen_position: (x, y), + screen_position: ( + x - layer.offset.x as f32, + y - layer.offset.y as f32, + ), bounds: (bounds.width, bounds.height), scale: wgpu_glyph::Scale { x: *size, y: *size }, color: color.into_linear(), @@ -225,6 +229,7 @@ impl Renderer { background, border_radius, } => { + // TODO: Move some of this computations to the GPU (?) layer.quads.push(Quad { position: [ bounds.x - layer.offset.x as f32, @@ -234,7 +239,7 @@ impl Renderer { color: match background { Background::Color(color) => color.into_linear(), }, - border_radius: u32::from(*border_radius), + border_radius: *border_radius as u32, }); } Primitive::Image { path, bounds } => { @@ -308,16 +313,13 @@ impl Renderer { fn flush( &mut self, + dpi: f32, transformation: Transformation, layer: &Layer, encoder: &mut wgpu::CommandEncoder, target: &wgpu::TextureView, ) { - let translated = transformation - * Transformation::translate( - -(layer.offset.x as f32), - -(layer.offset.y as f32), - ); + let bounds = layer.bounds * dpi; if layer.quads.len() > 0 { self.quad_pipeline.draw( @@ -325,18 +327,25 @@ impl Renderer { encoder, &layer.quads, transformation, - layer.bounds, + bounds, target, ); } if layer.images.len() > 0 { + let translated = transformation + * Transformation::scale(dpi, dpi) + * Transformation::translate( + -(layer.offset.x as f32), + -(layer.offset.y as f32), + ); + self.image_pipeline.draw( &mut self.device, encoder, &layer.images, translated, - layer.bounds, + bounds, target, ); } @@ -345,6 +354,20 @@ impl Renderer { let mut glyph_brush = self.glyph_brush.borrow_mut(); for text in layer.text.iter() { + // Target physical coordinates directly to avoid blurry text + let text = Section { + screen_position: ( + text.screen_position.0 * dpi, + text.screen_position.1 * dpi, + ), + bounds: (text.bounds.0 * dpi, text.bounds.1 * dpi), + scale: wgpu_glyph::Scale { + x: text.scale.x * dpi, + y: text.scale.y * dpi, + }, + ..*text + }; + glyph_brush.queue(text); } @@ -353,12 +376,12 @@ impl Renderer { &mut self.device, encoder, target, - translated.into(), + transformation.into(), wgpu_glyph::Region { - x: layer.bounds.x, - y: layer.bounds.y, - width: layer.bounds.width, - height: layer.bounds.height, + x: bounds.x, + y: bounds.y, + width: bounds.width, + height: bounds.height, }, ) .expect("Draw text"); diff --git a/wgpu/src/renderer/target.rs b/wgpu/src/renderer/target.rs index d9d05bf01a..eeeb629aaa 100644 --- a/wgpu/src/renderer/target.rs +++ b/wgpu/src/renderer/target.rs @@ -6,6 +6,7 @@ pub struct Target { surface: wgpu::Surface, width: u16, height: u16, + dpi: f32, transformation: Transformation, swap_chain: wgpu::SwapChain, } @@ -15,6 +16,10 @@ impl Target { (self.width, self.height) } + pub fn dpi(&self) -> f32 { + self.dpi + } + pub fn transformation(&self) -> Transformation { self.transformation } @@ -31,6 +36,7 @@ impl iced_native::renderer::Target for Target { window: &W, width: u16, height: u16, + dpi: f32, renderer: &Renderer, ) -> Target { let surface = wgpu::Surface::create(window); @@ -41,14 +47,22 @@ impl iced_native::renderer::Target for Target { surface, width, height, + dpi, transformation: Transformation::orthographic(width, height), swap_chain, } } - fn resize(&mut self, width: u16, height: u16, renderer: &Renderer) { + fn resize( + &mut self, + width: u16, + height: u16, + dpi: f32, + renderer: &Renderer, + ) { self.width = width; self.height = height; + self.dpi = dpi; self.transformation = Transformation::orthographic(width, height); self.swap_chain = new_swap_chain(&self.surface, width, height, &renderer.device); diff --git a/wgpu/src/transformation.rs b/wgpu/src/transformation.rs index b0d14cc88c..5216538747 100644 --- a/wgpu/src/transformation.rs +++ b/wgpu/src/transformation.rs @@ -26,6 +26,11 @@ impl Transformation { pub fn translate(x: f32, y: f32) -> Transformation { Transformation(Mat4::from_translation(Vec3::new(x, y, 0.0))) } + + /// Creates a scale transformation. + pub fn scale(x: f32, y: f32) -> Transformation { + Transformation(Mat4::from_scale(Vec3::new(x, y, 0.0))) + } } impl Mul for Transformation { diff --git a/winit/src/application.rs b/winit/src/application.rs index 5d1aae3802..3b908189e1 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -38,20 +38,19 @@ pub trait Application { .build(&event_loop) .expect("Open window"); - let mut size: Size = window - .inner_size() - .to_physical(window.hidpi_factor()) - .into(); - let mut new_size: Option = None; + let dpi = window.hidpi_factor(); + let mut size = window.inner_size(); + let mut new_size: Option = None; let mut renderer = Self::Renderer::new(); - let mut target = ::Target::new( - &window, - size.width, - size.height, - &renderer, - ); + let mut target = { + let (width, height) = to_physical(size, dpi); + + ::Target::new( + &window, width, height, dpi as f32, &renderer, + ) + }; debug.layout_started(); let user_interface = UserInterface::build( @@ -134,7 +133,15 @@ pub trait Application { debug.render_started(); if let Some(new_size) = new_size.take() { - target.resize(new_size.width, new_size.height, &renderer); + let dpi = window.hidpi_factor(); + let (width, height) = to_physical(new_size, dpi); + + target.resize( + width, + height, + window.hidpi_factor() as f32, + &renderer, + ); size = new_size; } @@ -160,13 +167,9 @@ pub trait Application { .. } => match window_event { WindowEvent::CursorMoved { position, .. } => { - // TODO: Remove when renderer supports HiDPI - let physical_position = - position.to_physical(window.hidpi_factor()); - events.push(Event::Mouse(mouse::Event::CursorMoved { - x: physical_position.x as f32, - y: physical_position.y as f32, + x: position.x as f32, + y: position.y as f32, })); } WindowEvent::MouseInput { button, state, .. } => { @@ -190,15 +193,11 @@ pub trait Application { )); } winit::event::MouseScrollDelta::PixelDelta(position) => { - // TODO: Remove when renderer supports HiDPI - let physical_position = - position.to_physical(window.hidpi_factor()); - events.push(Event::Mouse( mouse::Event::WheelScrolled { delta: mouse::ScrollDelta::Pixels { - x: physical_position.x as f32, - y: physical_position.y as f32, + x: position.x as f32, + y: position.y as f32, }, }, )); @@ -235,8 +234,7 @@ pub trait Application { *control_flow = ControlFlow::Exit; } WindowEvent::Resized(size) => { - new_size = - Some(size.to_physical(window.hidpi_factor()).into()); + new_size = Some(size.into()); log::debug!("Resized: {:?}", new_size); } @@ -249,24 +247,18 @@ pub trait Application { } } -#[derive(Debug, Clone, Copy, Eq, PartialEq)] -struct Size { - width: u16, - height: u16, -} +fn to_physical(size: winit::dpi::LogicalSize, dpi: f64) -> (u16, u16) { + let physical_size = size.to_physical(dpi); -impl From for Size { - fn from(physical_size: winit::dpi::PhysicalSize) -> Self { - Self { - width: physical_size.width.round() as u16, - height: physical_size.height.round() as u16, - } - } + ( + physical_size.width.round() as u16, + physical_size.height.round() as u16, + ) } fn document<'a, Application>( application: &'a mut Application, - size: Size, + size: winit::dpi::LogicalSize, debug: &mut Debug, ) -> Element<'a, Application::Message, Application::Renderer> where @@ -278,8 +270,8 @@ where debug.view_finished(); Column::new() - .width(Length::Units(size.width)) - .height(Length::Units(size.height)) + .width(Length::Units(size.width.round() as u16)) + .height(Length::Units(size.height.round() as u16)) .push(view) .into() } From 5ff05b7f02b2ff1b0859a9a61ca7e1af6476424f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 5 Nov 2019 20:40:17 +0100 Subject: [PATCH 2/6] Apply HiDPI scaling to quads The anti-aliasing strategy is pretty naive, but we will manage for now. --- wgpu/src/quad.rs | 58 +++++++++++++++++++++++++--------- wgpu/src/renderer.rs | 7 ++-- wgpu/src/shader/quad.frag | 8 ++--- wgpu/src/shader/quad.frag.spv | Bin 3212 -> 3044 bytes wgpu/src/shader/quad.vert | 20 +++++++----- wgpu/src/shader/quad.vert.spv | Bin 2544 -> 3020 bytes 6 files changed, 61 insertions(+), 32 deletions(-) diff --git a/wgpu/src/quad.rs b/wgpu/src/quad.rs index bfbd7e2d61..4356f8afd5 100644 --- a/wgpu/src/quad.rs +++ b/wgpu/src/quad.rs @@ -6,7 +6,7 @@ use std::mem; pub struct Pipeline { pipeline: wgpu::RenderPipeline, constants: wgpu::BindGroup, - transform: wgpu::Buffer, + constants_buffer: wgpu::Buffer, vertices: wgpu::Buffer, indices: wgpu::Buffer, instances: wgpu::Buffer, @@ -23,20 +23,20 @@ impl Pipeline { }], }); - let transform = device + let constants_buffer = device .create_buffer_mapped( - 16, + 1, wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, ) - .fill_from_slice(Transformation::identity().as_ref()); + .fill_from_slice(&[Uniforms::default()]); let constants = device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &constant_layout, bindings: &[wgpu::Binding { binding: 0, resource: wgpu::BindingResource::Buffer { - buffer: &transform, - range: 0..64, + buffer: &constants_buffer, + range: 0..std::mem::size_of::() as u64, }, }], }); @@ -124,7 +124,7 @@ impl Pipeline { }, wgpu::VertexAttributeDescriptor { shader_location: 4, - format: wgpu::VertexFormat::Uint, + format: wgpu::VertexFormat::Float, offset: 4 * (2 + 2 + 4), }, ], @@ -151,7 +151,7 @@ impl Pipeline { Pipeline { pipeline, constants, - transform, + constants_buffer, vertices, indices, instances, @@ -164,19 +164,22 @@ impl Pipeline { encoder: &mut wgpu::CommandEncoder, instances: &[Quad], transformation: Transformation, + scale: f32, bounds: Rectangle, target: &wgpu::TextureView, ) { - let transform_buffer = device - .create_buffer_mapped(16, wgpu::BufferUsage::COPY_SRC) - .fill_from_slice(transformation.as_ref()); + let uniforms = Uniforms::new(transformation, scale); + + let constants_buffer = device + .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC) + .fill_from_slice(&[uniforms]); encoder.copy_buffer_to_buffer( - &transform_buffer, + &constants_buffer, 0, - &self.transform, + &self.constants_buffer, 0, - 16 * 4, + std::mem::size_of::() as u64, ); let mut i = 0; @@ -271,9 +274,34 @@ pub struct Quad { pub position: [f32; 2], pub scale: [f32; 2], pub color: [f32; 4], - pub border_radius: u32, + pub border_radius: f32, } impl Quad { const MAX: usize = 100_000; } + +#[repr(C)] +#[derive(Debug, Clone, Copy)] +struct Uniforms { + transform: [f32; 16], + scale: f32, +} + +impl Uniforms { + fn new(transformation: Transformation, scale: f32) -> Uniforms { + Self { + transform: *transformation.as_ref(), + scale, + } + } +} + +impl Default for Uniforms { + fn default() -> Self { + Self { + transform: *Transformation::identity().as_ref(), + scale: 1.0, + } + } +} diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 7ac74e9349..f46acb8c7f 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -239,7 +239,7 @@ impl Renderer { color: match background { Background::Color(color) => color.into_linear(), }, - border_radius: *border_radius as u32, + border_radius: *border_radius as f32, }); } Primitive::Image { path, bounds } => { @@ -327,13 +327,14 @@ impl Renderer { encoder, &layer.quads, transformation, + dpi, bounds, target, ); } if layer.images.len() > 0 { - let translated = transformation + let translated_and_scaled = transformation * Transformation::scale(dpi, dpi) * Transformation::translate( -(layer.offset.x as f32), @@ -344,7 +345,7 @@ impl Renderer { &mut self.device, encoder, &layer.images, - translated, + translated_and_scaled, bounds, target, ); diff --git a/wgpu/src/shader/quad.frag b/wgpu/src/shader/quad.frag index 849f581e9e..2ee77e710a 100644 --- a/wgpu/src/shader/quad.frag +++ b/wgpu/src/shader/quad.frag @@ -3,7 +3,7 @@ layout(location = 0) in vec4 v_Color; layout(location = 1) in vec2 v_Pos; layout(location = 2) in vec2 v_Scale; -layout(location = 3) in flat uint v_BorderRadius; +layout(location = 3) in float v_BorderRadius; layout(location = 0) out vec4 o_Color; @@ -27,11 +27,7 @@ float rounded(in vec2 frag_coord, in vec2 position, in vec2 size, float radius, } void main() { - float radius_alpha = 1.0; - - if(v_BorderRadius > 0.0) { - radius_alpha = rounded(gl_FragCoord.xy, v_Pos, v_Scale, v_BorderRadius, 0.5); - } + float radius_alpha = rounded(gl_FragCoord.xy, v_Pos, v_Scale, v_BorderRadius, 0.5); o_Color = vec4(v_Color.xyz, v_Color.w * radius_alpha); } diff --git a/wgpu/src/shader/quad.frag.spv b/wgpu/src/shader/quad.frag.spv index 71b91b44f7cc7dda15caea27aeb00d1d99874601..17bd8f465ada98f5308097b3ce01c65532cdf70a 100644 GIT binary patch delta 1057 zcmZ9LO)mpc6o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ja8EnzI4y24JrR8(VF z&WDYIYGbEz5F9CSQhlKA;6MB`@ta9}ny-J+<=!bl3wcyKo&}>!e`_+z*uH!qZ)jGbUdPqMJ?*CG2p%#9u)SB9tifnvG z$NHFXn~)Or@D^WvPYSZ>@q|Dv*z_2iy0PgoHiAu$v8fN69>?S9F}CH)dE4UYEq>mA zLls%?`eh@=aXuWYapZ&}mvQKDtiz5N~!iVvlqBFx**z9K>1u^lnl> zxG#1lvf4KNR)w!NP_5=&0}tV04~@x#)IG5-ae?#IfsDy7zo0($^H zR;z{iVK24LX{8VB!H>;6!Qp?Tm00s*Ghep1T0%y@f>=yzup`FYZ z?@O*%T6tSIa$z%L_TF1wCmuiV&&-i~UNcP`vx896A2G~3vCLARz#Ue_^7exQ^TJFl zDTce96X1)AB=)@L?2TD%|A=(<(<9qIDjnYTk4a|_yyYO4cN`a(k!{t(*_S=1rNi+( z5yu@Z=N0M1;v*+>XF1Z+sTnaYBOQ($#!Y&To*9?*9KAE{s&wvv7ulPh~>K_9WI{lw&&vcraTwVH!Y5SAeQfrbhvoF8PCP@-Sr&#thf9}pB6I0 zq>vTNcV9aF;Qri~`lY`i0O)s;Gk6B?3 z9}D~*;Ny1#+ve!0bmj;jzX{mpTb9m@;^X%L8z0~OnvjpUgv)uG+1`@=THxE`TP2=a z-w5z#7o}6@FYUp Date: Tue, 5 Nov 2019 20:43:09 +0100 Subject: [PATCH 3/6] Fix checkbox border radius --- wgpu/src/renderer/widget/checkbox.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wgpu/src/renderer/widget/checkbox.rs b/wgpu/src/renderer/widget/checkbox.rs index ea7a4c0b54..5867a36f31 100644 --- a/wgpu/src/renderer/widget/checkbox.rs +++ b/wgpu/src/renderer/widget/checkbox.rs @@ -76,7 +76,7 @@ impl checkbox::Renderer for Renderer { a: 1.0, } }), - border_radius: 6, + border_radius: 5, }, ); From 1e021fd034cc2b7df850c867371a81a4765582ba Mon Sep 17 00:00:00 2001 From: Matthias Fauconneau Date: Mon, 4 Nov 2019 02:10:39 +0100 Subject: [PATCH 4/6] Fix Tour for HiDPI (stub) --- examples/tour.rs | 2 +- wgpu/src/renderer/widget/text.rs | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/tour.rs b/examples/tour.rs index c5daa2c570..238b04f1db 100644 --- a/examples/tour.rs +++ b/examples/tour.rs @@ -76,7 +76,7 @@ impl Application for Tour { } let element: Element<_> = Column::new() - .max_width(Length::Units(540)) + .max_width(Length::Units((540.0*3.0) as u16)) .spacing(20) .padding(20) .push(steps.view(self.debug).map(Message::StepMessage)) diff --git a/wgpu/src/renderer/widget/text.rs b/wgpu/src/renderer/widget/text.rs index 82f75f0918..082ce640f4 100644 --- a/wgpu/src/renderer/widget/text.rs +++ b/wgpu/src/renderer/widget/text.rs @@ -6,6 +6,8 @@ use wgpu_glyph::{GlyphCruncher, Section}; use std::cell::RefCell; use std::f32; +pub const DEFAULT_TEXT_SIZE : f32 = 20.0*3.0; + impl text::Renderer for Renderer { fn node(&self, text: &Text) -> Node { let glyph_brush = self.glyph_brush.clone(); @@ -18,7 +20,7 @@ impl text::Renderer for Renderer { // I noticed that the first measure is the one that matters in // practice. Here, we use a RefCell to store the cached measurement. let measure = RefCell::new(None); - let size = text.size.map(f32::from).unwrap_or(20.0); + let size = text.size.map(f32::from).unwrap_or(DEFAULT_TEXT_SIZE); let style = Style::default().width(text.width); @@ -71,7 +73,7 @@ impl text::Renderer for Renderer { ( Primitive::Text { content: text.content.clone(), - size: f32::from(text.size.unwrap_or(20)), + size: text.size.map(f32::from).unwrap_or(DEFAULT_TEXT_SIZE), bounds: layout.bounds(), color: text.color.unwrap_or(Color::BLACK), horizontal_alignment: text.horizontal_alignment, From 2026048053a09d72a01537712561ff5bfdfdf953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 6 Nov 2019 02:47:01 +0100 Subject: [PATCH 5/6] Remove hardcoded HiDPI scaling --- examples/tour.rs | 2 +- wgpu/src/renderer/widget/text.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/tour.rs b/examples/tour.rs index 238b04f1db..c5daa2c570 100644 --- a/examples/tour.rs +++ b/examples/tour.rs @@ -76,7 +76,7 @@ impl Application for Tour { } let element: Element<_> = Column::new() - .max_width(Length::Units((540.0*3.0) as u16)) + .max_width(Length::Units(540)) .spacing(20) .padding(20) .push(steps.view(self.debug).map(Message::StepMessage)) diff --git a/wgpu/src/renderer/widget/text.rs b/wgpu/src/renderer/widget/text.rs index 082ce640f4..29e07ff7d6 100644 --- a/wgpu/src/renderer/widget/text.rs +++ b/wgpu/src/renderer/widget/text.rs @@ -6,7 +6,8 @@ use wgpu_glyph::{GlyphCruncher, Section}; use std::cell::RefCell; use std::f32; -pub const DEFAULT_TEXT_SIZE : f32 = 20.0*3.0; +// TODO: Obtain from renderer configuration +const DEFAULT_TEXT_SIZE: f32 = 20.0; impl text::Renderer for Renderer { fn node(&self, text: &Text) -> Node { From 6216f197e927aef0993a36c1bb094c7f917d54dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 6 Nov 2019 19:35:28 +0100 Subject: [PATCH 6/6] Fix panic in `Transformation::scale` --- wgpu/src/transformation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wgpu/src/transformation.rs b/wgpu/src/transformation.rs index 5216538747..c8a7ee7536 100644 --- a/wgpu/src/transformation.rs +++ b/wgpu/src/transformation.rs @@ -29,7 +29,7 @@ impl Transformation { /// Creates a scale transformation. pub fn scale(x: f32, y: f32) -> Transformation { - Transformation(Mat4::from_scale(Vec3::new(x, y, 0.0))) + Transformation(Mat4::from_scale(Vec3::new(x, y, 1.0))) } }