euv_core/render/gpu/
impl.rs1use super::*;
2
3impl Default for NativeGpuDriver {
5 fn default() -> Self {
11 Self::probe(GraphicsBackend::Headless)
12 }
13}
14
15impl NativeGpuDriver {
17 #[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 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
68impl GpuDriver for NativeGpuDriver {
70 fn backend(&self) -> GraphicsBackend {
80 self.get_adapter().get_backend()
81 }
82
83 fn shader_source(&self) -> &'static str {
93 self.get_fallback().shader_source()
94 }
95
96 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
157impl Drop for NativeGpuDriver {
159 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}