Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Reduce overdraw for border-only quads (#61274)

John Tur0c51c7fd2481
diff --git a/crates/gpui/src/scene.rs b/crates/gpui/src/scene.rs
index ea2ad7b2b90..ea0f5d7e31a 100644
--- a/crates/gpui/src/scene.rs
+++ b/crates/gpui/src/scene.rs
@@ -495,6 +495,40 @@ pub enum PrimitiveBatch {
Surfaces(Range<usize>),
}
+impl PrimitiveBatch {
+ #[expect(missing_docs)]
+ pub fn label(&self) -> String {
+ match self {
+ Self::Shadows(range) => format!("shadows ({})", range.len()),
+ Self::Quads(range) => format!("quads ({})", range.len()),
+ Self::Paths(range) => format!("paths ({})", range.len()),
+ Self::Underlines(range) => format!("underlines ({})", range.len()),
+ Self::MonochromeSprites { texture_id, range } => {
+ format!(
+ "monochrome sprites ({}) on atlas {}",
+ range.len(),
+ texture_id.index
+ )
+ }
+ Self::SubpixelSprites { texture_id, range } => {
+ format!(
+ "subpixel sprites ({}) on atlas {}",
+ range.len(),
+ texture_id.index
+ )
+ }
+ Self::PolychromeSprites { texture_id, range } => {
+ format!(
+ "polychrome sprites ({}) on atlas {}",
+ range.len(),
+ texture_id.index
+ )
+ }
+ Self::Surfaces(range) => format!("surfaces ({})", range.len()),
+ }
+ }
+}
+
#[derive(Default, Debug, Copy, Clone)]
#[repr(C)]
#[expect(missing_docs)]
diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
index 6a2bbf7874b..8667d605a34 100644
--- a/crates/gpui/src/window.rs
+++ b/crates/gpui/src/window.rs
@@ -3835,7 +3835,7 @@ impl Window {
let opacity = self.element_opacity();
let snapped_bounds = self.snap_bounds(quad.bounds);
let snapped_border_widths = self.snap_border_widths(quad.border_widths);
- self.next_frame.scene.insert_primitive(Quad {
+ let quad = Quad {
order: 0,
bounds: snapped_bounds,
content_mask: self.snapped_content_mask(),
@@ -3844,7 +3844,76 @@ impl Window {
corner_radii: quad.corner_radii.scale(self.scale_factor()),
border_widths: snapped_border_widths,
border_style: quad.border_style,
- });
+ };
+
+ if !quad.background.is_transparent() {
+ self.next_frame.scene.insert_primitive(quad);
+ return;
+ }
+
+ // We're drawing a quad with a border but no fill color. Painting this quad would run the quad shader for every
+ // transparent interior pixel, which is especially costly when the quad is large.
+ // Instead, split it into four non-overlapping strips that cover the regions where borders are painted:
+ // the side strips own the straight left and right edges, while the top and bottom strips own the horizontal
+ // edges and the rounded corners.
+ let radii = &quad.corner_radii;
+ let widths = &quad.border_widths;
+
+ let antialias_slack = point(ScaledPixels(1.0), ScaledPixels(1.0));
+ let top_left_inset = point(
+ widths.left,
+ widths.top.max(radii.top_left).max(radii.top_right),
+ ) + antialias_slack;
+ let bottom_right_inset = point(
+ widths.right,
+ widths.bottom.max(radii.bottom_left).max(radii.bottom_right),
+ ) + antialias_slack;
+
+ let outer_bounds = quad.bounds;
+ let inner_bounds = Bounds::from_corners(
+ outer_bounds.origin + top_left_inset,
+ outer_bounds.bottom_right() - bottom_right_inset,
+ );
+
+ if inner_bounds.is_empty() {
+ self.next_frame.scene.insert_primitive(quad);
+ return;
+ }
+
+ let strips = [
+ // Top
+ Bounds::from_corners(
+ outer_bounds.origin,
+ point(outer_bounds.right(), inner_bounds.top()),
+ ),
+ // Bottom
+ Bounds::from_corners(
+ point(outer_bounds.left(), inner_bounds.bottom()),
+ outer_bounds.bottom_right(),
+ ),
+ // Left
+ Bounds::from_corners(
+ point(outer_bounds.left(), inner_bounds.top()),
+ inner_bounds.bottom_left(),
+ ),
+ // Right
+ Bounds::from_corners(
+ inner_bounds.top_right(),
+ point(outer_bounds.right(), inner_bounds.bottom()),
+ ),
+ ];
+
+ for strip in strips {
+ let content_mask_bounds = quad.content_mask.bounds.intersect(&strip);
+ if !content_mask_bounds.is_empty() {
+ self.next_frame.scene.insert_primitive(Quad {
+ content_mask: ContentMask {
+ bounds: content_mask_bounds,
+ },
+ ..quad
+ });
+ }
+ }
}
/// Paint the given `Path` into the scene for the next frame at the current z-index.
diff --git a/crates/gpui_windows/src/directx_renderer.rs b/crates/gpui_windows/src/directx_renderer.rs
index 03666241e67..8a350682e23 100644
--- a/crates/gpui_windows/src/directx_renderer.rs
+++ b/crates/gpui_windows/src/directx_renderer.rs
@@ -16,7 +16,7 @@ use windows::{
Dxgi::{Common::*, *},
},
},
- core::Interface,
+ core::{HSTRING, Interface},
};
use crate::directx_renderer::shader_resources::{RawShaderBytes, ShaderModule, ShaderTarget};
@@ -63,6 +63,7 @@ pub(crate) struct DirectXRendererDevices {
pub(crate) device: ID3D11Device,
pub(crate) device_context: ID3D11DeviceContext,
dxgi_device: Option<IDXGIDevice>,
+ annotation: Option<ID3DUserDefinedAnnotation>,
}
struct DirectXResources {
@@ -97,6 +98,21 @@ struct DirectXGlobalElements {
sampler: Option<ID3D11SamplerState>,
}
+struct Annotation<'a>(&'a ID3DUserDefinedAnnotation);
+
+impl<'a> Annotation<'a> {
+ fn new(annotation: &'a ID3DUserDefinedAnnotation, label: HSTRING) -> Self {
+ unsafe { annotation.BeginEvent(&label) };
+ Self(annotation)
+ }
+}
+
+impl Drop for Annotation<'_> {
+ fn drop(&mut self) {
+ unsafe { self.0.EndEvent() };
+ }
+}
+
struct DirectComposition {
comp_device: IDCompositionDevice,
comp_target: IDCompositionTarget,
@@ -119,6 +135,7 @@ impl DirectXRendererDevices {
} else {
Some(device.cast().context("Creating DXGI device")?)
};
+ let annotation = device_context.cast().ok();
Ok(Self {
adapter: adapter.clone(),
@@ -126,6 +143,7 @@ impl DirectXRendererDevices {
device: device.clone(),
device_context: device_context.clone(),
dxgi_device,
+ annotation,
})
}
}
@@ -318,7 +336,15 @@ impl DirectXRenderer {
self.upload_scene_buffers(scene)?;
+ let annotation = self
+ .devices
+ .as_ref()
+ .and_then(|devices| devices.annotation.clone())
+ .filter(|annotation| unsafe { annotation.GetStatus().as_bool() });
for batch in scene.batches() {
+ let _annotation = annotation
+ .as_ref()
+ .map(|annotation| Annotation::new(annotation, HSTRING::from(batch.label())));
match batch {
PrimitiveBatch::Shadows(range) => self.draw_shadows(range.start, range.len()),
PrimitiveBatch::Quads(range) => self.draw_quads(range.start, range.len()),
@@ -339,18 +365,20 @@ impl DirectXRenderer {
}
PrimitiveBatch::Surfaces(range) => self.draw_surfaces(&scene.surfaces[range]),
}
- .context(format!(
- "scene too large:\
- {} paths, {} shadows, {} quads, {} underlines, {} mono, {} subpixel, {} poly, {} surfaces",
- scene.paths.len(),
- scene.shadows.len(),
- scene.quads.len(),
- scene.underlines.len(),
- scene.monochrome_sprites.len(),
- scene.subpixel_sprites.len(),
- scene.polychrome_sprites.len(),
- scene.surfaces.len(),
- ))?;
+ .with_context(|| {
+ format!(
+ "scene too large:\
+ {} paths, {} shadows, {} quads, {} underlines, {} mono, {} subpixel, {} poly, {} surfaces",
+ scene.paths.len(),
+ scene.shadows.len(),
+ scene.quads.len(),
+ scene.underlines.len(),
+ scene.monochrome_sprites.len(),
+ scene.subpixel_sprites.len(),
+ scene.polychrome_sprites.len(),
+ scene.surfaces.len(),
+ )
+ })?;
}
self.present()
}