Skip to content

Commit 3da76e1

Browse files
committed
Switch to a single GPU context in Blade
1 parent fa7dddd commit 3da76e1

File tree

16 files changed

+382
-342
lines changed

16 files changed

+382
-342
lines changed

Cargo.lock

Lines changed: 8 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ async-watch = "0.3.1"
349349
async_zip = { version = "0.0.17", features = ["deflate", "deflate64"] }
350350
base64 = "0.22"
351351
bitflags = "2.6.0"
352-
blade-graphics = { git = "https://github.com/kvark/blade", rev = "e142a3a5e678eb6a13e642ad8401b1f3aa38e969" }
353-
blade-macros = { git = "https://github.com/kvark/blade", rev = "e142a3a5e678eb6a13e642ad8401b1f3aa38e969" }
354-
blade-util = { git = "https://github.com/kvark/blade", rev = "e142a3a5e678eb6a13e642ad8401b1f3aa38e969" }
352+
blade-graphics = { git = "https://github.com/kvark/blade", rev = "099555282605c7c4cca9e66a8f40148298347f80" }
353+
blade-macros = { git = "https://github.com/kvark/blade", rev = "099555282605c7c4cca9e66a8f40148298347f80" }
354+
blade-util = { git = "https://github.com/kvark/blade", rev = "099555282605c7c4cca9e66a8f40148298347f80" }
355355
blake3 = "1.5.3"
356356
bytes = "1.0"
357357
cargo_metadata = "0.19"
@@ -520,6 +520,12 @@ wasmtime-wasi = "24"
520520
which = "6.0.0"
521521
wit-component = "0.201"
522522
zstd = "0.11"
523+
# Custom metal-rs is only needed for "macos-blade" feature of GPUI
524+
#TODO: switch to crates once these are published:
525+
# - https://github.com/gfx-rs/metal-rs/pull/335
526+
# - https://github.com/gfx-rs/metal-rs/pull/336
527+
# - https://github.com/gfx-rs/metal-rs/pull/337
528+
metal = { git = "https://github.com/gfx-rs/metal-rs", rev = "ef768ff9d742ae6a0f4e83ddc8031264e7d460c4" }
523529

524530
[workspace.dependencies.async-stripe]
525531
git = "https://github.com/zed-industries/async-stripe"

crates/gpui/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ font-kit = { git = "https://github.com/zed-industries/font-kit", rev = "40391b7"
136136
foreign-types = "0.5"
137137
log.workspace = true
138138
media.workspace = true
139-
metal = "0.29"
140139
objc = "0.2"
140+
metal.workspace = true
141141

142142
[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))'.dependencies]
143143
pathfinder_geometry = "0.5"

crates/gpui/src/platform/blade.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
#[cfg(target_os = "macos")]
2+
mod apple_compat;
13
mod blade_atlas;
4+
mod blade_context;
25
mod blade_renderer;
36

7+
#[cfg(target_os = "macos")]
8+
pub(crate) use apple_compat::*;
49
pub(crate) use blade_atlas::*;
10+
pub(crate) use blade_context::*;
511
pub(crate) use blade_renderer::*;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use super::{BladeContext, BladeRenderer, BladeSurfaceConfig};
2+
use blade_graphics as gpu;
3+
use std::{ffi::c_void, ptr::NonNull};
4+
5+
#[derive(Clone)]
6+
pub struct Context {
7+
inner: BladeContext,
8+
}
9+
impl Default for Context {
10+
fn default() -> Self {
11+
Self {
12+
inner: BladeContext::new().unwrap(),
13+
}
14+
}
15+
}
16+
17+
pub type Renderer = BladeRenderer;
18+
19+
pub unsafe fn new_renderer(
20+
context: Context,
21+
_native_window: *mut c_void,
22+
native_view: *mut c_void,
23+
bounds: crate::Size<f32>,
24+
transparent: bool,
25+
) -> Renderer {
26+
use raw_window_handle as rwh;
27+
struct RawWindow {
28+
view: *mut c_void,
29+
}
30+
31+
impl rwh::HasWindowHandle for RawWindow {
32+
fn window_handle(&self) -> Result<rwh::WindowHandle, rwh::HandleError> {
33+
let view = NonNull::new(self.view).unwrap();
34+
let handle = rwh::AppKitWindowHandle::new(view);
35+
Ok(unsafe { rwh::WindowHandle::borrow_raw(handle.into()) })
36+
}
37+
}
38+
impl rwh::HasDisplayHandle for RawWindow {
39+
fn display_handle(&self) -> Result<rwh::DisplayHandle, rwh::HandleError> {
40+
let handle = rwh::AppKitDisplayHandle::new();
41+
Ok(unsafe { rwh::DisplayHandle::borrow_raw(handle.into()) })
42+
}
43+
}
44+
45+
BladeRenderer::new(
46+
&context.inner,
47+
&RawWindow {
48+
view: native_view as *mut _,
49+
},
50+
BladeSurfaceConfig {
51+
size: gpu::Extent {
52+
width: bounds.width as u32,
53+
height: bounds.height as u32,
54+
depth: 1,
55+
},
56+
transparent,
57+
},
58+
)
59+
.unwrap()
60+
}

crates/gpui/src/platform/blade/blade_atlas.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl BladeAtlasState {
268268
fn flush(&mut self, encoder: &mut gpu::CommandEncoder) {
269269
self.flush_initializations(encoder);
270270

271-
let mut transfers = encoder.transfer();
271+
let mut transfers = encoder.transfer("atlas");
272272
for upload in self.uploads.drain(..) {
273273
let texture = &self.storage[upload.id];
274274
transfers.copy_buffer_to_texture(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use blade_graphics as gpu;
2+
use std::sync::Arc;
3+
4+
#[cfg_attr(target_os = "macos", derive(Clone))]
5+
pub struct BladeContext {
6+
pub(super) gpu: Arc<gpu::Context>,
7+
}
8+
9+
impl BladeContext {
10+
pub fn new() -> anyhow::Result<Self> {
11+
let gpu = Arc::new(
12+
unsafe {
13+
gpu::Context::init(gpu::ContextDesc {
14+
presentation: true,
15+
validation: false,
16+
device_id: 0, //TODO: hook up to user settings
17+
..Default::default()
18+
})
19+
}
20+
.map_err(|e| anyhow::anyhow!("{:?}", e))?,
21+
);
22+
Ok(Self { gpu })
23+
}
24+
}

0 commit comments

Comments
 (0)