Skip to main content

euv_engine/config/
impl.rs

1use super::*;
2
3/// Implements `Default` for `RenderConfig` with sensible default values.
4impl Default for RenderConfig {
5    fn default() -> RenderConfig {
6        RenderConfig {
7            backend: RenderBackendType::default(),
8            canvas_selector: CONFIG_DEFAULT_CANVAS_SELECTOR.to_string(),
9            width: CONFIG_DEFAULT_CANVAS_WIDTH,
10            height: CONFIG_DEFAULT_CANVAS_HEIGHT,
11            quality: RenderQuality::default(),
12            antialias: CONFIG_DEFAULT_ANTIALIAS,
13            power_preference: GpuPowerPreference::default(),
14            ssaa_scale_factor: CONFIG_DEFAULT_SSAA_SCALE_FACTOR,
15        }
16    }
17}
18
19/// Implements `Default` for `EngineConfig` with default render and scheduler configs.
20impl Default for EngineConfig {
21    fn default() -> EngineConfig {
22        EngineConfig {
23            render: RenderConfig::default(),
24            scheduler: SchedulerConfig::default(),
25        }
26    }
27}
28
29/// Implements construction helpers for `RenderConfig`.
30impl RenderConfig {
31    /// Creates a rendering configuration for the Canvas 2D backend.
32    ///
33    /// # Arguments
34    ///
35    /// - `S: AsRef<str>` - The CSS selector for the canvas element.
36    /// - `f64` - The viewport width in CSS pixels.
37    /// - `f64` - The viewport height in CSS pixels.
38    ///
39    /// # Returns
40    ///
41    /// - `RenderConfig` - The Canvas 2D rendering configuration.
42    pub fn canvas2d<S>(canvas_selector: S, width: f64, height: f64) -> RenderConfig
43    where
44        S: AsRef<str>,
45    {
46        RenderConfig {
47            backend: RenderBackendType::Canvas2D,
48            canvas_selector: canvas_selector.as_ref().to_string(),
49            width,
50            height,
51            ..RenderConfig::default()
52        }
53    }
54
55    /// Creates a rendering configuration for the WebGPU backend.
56    ///
57    /// # Arguments
58    ///
59    /// - `S: AsRef<str>` - The CSS selector for the canvas element.
60    /// - `f64` - The viewport width in CSS pixels.
61    /// - `f64` - The viewport height in CSS pixels.
62    ///
63    /// # Returns
64    ///
65    /// - `RenderConfig` - The WebGPU rendering configuration.
66    pub fn webgpu<S>(canvas_selector: S, width: f64, height: f64) -> RenderConfig
67    where
68        S: AsRef<str>,
69    {
70        RenderConfig {
71            backend: RenderBackendType::WebGpu,
72            canvas_selector: canvas_selector.as_ref().to_string(),
73            width,
74            height,
75            ..RenderConfig::default()
76        }
77    }
78
79    /// Creates a rendering configuration for the WebGL 2 backend.
80    ///
81    /// # Arguments
82    ///
83    /// - `S: AsRef<str>` - The CSS selector for the canvas element.
84    /// - `f64` - The viewport width in CSS pixels.
85    /// - `f64` - The viewport height in CSS pixels.
86    ///
87    /// # Returns
88    ///
89    /// - `RenderConfig` - The WebGL 2 rendering configuration.
90    pub fn webgl<S>(canvas_selector: S, width: f64, height: f64) -> RenderConfig
91    where
92        S: AsRef<str>,
93    {
94        RenderConfig {
95            backend: RenderBackendType::WebGl,
96            canvas_selector: canvas_selector.as_ref().to_string(),
97            width,
98            height,
99            ..RenderConfig::default()
100        }
101    }
102}
103
104/// Implements construction helpers for `EngineConfig`.
105impl EngineConfig {
106    /// Creates an engine configuration with the given render config and default scheduler.
107    ///
108    /// # Arguments
109    ///
110    /// - `RenderConfig` - The rendering configuration.
111    ///
112    /// # Returns
113    ///
114    /// - `EngineConfig` - The engine configuration.
115    pub fn create(render: RenderConfig) -> EngineConfig {
116        EngineConfig {
117            render,
118            scheduler: SchedulerConfig::default(),
119        }
120    }
121
122    /// Sets the scheduler configuration and returns the updated config.
123    ///
124    /// # Arguments
125    ///
126    /// - `SchedulerConfig` - The scheduler configuration.
127    ///
128    /// # Returns
129    ///
130    /// - `EngineConfig` - The updated engine configuration.
131    pub fn with_scheduler(mut self, scheduler: SchedulerConfig) -> EngineConfig {
132        self.set_scheduler(scheduler);
133        self
134    }
135}
136
137/// Implements WebGPU power preference conversion for `GpuPowerPreference`.
138impl GpuPowerPreference {
139    /// Converts this power preference to the WebGPU string value.
140    ///
141    /// Used to set the `powerPreference` field on `GpuRequestAdapterOptions`
142    /// via `Reflect::set`, avoiding a direct dependency on the
143    /// `web_sys::GpuPowerPreference` type.
144    ///
145    /// # Returns
146    ///
147    /// - `&'static str` - The WebGPU power preference string (e.g., `"low-power"`).
148    pub fn to_web_sys_string(&self) -> &'static str {
149        match self {
150            GpuPowerPreference::LowPower => GPU_POWER_PREFERENCE_LOW_POWER,
151            GpuPowerPreference::HighPerformance => GPU_POWER_PREFERENCE_HIGH_PERFORMANCE,
152        }
153    }
154}