Fix scissor panic

Removes the Scale::physical_resolution method as it could become
desynced from the renderer resolution causing the panic
This commit is contained in:
João Capucho 2021-03-17 16:41:54 +00:00 committed by Imbris
parent f5dc871c59
commit e04970addd
6 changed files with 19 additions and 24 deletions

View File

@ -116,7 +116,7 @@ vek = { git = "https://gitlab.com/veloren/vek.git", branch = "fix_intrinsics2" }
# patch wgpu so we can use wgpu-profiler crate
wgpu = { git = "https://github.com/gfx-rs/wgpu-rs.git", rev = "a7e03546c1584cefef5b926f923108ea2cd1c146" }
# Uncomment this to use a local fork of wgpu (for testing purposes)
# # Uncomment this to use a local fork of wgpu (for testing purposes)
# [patch.'https://github.com/gfx-rs/wgpu']
# wgpu-core = { path = "../wgpu/wgpu-core" }
# wgpu-types = { path = "../wgpu/wgpu-types" }

View File

@ -713,12 +713,11 @@ impl<'pass_ref, 'pass: 'pass_ref> PreparedUiDrawer<'pass_ref, 'pass> {
pub fn set_scissor<'data: 'pass>(&mut self, scissor: Aabr<u16>) {
let Aabr { min, max } = scissor;
dbg!(&scissor);
self.render_pass.set_scissor_rect(
min.x as u32,
min.y as u32,
dbg!((max.x - min.x) as u32),
dbg!((max.y - min.y) as u32),
(max.x - min.x) as u32,
(max.y - min.y) as u32,
);
}

View File

@ -46,7 +46,7 @@ impl IcedUi {
let renderer = window.renderer_mut();
let scaled_resolution = scale.scaled_resolution().map(|e| e as f32);
let physical_resolution = scale.physical_resolution();
let physical_resolution = renderer.resolution();
// TODO: examine how much mem fonts take up and reduce clones if significant
Ok(Self {
@ -163,7 +163,7 @@ impl IcedUi {
// Avoid panic in graphic cache when minimizing.
// Somewhat inefficient for elements that won't change size after a window
// resize
let physical_resolution = self.scale.physical_resolution();
let physical_resolution = renderer.resolution();
if physical_resolution.map(|e| e > 0).reduce_and() {
self.renderer
.resize(scaled_resolution, physical_resolution, renderer);

View File

@ -118,7 +118,7 @@ impl IcedRenderer {
pub fn new(
renderer: &mut Renderer,
scaled_resolution: Vec2<f32>,
physical_resolution: Vec2<u16>,
physical_resolution: Vec2<u32>,
default_font: Font,
) -> Result<Self, Error> {
let (half_res, align, p_scale) =
@ -170,7 +170,7 @@ impl IcedRenderer {
pub fn resize(
&mut self,
scaled_resolution: Vec2<f32>,
physical_resolution: Vec2<u16>,
physical_resolution: Vec2<u32>,
renderer: &mut Renderer,
) {
self.win_dims = scaled_resolution;
@ -320,7 +320,7 @@ impl IcedRenderer {
// Returns (half_res, align)
fn calculate_resolution_dependents(
res: Vec2<u16>,
res: Vec2<u32>,
win_dims: Vec2<f32>,
) -> (Vec2<f32>, Vec2<f32>, f32) {
let half_res = res.map(|e| e as f32 / 2.0);
@ -331,7 +331,7 @@ impl IcedRenderer {
(half_res, align, p_scale)
}
fn update_resolution_dependents(&mut self, res: Vec2<u16>) {
fn update_resolution_dependents(&mut self, res: Vec2<u32>) {
let (half_res, align, p_scale) = Self::calculate_resolution_dependents(res, self.win_dims);
self.half_res = half_res;
self.align = align;
@ -800,7 +800,7 @@ impl IcedRenderer {
// Given the the resolution determines the offset needed to align integer
// offsets from the center of the sceen to pixels
#[inline(always)]
fn align(res: Vec2<u16>) -> Vec2<f32> {
fn align(res: Vec2<u32>) -> Vec2<f32> {
// TODO: does this logic still apply in iced's coordinate system?
// If the resolution is odd then the center of the screen will be within the
// middle of a pixel so we need to offset by 0.5 pixels to be on the edge of
@ -808,13 +808,13 @@ fn align(res: Vec2<u16>) -> Vec2<f32> {
res.map(|e| (e & 1) as f32 * 0.5)
}
fn default_scissor(physical_resolution: Vec2<u16>) -> Aabr<u16> {
fn default_scissor(physical_resolution: Vec2<u32>) -> Aabr<u16> {
let (screen_w, screen_h) = physical_resolution.into_tuple();
Aabr {
min: Vec2 { x: 0, y: 0 },
max: Vec2 {
x: screen_w,
y: screen_h,
x: screen_w as u16,
y: screen_h as u16,
},
}
}

View File

@ -136,10 +136,11 @@ impl Ui {
pub fn new(window: &mut Window) -> Result<Self, Error> {
let scale = Scale::new(window, ScaleMode::Absolute(1.0), 1.0);
let win_dims = scale.scaled_resolution().into_array();
let physical_resolution = scale.physical_resolution();
let renderer = window.renderer_mut();
let physical_resolution = renderer.resolution();
let mut ui = UiBuilder::new(win_dims).build();
// NOTE: Since we redraw the actual frame each time whether or not the UI needs
// to be updated, there's no reason to set the redraw count higher than
@ -340,7 +341,7 @@ impl Ui {
self.scale.window_resized(new_dims);
let (w, h) = self.scale.scaled_resolution().into_tuple();
self.ui.handle_event(Input::Resize(w, h));
self.window_scissor = default_scissor(self.scale.physical_resolution());
self.window_scissor = default_scissor(renderer.resolution());
// Avoid panic in graphic cache when minimizing.
// Avoid resetting cache if window size didn't change
@ -1046,13 +1047,13 @@ impl Ui {
}
}
fn default_scissor(physical_resolution: Vec2<u16>) -> Aabr<u16> {
fn default_scissor(physical_resolution: Vec2<u32>) -> Aabr<u16> {
let (screen_w, screen_h) = physical_resolution.into_tuple();
Aabr {
min: Vec2 { x: 0, y: 0 },
max: Vec2 {
x: screen_w,
y: screen_h,
x: screen_w as u16,
y: screen_h as u16,
},
}
}

View File

@ -103,11 +103,6 @@ impl Scale {
/// Get logical window size
pub fn logical_resolution(&self) -> Vec2<f64> { self.window_dims }
/// Get physical window size
pub fn physical_resolution(&self) -> Vec2<u16> {
(self.window_dims * self.scale_factor).map(|e| e.round() as u16)
}
// Transform point from logical to scaled coordinates.
pub fn scale_point(&self, point: Vec2<f64>) -> Vec2<f64> { point / self.scale_factor_logical() }
}