Flutter Engine
The Flutter Engine
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"
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
76constexpr 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;
110 }
111 return MTLPixelFormatInvalid;
112};
113
114constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type) {
115 switch (type) {
117 return MTLBlendFactorZero;
119 return MTLBlendFactorOne;
121 return MTLBlendFactorSourceColor;
123 return MTLBlendFactorOneMinusSourceColor;
125 return MTLBlendFactorSourceAlpha;
127 return MTLBlendFactorOneMinusSourceAlpha;
129 return MTLBlendFactorDestinationColor;
131 return MTLBlendFactorOneMinusDestinationColor;
133 return MTLBlendFactorDestinationAlpha;
135 return MTLBlendFactorOneMinusDestinationAlpha;
137 return MTLBlendFactorSourceAlphaSaturated;
139 return MTLBlendFactorBlendColor;
141 return MTLBlendFactorOneMinusBlendColor;
143 return MTLBlendFactorBlendAlpha;
145 return MTLBlendFactorOneMinusBlendAlpha;
146 }
147 return MTLBlendFactorZero;
148};
149
150constexpr MTLPrimitiveType ToMTLPrimitiveType(PrimitiveType type) {
151 switch (type) {
153 return MTLPrimitiveTypeTriangle;
155 return MTLPrimitiveTypeTriangleStrip;
157 return MTLPrimitiveTypeLine;
159 return MTLPrimitiveTypeLineStrip;
161 return MTLPrimitiveTypePoint;
162 }
163 return MTLPrimitiveTypePoint;
164}
165
166constexpr MTLTriangleFillMode ToMTLTriangleFillMode(PolygonMode mode) {
167 switch (mode) {
169 return MTLTriangleFillModeFill;
171 return MTLTriangleFillModeLines;
172 }
173 return MTLTriangleFillModeFill;
174}
175
176constexpr MTLIndexType ToMTLIndexType(IndexType type) {
177 switch (type) {
179 return MTLIndexTypeUInt16;
180 default:
181 return MTLIndexTypeUInt32;
182 }
183}
184
185constexpr MTLCullMode ToMTLCullMode(CullMode mode) {
186 switch (mode) {
187 case CullMode::kNone:
188 return MTLCullModeNone;
190 return MTLCullModeBack;
192 return MTLCullModeFront;
193 }
194 return MTLCullModeNone;
195}
196
197constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type) {
198 switch (type) {
200 return MTLBlendOperationAdd;
202 return MTLBlendOperationSubtract;
204 return MTLBlendOperationReverseSubtract;
205 }
206 return MTLBlendOperationAdd;
207};
208
209constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type) {
210 MTLColorWriteMask mask = MTLColorWriteMaskNone;
211
213 mask |= MTLColorWriteMaskRed;
214 }
215
217 mask |= MTLColorWriteMaskGreen;
218 }
219
221 mask |= MTLColorWriteMaskBlue;
222 }
223
225 mask |= MTLColorWriteMaskAlpha;
226 }
227
228 return mask;
229};
230
231constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func) {
232 switch (func) {
234 return MTLCompareFunctionNever;
236 return MTLCompareFunctionLess;
238 return MTLCompareFunctionEqual;
240 return MTLCompareFunctionLessEqual;
242 return MTLCompareFunctionGreater;
244 return MTLCompareFunctionNotEqual;
246 return MTLCompareFunctionGreaterEqual;
248 return MTLCompareFunctionAlways;
249 }
250 return MTLCompareFunctionAlways;
251};
252
253constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op) {
254 switch (op) {
256 return MTLStencilOperationKeep;
258 return MTLStencilOperationZero;
260 return MTLStencilOperationReplace;
262 return MTLStencilOperationIncrementClamp;
264 return MTLStencilOperationDecrementClamp;
266 return MTLStencilOperationInvert;
268 return MTLStencilOperationIncrementWrap;
270 return MTLStencilOperationDecrementWrap;
271 }
272 return MTLStencilOperationKeep;
273};
274
275constexpr MTLLoadAction ToMTLLoadAction(LoadAction action) {
276 switch (action) {
278 return MTLLoadActionDontCare;
280 return MTLLoadActionLoad;
282 return MTLLoadActionClear;
283 }
284
285 return MTLLoadActionDontCare;
286}
287
288constexpr LoadAction FromMTLLoadAction(MTLLoadAction action) {
289 switch (action) {
290 case MTLLoadActionDontCare:
292 case MTLLoadActionLoad:
293 return LoadAction::kLoad;
294 case MTLLoadActionClear:
295 return LoadAction::kClear;
296 default:
297 break;
298 }
299
301}
302
303constexpr MTLStoreAction ToMTLStoreAction(StoreAction action) {
304 switch (action) {
306 return MTLStoreActionDontCare;
308 return MTLStoreActionStore;
310 return MTLStoreActionMultisampleResolve;
312 return MTLStoreActionStoreAndMultisampleResolve;
313 }
314 return MTLStoreActionDontCare;
315}
316
317constexpr StoreAction FromMTLStoreAction(MTLStoreAction action) {
318 switch (action) {
319 case MTLStoreActionDontCare:
321 case MTLStoreActionStore:
322 return StoreAction::kStore;
323 case MTLStoreActionMultisampleResolve:
325 case MTLStoreActionStoreAndMultisampleResolve:
327 default:
328 break;
329 }
331}
332
333constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter) {
334 switch (filter) {
336 return MTLSamplerMinMagFilterNearest;
338 return MTLSamplerMinMagFilterLinear;
339 }
340 return MTLSamplerMinMagFilterNearest;
341}
342
343constexpr MTLSamplerMipFilter ToMTLSamplerMipFilter(MipFilter filter) {
344 switch (filter) {
345 case MipFilter::kBase:
346 return MTLSamplerMipFilterNotMipmapped;
348 return MTLSamplerMipFilterNearest;
350 return MTLSamplerMipFilterLinear;
351 }
352 return MTLSamplerMipFilterNotMipmapped;
353}
354
355constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(
357 switch (mode) {
359 return MTLSamplerAddressModeClampToEdge;
361 return MTLSamplerAddressModeRepeat;
363 return MTLSamplerAddressModeMirrorRepeat;
365 return MTLSamplerAddressModeClampToZero;
366 }
367 return MTLSamplerAddressModeClampToEdge;
368}
369
370inline MTLClearColor ToMTLClearColor(const Color& color) {
371 return MTLClearColorMake(color.red, color.green, color.blue, color.alpha);
372}
373
374constexpr MTLTextureType ToMTLTextureType(TextureType type) {
375 switch (type) {
377 return MTLTextureType2D;
379 return MTLTextureType2DMultisample;
381 return MTLTextureTypeCube;
384 << "kTextureExternalOES can not be used with the Metal backend.";
385 }
386 return MTLTextureType2D;
387}
388
389MTLRenderPipelineColorAttachmentDescriptor*
391 ColorAttachmentDescriptor descriptor);
392
393MTLDepthStencilDescriptor* ToMTLDepthStencilDescriptor(
394 std::optional<DepthAttachmentDescriptor> depth,
395 std::optional<StencilAttachmentDescriptor> front,
396 std::optional<StencilAttachmentDescriptor> back);
397
398MTLTextureDescriptor* ToMTLTextureDescriptor(const TextureDescriptor& desc);
399
400} // namespace impeller
401
402#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_FORMATS_MTL_H_
GLenum type
DlColor color
uint32_t uint32_t * format
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
BlendFactor
Definition: formats.h:178
PrimitiveType
Decides how backend draws pixels based on input vertices.
Definition: formats.h:352
@ kPoint
Draws a point at each input vertex.
constexpr PixelFormat FromMTLPixelFormat(MTLPixelFormat format)
Definition: formats_mtl.h:22
MipFilter
Options for selecting and filtering between mipmap levels.
Definition: formats.h:419
@ 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.
constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type)
Definition: formats_mtl.h:209
constexpr StoreAction FromMTLStoreAction(MTLStoreAction action)
Definition: formats_mtl.h:317
SamplerAddressMode
Definition: formats.h:435
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op)
Definition: formats_mtl.h:253
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:546
@ 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
StencilOperation
Definition: formats.h:565
@ 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:275
constexpr MTLSamplerMipFilter ToMTLSamplerMipFilter(MipFilter filter)
Definition: formats_mtl.h:343
TextureType
Definition: formats.h:262
constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type)
Definition: formats_mtl.h:197
MTLPixelFormat SafeMTLPixelFormatBGR10_XR()
Definition: formats_mtl.mm:130
MTLPixelFormat SafeMTLPixelFormatDepth24Unorm_Stencil8()
Definition: formats_mtl.mm:113
LoadAction
Definition: formats.h:202
StoreAction
Definition: formats.h:208
MTLPixelFormat SafeMTLPixelFormatBGRA10_XR()
Definition: formats_mtl.mm:138
constexpr MTLPrimitiveType ToMTLPrimitiveType(PrimitiveType type)
Definition: formats_mtl.h:150
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format)
Definition: formats_mtl.h:76
constexpr LoadAction FromMTLLoadAction(MTLLoadAction action)
Definition: formats_mtl.h:288
PolygonMode
Definition: formats.h:383
constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(SamplerAddressMode mode)
Definition: formats_mtl.h:355
constexpr MTLTextureType ToMTLTextureType(TextureType type)
Definition: formats_mtl.h:374
MinMagFilter
Describes how the texture should be sampled when the texture is being shrunk (minified) or expanded (...
Definition: formats.h:409
@ kNearest
Select nearest to the sample point. Most widely supported.
constexpr MTLTriangleFillMode ToMTLTriangleFillMode(PolygonMode mode)
Definition: formats_mtl.h:166
constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type)
Definition: formats_mtl.h:114
constexpr MTLIndexType ToMTLIndexType(IndexType type)
Definition: formats_mtl.h:176
MTLRenderPipelineColorAttachmentDescriptor * ToMTLRenderPipelineColorAttachmentDescriptor(ColorAttachmentDescriptor descriptor)
Definition: formats_mtl.mm:15
constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter)
Definition: formats_mtl.h:333
constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func)
Definition: formats_mtl.h:231
constexpr MTLStoreAction ToMTLStoreAction(StoreAction action)
Definition: formats_mtl.h:303
MTLTextureDescriptor * ToMTLTextureDescriptor(const TextureDescriptor &desc)
Definition: formats_mtl.mm:86
BlendOperation
Definition: formats.h:196
MTLClearColor ToMTLClearColor(const Color &color)
Definition: formats_mtl.h:370
constexpr MTLCullMode ToMTLCullMode(CullMode mode)
Definition: formats_mtl.h:185
#define VALIDATION_LOG
Definition: validation.h:73