Skip to content

Commit

Permalink
Use objc2-metal with metal naming scheme
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
madsmtm committed May 23, 2024
1 parent c7655e6 commit a700d1e
Show file tree
Hide file tree
Showing 12 changed files with 1,224 additions and 1,040 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ By @stefnotch in [#5410](https://github.com/gfx-rs/wgpu/pull/5410)

- Fix enablement of subgroup ops extension on Vulkan devices that don't support Vulkan 1.3. By @cwfitzgerald in [#5624](https://github.com/gfx-rs/wgpu/pull/5624).

#### Metal
- 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).

#### GLES / OpenGL

- Fix regression on OpenGL (EGL) where non-sRGB still used sRGB [#5642](https://github.com/gfx-rs/wgpu/pull/5642)
Expand Down
101 changes: 68 additions & 33 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ termcolor = "1.4.1"
#web-sys = { path = "../wasm-bindgen/crates/web-sys" }
#js-sys = { path = "../wasm-bindgen/crates/js-sys" }
#wasm-bindgen = { path = "../wasm-bindgen" }
block2 = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
objc2 = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
objc2-encode = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
objc-sys = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
objc2-foundation = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
objc2-quartz-core = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }
objc2-metal = { git = "https://github.com/madsmtm/objc2", branch = "metal-wgpu" }

[profile.release]
lto = "thin"
Expand Down
58 changes: 48 additions & 10 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ rust-version = "1.74"
[package.metadata.docs.rs]
# Ideally we would enable all the features.
#
# However, the metal features fail to be documented because the docs.rs runner cross-compiling under
# x86_64-unknown-linux-gnu and metal-rs cannot compile in that environment at the moment. The same applies
# for the dx12 feature.
features = ["vulkan", "gles", "renderdoc"]
# However, the dx12 features fail to be documented because the docs.rs runner cross-compiling under
# x86_64-unknown-linux-gnu cannot compile in that environment at the moment.
features = ["metal", "vulkan", "gles", "renderdoc"]
rustdoc-args = ["--cfg", "docsrs"]
targets = [
"x86_64-unknown-linux-gnu",
Expand All @@ -37,7 +36,7 @@ ignored = ["cfg_aliases"]
[lib]

[features]
metal = ["naga/msl-out", "dep:block"]
metal = ["naga/msl-out"]
vulkan = [
"naga/spv-out",
"dep:ash",
Expand Down Expand Up @@ -156,11 +155,50 @@ d3d12 = { path = "../d3d12/", version = "0.20.0", optional = true, features = [

[target.'cfg(any(target_os="macos", target_os="ios"))'.dependencies]
# backend: Metal
block = { version = "0.1", optional = true }

metal = { version = "0.28.0" }
objc = "0.2.5"
core-graphics-types = "0.1"
block2 = "0.5.1"
objc2 = "0.5.2"
objc2-foundation = { version = "0.2.2", features = [
"NSError",
"NSGeometry",
"NSProcessInfo",
"NSRange",
"NSString",
] }
objc2-metal = { version = "0.2.2", features = [
"block2",
"MTLBlitCommandEncoder",
"MTLBlitPass",
"MTLBuffer",
"MTLCaptureManager",
"MTLCaptureScope",
"MTLCommandBuffer",
"MTLCommandEncoder",
"MTLCommandQueue",
"MTLComputeCommandEncoder",
"MTLComputePass",
"MTLComputePipeline",
"MTLCounters",
"MTLDepthStencil",
"MTLDevice",
"MTLDrawable",
"MTLLibrary",
"MTLPipeline",
"MTLPixelFormat",
"MTLRenderCommandEncoder",
"MTLRenderPass",
"MTLRenderPipeline",
"MTLResource",
"MTLSampler",
"MTLStageInputOutputDescriptor",
"MTLTexture",
"MTLTypes",
"MTLVertexDescriptor",
] }
objc2-quartz-core = { version = "0.2.2", features = [
"CALayer",
"CAMetalLayer",
"objc2-metal",
] }

[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = "0.2.87"
Expand Down
7 changes: 4 additions & 3 deletions wgpu-hal/src/gles/egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1261,10 +1261,11 @@ impl crate::Surface for Surface {
let window_ptr = handle.ns_view.as_ptr();
#[cfg(target_os = "macos")]
let window_ptr = {
use objc::{msg_send, runtime::Object, sel, sel_impl};
use objc2::msg_send;
use objc2::runtime::AnyObject;
// ns_view always have a layer and don't need to verify that it exists.
let layer: *mut Object =
msg_send![handle.ns_view.as_ptr() as *mut Object, layer];
let layer: *mut AnyObject =
msg_send![handle.ns_view.as_ptr() as *mut AnyObject, layer];
layer as *mut ffi::c_void
};
window_ptr
Expand Down
Loading

0 comments on commit a700d1e

Please sign in to comment.