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 constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format) {
77  switch (format) {
79  return MTLPixelFormatInvalid;
81  return MTLPixelFormatA8Unorm;
83  return MTLPixelFormatR8Unorm;
85  return MTLPixelFormatRG8Unorm;
87  return MTLPixelFormatBGRA8Unorm;
89  return MTLPixelFormatBGRA8Unorm_sRGB;
91  return MTLPixelFormatRGBA8Unorm;
93  return MTLPixelFormatRGBA8Unorm_sRGB;
95  return MTLPixelFormatRGBA32Float;
97  return MTLPixelFormatRGBA16Float;
99  return MTLPixelFormatStencil8;
103  return MTLPixelFormatDepth32Float_Stencil8;
111  return MTLPixelFormatR32Float;
112  }
113  return MTLPixelFormatInvalid;
114 };
115 
116 constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type) {
117  switch (type) {
118  case BlendFactor::kZero:
119  return MTLBlendFactorZero;
120  case BlendFactor::kOne:
121  return MTLBlendFactorOne;
123  return MTLBlendFactorSourceColor;
125  return MTLBlendFactorOneMinusSourceColor;
127  return MTLBlendFactorSourceAlpha;
129  return MTLBlendFactorOneMinusSourceAlpha;
131  return MTLBlendFactorDestinationColor;
133  return MTLBlendFactorOneMinusDestinationColor;
135  return MTLBlendFactorDestinationAlpha;
137  return MTLBlendFactorOneMinusDestinationAlpha;
139  return MTLBlendFactorSourceAlphaSaturated;
141  return MTLBlendFactorBlendColor;
143  return MTLBlendFactorOneMinusBlendColor;
145  return MTLBlendFactorBlendAlpha;
147  return MTLBlendFactorOneMinusBlendAlpha;
148  }
149  return MTLBlendFactorZero;
150 };
151 
152 constexpr MTLPrimitiveType ToMTLPrimitiveType(PrimitiveType type) {
153  switch (type) {
155  return MTLPrimitiveTypeTriangle;
157  return MTLPrimitiveTypeTriangleStrip;
159  return MTLPrimitiveTypeLine;
161  return MTLPrimitiveTypeLineStrip;
163  return MTLPrimitiveTypePoint;
165  // Callers are expected to perform a capability check for triangle fan
166  // support.
167  return MTLPrimitiveTypePoint;
168  }
169  return MTLPrimitiveTypePoint;
170 }
171 
172 constexpr MTLTriangleFillMode ToMTLTriangleFillMode(PolygonMode mode) {
173  switch (mode) {
174  case PolygonMode::kFill:
175  return MTLTriangleFillModeFill;
176  case PolygonMode::kLine:
177  return MTLTriangleFillModeLines;
178  }
179  return MTLTriangleFillModeFill;
180 }
181 
182 constexpr MTLIndexType ToMTLIndexType(IndexType type) {
183  switch (type) {
184  case IndexType::k16bit:
185  return MTLIndexTypeUInt16;
186  default:
187  return MTLIndexTypeUInt32;
188  }
189 }
190 
191 constexpr MTLCullMode ToMTLCullMode(CullMode mode) {
192  switch (mode) {
193  case CullMode::kNone:
194  return MTLCullModeNone;
195  case CullMode::kBackFace:
196  return MTLCullModeBack;
198  return MTLCullModeFront;
199  }
200  return MTLCullModeNone;
201 }
202 
203 constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type) {
204  switch (type) {
206  return MTLBlendOperationAdd;
208  return MTLBlendOperationSubtract;
210  return MTLBlendOperationReverseSubtract;
211  }
212  return MTLBlendOperationAdd;
213 };
214 
215 constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type) {
216  MTLColorWriteMask mask = MTLColorWriteMaskNone;
217 
219  mask |= MTLColorWriteMaskRed;
220  }
221 
223  mask |= MTLColorWriteMaskGreen;
224  }
225 
227  mask |= MTLColorWriteMaskBlue;
228  }
229 
231  mask |= MTLColorWriteMaskAlpha;
232  }
233 
234  return mask;
235 };
236 
237 constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func) {
238  switch (func) {
240  return MTLCompareFunctionNever;
242  return MTLCompareFunctionLess;
244  return MTLCompareFunctionEqual;
246  return MTLCompareFunctionLessEqual;
248  return MTLCompareFunctionGreater;
250  return MTLCompareFunctionNotEqual;
252  return MTLCompareFunctionGreaterEqual;
254  return MTLCompareFunctionAlways;
255  }
256  return MTLCompareFunctionAlways;
257 };
258 
259 constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op) {
260  switch (op) {
262  return MTLStencilOperationKeep;
264  return MTLStencilOperationZero;
266  return MTLStencilOperationReplace;
268  return MTLStencilOperationIncrementClamp;
270  return MTLStencilOperationDecrementClamp;
272  return MTLStencilOperationInvert;
274  return MTLStencilOperationIncrementWrap;
276  return MTLStencilOperationDecrementWrap;
277  }
278  return MTLStencilOperationKeep;
279 };
280 
281 constexpr MTLLoadAction ToMTLLoadAction(LoadAction action) {
282  switch (action) {
284  return MTLLoadActionDontCare;
285  case LoadAction::kLoad:
286  return MTLLoadActionLoad;
287  case LoadAction::kClear:
288  return MTLLoadActionClear;
289  }
290 
291  return MTLLoadActionDontCare;
292 }
293 
294 constexpr LoadAction FromMTLLoadAction(MTLLoadAction action) {
295  switch (action) {
296  case MTLLoadActionDontCare:
297  return LoadAction::kDontCare;
298  case MTLLoadActionLoad:
299  return LoadAction::kLoad;
300  case MTLLoadActionClear:
301  return LoadAction::kClear;
302  default:
303  break;
304  }
305 
306  return LoadAction::kDontCare;
307 }
308 
309 constexpr MTLStoreAction ToMTLStoreAction(StoreAction action) {
310  switch (action) {
312  return MTLStoreActionDontCare;
313  case StoreAction::kStore:
314  return MTLStoreActionStore;
316  return MTLStoreActionMultisampleResolve;
318  return MTLStoreActionStoreAndMultisampleResolve;
319  }
320  return MTLStoreActionDontCare;
321 }
322 
323 constexpr StoreAction FromMTLStoreAction(MTLStoreAction action) {
324  switch (action) {
325  case MTLStoreActionDontCare:
326  return StoreAction::kDontCare;
327  case MTLStoreActionStore:
328  return StoreAction::kStore;
329  case MTLStoreActionMultisampleResolve:
331  case MTLStoreActionStoreAndMultisampleResolve:
333  default:
334  break;
335  }
336  return StoreAction::kDontCare;
337 }
338 
339 constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter) {
340  switch (filter) {
342  return MTLSamplerMinMagFilterNearest;
344  return MTLSamplerMinMagFilterLinear;
345  }
346  return MTLSamplerMinMagFilterNearest;
347 }
348 
349 constexpr MTLSamplerMipFilter ToMTLSamplerMipFilter(MipFilter filter) {
350  switch (filter) {
351  case MipFilter::kBase:
352  return MTLSamplerMipFilterNotMipmapped;
353  case MipFilter::kNearest:
354  return MTLSamplerMipFilterNearest;
355  case MipFilter::kLinear:
356  return MTLSamplerMipFilterLinear;
357  }
358  return MTLSamplerMipFilterNotMipmapped;
359 }
360 
361 constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(
362  SamplerAddressMode mode) {
363  switch (mode) {
365  return MTLSamplerAddressModeClampToEdge;
367  return MTLSamplerAddressModeRepeat;
369  return MTLSamplerAddressModeMirrorRepeat;
371  return MTLSamplerAddressModeClampToZero;
372  }
373  return MTLSamplerAddressModeClampToEdge;
374 }
375 
376 inline MTLClearColor ToMTLClearColor(const Color& color) {
377  return MTLClearColorMake(color.red, color.green, color.blue, color.alpha);
378 }
379 
380 constexpr MTLTextureType ToMTLTextureType(TextureType type) {
381  switch (type) {
383  return MTLTextureType2D;
385  return MTLTextureType2DMultisample;
387  return MTLTextureTypeCube;
390  << "kTextureExternalOES can not be used with the Metal backend.";
391  }
392  return MTLTextureType2D;
393 }
394 
395 MTLRenderPipelineColorAttachmentDescriptor*
397  ColorAttachmentDescriptor descriptor);
398 
399 MTLDepthStencilDescriptor* ToMTLDepthStencilDescriptor(
400  std::optional<DepthAttachmentDescriptor> depth,
401  std::optional<StencilAttachmentDescriptor> front,
402  std::optional<StencilAttachmentDescriptor> back);
403 
404 MTLTextureDescriptor* ToMTLTextureDescriptor(const TextureDescriptor& desc);
405 
406 } // namespace impeller
407 
408 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_FORMATS_MTL_H_
GLenum type
BlendFactor
Definition: formats.h:181
PrimitiveType
Decides how backend draws pixels based on input vertices.
Definition: formats.h:355
@ kPoint
Draws a point at each input vertex.
constexpr PixelFormat FromMTLPixelFormat(MTLPixelFormat format)
Definition: formats_mtl.h:22
SamplerAddressMode
Definition: formats.h:444
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type)
Definition: formats_mtl.h:215
constexpr StoreAction FromMTLStoreAction(MTLStoreAction action)
Definition: formats_mtl.h:323
constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op)
Definition: formats_mtl.h:259
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:556
@ 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:428
@ 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:575
@ 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:281
constexpr MTLSamplerMipFilter ToMTLSamplerMipFilter(MipFilter filter)
Definition: formats_mtl.h:349
TextureType
Definition: formats.h:265
constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type)
Definition: formats_mtl.h:203
MTLPixelFormat SafeMTLPixelFormatBGR10_XR()
Definition: formats_mtl.mm:130
MTLPixelFormat SafeMTLPixelFormatDepth24Unorm_Stencil8()
Definition: formats_mtl.mm:113
LoadAction
Definition: formats.h:205
StoreAction
Definition: formats.h:211
MTLPixelFormat SafeMTLPixelFormatBGRA10_XR()
Definition: formats_mtl.mm:138
constexpr MTLPrimitiveType ToMTLPrimitiveType(PrimitiveType type)
Definition: formats_mtl.h:152
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format)
Definition: formats_mtl.h:76
constexpr LoadAction FromMTLLoadAction(MTLLoadAction action)
Definition: formats_mtl.h:294
PolygonMode
Definition: formats.h:392
constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(SamplerAddressMode mode)
Definition: formats_mtl.h:361
constexpr MTLTextureType ToMTLTextureType(TextureType type)
Definition: formats_mtl.h:380
constexpr MTLTriangleFillMode ToMTLTriangleFillMode(PolygonMode mode)
Definition: formats_mtl.h:172
constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type)
Definition: formats_mtl.h:116
MinMagFilter
Describes how the texture should be sampled when the texture is being shrunk (minified) or expanded (...
Definition: formats.h:418
@ kNearest
Select nearest to the sample point. Most widely supported.
constexpr MTLIndexType ToMTLIndexType(IndexType type)
Definition: formats_mtl.h:182
MTLRenderPipelineColorAttachmentDescriptor * ToMTLRenderPipelineColorAttachmentDescriptor(ColorAttachmentDescriptor descriptor)
Definition: formats_mtl.mm:15
constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter)
Definition: formats_mtl.h:339
constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func)
Definition: formats_mtl.h:237
constexpr MTLStoreAction ToMTLStoreAction(StoreAction action)
Definition: formats_mtl.h:309
MTLTextureDescriptor * ToMTLTextureDescriptor(const TextureDescriptor &desc)
Definition: formats_mtl.mm:86
BlendOperation
Definition: formats.h:199
MTLClearColor ToMTLClearColor(const Color &color)
Definition: formats_mtl.h:376
constexpr MTLCullMode ToMTLCullMode(CullMode mode)
Definition: formats_mtl.h:191
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