Skip to content

Commit e90f4a5

Browse files
committed
Use objc2-metal with metal naming scheme
To keep the diff smaller and easier to review, this uses a temporary fork of `objc2-metal` and `objc2-quartz-core` whose methods use the naming scheme of the `metal` crate. One particular difficult part with this is that the `metal` crate has several methods where the order of the arguments are swapped relative to the corresponding Objective-C methods. This includes most perilously (since these have both an offset and an index argument, both of which are integers): - `set_bytes` - `set_vertex_bytes` - `set_fragment_bytes` - `set_buffer` - `set_vertex_buffer` - `set_fragment_buffer` - `set_threadgroup_memory_length` But also: - `set_vertex_texture` - `set_fragment_texture` - `set_sampler_state` - `set_vertex_sampler_state` - `set_fragment_sampler_state` Another noteworthy thing to mention is that `objc2-metal` does not (yet) provide a fallback for MTLCopyAllDevices, so we have to do that ourselves: madsmtm/objc2@3543940
1 parent ff90830 commit e90f4a5

File tree

11 files changed

+1218
-1045
lines changed

11 files changed

+1218
-1045
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ By @brodycj in [#6924](https://github.com/gfx-rs/wgpu/pull/6924).
195195
- Fix building a BLAS with a transform buffer by adding a flag to indicate usage of the transform buffer. By @Vecvec in
196196
[#7062](https://github.com/gfx-rs/wgpu/pull/7062).
197197
198+
#### Metal
199+
- Use autogenerated `objc2` bindings internally, which should resolve a lot of leaks and unsoundness. By @madsmtm in [#5641](https://github.com/gfx-rs/wgpu/pull/5641).
200+
198201
#### Vulkan
199202
200203
- Stop naga causing undefined behavior when a ray query misses. By @Vecvec in [#6752](https://github.com/gfx-rs/wgpu/pull/6752).

Cargo.lock

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

Cargo.toml

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,59 @@ walkdir = "2"
157157
winit = { version = "0.29", features = ["android-native-activity"] }
158158

159159
# Metal dependencies
160-
metal = "0.31.0"
161-
block = "0.1"
162-
core-graphics-types = "0.1"
163-
objc = "0.2.5"
160+
block2 = { version = "0.6", git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
161+
objc2 = { version = "0.6", git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
162+
objc2-core-foundation = { version = "0.3", default-features = false, features = [
163+
"std",
164+
"CFCGTypes",
165+
], git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
166+
objc2-foundation = { version = "0.3", default-features = false, features = [
167+
"std",
168+
"NSError",
169+
"NSProcessInfo",
170+
"NSRange",
171+
"NSString",
172+
], git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
173+
objc2-metal = { version = "0.3", default-features = false, features = [
174+
"std",
175+
"block2",
176+
"MTLAllocation",
177+
"MTLBlitCommandEncoder",
178+
"MTLBlitPass",
179+
"MTLBuffer",
180+
"MTLCaptureManager",
181+
"MTLCaptureScope",
182+
"MTLCommandBuffer",
183+
"MTLCommandEncoder",
184+
"MTLCommandQueue",
185+
"MTLComputeCommandEncoder",
186+
"MTLComputePass",
187+
"MTLComputePipeline",
188+
"MTLCounters",
189+
"MTLDepthStencil",
190+
"MTLDevice",
191+
"MTLDrawable",
192+
"MTLEvent",
193+
"MTLLibrary",
194+
"MTLPipeline",
195+
"MTLPixelFormat",
196+
"MTLRenderCommandEncoder",
197+
"MTLRenderPass",
198+
"MTLRenderPipeline",
199+
"MTLResource",
200+
"MTLSampler",
201+
"MTLStageInputOutputDescriptor",
202+
"MTLTexture",
203+
"MTLTypes",
204+
"MTLVertexDescriptor",
205+
], git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
206+
objc2-quartz-core = { version = "0.3", default-features = false, features = [
207+
"std",
208+
"objc2-core-foundation",
209+
"CALayer",
210+
"CAMetalLayer",
211+
"objc2-metal",
212+
], git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
164213
raw-window-metal = "1.0"
165214

166215
# Vulkan dependencies

wgpu-hal/Cargo.toml

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ rust-version = "1.76"
1818
[package.metadata.docs.rs]
1919
# Ideally we would enable all the features.
2020
#
21-
# However, the metal features fail to be documented because the docs.rs runner cross-compiling under
22-
# x86_64-unknown-linux-gnu and metal-rs cannot compile in that environment at the moment. The same applies
23-
# for the dx12 feature.
24-
features = ["vulkan", "gles", "renderdoc"]
21+
# However, the dx12 features fail to be documented because the docs.rs runner cross-compiling under
22+
# x86_64-unknown-linux-gnu cannot compile in that environment at the moment.
23+
features = ["metal", "vulkan", "gles", "renderdoc"]
2524
rustdoc-args = ["--cfg", "docsrs"]
2625
targets = [
2726
"x86_64-unknown-linux-gnu",
@@ -74,13 +73,15 @@ metal = [
7473
# Metal is only available on Apple platforms, therefore request MSL output also only if we target an Apple platform.
7574
"naga/msl-out",
7675
"dep:arrayvec",
77-
"dep:block",
78-
"dep:core-graphics-types",
76+
"dep:block2",
7977
"dep:hashbrown",
8078
"dep:libc",
8179
"dep:log",
82-
"dep:metal",
83-
"dep:objc",
80+
"dep:objc2",
81+
"dep:objc2-core-foundation",
82+
"dep:objc2-foundation",
83+
"dep:objc2-metal",
84+
"dep:objc2-quartz-core",
8485
"dep:profiling",
8586
"dep:raw-window-metal",
8687
]
@@ -114,7 +115,7 @@ gles = [
114115
"dep:libloading",
115116
"dep:log",
116117
"dep:ndk-sys",
117-
"dep:objc",
118+
"dep:objc2",
118119
"dep:profiling",
119120
"dep:wasm-bindgen",
120121
"dep:web-sys",
@@ -272,10 +273,12 @@ mach-dxcompiler-rs = { workspace = true, optional = true }
272273

273274
[target.'cfg(target_vendor = "apple")'.dependencies]
274275
# Backend: Metal
275-
block = { workspace = true, optional = true }
276-
core-graphics-types = { workspace = true, optional = true }
277-
metal = { workspace = true, optional = true }
278-
objc = { workspace = true, optional = true }
276+
block2 = { workspace = true, optional = true }
277+
objc2 = { workspace = true, optional = true }
278+
objc2-core-foundation = { workspace = true, optional = true }
279+
objc2-foundation = { workspace = true, optional = true }
280+
objc2-metal = { workspace = true, optional = true }
281+
objc2-quartz-core = { workspace = true, optional = true }
279282

280283
# backend: Metal + Vulkan
281284
raw-window-metal = { workspace = true, optional = true }

wgpu-hal/src/gles/egl.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,10 +1334,11 @@ impl crate::Surface for Surface {
13341334
let window_ptr = handle.ns_view.as_ptr();
13351335
#[cfg(target_os = "macos")]
13361336
let window_ptr = {
1337-
use objc::{msg_send, runtime::Object, sel, sel_impl};
1337+
use objc2::msg_send;
1338+
use objc2::runtime::AnyObject;
13381339
// ns_view always have a layer and don't need to verify that it exists.
1339-
let layer: *mut Object =
1340-
msg_send![handle.ns_view.as_ptr().cast::<Object>(), layer];
1340+
let layer: *mut AnyObject =
1341+
msg_send![handle.ns_view.as_ptr().cast::<AnyObject>(), layer];
13411342
layer.cast::<ffi::c_void>()
13421343
};
13431344
window_ptr

0 commit comments

Comments
 (0)