Skip to main content

Crate oxiui_compute_wgpu

Crate oxiui_compute_wgpu 

Source
Expand description

§oxiui-compute-wgpu

Pure-Rust wgpu GPU-compute abstraction for the COOLJAPAN ecosystem.

This crate consolidates the repeated Instance → Adapter → Device → Queue initialisation boilerplate that oxiaero-cfd, oxiaero-mcdc, and similar crates each duplicated for pure GPU compute workloads (sparse linear solvers, Lattice-Boltzmann, Monte-Carlo simulations, …).

§Quick start

use oxiui_compute_wgpu::{bytemuck, compute_pipeline, read_back, storage_buffer_init, wgpu, ComputeContext};

let Some(ctx) = ComputeContext::try_new() else {
    return; // no GPU — skip gracefully
};

let input: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0];
let buffer = storage_buffer_init(&ctx.device, "values", bytemuck::cast_slice(&input));

const SHADER: &str = r#"
    @group(0) @binding(0) var<storage, read_write> data: array<f32>;
    @compute @workgroup_size(64)
    fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
        if gid.x < arrayLength(&data) {
            data[gid.x] = data[gid.x] * 2.0;
        }
    }
"#;
let pipeline = compute_pipeline(&ctx.device, SHADER, "main");

let bind_group = ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
    label: Some("values-bind"),
    layout: &pipeline.get_bind_group_layout(0),
    entries: &[wgpu::BindGroupEntry {
        binding: 0,
        resource: buffer.as_entire_binding(),
    }],
});

let mut encoder = ctx.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
{
    let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
        label: None,
        timestamp_writes: None,
    });
    pass.set_pipeline(&pipeline);
    pass.set_bind_group(0, &bind_group, &[]);
    pass.dispatch_workgroups((input.len() as u32 + 63) / 64, 1, 1);
}
ctx.queue.submit(std::iter::once(encoder.finish()));

let output: Vec<f32> = read_back(&ctx.device, &ctx.queue, &buffer, input.len());
assert_eq!(output, vec![2.0, 4.0, 6.0, 8.0]);

§Module structure

ModuleContents
contextComputeContext — headless Device + Queue init, multi-queue, from_device
bufferStorage / uniform / staging buffer helpers + read_back
pipelinecompute_pipeline builder
errorComputeError type
wgslWGSL preprocessor, validation, and built-in compute kernels
dispatchDispatcher — high-level GPU compute helpers
integrationBridges to oxiui-render-soft, oxiui-render-wgpu, oxiui-text
hot_reloadWGSL hot-reload via notify (behind the hot-reload feature)

§Dependency re-exports

oxiui-compute-wgpu re-exports wgpu, bytemuck, and pollster so that consumers need only a single dependency declaration in their Cargo.toml.

§Feature flags

FeatureDescription
tracingAdds #[tracing::instrument] spans to key functions for span-based profiling.
hot-reloadAdds hot_reload::ShaderWatcher for live WGSL file watching via notify.

Re-exports§

pub use buffer::mapped_storage_init;
pub use buffer::read_back;
pub use buffer::read_back_async;
pub use buffer::read_back_range;
pub use buffer::staging_buffer;
pub use buffer::storage_buffer_init;
pub use buffer::uniform_buffer;
pub use buffer::BufferPool;
pub use buffer::SubAllocator;
pub use buffer::SubRegion;
pub use buffer::TypedBuffer;
pub use context::ComputeContext;
pub use context::ContextBuilder;
pub use dispatch::Dispatcher;
pub use error::ComputeError;
pub use pipeline::checked_compute_pipeline;
pub use pipeline::compute_pipeline;
pub use pipeline::dispatch_1d;
pub use pipeline::dispatch_2d;
pub use pipeline::dispatch_3d;
pub use pipeline::encode_indirect_dispatch;
pub use pipeline::supports_immediates;
pub use pipeline::validate_immediates;
pub use pipeline::DispatchBuilder;
pub use pipeline::DispatchResult;
pub use pipeline::PipelineCache;
pub use pipeline::MAX_WORKGROUPS;
pub use wgsl::preprocess;
pub use wgsl::validate;
pub use wgsl::WgslDiagnostic;
pub use wgsl::WgslError;
pub use wgsl::SHADER_BITONIC_SORT;
pub use wgsl::SHADER_HISTOGRAM;
pub use wgsl::SHADER_MAP_F32_TEMPLATE;
pub use wgsl::SHADER_MATMUL;
pub use wgsl::SHADER_PREFIX_SUM;
pub use wgsl::SHADER_REDUCTION_SUM;
pub use wgsl::SHADER_SPH_DENSITY;
pub use wgsl::SHADER_ZIP_MAP_F32_TEMPLATE;
pub use wgpu;
pub use bytemuck;
pub use pollster;

Modules§

buffer
Storage, uniform, and staging buffer helpers, typed buffer wrappers, buffer pool, sub-allocator, and async readback utilities.
context
Headless GPU compute context: InstanceAdapterDevice + Queue.
dispatch
High-level compute dispatch helpers built on ComputeContext.
error
Error types for GPU compute operations.
integration
Integration helpers that connect oxiui-compute-wgpu with the rest of the COOLJAPAN UI ecosystem.
pipeline
Compute pipeline builder helpers.
wgsl
WGSL shader utilities: preprocessor, validation, and built-in compute kernels.