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.

gpui_wgpu: Configure the window surface once at creation (#66002)

runsonmypce20d19d32f25
diff --git a/crates/gpui_wgpu/src/wgpu_context.rs b/crates/gpui_wgpu/src/wgpu_context.rs
index 16d451f0085..5563a761d33 100644
--- a/crates/gpui_wgpu/src/wgpu_context.rs
+++ b/crates/gpui_wgpu/src/wgpu_context.rs
@@ -1,4 +1,6 @@
#[cfg(not(target_family = "wasm"))]
+use crate::{WgpuRenderer, WgpuSurfaceConfig};
+#[cfg(not(target_family = "wasm"))]
use anyhow::Context as _;
#[cfg(not(target_family = "wasm"))]
use gpui_util::ResultExt;
@@ -131,24 +133,27 @@ impl WgpuContext {
pub fn new(
instance: wgpu::Instance,
surface: &wgpu::Surface<'_>,
+ config: &WgpuSurfaceConfig,
compositor_gpu: Option<CompositorGpuHint>,
) -> anyhow::Result<Self> {
- Self::new_with_options(instance, surface, compositor_gpu, false)
+ Self::new_with_options(instance, surface, config, compositor_gpu, false)
}
#[cfg(not(target_family = "wasm"))]
pub fn new_rejecting_software(
instance: wgpu::Instance,
surface: &wgpu::Surface<'_>,
+ config: &WgpuSurfaceConfig,
compositor_gpu: Option<CompositorGpuHint>,
) -> anyhow::Result<Self> {
- Self::new_with_options(instance, surface, compositor_gpu, true)
+ Self::new_with_options(instance, surface, config, compositor_gpu, true)
}
#[cfg(not(target_family = "wasm"))]
fn new_with_options(
instance: wgpu::Instance,
surface: &wgpu::Surface<'_>,
+ config: &WgpuSurfaceConfig,
compositor_gpu: Option<CompositorGpuHint>,
reject_software: bool,
) -> anyhow::Result<Self> {
@@ -161,6 +166,7 @@ impl WgpuContext {
&instance,
device_id_filter,
surface,
+ config,
compositor_gpu.as_ref(),
reject_software,
))?;
@@ -506,6 +512,7 @@ impl WgpuContext {
instance: &wgpu::Instance,
device_id_filter: Option<u32>,
surface: &wgpu::Surface<'_>,
+ config: &WgpuSurfaceConfig,
compositor_gpu: Option<&CompositorGpuHint>,
reject_software: bool,
) -> anyhow::Result<(
@@ -556,7 +563,7 @@ impl WgpuContext {
log::info!("Testing adapter: {} ({:?})...", info.name, info.backend);
- match Self::try_adapter_with_surface(&adapter, surface).await {
+ match Self::try_adapter_with_surface(&adapter, surface, config).await {
Ok((device, queue, dual_source_blending, color_atlas_texture_format)) => {
log::info!(
"Selected GPU (passed configuration test): {} ({:?})",
@@ -644,6 +651,7 @@ impl WgpuContext {
async fn try_adapter_with_surface(
adapter: &wgpu::Adapter,
surface: &wgpu::Surface<'_>,
+ config: &WgpuSurfaceConfig,
) -> anyhow::Result<(wgpu::Device, wgpu::Queue, bool, TextureFormat)> {
let caps = surface.get_capabilities(adapter);
if caps.formats.is_empty() {
@@ -655,21 +663,13 @@ impl WgpuContext {
let (device, queue, dual_source_blending, color_atlas_texture_format) =
Self::create_device(adapter).await?;
+ // Test with the configuration the renderer will use, so that it doesn't need to
+ // configure the surface a second time.
+ let (surface_config, _, _) =
+ WgpuRenderer::surface_configuration(surface, adapter, &device, config)?;
let error_scope = device.push_error_scope(wgpu::ErrorFilter::Validation);
- let test_config = wgpu::SurfaceConfiguration {
- usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
- format: caps.formats[0],
- color_space: wgpu::SurfaceColorSpace::Auto,
- width: 64,
- height: 64,
- present_mode: wgpu::PresentMode::Fifo,
- desired_maximum_frame_latency: 2,
- alpha_mode: caps.alpha_modes[0],
- view_formats: vec![],
- };
-
- surface.configure(&device, &test_config);
+ surface.configure(&device, &surface_config);
let error = error_scope.pop().await;
if let Some(e) = error {
diff --git a/crates/gpui_wgpu/src/wgpu_renderer.rs b/crates/gpui_wgpu/src/wgpu_renderer.rs
index a148a4ccfbe..46f9ffdb989 100644
--- a/crates/gpui_wgpu/src/wgpu_renderer.rs
+++ b/crates/gpui_wgpu/src/wgpu_renderer.rs
@@ -327,12 +327,18 @@ impl WgpuRenderer {
};
let mut ctx_ref = gpu_context.borrow_mut();
+ let surface_configured = ctx_ref.is_none();
let context = match ctx_ref.as_mut() {
Some(context) => {
context.check_compatible_with_surface(&surface)?;
context
}
- None => ctx_ref.insert(WgpuContext::new(instance, &surface, compositor_gpu)?),
+ None => ctx_ref.insert(WgpuContext::new(
+ instance,
+ &surface,
+ &config,
+ compositor_gpu,
+ )?),
};
let atlas = Arc::new(WgpuAtlas::from_context(context));
@@ -342,6 +348,7 @@ impl WgpuRenderer {
context,
surface,
config,
+ surface_configured,
compositor_gpu,
atlas,
)
@@ -368,18 +375,22 @@ impl WgpuRenderer {
config: WgpuSurfaceConfig,
) -> anyhow::Result<Self> {
let atlas = Arc::new(WgpuAtlas::from_context(context));
- Self::new_internal(None, context, surface, config, None, atlas)
+ Self::new_internal(None, context, surface, config, false, None, atlas)
}
- fn new_internal(
- gpu_context: Option<GpuContext>,
- context: &WgpuContext,
- surface: wgpu::Surface<'static>,
- config: WgpuSurfaceConfig,
- compositor_gpu: Option<CompositorGpuHint>,
- atlas: Arc<WgpuAtlas>,
- ) -> anyhow::Result<Self> {
- let surface_caps = surface.get_capabilities(&context.adapter);
+ /// Returns the surface configuration for `config`, along with the transparent and
+ /// opaque alpha modes that the surface supports.
+ pub(crate) fn surface_configuration(
+ surface: &wgpu::Surface<'_>,
+ adapter: &wgpu::Adapter,
+ device: &wgpu::Device,
+ config: &WgpuSurfaceConfig,
+ ) -> anyhow::Result<(
+ wgpu::SurfaceConfiguration,
+ wgpu::CompositeAlphaMode,
+ wgpu::CompositeAlphaMode,
+ )> {
+ let surface_caps = surface.get_capabilities(adapter);
let preferred_formats = [
wgpu::TextureFormat::Bgra8Unorm,
wgpu::TextureFormat::Rgba8Unorm,
@@ -399,7 +410,7 @@ impl WgpuRenderer {
.ok_or_else(|| {
anyhow::anyhow!(
"Surface reports no supported texture formats for adapter {:?}",
- context.adapter.get_info().name
+ adapter.get_info().name
)
})?;
@@ -413,7 +424,7 @@ impl WgpuRenderer {
.ok_or_else(|| {
anyhow::anyhow!(
"Surface reports no supported alpha modes for adapter {:?}",
- context.adapter.get_info().name
+ adapter.get_info().name
)
})
};
@@ -434,7 +445,6 @@ impl WgpuRenderer {
opaque_alpha_mode
};
- let device = Arc::clone(&context.device);
let max_texture_size = device.limits().max_texture_dimension_2d;
let requested_width = config.size.width.0 as u32;
@@ -464,10 +474,30 @@ impl WgpuRenderer {
alpha_mode,
view_formats: vec![],
};
- // Configure the surface immediately. The adapter selection process already validated
- // that this adapter can successfully configure this surface.
- surface.configure(&context.device, &surface_config);
+ Ok((surface_config, transparent_alpha_mode, opaque_alpha_mode))
+ }
+
+ fn new_internal(
+ gpu_context: Option<GpuContext>,
+ context: &WgpuContext,
+ surface: wgpu::Surface<'static>,
+ config: WgpuSurfaceConfig,
+ surface_configured: bool,
+ compositor_gpu: Option<CompositorGpuHint>,
+ atlas: Arc<WgpuAtlas>,
+ ) -> anyhow::Result<Self> {
+ let (surface_config, transparent_alpha_mode, opaque_alpha_mode) =
+ Self::surface_configuration(&surface, &context.adapter, &context.device, &config)?;
+ // Adapter selection configures the surface with this same configuration when it
+ // creates the context, so only configure it here when that did not happen.
+ if !surface_configured {
+ surface.configure(&context.device, &surface_config);
+ }
+
+ let surface_format = surface_config.format;
+ let alpha_mode = surface_config.alpha_mode;
+ let max_texture_size = context.device.limits().max_texture_dimension_2d;
let core = WgpuRendererCore::new(context, atlas.clone(), surface_format, alpha_mode);
Ok(Self {
@@ -2218,6 +2248,15 @@ impl WgpuRenderer {
.window_handle()
.map_err(|e| anyhow::anyhow!("Failed to get window handle: {e}"))?;
+ let config = WgpuSurfaceConfig {
+ size: gpui::Size {
+ width: gpui::DevicePixels(self.surface_config.width as i32),
+ height: gpui::DevicePixels(self.surface_config.height as i32),
+ },
+ transparent: self.surface_config.alpha_mode != wgpu::CompositeAlphaMode::Opaque,
+ preferred_present_mode: Some(self.surface_config.present_mode),
+ };
+
let surface = if needs_new_context {
log::warn!("GPU device lost, recreating context...");
@@ -2233,8 +2272,12 @@ impl WgpuRenderer {
let instance = WgpuContext::instance(Some(Box::new(window.clone())));
let surface = create_surface(&instance, window_handle.as_raw())?;
- let new_context =
- WgpuContext::new_rejecting_software(instance, &surface, self.compositor_gpu)?;
+ let new_context = WgpuContext::new_rejecting_software(
+ instance,
+ &surface,
+ &config,
+ self.compositor_gpu,
+ )?;
*gpu_context.borrow_mut() = Some(new_context);
surface
} else {
@@ -2243,14 +2286,6 @@ impl WgpuRenderer {
create_surface(instance, window_handle.as_raw())?
};
- let config = WgpuSurfaceConfig {
- size: gpui::Size {
- width: gpui::DevicePixels(self.surface_config.width as i32),
- height: gpui::DevicePixels(self.surface_config.height as i32),
- },
- transparent: self.surface_config.alpha_mode != wgpu::CompositeAlphaMode::Opaque,
- preferred_present_mode: Some(self.surface_config.present_mode),
- };
let gpu_context = Rc::clone(gpu_context);
let ctx_ref = gpu_context.borrow();
let context = ctx_ref.as_ref().expect("context should exist");
@@ -2264,6 +2299,7 @@ impl WgpuRenderer {
context,
surface,
config,
+ needs_new_context,
self.compositor_gpu,
self.atlas.clone(),
)?;