| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC |
| 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/graphite/mtl/MtlCaps.h" |
| 9 |
|
| 10 |
#include "include/core/SkTextureCompressionType.h" |
| 11 |
#include "include/gpu/graphite/TextureInfo.h" |
| 12 |
#include "include/gpu/graphite/mtl/MtlGraphiteTypes.h" |
| 13 |
#include "src/gpu/SwizzlePriv.h" |
| 14 |
#include "src/gpu/graphite/CommandBuffer.h" |
| 15 |
#include "src/gpu/graphite/ComputePipelineDesc.h" |
| 16 |
#include "src/gpu/graphite/GraphicsPipelineDesc.h" |
| 17 |
#include "src/gpu/graphite/GraphiteResourceKey.h" |
| 18 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 19 |
#include "src/gpu/graphite/RendererProvider.h" |
| 20 |
#include "src/gpu/graphite/TextureInfoPriv.h" |
| 21 |
#include "src/gpu/graphite/TextureProxy.h" |
| 22 |
#include "src/gpu/graphite/mtl/MtlGraphicsPipeline.h" |
| 23 |
#include "src/gpu/graphite/mtl/MtlGraphiteUtils.h" |
| 24 |
#include "src/gpu/mtl/MtlUtilsPriv.h" |
| 25 |
#include "src/sksl/SkSLUtil.h" |
| 26 |
|
| 27 |
namespace skgpu::graphite { |
| 28 |
|
| 29 |
MtlCaps::MtlCaps(const id<MTLDevice> device, const ContextOptions& options) |
| 30 |
: Caps() { |
| 31 |
this->initGPUFamily(device); |
| 32 |
this->initCaps(device); |
| 33 |
this->initShaderCaps(); |
| 34 |
|
| 35 |
this->initFormatTable(device); |
| 36 |
|
| 37 |
// Metal-specific MtlCaps |
| 38 |
|
| 39 |
this->finishInitialization(options); |
| 40 |
} |
| 41 |
|
| 42 |
bool MtlCaps::GetGPUFamily(id<MTLDevice> device, GPUFamily* gpuFamily, int* group) { |
| 43 |
#if SKGPU_GRAPHITE_METAL_SDK_VERSION >= 220 |
| 44 |
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) { |
| 45 |
// Apple Silicon |
| 46 |
#if SKGPU_GRAPHITE_METAL_SDK_VERSION >= 230 |
| 47 |
if ([device supportsFamily:MTLGPUFamilyApple7]) { |
| 48 |
*gpuFamily = GPUFamily::kApple; |
| 49 |
*group = 7; |
| 50 |
return true; |
| 51 |
} |
| 52 |
#endif |
| 53 |
#ifdef SK_BUILD_FOR_IOS |
| 54 |
if ([device supportsFamily:MTLGPUFamilyApple6]) { |
| 55 |
*gpuFamily = GPUFamily::kApple; |
| 56 |
*group = 6; |
| 57 |
return true; |
| 58 |
} |
| 59 |
if ([device supportsFamily:MTLGPUFamilyApple5]) { |
| 60 |
*gpuFamily = GPUFamily::kApple; |
| 61 |
*group = 5; |
| 62 |
return true; |
| 63 |
} |
| 64 |
if ([device supportsFamily:MTLGPUFamilyApple4]) { |
| 65 |
*gpuFamily = GPUFamily::kApple; |
| 66 |
*group = 4; |
| 67 |
return true; |
| 68 |
} |
| 69 |
if ([device supportsFamily:MTLGPUFamilyApple3]) { |
| 70 |
*gpuFamily = GPUFamily::kApple; |
| 71 |
*group = 3; |
| 72 |
return true; |
| 73 |
} |
| 74 |
if ([device supportsFamily:MTLGPUFamilyApple2]) { |
| 75 |
*gpuFamily = GPUFamily::kApple; |
| 76 |
*group = 2; |
| 77 |
return true; |
| 78 |
} |
| 79 |
if ([device supportsFamily:MTLGPUFamilyApple1]) { |
| 80 |
*gpuFamily = GPUFamily::kApple; |
| 81 |
*group = 1; |
| 82 |
return true; |
| 83 |
} |
| 84 |
#endif |
| 85 |
|
| 86 |
// Older Macs |
| 87 |
// MTLGPUFamilyMac1, MTLGPUFamilyMacCatalyst1, and MTLGPUFamilyMacCatalyst2 are deprecated. |
| 88 |
// However, some MTLGPUFamilyMac1 only hardware is still supported. |
| 89 |
// MacCatalyst families have the same features as Mac, so treat them the same |
| 90 |
// |
| 91 |
// Check if an Intel GPU is present; allow targeting issues specific to that hardware. |
| 92 |
bool isIntel = [device.name containsString:@"Intel"]; |
| 93 |
if ([device supportsFamily:MTLGPUFamilyMac2] || |
| 94 |
[device supportsFamily:(MTLGPUFamily)4002/*MTLGPUFamilyMacCatalyst2*/]) { |
| 95 |
*gpuFamily = isIntel ? GPUFamily::kMacIntel : GPUFamily::kMac; |
| 96 |
*group = 2; |
| 97 |
return true; |
| 98 |
} |
| 99 |
if ([device supportsFamily:(MTLGPUFamily)2001/*MTLGPUFamilyMac1*/] || |
| 100 |
[device supportsFamily:(MTLGPUFamily)4001/*MTLGPUFamilyMacCatalyst1*/]) { |
| 101 |
*gpuFamily = isIntel ? GPUFamily::kMacIntel : GPUFamily::kMac; |
| 102 |
*group = 1; |
| 103 |
return true; |
| 104 |
} |
| 105 |
} |
| 106 |
#endif |
| 107 |
|
| 108 |
// No supported GPU families were found |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
void MtlCaps::initGPUFamily(id<MTLDevice> device) { |
| 113 |
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) { |
| 114 |
if (GetGPUFamily(device, &fGPUFamily, &fFamilyGroup)) { |
| 115 |
return; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// We don't know what this is, fall back to minimum defaults |
| 120 |
#ifdef SK_BUILD_FOR_MAC |
| 121 |
fGPUFamily = GPUFamily::kMac; |
| 122 |
fFamilyGroup = 1; |
| 123 |
#else |
| 124 |
fGPUFamily = GPUFamily::kApple; |
| 125 |
fFamilyGroup = 1; |
| 126 |
#endif |
| 127 |
} |
| 128 |
|
| 129 |
void MtlCaps::initCaps(const id<MTLDevice> device) { |
| 130 |
#if defined(GPU_TEST_UTILS) |
| 131 |
this->setDeviceName([[device name] UTF8String]); |
| 132 |
#endif |
| 133 |
|
| 134 |
if (this->isMac() || fFamilyGroup >= 3) { |
| 135 |
fMaxTextureSize = 16384; |
| 136 |
} else { |
| 137 |
fMaxTextureSize = 8192; |
| 138 |
} |
| 139 |
|
| 140 |
// We use constant address space for our uniform buffers which has various alignment |
| 141 |
// requirements for the offset when binding the buffer. On MacOS Intel the offset must align |
| 142 |
// to 256. On iOS or Apple Silicon we must align to the max of the data type consumed by the |
| 143 |
// vertex function or 4 bytes, or we can ignore the data type and just use 16 bytes. |
| 144 |
// |
| 145 |
// On Mac, all copies must be aligned to at least 4 bytes; on iOS there is no alignment. |
| 146 |
if (this->isMac()) { |
| 147 |
fRequiredUniformBufferAlignment = 256; |
| 148 |
fRequiredTransferBufferAlignment = 4; |
| 149 |
} else { |
| 150 |
fRequiredUniformBufferAlignment = 16; |
| 151 |
fRequiredTransferBufferAlignment = 1; |
| 152 |
} |
| 153 |
|
| 154 |
fResourceBindingReqs.fBackendApi = BackendApi::kMetal; |
| 155 |
fResourceBindingReqs.fUniformBufferLayout = Layout::kMetal; |
| 156 |
fResourceBindingReqs.fStorageBufferLayout = Layout::kMetal; |
| 157 |
|
| 158 |
// Graphite/Metal does not group resources into different sets or bind groups at this time, |
| 159 |
// though ResourceBindingRequirements still expects valid assignments of these indices. |
| 160 |
// Assigning both to 0 conveys the usage of one single "set" for all resources. |
| 161 |
fResourceBindingReqs.fUniformsSetIdx = 0; |
| 162 |
fResourceBindingReqs.fTextureSamplerSetIdx = 0; |
| 163 |
|
| 164 |
fResourceBindingReqs.fComputeUsesDistinctIdxRangesForTextures = true; |
| 165 |
|
| 166 |
fResourceBindingReqs.fIntrinsicBufferBinding = |
| 167 |
MtlGraphicsPipeline::kIntrinsicUniformBufferIndex; |
| 168 |
fResourceBindingReqs.fCombinedUniformBufferBinding = MtlGraphicsPipeline::kCombinedUniformIndex; |
| 169 |
fResourceBindingReqs.fGradientBufferBinding = MtlGraphicsPipeline::kGradientBufferIndex; |
| 170 |
|
| 171 |
// Metal does not distinguish between uniform and storage buffers. |
| 172 |
fRequiredStorageBufferAlignment = fRequiredUniformBufferAlignment; |
| 173 |
|
| 174 |
fStorageBufferSupport = true; |
| 175 |
|
| 176 |
fComputeSupport = true; |
| 177 |
|
| 178 |
// See https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf, and what Dawn does at |
| 179 |
// https://crsrc.org/c/third_party/dawn/src/dawn/native/metal/PhysicalDeviceMTL.mm?q=maxInterStageShaderVariables |
| 180 |
if (this->isMac() || fFamilyGroup >= 4) { |
| 181 |
fMaxVaryings = 31; |
| 182 |
} else { |
| 183 |
fMaxVaryings = 15; |
| 184 |
} |
| 185 |
|
| 186 |
if (@available(macOS 10.12, iOS 14.0, tvOS 14.0, *)) { |
| 187 |
fClampToBorderSupport = (this->isMac() || fFamilyGroup >= 7); |
| 188 |
} else { |
| 189 |
fClampToBorderSupport = false; |
| 190 |
} |
| 191 |
|
| 192 |
// Init sample counts. All devices support 1 |
| 193 |
fSupportedSampleCounts = SampleCount::k1; |
| 194 |
if (!this->isIntel()) { |
| 195 |
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) { |
| 196 |
for (auto sampleCnt : {SampleCount::k2, SampleCount::k4, SampleCount::k8}) { |
| 197 |
if ([device supportsTextureSampleCount: (uint8_t) sampleCnt]) { |
| 198 |
fSupportedSampleCounts |= sampleCnt; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} else { |
| 203 |
// All supported Intel Macs are of an old enough Intel generation that we can just assume |
| 204 |
// MSAA should be avoided instead of checking its generation. (>gen 11 FIXME?) |
| 205 |
fAvoidMSAA = true; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void MtlCaps::initShaderCaps() { |
| 210 |
SkSL::ShaderCaps* shaderCaps = fShaderCaps.get(); |
| 211 |
|
| 212 |
// Dual source blending requires Metal 1.2. |
| 213 |
if (@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) { |
| 214 |
shaderCaps->fDualSourceBlendingSupport = true; |
| 215 |
} |
| 216 |
|
| 217 |
shaderCaps->fFlatInterpolationSupport = true; |
| 218 |
shaderCaps->fShaderDerivativeSupport = true; |
| 219 |
shaderCaps->fInfinitySupport = true; |
| 220 |
|
| 221 |
if (@available(macOS 11.0, *)) { |
| 222 |
if (this->isApple()) { |
| 223 |
shaderCaps->fFBFetchSupport = true; |
| 224 |
shaderCaps->fFBFetchColorName = "sk_LastFragColor"; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (this->isIntel()) { |
| 229 |
shaderCaps->fVectorClampMinMaxSupport = false; |
| 230 |
} |
| 231 |
|
| 232 |
shaderCaps->fIntegerSupport = true; |
| 233 |
shaderCaps->fNonsquareMatrixSupport = true; |
| 234 |
shaderCaps->fInverseHyperbolicSupport = true; |
| 235 |
|
| 236 |
// Metal uses IEEE floats so assuming those values here. |
| 237 |
shaderCaps->fFloatIs32Bits = true; |
| 238 |
} |
| 239 |
|
| 240 |
// Define this so we can use it to initialize arrays and work around |
| 241 |
// the fact that these pixel formats are not always available. |
| 242 |
#define kMTLPixelFormatB5G6R5Unorm MTLPixelFormat(40) |
| 243 |
#define kMTLPixelFormatABGR4Unorm MTLPixelFormat(42) |
| 244 |
#define kMTLPixelFormatETC2_RGB8 MTLPixelFormat(180) |
| 245 |
|
| 246 |
// These are all the valid MTLPixelFormats that we currently support in Skia. They are roughly |
| 247 |
// ordered from most frequently used to least to improve look up times in arrays. |
| 248 |
static constexpr MTLPixelFormat kMtlFormats[] = { |
| 249 |
MTLPixelFormatRGBA8Unorm, |
| 250 |
MTLPixelFormatR8Unorm, |
| 251 |
MTLPixelFormatA8Unorm, |
| 252 |
MTLPixelFormatBGRA8Unorm, |
| 253 |
kMTLPixelFormatB5G6R5Unorm, |
| 254 |
MTLPixelFormatRGBA16Float, |
| 255 |
MTLPixelFormatR16Float, |
| 256 |
MTLPixelFormatRG8Unorm, |
| 257 |
MTLPixelFormatRGB10A2Unorm, |
| 258 |
// MTLPixelFormatBGR10A2Unorm |
| 259 |
kMTLPixelFormatABGR4Unorm, |
| 260 |
MTLPixelFormatRGBA8Unorm_sRGB, |
| 261 |
MTLPixelFormatR16Unorm, |
| 262 |
MTLPixelFormatRG16Unorm, |
| 263 |
kMTLPixelFormatETC2_RGB8, |
| 264 |
#ifdef SK_BUILD_FOR_MAC |
| 265 |
MTLPixelFormatBC1_RGBA, |
| 266 |
#endif |
| 267 |
MTLPixelFormatRGBA16Unorm, |
| 268 |
MTLPixelFormatRG16Float, |
| 269 |
|
| 270 |
MTLPixelFormatStencil8, |
| 271 |
MTLPixelFormatDepth16Unorm, |
| 272 |
MTLPixelFormatDepth32Float, |
| 273 |
#ifdef SK_BUILD_FOR_MAC |
| 274 |
MTLPixelFormatDepth24Unorm_Stencil8, |
| 275 |
#endif |
| 276 |
MTLPixelFormatDepth32Float_Stencil8, |
| 277 |
|
| 278 |
MTLPixelFormatInvalid, |
| 279 |
}; |
| 280 |
|
| 281 |
void MtlCaps::setColorType(SkColorType colorType, std::initializer_list<MTLPixelFormat> formats) { |
| 282 |
int idx = static_cast<int>(colorType); |
| 283 |
for (auto it = formats.begin(); it != formats.end(); ++it) { |
| 284 |
const auto& info = this->getFormatInfo(*it); |
| 285 |
for (int i = 0; i < info.fColorTypeInfoCount; ++i) { |
| 286 |
if (info.fColorTypeInfos[i].fColorType == colorType) { |
| 287 |
fColorTypeToFormatTable[idx] = *it; |
| 288 |
return; |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
size_t MtlCaps::GetFormatIndex(MTLPixelFormat pixelFormat) { |
| 295 |
static_assert(std::size(kMtlFormats) == MtlCaps::kNumMtlFormats, |
| 296 |
"Size of kMtlFormats array must match static value in header"); |
| 297 |
for (size_t i = 0; i < MtlCaps::kNumMtlFormats; ++i) { |
| 298 |
if (kMtlFormats[i] == pixelFormat) { |
| 299 |
return i; |
| 300 |
} |
| 301 |
} |
| 302 |
return GetFormatIndex(MTLPixelFormatInvalid); |
| 303 |
} |
| 304 |
|
| 305 |
void MtlCaps::initFormatTable(const id<MTLDevice> device) { |
| 306 |
FormatInfo* info; |
| 307 |
|
| 308 |
if (@available(macOS 11.0, iOS 8.0, tvOS 9.0, *)) { |
| 309 |
if (this->isApple()) { |
| 310 |
SkASSERT(kMTLPixelFormatB5G6R5Unorm == MTLPixelFormatB5G6R5Unorm); |
| 311 |
SkASSERT(kMTLPixelFormatABGR4Unorm == MTLPixelFormatABGR4Unorm); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
// NOTE: MTLPixelFormat's naming convention orders channels from least significant to most, |
| 316 |
// matching the data address ordering of a little endian system. |
| 317 |
|
| 318 |
// Format: RGBA8Unorm |
| 319 |
{ |
| 320 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatRGBA8Unorm)]; |
| 321 |
info->fFlags = FormatInfo::kAllFlags; |
| 322 |
info->fColorTypeInfoCount = 2; |
| 323 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 324 |
int ctIdx = 0; |
| 325 |
// Format: RGBA8Unorm, Surface: kRGBA_8888 |
| 326 |
{ |
| 327 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 328 |
ctInfo.fColorType = kRGBA_8888_SkColorType; |
| 329 |
ctInfo.fTransferColorType = kRGBA_8888_SkColorType; |
| 330 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 331 |
} |
| 332 |
// Format: RGBA8Unorm, Surface: kRGB_888x |
| 333 |
{ |
| 334 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 335 |
ctInfo.fColorType = kRGB_888x_SkColorType; |
| 336 |
ctInfo.fTransferColorType = kRGB_888x_SkColorType; |
| 337 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag; |
| 338 |
ctInfo.fReadSwizzle = skgpu::Swizzle::RGB1(); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
// Format: R8Unorm |
| 343 |
{ |
| 344 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatR8Unorm)]; |
| 345 |
info->fFlags = FormatInfo::kAllFlags; |
| 346 |
info->fColorTypeInfoCount = 3; |
| 347 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 348 |
int ctIdx = 0; |
| 349 |
// Format: R8Unorm, Surface: kAlpha_8 |
| 350 |
{ |
| 351 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 352 |
ctInfo.fColorType = kAlpha_8_SkColorType; |
| 353 |
ctInfo.fTransferColorType = kAlpha_8_SkColorType; |
| 354 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 355 |
ctInfo.fReadSwizzle = skgpu::Swizzle("000r"); |
| 356 |
ctInfo.fWriteSwizzle = skgpu::Swizzle("a000"); |
| 357 |
} |
| 358 |
// Format: R8Unorm, Surface: kR8_unorm |
| 359 |
{ |
| 360 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 361 |
ctInfo.fColorType = kR8_unorm_SkColorType; |
| 362 |
ctInfo.fTransferColorType = kR8_unorm_SkColorType; |
| 363 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 364 |
} |
| 365 |
// Format: R8Unorm, Surface: kGray_8 |
| 366 |
{ |
| 367 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 368 |
ctInfo.fColorType = kGray_8_SkColorType; |
| 369 |
ctInfo.fTransferColorType = kGray_8_SkColorType; |
| 370 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag; |
| 371 |
ctInfo.fReadSwizzle = skgpu::Swizzle("rrr1"); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
// Format: A8Unorm |
| 376 |
{ |
| 377 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatA8Unorm)]; |
| 378 |
info->fFlags = FormatInfo::kTexturable_Flag; |
| 379 |
info->fColorTypeInfoCount = 1; |
| 380 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 381 |
int ctIdx = 0; |
| 382 |
// Format: A8Unorm, Surface: kAlpha_8 |
| 383 |
{ |
| 384 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 385 |
ctInfo.fColorType = kAlpha_8_SkColorType; |
| 386 |
ctInfo.fTransferColorType = kAlpha_8_SkColorType; |
| 387 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
// Format: BGRA8Unorm |
| 392 |
{ |
| 393 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatBGRA8Unorm)]; |
| 394 |
info->fFlags = FormatInfo::kAllFlags; |
| 395 |
info->fColorTypeInfoCount = 1; |
| 396 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 397 |
int ctIdx = 0; |
| 398 |
// Format: BGRA8Unorm, Surface: kBGRA_8888 |
| 399 |
{ |
| 400 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 401 |
ctInfo.fColorType = kBGRA_8888_SkColorType; |
| 402 |
ctInfo.fTransferColorType = kBGRA_8888_SkColorType; |
| 403 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
if (@available(macOS 11.0, iOS 8.0, tvOS 9.0, *)) { |
| 408 |
if (this->isApple()) { |
| 409 |
// Format: B5G6R5Unorm |
| 410 |
{ |
| 411 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatB5G6R5Unorm)]; |
| 412 |
info->fFlags = FormatInfo::kAllFlags; |
| 413 |
info->fColorTypeInfoCount = 1; |
| 414 |
info->fColorTypeInfos = |
| 415 |
std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 416 |
int ctIdx = 0; |
| 417 |
// Format: B5G6R5Unorm, Surface: kRGB_565; misnamed SkColorType is really BGR data |
| 418 |
{ |
| 419 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 420 |
ctInfo.fColorType = kRGB_565_SkColorType; |
| 421 |
ctInfo.fTransferColorType = kRGB_565_SkColorType; |
| 422 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | |
| 423 |
ColorTypeInfo::kRenderable_Flag; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
// Format: ABGR4Unorm |
| 428 |
{ |
| 429 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatABGR4Unorm)]; |
| 430 |
info->fFlags = FormatInfo::kAllFlags; |
| 431 |
info->fColorTypeInfoCount = 1; |
| 432 |
info->fColorTypeInfos = |
| 433 |
std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 434 |
int ctIdx = 0; |
| 435 |
// Format: ABGR4Unorm, Surface: kARGB_4444; misnamed SkColorType is really ABGR data |
| 436 |
{ |
| 437 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 438 |
ctInfo.fColorType = kARGB_4444_SkColorType; |
| 439 |
ctInfo.fTransferColorType = kARGB_4444_SkColorType; |
| 440 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | |
| 441 |
ColorTypeInfo::kRenderable_Flag; |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
// Format: RGBA8Unorm_sRGB |
| 448 |
{ |
| 449 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatRGBA8Unorm_sRGB)]; |
| 450 |
info->fFlags = FormatInfo::kAllFlags; |
| 451 |
info->fColorTypeInfoCount = 1; |
| 452 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 453 |
int ctIdx = 0; |
| 454 |
// Format: RGBA8Unorm_sRGB, Surface: kSRGBA_8888 |
| 455 |
{ |
| 456 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 457 |
ctInfo.fColorType = kSRGBA_8888_SkColorType; |
| 458 |
ctInfo.fTransferColorType = kSRGBA_8888_SkColorType; |
| 459 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
// Format: RGB10A2Unorm |
| 464 |
{ |
| 465 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatRGB10A2Unorm)]; |
| 466 |
if (this->isMac() || fFamilyGroup >= 3) { |
| 467 |
info->fFlags = FormatInfo::kAllFlags; |
| 468 |
} else { |
| 469 |
info->fFlags = FormatInfo::kTexturable_Flag; |
| 470 |
} |
| 471 |
info->fColorTypeInfoCount = 2; |
| 472 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 473 |
int ctIdx = 0; |
| 474 |
// Format: RGB10A2Unorm, Surface: kRGBA_1010102 |
| 475 |
{ |
| 476 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 477 |
ctInfo.fColorType = kRGBA_1010102_SkColorType; |
| 478 |
ctInfo.fTransferColorType = kRGBA_1010102_SkColorType; |
| 479 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 480 |
} |
| 481 |
// Format: RGB10A2Unorm, Surface: kRGB_101010x |
| 482 |
{ |
| 483 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 484 |
ctInfo.fColorType = kRGB_101010x_SkColorType; |
| 485 |
ctInfo.fTransferColorType = kRGB_101010x_SkColorType; |
| 486 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag; |
| 487 |
ctInfo.fReadSwizzle = skgpu::Swizzle::RGB1(); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
// Format: RGBA16Float |
| 492 |
{ |
| 493 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatRGBA16Float)]; |
| 494 |
info->fFlags = FormatInfo::kAllFlags; |
| 495 |
info->fColorTypeInfoCount = 3; |
| 496 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 497 |
int ctIdx = 0; |
| 498 |
// Format: RGBA16Float, Surface: RGBA_F16 |
| 499 |
{ |
| 500 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 501 |
ctInfo.fColorType = kRGBA_F16_SkColorType; |
| 502 |
ctInfo.fTransferColorType = kRGBA_F16_SkColorType; |
| 503 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 504 |
} |
| 505 |
// Format: RGBA16Float, Surface: RGBA_F16Norm |
| 506 |
{ |
| 507 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 508 |
ctInfo.fColorType = kRGBA_F16Norm_SkColorType; |
| 509 |
ctInfo.fTransferColorType = kRGBA_F16Norm_SkColorType; |
| 510 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 511 |
} |
| 512 |
// Format: RGBA16Float, Surface: RGB_F16F16F16x |
| 513 |
{ |
| 514 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 515 |
ctInfo.fColorType = kRGB_F16F16F16x_SkColorType; |
| 516 |
ctInfo.fTransferColorType = kRGB_F16F16F16x_SkColorType; |
| 517 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag; |
| 518 |
ctInfo.fReadSwizzle = skgpu::Swizzle::RGB1(); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
// Format: R16Float |
| 523 |
{ |
| 524 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatR16Float)]; |
| 525 |
info->fFlags = FormatInfo::kAllFlags; |
| 526 |
info->fColorTypeInfoCount = 1; |
| 527 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 528 |
int ctIdx = 0; |
| 529 |
// Format: R16Float, Surface: kA16_float |
| 530 |
{ |
| 531 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 532 |
ctInfo.fColorType = kA16_float_SkColorType; |
| 533 |
ctInfo.fTransferColorType = kA16_float_SkColorType; |
| 534 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 535 |
ctInfo.fReadSwizzle = skgpu::Swizzle("000r"); |
| 536 |
ctInfo.fWriteSwizzle = skgpu::Swizzle("a000"); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
// Format: RG8Unorm |
| 541 |
{ |
| 542 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatRG8Unorm)]; |
| 543 |
info->fFlags = FormatInfo::kAllFlags; |
| 544 |
info->fColorTypeInfoCount = 1; |
| 545 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 546 |
int ctIdx = 0; |
| 547 |
// Format: RG8Unorm, Surface: kR8G8_unorm |
| 548 |
{ |
| 549 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 550 |
ctInfo.fColorType = kR8G8_unorm_SkColorType; |
| 551 |
ctInfo.fTransferColorType = kR8G8_unorm_SkColorType; |
| 552 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
// Format: RGBA16Unorm |
| 557 |
{ |
| 558 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatRGBA16Unorm)]; |
| 559 |
if (this->isMac()) { |
| 560 |
info->fFlags = FormatInfo::kAllFlags; |
| 561 |
} else { |
| 562 |
info->fFlags = FormatInfo::kTexturable_Flag | FormatInfo::kRenderable_Flag; |
| 563 |
} |
| 564 |
info->fColorTypeInfoCount = 1; |
| 565 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 566 |
int ctIdx = 0; |
| 567 |
// Format: RGBA16Unorm, Surface: kR16G16B16A16_unorm |
| 568 |
{ |
| 569 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 570 |
ctInfo.fColorType = kR16G16B16A16_unorm_SkColorType; |
| 571 |
ctInfo.fTransferColorType = kR16G16B16A16_unorm_SkColorType; |
| 572 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
// Format: RG16Float |
| 577 |
{ |
| 578 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatRG16Float)]; |
| 579 |
info->fFlags = FormatInfo::kAllFlags; |
| 580 |
info->fColorTypeInfoCount = 1; |
| 581 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 582 |
int ctIdx = 0; |
| 583 |
// Format: RG16Float, Surface: kR16G16_float |
| 584 |
{ |
| 585 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 586 |
ctInfo.fColorType = kR16G16_float_SkColorType; |
| 587 |
ctInfo.fTransferColorType = kR16G16_float_SkColorType; |
| 588 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
// Format: R16Unorm |
| 593 |
{ |
| 594 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatR16Unorm)]; |
| 595 |
if (this->isMac()) { |
| 596 |
info->fFlags = FormatInfo::kAllFlags; |
| 597 |
} else { |
| 598 |
info->fFlags = FormatInfo::kTexturable_Flag | FormatInfo::kRenderable_Flag; |
| 599 |
} |
| 600 |
info->fColorTypeInfoCount = 2; |
| 601 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 602 |
int ctIdx = 0; |
| 603 |
// Format: R16Unorm, Surface: kA16_unorm |
| 604 |
{ |
| 605 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 606 |
ctInfo.fColorType = kA16_unorm_SkColorType; |
| 607 |
ctInfo.fTransferColorType = kA16_unorm_SkColorType; |
| 608 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 609 |
ctInfo.fReadSwizzle = skgpu::Swizzle("000r"); |
| 610 |
ctInfo.fWriteSwizzle = skgpu::Swizzle("a000"); |
| 611 |
} |
| 612 |
// Format: R16Unorm, Surface: kR16_unorm |
| 613 |
{ |
| 614 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 615 |
ctInfo.fColorType = kR16_unorm_SkColorType; |
| 616 |
ctInfo.fTransferColorType = kR16_unorm_SkColorType; |
| 617 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
// Format: RG16Unorm |
| 622 |
{ |
| 623 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatRG16Unorm)]; |
| 624 |
if (this->isMac()) { |
| 625 |
info->fFlags = FormatInfo::kAllFlags; |
| 626 |
} else { |
| 627 |
info->fFlags = FormatInfo::kTexturable_Flag | FormatInfo::kRenderable_Flag; |
| 628 |
} |
| 629 |
info->fColorTypeInfoCount = 1; |
| 630 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 631 |
int ctIdx = 0; |
| 632 |
// Format: RG16Unorm, Surface: kR16G16_unorm |
| 633 |
{ |
| 634 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 635 |
ctInfo.fColorType = kR16G16_unorm_SkColorType; |
| 636 |
ctInfo.fTransferColorType = kR16G16_unorm_SkColorType; |
| 637 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag; |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
// Format: ETC2_RGB8 |
| 642 |
{ |
| 643 |
if (@available(macOS 11.0, iOS 8.0, tvOS 9.0, *)) { |
| 644 |
if (this->isApple()) { |
| 645 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatETC2_RGB8)]; |
| 646 |
info->fFlags = FormatInfo::kTexturable_Flag; |
| 647 |
info->fColorTypeInfoCount = 1; |
| 648 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 649 |
int ctIdx = 0; |
| 650 |
// Format: ETC2_RGB8, Surface: kRGB_888x |
| 651 |
{ |
| 652 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 653 |
ctInfo.fColorType = kRGB_888x_SkColorType; |
| 654 |
ctInfo.fTransferColorType = kRGB_888x_SkColorType; |
| 655 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag; |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
// Format: BC1_RGBA |
| 662 |
{ |
| 663 |
#ifdef SK_BUILD_FOR_MAC |
| 664 |
if (this->isMac()) { |
| 665 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatBC1_RGBA)]; |
| 666 |
info->fFlags = FormatInfo::kTexturable_Flag; |
| 667 |
info->fColorTypeInfoCount = 1; |
| 668 |
info->fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info->fColorTypeInfoCount); |
| 669 |
int ctIdx = 0; |
| 670 |
// Format: BC1_RGBA, Surface: kRGBA_8888 |
| 671 |
{ |
| 672 |
auto& ctInfo = info->fColorTypeInfos[ctIdx++]; |
| 673 |
ctInfo.fColorType = kRGBA_8888_SkColorType; |
| 674 |
ctInfo.fTransferColorType = kRGBA_8888_SkColorType; |
| 675 |
ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag; |
| 676 |
} |
| 677 |
} |
| 678 |
#endif |
| 679 |
} |
| 680 |
|
| 681 |
/* |
| 682 |
* Non-color formats (renderable, but with no color type) |
| 683 |
*/ |
| 684 |
|
| 685 |
// Format: Stencil8 |
| 686 |
{ |
| 687 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatStencil8)]; |
| 688 |
info->fFlags = FormatInfo::kMSAA_Flag | FormatInfo::kRenderable_Flag; |
| 689 |
info->fColorTypeInfoCount = 0; |
| 690 |
} |
| 691 |
|
| 692 |
// Format: Depth16Unorm |
| 693 |
{ |
| 694 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatDepth16Unorm)]; |
| 695 |
info->fFlags = FormatInfo::kMSAA_Flag | FormatInfo::kRenderable_Flag; |
| 696 |
if (this->isMac() || fFamilyGroup >= 3) { |
| 697 |
info->fFlags |= FormatInfo::kResolve_Flag; |
| 698 |
} |
| 699 |
info->fColorTypeInfoCount = 0; |
| 700 |
} |
| 701 |
|
| 702 |
// Format: Depth32Float |
| 703 |
{ |
| 704 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatDepth32Float)]; |
| 705 |
info->fFlags = FormatInfo::kMSAA_Flag | FormatInfo::kRenderable_Flag; |
| 706 |
if (this->isMac() || fFamilyGroup >= 3) { |
| 707 |
info->fFlags |= FormatInfo::kResolve_Flag; |
| 708 |
} |
| 709 |
info->fColorTypeInfoCount = 0; |
| 710 |
} |
| 711 |
|
| 712 |
// Format: Depth24Unorm_Stencil8 |
| 713 |
{ |
| 714 |
#ifdef SK_BUILD_FOR_MAC |
| 715 |
if (this->isMac() && [device isDepth24Stencil8PixelFormatSupported]) { |
| 716 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatDepth24Unorm_Stencil8)]; |
| 717 |
info->fFlags = FormatInfo::kMSAA_Flag | |
| 718 |
FormatInfo::kRenderable_Flag | |
| 719 |
FormatInfo::kResolve_Flag; |
| 720 |
info->fColorTypeInfoCount = 0; |
| 721 |
} |
| 722 |
#endif |
| 723 |
} |
| 724 |
|
| 725 |
// Format: Depth32Float_Stencil8 |
| 726 |
{ |
| 727 |
info = &fFormatTable[GetFormatIndex(MTLPixelFormatDepth32Float_Stencil8)]; |
| 728 |
info->fFlags = FormatInfo::kMSAA_Flag | FormatInfo::kRenderable_Flag; |
| 729 |
if (this->isMac() || fFamilyGroup >= 3) { |
| 730 |
info->fFlags |= FormatInfo::kResolve_Flag; |
| 731 |
} |
| 732 |
info->fColorTypeInfoCount = 0; |
| 733 |
} |
| 734 |
|
| 735 |
//////////////////////////////////////////////////////////////////////////// |
| 736 |
// Map SkColorTypes (used for creating SkSurfaces) to MTLPixelFormats. The order in which the |
| 737 |
// formats are passed into the setColorType function indicates the priority in selecting which |
| 738 |
// format we use for a given SkColorType. |
| 739 |
|
| 740 |
std::fill_n(fColorTypeToFormatTable, kSkColorTypeCnt, MTLPixelFormatInvalid); |
| 741 |
|
| 742 |
this->setColorType(kAlpha_8_SkColorType, { MTLPixelFormatR8Unorm, |
| 743 |
MTLPixelFormatA8Unorm }); |
| 744 |
if (@available(macOS 11.0, iOS 8.0, tvOS 9.0, *)) { |
| 745 |
if (this->isApple()) { |
| 746 |
this->setColorType(kRGB_565_SkColorType, { MTLPixelFormatB5G6R5Unorm }); |
| 747 |
this->setColorType(kARGB_4444_SkColorType, { MTLPixelFormatABGR4Unorm }); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
this->setColorType(kRGBA_8888_SkColorType, { MTLPixelFormatRGBA8Unorm }); |
| 752 |
this->setColorType(kRGB_888x_SkColorType, { MTLPixelFormatRGBA8Unorm }); |
| 753 |
this->setColorType(kBGRA_8888_SkColorType, { MTLPixelFormatBGRA8Unorm }); |
| 754 |
this->setColorType(kRGBA_1010102_SkColorType, { MTLPixelFormatRGB10A2Unorm }); |
| 755 |
this->setColorType(kRGB_101010x_SkColorType, { MTLPixelFormatRGB10A2Unorm }); |
| 756 |
// kBGRA_1010102_SkColorType |
| 757 |
// kBGR_101010x_SkColorType |
| 758 |
// kBGR_101010x_XR_SkColorType |
| 759 |
this->setColorType(kGray_8_SkColorType, { MTLPixelFormatR8Unorm }); |
| 760 |
this->setColorType(kRGBA_F16Norm_SkColorType, { MTLPixelFormatRGBA16Float }); |
| 761 |
this->setColorType(kRGBA_F16_SkColorType, { MTLPixelFormatRGBA16Float }); |
| 762 |
this->setColorType(kRGB_F16F16F16x_SkColorType, { MTLPixelFormatRGBA16Float }); |
| 763 |
// kRGBA_F32_SkColorType |
| 764 |
this->setColorType(kR8G8_unorm_SkColorType, { MTLPixelFormatRG8Unorm }); |
| 765 |
this->setColorType(kA16_float_SkColorType, { MTLPixelFormatR16Float }); |
| 766 |
this->setColorType(kR16G16_float_SkColorType, { MTLPixelFormatRG16Float }); |
| 767 |
this->setColorType(kA16_unorm_SkColorType, { MTLPixelFormatR16Unorm }); |
| 768 |
this->setColorType(kR16_unorm_SkColorType, { MTLPixelFormatR16Unorm }); |
| 769 |
this->setColorType(kR16G16_unorm_SkColorType, { MTLPixelFormatRG16Unorm }); |
| 770 |
this->setColorType(kR16G16B16A16_unorm_SkColorType,{ MTLPixelFormatRGBA16Unorm }); |
| 771 |
this->setColorType(kSRGBA_8888_SkColorType, { MTLPixelFormatRGBA8Unorm_sRGB }); |
| 772 |
this->setColorType(kR8_unorm_SkColorType, { MTLPixelFormatR8Unorm }); |
| 773 |
|
| 774 |
} |
| 775 |
|
| 776 |
bool MtlCaps::isSampleCountSupported(TextureFormat format, SampleCount requestedSampleCount) const { |
| 777 |
const FormatInfo& formatInfo = this->getFormatInfo(TextureFormatToMTLPixelFormat(format)); |
| 778 |
if (!SkToBool(formatInfo.fFlags & FormatInfo::kRenderable_Flag)) { |
| 779 |
return false; |
| 780 |
} |
| 781 |
if (SkToBool(formatInfo.fFlags & FormatInfo::kMSAA_Flag)) { |
| 782 |
return SkToBool(fSupportedSampleCounts & requestedSampleCount); |
| 783 |
} else { |
| 784 |
// Only single sampling is supported for the format, so 1 sample should be generally |
| 785 |
// available, too. |
| 786 |
SkASSERT(SkToBool(fSupportedSampleCounts & SampleCount::k1)); |
| 787 |
return requestedSampleCount == SampleCount::k1; |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
TextureFormat MtlCaps::getDepthStencilFormat(SkEnumBitMask<DepthStencilFlags> mask) const { |
| 792 |
if (mask == DepthStencilFlags::kDepth) { |
| 793 |
// Graphite only needs 16-bits for depth values, so save some memory. If needed for |
| 794 |
// workarounds, MTLPixelFormatDepth32Float is also available. |
| 795 |
return TextureFormat::kD16; |
| 796 |
} else if (mask == DepthStencilFlags::kStencil) { |
| 797 |
return TextureFormat::kS8; |
| 798 |
} else if (mask == DepthStencilFlags::kDepthStencil) { |
| 799 |
#if defined(SK_BUILD_FOR_MAC) |
| 800 |
if (SkToBool(this->getFormatInfo(MTLPixelFormatDepth24Unorm_Stencil8).fFlags)) { |
| 801 |
return TextureFormat::kD24_S8; |
| 802 |
} |
| 803 |
#endif |
| 804 |
return TextureFormat::kD32F_S8; |
| 805 |
} |
| 806 |
return TextureFormat::kUnsupported; |
| 807 |
} |
| 808 |
|
| 809 |
TextureInfo MtlCaps::getDefaultAttachmentTextureInfo(AttachmentDesc desc, |
| 810 |
Protected, |
| 811 |
Discardable discardable) const { |
| 812 |
if (!this->isSampleCountSupported(desc.fFormat, desc.fSampleCount)) { |
| 813 |
return {}; |
| 814 |
} |
| 815 |
|
| 816 |
// Default to private in the event it's not discardable or memoryless is not available |
| 817 |
MTLStorageMode storageMode = MTLStorageModePrivate; |
| 818 |
|
| 819 |
// Try to use memoryless if it's available (only on new Apple silicon) |
| 820 |
if (discardable == Discardable::kYes && this->isApple()) { |
| 821 |
if (@available(macOS 11.0, iOS 10.0, tvOS 10.0, *)) { |
| 822 |
storageMode = MTLStorageModeMemoryless; |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
MtlTextureInfo info; |
| 827 |
info.fSampleCount = desc.fSampleCount; |
| 828 |
info.fMipmapped = Mipmapped::kNo; |
| 829 |
info.fFormat = TextureFormatToMTLPixelFormat(desc.fFormat); |
| 830 |
info.fUsage = MTLTextureUsageRenderTarget; |
| 831 |
info.fStorageMode = storageMode; |
| 832 |
info.fFramebufferOnly = false; |
| 833 |
|
| 834 |
return TextureInfos::MakeMetal(info); |
| 835 |
} |
| 836 |
|
| 837 |
TextureInfo MtlCaps::getDefaultSampledTextureInfo(SkColorType colorType, |
| 838 |
Mipmapped mipmapped, |
| 839 |
Protected, |
| 840 |
Renderable renderable) const { |
| 841 |
MTLTextureUsage usage = MTLTextureUsageShaderRead; |
| 842 |
if (renderable == Renderable::kYes) { |
| 843 |
usage |= MTLTextureUsageRenderTarget; |
| 844 |
} |
| 845 |
|
| 846 |
MTLPixelFormat format = this->getFormatFromColorType(colorType); |
| 847 |
if (format == MTLPixelFormatInvalid) { |
| 848 |
return {}; |
| 849 |
} |
| 850 |
|
| 851 |
MtlTextureInfo info; |
| 852 |
info.fSampleCount = SampleCount::k1; |
| 853 |
info.fMipmapped = mipmapped; |
| 854 |
info.fFormat = format; |
| 855 |
info.fUsage = usage; |
| 856 |
info.fStorageMode = MTLStorageModePrivate; |
| 857 |
info.fFramebufferOnly = false; |
| 858 |
|
| 859 |
return TextureInfos::MakeMetal(info); |
| 860 |
} |
| 861 |
|
| 862 |
TextureInfo MtlCaps::getTextureInfoForSampledCopy(const TextureInfo& textureInfo, |
| 863 |
Mipmapped mipmapped) const { |
| 864 |
MtlTextureInfo info; |
| 865 |
info.fSampleCount = SampleCount::k1; |
| 866 |
info.fMipmapped = mipmapped; |
| 867 |
info.fFormat = TextureInfoPriv::Get<MtlTextureInfo>(textureInfo).fFormat; |
| 868 |
info.fUsage = MTLTextureUsageShaderRead; |
| 869 |
info.fStorageMode = MTLStorageModePrivate; |
| 870 |
info.fFramebufferOnly = false; |
| 871 |
|
| 872 |
return TextureInfos::MakeMetal(info); |
| 873 |
} |
| 874 |
|
| 875 |
namespace { |
| 876 |
|
| 877 |
skgpu::UniqueKey::Domain get_domain() { |
| 878 |
static const skgpu::UniqueKey::Domain kMtlGraphicsPipelineDomain = |
| 879 |
skgpu::UniqueKey::GenerateDomain(); |
| 880 |
|
| 881 |
return kMtlGraphicsPipelineDomain; |
| 882 |
} |
| 883 |
|
| 884 |
MTLPixelFormat format_from_compression(SkTextureCompressionType compression) { |
| 885 |
switch (compression) { |
| 886 |
case SkTextureCompressionType::kETC2_RGB8_UNORM: |
| 887 |
return kMTLPixelFormatETC2_RGB8; |
| 888 |
case SkTextureCompressionType::kBC1_RGBA8_UNORM: |
| 889 |
#ifdef SK_BUILD_FOR_MAC |
| 890 |
return MTLPixelFormatBC1_RGBA; |
| 891 |
#endif |
| 892 |
default: |
| 893 |
return MTLPixelFormatInvalid; |
| 894 |
} |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
TextureInfo MtlCaps::getDefaultCompressedTextureInfo(SkTextureCompressionType compression, |
| 899 |
Mipmapped mipmapped, |
| 900 |
Protected) const { |
| 901 |
MTLTextureUsage usage = MTLTextureUsageShaderRead; |
| 902 |
|
| 903 |
MTLPixelFormat format = format_from_compression(compression); |
| 904 |
if (format == MTLPixelFormatInvalid) { |
| 905 |
return {}; |
| 906 |
} |
| 907 |
|
| 908 |
MtlTextureInfo info; |
| 909 |
info.fSampleCount = SampleCount::k1; |
| 910 |
info.fMipmapped = mipmapped; |
| 911 |
info.fFormat = format; |
| 912 |
info.fUsage = usage; |
| 913 |
info.fStorageMode = MTLStorageModePrivate; |
| 914 |
info.fFramebufferOnly = false; |
| 915 |
|
| 916 |
return TextureInfos::MakeMetal(info); |
| 917 |
} |
| 918 |
|
| 919 |
TextureInfo MtlCaps::getDefaultStorageTextureInfo(SkColorType colorType) const { |
| 920 |
// Storage textures are currently always sampleable from a shader. |
| 921 |
MTLPixelFormat format = static_cast<MTLPixelFormat>(this->getFormatFromColorType(colorType)); |
| 922 |
if (format == MTLPixelFormatInvalid) { |
| 923 |
return {}; |
| 924 |
} |
| 925 |
|
| 926 |
const FormatInfo& formatInfo = this->getFormatInfo(format); |
| 927 |
if (!SkToBool(FormatInfo::kStorage_Flag & formatInfo.fFlags)) { |
| 928 |
return {}; |
| 929 |
} |
| 930 |
|
| 931 |
MtlTextureInfo info; |
| 932 |
info.fSampleCount = SampleCount::k1; |
| 933 |
info.fMipmapped = Mipmapped::kNo; |
| 934 |
info.fFormat = format; |
| 935 |
info.fUsage = MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead; |
| 936 |
info.fStorageMode = MTLStorageModePrivate; |
| 937 |
info.fFramebufferOnly = false; |
| 938 |
|
| 939 |
return TextureInfos::MakeMetal(info); |
| 940 |
} |
| 941 |
|
| 942 |
SkSpan<const Caps::ColorTypeInfo> MtlCaps::getColorTypeInfos(const TextureInfo& textureInfo) const { |
| 943 |
MTLPixelFormat mtlFormat = TextureInfoPriv::Get<MtlTextureInfo>(textureInfo).fFormat; |
| 944 |
if (mtlFormat == MTLPixelFormatInvalid) { |
| 945 |
return {}; |
| 946 |
} |
| 947 |
|
| 948 |
const FormatInfo& formatInfo = this->getFormatInfo(mtlFormat); |
| 949 |
return {formatInfo.fColorTypeInfos.get(), formatInfo.fColorTypeInfoCount}; |
| 950 |
} |
| 951 |
|
| 952 |
static constexpr uint16_t kMtlGraphicsPipelineKeyData32Count = 4; |
| 953 |
|
| 954 |
UniqueKey MtlCaps::makeGraphicsPipelineKey(const GraphicsPipelineDesc& pipelineDesc, |
| 955 |
const RenderPassDesc& renderPassDesc) const { |
| 956 |
UniqueKey pipelineKey; |
| 957 |
{ |
| 958 |
// 4 uint32_t's (render step id, paint id, renderpass desc, uint16 write swizzle key) |
| 959 |
UniqueKey::Builder builder(&pipelineKey, get_domain(), |
| 960 |
kMtlGraphicsPipelineKeyData32Count, "MtlGraphicsPipeline"); |
| 961 |
// add GraphicsPipelineDesc key |
| 962 |
builder[0] = static_cast<uint32_t>(pipelineDesc.renderStepID()); |
| 963 |
builder[1] = pipelineDesc.paintParamsID().asUInt(); |
| 964 |
|
| 965 |
// add RenderPassDesc key |
| 966 |
builder[2] = this->getRenderPassDescKey(renderPassDesc); |
| 967 |
builder[3] = renderPassDesc.fWriteSwizzle.asKey(); |
| 968 |
|
| 969 |
builder.finish(); |
| 970 |
} |
| 971 |
|
| 972 |
return pipelineKey; |
| 973 |
} |
| 974 |
|
| 975 |
bool MtlCaps::extractGraphicsDescs(const UniqueKey& key, |
| 976 |
GraphicsPipelineDesc* pipelineDesc, |
| 977 |
RenderPassDesc* renderPassDesc, |
| 978 |
const RendererProvider* rendererProvider) const { |
| 979 |
struct UnpackedKeyData { |
| 980 |
// From the GraphicsPipelineDesc |
| 981 |
RenderStep::RenderStepID fRenderStepID = RenderStep::RenderStepID::kInvalid; |
| 982 |
UniquePaintParamsID fPaintParamsID = UniquePaintParamsID::Invalid(); |
| 983 |
|
| 984 |
// From the RenderPassDesc |
| 985 |
TextureFormat fColorFormat = TextureFormat::kUnsupported; |
| 986 |
SampleCount fColorSampleCount = SampleCount::k1; |
| 987 |
|
| 988 |
TextureFormat fDSFormat = TextureFormat::kUnsupported; |
| 989 |
SampleCount fDSSampleCount = SampleCount::k1; |
| 990 |
|
| 991 |
Swizzle fWriteSwizzle; |
| 992 |
} keyData; |
| 993 |
|
| 994 |
SkASSERT(key.domain() == get_domain()); |
| 995 |
SkASSERT(key.dataSize() == 4 * kMtlGraphicsPipelineKeyData32Count); |
| 996 |
|
| 997 |
const uint32_t* rawKeyData = key.data(); |
| 998 |
|
| 999 |
SkASSERT(RenderStep::IsValidRenderStepID(rawKeyData[0])); |
| 1000 |
keyData.fRenderStepID = static_cast<RenderStep::RenderStepID>(rawKeyData[0]); |
| 1001 |
keyData.fPaintParamsID = rawKeyData[1] ? UniquePaintParamsID(rawKeyData[1]) |
| 1002 |
: UniquePaintParamsID::Invalid(); |
| 1003 |
|
| 1004 |
keyData.fDSFormat = static_cast<TextureFormat>((rawKeyData[2] >> 8) & 0xFF); |
| 1005 |
keyData.fDSSampleCount = static_cast<SampleCount>(rawKeyData[2] & 0xFF); |
| 1006 |
|
| 1007 |
keyData.fColorFormat = static_cast<TextureFormat>((rawKeyData[2] >> 24) & 0xFF); |
| 1008 |
keyData.fColorSampleCount = static_cast<SampleCount>((rawKeyData[2] >> 16) & 0xFF); |
| 1009 |
|
| 1010 |
keyData.fWriteSwizzle = SwizzleCtorAccessor::Make(rawKeyData[3]); |
| 1011 |
|
| 1012 |
// Recreate the RenderPassDesc, picking arbitrary load/store ops. Since Metal doesn't need |
| 1013 |
// to include resolve attachment details, assume that if color attachment's sample count is > 1 |
| 1014 |
// that there is a matching resolve attachment (no MSAA-render-to-single-sample support in MTL). |
| 1015 |
SkASSERT(keyData.fColorSampleCount == keyData.fDSSampleCount || |
| 1016 |
keyData.fDSFormat == TextureFormat::kUnsupported); |
| 1017 |
*renderPassDesc = {}; |
| 1018 |
renderPassDesc->fColorAttachment = {keyData.fColorFormat, |
| 1019 |
LoadOp::kClear, |
| 1020 |
StoreOp::kStore, |
| 1021 |
keyData.fColorSampleCount}; |
| 1022 |
renderPassDesc->fDepthStencilAttachment = {keyData.fDSFormat, |
| 1023 |
LoadOp::kClear, |
| 1024 |
StoreOp::kDiscard, |
| 1025 |
keyData.fDSSampleCount}; |
| 1026 |
if (keyData.fColorSampleCount > SampleCount::k1) { |
| 1027 |
renderPassDesc->fColorResolveAttachment = {keyData.fColorFormat, |
| 1028 |
LoadOp::kClear, |
| 1029 |
StoreOp::kStore, |
| 1030 |
SampleCount::k1}; |
| 1031 |
renderPassDesc->fColorAttachment.fStoreOp = StoreOp::kDiscard; |
| 1032 |
} |
| 1033 |
|
| 1034 |
renderPassDesc->fSampleCount = keyData.fColorSampleCount; |
| 1035 |
renderPassDesc->fWriteSwizzle = keyData.fWriteSwizzle; |
| 1036 |
renderPassDesc->fDstReadStrategy = this->getDstReadStrategy(); |
| 1037 |
|
| 1038 |
// Recreate the GraphicsPipelineDesc |
| 1039 |
const RenderStep* renderStep = rendererProvider->lookup(keyData.fRenderStepID); |
| 1040 |
|
| 1041 |
UniquePaintParamsID paintID = renderStep->performsShading() ? keyData.fPaintParamsID |
| 1042 |
: UniquePaintParamsID::Invalid(); |
| 1043 |
|
| 1044 |
*pipelineDesc = GraphicsPipelineDesc(renderStep->renderStepID(), paintID); |
| 1045 |
|
| 1046 |
return true; |
| 1047 |
} |
| 1048 |
|
| 1049 |
uint32_t MtlCaps::getRenderPassDescKey(const RenderPassDesc& renderPassDesc) const { |
| 1050 |
static_assert(kTextureFormatCount <= 256); |
| 1051 |
|
| 1052 |
// Each attachment format + sample count fits in 16-bits. Load/store ops are ignored. |
| 1053 |
auto attachmentKey = [](AttachmentDesc desc) { |
| 1054 |
SkASSERT(desc.fFormat != TextureFormat::kUnsupported || |
| 1055 |
desc.fSampleCount == SampleCount::k1); |
| 1056 |
return (static_cast<uint32_t>(desc.fFormat) << 8) | |
| 1057 |
static_cast<uint32_t>(desc.fSampleCount); |
| 1058 |
}; |
| 1059 |
|
| 1060 |
// The MtlRenderPassDescriptor requires no information about the resolve attachment |
| 1061 |
return (attachmentKey(renderPassDesc.fColorAttachment) << 16) | |
| 1062 |
attachmentKey(renderPassDesc.fDepthStencilAttachment); |
| 1063 |
} |
| 1064 |
|
| 1065 |
UniqueKey MtlCaps::makeComputePipelineKey(const ComputePipelineDesc& pipelineDesc) const { |
| 1066 |
UniqueKey pipelineKey; |
| 1067 |
{ |
| 1068 |
static const skgpu::UniqueKey::Domain kComputePipelineDomain = UniqueKey::GenerateDomain(); |
| 1069 |
// The key is made up of a single uint32_t corresponding to the compute step ID. |
| 1070 |
UniqueKey::Builder builder(&pipelineKey, kComputePipelineDomain, 1, "ComputePipeline"); |
| 1071 |
builder[0] = pipelineDesc.computeStep()->uniqueID(); |
| 1072 |
|
| 1073 |
// TODO(b/240615224): The local work group size should factor into the key on platforms |
| 1074 |
// that don't support specialization constants and require the workgroup/threadgroup size to |
| 1075 |
// be specified in the shader text (D3D12, Vulkan 1.0, and OpenGL). |
| 1076 |
|
| 1077 |
builder.finish(); |
| 1078 |
} |
| 1079 |
return pipelineKey; |
| 1080 |
} |
| 1081 |
|
| 1082 |
bool MtlCaps::onIsTexturable(const TextureInfo& info) const { |
| 1083 |
if (!info.isValid()) { |
| 1084 |
return false; |
| 1085 |
} |
| 1086 |
const auto& mtlInfo = TextureInfoPriv::Get<MtlTextureInfo>(info); |
| 1087 |
if (!(mtlInfo.fUsage & MTLTextureUsageShaderRead)) { |
| 1088 |
return false; |
| 1089 |
} |
| 1090 |
if (mtlInfo.fFramebufferOnly) { |
| 1091 |
return false; |
| 1092 |
} |
| 1093 |
return this->isTexturable(mtlInfo.fFormat); |
| 1094 |
} |
| 1095 |
|
| 1096 |
bool MtlCaps::isTexturable(MTLPixelFormat format) const { |
| 1097 |
const FormatInfo& formatInfo = this->getFormatInfo(format); |
| 1098 |
return SkToBool(FormatInfo::kTexturable_Flag & formatInfo.fFlags); |
| 1099 |
} |
| 1100 |
|
| 1101 |
bool MtlCaps::isRenderable(const TextureInfo& info) const { |
| 1102 |
if (!info.isValid()) { |
| 1103 |
return false; |
| 1104 |
} |
| 1105 |
TextureFormat format = TextureInfoPriv::ViewFormat(info); |
| 1106 |
const auto& mtlInfo = TextureInfoPriv::Get<MtlTextureInfo>(info); |
| 1107 |
return (mtlInfo.fUsage & MTLTextureUsageRenderTarget) && |
| 1108 |
this->isSampleCountSupported(format, info.sampleCount()); |
| 1109 |
} |
| 1110 |
|
| 1111 |
bool MtlCaps::isStorage(const TextureInfo& info) const { |
| 1112 |
if (!info.isValid()) { |
| 1113 |
return false; |
| 1114 |
} |
| 1115 |
const auto& mtlInfo = TextureInfoPriv::Get<MtlTextureInfo>(info); |
| 1116 |
if (!(mtlInfo.fUsage & MTLTextureUsageShaderWrite)) { |
| 1117 |
return false; |
| 1118 |
} |
| 1119 |
if (mtlInfo.fFramebufferOnly) { |
| 1120 |
return false; |
| 1121 |
} |
| 1122 |
const FormatInfo& formatInfo = this->getFormatInfo(mtlInfo.fFormat); |
| 1123 |
return info.sampleCount() == SampleCount::k1 && |
| 1124 |
SkToBool(FormatInfo::kStorage_Flag & formatInfo.fFlags); |
| 1125 |
} |
| 1126 |
|
| 1127 |
bool MtlCaps::supportsWritePixels(const TextureInfo& texInfo) const { |
| 1128 |
if (!texInfo.isValid()) { |
| 1129 |
return false; |
| 1130 |
} |
| 1131 |
const auto& mtlInfo = TextureInfoPriv::Get<MtlTextureInfo>(texInfo); |
| 1132 |
if (mtlInfo.fFramebufferOnly) { |
| 1133 |
return false; |
| 1134 |
} |
| 1135 |
|
| 1136 |
if (texInfo.sampleCount() > SampleCount::k1) { |
| 1137 |
return false; |
| 1138 |
} |
| 1139 |
|
| 1140 |
return true; |
| 1141 |
} |
| 1142 |
|
| 1143 |
bool MtlCaps::supportsReadPixels(const TextureInfo& texInfo) const { |
| 1144 |
if (!texInfo.isValid()) { |
| 1145 |
return false; |
| 1146 |
} |
| 1147 |
const auto& mtlInfo = TextureInfoPriv::Get<MtlTextureInfo>(texInfo); |
| 1148 |
if (mtlInfo.fFramebufferOnly) { |
| 1149 |
return false; |
| 1150 |
} |
| 1151 |
|
| 1152 |
// We disallow reading back directly from compressed textures. |
| 1153 |
if (MtlFormatIsCompressed(mtlInfo.fFormat)) { |
| 1154 |
return false; |
| 1155 |
} |
| 1156 |
|
| 1157 |
if (texInfo.sampleCount() > SampleCount::k1) { |
| 1158 |
return false; |
| 1159 |
} |
| 1160 |
|
| 1161 |
return true; |
| 1162 |
} |
| 1163 |
|
| 1164 |
void MtlCaps::buildKeyForTexture(SkISize dimensions, |
| 1165 |
const TextureInfo& info, |
| 1166 |
ResourceType type, |
| 1167 |
GraphiteResourceKey* key) const { |
| 1168 |
const auto& mtlInfo = TextureInfoPriv::Get<MtlTextureInfo>(info); |
| 1169 |
|
| 1170 |
SkASSERT(!dimensions.isEmpty()); |
| 1171 |
|
| 1172 |
// A MTLPixelFormat is an NSUInteger type which is documented to be 32 bits in 32 bit |
| 1173 |
// applications and 64 bits in 64 bit applications. So it should fit in an uint64_t, but adding |
| 1174 |
// the assert heere to make sure. |
| 1175 |
static_assert(sizeof(MTLPixelFormat) <= sizeof(uint64_t)); |
| 1176 |
SkASSERT(mtlInfo.fFormat != MTLPixelFormatInvalid); |
| 1177 |
uint64_t formatKey = static_cast<uint64_t>(mtlInfo.fFormat); |
| 1178 |
|
| 1179 |
uint32_t samplesKey = SamplesToKey(info.sampleCount()); |
| 1180 |
// We don't have to key the number of mip levels because it is inherit in the combination of |
| 1181 |
// isMipped and dimensions. |
| 1182 |
bool isMipped = mtlInfo.fMipmapped == Mipmapped::kYes; |
| 1183 |
Protected isProtected = info.isProtected(); |
| 1184 |
bool isFBOnly = mtlInfo.fFramebufferOnly; |
| 1185 |
|
| 1186 |
// Confirm all the below parts of the key can fit in a single uint32_t. The sum of the shift |
| 1187 |
// amounts in the asserts must be less than or equal to 32. |
| 1188 |
SkASSERT(samplesKey < (1u << kNumSampleKeyBits)); |
| 1189 |
SkASSERT(static_cast<uint32_t>(isMipped) < (1u << 1)); |
| 1190 |
SkASSERT(static_cast<uint32_t>(isProtected) < (1u << 1)); |
| 1191 |
SkASSERT(mtlInfo.fUsage < (1u << 5)); |
| 1192 |
SkASSERT(mtlInfo.fStorageMode < (1u << 2)); |
| 1193 |
SkASSERT(static_cast<uint32_t>(isFBOnly) < (1u << 1)); |
| 1194 |
|
| 1195 |
// We need two uint32_ts for dimensions, 2 for format, and 1 for the rest of the key; |
| 1196 |
static uint16_t kNum32DataCnt = 2 + 2 + 1; |
| 1197 |
|
| 1198 |
GraphiteResourceKey::Builder builder(key, type, kNum32DataCnt); |
| 1199 |
|
| 1200 |
builder[0] = dimensions.width(); |
| 1201 |
builder[1] = dimensions.height(); |
| 1202 |
builder[2] = formatKey & 0xFFFFFFFF; |
| 1203 |
builder[3] = (formatKey >> 32) & 0xFFFFFFFF; |
| 1204 |
builder[4] = (samplesKey << 0) | |
| 1205 |
(static_cast<uint32_t>(isMipped) << kNumSampleKeyBits) | |
| 1206 |
(static_cast<uint32_t>(isProtected) << 4) | |
| 1207 |
(static_cast<uint32_t>(mtlInfo.fUsage) << 5) | |
| 1208 |
(static_cast<uint32_t>(mtlInfo.fStorageMode) << 10)| |
| 1209 |
(static_cast<uint32_t>(isFBOnly) << 12); |
| 1210 |
|
| 1211 |
} |
| 1212 |
|
| 1213 |
} // namespace skgpu::graphite |