Debian Codegraph @

third_party/rust/wgpu-hal/src/metal/adapter.rs

raw
1 use metal::{
2 MTLArgumentBuffersTier, MTLCounterSamplingPoint, MTLFeatureSet, MTLGPUFamily,
3 MTLLanguageVersion, MTLPixelFormat, MTLReadWriteTextureTier, NSInteger,
4 };
5 use objc::{class, msg_send, sel, sel_impl};
6 use parking_lot::Mutex;
7 use wgt::{AstcBlock, AstcChannel};
8
9 use alloc::sync::Arc;
10 use std::thread;
11
12 use super::TimestampQuerySupport;
13
14 const MAX_COMMAND_BUFFERS: u64 = 2048;
15
16 unsafe impl Send for super::Adapter {}
17 unsafe impl Sync for super::Adapter {}
18
19 impl super::Adapter {
20 pub(super) fn new(shared: Arc<super::AdapterShared>) -> Self {
21 Self { shared }
22 }
23 }
24
25 impl crate::Adapter for super::Adapter {
26 type A = super::Api;
27
28 unsafe fn open(
29 &self,
30 features: wgt::Features,
31 _limits: &wgt::Limits,
32 _memory_hints: &wgt::MemoryHints,
33 ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> {
34 let queue = self
35 .shared
36 .device
37 .lock()
38 .new_command_queue_with_max_command_buffer_count(MAX_COMMAND_BUFFERS);
39
40 // Acquiring the meaning of timestamp ticks is hard with Metal!
41 // The only thing there is is a method correlating cpu & gpu timestamps (`device.sample_timestamps`).
42 // Users are supposed to call this method twice and calculate the difference,
43 // see "Converting GPU Timestamps into CPU Time":
44 // https://developer.apple.com/documentation/metal/gpu_counters_and_counter_sample_buffers/converting_gpu_timestamps_into_cpu_time
45 // Not only does this mean we get an approximate value, this is as also *very slow*!
46 // Chromium opted to solve this using a linear regression that they stop at some point
47 // https://source.chromium.org/chromium/chromium/src/+/refs/heads/main:third_party/dawn/src/dawn/native/metal/DeviceMTL.mm;drc=76be2f9f117654f3fe4faa477b0445114fccedda;bpv=0;bpt=1;l=46
48 // Generally, the assumption is that timestamp values aren't changing over time, after all all other APIs provide stable values.
49 //
50 // We should do as Chromium does for the general case, but this requires quite some state tracking
51 // and doesn't even provide perfectly accurate values, especially at the start of the application when
52 // we didn't have the chance to sample a lot of values just yet.
53 //
54 // So instead, we're doing the dangerous but easy thing and use our "knowledge" of timestamps
55 // conversions on different devices, after all Metal isn't supported on that many ;)
56 // Based on:
57 // * https://github.com/gfx-rs/wgpu/pull/2528
58 // * https://github.com/gpuweb/gpuweb/issues/1325#issuecomment-761041326
59 let timestamp_period = if self.shared.device.lock().name().starts_with("Intel") {
60 83.333
61 } else {
62 // Known for Apple Silicon (at least M1 & M2, iPad Pro 2018) and AMD GPUs.
63 1.0
64 };
65
66 Ok(crate::OpenDevice {
67 device: super::Device {
68 shared: Arc::clone(&self.shared),
69 features,
70 counters: Default::default(),
71 },
72 queue: super::Queue {
73 raw: Arc::new(Mutex::new(queue)),
74 timestamp_period,
75 },
76 })
77 }
78
79 unsafe fn texture_format_capabilities(
80 &self,
81 format: wgt::TextureFormat,
82 ) -> crate::TextureFormatCapabilities {
83 use crate::TextureFormatCapabilities as Tfc;
84 use wgt::TextureFormat as Tf;
85
86 let pc = &self.shared.private_caps;
87 // Affected formats documented at:
88 // https://developer.apple.com/documentation/metal/mtlreadwritetexturetier/mtlreadwritetexturetier1?language=objc
89 // https://developer.apple.com/documentation/metal/mtlreadwritetexturetier/mtlreadwritetexturetier2?language=objc
90 let (read_write_tier1_if, read_write_tier2_if) = match pc.read_write_texture_tier {
91 MTLReadWriteTextureTier::TierNone => (Tfc::empty(), Tfc::empty()),
92 MTLReadWriteTextureTier::Tier1 => (Tfc::STORAGE_READ_WRITE, Tfc::empty()),
93 MTLReadWriteTextureTier::Tier2 => (Tfc::STORAGE_READ_WRITE, Tfc::STORAGE_READ_WRITE),
94 };
95 let msaa_count = pc.sample_count_mask;
96
97 let msaa_resolve_desktop_if = if pc.msaa_desktop {
98 Tfc::MULTISAMPLE_RESOLVE
99 } else {
100 Tfc::empty()
101 };
102 let msaa_resolve_apple3x_if = if pc.msaa_desktop | pc.msaa_apple3 {
103 Tfc::MULTISAMPLE_RESOLVE
104 } else {
105 Tfc::empty()
106 };
107 let is_not_apple1x = super::PrivateCapabilities::supports_any(
108 self.shared.device.lock().as_ref(),
109 &[
110 MTLFeatureSet::iOS_GPUFamily2_v1,
111 MTLFeatureSet::macOS_GPUFamily1_v1,
112 MTLFeatureSet::tvOS_GPUFamily1_v1,
113 ],
114 );
115
116 let image_atomic_if = if pc.msl_version >= MTLLanguageVersion::V3_1 {
117 Tfc::STORAGE_ATOMIC
118 } else {
119 Tfc::empty()
120 };
121
122 let image_64_atomic_if = if pc.int64_atomics {
123 Tfc::STORAGE_ATOMIC
124 } else {
125 Tfc::empty()
126 };
127
128 // Metal defined pixel format capabilities
129 let all_caps = Tfc::SAMPLED_LINEAR
130 | Tfc::STORAGE_WRITE_ONLY
131 | Tfc::COLOR_ATTACHMENT
132 | Tfc::COLOR_ATTACHMENT_BLEND
133 | msaa_count
134 | Tfc::MULTISAMPLE_RESOLVE;
135
136 let extra = match format {
137 Tf::R8Unorm | Tf::R16Float | Tf::Rgba8Unorm | Tf::Rgba16Float => {
138 read_write_tier2_if | all_caps
139 }
140 Tf::R8Snorm | Tf::Rg8Snorm | Tf::Rgba8Snorm => {
141 let mut flags = all_caps;
142 flags.set(Tfc::MULTISAMPLE_RESOLVE, is_not_apple1x);
143 flags
144 }
145 Tf::R8Uint
146 | Tf::R8Sint
147 | Tf::R16Uint
148 | Tf::R16Sint
149 | Tf::Rgba8Uint
150 | Tf::Rgba8Sint
151 | Tf::Rgba16Uint
152 | Tf::Rgba16Sint => {
153 read_write_tier2_if | Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT | msaa_count
154 }
155 Tf::R16Unorm
156 | Tf::R16Snorm
157 | Tf::Rg16Unorm
158 | Tf::Rg16Snorm
159 | Tf::Rgba16Unorm
160 | Tf::Rgba16Snorm => {
161 Tfc::SAMPLED_LINEAR
162 | Tfc::STORAGE_WRITE_ONLY
163 | Tfc::COLOR_ATTACHMENT
164 | Tfc::COLOR_ATTACHMENT_BLEND
165 | msaa_count
166 | msaa_resolve_desktop_if
167 }
168 Tf::Rg8Unorm | Tf::Rg16Float | Tf::Bgra8Unorm => all_caps,
169 Tf::Rg8Uint | Tf::Rg8Sint => {
170 Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT | msaa_count
171 }
172 Tf::R32Uint | Tf::R32Sint => {
173 read_write_tier1_if
174 | Tfc::STORAGE_WRITE_ONLY
175 | Tfc::COLOR_ATTACHMENT
176 | msaa_count
177 | image_atomic_if
178 }
179 Tf::R32Float => {
180 let flags = if pc.format_r32float_all {
181 all_caps
182 } else {
183 Tfc::STORAGE_WRITE_ONLY
184 | Tfc::COLOR_ATTACHMENT
185 | Tfc::COLOR_ATTACHMENT_BLEND
186 | msaa_count
187 };
188 read_write_tier1_if | flags
189 }
190 Tf::Rg16Uint | Tf::Rg16Sint => {
191 Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT | msaa_count
192 }
193 Tf::Rgba8UnormSrgb | Tf::Bgra8UnormSrgb => {
194 let mut flags = all_caps;
195 flags.set(Tfc::STORAGE_WRITE_ONLY, pc.format_rgba8_srgb_all);
196 flags
197 }
198 Tf::Rgb10a2Uint => {
199 let mut flags = Tfc::COLOR_ATTACHMENT | msaa_count;
200 flags.set(Tfc::STORAGE_WRITE_ONLY, pc.format_rgb10a2_uint_write);
201 flags
202 }
203 Tf::Rgb10a2Unorm => {
204 let mut flags = all_caps;
205 flags.set(Tfc::STORAGE_WRITE_ONLY, pc.format_rgb10a2_unorm_all);
206 flags
207 }
208 Tf::Rg11b10Ufloat => {
209 let mut flags = all_caps;
210 flags.set(Tfc::STORAGE_WRITE_ONLY, pc.format_rg11b10_all);
211 flags
212 }
213 Tf::R64Uint => {
214 Tfc::COLOR_ATTACHMENT
215 | Tfc::STORAGE_WRITE_ONLY
216 | image_64_atomic_if
217 | read_write_tier1_if
218 }
219 Tf::Rg32Uint | Tf::Rg32Sint => {
220 Tfc::COLOR_ATTACHMENT | Tfc::STORAGE_WRITE_ONLY | msaa_count
221 }
222 Tf::Rg32Float => {
223 if pc.format_rg32float_all {
224 all_caps
225 } else {
226 Tfc::STORAGE_WRITE_ONLY
227 | Tfc::COLOR_ATTACHMENT
228 | Tfc::COLOR_ATTACHMENT_BLEND
229 | msaa_count
230 }
231 }
232 Tf::Rgba32Uint | Tf::Rgba32Sint => {
233 read_write_tier2_if | Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT | msaa_count
234 }
235 Tf::Rgba32Float => {
236 let mut flags =
237 read_write_tier2_if | Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT;
238 if pc.format_rgba32float_all {
239 flags |= all_caps
240 } else if pc.msaa_apple7 {
241 flags |= msaa_count
242 };
243 flags
244 }
245 Tf::Stencil8 => {
246 all_caps | Tfc::DEPTH_STENCIL_ATTACHMENT | msaa_count | msaa_resolve_apple3x_if
247 }
248 Tf::Depth16Unorm => {
249 let mut flags =
250 Tfc::DEPTH_STENCIL_ATTACHMENT | msaa_count | msaa_resolve_apple3x_if;
251 if pc.format_depth16unorm {
252 flags |= Tfc::SAMPLED_LINEAR
253 }
254 flags
255 }
256 Tf::Depth32Float | Tf::Depth32FloatStencil8 => {
257 let mut flags =
258 Tfc::DEPTH_STENCIL_ATTACHMENT | msaa_count | msaa_resolve_apple3x_if;
259 if pc.format_depth32float_filter {
260 flags |= Tfc::SAMPLED_LINEAR
261 }
262 flags
263 }
264 Tf::Depth24Plus | Tf::Depth24PlusStencil8 => {
265 let mut flags = Tfc::DEPTH_STENCIL_ATTACHMENT | msaa_count;
266 if pc.format_depth24_stencil8 {
267 flags |= Tfc::SAMPLED_LINEAR | Tfc::MULTISAMPLE_RESOLVE
268 } else {
269 flags |= msaa_resolve_apple3x_if;
270 if pc.format_depth32float_filter {
271 flags |= Tfc::SAMPLED_LINEAR
272 }
273 }
274 flags
275 }
276 Tf::NV12 => return Tfc::empty(),
277 Tf::Rgb9e5Ufloat => {
278 if pc.msaa_apple3 {
279 all_caps
280 } else if pc.msaa_desktop {
281 Tfc::SAMPLED_LINEAR
282 } else {
283 Tfc::SAMPLED_LINEAR
284 | Tfc::COLOR_ATTACHMENT
285 | Tfc::COLOR_ATTACHMENT_BLEND
286 | msaa_count
287 | Tfc::MULTISAMPLE_RESOLVE
288 }
289 }
290 Tf::Bc1RgbaUnorm
291 | Tf::Bc1RgbaUnormSrgb
292 | Tf::Bc2RgbaUnorm
293 | Tf::Bc2RgbaUnormSrgb
294 | Tf::Bc3RgbaUnorm
295 | Tf::Bc3RgbaUnormSrgb
296 | Tf::Bc4RUnorm
297 | Tf::Bc4RSnorm
298 | Tf::Bc5RgUnorm
299 | Tf::Bc5RgSnorm
300 | Tf::Bc6hRgbUfloat
301 | Tf::Bc6hRgbFloat
302 | Tf::Bc7RgbaUnorm
303 | Tf::Bc7RgbaUnormSrgb => {
304 if pc.format_bc {
305 Tfc::SAMPLED_LINEAR
306 } else {
307 Tfc::empty()
308 }
309 }
310 Tf::Etc2Rgb8Unorm
311 | Tf::Etc2Rgb8UnormSrgb
312 | Tf::Etc2Rgb8A1Unorm
313 | Tf::Etc2Rgb8A1UnormSrgb
314 | Tf::Etc2Rgba8Unorm
315 | Tf::Etc2Rgba8UnormSrgb
316 | Tf::EacR11Unorm
317 | Tf::EacR11Snorm
318 | Tf::EacRg11Unorm
319 | Tf::EacRg11Snorm => {
320 if pc.format_eac_etc {
321 Tfc::SAMPLED_LINEAR
322 } else {
323 Tfc::empty()
324 }
325 }
326 Tf::Astc {
327 block: _,
328 channel: _,
329 } => {
330 if pc.format_astc || pc.format_astc_hdr {
331 Tfc::SAMPLED_LINEAR
332 } else {
333 Tfc::empty()
334 }
335 }
336 };
337
338 Tfc::COPY_SRC | Tfc::COPY_DST | Tfc::SAMPLED | Tfc::STORAGE_READ_ONLY | extra
339 }
340
341 unsafe fn surface_capabilities(
342 &self,
343 surface: &super::Surface,
344 ) -> Option<crate::SurfaceCapabilities> {
345 let current_extent = if surface.main_thread_id == thread::current().id() {
346 Some(surface.dimensions())
347 } else {
348 log::warn!("Unable to get the current view dimensions on a non-main thread");
349 None
350 };
351
352 let mut formats = vec![
353 wgt::TextureFormat::Bgra8Unorm,
354 wgt::TextureFormat::Bgra8UnormSrgb,
355 wgt::TextureFormat::Rgba16Float,
356 ];
357 if self.shared.private_caps.format_rgb10a2_unorm_all {
358 formats.push(wgt::TextureFormat::Rgb10a2Unorm);
359 }
360
361 let pc = &self.shared.private_caps;
362 Some(crate::SurfaceCapabilities {
363 formats,
364 // We use this here to govern the maximum number of drawables + 1.
365 // See https://developer.apple.com/documentation/quartzcore/cametallayer/2938720-maximumdrawablecount
366 maximum_frame_latency: if pc.can_set_maximum_drawables_count {
367 1..=2
368 } else {
369 // 3 is the default value for maximum drawables in `CAMetalLayer` documentation
370 // iOS 10.3 was tested to use 3 on iphone5s
371 2..=2
372 },
373 present_modes: if pc.can_set_display_sync {
374 vec![wgt::PresentMode::Fifo, wgt::PresentMode::Immediate]
375 } else {
376 vec![wgt::PresentMode::Fifo]
377 },
378 composite_alpha_modes: vec![
379 wgt::CompositeAlphaMode::Opaque,
380 wgt::CompositeAlphaMode::PostMultiplied,
381 ],
382
383 current_extent,
384 usage: wgt::TextureUses::COLOR_TARGET
385 | wgt::TextureUses::COPY_SRC
386 | wgt::TextureUses::COPY_DST
387 | wgt::TextureUses::STORAGE_READ_ONLY
388 | wgt::TextureUses::STORAGE_WRITE_ONLY
389 | wgt::TextureUses::STORAGE_READ_WRITE,
390 })
391 }
392
393 unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
394 let timestamp = self.shared.presentation_timer.get_timestamp_ns();
395
396 wgt::PresentationTimestamp(timestamp)
397 }
398 }
399
400 const RESOURCE_HEAP_SUPPORT: &[MTLFeatureSet] = &[
401 MTLFeatureSet::iOS_GPUFamily1_v3,
402 MTLFeatureSet::tvOS_GPUFamily1_v2,
403 MTLFeatureSet::macOS_GPUFamily1_v3,
404 ];
405
406 const MUTABLE_COMPARISON_SAMPLER_SUPPORT: &[MTLFeatureSet] = &[
407 MTLFeatureSet::iOS_GPUFamily3_v1,
408 MTLFeatureSet::macOS_GPUFamily1_v1,
409 ];
410
411 const SAMPLER_CLAMP_TO_BORDER_SUPPORT: &[MTLFeatureSet] = &[MTLFeatureSet::macOS_GPUFamily1_v2];
412
413 const ASTC_PIXEL_FORMAT_FEATURES: &[MTLFeatureSet] = &[
414 MTLFeatureSet::iOS_GPUFamily2_v1,
415 MTLFeatureSet::tvOS_GPUFamily1_v1,
416 ];
417
418 const ANY8_UNORM_SRGB_ALL: &[MTLFeatureSet] = &[
419 MTLFeatureSet::iOS_GPUFamily2_v3,
420 MTLFeatureSet::tvOS_GPUFamily1_v2,
421 ];
422
423 const ANY8_SNORM_RESOLVE: &[MTLFeatureSet] = &[
424 MTLFeatureSet::iOS_GPUFamily2_v1,
425 MTLFeatureSet::tvOS_GPUFamily1_v1,
426 MTLFeatureSet::macOS_GPUFamily1_v1,
427 ];
428
429 const RGBA8_SRGB: &[MTLFeatureSet] = &[
430 MTLFeatureSet::iOS_GPUFamily2_v3,
431 MTLFeatureSet::tvOS_GPUFamily1_v2,
432 ];
433
434 const RGB10A2UNORM_ALL: &[MTLFeatureSet] = &[
435 MTLFeatureSet::iOS_GPUFamily3_v1,
436 MTLFeatureSet::tvOS_GPUFamily2_v1,
437 MTLFeatureSet::macOS_GPUFamily1_v1,
438 ];
439
440 const RGB10A2UINT_WRITE: &[MTLFeatureSet] = &[
441 MTLFeatureSet::iOS_GPUFamily3_v1,
442 MTLFeatureSet::tvOS_GPUFamily2_v1,
443 MTLFeatureSet::macOS_GPUFamily1_v1,
444 ];
445
446 const RG11B10FLOAT_ALL: &[MTLFeatureSet] = &[
447 MTLFeatureSet::iOS_GPUFamily3_v1,
448 MTLFeatureSet::tvOS_GPUFamily2_v1,
449 MTLFeatureSet::macOS_GPUFamily1_v1,
450 ];
451
452 const RGB9E5FLOAT_ALL: &[MTLFeatureSet] = &[
453 MTLFeatureSet::iOS_GPUFamily3_v1,
454 MTLFeatureSet::tvOS_GPUFamily2_v1,
455 ];
456
457 const BGR10A2_ALL: &[MTLFeatureSet] = &[
458 MTLFeatureSet::iOS_GPUFamily1_v4,
459 MTLFeatureSet::tvOS_GPUFamily1_v3,
460 MTLFeatureSet::macOS_GPUFamily2_v1,
461 ];
462
463 /// "Indirect draw & dispatch arguments" in the Metal feature set tables
464 const INDIRECT_DRAW_DISPATCH_SUPPORT: &[MTLFeatureSet] = &[
465 MTLFeatureSet::iOS_GPUFamily3_v1,
466 MTLFeatureSet::tvOS_GPUFamily2_v1,
467 MTLFeatureSet::macOS_GPUFamily1_v1,
468 ];
469
470 /// "Base vertex/instance drawing" in the Metal feature set tables
471 ///
472 /// in our terms, `base_vertex` and `first_instance` must be 0
473 const BASE_VERTEX_FIRST_INSTANCE_SUPPORT: &[MTLFeatureSet] = INDIRECT_DRAW_DISPATCH_SUPPORT;
474
475 const TEXTURE_CUBE_ARRAY_SUPPORT: &[MTLFeatureSet] = &[
476 MTLFeatureSet::iOS_GPUFamily4_v1,
477 MTLFeatureSet::tvOS_GPUFamily1_v2,
478 MTLFeatureSet::macOS_GPUFamily1_v1,
479 ];
480
481 const DUAL_SOURCE_BLEND_SUPPORT: &[MTLFeatureSet] = &[
482 MTLFeatureSet::iOS_GPUFamily1_v4,
483 MTLFeatureSet::tvOS_GPUFamily1_v3,
484 MTLFeatureSet::macOS_GPUFamily1_v2,
485 ];
486
487 const LAYERED_RENDERING_SUPPORT: &[MTLFeatureSet] = &[
488 MTLFeatureSet::iOS_GPUFamily5_v1,
489 MTLFeatureSet::macOS_GPUFamily1_v1,
490 MTLFeatureSet::macOS_GPUFamily2_v1,
491 ];
492
493 const FUNCTION_SPECIALIZATION_SUPPORT: &[MTLFeatureSet] = &[
494 MTLFeatureSet::iOS_GPUFamily1_v3,
495 MTLFeatureSet::tvOS_GPUFamily1_v2,
496 MTLFeatureSet::macOS_GPUFamily1_v2,
497 ];
498
499 const DEPTH_CLIP_MODE: &[MTLFeatureSet] = &[
500 MTLFeatureSet::iOS_GPUFamily4_v1,
501 MTLFeatureSet::tvOS_GPUFamily1_v3,
502 MTLFeatureSet::macOS_GPUFamily1_v1,
503 ];
504
505 const OS_NOT_SUPPORT: (usize, usize) = (10000, 0);
506
507 impl super::PrivateCapabilities {
508 fn supports_any(raw: &metal::DeviceRef, features_sets: &[MTLFeatureSet]) -> bool {
509 features_sets
510 .iter()
511 .cloned()
512 .any(|x| raw.supports_feature_set(x))
513 }
514
515 pub fn new(device: &metal::Device) -> Self {
516 #[repr(C)]
517 #[derive(Clone, Copy, Debug)]
518 #[allow(clippy::upper_case_acronyms)]
519 struct NSOperatingSystemVersion {
520 major: usize,
521 minor: usize,
522 patch: usize,
523 }
524
525 impl NSOperatingSystemVersion {
526 fn at_least(
527 &self,
528 mac_version: (usize, usize),
529 ios_version: (usize, usize),
530 is_mac: bool,
531 ) -> bool {
532 if is_mac {
533 self.major > mac_version.0
534 || (self.major == mac_version.0 && self.minor >= mac_version.1)
535 } else {
536 self.major > ios_version.0
537 || (self.major == ios_version.0 && self.minor >= ios_version.1)
538 }
539 }
540 }
541
542 let version: NSOperatingSystemVersion = unsafe {
543 let process_info: *mut objc::runtime::Object =
544 msg_send![class!(NSProcessInfo), processInfo];
545 msg_send![process_info, operatingSystemVersion]
546 };
547
548 let os_is_mac = device.supports_feature_set(MTLFeatureSet::macOS_GPUFamily1_v1);
549 // Metal was first introduced in OS X 10.11 and iOS 8. The current version number of visionOS is 1.0.0. Additionally,
550 // on the Simulator, Apple only provides the Apple2 GPU capability, and the Apple2+ GPU capability covers the capabilities of Apple2.
551 // Therefore, the following conditions can be used to determine if it is visionOS.
552 // https://developer.apple.com/documentation/metal/developing_metal_apps_that_run_in_simulator
553 let os_is_xr = version.major < 8 && device.supports_family(MTLGPUFamily::Apple2);
554 let family_check = os_is_xr || version.at_least((10, 15), (13, 0), os_is_mac);
555
556 let mut sample_count_mask = crate::TextureFormatCapabilities::MULTISAMPLE_X4; // 1 and 4 samples are supported on all devices
557 if device.supports_texture_sample_count(2) {
558 sample_count_mask |= crate::TextureFormatCapabilities::MULTISAMPLE_X2;
559 }
560 if device.supports_texture_sample_count(8) {
561 sample_count_mask |= crate::TextureFormatCapabilities::MULTISAMPLE_X8;
562 }
563 if device.supports_texture_sample_count(16) {
564 sample_count_mask |= crate::TextureFormatCapabilities::MULTISAMPLE_X16;
565 }
566
567 let rw_texture_tier = if version.at_least((10, 13), (11, 0), os_is_mac) {
568 device.read_write_texture_support()
569 } else if version.at_least((10, 12), OS_NOT_SUPPORT, os_is_mac) {
570 if Self::supports_any(device, &[MTLFeatureSet::macOS_ReadWriteTextureTier2]) {
571 MTLReadWriteTextureTier::Tier2
572 } else {
573 MTLReadWriteTextureTier::Tier1
574 }
575 } else {
576 MTLReadWriteTextureTier::TierNone
577 };
578
579 let mut timestamp_query_support = TimestampQuerySupport::empty();
580 if version.at_least((11, 0), (14, 0), os_is_mac)
581 && device.supports_counter_sampling(MTLCounterSamplingPoint::AtStageBoundary)
582 {
583 // If we don't support at stage boundary, don't support anything else.
584 timestamp_query_support.insert(TimestampQuerySupport::STAGE_BOUNDARIES);
585
586 if device.supports_counter_sampling(MTLCounterSamplingPoint::AtDrawBoundary) {
587 timestamp_query_support.insert(TimestampQuerySupport::ON_RENDER_ENCODER);
588 }
589 if device.supports_counter_sampling(MTLCounterSamplingPoint::AtDispatchBoundary) {
590 timestamp_query_support.insert(TimestampQuerySupport::ON_COMPUTE_ENCODER);
591 }
592 if device.supports_counter_sampling(MTLCounterSamplingPoint::AtBlitBoundary) {
593 timestamp_query_support.insert(TimestampQuerySupport::ON_BLIT_ENCODER);
594 }
595 // `TimestampQuerySupport::INSIDE_WGPU_PASSES` emerges from the other flags.
596 }
597
598 let argument_buffers = device.argument_buffers_support();
599
600 Self {
601 family_check,
602 msl_version: if os_is_xr || version.at_least((14, 0), (17, 0), os_is_mac) {
603 MTLLanguageVersion::V3_1
604 } else if version.at_least((13, 0), (16, 0), os_is_mac) {
605 MTLLanguageVersion::V3_0
606 } else if version.at_least((12, 0), (15, 0), os_is_mac) {
607 MTLLanguageVersion::V2_4
608 } else if version.at_least((11, 0), (14, 0), os_is_mac) {
609 MTLLanguageVersion::V2_3
610 } else if version.at_least((10, 15), (13, 0), os_is_mac) {
611 MTLLanguageVersion::V2_2
612 } else if version.at_least((10, 14), (12, 0), os_is_mac) {
613 MTLLanguageVersion::V2_1
614 } else if version.at_least((10, 13), (11, 0), os_is_mac) {
615 MTLLanguageVersion::V2_0
616 } else if version.at_least((10, 12), (10, 0), os_is_mac) {
617 MTLLanguageVersion::V1_2
618 } else if version.at_least((10, 11), (9, 0), os_is_mac) {
619 MTLLanguageVersion::V1_1
620 } else {
621 MTLLanguageVersion::V1_0
622 },
623 // macOS 10.11 doesn't support read-write resources
624 fragment_rw_storage: version.at_least((10, 12), (8, 0), os_is_mac),
625 read_write_texture_tier: rw_texture_tier,
626 msaa_desktop: os_is_mac,
627 msaa_apple3: if family_check {
628 device.supports_family(MTLGPUFamily::Apple3)
629 } else {
630 device.supports_feature_set(MTLFeatureSet::iOS_GPUFamily3_v4)
631 },
632 msaa_apple7: family_check && device.supports_family(MTLGPUFamily::Apple7),
633 resource_heaps: Self::supports_any(device, RESOURCE_HEAP_SUPPORT),
634 argument_buffers,
635 shared_textures: !os_is_mac,
636 mutable_comparison_samplers: Self::supports_any(
637 device,
638 MUTABLE_COMPARISON_SAMPLER_SUPPORT,
639 ),
640 sampler_clamp_to_border: Self::supports_any(device, SAMPLER_CLAMP_TO_BORDER_SUPPORT),
641 indirect_draw_dispatch: Self::supports_any(device, INDIRECT_DRAW_DISPATCH_SUPPORT),
642 base_vertex_first_instance_drawing: Self::supports_any(
643 device,
644 BASE_VERTEX_FIRST_INSTANCE_SUPPORT,
645 ),
646 dual_source_blending: Self::supports_any(device, DUAL_SOURCE_BLEND_SUPPORT),
647 low_power: !os_is_mac || device.is_low_power(),
648 headless: os_is_mac && device.is_headless(),
649 layered_rendering: Self::supports_any(device, LAYERED_RENDERING_SUPPORT),
650 function_specialization: Self::supports_any(device, FUNCTION_SPECIALIZATION_SUPPORT),
651 depth_clip_mode: Self::supports_any(device, DEPTH_CLIP_MODE),
652 texture_cube_array: Self::supports_any(device, TEXTURE_CUBE_ARRAY_SUPPORT),
653 supports_float_filtering: os_is_mac
654 || (version.at_least((11, 0), (14, 0), os_is_mac)
655 && device.supports_32bit_float_filtering()),
656 format_depth24_stencil8: os_is_mac && device.d24_s8_supported(),
657 format_depth32_stencil8_filter: os_is_mac,
658 format_depth32_stencil8_none: !os_is_mac,
659 format_min_srgb_channels: if os_is_mac { 4 } else { 1 },
660 format_b5: !os_is_mac,
661 format_bc: os_is_mac,
662 format_eac_etc: !os_is_mac
663 // M1 in macOS supports EAC/ETC2
664 || (family_check && device.supports_family(MTLGPUFamily::Apple7)),
665 // A8(Apple2) and later always support ASTC pixel formats
666 format_astc: (family_check && device.supports_family(MTLGPUFamily::Apple2))
667 || Self::supports_any(device, ASTC_PIXEL_FORMAT_FEATURES),
668 // A13(Apple6) M1(Apple7) and later always support HDR ASTC pixel formats
669 format_astc_hdr: family_check && device.supports_family(MTLGPUFamily::Apple6),
670 // Apple3 and later supports compressed volume texture formats including ASTC Sliced 3D
671 format_astc_3d: family_check && device.supports_family(MTLGPUFamily::Apple3),
672 format_any8_unorm_srgb_all: Self::supports_any(device, ANY8_UNORM_SRGB_ALL),
673 format_any8_unorm_srgb_no_write: !Self::supports_any(device, ANY8_UNORM_SRGB_ALL)
674 && !os_is_mac,
675 format_any8_snorm_all: Self::supports_any(device, ANY8_SNORM_RESOLVE),
676 format_r16_norm_all: os_is_mac,
677 // No devices support r32's all capabilities
678 format_r32_all: false,
679 // All devices support r32's write capability
680 format_r32_no_write: false,
681 // iOS support r32float's write capability, macOS support r32float's all capabilities
682 format_r32float_no_write_no_filter: false,
683 // Only iOS doesn't support r32float's filter capability
684 format_r32float_no_filter: !os_is_mac,
685 format_r32float_all: os_is_mac,
686 format_rgba8_srgb_all: Self::supports_any(device, RGBA8_SRGB),
687 format_rgba8_srgb_no_write: !Self::supports_any(device, RGBA8_SRGB),
688 format_rgb10a2_unorm_all: Self::supports_any(device, RGB10A2UNORM_ALL),
689 format_rgb10a2_unorm_no_write: !Self::supports_any(device, RGB10A2UNORM_ALL),
690 format_rgb10a2_uint_write: Self::supports_any(device, RGB10A2UINT_WRITE),
691 format_rg11b10_all: Self::supports_any(device, RG11B10FLOAT_ALL),
692 format_rg11b10_no_write: !Self::supports_any(device, RG11B10FLOAT_ALL),
693 format_rgb9e5_all: Self::supports_any(device, RGB9E5FLOAT_ALL),
694 format_rgb9e5_no_write: !Self::supports_any(device, RGB9E5FLOAT_ALL) && !os_is_mac,
695 format_rgb9e5_filter_only: os_is_mac,
696 format_rg32_color: true,
697 format_rg32_color_write: true,
698 // Only macOS support rg32float's all capabilities
699 format_rg32float_all: os_is_mac,
700 // All devices support rg32float's color + blend capabilities
701 format_rg32float_color_blend: true,
702 // Only iOS doesn't support rg32float's filter
703 format_rg32float_no_filter: !os_is_mac,
704 format_rgba32int_color: true,
705 // All devices support rgba32uint and rgba32sint's color + write capabilities
706 format_rgba32int_color_write: true,
707 format_rgba32float_color: true,
708 // All devices support rgba32float's color + write capabilities
709 format_rgba32float_color_write: true,
710 // Only macOS support rgba32float's all capabilities
711 format_rgba32float_all: os_is_mac,
712 format_depth16unorm: Self::supports_any(
713 device,
714 &[
715 MTLFeatureSet::iOS_GPUFamily3_v3,
716 MTLFeatureSet::macOS_GPUFamily1_v2,
717 ],
718 ),
719 format_depth32float_filter: os_is_mac,
720 format_depth32float_none: !os_is_mac,
721 format_bgr10a2_all: Self::supports_any(device, BGR10A2_ALL),
722 format_bgr10a2_no_write: !Self::supports_any(device, BGR10A2_ALL),
723 max_buffers_per_stage: 31,
724 max_vertex_buffers: 31.min(crate::MAX_VERTEX_BUFFERS as u32),
725 max_textures_per_stage: if os_is_mac
726 || (family_check && device.supports_family(MTLGPUFamily::Apple6))
727 {
728 128
729 } else if family_check && device.supports_family(MTLGPUFamily::Apple4) {
730 96
731 } else {
732 31
733 },
734 max_samplers_per_stage: 16,
735 max_binding_array_elements: if argument_buffers == MTLArgumentBuffersTier::Tier2 {
736 1_000_000
737 } else if family_check && device.supports_family(MTLGPUFamily::Apple4) {
738 96
739 } else {
740 31
741 },
742 max_sampler_binding_array_elements: if family_check
743 && device.supports_family(MTLGPUFamily::Apple9)
744 {
745 500_000
746 } else if family_check
747 && (device.supports_family(MTLGPUFamily::Apple7)
748 || device.supports_family(MTLGPUFamily::Mac2))
749 {
750 1000
751 } else if family_check && device.supports_family(MTLGPUFamily::Apple6) {
752 128
753 } else {
754 16
755 },
756 buffer_alignment: if os_is_mac || os_is_xr { 256 } else { 64 },
757 max_buffer_size: if version.at_least((10, 14), (12, 0), os_is_mac) {
758 // maxBufferLength available on macOS 10.14+ and iOS 12.0+
759 let buffer_size: NSInteger = unsafe { msg_send![device.as_ref(), maxBufferLength] };
760 buffer_size as _
761 } else if os_is_mac {
762 1 << 30 // 1GB on macOS 10.11 and up
763 } else {
764 1 << 28 // 256MB on iOS 8.0+
765 },
766 max_texture_size: if Self::supports_any(
767 device,
768 &[
769 MTLFeatureSet::iOS_GPUFamily3_v1,
770 MTLFeatureSet::tvOS_GPUFamily2_v1,
771 MTLFeatureSet::macOS_GPUFamily1_v1,
772 ],
773 ) {
774 16384
775 } else {
776 8192
777 },
778 max_texture_3d_size: 2048,
779 max_texture_layers: 2048,
780 max_fragment_input_components: if os_is_mac
781 || device.supports_feature_set(MTLFeatureSet::iOS_GPUFamily4_v1)
782 {
783 124
784 } else {
785 60
786 },
787 max_color_render_targets: if Self::supports_any(
788 device,
789 &[
790 MTLFeatureSet::iOS_GPUFamily2_v1,
791 MTLFeatureSet::tvOS_GPUFamily1_v1,
792 MTLFeatureSet::macOS_GPUFamily1_v1,
793 ],
794 ) {
795 8
796 } else {
797 4
798 },
799 // Per https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
800 max_color_attachment_bytes_per_sample: if family_check
801 && device.supports_family(MTLGPUFamily::Apple4)
802 {
803 64
804 } else {
805 32
806 },
807 max_varying_components: if device
808 .supports_feature_set(MTLFeatureSet::macOS_GPUFamily1_v1)
809 {
810 124
811 } else {
812 60
813 },
814 max_threads_per_group: if Self::supports_any(
815 device,
816 &[
817 MTLFeatureSet::iOS_GPUFamily4_v2,
818 MTLFeatureSet::macOS_GPUFamily1_v1,
819 ],
820 ) {
821 1024
822 } else {
823 512
824 },
825 max_total_threadgroup_memory: if Self::supports_any(
826 device,
827 &[
828 MTLFeatureSet::iOS_GPUFamily4_v1,
829 MTLFeatureSet::macOS_GPUFamily1_v2,
830 ],
831 ) {
832 32 << 10
833 } else {
834 16 << 10
835 },
836 sample_count_mask,
837 supports_debug_markers: Self::supports_any(
838 device,
839 &[
840 MTLFeatureSet::macOS_GPUFamily1_v2,
841 MTLFeatureSet::iOS_GPUFamily1_v3,
842 MTLFeatureSet::tvOS_GPUFamily1_v2,
843 ],
844 ),
845 supports_binary_archives: family_check
846 && (device.supports_family(MTLGPUFamily::Apple3)
847 || device.supports_family(MTLGPUFamily::Mac1)),
848 supports_capture_manager: version.at_least((10, 13), (11, 0), os_is_mac),
849 can_set_maximum_drawables_count: version.at_least((10, 14), (11, 2), os_is_mac),
850 can_set_display_sync: version.at_least((10, 13), OS_NOT_SUPPORT, os_is_mac),
851 can_set_next_drawable_timeout: version.at_least((10, 13), (11, 0), os_is_mac),
852 supports_arrays_of_textures: Self::supports_any(
853 device,
854 &[
855 MTLFeatureSet::iOS_GPUFamily3_v2,
856 MTLFeatureSet::tvOS_GPUFamily2_v1,
857 MTLFeatureSet::macOS_GPUFamily1_v3,
858 ],
859 ),
860 supports_arrays_of_textures_write: family_check
861 && (device.supports_family(MTLGPUFamily::Apple6)
862 || device.supports_family(MTLGPUFamily::Mac1)
863 || device.supports_family(MTLGPUFamily::MacCatalyst1)),
864 supports_mutability: version.at_least((10, 13), (11, 0), os_is_mac),
865 //Depth clipping is supported on all macOS GPU families and iOS family 4 and later
866 supports_depth_clip_control: os_is_mac
867 || device.supports_feature_set(MTLFeatureSet::iOS_GPUFamily4_v1),
868 supports_preserve_invariance: version.at_least((11, 0), (13, 0), os_is_mac),
869 // Metal 2.2 on mac, 2.3 on iOS.
870 supports_shader_primitive_index: version.at_least((10, 15), (14, 0), os_is_mac),
871 has_unified_memory: if version.at_least((10, 15), (13, 0), os_is_mac) {
872 Some(device.has_unified_memory())
873 } else {
874 None
875 },
876 timestamp_query_support,
877 supports_simd_scoped_operations: family_check
878 && (device.supports_family(MTLGPUFamily::Metal3)
879 || device.supports_family(MTLGPUFamily::Mac2)
880 || device.supports_family(MTLGPUFamily::Apple7)),
881 // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=5
882 int64: family_check
883 && (device.supports_family(MTLGPUFamily::Apple3)
884 || device.supports_family(MTLGPUFamily::Metal3)),
885 // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=6
886 int64_atomics: family_check
887 && ((device.supports_family(MTLGPUFamily::Apple8)
888 && device.supports_family(MTLGPUFamily::Mac2))
889 || device.supports_family(MTLGPUFamily::Apple9)),
890 // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=6
891 float_atomics: family_check
892 && (device.supports_family(MTLGPUFamily::Apple7)
893 || device.supports_family(MTLGPUFamily::Mac2)),
894 supports_shared_event: version.at_least((10, 14), (12, 0), os_is_mac),
895 }
896 }
897
898 pub fn device_type(&self) -> wgt::DeviceType {
899 if self.has_unified_memory.unwrap_or(self.low_power) {
900 wgt::DeviceType::IntegratedGpu
901 } else {
902 wgt::DeviceType::DiscreteGpu
903 }
904 }
905
906 pub fn features(&self) -> wgt::Features {
907 use wgt::Features as F;
908
909 let mut features = F::empty()
910 | F::MSL_SHADER_PASSTHROUGH
911 | F::MAPPABLE_PRIMARY_BUFFERS
912 | F::VERTEX_WRITABLE_STORAGE
913 | F::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
914 | F::PUSH_CONSTANTS
915 | F::POLYGON_MODE_LINE
916 | F::CLEAR_TEXTURE
917 | F::TEXTURE_FORMAT_16BIT_NORM
918 | F::SHADER_F16
919 | F::DEPTH32FLOAT_STENCIL8
920 | F::BGRA8UNORM_STORAGE;
921
922 features.set(F::FLOAT32_FILTERABLE, self.supports_float_filtering);
923 features.set(
924 F::INDIRECT_FIRST_INSTANCE | F::MULTI_DRAW_INDIRECT,
925 self.indirect_draw_dispatch,
926 );
927 features.set(
928 F::TIMESTAMP_QUERY | F::TIMESTAMP_QUERY_INSIDE_ENCODERS,
929 self.timestamp_query_support
930 .contains(TimestampQuerySupport::STAGE_BOUNDARIES),
931 );
932 features.set(
933 F::TIMESTAMP_QUERY_INSIDE_PASSES,
934 self.timestamp_query_support
935 .contains(TimestampQuerySupport::INSIDE_WGPU_PASSES),
936 );
937 features.set(
938 F::DUAL_SOURCE_BLENDING,
939 self.msl_version >= MTLLanguageVersion::V1_2 && self.dual_source_blending,
940 );
941 features.set(F::TEXTURE_COMPRESSION_ASTC, self.format_astc);
942 features.set(F::TEXTURE_COMPRESSION_ASTC_HDR, self.format_astc_hdr);
943 features.set(F::TEXTURE_COMPRESSION_ASTC_SLICED_3D, self.format_astc_3d);
944 features.set(F::TEXTURE_COMPRESSION_BC, self.format_bc);
945 features.set(F::TEXTURE_COMPRESSION_BC_SLICED_3D, self.format_bc); // BC guarantees Sliced 3D
946 features.set(F::TEXTURE_COMPRESSION_ETC2, self.format_eac_etc);
947
948 features.set(F::DEPTH_CLIP_CONTROL, self.supports_depth_clip_control);
949 features.set(
950 F::SHADER_PRIMITIVE_INDEX,
951 self.supports_shader_primitive_index,
952 );
953
954 features.set(
955 F::TEXTURE_BINDING_ARRAY
956 | F::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING
957 | F::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING
958 | F::PARTIALLY_BOUND_BINDING_ARRAY,
959 self.msl_version >= MTLLanguageVersion::V3_0
960 && self.supports_arrays_of_textures
961 && self.argument_buffers as u64 >= MTLArgumentBuffersTier::Tier2 as u64,
962 );
963 features.set(
964 F::SHADER_INT64,
965 self.int64 && self.msl_version >= MTLLanguageVersion::V2_3,
966 );
967 features.set(
968 F::SHADER_INT64_ATOMIC_MIN_MAX,
969 self.int64_atomics && self.msl_version >= MTLLanguageVersion::V2_4,
970 );
971 features.set(
972 F::TEXTURE_INT64_ATOMIC,
973 self.int64_atomics && self.msl_version >= MTLLanguageVersion::V3_1,
974 );
975 features.set(
976 F::TEXTURE_ATOMIC,
977 self.msl_version >= MTLLanguageVersion::V3_1,
978 );
979 features.set(
980 F::SHADER_FLOAT32_ATOMIC,
981 self.float_atomics && self.msl_version >= MTLLanguageVersion::V3_0,
982 );
983
984 features.set(
985 F::ADDRESS_MODE_CLAMP_TO_BORDER,
986 self.sampler_clamp_to_border,
987 );
988 features.set(F::ADDRESS_MODE_CLAMP_TO_ZERO, true);
989
990 features.set(F::RG11B10UFLOAT_RENDERABLE, self.format_rg11b10_all);
991
992 if self.supports_simd_scoped_operations {
993 features.insert(F::SUBGROUP | F::SUBGROUP_BARRIER);
994 }
995
996 features
997 }
998
999 pub fn capabilities(&self) -> crate::Capabilities {
1000 let mut downlevel = wgt::DownlevelCapabilities::default();
1001 downlevel.flags.set(
1002 wgt::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE,
1003 self.fragment_rw_storage,
1004 );
1005 downlevel.flags.set(
1006 wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES,
1007 self.texture_cube_array,
1008 );
1009 // TODO: separate the mutable comparisons from immutable ones
1010 downlevel.flags.set(
1011 wgt::DownlevelFlags::COMPARISON_SAMPLERS,
1012 self.mutable_comparison_samplers,
1013 );
1014 downlevel.flags.set(
1015 wgt::DownlevelFlags::INDIRECT_EXECUTION,
1016 self.indirect_draw_dispatch,
1017 );
1018 // TODO: add another flag for `first_instance`
1019 downlevel.flags.set(
1020 wgt::DownlevelFlags::BASE_VERTEX,
1021 self.base_vertex_first_instance_drawing,
1022 );
1023 downlevel
1024 .flags
1025 .set(wgt::DownlevelFlags::ANISOTROPIC_FILTERING, true);
1026
1027 let base = wgt::Limits::default();
1028 crate::Capabilities {
1029 limits: wgt::Limits {
1030 max_texture_dimension_1d: self.max_texture_size as u32,
1031 max_texture_dimension_2d: self.max_texture_size as u32,
1032 max_texture_dimension_3d: self.max_texture_3d_size as u32,
1033 max_texture_array_layers: self.max_texture_layers as u32,
1034 max_bind_groups: 8,
1035 max_bindings_per_bind_group: 65535,
1036 max_dynamic_uniform_buffers_per_pipeline_layout: base
1037 .max_dynamic_uniform_buffers_per_pipeline_layout,
1038 max_dynamic_storage_buffers_per_pipeline_layout: base
1039 .max_dynamic_storage_buffers_per_pipeline_layout,
1040 max_sampled_textures_per_shader_stage: self.max_textures_per_stage,
1041 max_samplers_per_shader_stage: self.max_samplers_per_stage,
1042 max_storage_buffers_per_shader_stage: self.max_buffers_per_stage,
1043 max_storage_textures_per_shader_stage: self.max_textures_per_stage,
1044 max_uniform_buffers_per_shader_stage: self.max_buffers_per_stage,
1045 max_binding_array_elements_per_shader_stage: self.max_binding_array_elements,
1046 max_binding_array_sampler_elements_per_shader_stage: self
1047 .max_sampler_binding_array_elements,
1048 max_uniform_buffer_binding_size: self.max_buffer_size.min(!0u32 as u64) as u32,
1049 max_storage_buffer_binding_size: self.max_buffer_size.min(!0u32 as u64) as u32,
1050 max_vertex_buffers: self.max_vertex_buffers,
1051 max_vertex_attributes: 31,
1052 max_vertex_buffer_array_stride: base.max_vertex_buffer_array_stride,
1053 min_subgroup_size: 4,
1054 max_subgroup_size: 64,
1055 max_push_constant_size: 0x1000,
1056 min_uniform_buffer_offset_alignment: self.buffer_alignment as u32,
1057 min_storage_buffer_offset_alignment: self.buffer_alignment as u32,
1058 max_inter_stage_shader_components: self.max_varying_components,
1059 max_color_attachments: (self.max_color_render_targets as u32)
1060 .min(crate::MAX_COLOR_ATTACHMENTS as u32),
1061 max_color_attachment_bytes_per_sample: self.max_color_attachment_bytes_per_sample
1062 as u32,
1063 max_compute_workgroup_storage_size: self.max_total_threadgroup_memory,
1064 max_compute_invocations_per_workgroup: self.max_threads_per_group,
1065 max_compute_workgroup_size_x: self.max_threads_per_group,
1066 max_compute_workgroup_size_y: self.max_threads_per_group,
1067 max_compute_workgroup_size_z: self.max_threads_per_group,
1068 max_compute_workgroups_per_dimension: 0xFFFF,
1069 max_buffer_size: self.max_buffer_size,
1070 max_non_sampler_bindings: u32::MAX,
1071 },
1072 alignments: crate::Alignments {
1073 buffer_copy_offset: wgt::BufferSize::new(self.buffer_alignment).unwrap(),
1074 buffer_copy_pitch: wgt::BufferSize::new(4).unwrap(),
1075 // This backend has Naga incorporate bounds checks into the
1076 // Metal Shading Language it generates, so from `wgpu_hal`'s
1077 // users' point of view, references are tightly checked.
1078 uniform_bounds_check_alignment: wgt::BufferSize::new(1).unwrap(),
1079 raw_tlas_instance_size: 0,
1080 ray_tracing_scratch_buffer_alignment: 0,
1081 },
1082 downlevel,
1083 }
1084 }
1085
1086 pub fn map_format(&self, format: wgt::TextureFormat) -> MTLPixelFormat {
1087 use wgt::TextureFormat as Tf;
1088 use MTLPixelFormat as MTL;
1089 match format {
1090 Tf::R8Unorm => MTL::R8Unorm,
1091 Tf::R8Snorm => MTL::R8Snorm,
1092 Tf::R8Uint => MTL::R8Uint,
1093 Tf::R8Sint => MTL::R8Sint,
1094 Tf::R16Uint => MTL::R16Uint,
1095 Tf::R16Sint => MTL::R16Sint,
1096 Tf::R16Unorm => MTL::R16Unorm,
1097 Tf::R16Snorm => MTL::R16Snorm,
1098 Tf::R16Float => MTL::R16Float,
1099 Tf::Rg8Unorm => MTL::RG8Unorm,
1100 Tf::Rg8Snorm => MTL::RG8Snorm,
1101 Tf::Rg8Uint => MTL::RG8Uint,
1102 Tf::Rg8Sint => MTL::RG8Sint,
1103 Tf::Rg16Unorm => MTL::RG16Unorm,
1104 Tf::Rg16Snorm => MTL::RG16Snorm,
1105 Tf::R32Uint => MTL::R32Uint,
1106 Tf::R32Sint => MTL::R32Sint,
1107 Tf::R32Float => MTL::R32Float,
1108 Tf::Rg16Uint => MTL::RG16Uint,
1109 Tf::Rg16Sint => MTL::RG16Sint,
1110 Tf::Rg16Float => MTL::RG16Float,
1111 Tf::Rgba8Unorm => MTL::RGBA8Unorm,
1112 Tf::Rgba8UnormSrgb => MTL::RGBA8Unorm_sRGB,
1113 Tf::Bgra8UnormSrgb => MTL::BGRA8Unorm_sRGB,
1114 Tf::Rgba8Snorm => MTL::RGBA8Snorm,
1115 Tf::Bgra8Unorm => MTL::BGRA8Unorm,
1116 Tf::Rgba8Uint => MTL::RGBA8Uint,
1117 Tf::Rgba8Sint => MTL::RGBA8Sint,
1118 Tf::Rgb10a2Uint => MTL::RGB10A2Uint,
1119 Tf::Rgb10a2Unorm => MTL::RGB10A2Unorm,
1120 Tf::Rg11b10Ufloat => MTL::RG11B10Float,
1121 // Ruint64 textures are emulated on metal
1122 Tf::R64Uint => MTL::RG32Uint,
1123 Tf::Rg32Uint => MTL::RG32Uint,
1124 Tf::Rg32Sint => MTL::RG32Sint,
1125 Tf::Rg32Float => MTL::RG32Float,
1126 Tf::Rgba16Uint => MTL::RGBA16Uint,
1127 Tf::Rgba16Sint => MTL::RGBA16Sint,
1128 Tf::Rgba16Unorm => MTL::RGBA16Unorm,
1129 Tf::Rgba16Snorm => MTL::RGBA16Snorm,
1130 Tf::Rgba16Float => MTL::RGBA16Float,
1131 Tf::Rgba32Uint => MTL::RGBA32Uint,
1132 Tf::Rgba32Sint => MTL::RGBA32Sint,
1133 Tf::Rgba32Float => MTL::RGBA32Float,
1134 Tf::Stencil8 => MTL::Stencil8,
1135 Tf::Depth16Unorm => MTL::Depth16Unorm,
1136 Tf::Depth32Float => MTL::Depth32Float,
1137 Tf::Depth32FloatStencil8 => MTL::Depth32Float_Stencil8,
1138 Tf::Depth24Plus => {
1139 if self.format_depth24_stencil8 {
1140 MTL::Depth24Unorm_Stencil8
1141 } else {
1142 MTL::Depth32Float
1143 }
1144 }
1145 Tf::Depth24PlusStencil8 => {
1146 if self.format_depth24_stencil8 {
1147 MTL::Depth24Unorm_Stencil8
1148 } else {
1149 MTL::Depth32Float_Stencil8
1150 }
1151 }
1152 Tf::NV12 => unreachable!(),
1153 Tf::Rgb9e5Ufloat => MTL::RGB9E5Float,
1154 Tf::Bc1RgbaUnorm => MTL::BC1_RGBA,
1155 Tf::Bc1RgbaUnormSrgb => MTL::BC1_RGBA_sRGB,
1156 Tf::Bc2RgbaUnorm => MTL::BC2_RGBA,
1157 Tf::Bc2RgbaUnormSrgb => MTL::BC2_RGBA_sRGB,
1158 Tf::Bc3RgbaUnorm => MTL::BC3_RGBA,
1159 Tf::Bc3RgbaUnormSrgb => MTL::BC3_RGBA_sRGB,
1160 Tf::Bc4RUnorm => MTL::BC4_RUnorm,
1161 Tf::Bc4RSnorm => MTL::BC4_RSnorm,
1162 Tf::Bc5RgUnorm => MTL::BC5_RGUnorm,
1163 Tf::Bc5RgSnorm => MTL::BC5_RGSnorm,
1164 Tf::Bc6hRgbFloat => MTL::BC6H_RGBFloat,
1165 Tf::Bc6hRgbUfloat => MTL::BC6H_RGBUfloat,
1166 Tf::Bc7RgbaUnorm => MTL::BC7_RGBAUnorm,
1167 Tf::Bc7RgbaUnormSrgb => MTL::BC7_RGBAUnorm_sRGB,
1168 Tf::Etc2Rgb8Unorm => MTL::ETC2_RGB8,
1169 Tf::Etc2Rgb8UnormSrgb => MTL::ETC2_RGB8_sRGB,
1170 Tf::Etc2Rgb8A1Unorm => MTL::ETC2_RGB8A1,
1171 Tf::Etc2Rgb8A1UnormSrgb => MTL::ETC2_RGB8A1_sRGB,
1172 Tf::Etc2Rgba8Unorm => MTL::EAC_RGBA8,
1173 Tf::Etc2Rgba8UnormSrgb => MTL::EAC_RGBA8_sRGB,
1174 Tf::EacR11Unorm => MTL::EAC_R11Unorm,
1175 Tf::EacR11Snorm => MTL::EAC_R11Snorm,
1176 Tf::EacRg11Unorm => MTL::EAC_RG11Unorm,
1177 Tf::EacRg11Snorm => MTL::EAC_RG11Snorm,
1178 Tf::Astc { block, channel } => match channel {
1179 AstcChannel::Unorm => match block {
1180 AstcBlock::B4x4 => MTL::ASTC_4x4_LDR,
1181 AstcBlock::B5x4 => MTL::ASTC_5x4_LDR,
1182 AstcBlock::B5x5 => MTL::ASTC_5x5_LDR,
1183 AstcBlock::B6x5 => MTL::ASTC_6x5_LDR,
1184 AstcBlock::B6x6 => MTL::ASTC_6x6_LDR,
1185 AstcBlock::B8x5 => MTL::ASTC_8x5_LDR,
1186 AstcBlock::B8x6 => MTL::ASTC_8x6_LDR,
1187 AstcBlock::B8x8 => MTL::ASTC_8x8_LDR,
1188 AstcBlock::B10x5 => MTL::ASTC_10x5_LDR,
1189 AstcBlock::B10x6 => MTL::ASTC_10x6_LDR,
1190 AstcBlock::B10x8 => MTL::ASTC_10x8_LDR,
1191 AstcBlock::B10x10 => MTL::ASTC_10x10_LDR,
1192 AstcBlock::B12x10 => MTL::ASTC_12x10_LDR,
1193 AstcBlock::B12x12 => MTL::ASTC_12x12_LDR,
1194 },
1195 AstcChannel::UnormSrgb => match block {
1196 AstcBlock::B4x4 => MTL::ASTC_4x4_sRGB,
1197 AstcBlock::B5x4 => MTL::ASTC_5x4_sRGB,
1198 AstcBlock::B5x5 => MTL::ASTC_5x5_sRGB,
1199 AstcBlock::B6x5 => MTL::ASTC_6x5_sRGB,
1200 AstcBlock::B6x6 => MTL::ASTC_6x6_sRGB,
1201 AstcBlock::B8x5 => MTL::ASTC_8x5_sRGB,
1202 AstcBlock::B8x6 => MTL::ASTC_8x6_sRGB,
1203 AstcBlock::B8x8 => MTL::ASTC_8x8_sRGB,
1204 AstcBlock::B10x5 => MTL::ASTC_10x5_sRGB,
1205 AstcBlock::B10x6 => MTL::ASTC_10x6_sRGB,
1206 AstcBlock::B10x8 => MTL::ASTC_10x8_sRGB,
1207 AstcBlock::B10x10 => MTL::ASTC_10x10_sRGB,
1208 AstcBlock::B12x10 => MTL::ASTC_12x10_sRGB,
1209 AstcBlock::B12x12 => MTL::ASTC_12x12_sRGB,
1210 },
1211 AstcChannel::Hdr => match block {
1212 AstcBlock::B4x4 => MTL::ASTC_4x4_HDR,
1213 AstcBlock::B5x4 => MTL::ASTC_5x4_HDR,
1214 AstcBlock::B5x5 => MTL::ASTC_5x5_HDR,
1215 AstcBlock::B6x5 => MTL::ASTC_6x5_HDR,
1216 AstcBlock::B6x6 => MTL::ASTC_6x6_HDR,
1217 AstcBlock::B8x5 => MTL::ASTC_8x5_HDR,
1218 AstcBlock::B8x6 => MTL::ASTC_8x6_HDR,
1219 AstcBlock::B8x8 => MTL::ASTC_8x8_HDR,
1220 AstcBlock::B10x5 => MTL::ASTC_10x5_HDR,
1221 AstcBlock::B10x6 => MTL::ASTC_10x6_HDR,
1222 AstcBlock::B10x8 => MTL::ASTC_10x8_HDR,
1223 AstcBlock::B10x10 => MTL::ASTC_10x10_HDR,
1224 AstcBlock::B12x10 => MTL::ASTC_12x10_HDR,
1225 AstcBlock::B12x12 => MTL::ASTC_12x12_HDR,
1226 },
1227 },
1228 }
1229 }
1230
1231 pub fn map_view_format(
1232 &self,
1233 format: wgt::TextureFormat,
1234 aspects: crate::FormatAspects,
1235 ) -> MTLPixelFormat {
1236 use crate::FormatAspects as Fa;
1237 use wgt::TextureFormat as Tf;
1238 use MTLPixelFormat as MTL;
1239 match (format, aspects) {
1240 // map combined depth-stencil format to their stencil-only format
1241 // see https://developer.apple.com/library/archive/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/WhatsNewiniOS10tvOS10andOSX1012/WhatsNewiniOS10tvOS10andOSX1012.html#//apple_ref/doc/uid/TP40014221-CH14-DontLinkElementID_77
1242 (Tf::Depth24PlusStencil8, Fa::STENCIL) => {
1243 if self.format_depth24_stencil8 {
1244 MTL::X24_Stencil8
1245 } else {
1246 MTL::X32_Stencil8
1247 }
1248 }
1249 (Tf::Depth32FloatStencil8, Fa::STENCIL) => MTL::X32_Stencil8,
1250
1251 _ => self.map_format(format),
1252 }
1253 }
1254 }
1255
1256 impl super::PrivateDisabilities {
1257 pub fn new(device: &metal::Device) -> Self {
1258 let is_intel = device.name().starts_with("Intel");
1259 Self {
1260 broken_viewport_near_depth: is_intel
1261 && !device.supports_feature_set(MTLFeatureSet::macOS_GPUFamily1_v4),
1262 broken_layered_clear_image: is_intel,
1263 }
1264 }
1265 }