Flutter Impeller
formats_mtl.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_FORMATS_MTL_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_FORMATS_MTL_H_
7 
8 #include <Metal/Metal.h>
9 
10 #include <optional>
11 
12 #include "flutter/fml/build_config.h"
14 #include "impeller/core/formats.h"
17 
18 namespace impeller {
19 
20 class RenderTarget;
21 
22 constexpr PixelFormat FromMTLPixelFormat(MTLPixelFormat format) {
23  switch (format) {
24  case MTLPixelFormatInvalid:
25  return PixelFormat::kUnknown;
26  case MTLPixelFormatBGRA8Unorm:
28  case MTLPixelFormatBGRA8Unorm_sRGB:
30  case MTLPixelFormatRGBA8Unorm:
32  case MTLPixelFormatRGBA8Unorm_sRGB:
34  case MTLPixelFormatRGBA32Float:
36  case MTLPixelFormatRGBA16Float:
38  case MTLPixelFormatStencil8:
39  return PixelFormat::kS8UInt;
40 #if !FML_OS_IOS
41  case MTLPixelFormatDepth24Unorm_Stencil8:
43 #endif // FML_OS_IOS
44  case MTLPixelFormatDepth32Float_Stencil8:
46  case MTLPixelFormatBGR10_XR_sRGB:
48  case MTLPixelFormatBGR10_XR:
50  case MTLPixelFormatBGRA10_XR:
52  default:
53  return PixelFormat::kUnknown;
54  }
55  return PixelFormat::kUnknown;
56 }
57 
58 /// Safe accessor for MTLPixelFormatDepth24Unorm_Stencil8.
59 /// Returns PixelFormat::kUnknown if MTLPixelFormatDepth24Unorm_Stencil8 isn't
60 /// supported.
62 
63 /// Safe accessor for MTLPixelFormatBGR10_XR_sRGB.
64 /// Returns PixelFormat::kUnknown if MTLPixelFormatBGR10_XR_sRGB isn't
65 /// supported.
66 MTLPixelFormat SafeMTLPixelFormatBGR10_XR_sRGB();
67 
68 /// Safe accessor for MTLPixelFormatBGR10_XR.
69 /// Returns PixelFormat::kUnknown if MTLPixelFormatBGR10_XR isn't supported.
70 MTLPixelFormat SafeMTLPixelFormatBGR10_XR();
71 
72 /// Safe accessor for MTLPixelFormatBGRA10_XR.
73 /// Returns PixelFormat::kUnknown if MTLPixelFormatBGR10_XR isn't supported.
74 MTLPixelFormat SafeMTLPixelFormatBGRA10_XR();
75 
76 /// Safe accessors for the block-compressed formats. Each returns
77 /// MTLPixelFormatInvalid on platforms or OS versions where the format is not
78 /// available (BC is macOS only; ETC2 and ASTC require macOS 11.0+).
79 MTLPixelFormat SafeMTLPixelFormatBC1_RGBA();
80 MTLPixelFormat SafeMTLPixelFormatBC1_RGBA_sRGB();
81 MTLPixelFormat SafeMTLPixelFormatBC3_RGBA();
82 MTLPixelFormat SafeMTLPixelFormatBC3_RGBA_sRGB();
83 MTLPixelFormat SafeMTLPixelFormatBC5_RGUnorm();
84 MTLPixelFormat SafeMTLPixelFormatBC7_RGBAUnorm();
86 MTLPixelFormat SafeMTLPixelFormatETC2_RGB8();
87 MTLPixelFormat SafeMTLPixelFormatETC2_RGB8_sRGB();
88 MTLPixelFormat SafeMTLPixelFormatEAC_RGBA8();
89 MTLPixelFormat SafeMTLPixelFormatEAC_RGBA8_sRGB();
90 MTLPixelFormat SafeMTLPixelFormatASTC_4x4_LDR();
91 MTLPixelFormat SafeMTLPixelFormatASTC_4x4_sRGB();
92 MTLPixelFormat SafeMTLPixelFormatASTC_8x8_LDR();
93 MTLPixelFormat SafeMTLPixelFormatASTC_8x8_sRGB();
94 MTLPixelFormat SafeMTLPixelFormatASTC_4x4_HDR();
95 MTLPixelFormat SafeMTLPixelFormatASTC_8x8_HDR();
96 
97 constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format) {
98  switch (format) {
100  return MTLPixelFormatInvalid;
102  return MTLPixelFormatA8Unorm;
104  return MTLPixelFormatR8Unorm;
106  return MTLPixelFormatRG8Unorm;
108  return MTLPixelFormatBGRA8Unorm;
110  return MTLPixelFormatBGRA8Unorm_sRGB;
112  return MTLPixelFormatRGBA8Unorm;
114  return MTLPixelFormatRGBA8Unorm_sRGB;
116  return MTLPixelFormatRGBA32Float;
118  return MTLPixelFormatRGBA16Float;
120  return MTLPixelFormatStencil8;
124  return MTLPixelFormatDepth32Float_Stencil8;
132  return MTLPixelFormatR32Float;
167  }
168  return MTLPixelFormatInvalid;
169 };
170 
171 constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type) {
172  switch (type) {
173  case BlendFactor::kZero:
174  return MTLBlendFactorZero;
175  case BlendFactor::kOne:
176  return MTLBlendFactorOne;
178  return MTLBlendFactorSourceColor;
180  return MTLBlendFactorOneMinusSourceColor;
182  return MTLBlendFactorSourceAlpha;
184  return MTLBlendFactorOneMinusSourceAlpha;
186  return MTLBlendFactorDestinationColor;
188  return MTLBlendFactorOneMinusDestinationColor;
190  return MTLBlendFactorDestinationAlpha;
192  return MTLBlendFactorOneMinusDestinationAlpha;
194  return MTLBlendFactorSourceAlphaSaturated;
196  return MTLBlendFactorBlendColor;
198  return MTLBlendFactorOneMinusBlendColor;
200  return MTLBlendFactorBlendAlpha;
202  return MTLBlendFactorOneMinusBlendAlpha;
203  }
204  return MTLBlendFactorZero;
205 };
206 
207 constexpr MTLPrimitiveType ToMTLPrimitiveType(PrimitiveType type) {
208  switch (type) {
210  return MTLPrimitiveTypeTriangle;
212  return MTLPrimitiveTypeTriangleStrip;
214  return MTLPrimitiveTypeLine;
216  return MTLPrimitiveTypeLineStrip;
218  return MTLPrimitiveTypePoint;
220  // Callers are expected to perform a capability check for triangle fan
221  // support.
222  return MTLPrimitiveTypePoint;
223  }
224  return MTLPrimitiveTypePoint;
225 }
226 
227 constexpr MTLTriangleFillMode ToMTLTriangleFillMode(PolygonMode mode) {
228  switch (mode) {
229  case PolygonMode::kFill:
230  return MTLTriangleFillModeFill;
231  case PolygonMode::kLine:
232  return MTLTriangleFillModeLines;
233  }
234  return MTLTriangleFillModeFill;
235 }
236 
237 constexpr MTLIndexType ToMTLIndexType(IndexType type) {
238  switch (type) {
239  case IndexType::k16bit:
240  return MTLIndexTypeUInt16;
241  default:
242  return MTLIndexTypeUInt32;
243  }
244 }
245 
246 constexpr MTLCullMode ToMTLCullMode(CullMode mode) {
247  switch (mode) {
248  case CullMode::kNone:
249  return MTLCullModeNone;
250  case CullMode::kBackFace:
251  return MTLCullModeBack;
253  return MTLCullModeFront;
254  }
255  return MTLCullModeNone;
256 }
257 
258 constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type) {
259  switch (type) {
261  return MTLBlendOperationAdd;
263  return MTLBlendOperationSubtract;
265  return MTLBlendOperationReverseSubtract;
266  }
267  return MTLBlendOperationAdd;
268 };
269 
270 constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type) {
271  MTLColorWriteMask mask = MTLColorWriteMaskNone;
272 
273  if (type & ColorWriteMaskBits::kRed) {
274  mask |= MTLColorWriteMaskRed;
275  }
276 
277  if (type & ColorWriteMaskBits::kGreen) {
278  mask |= MTLColorWriteMaskGreen;
279  }
280 
281  if (type & ColorWriteMaskBits::kBlue) {
282  mask |= MTLColorWriteMaskBlue;
283  }
284 
285  if (type & ColorWriteMaskBits::kAlpha) {
286  mask |= MTLColorWriteMaskAlpha;
287  }
288 
289  return mask;
290 };
291 
292 constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func) {
293  switch (func) {
295  return MTLCompareFunctionNever;
297  return MTLCompareFunctionLess;
299  return MTLCompareFunctionEqual;
301  return MTLCompareFunctionLessEqual;
303  return MTLCompareFunctionGreater;
305  return MTLCompareFunctionNotEqual;
307  return MTLCompareFunctionGreaterEqual;
309  return MTLCompareFunctionAlways;
310  }
311  return MTLCompareFunctionAlways;
312 };
313 
314 constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op) {
315  switch (op) {
317  return MTLStencilOperationKeep;
319  return MTLStencilOperationZero;
321  return MTLStencilOperationReplace;
323  return MTLStencilOperationIncrementClamp;
325  return MTLStencilOperationDecrementClamp;
327  return MTLStencilOperationInvert;
329  return MTLStencilOperationIncrementWrap;
331  return MTLStencilOperationDecrementWrap;
332  }
333  return MTLStencilOperationKeep;
334 };
335 
336 constexpr MTLLoadAction ToMTLLoadAction(LoadAction action) {
337  switch (action) {
339  return MTLLoadActionDontCare;
340  case LoadAction::kLoad:
341  return MTLLoadActionLoad;
342  case LoadAction::kClear:
343  return MTLLoadActionClear;
344  }
345 
346  return MTLLoadActionDontCare;
347 }
348 
349 constexpr LoadAction FromMTLLoadAction(MTLLoadAction action) {
350  switch (action) {
351  case MTLLoadActionDontCare:
352  return LoadAction::kDontCare;
353  case MTLLoadActionLoad:
354  return LoadAction::kLoad;
355  case MTLLoadActionClear:
356  return LoadAction::kClear;
357  default:
358  break;
359  }
360 
361  return LoadAction::kDontCare;
362 }
363 
364 constexpr MTLStoreAction ToMTLStoreAction(StoreAction action) {
365  switch (action) {
367  return MTLStoreActionDontCare;
368  case StoreAction::kStore:
369  return MTLStoreActionStore;
371  return MTLStoreActionMultisampleResolve;
373  return MTLStoreActionStoreAndMultisampleResolve;
374  }
375  return MTLStoreActionDontCare;
376 }
377 
378 constexpr StoreAction FromMTLStoreAction(MTLStoreAction action) {
379  switch (action) {
380  case MTLStoreActionDontCare:
381  return StoreAction::kDontCare;
382  case MTLStoreActionStore:
383  return StoreAction::kStore;
384  case MTLStoreActionMultisampleResolve:
386  case MTLStoreActionStoreAndMultisampleResolve:
388  default:
389  break;
390  }
391  return StoreAction::kDontCare;
392 }
393 
394 constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter) {
395  switch (filter) {
397  return MTLSamplerMinMagFilterNearest;
399  return MTLSamplerMinMagFilterLinear;
400  }
401  return MTLSamplerMinMagFilterNearest;
402 }
403 
404 constexpr MTLSamplerMipFilter ToMTLSamplerMipFilter(MipFilter filter) {
405  switch (filter) {
406  case MipFilter::kBase:
407  return MTLSamplerMipFilterNotMipmapped;
408  case MipFilter::kNearest:
409  return MTLSamplerMipFilterNearest;
410  case MipFilter::kLinear:
411  return MTLSamplerMipFilterLinear;
412  }
413  return MTLSamplerMipFilterNotMipmapped;
414 }
415 
416 constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(
417  SamplerAddressMode mode) {
418  switch (mode) {
420  return MTLSamplerAddressModeClampToEdge;
422  return MTLSamplerAddressModeRepeat;
424  return MTLSamplerAddressModeMirrorRepeat;
426  return MTLSamplerAddressModeClampToZero;
427  }
428  return MTLSamplerAddressModeClampToEdge;
429 }
430 
431 inline MTLClearColor ToMTLClearColor(const Color& color) {
432  return MTLClearColorMake(color.red, color.green, color.blue, color.alpha);
433 }
434 
435 constexpr MTLTextureType ToMTLTextureType(TextureType type) {
436  switch (type) {
438  return MTLTextureType2D;
440  return MTLTextureType2DMultisample;
442  return MTLTextureTypeCube;
445  << "kTextureExternalOES can not be used with the Metal backend.";
446  }
447  return MTLTextureType2D;
448 }
449 
450 MTLRenderPipelineColorAttachmentDescriptor*
452  ColorAttachmentDescriptor descriptor);
453 
454 MTLDepthStencilDescriptor* ToMTLDepthStencilDescriptor(
455  std::optional<DepthAttachmentDescriptor> depth,
456  std::optional<StencilAttachmentDescriptor> front,
457  std::optional<StencilAttachmentDescriptor> back);
458 
459 MTLTextureDescriptor* ToMTLTextureDescriptor(const TextureDescriptor& desc);
460 
461 } // namespace impeller
462 
463 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_FORMATS_MTL_H_
MTLPixelFormat SafeMTLPixelFormatETC2_RGB8_sRGB()
Definition: formats_mtl.mm:220
MTLPixelFormat SafeMTLPixelFormatEAC_RGBA8()
Definition: formats_mtl.mm:227
BlendFactor
Definition: formats.h:366
PrimitiveType
Decides how backend draws pixels based on input vertices.
Definition: formats.h:529
@ kPoint
Draws a point at each input vertex.
constexpr PixelFormat FromMTLPixelFormat(MTLPixelFormat format)
Definition: formats_mtl.h:22
MTLPixelFormat SafeMTLPixelFormatASTC_4x4_HDR()
Definition: formats_mtl.mm:269
MTLPixelFormat SafeMTLPixelFormatETC2_RGB8()
Definition: formats_mtl.mm:213
SamplerAddressMode
Definition: formats.h:618
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
MTLPixelFormat SafeMTLPixelFormatASTC_4x4_sRGB()
Definition: formats_mtl.mm:248
constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type)
Definition: formats_mtl.h:270
constexpr StoreAction FromMTLStoreAction(MTLStoreAction action)
Definition: formats_mtl.h:378
MTLPixelFormat SafeMTLPixelFormatASTC_4x4_LDR()
Definition: formats_mtl.mm:241
MTLPixelFormat SafeMTLPixelFormatASTC_8x8_HDR()
Definition: formats_mtl.mm:276
constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op)
Definition: formats_mtl.h:314
MTLPixelFormat SafeMTLPixelFormatBC7_RGBAUnorm_sRGB()
Definition: formats_mtl.mm:202
MTLPixelFormat SafeMTLPixelFormatBGR10_XR_sRGB()
Definition: formats_mtl.mm:122
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
CompareFunction
Definition: formats.h:804
@ kEqual
Comparison test passes if new_value == current_value.
@ kLessEqual
Comparison test passes if new_value <= current_value.
@ kGreaterEqual
Comparison test passes if new_value >= current_value.
@ kAlways
Comparison test passes always passes.
@ kLess
Comparison test passes if new_value < current_value.
@ kGreater
Comparison test passes if new_value > current_value.
@ kNotEqual
Comparison test passes if new_value != current_value.
@ kNever
Comparison test never passes.
MTLDepthStencilDescriptor * ToMTLDepthStencilDescriptor(std::optional< DepthAttachmentDescriptor > depth, std::optional< StencilAttachmentDescriptor > front, std::optional< StencilAttachmentDescriptor > back)
Definition: formats_mtl.mm:54
MipFilter
Options for selecting and filtering between mipmap levels.
Definition: formats.h:602
@ kLinear
Sample from the two nearest mip levels and linearly interpolate.
@ kBase
The texture is sampled as if it only had a single mipmap level.
@ kNearest
The nearst mipmap level is selected.
StencilOperation
Definition: formats.h:823
@ kDecrementWrap
Decrement the current stencil value by 1. If at zero, set to maximum.
@ kSetToReferenceValue
Reset the stencil value to the reference value.
@ kDecrementClamp
Decrement the current stencil value by 1. Clamp it to zero.
@ kZero
Reset the stencil value to zero.
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
@ kIncrementWrap
Increment the current stencil value by 1. If at maximum, set to zero.
@ kInvert
Perform a logical bitwise invert on the current stencil value.
@ kKeep
Don't modify the current stencil value.
constexpr MTLLoadAction ToMTLLoadAction(LoadAction action)
Definition: formats_mtl.h:336
constexpr MTLSamplerMipFilter ToMTLSamplerMipFilter(MipFilter filter)
Definition: formats_mtl.h:404
MTLPixelFormat SafeMTLPixelFormatBC1_RGBA()
Definition: formats_mtl.mm:148
MTLPixelFormat SafeMTLPixelFormatBC7_RGBAUnorm()
Definition: formats_mtl.mm:193
TextureType
Definition: formats.h:450
constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type)
Definition: formats_mtl.h:258
MTLPixelFormat SafeMTLPixelFormatBC3_RGBA()
Definition: formats_mtl.mm:166
MTLPixelFormat SafeMTLPixelFormatASTC_8x8_LDR()
Definition: formats_mtl.mm:255
MTLPixelFormat SafeMTLPixelFormatBGR10_XR()
Definition: formats_mtl.mm:130
MTLPixelFormat SafeMTLPixelFormatBC3_RGBA_sRGB()
Definition: formats_mtl.mm:175
MTLPixelFormat SafeMTLPixelFormatDepth24Unorm_Stencil8()
Definition: formats_mtl.mm:113
LoadAction
Definition: formats.h:390
StoreAction
Definition: formats.h:396
MTLPixelFormat SafeMTLPixelFormatBGRA10_XR()
Definition: formats_mtl.mm:138
constexpr MTLPrimitiveType ToMTLPrimitiveType(PrimitiveType type)
Definition: formats_mtl.h:207
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format)
Definition: formats_mtl.h:97
constexpr LoadAction FromMTLLoadAction(MTLLoadAction action)
Definition: formats_mtl.h:349
PolygonMode
Definition: formats.h:566
constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(SamplerAddressMode mode)
Definition: formats_mtl.h:416
constexpr MTLTextureType ToMTLTextureType(TextureType type)
Definition: formats_mtl.h:435
constexpr MTLTriangleFillMode ToMTLTriangleFillMode(PolygonMode mode)
Definition: formats_mtl.h:227
constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type)
Definition: formats_mtl.h:171
MinMagFilter
Describes how the texture should be sampled when the texture is being shrunk (minified) or expanded (...
Definition: formats.h:592
@ kNearest
Select nearest to the sample point. Most widely supported.
constexpr MTLIndexType ToMTLIndexType(IndexType type)
Definition: formats_mtl.h:237
MTLRenderPipelineColorAttachmentDescriptor * ToMTLRenderPipelineColorAttachmentDescriptor(ColorAttachmentDescriptor descriptor)
Definition: formats_mtl.mm:15
constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter)
Definition: formats_mtl.h:394
MTLPixelFormat SafeMTLPixelFormatBC1_RGBA_sRGB()
Definition: formats_mtl.mm:157
constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func)
Definition: formats_mtl.h:292
constexpr MTLStoreAction ToMTLStoreAction(StoreAction action)
Definition: formats_mtl.h:364
MTLPixelFormat SafeMTLPixelFormatASTC_8x8_sRGB()
Definition: formats_mtl.mm:262
MTLTextureDescriptor * ToMTLTextureDescriptor(const TextureDescriptor &desc)
Definition: formats_mtl.mm:86
BlendOperation
Definition: formats.h:384
MTLClearColor ToMTLClearColor(const Color &color)
Definition: formats_mtl.h:431
MTLPixelFormat SafeMTLPixelFormatBC5_RGUnorm()
Definition: formats_mtl.mm:184
MTLPixelFormat SafeMTLPixelFormatEAC_RGBA8_sRGB()
Definition: formats_mtl.mm:234
constexpr MTLCullMode ToMTLCullMode(CullMode mode)
Definition: formats_mtl.h:246
Scalar blue
Definition: color.h:138
Scalar alpha
Definition: color.h:143
Scalar red
Definition: color.h:128
Scalar green
Definition: color.h:133
#define VALIDATION_LOG
Definition: validation.h:91