Skip to content

Commit b0798e8

Browse files
timon-schellingKeavon
authored andcommitted
less weird resize frames
1 parent eb53bb5 commit b0798e8

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

desktop/src/render/composite_shader.wgsl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,19 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
4343
return ui_linear;
4444
}
4545

46+
// UI texture is premultiplied, we need to unpremultiply before blending
47+
let ui_srgb = linear_to_srgb(unpremultiply(ui_linear));
48+
4649
let viewport_coordinate = (in.tex_coords - constants.viewport_offset) * constants.viewport_scale;
4750

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+
4856
let overlay_srgb = textureSample(t_overlays, s_diffuse, viewport_coordinate);
4957
let viewport_srgb = textureSample(t_viewport, s_diffuse, viewport_coordinate);
5058

51-
// UI texture is premultiplied, we need to unpremultiply before blending
52-
let ui_srgb = linear_to_srgb(unpremultiply(ui_linear));
53-
5459
if (overlay_srgb.a < 0.001) {
5560
if (ui_srgb.a < 0.001) {
5661
return srgb_to_linear(viewport_srgb);

desktop/src/render/state.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub(crate) struct RenderState {
1212
render_pipeline: wgpu::RenderPipeline,
1313
transparent_texture: wgpu::Texture,
1414
sampler: wgpu::Sampler,
15+
desired_width: u32,
16+
desired_height: u32,
1517
viewport_scale: [f32; 2],
1618
viewport_offset: [f32; 2],
1719
viewport_texture: Option<wgpu::Texture>,
@@ -171,6 +173,8 @@ impl RenderState {
171173
render_pipeline,
172174
transparent_texture,
173175
sampler,
176+
desired_width: size.width,
177+
desired_height: size.height,
174178
viewport_scale: [1.0, 1.0],
175179
viewport_offset: [0.0, 0.0],
176180
viewport_texture: None,
@@ -182,11 +186,8 @@ impl RenderState {
182186
}
183187

184188
pub(crate) fn resize(&mut self, width: u32, height: u32) {
185-
if width > 0 && height > 0 && (self.config.width != width || self.config.height != height) {
186-
self.config.width = width;
187-
self.config.height = height;
188-
self.surface.configure(&self.context.device, &self.config);
189-
}
189+
self.desired_width = width;
190+
self.desired_height = height;
190191
}
191192

192193
pub(crate) fn bind_viewport_texture(&mut self, viewport_texture: wgpu::Texture) {
@@ -200,8 +201,17 @@ impl RenderState {
200201
}
201202

202203
pub(crate) fn bind_ui_texture(&mut self, bind_ui_texture: wgpu::Texture) {
204+
let width = bind_ui_texture.width();
205+
let height = bind_ui_texture.height();
206+
203207
self.ui_texture = Some(bind_ui_texture);
204208
self.update_bindgroup();
209+
210+
if width > 0 && height > 0 && (self.config.width != width || self.config.height != height) {
211+
self.config.width = width;
212+
self.config.height = height;
213+
self.surface.configure(&self.context.device, &self.config);
214+
}
205215
}
206216

207217
pub(crate) fn set_viewport_scale(&mut self, scale: [f32; 2]) {
@@ -235,17 +245,15 @@ impl RenderState {
235245
self.render_overlays(scene);
236246
}
237247

238-
let output = self.surface.get_current_texture().map_err(|e| RenderError::SurfaceError(e))?;
239-
let output_width = output.texture.width();
240-
let output_height = output.texture.height();
248+
let output = self.surface.get_current_texture().map_err(RenderError::SurfaceError)?;
241249

242250
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
243251

244252
let mut encoder = self.context.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("Render Encoder") });
245253

246254
{
247255
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
248-
label: Some("Render Pass"),
256+
label: Some("Graphite Composition Render Pass"),
249257
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
250258
view: &view,
251259
resolve_target: None,
@@ -271,7 +279,7 @@ impl RenderState {
271279
);
272280
if let Some(bind_group) = &self.bind_group {
273281
render_pass.set_bind_group(0, bind_group, &[]);
274-
render_pass.draw(0..6, 0..1); // Draw 3 vertices for fullscreen triangle
282+
render_pass.draw(0..3, 0..1); // Draw 3 vertices for fullscreen triangle
275283
} else {
276284
tracing::warn!("No bind group available - showing clear color only");
277285
}
@@ -281,7 +289,7 @@ impl RenderState {
281289
output.present();
282290

283291
if let Some(ui_texture) = &self.ui_texture
284-
&& (output_width != ui_texture.width() || output_height != ui_texture.height())
292+
&& (self.desired_width != ui_texture.width() || self.desired_height != ui_texture.height())
285293
{
286294
return Err(RenderError::OutdatedUITextureError);
287295
}

0 commit comments

Comments
 (0)