pub struct ComputeContext {
pub device: Device,
pub queue: Queue,
/* private fields */
}Expand description
An initialised headless GPU compute context.
Owns the logical wgpu::Device, the primary compute wgpu::Queue,
an optional dedicated transfer queue (when the adapter exposes more than one
queue family), and the wgpu::AdapterInfo snapshot captured at
construction time. No window handle, surface, or swap-chain is involved.
Fields§
§device: DeviceThe logical GPU device.
queue: QueueThe primary command submission queue (compute and, when no separate transfer queue is available, also used for DMA transfers).
Implementations§
Source§impl ComputeContext
impl ComputeContext
Sourcepub fn adapter_info(&self) -> &AdapterInfo
pub fn adapter_info(&self) -> &AdapterInfo
Return a reference to the adapter metadata captured at construction time.
The returned wgpu::AdapterInfo contains fields such as name,
vendor, device, backend, driver, and driver_info.
use oxiui_compute_wgpu::ComputeContext;
if let Some(ctx) = ComputeContext::try_new() {
let info = ctx.adapter_info();
println!("GPU backend: {:?}", info.backend);
}Sourcepub fn transfer_queue(&self) -> Option<&Queue>
pub fn transfer_queue(&self) -> Option<&Queue>
Return the dedicated transfer queue when one was obtained.
Returns Some only when ContextBuilder::with_multi_queue was
called and the underlying adapter exposed a separate transfer queue
family. Callers should fall back to self.queue when this is None.
use oxiui_compute_wgpu::ComputeContext;
if let Some(ctx) = ComputeContext::try_new() {
if let Some(tq) = ctx.transfer_queue() {
// Use the dedicated DMA queue for uploads.
let _ = tq;
}
}Sourcepub fn builder() -> ContextBuilder
pub fn builder() -> ContextBuilder
Return a ContextBuilder for fluent configuration of limits,
features, and power preference.
use oxiui_compute_wgpu::ComputeContext;
let ctx = ComputeContext::builder()
.with_power_preference(wgpu::PowerPreference::LowPower)
.build();Sourcepub fn new() -> Result<Self, ComputeError>
pub fn new() -> Result<Self, ComputeError>
Create a context with high-performance power preference and default limits.
§Errors
ComputeError::NoAdapter— no suitable GPU adapter was found.ComputeError::DeviceRequest— the device/queue request failed.
use oxiui_compute_wgpu::{ComputeContext, ComputeError};
match ComputeContext::new() {
Ok(ctx) => { let _ = ctx; }
Err(ComputeError::NoAdapter) => { /* skip */ }
Err(e) => panic!("unexpected: {e}"),
}Sourcepub fn try_new() -> Option<Self>
pub fn try_new() -> Option<Self>
Try to create a ComputeContext, returning None when no suitable GPU
adapter is available on this host.
This constructor never panics. Call sites that want a graceful skip on headless CI environments (VMs, containers without GPU pass-through) should use this variant:
use oxiui_compute_wgpu::ComputeContext;
if let Some(ctx) = ComputeContext::try_new() {
// GPU is available — run the compute workload
let _ = ctx;
} else {
// No GPU — skip gracefully
}Sourcepub async fn new_async() -> Result<Self, ComputeError>
pub async fn new_async() -> Result<Self, ComputeError>
Sourcepub fn from_device(
device: Device,
queue: Queue,
adapter_info: Option<AdapterInfo>,
) -> Self
pub fn from_device( device: Device, queue: Queue, adapter_info: Option<AdapterInfo>, ) -> Self
Wrap externally owned wgpu::Device and wgpu::Queue in a
ComputeContext so that the compute layer can share a device/queue pair
that was already created by another backend (e.g. oxiui-render-wgpu).
A synthetic wgpu::AdapterInfo is constructed from the optional
adapter_info argument; pass None to use a placeholder.
§Example
use oxiui_compute_wgpu::ComputeContext;
// Suppose `device` and `queue` come from an external renderer.
let (device, queue) = external();
let ctx = ComputeContext::from_device(device, queue, None);Sourcepub fn dispatcher(&self) -> Dispatcher<'_>
pub fn dispatcher(&self) -> Dispatcher<'_>
Create a crate::dispatch::Dispatcher that borrows this context.
The Dispatcher provides high-level, zero-boilerplate GPU compute
operations (map_f32, zip_map_f32, reduce_sum_f32, sph_density,
sort_f32, …).
use oxiui_compute_wgpu::ComputeContext;
if let Some(ctx) = ComputeContext::try_new() {
let d = ctx.dispatcher();
let out = d.map_f32(&[1.0, 2.0, 3.0], "x * 2.0");
assert_eq!(out, vec![2.0, 4.0, 6.0]);
}Sourcepub fn with_limits(limits: Limits) -> ContextBuilder
pub fn with_limits(limits: Limits) -> ContextBuilder
Start building a context with custom memory limits.
Equivalent to ComputeContext::builder().with_limits(limits).
Sourcepub fn with_features(features: Features) -> ContextBuilder
pub fn with_features(features: Features) -> ContextBuilder
Start building a context with specific GPU features enabled.
Equivalent to ComputeContext::builder().with_features(features).
Sourcepub fn with_power_preference(pref: PowerPreference) -> ContextBuilder
pub fn with_power_preference(pref: PowerPreference) -> ContextBuilder
Start building a context with a specific power preference.
Equivalent to ComputeContext::builder().with_power_preference(pref).