Flutter Engine
 
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
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;
163 // Callers are expected to perform a capability check for triangle fan
164 // support.
165 return MTLPrimitiveTypePoint;
166 }
167 return MTLPrimitiveTypePoint;
168}
169
170constexpr MTLTriangleFillMode ToMTLTriangleFillMode(PolygonMode mode) {
171 switch (mode) {
173 return MTLTriangleFillModeFill;
175 return MTLTriangleFillModeLines;
176 }
177 return MTLTriangleFillModeFill;
178}
179
180constexpr MTLIndexType ToMTLIndexType(IndexType type) {
181 switch (type) {
183 return MTLIndexTypeUInt16;
184 default:
185 return MTLIndexTypeUInt32;
186 }
187}
188
189constexpr MTLCullMode ToMTLCullMode(CullMode mode) {
190 switch (mode) {
191 case CullMode::kNone:
192 return MTLCullModeNone;
194 return MTLCullModeBack;
196 return MTLCullModeFront;
197 }
198 return MTLCullModeNone;
199}
200
201constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type) {
202 switch (type) {
204 return MTLBlendOperationAdd;
206 return MTLBlendOperationSubtract;
208 return MTLBlendOperationReverseSubtract;
209 }
210 return MTLBlendOperationAdd;
211};
212
213constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type) {
214 MTLColorWriteMask mask = MTLColorWriteMaskNone;
215
217 mask |= MTLColorWriteMaskRed;
218 }
219
221 mask |= MTLColorWriteMaskGreen;
222 }
223
225 mask |= MTLColorWriteMaskBlue;
226 }
227
229 mask |= MTLColorWriteMaskAlpha;
230 }
231
232 return mask;
233};
234
235constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func) {
236 switch (func) {
238 return MTLCompareFunctionNever;
240 return MTLCompareFunctionLess;
242 return MTLCompareFunctionEqual;
244 return MTLCompareFunctionLessEqual;
246 return MTLCompareFunctionGreater;
248 return MTLCompareFunctionNotEqual;
250 return MTLCompareFunctionGreaterEqual;
252 return MTLCompareFunctionAlways;
253 }
254 return MTLCompareFunctionAlways;
255};
256
257constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op) {
258 switch (op) {
260 return MTLStencilOperationKeep;
262 return MTLStencilOperationZero;
264 return MTLStencilOperationReplace;
266 return MTLStencilOperationIncrementClamp;
268 return MTLStencilOperationDecrementClamp;
270 return MTLStencilOperationInvert;
272 return MTLStencilOperationIncrementWrap;
274 return MTLStencilOperationDecrementWrap;
275 }
276 return MTLStencilOperationKeep;
277};
278
279constexpr MTLLoadAction ToMTLLoadAction(LoadAction action) {
280 switch (action) {
282 return MTLLoadActionDontCare;
284 return MTLLoadActionLoad;
286 return MTLLoadActionClear;
287 }
288
289 return MTLLoadActionDontCare;
290}
291
292constexpr LoadAction FromMTLLoadAction(MTLLoadAction action) {
293 switch (action) {
294 case MTLLoadActionDontCare:
296 case MTLLoadActionLoad:
297 return LoadAction::kLoad;
298 case MTLLoadActionClear:
299 return LoadAction::kClear;
300 default:
301 break;
302 }
303
305}
306
307constexpr MTLStoreAction ToMTLStoreAction(StoreAction action) {
308 switch (action) {
310 return MTLStoreActionDontCare;
312 return MTLStoreActionStore;
314 return MTLStoreActionMultisampleResolve;
316 return MTLStoreActionStoreAndMultisampleResolve;
317 }
318 return MTLStoreActionDontCare;
319}
320
321constexpr StoreAction FromMTLStoreAction(MTLStoreAction action) {
322 switch (action) {
323 case MTLStoreActionDontCare:
325 case MTLStoreActionStore:
326 return StoreAction::kStore;
327 case MTLStoreActionMultisampleResolve:
329 case MTLStoreActionStoreAndMultisampleResolve:
331 default:
332 break;
333 }
335}
336
337constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter) {
338 switch (filter) {
340 return MTLSamplerMinMagFilterNearest;
342 return MTLSamplerMinMagFilterLinear;
343 }
344 return MTLSamplerMinMagFilterNearest;
345}
346
347constexpr MTLSamplerMipFilter ToMTLSamplerMipFilter(MipFilter filter) {
348 switch (filter) {
349 case MipFilter::kBase:
350 return MTLSamplerMipFilterNotMipmapped;
352 return MTLSamplerMipFilterNearest;
354 return MTLSamplerMipFilterLinear;
355 }
356 return MTLSamplerMipFilterNotMipmapped;
357}
358
359constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(
360 SamplerAddressMode mode) {
361 switch (mode) {
363 return MTLSamplerAddressModeClampToEdge;
365 return MTLSamplerAddressModeRepeat;
367 return MTLSamplerAddressModeMirrorRepeat;
369 return MTLSamplerAddressModeClampToZero;
370 }
371 return MTLSamplerAddressModeClampToEdge;
372}
373
374inline MTLClearColor ToMTLClearColor(const Color& color) {
375 return MTLClearColorMake(color.red, color.green, color.blue, color.alpha);
376}
377
378constexpr MTLTextureType ToMTLTextureType(TextureType type) {
379 switch (type) {
381 return MTLTextureType2D;
383 return MTLTextureType2DMultisample;
385 return MTLTextureTypeCube;
388 << "kTextureExternalOES can not be used with the Metal backend.";
389 }
390 return MTLTextureType2D;
391}
392
393MTLRenderPipelineColorAttachmentDescriptor*
395 ColorAttachmentDescriptor descriptor);
396
397MTLDepthStencilDescriptor* ToMTLDepthStencilDescriptor(
398 std::optional<DepthAttachmentDescriptor> depth,
399 std::optional<StencilAttachmentDescriptor> front,
400 std::optional<StencilAttachmentDescriptor> back);
401
402MTLTextureDescriptor* ToMTLTextureDescriptor(const TextureDescriptor& desc);
403
404} // namespace impeller
405
406#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_FORMATS_MTL_H_
GLenum type
uint32_t uint32_t * format
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
SamplerAddressMode
Definition formats.h:441
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type)
constexpr StoreAction FromMTLStoreAction(MTLStoreAction action)
constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op)
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:552
@ 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:425
@ 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:571
@ 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)
constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type)
MTLPixelFormat SafeMTLPixelFormatBGR10_XR()
MTLPixelFormat SafeMTLPixelFormatDepth24Unorm_Stencil8()
MTLPixelFormat SafeMTLPixelFormatBGRA10_XR()
constexpr MTLPrimitiveType ToMTLPrimitiveType(PrimitiveType type)
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format)
Definition formats_mtl.h:76
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:415
@ kNearest
Select nearest to the sample point. Most widely supported.
constexpr MTLIndexType ToMTLIndexType(IndexType type)
MTLRenderPipelineColorAttachmentDescriptor * ToMTLRenderPipelineColorAttachmentDescriptor(ColorAttachmentDescriptor descriptor)
constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter)
constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func)
constexpr MTLStoreAction ToMTLStoreAction(StoreAction action)
MTLTextureDescriptor * ToMTLTextureDescriptor(const TextureDescriptor &desc)
BlendOperation
Definition formats.h:196
MTLClearColor ToMTLClearColor(const Color &color)
constexpr MTLCullMode ToMTLCullMode(CullMode mode)
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