Skip to content

Commit c036630

Browse files
timon-schellingKeavon
authored andcommitted
better looking resize on mac
1 parent a9f76d3 commit c036630

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

desktop/src/app.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ impl ApplicationHandler for App {
442442
WindowEvent::RedrawRequested => {
443443
let Some(render_state) = &mut self.render_state else { return };
444444
if let Some(window) = &self.window {
445+
let size = window.surface_size();
446+
render_state.resize(size.width, size.height);
447+
445448
match render_state.render(window) {
446449
Ok(_) => {}
447450
Err(RenderError::OutdatedUITextureError) => {

desktop/src/render/composite_shader.wgsl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
2323
struct Constants {
2424
viewport_scale: vec2<f32>,
2525
viewport_offset: vec2<f32>,
26+
ui_scale: vec2<f32>,
27+
background_color: vec4<f32>,
2628
};
2729

2830
var<push_constant> constants: Constants;
@@ -38,7 +40,13 @@ var s_diffuse: sampler;
3840

3941
@fragment
4042
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
41-
let ui_linear = srgb_to_linear(textureSample(t_ui, s_diffuse, in.tex_coords));
43+
let ui_coordinate = in.tex_coords * constants.ui_scale;
44+
if (ui_coordinate.x < 0.0 || ui_coordinate.x > 1.0 ||
45+
ui_coordinate.y < 0.0 || ui_coordinate.y > 1.0) {
46+
return srgb_to_linear(vec4<f32>(0.1289, 0.1289, 0.1289, 1.0));
47+
}
48+
49+
let ui_linear = srgb_to_linear(textureSample(t_ui, s_diffuse, ui_coordinate));
4250
if (ui_linear.a >= 0.999) {
4351
return ui_linear;
4452
}
@@ -48,11 +56,6 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
4856

4957
let viewport_coordinate = (in.tex_coords - constants.viewport_offset) * constants.viewport_scale;
5058

51-
if (viewport_coordinate.x < 0.0 || viewport_coordinate.x > 1.0 ||
52-
viewport_coordinate.y < 0.0 || viewport_coordinate.y > 1.0) {
53-
return srgb_to_linear(blend(ui_srgb, vec4<f32>(0.1289, 0.1289, 0.1289, 1.0)));
54-
}
55-
5659
let overlay_srgb = textureSample(t_overlays, s_diffuse, viewport_coordinate);
5760
let viewport_srgb = textureSample(t_viewport, s_diffuse, viewport_coordinate);
5861

desktop/src/render/state.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ impl RenderState {
186186
}
187187

188188
pub(crate) fn resize(&mut self, width: u32, height: u32) {
189+
if width == self.desired_width && height == self.desired_height {
190+
return;
191+
}
192+
189193
self.desired_width = width;
190194
self.desired_height = height;
191195

@@ -238,6 +242,14 @@ impl RenderState {
238242
}
239243

240244
pub(crate) fn render(&mut self, window: &Window) -> Result<(), RenderError> {
245+
let ui_scale = if let Some(ui_texture) = &self.ui_texture
246+
&& (self.desired_width != ui_texture.width() || self.desired_height != ui_texture.height())
247+
{
248+
Some([self.desired_width as f32 / ui_texture.width() as f32, self.desired_height as f32 / ui_texture.height() as f32])
249+
} else {
250+
None
251+
};
252+
241253
if let Some(scene) = self.overlays_scene.take() {
242254
self.render_overlays(scene);
243255
}
@@ -255,7 +267,7 @@ impl RenderState {
255267
view: &view,
256268
resolve_target: None,
257269
ops: wgpu::Operations {
258-
load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.01, g: 0.01, b: 0.01, a: 1.0 }),
270+
load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.01, g: 0.01, b: 0.01, a: 1. }),
259271
store: wgpu::StoreOp::Store,
260272
},
261273
depth_slice: None,
@@ -272,6 +284,8 @@ impl RenderState {
272284
bytemuck::bytes_of(&Constants {
273285
viewport_scale: self.viewport_scale,
274286
viewport_offset: self.viewport_offset,
287+
ui_scale: ui_scale.unwrap_or([1., 1.]),
288+
background_color: [255. / 33., 255. / 33., 255. / 33., 255. / 33.], // #212121
275289
}),
276290
);
277291
if let Some(bind_group) = &self.bind_group {
@@ -285,9 +299,7 @@ impl RenderState {
285299
window.pre_present_notify();
286300
output.present();
287301

288-
if let Some(ui_texture) = &self.ui_texture
289-
&& (self.desired_width != ui_texture.width() || self.desired_height != ui_texture.height())
290-
{
302+
if ui_scale.is_some() {
291303
return Err(RenderError::OutdatedUITextureError);
292304
}
293305

@@ -336,4 +348,6 @@ pub(crate) enum RenderError {
336348
struct Constants {
337349
viewport_scale: [f32; 2],
338350
viewport_offset: [f32; 2],
351+
ui_scale: [f32; 2],
352+
background_color: [f32; 4],
339353
}

0 commit comments

Comments
 (0)