Desktop: Fix missing resize events causing all-gray window on Mac after launch (#3445)

* okayish solution

should be improved at some point but for now it works well enough.

* do leftover renames

* better solution

* less weird resize frames

* move surface reconfiguration

* fix recent desktop mac breakages

* better looking resize on mac

* fix background color

* Fix blank screen on window initialization

* cleanup

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Timon
2025-12-06 23:11:47 +00:00
committed by GitHub
parent 5b472a64b2
commit 2e4481880e
11 changed files with 129 additions and 66 deletions

View File

@@ -23,6 +23,8 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
struct Constants {
viewport_scale: vec2<f32>,
viewport_offset: vec2<f32>,
ui_scale: vec2<f32>,
background_color: vec4<f32>,
};
var<push_constant> constants: Constants;
@@ -38,19 +40,29 @@ var s_diffuse: sampler;
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let ui_linear = srgb_to_linear(textureSample(t_ui, s_diffuse, in.tex_coords));
let ui_coordinate = in.tex_coords * constants.ui_scale;
if (ui_coordinate.x < 0.0 || ui_coordinate.x > 1.0 ||
ui_coordinate.y < 0.0 || ui_coordinate.y > 1.0) {
return srgb_to_linear(constants.background_color);
}
let ui_linear = srgb_to_linear(textureSample(t_ui, s_diffuse, ui_coordinate));
if (ui_linear.a >= 0.999) {
return ui_linear;
}
// UI texture is premultiplied, we need to unpremultiply before blending
let ui_srgb = linear_to_srgb(unpremultiply(ui_linear));
let viewport_coordinate = (in.tex_coords - constants.viewport_offset) * constants.viewport_scale;
if (viewport_coordinate.x < 0.0 || viewport_coordinate.x > 1.0 ||
viewport_coordinate.y < 0.0 || viewport_coordinate.y > 1.0) {
return srgb_to_linear(constants.background_color);
}
let overlay_srgb = textureSample(t_overlays, s_diffuse, viewport_coordinate);
let viewport_srgb = textureSample(t_viewport, s_diffuse, viewport_coordinate);
// UI texture is premultiplied, we need to unpremultiply before blending
let ui_srgb = linear_to_srgb(unpremultiply(ui_linear));
if (overlay_srgb.a < 0.001) {
if (ui_srgb.a < 0.001) {
return srgb_to_linear(viewport_srgb);

View File

@@ -4,7 +4,7 @@ use crate::wrapper::{Color, WgpuContext, WgpuExecutor};
#[derive(derivative::Derivative)]
#[derivative(Debug)]
pub(crate) struct GraphicsState {
pub(crate) struct RenderState {
surface: wgpu::Surface<'static>,
context: WgpuContext,
executor: WgpuExecutor,
@@ -12,6 +12,8 @@ pub(crate) struct GraphicsState {
render_pipeline: wgpu::RenderPipeline,
transparent_texture: wgpu::Texture,
sampler: wgpu::Sampler,
desired_width: u32,
desired_height: u32,
viewport_scale: [f32; 2],
viewport_offset: [f32; 2],
viewport_texture: Option<wgpu::Texture>,
@@ -22,7 +24,7 @@ pub(crate) struct GraphicsState {
overlays_scene: Option<vello::Scene>,
}
impl GraphicsState {
impl RenderState {
pub(crate) fn new(window: &Window, context: WgpuContext) -> Self {
let size = window.surface_size();
let surface = window.create_surface(context.instance.clone());
@@ -171,6 +173,8 @@ impl GraphicsState {
render_pipeline,
transparent_texture,
sampler,
desired_width: size.width,
desired_height: size.height,
viewport_scale: [1.0, 1.0],
viewport_offset: [0.0, 0.0],
viewport_texture: None,
@@ -182,6 +186,13 @@ impl GraphicsState {
}
pub(crate) fn resize(&mut self, width: u32, height: u32) {
if width == self.desired_width && height == self.desired_height {
return;
}
self.desired_width = width;
self.desired_height = height;
if width > 0 && height > 0 && (self.config.width != width || self.config.height != height) {
self.config.width = width;
self.config.height = height;
@@ -230,24 +241,33 @@ impl GraphicsState {
self.bind_overlays_texture(texture);
}
pub(crate) fn render(&mut self, window: &Window) -> Result<(), wgpu::SurfaceError> {
pub(crate) fn render(&mut self, window: &Window) -> Result<(), RenderError> {
let ui_scale = if let Some(ui_texture) = &self.ui_texture
&& (self.desired_width != ui_texture.width() || self.desired_height != ui_texture.height())
{
Some([self.desired_width as f32 / ui_texture.width() as f32, self.desired_height as f32 / ui_texture.height() as f32])
} else {
None
};
if let Some(scene) = self.overlays_scene.take() {
self.render_overlays(scene);
}
let output = self.surface.get_current_texture()?;
let output = self.surface.get_current_texture().map_err(RenderError::SurfaceError)?;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self.context.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("Render Encoder") });
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
label: Some("Graphite Composition Render Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.01, g: 0.01, b: 0.01, a: 1.0 }),
load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.01, g: 0.01, b: 0.01, a: 1. }),
store: wgpu::StoreOp::Store,
},
depth_slice: None,
@@ -264,11 +284,14 @@ impl GraphicsState {
bytemuck::bytes_of(&Constants {
viewport_scale: self.viewport_scale,
viewport_offset: self.viewport_offset,
ui_scale: ui_scale.unwrap_or([1., 1.]),
_pad: [0., 0.],
background_color: [0x22 as f32 / 0xff as f32, 0x22 as f32 / 0xff as f32, 0x22 as f32 / 0xff as f32, 1.], // #222222
}),
);
if let Some(bind_group) = &self.bind_group {
render_pass.set_bind_group(0, bind_group, &[]);
render_pass.draw(0..6, 0..1); // Draw 3 vertices for fullscreen triangle
render_pass.draw(0..3, 0..1); // Draw 3 vertices for fullscreen triangle
} else {
tracing::warn!("No bind group available - showing clear color only");
}
@@ -277,6 +300,10 @@ impl GraphicsState {
window.pre_present_notify();
output.present();
if ui_scale.is_some() {
return Err(RenderError::OutdatedUITextureError);
}
Ok(())
}
@@ -312,9 +339,17 @@ impl GraphicsState {
}
}
pub(crate) enum RenderError {
OutdatedUITextureError,
SurfaceError(wgpu::SurfaceError),
}
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Constants {
viewport_scale: [f32; 2],
viewport_offset: [f32; 2],
ui_scale: [f32; 2],
_pad: [f32; 2],
background_color: [f32; 4],
}