-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgpu.rs
More file actions
437 lines (391 loc) · 12.2 KB
/
gpu.rs
File metadata and controls
437 lines (391 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use pollster::block_on;
use super::{
command::CommandBuffer,
instance::Instance,
surface::Surface,
texture,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// Power preference for selecting a GPU adapter.
pub enum PowerPreference {
HighPerformance,
LowPower,
}
impl PowerPreference {
pub(crate) fn to_wgpu(self) -> wgpu::PowerPreference {
return match self {
PowerPreference::HighPerformance => {
wgpu::PowerPreference::HighPerformance
}
PowerPreference::LowPower => wgpu::PowerPreference::LowPower,
};
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// Memory allocation hints for device and resource creation.
pub enum MemoryHints {
Performance,
MemoryUsage,
}
impl MemoryHints {
pub(crate) fn to_wgpu(self) -> wgpu::MemoryHints {
return match self {
MemoryHints::Performance => wgpu::MemoryHints::Performance,
MemoryHints::MemoryUsage => wgpu::MemoryHints::MemoryUsage,
};
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// Feature bitset required/enabled on the device.
pub struct Features(wgpu::Features);
impl Features {
pub(crate) fn to_wgpu(self) -> wgpu::Features {
self.0
}
}
impl std::ops::BitOr for Features {
type Output = Features;
fn bitor(self, rhs: Features) -> Features {
return Features(self.0 | rhs.0);
}
}
#[derive(Clone, Copy, Debug)]
/// Public, engine-facing subset of device limits.
pub struct GpuLimits {
pub max_uniform_buffer_binding_size: u64,
pub max_bind_groups: u32,
pub max_vertex_buffers: u32,
pub max_vertex_attributes: u32,
pub min_uniform_buffer_offset_alignment: u32,
}
#[derive(Debug, Clone)]
/// Builder for a `Gpu` (adapter, device, queue) with feature validation.
pub struct GpuBuilder {
label: Option<String>,
power_preference: PowerPreference,
force_fallback_adapter: bool,
required_features: Features,
memory_hints: MemoryHints,
}
impl GpuBuilder {
/// Create a builder with defaults favoring performance.
pub fn new() -> Self {
Self {
label: Some("Lambda GPU".to_string()),
power_preference: PowerPreference::HighPerformance,
force_fallback_adapter: false,
required_features: Features(wgpu::Features::IMMEDIATES),
memory_hints: MemoryHints::Performance,
}
}
/// Attach a label used for the device.
pub fn with_label(mut self, label: &str) -> Self {
self.label = Some(label.to_string());
self
}
/// Select the adapter power preference (e.g., LowPower for laptops).
pub fn with_power_preference(mut self, preference: PowerPreference) -> Self {
self.power_preference = preference;
self
}
/// Force using a fallback adapter when a primary device is unavailable.
pub fn force_fallback(mut self, force: bool) -> Self {
self.force_fallback_adapter = force;
self
}
/// Require `wgpu::Features` to be present on the adapter.
pub fn with_required_features(mut self, features: Features) -> Self {
self.required_features = features;
self
}
/// Provide memory allocation hints for the device.
pub fn with_memory_hints(mut self, hints: MemoryHints) -> Self {
self.memory_hints = hints;
self
}
/// Request an adapter and device/queue pair and return a `Gpu` wrapper.
///
/// Returns an error if no adapter is available, required features are
/// missing, or device creation fails.
pub fn build<'surface>(
self,
instance: &Instance,
surface: Option<&Surface<'surface>>,
) -> Result<Gpu, GpuBuildError> {
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: self.power_preference.to_wgpu(),
force_fallback_adapter: self.force_fallback_adapter,
compatible_surface: surface.map(|surface| surface.surface()),
})
.map_err(|_| GpuBuildError::AdapterUnavailable)?;
let adapter_features = adapter.features();
if !adapter_features.contains(self.required_features.to_wgpu()) {
return Err(GpuBuildError::MissingFeatures {
requested: self.required_features,
available: Features(adapter_features),
});
}
let descriptor = wgpu::DeviceDescriptor {
label: self.label.as_deref(),
required_features: self.required_features.to_wgpu(),
required_limits: adapter.limits(),
experimental_features: wgpu::ExperimentalFeatures::default(),
memory_hints: self.memory_hints.to_wgpu(),
trace: wgpu::Trace::Off,
};
let (device, queue) = block_on(adapter.request_device(&descriptor))?;
return Ok(Gpu {
adapter,
device,
queue,
limits: descriptor.required_limits,
});
}
}
impl Default for GpuBuilder {
fn default() -> Self {
return Self::new();
}
}
/// Errors emitted while building a `Gpu`.
#[derive(Debug)]
pub enum GpuBuildError {
/// No compatible adapter could be found.
AdapterUnavailable,
/// The requested features are not supported by the selected adapter.
MissingFeatures {
requested: Features,
available: Features,
},
/// Wrapper for `wgpu::RequestDeviceError`.
RequestDevice(String),
}
impl From<wgpu::RequestDeviceError> for GpuBuildError {
fn from(error: wgpu::RequestDeviceError) -> Self {
return GpuBuildError::RequestDevice(format!("{:?}", error));
}
}
/// Holds the chosen adapter along with its logical device and submission queue
/// plus an immutable copy of the limits used to create the device.
#[derive(Debug)]
pub struct Gpu {
adapter: wgpu::Adapter,
device: wgpu::Device,
queue: wgpu::Queue,
limits: wgpu::Limits,
}
impl Gpu {
/// Whether the provided texture format supports the sample count for render attachments.
pub fn supports_sample_count_for_format(
&self,
format: texture::TextureFormat,
sample_count: u32,
) -> bool {
return self.supports_sample_count(format.to_wgpu(), sample_count);
}
/// Whether the provided depth format supports the sample count for render attachments.
pub fn supports_sample_count_for_depth(
&self,
format: texture::DepthFormat,
sample_count: u32,
) -> bool {
return self.supports_sample_count(format.to_wgpu(), sample_count);
}
/// Borrow the adapter used to create the device.
///
/// Crate-visible to avoid exposing raw `wgpu` to higher layers.
pub(crate) fn adapter(&self) -> &wgpu::Adapter {
&self.adapter
}
/// Borrow the logical device for resource creation.
///
/// Crate-visible to avoid exposing raw `wgpu` to higher layers.
pub(crate) fn device(&self) -> &wgpu::Device {
&self.device
}
/// Borrow the submission queue for command submission.
///
/// Crate-visible to avoid exposing raw `wgpu` to higher layers.
pub(crate) fn queue(&self) -> &wgpu::Queue {
&self.queue
}
/// Limits captured at device creation time.
pub fn limits(&self) -> GpuLimits {
return GpuLimits {
max_uniform_buffer_binding_size: self
.limits
.max_uniform_buffer_binding_size,
max_bind_groups: self.limits.max_bind_groups,
max_vertex_buffers: self.limits.max_vertex_buffers,
max_vertex_attributes: self.limits.max_vertex_attributes,
min_uniform_buffer_offset_alignment: self
.limits
.min_uniform_buffer_offset_alignment,
};
}
/// Submit one or more command buffers to the device queue.
pub fn submit<I>(&self, list: I)
where
I: IntoIterator<Item = CommandBuffer>,
{
let iter = list.into_iter().map(|cb| cb.into_raw());
self.queue.submit(iter);
}
fn supports_sample_count(
&self,
format: wgpu::TextureFormat,
sample_count: u32,
) -> bool {
if sample_count <= 1 {
return true;
}
let features = self.adapter.get_texture_format_features(format);
if !features
.allowed_usages
.contains(wgpu::TextureUsages::RENDER_ATTACHMENT)
{
return false;
}
match sample_count {
2 => features
.flags
.contains(wgpu::TextureFormatFeatureFlags::MULTISAMPLE_X2),
4 => features
.flags
.contains(wgpu::TextureFormatFeatureFlags::MULTISAMPLE_X4),
8 => features
.flags
.contains(wgpu::TextureFormatFeatureFlags::MULTISAMPLE_X8),
16 => features
.flags
.contains(wgpu::TextureFormatFeatureFlags::MULTISAMPLE_X16),
_ => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::wgpu::{
instance,
texture,
};
#[test]
fn gpu_build_error_wraps_request_device_error() {
// RequestDeviceError is opaque in wgpu 26 (no public constructors or variants).
// This test previously validated pattern matching on a specific variant; now we
// simply assert the From<wgpu::RequestDeviceError> implementation exists by
// checking the trait bound at compile time.
fn assert_from_impl<T: From<wgpu::RequestDeviceError>>() {}
assert_from_impl::<GpuBuildError>();
}
/// Create an offscreen GPU for sample-count support tests.
///
/// Returns `None` when no compatible adapter is available so tests can be
/// skipped instead of failing.
fn create_test_gpu() -> Option<Gpu> {
let instance = instance::InstanceBuilder::new()
.with_label("gpu-test-instance")
.build();
return GpuBuilder::new()
.with_label("gpu-test-device")
.build(&instance, None)
.ok();
}
/// Accepts zero or single-sample attachments for any format.
#[test]
fn single_sample_always_supported() {
let gpu = match create_test_gpu() {
Some(gpu) => gpu,
None => {
eprintln!(
"Skipping single_sample_always_supported: no compatible GPU adapter"
);
return;
}
};
let surface_format =
texture::TextureFormat::from_wgpu(wgpu::TextureFormat::Bgra8UnormSrgb);
let depth_format = texture::DepthFormat::Depth32Float;
assert!(gpu.supports_sample_count_for_format(surface_format, 1));
assert!(gpu.supports_sample_count_for_format(surface_format, 0));
assert!(gpu.supports_sample_count_for_depth(depth_format, 1));
assert!(gpu.supports_sample_count_for_depth(depth_format, 0));
}
/// Rejects sample counts that are outside the supported set.
#[test]
fn unsupported_sample_count_rejected() {
let gpu = match create_test_gpu() {
Some(gpu) => gpu,
None => {
eprintln!(
"Skipping unsupported_sample_count_rejected: no compatible GPU adapter"
);
return;
}
};
let surface_format =
texture::TextureFormat::from_wgpu(wgpu::TextureFormat::Bgra8Unorm);
let depth_format = texture::DepthFormat::Depth32Float;
assert!(!gpu.supports_sample_count_for_format(surface_format, 3));
assert!(!gpu.supports_sample_count_for_depth(depth_format, 3));
}
/// Mirrors the adapter's texture feature flags for surface formats.
#[test]
fn surface_support_matches_texture_features() {
let gpu = match create_test_gpu() {
Some(gpu) => gpu,
None => {
eprintln!(
"Skipping surface_support_matches_texture_features: \
no compatible GPU adapter"
);
return;
}
};
let surface_format =
texture::TextureFormat::from_wgpu(wgpu::TextureFormat::Bgra8UnormSrgb);
let features = gpu
.adapter
.get_texture_format_features(surface_format.to_wgpu());
let expected = features
.allowed_usages
.contains(wgpu::TextureUsages::RENDER_ATTACHMENT)
&& features
.flags
.contains(wgpu::TextureFormatFeatureFlags::MULTISAMPLE_X4);
assert_eq!(
gpu.supports_sample_count_for_format(surface_format, 4),
expected
);
}
/// Mirrors the adapter's texture feature flags for depth formats.
#[test]
fn depth_support_matches_texture_features() {
let gpu = match create_test_gpu() {
Some(gpu) => gpu,
None => {
eprintln!(
"Skipping depth_support_matches_texture_features: \
no compatible GPU adapter"
);
return;
}
};
let depth_format = texture::DepthFormat::Depth32Float;
let features = gpu
.adapter
.get_texture_format_features(depth_format.to_wgpu());
let expected = features
.allowed_usages
.contains(wgpu::TextureUsages::RENDER_ATTACHMENT)
&& features
.flags
.contains(wgpu::TextureFormatFeatureFlags::MULTISAMPLE_X4);
assert_eq!(
gpu.supports_sample_count_for_depth(depth_format, 4),
expected
);
}
}