Skip to content

Commit 90eb399

Browse files
authored
Fill in Adapter::as_hal and Device::as_hal documentation. (#2992)
In particular, explain why a callback is needed, rather than just having these functions return an `Option<&A::Mumble>`.
1 parent e350f50 commit 90eb399

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ the same every time it is rendered, we now warn if it is missing.
9090
- Update Winit to version 0.27 and raw-window-handle to 0.5 by @wyatt-herkamp in [#2918](https://github.com/gfx-rs/wgpu/pull/2918)
9191
- Address Clippy 0.1.63 complaints. By @jimblandy in [#2977](https://github.com/gfx-rs/wgpu/pull/2977)
9292
- Don't use `PhantomData` for `IdentityManager`'s `Input` type. By @jimblandy in [#2972](https://github.com/gfx-rs/wgpu/pull/2972)
93+
9394
#### Metal
9495
- Extract the generic code into `get_metal_layer` by @jinleili in [#2826](https://github.com/gfx-rs/wgpu/pull/2826)
9596

@@ -110,6 +111,7 @@ the same every time it is rendered, we now warn if it is missing.
110111
`Instance::create_surface_from_offscreen_canvas` regarding their
111112
safety contract. These functions are not unsafe. By @jimblandy [#2990](https://github.com/gfx-rs/wgpu/pull/2990)
112113
- Document that `write_buffer_with()` is sound but unwise to read from by @kpreid in [#3006](https://github.com/gfx-rs/wgpu/pull/3006)
114+
- Explain why `Adapter::as_hal` and `Device::as_hal` have to take callback functions. By @jimblandy in [#2992](https://github.com/gfx-rs/wgpu/pull/2992)
113115

114116
### Build Configuration
115117

wgpu/src/lib.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,12 +1916,25 @@ impl Adapter {
19161916
})
19171917
}
19181918

1919-
/// Returns the inner hal Adapter using a callback. The hal adapter will be `None` if the
1920-
/// backend type argument does not match with this wgpu Adapter
1919+
/// Apply a callback to this `Adapter`'s underlying backend adapter.
1920+
///
1921+
/// If this `Adapter` is implemented by the backend API given by `A` (Vulkan,
1922+
/// Dx12, etc.), then apply `hal_adapter_callback` to `Some(&adapter)`, where
1923+
/// `adapter` is the underlying backend adapter type, [`A::Adapter`].
1924+
///
1925+
/// If this `Adapter` uses a different backend, apply `hal_adapter_callback`
1926+
/// to `None`.
1927+
///
1928+
/// The adapter is locked for reading while `hal_adapter_callback` runs. If
1929+
/// the callback attempts to perform any `wgpu` operations that require
1930+
/// write access to the adapter, deadlock will occur. The locks are
1931+
/// automatically released when the callback returns.
19211932
///
19221933
/// # Safety
19231934
///
1924-
/// - The raw handle obtained from the hal Adapter must not be manually destroyed
1935+
/// - The raw handle passed to the callback must not be manually destroyed.
1936+
///
1937+
/// [`A::Adapter`]: hal::Api::Adapter
19251938
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
19261939
pub unsafe fn as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Adapter>) -> R, R>(
19271940
&self,
@@ -2211,12 +2224,25 @@ impl Device {
22112224
Context::device_stop_capture(&*self.context, &self.id)
22122225
}
22132226

2214-
/// Returns the inner hal Device using a callback. The hal device will be `None` if the
2215-
/// backend type argument does not match with this wgpu Device
2227+
/// Apply a callback to this `Device`'s underlying backend device.
2228+
///
2229+
/// If this `Device` is implemented by the backend API given by `A` (Vulkan,
2230+
/// Dx12, etc.), then apply `hal_device_callback` to `Some(&device)`, where
2231+
/// `device` is the underlying backend device type, [`A::Device`].
2232+
///
2233+
/// If this `Device` uses a different backend, apply `hal_device_callback`
2234+
/// to `None`.
2235+
///
2236+
/// The device is locked for reading while `hal_device_callback` runs. If
2237+
/// the callback attempts to perform any `wgpu` operations that require
2238+
/// write access to the device (destroying a buffer, say), deadlock will
2239+
/// occur. The locks are automatically released when the callback returns.
22162240
///
22172241
/// # Safety
22182242
///
2219-
/// - The raw handle obtained from the hal Device must not be manually destroyed
2243+
/// - The raw handle passed to the callback must not be manually destroyed.
2244+
///
2245+
/// [`A::Device`]: hal::Api::Device
22202246
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
22212247
pub unsafe fn as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Device>) -> R, R>(
22222248
&self,

0 commit comments

Comments
 (0)