Skip to main content

cranpose_render_wgpu/
gpu_target.rs

1use std::sync::Arc;
2
3/// The device a renderer draws with, the surface format it presents and what
4/// the adapter behind the device supports.
5#[derive(Clone)]
6pub struct GpuTarget {
7    /// The device every GPU resource is created on.
8    pub device: Arc<wgpu::Device>,
9    /// The queue frames are submitted to.
10    pub queue: Arc<wgpu::Queue>,
11    /// The format of the surface frames are presented to.
12    pub surface_format: wgpu::TextureFormat,
13    /// The graphics API the adapter runs on.
14    pub adapter_backend: wgpu::Backend,
15    /// What the adapter supports below the full WebGPU feature set.
16    pub adapter_downlevel: wgpu::DownlevelFlags,
17}
18
19impl GpuTarget {
20    /// A target on a device of `adapter`, presenting to a surface of
21    /// `surface_format`.
22    pub fn on_adapter(
23        adapter: &wgpu::Adapter,
24        device: Arc<wgpu::Device>,
25        queue: Arc<wgpu::Queue>,
26        surface_format: wgpu::TextureFormat,
27    ) -> Self {
28        Self {
29            device,
30            queue,
31            surface_format,
32            adapter_backend: adapter.get_info().backend,
33            adapter_downlevel: adapter.get_downlevel_capabilities().flags,
34        }
35    }
36}
37
38/// The optional device features the renderer exploits when the adapter
39/// offers them: pipeline caching (see `pipeline_disk_cache`), the
40/// timestamp queries behind `CRANPOSE_GPU_PASS_TIMING`, and, on a GPU
41/// that shares the host's memory, vertex and uniform buffers the CPU
42/// writes directly, so a frame's uploads need no copy. Every platform's
43/// `request_device` passes this so a profiling toggle never needs a rebuilt
44/// binary; intersecting with the adapter's own features keeps the request
45/// valid on adapters without them.
46pub fn optional_device_features(adapter: &wgpu::Adapter) -> wgpu::Features {
47    let mut wanted = wgpu::Features::PIPELINE_CACHE | wgpu::Features::TIMESTAMP_QUERY;
48    if matches!(
49        adapter.get_info().device_type,
50        wgpu::DeviceType::IntegratedGpu | wgpu::DeviceType::Cpu
51    ) {
52        wanted |= wgpu::Features::MAPPABLE_PRIMARY_BUFFERS;
53    }
54    adapter.features() & wanted
55}