threecrate_gpu/
device.rs

1//! GPU device management
2
3use threecrate_core::Result;
4use wgpu::util::DeviceExt;
5
6/// GPU context for managing compute and rendering operations
7pub struct GpuContext {
8    pub instance: wgpu::Instance,
9    pub device: wgpu::Device,
10    pub queue: wgpu::Queue,
11    pub adapter: wgpu::Adapter,
12}
13
14impl GpuContext {
15    /// Create a new GPU context
16    pub async fn new() -> Result<Self> {
17        let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
18            backends: wgpu::Backends::all(),
19            flags: wgpu::InstanceFlags::default(),
20            ..Default::default()
21        });
22
23        let adapter = instance
24            .request_adapter(&wgpu::RequestAdapterOptions {
25                power_preference: wgpu::PowerPreference::HighPerformance,
26                compatible_surface: None,
27                force_fallback_adapter: false,
28            })
29            .await
30            .ok_or_else(|| threecrate_core::Error::Gpu("Failed to find suitable adapter".to_string()))?;
31
32        let (device, queue) = adapter
33            .request_device(
34                &wgpu::DeviceDescriptor {
35                    label: Some("ThreeCrate GPU Device"),
36                    required_features: wgpu::Features::empty(),
37                    required_limits: wgpu::Limits::default(),
38                    ..Default::default()
39                },
40                None,
41            )
42            .await
43            .map_err(|e| threecrate_core::Error::Gpu(format!("Failed to create device: {}", e)))?;
44
45        Ok(Self {
46            instance,
47            device,
48            queue,
49            adapter,
50        })
51    }
52
53    /// Create a buffer from data
54    pub fn create_buffer_init<T: bytemuck::Pod>(&self, label: &str, data: &[T], usage: wgpu::BufferUsages) -> wgpu::Buffer {
55        self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
56            label: Some(label),
57            contents: bytemuck::cast_slice(data),
58            usage,
59        })
60    }
61
62    /// Create an empty buffer
63    pub fn create_buffer(&self, label: &str, size: u64, usage: wgpu::BufferUsages) -> wgpu::Buffer {
64        self.device.create_buffer(&wgpu::BufferDescriptor {
65            label: Some(label),
66            size,
67            usage,
68            mapped_at_creation: false,
69        })
70    }
71
72    /// Create a compute pipeline
73    pub fn create_compute_pipeline(&self, label: &str, shader: &wgpu::ShaderModule, entry_point: &str) -> wgpu::ComputePipeline {
74        self.device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
75            label: Some(label),
76            layout: None,
77            module: shader,
78            entry_point,
79            compilation_options: wgpu::PipelineCompilationOptions::default(),
80        })
81    }
82
83    /// Create a shader module from WGSL source
84    pub fn create_shader_module(&self, label: &str, source: &str) -> wgpu::ShaderModule {
85        self.device.create_shader_module(wgpu::ShaderModuleDescriptor {
86            label: Some(label),
87            source: wgpu::ShaderSource::Wgsl(source.into()),
88        })
89    }
90
91    /// Create a bind group layout
92    pub fn create_bind_group_layout(&self, label: &str, entries: &[wgpu::BindGroupLayoutEntry]) -> wgpu::BindGroupLayout {
93        self.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
94            label: Some(label),
95            entries,
96        })
97    }
98
99    /// Create a bind group
100    pub fn create_bind_group(&self, label: &str, layout: &wgpu::BindGroupLayout, entries: &[wgpu::BindGroupEntry]) -> wgpu::BindGroup {
101        self.device.create_bind_group(&wgpu::BindGroupDescriptor {
102            label: Some(label),
103            layout,
104            entries,
105        })
106    }
107}