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
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ By @jimblandy in [#3254](https://github.com/gfx-rs/wgpu/pull/3254).
- Implemented `TextureFormat::Stencil8`, allowing for stencil testing without depth components. By @Dinnerbone in [#3343](https://github.com/gfx-rs/wgpu/pull/3343)
- Implemented `add_srgb_suffix()` for `TextureFormat` for converting linear formats to sRGB. By @Elabajaba in [#3419](https://github.com/gfx-rs/wgpu/pull/3419)
- Zero-initialize workgroup memory. By @teoxoy in [#3174](https://github.com/gfx-rs/wgpu/pull/3174)
- Added support for importing external buffers using `buffer_from_raw` (Dx12, Metal) and `create_buffer_from_hal`. By @AdrianEddy in [#3355](https://github.com/gfx-rs/wgpu/pull/3355)

#### GLES

Expand All @@ -268,6 +269,9 @@ By @jimblandy in [#3254](https://github.com/gfx-rs/wgpu/pull/3254).
- Add `MULTISAMPLE_X2`, `MULTISAMPLE_X4` and `MULTISAMPLE_X8` to `TextureFormatFeatureFlags`. By @39ali in [3140](https://github.com/gfx-rs/wgpu/pull/3140)
- Sync `TextureFormat.describe` with the spec. By @teoxoy in [3312](https://github.com/gfx-rs/wgpu/pull/3312)

#### Metal
- Add a way to create `Device` and `Queue` from raw Metal resources in wgpu-hal

### Bug Fixes

#### General
Expand Down
79 changes: 79 additions & 0 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,29 @@ impl<A: HalApi> Device<A> {
}
}

fn create_buffer_from_hal(
&self,
hal_buffer: A::Buffer,
self_id: id::DeviceId,
desc: &resource::BufferDescriptor,
) -> resource::Buffer<A> {
debug_assert_eq!(self_id.backend(), A::VARIANT);

resource::Buffer {
raw: Some(hal_buffer),
device_id: Stored {
value: id::Valid(self_id),
ref_count: self.life_guard.add_ref(),
},
usage: desc.usage,
size: desc.size,
initialization_status: BufferInitTracker::new(desc.size),
sync_mapped_writes: None,
map_state: resource::BufferMapState::Idle,
life_guard: LifeGuard::new(desc.label.borrow_or_default()),
}
}

fn create_texture(
&self,
self_id: id::DeviceId,
Expand Down Expand Up @@ -3946,6 +3969,62 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
(id, Some(error))
}

/// # Safety
///
/// - `hal_buffer` must be created from `device_id` corresponding raw handle.
/// - `hal_buffer` must be created respecting `desc`
/// - `hal_buffer` must be initialized
pub unsafe fn create_buffer_from_hal<A: HalApi>(
&self,
hal_buffer: A::Buffer,
device_id: id::DeviceId,
desc: &resource::BufferDescriptor,
id_in: Input<G, id::BufferId>,
) -> (id::BufferId, Option<resource::CreateBufferError>) {
profiling::scope!("Device::create_buffer");

let hub = A::hub(self);
let mut token = Token::root();
let fid = hub.buffers.prepare(id_in);

let (device_guard, mut token) = hub.devices.read(&mut token);
let error = loop {
let device = match device_guard.get(device_id) {
Ok(device) => device,
Err(_) => break DeviceError::Invalid.into(),
};

// NB: Any change done through the raw buffer handle will not be
// recorded in the replay
#[cfg(feature = "trace")]
if let Some(ref trace) = device.trace {
trace
.lock()
.add(trace::Action::CreateBuffer(fid.id(), desc.clone()));
}

let mut buffer = device.create_buffer_from_hal(hal_buffer, device_id, desc);

buffer.initialization_status = BufferInitTracker::new(desc.size);

let ref_count = buffer.life_guard.add_ref();

let id = fid.assign(buffer, &mut token);
log::info!("Created buffer {:?} with {:?}", id, desc);

device
.trackers
.lock()
.buffers
.insert_single(id, ref_count, hal::BufferUses::empty());

return (id.0, None);
};

let id = fid.assign_error(desc.label.borrow_or_default(), &mut token);
(id, Some(error))
}

pub fn texture_label<A: HalApi>(&self, id: id::TextureId) -> String {
A::hub(self).textures.label_for_resource(id)
}
Expand Down
11 changes: 11 additions & 0 deletions wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@ impl super::Device {
allocation: None,
}
}

pub unsafe fn buffer_from_raw(
resource: native::Resource,
size: wgt::BufferAddress,
) -> super::Buffer {
super::Buffer {
resource,
size,
allocation: None,
}
}
}

impl crate::Device<super::Api> for super::Device {
Expand Down
11 changes: 11 additions & 0 deletions wgpu-hal/src/metal/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ impl super::Device {
}
}

pub unsafe fn buffer_from_raw(raw: mtl::Buffer, size: wgt::BufferAddress) -> super::Buffer {
super::Buffer { raw, size }
}

pub unsafe fn device_from_raw(raw: mtl::Device, features: wgt::Features) -> super::Device {
cwfitzgerald marked this conversation as resolved.
Show resolved Hide resolved
super::Device {
shared: Arc::new(super::AdapterShared::new(raw)),
features,
}
}

pub fn raw_device(&self) -> &Mutex<mtl::Device> {
&self.shared.device
}
Expand Down
7 changes: 7 additions & 0 deletions wgpu-hal/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ pub struct Queue {
unsafe impl Send for Queue {}
unsafe impl Sync for Queue {}

impl Queue {
pub unsafe fn queue_from_raw(raw: mtl::CommandQueue) -> Self {
Self {
raw: Arc::new(Mutex::new(raw)),
}
}
}
pub struct Device {
shared: Arc<AdapterShared>,
features: wgt::Features,
Expand Down
35 changes: 34 additions & 1 deletion wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
context::{ObjectId, Unused},
AdapterInfo, BindGroupDescriptor, BindGroupLayoutDescriptor, BindingResource, BufferBinding,
CommandEncoderDescriptor, ComputePassDescriptor, ComputePipelineDescriptor,
BufferDescriptor, CommandEncoderDescriptor, ComputePassDescriptor, ComputePipelineDescriptor,
DownlevelCapabilities, Features, Label, Limits, LoadOp, MapMode, Operations,
PipelineLayoutDescriptor, RenderBundleEncoderDescriptor, RenderPipelineDescriptor,
SamplerDescriptor, ShaderModuleDescriptor, ShaderModuleDescriptorSpirV, ShaderSource,
Expand Down Expand Up @@ -154,6 +154,39 @@ impl Context {
}
}

#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
pub unsafe fn create_buffer_from_hal<A: wgc::hub::HalApi>(
&self,
hal_buffer: A::Buffer,
device: &Device,
desc: &BufferDescriptor,
) -> (wgc::id::BufferId, Buffer) {
let global = &self.0;
let (id, error) = unsafe {
global.create_buffer_from_hal::<A>(
hal_buffer,
device.id,
&desc.map_label(|l| l.map(Borrowed)),
(),
)
};
if let Some(cause) = error {
self.handle_error(
&device.error_sink,
cause,
LABEL,
desc.label,
"Device::create_buffer_from_hal",
);
}
(
id,
Buffer {
error_sink: Arc::clone(&device.error_sink),
},
)
}

#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
pub unsafe fn device_as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Device>) -> R, R>(
&self,
Expand Down
40 changes: 40 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2156,6 +2156,46 @@ impl Device {
}
}

/// Creates a [`Buffer`] from a wgpu-hal Buffer.
///
/// # Safety
///
/// - `hal_buffer` must be created from this device internal handle
/// - `hal_buffer` must be created respecting `desc`
/// - `hal_buffer` must be initialized
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
pub unsafe fn create_buffer_from_hal<A: wgc::hub::HalApi>(
&self,
hal_buffer: A::Buffer,
desc: &BufferDescriptor,
) -> Buffer {
let mut map_context = MapContext::new(desc.size);
if desc.mapped_at_creation {
map_context.initial_range = 0..desc.size;
}

let (id, buffer) = unsafe {
self.context
.as_any()
.downcast_ref::<crate::backend::Context>()
.unwrap()
.create_buffer_from_hal::<A>(
hal_buffer,
self.data.as_ref().downcast_ref().unwrap(),
desc,
)
};

Buffer {
context: Arc::clone(&self.context),
id: ObjectId::from(id),
data: Box::new(buffer),
map_context: Mutex::new(map_context),
size: desc.size,
usage: desc.usage,
}
}

/// Creates a new [`Sampler`].
///
/// `desc` specifies the behavior of the sampler.
Expand Down