shdrlib 0.2.0

High-level shader compilation and rendering library built on wgpu and naga
Documentation
use wgpu;

/// Manages the wgpu device, queue, and adapter
/// This is your main GPU interface
pub struct GPUDevice {
    pub device: wgpu::Device,
    pub queue: wgpu::Queue,
    pub adapter: wgpu::Adapter,
    pub instance: wgpu::Instance,
}

impl Default for GPUDevice {
    fn default() -> Self {
        Self::new()
    }
}

impl GPUDevice {
    /// Create a new GPU device
    /// This handles all the wgpu boilerplate for you
    pub fn new() -> Self {
        pollster::block_on(Self::new_async())
    }

    async fn new_async() -> Self {
        // Create wgpu instance
        let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
            backends: wgpu::Backends::all(),
            ..Default::default()
        });

        // Request an adapter (physical GPU)
        let adapter = instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                compatible_surface: None,
                force_fallback_adapter: false,
            })
            .await
            .expect("Failed to find a GPU adapter");

        // Request a device and queue
        let (device, queue) = adapter
            .request_device(
                &wgpu::DeviceDescriptor {
                    label: Some("shdrlib GPU Device"),
                    required_features: wgpu::Features::empty(),
                    required_limits: wgpu::Limits::default(),
                    memory_hints: Default::default(),
                },
                None,
            )
            .await
            .expect("Failed to create GPU device");

        GPUDevice {
            device,
            queue,
            adapter,
            instance,
        }
    }

    /// Get info about the GPU
    pub fn info(&self) -> wgpu::AdapterInfo {
        self.adapter.get_info()
    }

    /// Get reference to the adapter
    pub fn adapter(&self) -> &wgpu::Adapter {
        &self.adapter
    }

    /// Get reference to the instance
    pub fn instance(&self) -> &wgpu::Instance {
        &self.instance
    }
}