Skip to main content

webizen_render/wgpu_renderer/
probe.rs

1//! Bounded WebGPU adapter/device capability probing.
2
3use super::{
4    bounded_capability_text, PixelFormat, WebGpuAdapterReport, WebGpuCapabilityReport,
5    WebGpuFeatureReport, WebGpuLimitReport, WebGpuStatus, MAX_OFFSCREEN_DIMENSION,
6    MAX_PIXEL_READBACK_BYTES,
7};
8
9pub(crate) fn capability_report_from_adapter(
10    adapter: &etalon::Adapter,
11    device: Option<&etalon::Device>,
12) -> WebGpuCapabilityReport {
13    let info = adapter.get_info();
14    let features = device
15        .map(etalon::Device::features)
16        .unwrap_or_else(|| adapter.features());
17    let limits = device
18        .map(etalon::Device::limits)
19        .unwrap_or_else(|| adapter.limits());
20    let status = if device.is_some() {
21        WebGpuStatus::Ready
22    } else {
23        WebGpuStatus::AdapterOnly
24    };
25
26    WebGpuCapabilityReport {
27        status,
28        adapter_available: true,
29        device_available: device.is_some(),
30        adapter: Some(WebGpuAdapterReport {
31            name: bounded_capability_text(info.name),
32            backend: backend_label(info.backend).to_string(),
33            device_type: device_type_label(info.device_type).to_string(),
34            vendor: info.vendor,
35            device: info.device,
36            driver: bounded_capability_text(info.driver),
37            driver_info: bounded_capability_text(info.driver_info),
38            features: WebGpuFeatureReport {
39                shader_f16: features.contains(etalon::Features::SHADER_F16),
40                subgroup: features.contains(etalon::Features::SUBGROUP),
41                timestamp_query: features.contains(etalon::Features::TIMESTAMP_QUERY),
42                texture_compression_bc: features
43                    .contains(etalon::Features::TEXTURE_COMPRESSION_BC),
44            },
45            limits: WebGpuLimitReport {
46                max_texture_dimension_2d: limits.max_texture_dimension_2d,
47                max_buffer_size: limits.max_buffer_size,
48            },
49        }),
50        offscreen_format: PixelFormat::Rgba8Unorm,
51        max_offscreen_dimension: MAX_OFFSCREEN_DIMENSION,
52        max_pixel_readback_bytes: MAX_PIXEL_READBACK_BYTES,
53    }
54}
55
56fn backend_label(backend: etalon::Backend) -> &'static str {
57    match backend {
58        etalon::Backend::Noop => "noop",
59        etalon::Backend::Vulkan => "vulkan",
60        etalon::Backend::Metal => "metal",
61        etalon::Backend::Dx12 => "dx12",
62        etalon::Backend::Gl => "gl",
63        etalon::Backend::BrowserWebGpu => "browser-webgpu",
64    }
65}
66
67fn device_type_label(device_type: etalon::DeviceType) -> &'static str {
68    match device_type {
69        etalon::DeviceType::Other => "other",
70        etalon::DeviceType::IntegratedGpu => "integrated",
71        etalon::DeviceType::DiscreteGpu => "discrete",
72        etalon::DeviceType::VirtualGpu => "virtual",
73        etalon::DeviceType::Cpu => "cpu",
74    }
75}
76
77/// Probe the adapter/device path without constructing a renderer target.
78///
79/// This is safe for callers that can fall back when WebGPU is absent. The
80/// report is deliberately bounded and contains no wgpu handles.
81pub async fn probe_webgpu_capabilities() -> WebGpuCapabilityReport {
82    #[cfg(feature = "qualia")]
83    if let Some(shared) = qualia_core_db::gpu_context::try_shared_gpu() {
84        return capability_report_from_adapter(&shared.adapter, Some(&shared.device));
85    }
86
87    let instance = etalon::Instance::default();
88    let adapter = match instance
89        .request_adapter(&etalon::RequestAdapterOptions {
90            power_preference: etalon::PowerPreference::LowPower,
91            compatible_surface: None,
92            force_fallback_adapter: false,
93            apply_limit_buckets: false,
94        })
95        .await
96    {
97        Ok(adapter) => adapter,
98        Err(_) => return WebGpuCapabilityReport::unavailable(),
99    };
100
101    match adapter
102        .request_device(&etalon::DeviceDescriptor {
103            label: Some("webizen-render-capability-probe"),
104            required_features: etalon::Features::empty(),
105            required_limits: etalon::Limits::downlevel_defaults(),
106            ..Default::default()
107        })
108        .await
109    {
110        Ok((device, _queue)) => capability_report_from_adapter(&adapter, Some(&device)),
111        Err(_) => WebGpuCapabilityReport {
112            status: WebGpuStatus::DeviceUnavailable,
113            ..capability_report_from_adapter(&adapter, None)
114        },
115    }
116}