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

[Metal] Add a way to create a device and queue from raw resources in wgpu-hal #3338

Merged
merged 13 commits into from
Feb 1, 2023

Conversation

AdrianEddy
Copy link
Contributor

@AdrianEddy AdrianEddy commented Dec 28, 2022

Checklist

  • Run cargo clippy.
  • Run RUSTFLAGS=--cfg=web_sys_unstable_apis cargo clippy --target wasm32-unknown-unknown if applicable.
  • Add change to CHANGELOG.md. See simple instructions inside file.

Connections
Related to #3145 and #2320

Description
This PR adds device_from_raw and queue_from_raw in addition to the existing texture_from_raw. This allows to get the MTLDevice and/or MTLCommandQueue from external application, and use them to process external memory/texture in wgpu.
My use case involves OpenFX plugin for DaVinci Resolve video editor, and Resolve provides MTLTexture and MTLCommandQueue pointers, which I then use in a plugin written in Rust and wgpu.

With these 2 new functions, one can create wgpu::Device and wgpu::Queue from a raw pointer to MTLCommandQueue the following way:

use foreign_types::ForeignType;
use wgpu_hal::api::Metal;

let mtl_command_queue: *mut metal::MTLCommandQueue = ...;

let (device, queue) = unsafe {
    let mtl_queue = metal::CommandQueue::from_ptr(mtl_command_queue);
    let mtl_device = mtl_queue.device();

    adapter.create_device_from_hal(wgpu_hal::OpenDevice::<Metal> {
        device: <Metal as wgpu_hal::Api>::Device::device_from_raw(mtl_device.to_owned(), wgpu::Features::empty()),
        queue: <Metal as wgpu_hal::Api>::Queue::queue_from_raw(mtl_queue)
    }, &wgpu::DeviceDescriptor {
        label: None,
        features: wgpu::Features::empty(),
        limits: wgpu::Limits::default()
    }, None).unwrap()
};

Testing
I tested these changes with pointers provided by the DaVinci Resolve, and also with pointers provided from Qt and QML renderer, both cases work as intended.

@codecov-commenter
Copy link

codecov-commenter commented Jan 4, 2023

Codecov Report

Merging #3338 (ac3bd87) into master (aa46e82) will increase coverage by 0.41%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #3338      +/-   ##
==========================================
+ Coverage   64.10%   64.51%   +0.41%     
==========================================
  Files          86       86              
  Lines       42591    42597       +6     
==========================================
+ Hits        27301    27480     +179     
+ Misses      15290    15117     -173     
Impacted Files Coverage Δ
wgpu-core/src/hub.rs 60.67% <0.00%> (-0.31%) ⬇️
wgpu-hal/src/dx12/mod.rs 27.27% <0.00%> (ø)
wgpu-hal/src/vulkan/mod.rs 56.37% <0.00%> (ø)
wgpu-core/src/device/mod.rs 66.73% <0.00%> (ø)
wgpu-hal/src/dx12/command.rs 73.46% <0.00%> (+0.02%) ⬆️
wgpu-hal/src/auxil/dxgi/conv.rs 76.02% <0.00%> (+0.33%) ⬆️
wgpu-hal/src/vulkan/adapter.rs 76.77% <0.00%> (+0.33%) ⬆️
wgpu/src/backend/direct.rs 56.87% <0.00%> (+0.38%) ⬆️
wgpu-hal/src/vulkan/device.rs 75.84% <0.00%> (+0.43%) ⬆️
wgpu/src/context.rs 53.32% <0.00%> (+0.58%) ⬆️
... and 11 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good enough for now - we need to go through and audit all of these external interface functions later anyway.

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, wrong button

@cwfitzgerald cwfitzgerald enabled auto-merge (squash) February 1, 2023 23:04
@AdrianEddy
Copy link
Contributor Author

hmm, something went wrong here, I think I messed up the branches, give me a moment

auto-merge was automatically disabled February 1, 2023 23:36

Head branch was pushed to by a user without write access

@AdrianEddy
Copy link
Contributor Author

Fixed, sorry about that. This change should only add device_from_raw and queue_from_raw to the metal backend. Please squash when merging

@cwfitzgerald cwfitzgerald enabled auto-merge (squash) February 1, 2023 23:43
@cwfitzgerald cwfitzgerald merged commit 2562f32 into gfx-rs:master Feb 1, 2023
@AdrianEddy AdrianEddy deleted the metal-device-from-raw branch February 6, 2023 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants