#[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
Cpu
OpenCl
Fields
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.
Metal
D3D11
D3D12
Vulkan
Fields
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.
Wgpu
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.
Implementations§
Source§impl GpuContext
impl GpuContext
Sourcepub fn from_wgpu(device: Device, queue: Queue, adapter: Adapter) -> Self
Available on crate feature wgpu only.
pub fn from_wgpu(device: Device, queue: Queue, adapter: Adapter) -> Self
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.
pub fn backend(&self) -> BackendKind
Sourcepub fn cuda_context_and_stream(
&self,
) -> Result<(*mut c_void, *mut c_void), GpuContextError>
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.
Sourcepub fn opencl_context_and_queue(
&self,
) -> Result<(*mut c_void, *mut c_void), GpuContextError>
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.
Sourcepub fn metal_command_queue(&self) -> Result<*mut c_void, GpuContextError>
pub fn metal_command_queue(&self) -> Result<*mut c_void, GpuContextError>
Returns the MTLCommandQueue handle for the Metal variant;
Err otherwise.
Sourcepub fn metal_device(&self) -> Result<*mut c_void, GpuContextError>
pub fn metal_device(&self) -> Result<*mut c_void, GpuContextError>
Returns the MTLDevice handle for the Metal variant; Err
otherwise.
Sourcepub fn cuda_uuid(&self) -> Result<[u8; 16], GpuContextError>
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.
Sourcepub fn cuda_device_id(&self) -> Result<i32, GpuContextError>
pub fn cuda_device_id(&self) -> Result<i32, GpuContextError>
CUDA device id (ordinal) for the Cuda variant.
Sourcepub fn metal_registry_id(&self) -> Result<u64, GpuContextError>
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.
Sourcepub fn opencl_device(&self) -> Result<*mut c_void, GpuContextError>
pub fn opencl_device(&self) -> Result<*mut c_void, GpuContextError>
OpenCL cl_device_id for the OpenCl variant.
Sourcepub fn opencl_gl_sharing(
&self,
) -> Result<Option<OpenClGlSharing>, GpuContextError>
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.
Trait Implementations§
Source§impl Debug for GpuContext
impl Debug for GpuContext
impl Send for GpuContext
target_family=wasm only.impl Sync for GpuContext
target_family=wasm only.