Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to a single GPU context in Blade #20853

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 8 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,9 @@ async-watch = "0.3.1"
async_zip = { version = "0.0.17", features = ["deflate", "deflate64"] }
base64 = "0.22"
bitflags = "2.6.0"
blade-graphics = { git = "https://github.com/kvark/blade", rev = "e142a3a5e678eb6a13e642ad8401b1f3aa38e969" }
blade-macros = { git = "https://github.com/kvark/blade", rev = "e142a3a5e678eb6a13e642ad8401b1f3aa38e969" }
blade-util = { git = "https://github.com/kvark/blade", rev = "e142a3a5e678eb6a13e642ad8401b1f3aa38e969" }
blade-graphics = { git = "https://github.com/kvark/blade", rev = "099555282605c7c4cca9e66a8f40148298347f80" }
blade-macros = { git = "https://github.com/kvark/blade", rev = "099555282605c7c4cca9e66a8f40148298347f80" }
blade-util = { git = "https://github.com/kvark/blade", rev = "099555282605c7c4cca9e66a8f40148298347f80" }
blake3 = "1.5.3"
bytes = "1.0"
cargo_metadata = "0.19"
Expand Down Expand Up @@ -525,6 +525,12 @@ wasmtime-wasi = "24"
which = "6.0.0"
wit-component = "0.201"
zstd = "0.11"
# Custom metal-rs is only needed for "macos-blade" feature of GPUI
#TODO: switch to crates once these are published:
# - https://github.com/gfx-rs/metal-rs/pull/335
# - https://github.com/gfx-rs/metal-rs/pull/336
# - https://github.com/gfx-rs/metal-rs/pull/337
metal = { git = "https://github.com/gfx-rs/metal-rs", rev = "ef768ff9d742ae6a0f4e83ddc8031264e7d460c4" }

[workspace.dependencies.async-stripe]
git = "https://github.com/zed-industries/async-stripe"
Expand Down
2 changes: 1 addition & 1 deletion crates/gpui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ font-kit = { git = "https://github.com/zed-industries/font-kit", rev = "40391b7"
foreign-types = "0.5"
log.workspace = true
media.workspace = true
metal = "0.29"
objc = "0.2"
metal.workspace = true

[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))'.dependencies]
pathfinder_geometry = "0.5"
Expand Down
6 changes: 6 additions & 0 deletions crates/gpui/src/platform/blade.rs
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::*;
60 changes: 60 additions & 0 deletions crates/gpui/src/platform/blade/apple_compat.rs
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()
}
2 changes: 1 addition & 1 deletion crates/gpui/src/platform/blade/blade_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl BladeAtlasState {
fn flush(&mut self, encoder: &mut gpu::CommandEncoder) {
self.flush_initializations(encoder);

let mut transfers = encoder.transfer();
let mut transfers = encoder.transfer("atlas");
for upload in self.uploads.drain(..) {
let texture = &self.storage[upload.id];
transfers.copy_buffer_to_texture(
Expand Down
24 changes: 24 additions & 0 deletions crates/gpui/src/platform/blade/blade_context.rs
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 })
}
}
Loading
Loading