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    // What a renderer of `renderer_epoch` on this target is built from.
38    pub(crate) fn renderer_init(
39        self,
40        renderer_epoch: u64,
41        pipeline_compilation: crate::pipeline_compiler::PipelineCompilation,
42        on_device_lost: Option<crate::DeviceLostHandler>,
43    ) -> crate::render::GpuRendererInit {
44        crate::render::GpuRendererInit {
45            device_errors: crate::device_error_sentry::DeviceErrorSentry::watching(
46                &self.device,
47                on_device_lost,
48            ),
49            device: self.device,
50            queue: self.queue,
51            surface_format: self.surface_format,
52            adapter_backend: self.adapter_backend,
53            adapter_downlevel: self.adapter_downlevel,
54            renderer_epoch,
55            pipeline_compilation,
56        }
57    }
58}
59
60/// The optional device features the renderer exploits when the adapter
61/// offers them: pipeline caching (see `pipeline_disk_cache`), the
62/// timestamp queries behind `CRANPOSE_GPU_PASS_TIMING`, and, on a GPU
63/// that shares the host's memory, vertex and uniform buffers the CPU
64/// writes directly, so a frame's uploads need no copy. Every platform's
65/// `request_device` passes this so a profiling toggle never needs a rebuilt
66/// binary; intersecting with the adapter's own features keeps the request
67/// valid on adapters without them.
68pub fn optional_device_features(adapter: &wgpu::Adapter) -> wgpu::Features {
69    let mut wanted = wgpu::Features::PIPELINE_CACHE | wgpu::Features::TIMESTAMP_QUERY;
70    if matches!(
71        adapter.get_info().device_type,
72        wgpu::DeviceType::IntegratedGpu | wgpu::DeviceType::Cpu
73    ) {
74        wanted |= wgpu::Features::MAPPABLE_PRIMARY_BUFFERS;
75    }
76    adapter.features() & wanted
77}