Skip to content

Commit 6918851

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`
1 parent d8e7ab1 commit 6918851

File tree

12 files changed

+1240
-1085
lines changed

12 files changed

+1240
-1085
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ By @brodycj in [#6925](https://github.com/gfx-rs/wgpu/pull/6925).
7979
- Fix `wgpu` not building with `--no-default-features` on when targeting `wasm32-unknown-unknown`. By @wumpf in [#6946](https://github.com/gfx-rs/wgpu/pull/6946).
8080
- Fix `CopyExternalImageDestInfo` not exported on `wgpu`. By @wumpf in [#6962](https://github.com/gfx-rs/wgpu/pull/6962).
8181

82+
#### Metal
83+
- 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).
84+
8285
#### Vulkan
8386

8487
- 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: 74 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,59 @@ thiserror = "2"
139139
winit = { version = "0.29", features = ["android-native-activity"] }
140140

141141
# Metal dependencies
142-
metal = "0.31.0"
143-
block = "0.1"
144-
core-graphics-types = "0.1"
145-
objc = "0.2.5"
142+
block2 = "0.6"
143+
objc2 = "0.6"
144+
objc2-core-foundation = { version = "0.3", default-features = false, features = [
145+
"std",
146+
"CFCGTypes",
147+
] }
148+
objc2-foundation = { version = "0.3", default-features = false, features = [
149+
"std",
150+
"NSError",
151+
"NSProcessInfo",
152+
"NSRange",
153+
"NSString",
154+
] }
155+
objc2-metal = { version = "0.3", default-features = false, features = [
156+
"std",
157+
"block2",
158+
"MTLAllocation",
159+
"MTLBlitCommandEncoder",
160+
"MTLBlitPass",
161+
"MTLBuffer",
162+
"MTLCaptureManager",
163+
"MTLCaptureScope",
164+
"MTLCommandBuffer",
165+
"MTLCommandEncoder",
166+
"MTLCommandQueue",
167+
"MTLComputeCommandEncoder",
168+
"MTLComputePass",
169+
"MTLComputePipeline",
170+
"MTLCounters",
171+
"MTLDepthStencil",
172+
"MTLDevice",
173+
"MTLDrawable",
174+
"MTLEvent",
175+
"MTLLibrary",
176+
"MTLPipeline",
177+
"MTLPixelFormat",
178+
"MTLRenderCommandEncoder",
179+
"MTLRenderPass",
180+
"MTLRenderPipeline",
181+
"MTLResource",
182+
"MTLSampler",
183+
"MTLStageInputOutputDescriptor",
184+
"MTLTexture",
185+
"MTLTypes",
186+
"MTLVertexDescriptor",
187+
] }
188+
objc2-quartz-core = { version = "0.3", default-features = false, features = [
189+
"std",
190+
"objc2-core-foundation",
191+
"CALayer",
192+
"CAMetalLayer",
193+
"objc2-metal",
194+
] }
146195

147196
# Vulkan dependencies
148197
android_system_properties = "0.1.1"
@@ -190,9 +239,16 @@ termcolor = "1.4.1"
190239
# android dependencies
191240
ndk-sys = "0.5.0"
192241

193-
# These overrides allow our examples to explicitly depend on release crates
242+
# These overrides allow our examples to explicitly depend on release crates
194243
[patch.crates-io]
195244
wgpu = { path = "./wgpu" }
245+
block2 = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
246+
objc2 = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
247+
objc2-encode = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
248+
objc2-foundation = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
249+
objc2-quartz-core = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
250+
objc2-metal = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
251+
objc2-core-foundation = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
196252

197253
[profile.release]
198254
lto = "thin"

wgpu-hal/Cargo.toml

Lines changed: 9 additions & 9 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",
@@ -46,7 +45,6 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(web_sys_unstable_apis)'] }
4645
metal = [
4746
# Metal is only available on Apple platforms, therefore request MSL output also only if we target an Apple platform.
4847
"naga/msl-out-if-target-apple",
49-
"dep:block",
5048
]
5149
vulkan = [
5250
"naga/spv-out",
@@ -197,10 +195,12 @@ mach-dxcompiler-rs = { workspace = true, optional = true }
197195

198196
[target.'cfg(target_vendor = "apple")'.dependencies]
199197
# Backend: Metal
200-
block = { workspace = true, optional = true }
201-
core-graphics-types.workspace = true
202-
metal.workspace = true
203-
objc.workspace = true
198+
block2.workspace = true
199+
objc2.workspace = true
200+
objc2-core-foundation.workspace = true
201+
objc2-foundation.workspace = true
202+
objc2-metal.workspace = true
203+
objc2-quartz-core.workspace = true
204204

205205
#########################
206206
### Platform: Android ###

wgpu-hal/src/gles/egl.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,10 +1326,11 @@ impl crate::Surface for Surface {
13261326
let window_ptr = handle.ns_view.as_ptr();
13271327
#[cfg(target_os = "macos")]
13281328
let window_ptr = {
1329-
use objc::{msg_send, runtime::Object, sel, sel_impl};
1329+
use objc2::msg_send;
1330+
use objc2::runtime::AnyObject;
13301331
// ns_view always have a layer and don't need to verify that it exists.
1331-
let layer: *mut Object =
1332-
msg_send![handle.ns_view.as_ptr().cast::<Object>(), layer];
1332+
let layer: *mut AnyObject =
1333+
msg_send![handle.ns_view.as_ptr().cast::<AnyObject>(), layer];
13331334
layer.cast::<ffi::c_void>()
13341335
};
13351336
window_ptr

0 commit comments

Comments
 (0)