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