A Rust library providing utility functions and abstractions for working with WGPU, similar to MetalTools.
- Context creation and management
- Texture handling utilities
- GPU operation scheduling
- Error handling
Add this to your Cargo.toml
:
[dependencies]
wgpu-tools = "0.0.1"
Here are some examples of how to use wgpu-tools:
use wgpu_tools::Context;
async fn create_context() -> Result<Context, wgpu_tools::Error> {
Context::default().await
}
use wgpu_tools::Context;
async fn create_context_with_surface(window: &impl raw_window_handle::HasRawWindowHandle) -> Result<(Context, wgpu::Surface), wgpu_tools::Error> {
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::default());
let surface = unsafe { instance.create_surface(window) }?;
let context = Context::default_with_surface(instance, Some(&surface)).await?;
Ok((context, surface))
}
use wgpu_tools::Context;
fn schedule_gpu_operations(context: &Context) {
context.schedule(|encoder| {
// Perform GPU operations using the encoder
// For example:
let _render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[],
depth_stencil_attachment: None,
});
// Add more GPU commands as needed
});
}
use wgpu_tools::Context;
use image::io::Reader as ImageReader;
fn load_texture_from_file(context: &Context, path: &std::path::Path) -> Result<wgpu_tools::Texture, wgpu_tools::Error> {
let img = ImageReader::open(path)?.decode()?;
context.texture_from_image(&img, &wgpu::TextureFormat::Rgba8UnormSrgb, Some("Loaded Texture"))
}
The Context
struct provides a convenient wrapper around wgpu's instance, adapter, device, and queue. It offers methods for creating contexts, scheduling GPU operations, and creating textures.
The Texture
struct encapsulates wgpu texture, view, and sampler objects.
Custom error types are provided to handle various failure scenarios in wgpu operations.
WGPUTools is licensed under MIT license.