Skip to main content

ComputeContext

Struct ComputeContext 

Source
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: Device

The logical GPU device.

§queue: Queue

The primary command submission queue (compute and, when no separate transfer queue is available, also used for DMA transfers).

Implementations§

Source§

impl ComputeContext

Source

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);
}
Source

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;
    }
}
Source

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();
Source

pub fn new() -> Result<Self, ComputeError>

Create a context with high-performance power preference and default limits.

§Errors
use oxiui_compute_wgpu::{ComputeContext, ComputeError};

match ComputeContext::new() {
    Ok(ctx)                      => { let _ = ctx; }
    Err(ComputeError::NoAdapter) => { /* skip */ }
    Err(e)                       => panic!("unexpected: {e}"),
}
Source

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
}
Source

pub async fn new_async() -> Result<Self, ComputeError>

Async variant of new — awaits adapter and device requests directly without a pollster::block_on wrapper.

Suitable for use inside an async runtime (Tokio, async-std, etc.).

§Errors

Same as new.

use oxiui_compute_wgpu::ComputeContext;

let ctx = ComputeContext::new_async().await?;
Source

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);
Source

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]);
}
Source

pub fn with_limits(limits: Limits) -> ContextBuilder

Start building a context with custom memory limits.

Equivalent to ComputeContext::builder().with_limits(limits).

Source

pub fn with_features(features: Features) -> ContextBuilder

Start building a context with specific GPU features enabled.

Equivalent to ComputeContext::builder().with_features(features).

Source

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).

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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

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

Source§

impl<T> WasmNotSendSync for T

Source§

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