-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to a single GPU context in Blade (#20853)
Closes #17005 Release Notes: - Improved GPU context management: share a single context with multiple surfaces. ### High Level Blade got a proper support for Surface objects in kvark/blade#203. That was mainly motivated by Zed needing to draw multiple windows. With the Surface API, Zed is now able to have the GPU context tied to the "Platform" instead of "Window". Practically speaking, this means: - architecture more sound - faster to open/close windows - less surprises, more robust ### Concerns 1. Zed has been using a temporary workaround for the platform bug on some Intel+Nvidia machines that makes us unable to present - kvark/blade#144 . This workaround is no longer available with the new architecture. I'm looking for ideas on how to approach this better. - we are now picking up the change in kvark/blade#210, which allows forcing a specific Device ID. This should allow Zed users to work around the issue. We could help them to automate it, too. 2. ~~Metal-rs dependency is switched to https://github.com/kvark/metal-rs/tree/blade, since upstream isn't responsive in merging changes that are required for Blade. Hopefully, temporary.~~ - ~~we can also hack around it by just transmuting the texture references, since we know those are unchanged in the branch. That would allow Blade to use it's own version of Metal, temporarily, if switching metal-rs in the workspace is a concern.~~ - merged my metal-rs changes and updated Zed to use the upstream github reference --------- Co-authored-by: Mikayla Maki <mikayla@zed.dev> Co-authored-by: Mikayla Maki <mikayla.c.maki@gmail.com> Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
- Loading branch information
1 parent
56d20fc
commit 298b9df
Showing
16 changed files
with
381 additions
and
342 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
#[cfg(target_os = "macos")] | ||
mod apple_compat; | ||
mod blade_atlas; | ||
mod blade_context; | ||
mod blade_renderer; | ||
|
||
#[cfg(target_os = "macos")] | ||
pub(crate) use apple_compat::*; | ||
pub(crate) use blade_atlas::*; | ||
pub(crate) use blade_context::*; | ||
pub(crate) use blade_renderer::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use super::{BladeContext, BladeRenderer, BladeSurfaceConfig}; | ||
use blade_graphics as gpu; | ||
use std::{ffi::c_void, ptr::NonNull}; | ||
|
||
#[derive(Clone)] | ||
pub struct Context { | ||
inner: BladeContext, | ||
} | ||
impl Default for Context { | ||
fn default() -> Self { | ||
Self { | ||
inner: BladeContext::new().unwrap(), | ||
} | ||
} | ||
} | ||
|
||
pub type Renderer = BladeRenderer; | ||
|
||
pub unsafe fn new_renderer( | ||
context: Context, | ||
_native_window: *mut c_void, | ||
native_view: *mut c_void, | ||
bounds: crate::Size<f32>, | ||
transparent: bool, | ||
) -> Renderer { | ||
use raw_window_handle as rwh; | ||
struct RawWindow { | ||
view: *mut c_void, | ||
} | ||
|
||
impl rwh::HasWindowHandle for RawWindow { | ||
fn window_handle(&self) -> Result<rwh::WindowHandle, rwh::HandleError> { | ||
let view = NonNull::new(self.view).unwrap(); | ||
let handle = rwh::AppKitWindowHandle::new(view); | ||
Ok(unsafe { rwh::WindowHandle::borrow_raw(handle.into()) }) | ||
} | ||
} | ||
impl rwh::HasDisplayHandle for RawWindow { | ||
fn display_handle(&self) -> Result<rwh::DisplayHandle, rwh::HandleError> { | ||
let handle = rwh::AppKitDisplayHandle::new(); | ||
Ok(unsafe { rwh::DisplayHandle::borrow_raw(handle.into()) }) | ||
} | ||
} | ||
|
||
BladeRenderer::new( | ||
&context.inner, | ||
&RawWindow { | ||
view: native_view as *mut _, | ||
}, | ||
BladeSurfaceConfig { | ||
size: gpu::Extent { | ||
width: bounds.width as u32, | ||
height: bounds.height as u32, | ||
depth: 1, | ||
}, | ||
transparent, | ||
}, | ||
) | ||
.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use blade_graphics as gpu; | ||
use std::sync::Arc; | ||
|
||
#[cfg_attr(target_os = "macos", derive(Clone))] | ||
pub struct BladeContext { | ||
pub(super) gpu: Arc<gpu::Context>, | ||
} | ||
|
||
impl BladeContext { | ||
pub fn new() -> anyhow::Result<Self> { | ||
let gpu = Arc::new( | ||
unsafe { | ||
gpu::Context::init(gpu::ContextDesc { | ||
presentation: true, | ||
validation: false, | ||
device_id: 0, //TODO: hook up to user settings | ||
..Default::default() | ||
}) | ||
} | ||
.map_err(|e| anyhow::anyhow!("{:?}", e))?, | ||
); | ||
Ok(Self { gpu }) | ||
} | ||
} |
Oops, something went wrong.