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            staging_buffer_handle: 0,
42            staging_contents_ptr: 0,
43            staging_buffer_capacity: 0,
44            submitted_frames: 0,
45            uploaded_instances: 0,
46            uploaded_bytes: 0,
47            last_upload_bytes: 0,
48            last_submitted_batches: 0,
49            staging_reallocations: 0,
50            fallback,
51        }
52    }
53
54    /// Re-initializes the GPU driver for `backend`, releasing any previously retained OS handles.
55    ///
56    /// # Arguments
57    ///
58    /// - `&Self` - Mutable GPU driver.
59    /// - `GraphicsBackend` - Target graphics backend.
60    pub fn switch_backend(&mut self, backend: GraphicsBackend) {
61        if self.get_adapter().get_backend() == backend {
62            return;
63        }
64        *self = Self::probe(backend);
65    }
66}
67
68/// `GpuDriver` implementation uploading instanced quad buffers to the active physical OS GPU.
69impl GpuDriver for NativeGpuDriver {
70    /// Returns the resolved `GraphicsBackend`.
71    ///
72    /// # Arguments
73    ///
74    /// - `&Self` - Driver reference.
75    ///
76    /// # Returns
77    ///
78    /// - `GraphicsBackend` - Active backend tag.
79    fn backend(&self) -> GraphicsBackend {
80        self.get_adapter().get_backend()
81    }
82
83    /// Returns the native shader source compiled by this GPU backend.
84    ///
85    /// # Arguments
86    ///
87    /// - `&Self` - Driver reference.
88    ///
89    /// # Returns
90    ///
91    /// - `&'static str` - Native shader program source.
92    fn shader_source(&self) -> &'static str {
93        self.get_fallback().shader_source()
94    }
95
96    /// Uploads instanced batches to physical GPU memory and records draw telemetry.
97    ///
98    /// # Arguments
99    ///
100    /// - `&Self` - Mutable GPU driver.
101    /// - `u32` - Surface width.
102    /// - `u32` - Surface height.
103    /// - `Color` - Surface clear color.
104    /// - `&[GpuBatch]` - Ordered draw batches.
105    ///
106    /// # Returns
107    ///
108    /// - `Result<usize, PlatformError>` - Number of submitted GPU draw calls.
109    fn submit_batches(
110        &mut self,
111        width: u32,
112        height: u32,
113        clear_color: Color,
114        batches: &[GpuBatch],
115    ) -> Result<usize, PlatformError> {
116        let Self {
117            device_handle,
118            queue_handle,
119            staging_buffer_handle,
120            staging_contents_ptr,
121            staging_buffer_capacity,
122            submitted_frames,
123            uploaded_instances,
124            uploaded_bytes,
125            last_upload_bytes,
126            last_submitted_batches,
127            staging_reallocations,
128            fallback,
129            ..
130        } = self;
131        let prev_staging_handle: usize = *staging_buffer_handle;
132        let mut staging: (usize, usize, usize) = (
133            *staging_buffer_handle,
134            *staging_contents_ptr,
135            *staging_buffer_capacity,
136        );
137        let uploaded: usize =
138            submit_native_gpu_batches_staged(*device_handle, *queue_handle, &mut staging, batches);
139        (
140            *staging_buffer_handle,
141            *staging_contents_ptr,
142            *staging_buffer_capacity,
143        ) = staging;
144        if *staging_buffer_handle != 0 && *staging_buffer_handle != prev_staging_handle {
145            *staging_reallocations = staging_reallocations.wrapping_add(1);
146        }
147        let frame_bytes: usize = uploaded.saturating_mul(GPU_QUAD_INSTANCE_BYTES);
148        *submitted_frames = submitted_frames.wrapping_add(1);
149        *uploaded_instances = uploaded_instances.wrapping_add(uploaded as u64);
150        *uploaded_bytes = uploaded_bytes.wrapping_add(frame_bytes as u64);
151        *last_upload_bytes = frame_bytes;
152        *last_submitted_batches = batches.len();
153        fallback.submit_batches(width, height, clear_color, batches)
154    }
155}
156
157/// Automatic native GPU handle cleanup when `NativeGpuDriver` is dropped.
158impl Drop for NativeGpuDriver {
159    /// Releases retained OS GPU staging buffer, device, command queue, and shader library handles.
160    ///
161    /// # Arguments
162    ///
163    /// - `&Self` - Dropping driver.
164    fn drop(&mut self) {
165        let Self {
166            device_handle,
167            queue_handle,
168            library_handle,
169            staging_buffer_handle,
170            staging_contents_ptr,
171            staging_buffer_capacity,
172            ..
173        } = self;
174        release_native_gpu_staging(*staging_buffer_handle);
175        *staging_buffer_handle = 0;
176        *staging_contents_ptr = 0;
177        *staging_buffer_capacity = 0;
178        release_native_gpu_handles(*device_handle, *queue_handle, *library_handle);
179    }
180}