Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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
17
18namespace impeller {
19
20class RenderTarget;
21
22constexpr PixelFormat FromMTLPixelFormat(MTLPixelFormat format) {
23 switch (format) {
24 case MTLPixelFormatInvalid:
26 case MTLPixelFormatBGRA8Unorm:
28 case MTLPixelFormatBGRA8Unorm_sRGB:
30 case MTLPixelFormatRGBA8Unorm:
32 case MTLPixelFormatRGBA8Unorm_sRGB:
34 case MTLPixelFormatRGBA32Float:
36 case MTLPixelFormatRGBA16Float:
38 case MTLPixelFormatStencil8:
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:
54 }
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.
66MTLPixelFormat SafeMTLPixelFormatBGR10_XR_sRGB();
67
68/// Safe accessor for MTLPixelFormatBGR10_XR.
69/// Returns PixelFormat::kUnknown if MTLPixelFormatBGR10_XR isn't supported.
70MTLPixelFormat SafeMTLPixelFormatBGR10_XR();
71
72/// Safe accessor for MTLPixelFormatBGRA10_XR.
73/// Returns PixelFormat::kUnknown if MTLPixelFormatBGR10_XR isn't supported.
74MTLPixelFormat 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+).
79MTLPixelFormat SafeMTLPixelFormatBC1_RGBA();
80MTLPixelFormat SafeMTLPixelFormatBC1_RGBA_sRGB();
81MTLPixelFormat SafeMTLPixelFormatBC3_RGBA();
82MTLPixelFormat SafeMTLPixelFormatBC3_RGBA_sRGB();
83MTLPixelFormat SafeMTLPixelFormatBC5_RGUnorm();
84MTLPixelFormat SafeMTLPixelFormatBC7_RGBAUnorm();
86MTLPixelFormat SafeMTLPixelFormatETC2_RGB8();
88MTLPixelFormat SafeMTLPixelFormatEAC_RGBA8();
90MTLPixelFormat SafeMTLPixelFormatASTC_4x4_LDR();
91MTLPixelFormat SafeMTLPixelFormatASTC_4x4_sRGB();
92MTLPixelFormat SafeMTLPixelFormatASTC_8x8_LDR();
93MTLPixelFormat SafeMTLPixelFormatASTC_8x8_sRGB();
94MTLPixelFormat SafeMTLPixelFormatASTC_4x4_HDR();
95MTLPixelFormat SafeMTLPixelFormatASTC_8x8_HDR();
96
97constexpr 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
171constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type) {
172 switch (type) {
174 return MTLBlendFactorZero;
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
207constexpr 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
227constexpr MTLTriangleFillMode ToMTLTriangleFillMode(PolygonMode mode) {
228 switch (mode) {
230 return MTLTriangleFillModeFill;
232 return MTLTriangleFillModeLines;
233 }
234 return MTLTriangleFillModeFill;
235}
236
237constexpr MTLIndexType ToMTLIndexType(IndexType type) {
238 switch (type) {
240 return MTLIndexTypeUInt16;
241 default:
242 return MTLIndexTypeUInt32;
243 }
244}
245
246constexpr MTLCullMode ToMTLCullMode(CullMode mode) {
247 switch (mode) {
248 case CullMode::kNone:
249 return MTLCullModeNone;
251 return MTLCullModeBack;
253 return MTLCullModeFront;
254 }
255 return MTLCullModeNone;
256}
257
258constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type) {
259 switch (type) {
261 return MTLBlendOperationAdd;
263 return MTLBlendOperationSubtract;
265 return MTLBlendOperationReverseSubtract;
266 }
267 return MTLBlendOperationAdd;
268};
269
270constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type) {
271 MTLColorWriteMask mask = MTLColorWriteMaskNone;
272
274 mask |= MTLColorWriteMaskRed;
275 }
276
278 mask |= MTLColorWriteMaskGreen;
279 }
280
282 mask |= MTLColorWriteMaskBlue;
283 }
284
286 mask |= MTLColorWriteMaskAlpha;
287 }
288
289 return mask;
290};
291
292constexpr 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
314constexpr 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
336constexpr MTLLoadAction ToMTLLoadAction(LoadAction action) {
337 switch (action) {
339 return MTLLoadActionDontCare;
341 return MTLLoadActionLoad;
343 return MTLLoadActionClear;
344 }
345
346 return MTLLoadActionDontCare;
347}
348
349constexpr LoadAction FromMTLLoadAction(MTLLoadAction action) {
350 switch (action) {
351 case MTLLoadActionDontCare:
353 case MTLLoadActionLoad:
354 return LoadAction::kLoad;
355 case MTLLoadActionClear:
356 return LoadAction::kClear;
357 default:
358 break;
359 }
360
362}
363
364constexpr MTLStoreAction ToMTLStoreAction(StoreAction action) {
365 switch (action) {
367 return MTLStoreActionDontCare;
369 return MTLStoreActionStore;
371 return MTLStoreActionMultisampleResolve;
373 return MTLStoreActionStoreAndMultisampleResolve;
374 }
375 return MTLStoreActionDontCare;
376}
377
378constexpr StoreAction FromMTLStoreAction(MTLStoreAction action) {
379 switch (action) {
380 case MTLStoreActionDontCare:
382 case MTLStoreActionStore:
383 return StoreAction::kStore;
384 case MTLStoreActionMultisampleResolve:
386 case MTLStoreActionStoreAndMultisampleResolve:
388 default:
389 break;
390 }
392}
393
394constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter) {
395 switch (filter) {
397 return MTLSamplerMinMagFilterNearest;
399 return MTLSamplerMinMagFilterLinear;
400 }
401 return MTLSamplerMinMagFilterNearest;
402}
403
404constexpr MTLSamplerMipFilter ToMTLSamplerMipFilter(MipFilter filter) {
405 switch (filter) {
406 case MipFilter::kBase:
407 return MTLSamplerMipFilterNotMipmapped;
409 return MTLSamplerMipFilterNearest;
411 return MTLSamplerMipFilterLinear;
412 }
413 return MTLSamplerMipFilterNotMipmapped;
414}
415
416constexpr 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
431inline MTLClearColor ToMTLClearColor(const Color& color) {
432 return MTLClearColorMake(color.red, color.green, color.blue, color.alpha);
433}
434
435constexpr 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
450MTLRenderPipelineColorAttachmentDescriptor*
452 ColorAttachmentDescriptor descriptor);
453
454MTLDepthStencilDescriptor* ToMTLDepthStencilDescriptor(
455 std::optional<DepthAttachmentDescriptor> depth,
456 std::optional<StencilAttachmentDescriptor> front,
457 std::optional<StencilAttachmentDescriptor> back);
458
459MTLTextureDescriptor* ToMTLTextureDescriptor(const TextureDescriptor& desc);
460
461} // namespace impeller
462
463#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_FORMATS_MTL_H_
uint32_t uint32_t * format
MTLPixelFormat SafeMTLPixelFormatETC2_RGB8_sRGB()
MTLPixelFormat SafeMTLPixelFormatEAC_RGBA8()
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()
MTLPixelFormat SafeMTLPixelFormatETC2_RGB8()
SamplerAddressMode
Definition formats.h:618
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
MTLPixelFormat SafeMTLPixelFormatASTC_4x4_sRGB()
constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type)
constexpr StoreAction FromMTLStoreAction(MTLStoreAction action)
MTLPixelFormat SafeMTLPixelFormatASTC_4x4_LDR()
MTLPixelFormat SafeMTLPixelFormatASTC_8x8_HDR()
constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op)
MTLPixelFormat SafeMTLPixelFormatBC7_RGBAUnorm_sRGB()
MTLPixelFormat SafeMTLPixelFormatBGR10_XR_sRGB()
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)
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)
constexpr MTLSamplerMipFilter ToMTLSamplerMipFilter(MipFilter filter)
MTLPixelFormat SafeMTLPixelFormatBC1_RGBA()
MTLPixelFormat SafeMTLPixelFormatBC7_RGBAUnorm()
constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type)
MTLPixelFormat SafeMTLPixelFormatBC3_RGBA()
MTLPixelFormat SafeMTLPixelFormatASTC_8x8_LDR()
MTLPixelFormat SafeMTLPixelFormatBGR10_XR()
MTLPixelFormat SafeMTLPixelFormatBC3_RGBA_sRGB()
MTLPixelFormat SafeMTLPixelFormatDepth24Unorm_Stencil8()
MTLPixelFormat SafeMTLPixelFormatBGRA10_XR()
constexpr MTLPrimitiveType ToMTLPrimitiveType(PrimitiveType type)
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format)
Definition formats_mtl.h:97
constexpr LoadAction FromMTLLoadAction(MTLLoadAction action)
constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(SamplerAddressMode mode)
constexpr MTLTextureType ToMTLTextureType(TextureType type)
constexpr MTLTriangleFillMode ToMTLTriangleFillMode(PolygonMode mode)
constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type)
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)
MTLRenderPipelineColorAttachmentDescriptor * ToMTLRenderPipelineColorAttachmentDescriptor(ColorAttachmentDescriptor descriptor)
constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter)
MTLPixelFormat SafeMTLPixelFormatBC1_RGBA_sRGB()
constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func)
constexpr MTLStoreAction ToMTLStoreAction(StoreAction action)
MTLPixelFormat SafeMTLPixelFormatASTC_8x8_sRGB()
MTLTextureDescriptor * ToMTLTextureDescriptor(const TextureDescriptor &desc)
BlendOperation
Definition formats.h:384
MTLClearColor ToMTLClearColor(const Color &color)
MTLPixelFormat SafeMTLPixelFormatBC5_RGUnorm()
MTLPixelFormat SafeMTLPixelFormatEAC_RGBA8_sRGB()
constexpr MTLCullMode ToMTLCullMode(CullMode mode)
impeller::ShaderType type
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