Skip to main content

GpuContext

Enum GpuContext 

Source
#[non_exhaustive]
pub enum GpuContext { Cpu, OpenCl { context: *mut c_void, queue: *mut c_void, device: *mut c_void, gl_sharing: Option<OpenClGlSharing>, }, Cuda { context: *mut c_void, stream: *mut c_void, device_id: i32, uuid: Option<[u8; 16]>, }, Metal { device: *mut c_void, queue: *mut c_void, registry_id: Option<u64>, }, D3D11 { device: *mut c_void, device_context: *mut c_void, luid: Option<(i32, u32)>, }, D3D12 { device: *mut c_void, queue: *mut c_void, luid: Option<(i32, u32)>, }, Vulkan { instance: *mut c_void, physical_device: *mut c_void, device: *mut c_void, queue: u64, queue_family_index: u32, uuid: Option<[u8; 16]>, }, OpenGL { display: *mut c_void, context: *mut c_void, share_group: Option<u64>, backend: GlBackend, }, Wgpu { device: Device, queue: Queue, adapter: Adapter, }, }

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Cpu

§

OpenCl

Fields

§context: *mut c_void
§gl_sharing: Option<OpenClGlSharing>

Caller-asserted GL-sharing properties for context.

When set, the caller asserts that context was created with CL_GL_CONTEXT_KHR + the matching platform property (CL_WGL_HDC_KHR on Windows desktop, CL_GLX_DISPLAY_KHR on Linux X11, CL_EGL_DISPLAY_KHR on EGL/ANGLE, CL_CGL_SHAREGROUP_KHR on macOS CGL) pointing at the GL display + context recorded here. Required for the cl_khr_gl_sharing route into a wgpu-GL device — without it, a consumer has to fall back to cl_khr_external_memory cross-context sharing (more setup per import) or a CPU copy.

None is correct when the caller’s CL context is not GL-shared (compute-only work that never feeds wgpu-GL). A consumer that feeds wgpu-GL may refuse a non-GL-shared CL context.

§

Cuda

External CUDA context + stream owned by the caller.

Lifetime contract: context and stream must remain valid for the lifetime of every resource/handle minted through an interop / decoder / encoder built on this context — including Drop-time teardown, which may run on arbitrary threads long after the mint. Keep-alives minted against this context capture the raw CUcontext and re-bind it to destroy per-context tokens (e.g. CUsurfObject); a context destroyed early cannot be detected — the driver may recycle the address, resolving those tokens against an unrelated context.

Fields

§context: *mut c_void
§device_id: i32
§uuid: Option<[u8; 16]>
§

Metal

Fields

§registry_id: Option<u64>
§

D3D11

Fields

§device_context: *mut c_void
§luid: Option<(i32, u32)>
§

D3D12

Fields

§luid: Option<(i32, u32)>
§

Vulkan

Fields

§instance: *mut c_void
§physical_device: *mut c_void
§queue: u64
§queue_family_index: u32
§uuid: Option<[u8; 16]>
§

OpenGL

External OpenGL / OpenGL ES context owned by the caller.

display / context are the caller’s platform-native identifiers — EGLDisplay+EGLContext on EGL, HDC+HGLRC on WGL, CGLContextObj on CGL (with display mirroring context).

share_group is an opaque identifier the caller uses to stamp share-group identity. Two OpenGL contexts with the same non- None share_group are assumed to share GL object names; None means “solo context, no sharing guarantees”. There is no runtime query for this on EGL or WGL, so the value is purely caller-supplied.

Fields

§display: *mut c_void
§context: *mut c_void
§share_group: Option<u64>
§backend: GlBackend
§

Wgpu

Available on crate feature wgpu only.

Caller-supplied wgpu device, queue, and the adapter the device was created from.

adapter is load-bearing for per-format capability probes: wgpu::Adapter::get_texture_format_features(...) is the only reliable way to ask whether a given format supports STORAGE_BINDING, RENDER_ATTACHMENT, etc. on the caller’s physical device. wgpu::Device::features() reports the device-creation-time requested feature set, which is a strict subset of what the adapter can actually do per format — inadequate for the probe (e.g. on a desktop Vulkan adapter R8 storage is available without any feature flag, so Device::features() can’t tell you).

External-context-mode factories run their per-format capability detection against the borrowed adapter; the alternative — enumerating fresh adapters from a new wgpu::Instance and guessing which one matches the device — is incorrect on multi-GPU systems, which is why this field exists.

The three wgpu handles are internally reference-counted, so the caller passes clones of its own and keeps using them.

Fields

§device: Device
§queue: Queue
§adapter: Adapter

Implementations§

Source§

impl GpuContext

Source

pub fn from_wgpu(device: Device, queue: Queue, adapter: Adapter) -> Self

Available on crate feature wgpu only.

A GpuContext::Wgpu over the caller’s device, queue and the adapter the device was created from. The wgpu handles are internally reference-counted; pass clones.

Source

pub fn backend(&self) -> BackendKind

Source

pub fn cuda_context_and_stream( &self, ) -> Result<(*mut c_void, *mut c_void), GpuContextError>

Returns (cuCtx, cuStream) for the Cuda variant; Err otherwise. Lets a CUDA consumer read the pair without pattern-matching the GpuContext enum at every call site.

Source

pub fn opencl_context_and_queue( &self, ) -> Result<(*mut c_void, *mut c_void), GpuContextError>

Returns (cl_context, cl_command_queue) for the OpenCl variant; Err otherwise.

Source

pub fn metal_command_queue(&self) -> Result<*mut c_void, GpuContextError>

Returns the MTLCommandQueue handle for the Metal variant; Err otherwise.

Source

pub fn metal_device(&self) -> Result<*mut c_void, GpuContextError>

Returns the MTLDevice handle for the Metal variant; Err otherwise.

Source

pub fn cuda_uuid(&self) -> Result<[u8; 16], GpuContextError>

CUDA device UUID for the Cuda variant — populated when the caller filled the uuid field. Used to stamp GpuResource::CudaPtr2D for cross-API import identity matching.

Source

pub fn cuda_device_id(&self) -> Result<i32, GpuContextError>

CUDA device id (ordinal) for the Cuda variant.

Source

pub fn metal_registry_id(&self) -> Result<u64, GpuContextError>

Metal IORegistry id for the Metal variant — populated when the caller filled the registry_id field.

Source

pub fn opencl_device(&self) -> Result<*mut c_void, GpuContextError>

OpenCL cl_device_id for the OpenCl variant.

Source

pub fn opencl_gl_sharing( &self, ) -> Result<Option<OpenClGlSharing>, GpuContextError>

GL-sharing handles for the OpenCl variant, if the caller set them at construction. Ok(None) means the variant matches but no sharing was declared; Err(LaneMismatch) means the caller asked for OpenCL on a non-OpenCL context.

Source

pub fn device_id(&self) -> Option<DeviceId>

Stable identity of the underlying device, when the context carries enough information to construct one. Wgpu returns None here — wgpu-side device lookup belongs to the interop layer.

Trait Implementations§

Source§

impl Debug for GpuContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Send for GpuContext

Available on non-target_family=wasm only.
Source§

impl Sync for GpuContext

Available on non-target_family=wasm only.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T> MaybeSendSync for T
where T: Send + Sync + ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,