Debian Codegraph @

src/gpu/ganesh/mtl/GrMtlCaps.mm

raw
1 /*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/gpu/ganesh/mtl/GrMtlCaps.h"
9
10 #include "include/core/SkRect.h"
11 #include "include/core/SkTextureCompressionType.h"
12 #include "include/gpu/ganesh/GrBackendSurface.h"
13 #include "include/gpu/ganesh/mtl/GrMtlBackendSurface.h"
14 #include "src/core/SkCompressedDataUtils.h"
15 #include "src/core/SkReadBuffer.h"
16 #include "src/gpu/KeyBuilder.h"
17 #include "src/gpu/ganesh/GrBackendUtils.h"
18 #include "src/gpu/ganesh/GrProcessor.h"
19 #include "src/gpu/ganesh/GrProgramDesc.h"
20 #include "src/gpu/ganesh/GrProgramInfo.h"
21 #include "src/gpu/ganesh/GrRenderTarget.h"
22 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
23 #include "src/gpu/ganesh/GrShaderCaps.h"
24 #include "src/gpu/ganesh/GrSurfaceProxy.h"
25 #include "src/gpu/ganesh/mtl/GrMtlRenderTarget.h"
26 #include "src/gpu/ganesh/mtl/GrMtlTexture.h"
27 #include "src/gpu/ganesh/mtl/GrMtlUtil.h"
28 #include "src/gpu/mtl/MtlUtilsPriv.h"
29
30 #if defined(GPU_TEST_UTILS)
31 #include "src/gpu/ganesh/TestFormatColorTypeCombination.h"
32 #endif
33
34 #if !__has_feature(objc_arc)
35 #error This file must be compiled with Arc. Use -fobjc-arc flag
36 #endif
37
38 GR_NORETAIN_BEGIN
39
40 GrMtlCaps::GrMtlCaps(const GrContextOptions& contextOptions, const id<MTLDevice> device)
41 : INHERITED(contextOptions) {
42 fShaderCaps = std::make_unique<GrShaderCaps>();
43
44 this->initGPUFamily(device);
45 this->initGrCaps(device);
46 this->initShaderCaps();
47 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
48 this->applyDriverCorrectnessWorkarounds(contextOptions, device);
49 }
50
51 this->initFormatTable();
52 this->initStencilFormat(device);
53
54 // TODO: appears to be slow with Mac msaa8, disabled for now
55 fStoreAndMultisampleResolveSupport = (fGPUFamily == GPUFamily::kApple &&
56 fFamilyGroup >= 3);
57 // Also slow with non-Apple silicon
58 fPreferDiscardableMSAAAttachment = (fGPUFamily == GPUFamily::kApple);
59
60 this->finishInitialization(contextOptions);
61 }
62
63 // translates from older MTLFeatureSet interface to MTLGPUFamily interface
64 bool GrMtlCaps::getGPUFamilyFromFeatureSet(id<MTLDevice> device,
65 GPUFamily* gpuFamily,
66 int* group) {
67 // MTLFeatureSet is deprecated for newer versions of the SDK
68 #if GR_METAL_SDK_VERSION < 300
69
70 #if defined(SK_BUILD_FOR_MAC)
71 // Apple Silicon is only available in later OSes
72 *gpuFamily = GPUFamily::kMac;
73 // Mac OSX 14
74 if (@available(macOS 10.14, *)) {
75 if ([device supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily2_v1]) {
76 *group = 2;
77 return true;
78 }
79 if ([device supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v4]) {
80 *group = 1;
81 return true;
82 }
83 }
84 // Mac OSX 13
85 if (@available(macOS 10.13, *)) {
86 if ([device supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v3]) {
87 *group = 1;
88 return true;
89 }
90 }
91 // Mac OSX 12
92 if (@available(macOS 10.12, *)) {
93 if ([device supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v2]) {
94 *group = 1;
95 return true;
96 }
97 }
98 // Mac OSX 11
99 if (@available(macOS 10.11, *)) {
100 if ([device supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v1]) {
101 *group = 1;
102 return true;
103 }
104 }
105 #elif defined(SK_BUILD_FOR_IOS)
106 // TODO: support tvOS
107 *gpuFamily = GPUFamily::kApple;
108 // iOS 12
109 if (@available(iOS 12.0, tvOS 12.0, *)) {
110 if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily5_v1]) {
111 *group = 5;
112 return true;
113 }
114 if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily4_v2]) {
115 *group = 4;
116 return true;
117 }
118 if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v4]) {
119 *group = 3;
120 return true;
121 }
122 if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily2_v5]) {
123 *group = 2;
124 return true;
125 }
126 if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v5]) {
127 *group = 1;
128 return true;
129 }
130 }
131 // iOS 11
132 if (@available(iOS 11.0, tvOS 11.0, *)) {
133 if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily4_v1]) {
134 *group = 4;
135 return true;
136 }
137 if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v3]) {
138 *group = 3;
139 return true;
140 }
141 if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily2_v4]) {
142 *group = 2;
143 return true;
144 }
145 if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v4]) {
146 *group = 1;
147 return true;
148 }
149 }
150 // iOS 10
151 if (@available(iOS 10.0, tvOS 10.0, *)) {
152 if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v2]) {
153 *group = 3;
154 return true;
155 }
156 if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily2_v3]) {
157 *group = 2;
158 return true;
159 }
160 if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v3]) {
161 *group = 1;
162 return true;
163 }
164 }
165 // We don't support earlier OSes
166 #endif
167
168 #endif // GR_METAL_SDK_VERSION < 300
169
170 // No supported GPU families were found
171 return false;
172 }
173
174 bool GrMtlCaps::getGPUFamily(id<MTLDevice> device, GPUFamily* gpuFamily, int* group) {
175 #if GR_METAL_SDK_VERSION >= 220
176 if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
177 // Apple Silicon
178 #if GR_METAL_SDK_VERSION >= 230
179 if ([device supportsFamily:MTLGPUFamilyApple7]) {
180 *gpuFamily = GPUFamily::kApple;
181 *group = 7;
182 return true;
183 }
184 #endif
185 #ifdef SK_BUILD_FOR_IOS
186 if ([device supportsFamily:MTLGPUFamilyApple6]) {
187 *gpuFamily = GPUFamily::kApple;
188 *group = 6;
189 return true;
190 }
191 if ([device supportsFamily:MTLGPUFamilyApple5]) {
192 *gpuFamily = GPUFamily::kApple;
193 *group = 5;
194 return true;
195 }
196 if ([device supportsFamily:MTLGPUFamilyApple4]) {
197 *gpuFamily = GPUFamily::kApple;
198 *group = 4;
199 return true;
200 }
201 if ([device supportsFamily:MTLGPUFamilyApple3]) {
202 *gpuFamily = GPUFamily::kApple;
203 *group = 3;
204 return true;
205 }
206 if ([device supportsFamily:MTLGPUFamilyApple2]) {
207 *gpuFamily = GPUFamily::kApple;
208 *group = 2;
209 return true;
210 }
211 if ([device supportsFamily:MTLGPUFamilyApple1]) {
212 *gpuFamily = GPUFamily::kApple;
213 *group = 1;
214 return true;
215 }
216 #endif
217
218 // Older Macs
219 // MTLGPUFamilyMac1, MTLGPUFamilyMacCatalyst1, and MTLGPUFamilyMacCatalyst2 are deprecated.
220 // However, some MTLGPUFamilyMac1 only hardware is still supported.
221 // MacCatalyst families have the same features as Mac, so treat them the same
222 //
223 // Check if an Intel GPU is present; allow targeting issues specific to that hardware.
224 bool isIntel = [device.name containsString:@"Intel"];
225 if ([device supportsFamily:MTLGPUFamilyMac2] ||
226 [device supportsFamily:(MTLGPUFamily)4002/*MTLGPUFamilyMacCatalyst2*/]) {
227 *gpuFamily = isIntel ? GPUFamily::kMacIntel : GPUFamily::kMac;
228 *group = 2;
229 return true;
230 }
231 if ([device supportsFamily:(MTLGPUFamily)2001/*MTLGPUFamilyMac1*/] ||
232 [device supportsFamily:(MTLGPUFamily)4001/*MTLGPUFamilyMacCatalyst1*/]) {
233 *gpuFamily = isIntel ? GPUFamily::kMacIntel : GPUFamily::kMac;
234 *group = 1;
235 return true;
236 }
237 }
238 #endif
239
240 // No supported GPU families were found
241 return false;
242 }
243
244 void GrMtlCaps::initGPUFamily(id<MTLDevice> device) {
245 if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
246 if (this->getGPUFamily(device, &fGPUFamily, &fFamilyGroup)) {
247 return;
248 }
249 } else {
250 if (this->getGPUFamilyFromFeatureSet(device, &fGPUFamily, &fFamilyGroup)) {
251 return;
252 }
253 }
254 // We don't know what this is, fall back to minimum defaults
255 #ifdef SK_BUILD_FOR_MAC
256 fGPUFamily = GPUFamily::kMac;
257 fFamilyGroup = 1;
258 #else
259 fGPUFamily = GPUFamily::kApple;
260 fFamilyGroup = 1;
261 #endif
262 }
263
264 bool GrMtlCaps::canCopyAsBlit(MTLPixelFormat dstFormat, int dstSampleCount,
265 MTLPixelFormat srcFormat, int srcSampleCount,
266 const SkIRect& srcRect, const SkIPoint& dstPoint,
267 bool areDstSrcSameObj) const {
268 if (!dstFormat || dstFormat != srcFormat) {
269 return false;
270 }
271 if ((dstSampleCount > 1 || srcSampleCount > 1) && (dstSampleCount != srcSampleCount)) {
272 return false;
273 }
274 if (areDstSrcSameObj) {
275 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.x(), dstPoint.y(),
276 srcRect.width(), srcRect.height());
277 if (dstRect.intersect(srcRect)) {
278 return false;
279 }
280 }
281 return true;
282 }
283
284 bool GrMtlCaps::canCopyAsResolve(MTLPixelFormat dstFormat, int dstSampleCount,
285 MTLPixelFormat srcFormat, int srcSampleCount,
286 bool srcIsRenderTarget, const SkISize srcDimensions,
287 const SkIRect& srcRect,
288 const SkIPoint& dstPoint,
289 bool areDstSrcSameObj) const {
290 if (areDstSrcSameObj) {
291 return false;
292 }
293 if (dstFormat != srcFormat) {
294 return false;
295 }
296 if (dstSampleCount > 1 || srcSampleCount == 1 || !srcIsRenderTarget) {
297 return false;
298 }
299
300 // TODO: Support copying subrectangles
301 if (dstPoint != SkIPoint::Make(0, 0)) {
302 return false;
303 }
304 if (srcRect != SkIRect::MakeSize(srcDimensions)) {
305 return false;
306 }
307
308 return true;
309 }
310
311 bool GrMtlCaps::onCanCopySurface(const GrSurfaceProxy* dst, const SkIRect& dstRect,
312 const GrSurfaceProxy* src, const SkIRect& srcRect) const {
313 // Metal does not support scaling copies
314 if (srcRect.size() != dstRect.size()) {
315 return false;
316 }
317
318 int dstSampleCnt = 1;
319 int srcSampleCnt = 1;
320 if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
321 dstSampleCnt = rtProxy->numSamples();
322 }
323 if (const GrRenderTargetProxy* rtProxy = src->asRenderTargetProxy()) {
324 srcSampleCnt = rtProxy->numSamples();
325 }
326
327 // TODO: need some way to detect whether the proxy is framebufferOnly
328
329 const SkIPoint dstPoint = dstRect.topLeft();
330 if (this->canCopyAsBlit(GrBackendFormatAsMTLPixelFormat(dst->backendFormat()), dstSampleCnt,
331 GrBackendFormatAsMTLPixelFormat(src->backendFormat()), srcSampleCnt,
332 srcRect, dstPoint, dst == src)) {
333 return true;
334 }
335 bool srcIsRenderTarget = src->asRenderTargetProxy();
336 MTLPixelFormat dstFormat = GrBackendFormatAsMTLPixelFormat(dst->backendFormat());
337 MTLPixelFormat srcFormat = GrBackendFormatAsMTLPixelFormat(src->backendFormat());
338 return this->canCopyAsResolve(dstFormat, dstSampleCnt,
339 srcFormat, srcSampleCnt,
340 srcIsRenderTarget, src->backingStoreDimensions(), srcRect,
341 dstPoint,
342 dst == src);
343 }
344
345 void GrMtlCaps::initGrCaps(id<MTLDevice> device) {
346 #if defined(GPU_TEST_UTILS)
347 this->setDeviceName([[device name] UTF8String]);
348 #endif
349
350 // Max vertex attribs is the same on all devices
351 fMaxVertexAttributes = 31;
352
353 // Metal does not support scissor + clear
354 fPerformPartialClearsAsDraws = true;
355
356 // We always copy in/out of a transfer buffer so it's trivial to support row bytes.
357 fReadPixelsRowBytesSupport = true;
358 fWritePixelsRowBytesSupport = true;
359 fTransferPixelsToRowBytesSupport = true;
360
361 // RenderTarget and Texture size
362 if (this->isMac() || fFamilyGroup >= 3) {
363 fMaxRenderTargetSize = 16384;
364 } else {
365 fMaxRenderTargetSize = 8192;
366 }
367 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
368 fMaxTextureSize = fMaxRenderTargetSize;
369
370 fMaxPushConstantsSize = 4*1024;
371 fTransferBufferRowBytesAlignment = 1;
372
373 // This is documented to be 4 for all Macs. However, on Apple GPUs on Mac it appears there is
374 // no actual alignment requirement
375 // https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400767-copyfrombuffer
376 if (this->isMac()) {
377 fTransferFromBufferToBufferAlignment = 4;
378 // Buffer updates are sometimes implemented through transfers in GrMtlBuffer.
379 fBufferUpdateDataPreserveAlignment = 4;
380 }
381
382 // Metal buffers are initialized to zero (if not created with initial data)
383 fBuffersAreInitiallyZero = true;
384
385 // Init sample counts. All devices support 1 (i.e. 0 in skia).
386 fSampleCounts.push_back(1);
387 if (@available(iOS 9.0, tvOS 9.0, *)) {
388 for (auto sampleCnt : {2, 4, 8}) {
389 if ([device supportsTextureSampleCount:sampleCnt]) {
390 fSampleCounts.push_back(sampleCnt);
391 }
392 }
393 }
394
395 // Clamp to border is supported on Mac 10.12 and higher. It is not supported on iOS.
396 fClampToBorderSupport = false;
397 #ifdef SK_BUILD_FOR_MAC
398 if (@available(macOS 10.12, *)) {
399 fClampToBorderSupport = true;
400 }
401 #endif
402
403 // Starting with the assumption that there isn't a reason to not map small buffers.
404 fBufferMapThreshold = 0;
405
406 // Buffers are always fully mapped.
407 fMapBufferFlags = kCanMap_MapFlag | kAsyncRead_MapFlag;
408
409 fOversizedStencilSupport = true;
410
411 fNPOTTextureTileSupport = true; // always available in Metal
412 fMipmapSupport = true; // always available in Metal
413 fAnisoSupport = true; // always available in Metal
414
415 fReuseScratchTextures = true; // Assuming this okay
416
417 fTransferFromBufferToTextureSupport = true;
418 fTransferFromSurfaceToBufferSupport = true;
419 fTransferFromBufferToBufferSupport = true;
420
421 fTextureBarrierSupport = false; // Need to figure out if we can do this
422
423 fSampleLocationsSupport = false;
424
425 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
426 if (this->isMac() || fFamilyGroup >= 3) {
427 fDrawInstancedSupport = true;
428 fNativeDrawIndirectSupport = true;
429 }
430 }
431
432 fGpuTracingSupport = false;
433
434 bool supportsMTLEvent = false;
435 if (@available(macOS 10.14, iOS 12.0, tvOS 12.0, *)) {
436 supportsMTLEvent = true;
437 }
438 fSemaphoreSupport = supportsMTLEvent;
439 fBackendSemaphoreSupport = fSemaphoreSupport;
440 fFinishedProcAsyncCallbackSupport = true;
441
442 fCrossContextTextureSupport = true;
443 fHalfFloatVertexAttributeSupport = true;
444
445 fDynamicStateArrayGeometryProcessorTextureSupport = true;
446 }
447
448 static bool format_is_srgb(MTLPixelFormat format) {
449 switch (format) {
450 case MTLPixelFormatRGBA8Unorm_sRGB:
451 case MTLPixelFormatBGRA8Unorm_sRGB:
452 return true;
453 default:
454 return false;
455 }
456 }
457
458 bool GrMtlCaps::isFormatSRGB(const GrBackendFormat& format) const {
459 return format_is_srgb(GrBackendFormatAsMTLPixelFormat(format));
460 }
461
462 bool GrMtlCaps::isFormatTexturable(const GrBackendFormat& format, GrTextureType) const {
463 MTLPixelFormat mtlFormat = GrBackendFormatAsMTLPixelFormat(format);
464 return this->isFormatTexturable(mtlFormat);
465 }
466
467 bool GrMtlCaps::isFormatTexturable(MTLPixelFormat format) const {
468 const FormatInfo& formatInfo = this->getFormatInfo(format);
469 return SkToBool(FormatInfo::kTexturable_Flag & formatInfo.fFlags);
470 }
471
472 bool GrMtlCaps::isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
473 int sampleCount) const {
474 if (!this->isFormatRenderable(format, sampleCount)) {
475 return false;
476 }
477 MTLPixelFormat mtlFormat = GrBackendFormatAsMTLPixelFormat(format);
478 SkASSERT(mtlFormat != MTLPixelFormatInvalid);
479 const auto& info = this->getFormatInfo(mtlFormat);
480 if (!SkToBool(info.colorTypeFlags(ct) & ColorTypeInfo::kRenderable_Flag)) {
481 return false;
482 }
483 return true;
484 }
485
486 bool GrMtlCaps::isFormatRenderable(const GrBackendFormat& format, int sampleCount) const {
487 return this->isFormatRenderable(GrBackendFormatAsMTLPixelFormat(format), sampleCount);
488 }
489
490 bool GrMtlCaps::isFormatRenderable(MTLPixelFormat format, int sampleCount) const {
491 return sampleCount <= this->maxRenderTargetSampleCount(format);
492 }
493
494 int GrMtlCaps::maxRenderTargetSampleCount(const GrBackendFormat& format) const {
495 return this->maxRenderTargetSampleCount(GrBackendFormatAsMTLPixelFormat(format));
496 }
497
498 int GrMtlCaps::maxRenderTargetSampleCount(MTLPixelFormat format) const {
499 const FormatInfo& formatInfo = this->getFormatInfo(format);
500 if (formatInfo.fFlags & FormatInfo::kMSAA_Flag) {
501 return fSampleCounts[fSampleCounts.size() - 1];
502 } else if (formatInfo.fFlags & FormatInfo::kRenderable_Flag) {
503 return 1;
504 }
505 return 0;
506 }
507
508 int GrMtlCaps::getRenderTargetSampleCount(int requestedCount,
509 const GrBackendFormat& format) const {
510 MTLPixelFormat mtlFormat = GrBackendFormatAsMTLPixelFormat(format);
511
512 return this->getRenderTargetSampleCount(requestedCount, mtlFormat);
513 }
514
515 int GrMtlCaps::getRenderTargetSampleCount(int requestedCount, MTLPixelFormat format) const {
516 requestedCount = std::max(requestedCount, 1);
517 const FormatInfo& formatInfo = this->getFormatInfo(format);
518 if (!(formatInfo.fFlags & FormatInfo::kRenderable_Flag)) {
519 return 0;
520 }
521 if (formatInfo.fFlags & FormatInfo::kMSAA_Flag) {
522 int count = fSampleCounts.size();
523 for (int i = 0; i < count; ++i) {
524 if (fSampleCounts[i] >= requestedCount) {
525 return fSampleCounts[i];
526 }
527 }
528 }
529 return 1 == requestedCount ? 1 : 0;
530 }
531
532 void GrMtlCaps::initShaderCaps() {
533 GrShaderCaps* shaderCaps = fShaderCaps.get();
534
535 shaderCaps->fFlatInterpolationSupport = true;
536 // We haven't yet tested that using flat attributes perform well.
537 shaderCaps->fPreferFlatInterpolation = true;
538
539 shaderCaps->fShaderDerivativeSupport = true;
540 shaderCaps->fExplicitTextureLodSupport = true;
541
542 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
543 shaderCaps->fDualSourceBlendingSupport = true;
544 } else {
545 shaderCaps->fDualSourceBlendingSupport = false;
546 }
547
548 if (this->isIntel()) {
549 shaderCaps->fVectorClampMinMaxSupport = false;
550 }
551
552 // TODO(skbug.com/40039539): Re-enable this once bug is fixed. Will also need to remove asserts in
553 // GrMtlPipelineStateBuilder which assert we aren't using this feature.
554 #if 0
555 if (this->isIOS()) {
556 shaderCaps->fFBFetchSupport = true;
557 shaderCaps->fFBFetchNeedsCustomOutput = true; // ??
558 shaderCaps->fFBFetchColorName = ""; // Somehow add [[color(0)]] to arguments to frag shader
559 }
560 #endif
561 shaderCaps->fDstReadInShaderSupport = shaderCaps->fFBFetchSupport;
562
563 shaderCaps->fIntegerSupport = true;
564 shaderCaps->fNonsquareMatrixSupport = true;
565 shaderCaps->fInverseHyperbolicSupport = true;
566 shaderCaps->fVertexIDSupport = true;
567 shaderCaps->fInfinitySupport = true;
568 shaderCaps->fNonconstantArrayIndexSupport = true;
569
570 // Metal uses IEEE float and half floats so assuming those values here.
571 shaderCaps->fFloatIs32Bits = true;
572 shaderCaps->fHalfIs32Bits = false;
573
574 shaderCaps->fMaxFragmentSamplers = 16;
575 }
576
577 void GrMtlCaps::applyDriverCorrectnessWorkarounds(const GrContextOptions&, const id<MTLDevice>) {
578 // We don't have any active Metal workarounds.
579 }
580
581 // Define these so we can use them to initialize arrays and work around
582 // the fact that these pixel formats are not always available.
583 #define kMTLPixelFormatB5G6R5Unorm MTLPixelFormat(40)
584 #define kMTLPixelFormatABGR4Unorm MTLPixelFormat(42)
585 #define kMTLPixelFormatETC2_RGB8 MTLPixelFormat(180)
586
587 // These are all the valid MTLPixelFormats that we support in Skia. They are roughly ordered from
588 // most frequently used to least to improve look up times in arrays.
589 static constexpr MTLPixelFormat kMtlFormats[] = {
590 MTLPixelFormatRGBA8Unorm,
591 MTLPixelFormatR8Unorm,
592 MTLPixelFormatA8Unorm,
593 MTLPixelFormatBGRA8Unorm,
594 kMTLPixelFormatB5G6R5Unorm,
595 MTLPixelFormatRGBA16Float,
596 MTLPixelFormatR16Float,
597 MTLPixelFormatRG8Unorm,
598 MTLPixelFormatRGB10A2Unorm,
599 MTLPixelFormatBGR10A2Unorm,
600 kMTLPixelFormatABGR4Unorm,
601 MTLPixelFormatRGBA8Unorm_sRGB,
602 MTLPixelFormatR16Unorm,
603 MTLPixelFormatRG16Unorm,
604 kMTLPixelFormatETC2_RGB8,
605 #ifdef SK_BUILD_FOR_MAC
606 MTLPixelFormatBC1_RGBA,
607 #endif
608 MTLPixelFormatRGBA16Unorm,
609 MTLPixelFormatRG16Float,
610
611 MTLPixelFormatInvalid,
612 };
613
614 void GrMtlCaps::setColorType(GrColorType colorType, std::initializer_list<MTLPixelFormat> formats) {
615 #ifdef SK_DEBUG
616 for (size_t i = 0; i < kNumMtlFormats; ++i) {
617 const auto& formatInfo = fFormatTable[i];
618 for (int j = 0; j < formatInfo.fColorTypeInfoCount; ++j) {
619 const auto& ctInfo = formatInfo.fColorTypeInfos[j];
620 if (ctInfo.fColorType == colorType) {
621 bool found = false;
622 for (auto it = formats.begin(); it != formats.end(); ++it) {
623 if (kMtlFormats[i] == *it) {
624 found = true;
625 }
626 }
627 SkASSERT(found);
628 }
629 }
630 }
631 #endif
632 int idx = static_cast<int>(colorType);
633 for (auto it = formats.begin(); it != formats.end(); ++it) {
634 const auto& info = this->getFormatInfo(*it);
635 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
636 if (info.fColorTypeInfos[i].fColorType == colorType) {
637 fColorTypeToFormatTable[idx] = *it;
638 return;
639 }
640 }
641 }
642 }
643
644 size_t GrMtlCaps::GetFormatIndex(MTLPixelFormat pixelFormat) {
645 static_assert(std::size(kMtlFormats) == GrMtlCaps::kNumMtlFormats,
646 "Size of kMtlFormats array must match static value in header");
647 for (size_t i = 0; i < GrMtlCaps::kNumMtlFormats; ++i) {
648 if (kMtlFormats[i] == pixelFormat) {
649 return i;
650 }
651 }
652 SK_ABORT("Invalid MTLPixelFormat: %d", static_cast<int>(pixelFormat));
653 }
654
655 void GrMtlCaps::initFormatTable() {
656 FormatInfo* info;
657
658 if (@available(macos 11.0, *)) {
659 SkASSERT(kMTLPixelFormatB5G6R5Unorm == MTLPixelFormatB5G6R5Unorm);
660 SkASSERT(kMTLPixelFormatABGR4Unorm == MTLPixelFormatABGR4Unorm);
661 SkASSERT(kMTLPixelFormatETC2_RGB8 == MTLPixelFormatETC2_RGB8);
662 }
663
664 // Format: R8Unorm
665 {
666 info = &fFormatTable[GetFormatIndex(MTLPixelFormatR8Unorm)];
667 info->fFlags = FormatInfo::kAllFlags;
668 info->fColorTypeInfoCount = 3;
669 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
670 int ctIdx = 0;
671 // Format: R8Unorm, Surface: kR_8
672 {
673 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
674 ctInfo.fColorType = GrColorType::kR_8;
675 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
676 }
677 // Format: R8Unorm, Surface: kAlpha_8
678 {
679 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
680 ctInfo.fColorType = GrColorType::kAlpha_8;
681 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
682 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
683 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
684 }
685 // Format: R8Unorm, Surface: kGray_8
686 {
687 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
688 ctInfo.fColorType = GrColorType::kGray_8;
689 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
690 ctInfo.fReadSwizzle = skgpu::Swizzle("rrr1");
691 }
692 }
693
694 // Format: A8Unorm
695 {
696 info = &fFormatTable[GetFormatIndex(MTLPixelFormatA8Unorm)];
697 info->fFlags = FormatInfo::kTexturable_Flag;
698 info->fColorTypeInfoCount = 1;
699 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
700 int ctIdx = 0;
701 // Format: A8Unorm, Surface: kAlpha_8
702 {
703 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
704 ctInfo.fColorType = GrColorType::kAlpha_8;
705 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
706 }
707 }
708
709 if (@available(macOS 11.0, iOS 8.0, tvOS 9.0, *)) {
710 if (this->isApple()) {
711 // Format: B5G6R5Unorm
712 {
713 info = &fFormatTable[GetFormatIndex(MTLPixelFormatB5G6R5Unorm)];
714 info->fFlags = FormatInfo::kAllFlags;
715 info->fColorTypeInfoCount = 1;
716 info->fColorTypeInfos =
717 std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
718 int ctIdx = 0;
719 // Format: B5G6R5Unorm, Surface: kBGR_565
720 {
721 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
722 ctInfo.fColorType = GrColorType::kBGR_565;
723 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag |
724 ColorTypeInfo::kRenderable_Flag;
725 }
726 }
727
728 // Format: ABGR4Unorm
729 {
730 info = &fFormatTable[GetFormatIndex(MTLPixelFormatABGR4Unorm)];
731 info->fFlags = FormatInfo::kAllFlags;
732 info->fColorTypeInfoCount = 1;
733 info->fColorTypeInfos =
734 std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
735 int ctIdx = 0;
736 // Format: ABGR4Unorm, Surface: kABGR_4444
737 {
738 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
739 ctInfo.fColorType = GrColorType::kABGR_4444;
740 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag |
741 ColorTypeInfo::kRenderable_Flag;
742 }
743 }
744 }
745 }
746
747 // Format: RGBA8Unorm
748 {
749 info = &fFormatTable[GetFormatIndex(MTLPixelFormatRGBA8Unorm)];
750 info->fFlags = FormatInfo::kAllFlags;
751 info->fColorTypeInfoCount = 2;
752 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
753 int ctIdx = 0;
754 // Format: RGBA8Unorm, Surface: kRGBA_8888
755 {
756 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
757 ctInfo.fColorType = GrColorType::kRGBA_8888;
758 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
759 }
760 // Format: RGBA8Unorm, Surface: kRGB_888x
761 {
762 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
763 ctInfo.fColorType = GrColorType::kRGB_888x;
764 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
765 ctInfo.fReadSwizzle = skgpu::Swizzle::RGB1();
766 }
767 }
768
769 // Format: RG8Unorm
770 {
771 info = &fFormatTable[GetFormatIndex(MTLPixelFormatRG8Unorm)];
772 info->fFlags = FormatInfo::kAllFlags;
773 info->fColorTypeInfoCount = 1;
774 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
775 int ctIdx = 0;
776 // Format: RG8Unorm, Surface: kRG_88
777 {
778 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
779 ctInfo.fColorType = GrColorType::kRG_88;
780 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
781 }
782 }
783
784 // Format: BGRA8Unorm
785 {
786 info = &fFormatTable[GetFormatIndex(MTLPixelFormatBGRA8Unorm)];
787 info->fFlags = FormatInfo::kAllFlags;
788 info->fColorTypeInfoCount = 1;
789 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
790 int ctIdx = 0;
791 // Format: BGRA8Unorm, Surface: kBGRA_8888
792 {
793 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
794 ctInfo.fColorType = GrColorType::kBGRA_8888;
795 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
796 }
797 }
798
799 // Format: RGBA8Unorm_sRGB
800 {
801 info = &fFormatTable[GetFormatIndex(MTLPixelFormatRGBA8Unorm_sRGB)];
802 info->fFlags = FormatInfo::kAllFlags;
803 info->fColorTypeInfoCount = 1;
804 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
805 int ctIdx = 0;
806 // Format: RGBA8Unorm_sRGB, Surface: kRGBA_8888_SRGB
807 {
808 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
809 ctInfo.fColorType = GrColorType::kRGBA_8888_SRGB;
810 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
811 }
812 }
813
814 // Format: RGB10A2Unorm
815 {
816 info = &fFormatTable[GetFormatIndex(MTLPixelFormatRGB10A2Unorm)];
817 if (this->isMac() || fFamilyGroup >= 3) {
818 info->fFlags = FormatInfo::kAllFlags;
819 } else {
820 info->fFlags = FormatInfo::kTexturable_Flag;
821 }
822 info->fColorTypeInfoCount = 2;
823 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
824 int ctIdx = 0;
825 // Format: RGB10A2Unorm, Surface: kRGBA_1010102
826 {
827 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
828 ctInfo.fColorType = GrColorType::kRGBA_1010102;
829 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
830 }
831 // Format: RGB10A2Unorm, Surface: kRGB_101010x
832 {
833 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
834 ctInfo.fColorType = GrColorType::kRGB_101010x;
835 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
836 ctInfo.fReadSwizzle = skgpu::Swizzle::RGB1();
837 }
838 }
839
840 // Format: BGR10A2Unorm
841 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
842 info = &fFormatTable[GetFormatIndex(MTLPixelFormatBGR10A2Unorm)];
843 if (this->isMac() && fFamilyGroup == 1) {
844 info->fFlags = FormatInfo::kTexturable_Flag;
845 } else {
846 info->fFlags = FormatInfo::kAllFlags;
847 }
848 info->fColorTypeInfoCount = 1;
849 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
850 int ctIdx = 0;
851 // Format: BGR10A2Unorm, Surface: kBGRA_1010102
852 {
853 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
854 ctInfo.fColorType = GrColorType::kBGRA_1010102;
855 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
856 }
857 }
858
859 // Format: R16Float
860 {
861 info = &fFormatTable[GetFormatIndex(MTLPixelFormatR16Float)];
862 info->fFlags = FormatInfo::kAllFlags;
863 info->fColorTypeInfoCount = 1;
864 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
865 int ctIdx = 0;
866 // Format: R16Float, Surface: kAlpha_F16
867 {
868 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
869 ctInfo.fColorType = GrColorType::kAlpha_F16;
870 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
871 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
872 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
873 }
874 }
875
876 // Format: RGBA16Float
877 {
878 info = &fFormatTable[GetFormatIndex(MTLPixelFormatRGBA16Float)];
879 info->fFlags = FormatInfo::kAllFlags;
880 info->fColorTypeInfoCount = 3;
881 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
882 int ctIdx = 0;
883 // Format: RGBA16Float, Surface: kRGBA_F16
884 {
885 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
886 ctInfo.fColorType = GrColorType::kRGBA_F16;
887 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
888 }
889 // Format: RGBA16Float, Surface: kRGBA_F16_Clamped
890 {
891 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
892 ctInfo.fColorType = GrColorType::kRGBA_F16_Clamped;
893 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
894 }
895 // Format: RGBA16Float, Surface: kRGB_F16F16F16x
896 {
897 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
898 ctInfo.fColorType = GrColorType::kRGB_F16F16F16x;
899 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
900 ctInfo.fReadSwizzle = skgpu::Swizzle::RGB1();
901 }
902 }
903
904 // Format: R16Unorm
905 {
906 info = &fFormatTable[GetFormatIndex(MTLPixelFormatR16Unorm)];
907 if (this->isMac()) {
908 info->fFlags = FormatInfo::kAllFlags;
909 } else {
910 info->fFlags = FormatInfo::kTexturable_Flag | FormatInfo::kRenderable_Flag;
911 }
912 info->fColorTypeInfoCount = 2;
913 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
914 int ctIdx = 0;
915 // Format: R16Unorm, Surface: kAlpha_16
916 {
917 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
918 ctInfo.fColorType = GrColorType::kAlpha_16;
919 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
920 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
921 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
922 }
923 // Format: R16Unorm, Surface: kR16_unorm
924 {
925 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
926 ctInfo.fColorType = GrColorType::kR_16;
927 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
928 }
929 }
930
931 // Format: RG16Unorm
932 {
933 info = &fFormatTable[GetFormatIndex(MTLPixelFormatRG16Unorm)];
934 if (this->isMac()) {
935 info->fFlags = FormatInfo::kAllFlags;
936 } else {
937 info->fFlags = FormatInfo::kTexturable_Flag | FormatInfo::kRenderable_Flag;
938 }
939 info->fColorTypeInfoCount = 1;
940 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
941 int ctIdx = 0;
942 // Format: RG16Unorm, Surface: kRG_1616
943 {
944 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
945 ctInfo.fColorType = GrColorType::kRG_1616;
946 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
947 }
948 }
949
950 if (@available(macOS 11.0, iOS 8.0, tvOS 9.0, *)) {
951 if (this->isApple()) {
952 // ETC2_RGB8
953 info = &fFormatTable[GetFormatIndex(MTLPixelFormatETC2_RGB8)];
954 info->fFlags = FormatInfo::kTexturable_Flag;
955 // NO supported colorTypes
956 }
957 }
958 #ifdef SK_BUILD_FOR_MAC
959 if (this->isMac()) {
960 // BC1_RGBA
961 info = &fFormatTable[GetFormatIndex(MTLPixelFormatBC1_RGBA)];
962 info->fFlags = FormatInfo::kTexturable_Flag;
963 // NO supported colorTypes
964 }
965 #endif
966
967 // Format: RGBA16Unorm
968 {
969 info = &fFormatTable[GetFormatIndex(MTLPixelFormatRGBA16Unorm)];
970 if (this->isMac()) {
971 info->fFlags = FormatInfo::kAllFlags;
972 } else {
973 info->fFlags = FormatInfo::kTexturable_Flag | FormatInfo::kRenderable_Flag;
974 }
975 info->fColorTypeInfoCount = 1;
976 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
977 int ctIdx = 0;
978 // Format: RGBA16Unorm, Surface: kRGBA_16161616
979 {
980 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
981 ctInfo.fColorType = GrColorType::kRGBA_16161616;
982 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
983 }
984 }
985
986 // Format: RG16Float
987 {
988 info = &fFormatTable[GetFormatIndex(MTLPixelFormatRG16Float)];
989 info->fFlags = FormatInfo::kAllFlags;
990 info->fColorTypeInfoCount = 1;
991 info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount);
992 int ctIdx = 0;
993 // Format: RG16Float, Surface: kRG_F16
994 {
995 auto& ctInfo = info->fColorTypeInfos[ctIdx++];
996 ctInfo.fColorType = GrColorType::kRG_F16;
997 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
998 }
999 }
1000
1001 ////////////////////////////////////////////////////////////////////////////
1002 // Map GrColorTypes (used for creating GrSurfaces) to MTLPixelFormats. The order in which the
1003 // formats are passed into the setColorType function indicates the priority in selecting which
1004 // format we use for a given GrcolorType.
1005
1006 std::fill_n(fColorTypeToFormatTable, kGrColorTypeCnt, MTLPixelFormatInvalid);
1007
1008 this->setColorType(GrColorType::kAlpha_8, { MTLPixelFormatR8Unorm,
1009 MTLPixelFormatA8Unorm });
1010 if (@available(macOS 11.0, iOS 8.0, tvOS 9.0, *)) {
1011 if (this->isApple()) {
1012 this->setColorType(GrColorType::kBGR_565, { MTLPixelFormatB5G6R5Unorm });
1013 this->setColorType(GrColorType::kABGR_4444, { MTLPixelFormatABGR4Unorm });
1014 }
1015 }
1016 this->setColorType(GrColorType::kRGBA_8888, { MTLPixelFormatRGBA8Unorm });
1017 this->setColorType(GrColorType::kRGBA_8888_SRGB, { MTLPixelFormatRGBA8Unorm_sRGB });
1018 this->setColorType(GrColorType::kRGB_888x, { MTLPixelFormatRGBA8Unorm });
1019 this->setColorType(GrColorType::kR_8, { MTLPixelFormatR8Unorm });
1020 this->setColorType(GrColorType::kRG_88, { MTLPixelFormatRG8Unorm });
1021 this->setColorType(GrColorType::kBGRA_8888, { MTLPixelFormatBGRA8Unorm });
1022 this->setColorType(GrColorType::kRGBA_1010102, { MTLPixelFormatRGB10A2Unorm });
1023 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
1024 this->setColorType(GrColorType::kBGRA_1010102, { MTLPixelFormatBGR10A2Unorm });
1025 }
1026 this->setColorType(GrColorType::kRGB_101010x, { MTLPixelFormatRGB10A2Unorm });
1027 this->setColorType(GrColorType::kGray_8, { MTLPixelFormatR8Unorm });
1028 this->setColorType(GrColorType::kAlpha_F16, { MTLPixelFormatR16Float });
1029 this->setColorType(GrColorType::kRGBA_F16, { MTLPixelFormatRGBA16Float });
1030 this->setColorType(GrColorType::kRGBA_F16_Clamped, { MTLPixelFormatRGBA16Float });
1031 this->setColorType(GrColorType::kRGB_F16F16F16x, { MTLPixelFormatRGBA16Float });
1032 this->setColorType(GrColorType::kAlpha_16, { MTLPixelFormatR16Unorm });
1033 this->setColorType(GrColorType::kRG_1616, { MTLPixelFormatRG16Unorm });
1034 this->setColorType(GrColorType::kRGBA_16161616, { MTLPixelFormatRGBA16Unorm });
1035 this->setColorType(GrColorType::kRG_F16, { MTLPixelFormatRG16Float });
1036 }
1037
1038 void GrMtlCaps::initStencilFormat(id<MTLDevice> physDev) {
1039 fPreferredStencilFormat = MTLPixelFormatStencil8;
1040 }
1041
1042 bool GrMtlCaps::onSurfaceSupportsWritePixels(const GrSurface* surface) const {
1043 if (auto rt = surface->asRenderTarget()) {
1044 return rt->numSamples() <= 1 && SkToBool(surface->asTexture());
1045 }
1046 return true;
1047 }
1048
1049 GrCaps::SurfaceReadPixelsSupport GrMtlCaps::surfaceSupportsReadPixels(
1050 const GrSurface* surface) const {
1051 if (auto tex = static_cast<const GrMtlTexture*>(surface->asTexture())) {
1052 // We disallow reading back directly from compressed textures.
1053 if (skgpu::MtlFormatIsCompressed(tex->attachment()->mtlFormat())) {
1054 return SurfaceReadPixelsSupport::kCopyToTexture2D;
1055 }
1056 }
1057
1058 if (auto mtlRT = static_cast<const GrMtlRenderTarget*>(surface->asRenderTarget())) {
1059 if (mtlRT->numSamples() > 1 && !mtlRT->resolveAttachment()) {
1060 return SurfaceReadPixelsSupport::kCopyToTexture2D;
1061 }
1062 }
1063 return SurfaceReadPixelsSupport::kSupported;
1064 }
1065
1066 GrCaps::DstCopyRestrictions GrMtlCaps::getDstCopyRestrictions(const GrRenderTargetProxy* src,
1067 GrColorType ct) const {
1068 // If the src is a MSAA RT then the only supported copy action (not considering falling back
1069 // to a draw) is to resolve from the MSAA src to the non-MSAA dst. Currently we only support
1070 // resolving the entire texture to a resolve buffer of the same size.
1071 DstCopyRestrictions restrictions = {};
1072 if (auto rtProxy = src->asRenderTargetProxy()) {
1073 if (rtProxy->numSamples() > 1) {
1074 restrictions.fMustCopyWholeSrc = true;
1075 restrictions.fRectsMustMatch = GrSurfaceProxy::RectsMustMatch::kYes;
1076 }
1077 }
1078 return restrictions;
1079 }
1080
1081 bool GrMtlCaps::onAreColorTypeAndFormatCompatible(GrColorType ct,
1082 const GrBackendFormat& format) const {
1083 MTLPixelFormat mtlFormat = GrBackendFormatAsMTLPixelFormat(format);
1084
1085 const auto& info = this->getFormatInfo(mtlFormat);
1086 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1087 if (info.fColorTypeInfos[i].fColorType == ct) {
1088 return true;
1089 }
1090 }
1091 return false;
1092 }
1093
1094 GrBackendFormat GrMtlCaps::onGetDefaultBackendFormat(GrColorType ct) const {
1095 MTLPixelFormat format = this->getFormatFromColorType(ct);
1096 if (!format) {
1097 return {};
1098 }
1099 return GrBackendFormats::MakeMtl(format);
1100 }
1101
1102 GrBackendFormat GrMtlCaps::getBackendFormatFromCompressionType(
1103 SkTextureCompressionType compressionType) const {
1104 switch (compressionType) {
1105 case SkTextureCompressionType::kNone:
1106 return {};
1107 case SkTextureCompressionType::kETC2_RGB8_UNORM:
1108 if (@available(macOS 11.0, *)) {
1109 if (this->isApple()) {
1110 return GrBackendFormats::MakeMtl(MTLPixelFormatETC2_RGB8);
1111 } else {
1112 return {};
1113 }
1114 } else {
1115 return {};
1116 }
1117 case SkTextureCompressionType::kBC1_RGB8_UNORM:
1118 // Metal only supports the RGBA BC1 variant (see following)
1119 return {};
1120 case SkTextureCompressionType::kBC1_RGBA8_UNORM:
1121 #ifdef SK_BUILD_FOR_MAC
1122 if (this->isMac()) {
1123 return GrBackendFormats::MakeMtl(MTLPixelFormatBC1_RGBA);
1124 } else {
1125 return {};
1126 }
1127 #else
1128 return {};
1129 #endif
1130
1131 }
1132 SK_ABORT("Invalid compression type");
1133 }
1134
1135 skgpu::Swizzle GrMtlCaps::onGetReadSwizzle(const GrBackendFormat& format,
1136 GrColorType colorType) const {
1137 MTLPixelFormat mtlFormat = GrBackendFormatAsMTLPixelFormat(format);
1138 SkASSERT(mtlFormat != MTLPixelFormatInvalid);
1139 const auto& info = this->getFormatInfo(mtlFormat);
1140 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1141 const auto& ctInfo = info.fColorTypeInfos[i];
1142 if (ctInfo.fColorType == colorType) {
1143 return ctInfo.fReadSwizzle;
1144 }
1145 }
1146 SkDEBUGFAILF("Illegal color type (%d) and format (%d) combination.", (int)colorType,
1147 static_cast<int>(mtlFormat));
1148 return {};
1149 }
1150
1151 skgpu::Swizzle GrMtlCaps::getWriteSwizzle(const GrBackendFormat& format,
1152 GrColorType colorType) const {
1153 MTLPixelFormat mtlFormat = GrBackendFormatAsMTLPixelFormat(format);
1154 SkASSERT(mtlFormat != MTLPixelFormatInvalid);
1155 const auto& info = this->getFormatInfo(mtlFormat);
1156 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1157 const auto& ctInfo = info.fColorTypeInfos[i];
1158 if (ctInfo.fColorType == colorType) {
1159 return ctInfo.fWriteSwizzle;
1160 }
1161 }
1162 SkDEBUGFAILF("Illegal color type (%d) and format (%d) combination.", (int)colorType,
1163 static_cast<int>(mtlFormat));
1164 return {};
1165 }
1166
1167 uint64_t GrMtlCaps::computeFormatKey(const GrBackendFormat& format) const {
1168 MTLPixelFormat mtlFormat = GrBackendFormatAsMTLPixelFormat(format);
1169 SkASSERT(mtlFormat != MTLPixelFormatInvalid);
1170 // A MTLPixelFormat is an NSUInteger type which is documented to be 32 bits in 32 bit
1171 // applications and 64 bits in 64 bit applications. So it should fit in an uint64_t, but adding
1172 // the assert heere to make sure.
1173 static_assert(sizeof(MTLPixelFormat) <= sizeof(uint64_t));
1174 return (uint64_t)mtlFormat;
1175 }
1176
1177 GrCaps::SupportedWrite GrMtlCaps::supportedWritePixelsColorType(
1178 GrColorType surfaceColorType, const GrBackendFormat& surfaceFormat,
1179 GrColorType srcColorType) const {
1180 // Metal requires the destination offset for copyFromTexture to be a multiple of the textures
1181 // pixels size.
1182 size_t offsetAlignment = GrColorTypeBytesPerPixel(surfaceColorType);
1183
1184 const auto& info = this->getFormatInfo(GrBackendFormatAsMTLPixelFormat(surfaceFormat));
1185 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1186 const auto& ctInfo = info.fColorTypeInfos[i];
1187 if (ctInfo.fColorType == surfaceColorType) {
1188 return {surfaceColorType, offsetAlignment};
1189 }
1190 }
1191 return {GrColorType::kUnknown, 0};
1192 }
1193
1194 GrCaps::SupportedRead GrMtlCaps::onSupportedReadPixelsColorType(
1195 GrColorType srcColorType, const GrBackendFormat& srcBackendFormat,
1196 GrColorType dstColorType) const {
1197 SkTextureCompressionType compression = GrBackendFormatToCompressionType(srcBackendFormat);
1198 if (compression != SkTextureCompressionType::kNone) {
1199 #ifdef SK_BUILD_FOR_IOS
1200 // Reading back to kRGB_888x doesn't work on Metal/iOS (skbug.com/40041169)
1201 return { GrColorType::kUnknown, 0 };
1202 #else
1203 return { SkTextureCompressionTypeIsOpaque(compression) ? GrColorType::kRGB_888x
1204 : GrColorType::kRGBA_8888, 0 };
1205 #endif
1206 }
1207
1208 // Metal requires the destination offset for copyFromTexture to be a multiple of the textures
1209 // pixels size.
1210 size_t offsetAlignment = GrColorTypeBytesPerPixel(srcColorType);
1211 MTLPixelFormat mtlFormat = GrBackendFormatAsMTLPixelFormat(srcBackendFormat);
1212
1213 const auto& info = this->getFormatInfo(mtlFormat);
1214 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1215 const auto& ctInfo = info.fColorTypeInfos[i];
1216 if (ctInfo.fColorType == srcColorType) {
1217 return {srcColorType, offsetAlignment};
1218 }
1219 }
1220 return {GrColorType::kUnknown, 0};
1221 }
1222
1223 /**
1224 * For Metal we want to cache the entire pipeline for reuse of draws. The Desc here holds all
1225 * the information needed to differentiate one pipeline from another.
1226 *
1227 * The GrProgramDesc contains all the information need to create the actual shaders for the
1228 * pipeline.
1229 *
1230 * For Metal we need to add to the GrProgramDesc to include the rest of the state on the
1231 * pipeline. This includes blending information and primitive type. The pipeline is immutable
1232 * so any remaining dynamic state is set via the MtlRenderCmdEncoder.
1233 */
1234 GrProgramDesc GrMtlCaps::makeDesc(GrRenderTarget*, const GrProgramInfo& programInfo,
1235 ProgramDescOverrideFlags overrideFlags) const {
1236 SkASSERT(overrideFlags == ProgramDescOverrideFlags::kNone);
1237 GrProgramDesc desc;
1238 GrProgramDesc::Build(&desc, programInfo, *this);
1239
1240 skgpu::KeyBuilder b(desc.key());
1241
1242 // If ordering here is changed, update getStencilPixelFormat() below
1243 b.add32(GrBackendFormats::AsMtlFormat(programInfo.backendFormat()));
1244
1245 b.add32(programInfo.numSamples());
1246
1247 b.add32(programInfo.needsStencil() ? this->preferredStencilFormat() : MTLPixelFormatInvalid);
1248 b.add32((uint32_t)programInfo.isStencilEnabled());
1249 // Stencil samples don't seem to be tracked in the MTLRenderPipeline
1250
1251 programInfo.pipeline().genKey(&b, *this);
1252
1253 b.add32(programInfo.primitiveTypeKey());
1254
1255 b.flush();
1256 return desc;
1257 }
1258
1259 MTLPixelFormat GrMtlCaps::getStencilPixelFormat(const GrProgramDesc& desc) const {
1260 // Set up read buffer to point to platform-dependent part of the key
1261 SkReadBuffer readBuffer(desc.asKey() + desc.initialKeyLength()/sizeof(uint32_t),
1262 desc.keyLength() - desc.initialKeyLength());
1263 // skip backend format
1264 readBuffer.readUInt();
1265 // skip raster samples
1266 readBuffer.readUInt();
1267
1268 return (MTLPixelFormat) readBuffer.readUInt();
1269 }
1270
1271 bool GrMtlCaps::renderTargetSupportsDiscardableMSAA(const GrMtlRenderTarget* rt) const {
1272 return rt->resolveAttachment() &&
1273 !rt->resolveAttachment()->framebufferOnly() &&
1274 (rt->numSamples() > 1 && this->preferDiscardableMSAAAttachment());
1275 }
1276
1277 #if defined(GPU_TEST_UTILS)
1278 std::vector<GrTest::TestFormatColorTypeCombination> GrMtlCaps::getTestingCombinations() const {
1279 std::vector<GrTest::TestFormatColorTypeCombination> combos = {
1280 { GrColorType::kAlpha_8, GrBackendFormats::MakeMtl(MTLPixelFormatA8Unorm) },
1281 { GrColorType::kAlpha_8, GrBackendFormats::MakeMtl(MTLPixelFormatR8Unorm) },
1282 { GrColorType::kBGR_565, GrBackendFormats::MakeMtl(kMTLPixelFormatB5G6R5Unorm) },
1283 { GrColorType::kABGR_4444, GrBackendFormats::MakeMtl(kMTLPixelFormatABGR4Unorm) },
1284 { GrColorType::kRGBA_8888, GrBackendFormats::MakeMtl(MTLPixelFormatRGBA8Unorm) },
1285 { GrColorType::kRGBA_8888_SRGB, GrBackendFormats::MakeMtl(MTLPixelFormatRGBA8Unorm_sRGB) },
1286 { GrColorType::kRGB_888x, GrBackendFormats::MakeMtl(MTLPixelFormatRGBA8Unorm) },
1287 { GrColorType::kRGB_888x, GrBackendFormats::MakeMtl(kMTLPixelFormatETC2_RGB8) },
1288 #ifdef SK_BUILD_FOR_MAC
1289 { GrColorType::kRGBA_8888, GrBackendFormats::MakeMtl(MTLPixelFormatBC1_RGBA) },
1290 #endif
1291 { GrColorType::kR_8, GrBackendFormats::MakeMtl(MTLPixelFormatR8Unorm) },
1292 { GrColorType::kRG_88, GrBackendFormats::MakeMtl(MTLPixelFormatRG8Unorm) },
1293 { GrColorType::kBGRA_8888, GrBackendFormats::MakeMtl(MTLPixelFormatBGRA8Unorm) },
1294 { GrColorType::kRGBA_1010102, GrBackendFormats::MakeMtl(MTLPixelFormatRGB10A2Unorm) },
1295 { GrColorType::kBGRA_1010102, GrBackendFormats::MakeMtl(MTLPixelFormatBGR10A2Unorm) },
1296 { GrColorType::kRGB_101010x, GrBackendFormats::MakeMtl(MTLPixelFormatRGB10A2Unorm) },
1297 { GrColorType::kGray_8, GrBackendFormats::MakeMtl(MTLPixelFormatR8Unorm) },
1298 { GrColorType::kAlpha_F16, GrBackendFormats::MakeMtl(MTLPixelFormatR16Float) },
1299 { GrColorType::kRGBA_F16, GrBackendFormats::MakeMtl(MTLPixelFormatRGBA16Float) },
1300 { GrColorType::kRGBA_F16_Clamped, GrBackendFormats::MakeMtl(MTLPixelFormatRGBA16Float) },
1301 { GrColorType::kRGB_F16F16F16x, GrBackendFormats::MakeMtl(MTLPixelFormatRGBA16Float) },
1302 { GrColorType::kAlpha_16, GrBackendFormats::MakeMtl(MTLPixelFormatR16Unorm) },
1303 { GrColorType::kRG_1616, GrBackendFormats::MakeMtl(MTLPixelFormatRG16Unorm) },
1304 { GrColorType::kRGBA_16161616, GrBackendFormats::MakeMtl(MTLPixelFormatRGBA16Unorm) },
1305 { GrColorType::kRG_F16, GrBackendFormats::MakeMtl(MTLPixelFormatRG16Float) },
1306 };
1307
1308 return combos;
1309 }
1310 #endif
1311
1312 #ifdef SK_ENABLE_DUMP_GPU
1313 #include "src/utils/SkJSONWriter.h"
1314 void GrMtlCaps::onDumpJSON(SkJSONWriter* writer) const {
1315
1316 // We are called by the base class, which has already called beginObject(). We choose to nest
1317 // all of our caps information in a named sub-object.
1318 writer->beginObject("Metal caps");
1319
1320 writer->beginObject("Preferred Stencil Format");
1321 writer->appendS32("stencil bits", GrMtlFormatStencilBits(fPreferredStencilFormat));
1322 writer->appendS32("total bytes", skgpu::MtlFormatBytesPerBlock(fPreferredStencilFormat));
1323 writer->endObject();
1324
1325 switch (fGPUFamily) {
1326 case GPUFamily::kMac:
1327 writer->appendNString("GPU Family", "Mac");
1328 break;
1329 case GPUFamily::kApple:
1330 writer->appendNString("GPU Family", "Apple");
1331 break;
1332 default:
1333 writer->appendNString("GPU Family", "unknown");
1334 break;
1335 }
1336 writer->appendS32("Family Group", fFamilyGroup);
1337
1338 writer->beginArray("Sample Counts");
1339 for (int v : fSampleCounts) {
1340 writer->appendS32(nullptr, v);
1341 }
1342 writer->endArray();
1343
1344 writer->endObject();
1345 }
1346 #else
1347 void GrMtlCaps::onDumpJSON(SkJSONWriter* writer) const { }
1348 #endif
1349
1350 GR_NORETAIN_END