Skip to main content

euv_core/render/gpu/
impl.rs

1use super::*;
2
3/// Default `NativeGpuDriver` targets headless recording fallback.
4impl Default for NativeGpuDriver {
5    /// Creates a headless `NativeGpuDriver`.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Headless GPU driver.
10    fn default() -> Self {
11        Self::probe(GraphicsBackend::Headless)
12    }
13}
14
15/// Construction helpers for `NativeGpuDriver`.
16impl NativeGpuDriver {
17    /// Probes the active OS GPU hardware, compiles native shaders, and returns a live `NativeGpuDriver`.
18    ///
19    /// # Arguments
20    ///
21    /// - `GraphicsBackend` - Requested graphics backend.
22    ///
23    /// # Returns
24    ///
25    /// - `Self` - Initialized native GPU driver with automatic headless fallback.
26    #[must_use]
27    pub fn probe(backend: GraphicsBackend) -> Self {
28        let (adapter, device_handle, queue_handle, library_handle): (
29            GpuAdapterInfo,
30            usize,
31            usize,
32            usize,
33        ) = probe_native_gpu(backend);
34        let mut fallback: RecordingGpuDriver = RecordingGpuDriver::default();
35        fallback.set_backend_kind(backend);
36        Self {
37            adapter,
38            device_handle,
39            queue_handle,
40            library_handle,
41            submitted_frames: 0,
42            uploaded_instances: 0,
43            fallback,
44        }
45    }
46
47    /// Re-initializes the GPU driver for `backend`, releasing any previously retained OS handles.
48    ///
49    /// # Arguments
50    ///
51    /// - `&Self` - Mutable GPU driver.
52    /// - `GraphicsBackend` - Target graphics backend.
53    pub fn switch_backend(&mut self, backend: GraphicsBackend) {
54        if self.get_adapter().get_backend() == backend {
55            return;
56        }
57        *self = Self::probe(backend);
58    }
59}
60
61/// `GpuDriver` implementation uploading instanced quad buffers to the active physical OS GPU.
62impl GpuDriver for NativeGpuDriver {
63    /// Returns the resolved `GraphicsBackend`.
64    ///
65    /// # Arguments
66    ///
67    /// - `&Self` - Driver reference.
68    ///
69    /// # Returns
70    ///
71    /// - `GraphicsBackend` - Active backend tag.
72    fn backend(&self) -> GraphicsBackend {
73        self.get_adapter().get_backend()
74    }
75
76    /// Returns the native shader source compiled by this GPU backend.
77    ///
78    /// # Arguments
79    ///
80    /// - `&Self` - Driver reference.
81    ///
82    /// # Returns
83    ///
84    /// - `&'static str` - Native shader program source.
85    fn shader_source(&self) -> &'static str {
86        self.get_fallback().shader_source()
87    }
88
89    /// Uploads instanced batches to physical GPU memory and records draw telemetry.
90    ///
91    /// # Arguments
92    ///
93    /// - `&Self` - Mutable GPU driver.
94    /// - `u32` - Surface width.
95    /// - `u32` - Surface height.
96    /// - `Color` - Surface clear color.
97    /// - `&[GpuBatch]` - Ordered draw batches.
98    ///
99    /// # Returns
100    ///
101    /// - `Result<usize, PlatformError>` - Number of submitted GPU draw calls.
102    fn submit_batches(
103        &mut self,
104        width: u32,
105        height: u32,
106        clear_color: Color,
107        batches: &[GpuBatch],
108    ) -> Result<usize, PlatformError> {
109        let uploaded: usize =
110            submit_native_gpu_batches(self.get_device_handle(), self.get_queue_handle(), batches);
111        let Self {
112            submitted_frames,
113            uploaded_instances,
114            fallback,
115            ..
116        } = self;
117        *submitted_frames = submitted_frames.wrapping_add(1);
118        *uploaded_instances = uploaded_instances.wrapping_add(uploaded as u64);
119        fallback.submit_batches(width, height, clear_color, batches)
120    }
121}
122
123/// Automatic native GPU handle cleanup when `NativeGpuDriver` is dropped.
124impl Drop for NativeGpuDriver {
125    /// Releases retained OS GPU device, command queue, and shader library handles.
126    ///
127    /// # Arguments
128    ///
129    /// - `&Self` - Dropping driver.
130    fn drop(&mut self) {
131        release_native_gpu_handles(
132            self.get_device_handle(),
133            self.get_queue_handle(),
134            self.get_library_handle(),
135        );
136    }
137}