summaryrefslogtreecommitdiffstats
path: root/wgpu/src/renderer
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--wgpu/src/renderer.rs56
-rw-r--r--wgpu/src/renderer/target.rs16
-rw-r--r--wgpu/src/renderer/widget/checkbox.rs2
-rw-r--r--wgpu/src/renderer/widget/text.rs7
4 files changed, 61 insertions, 20 deletions
diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs
index 060f07a3..f46acb8c 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 f32,
});
}
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,26 @@ impl Renderer {
encoder,
&layer.quads,
transformation,
- layer.bounds,
+ dpi,
+ bounds,
target,
);
}
if layer.images.len() > 0 {
+ let translated_and_scaled = 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,
+ translated_and_scaled,
+ bounds,
target,
);
}
@@ -345,6 +355,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 +377,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 d9d05bf0..eeeb629a 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/renderer/widget/checkbox.rs b/wgpu/src/renderer/widget/checkbox.rs
index ea7a4c0b..5867a36f 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,
},
);
diff --git a/wgpu/src/renderer/widget/text.rs b/wgpu/src/renderer/widget/text.rs
index 82f75f09..29e07ff7 100644
--- a/wgpu/src/renderer/widget/text.rs
+++ b/wgpu/src/renderer/widget/text.rs
@@ -6,6 +6,9 @@ use wgpu_glyph::{GlyphCruncher, Section};
use std::cell::RefCell;
use std::f32;
+// TODO: Obtain from renderer configuration
+const DEFAULT_TEXT_SIZE: f32 = 20.0;
+
impl text::Renderer for Renderer {
fn node(&self, text: &Text) -> Node {
let glyph_brush = self.glyph_brush.clone();
@@ -18,7 +21,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 +74,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,