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
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;
111 return MTLPixelFormatR32Float;
112 }
113 return MTLPixelFormatInvalid;
114};
115
116constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type) {
117 switch (type) {
119 return MTLBlendFactorZero;
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
152constexpr 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
172constexpr MTLTriangleFillMode ToMTLTriangleFillMode(PolygonMode mode) {
173 switch (mode) {
175 return MTLTriangleFillModeFill;
177 return MTLTriangleFillModeLines;
178 }
179 return MTLTriangleFillModeFill;
180}
181
182constexpr MTLIndexType ToMTLIndexType(IndexType type) {
183 switch (type) {
185 return MTLIndexTypeUInt16;
186 default:
187 return MTLIndexTypeUInt32;
188 }
189}
190
191constexpr MTLCullMode ToMTLCullMode(CullMode mode) {
192 switch (mode) {
193 case CullMode::kNone:
194 return MTLCullModeNone;
196 return MTLCullModeBack;
198 return MTLCullModeFront;
199 }
200 return MTLCullModeNone;
201}
202
203constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type) {
204 switch (type) {
206 return MTLBlendOperationAdd;
208 return MTLBlendOperationSubtract;
210 return MTLBlendOperationReverseSubtract;
211 }
212 return MTLBlendOperationAdd;
213};
214
215constexpr 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
237constexpr 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
259constexpr 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
281constexpr MTLLoadAction ToMTLLoadAction(LoadAction action) {
282 switch (action) {
284 return MTLLoadActionDontCare;
286 return MTLLoadActionLoad;
288 return MTLLoadActionClear;
289 }
290
291 return MTLLoadActionDontCare;
292}
293
294constexpr LoadAction FromMTLLoadAction(MTLLoadAction action) {
295 switch (action) {
296 case MTLLoadActionDontCare:
298 case MTLLoadActionLoad:
299 return LoadAction::kLoad;
300 case MTLLoadActionClear:
301 return LoadAction::kClear;
302 default:
303 break;
304 }
305
307}
308
309constexpr MTLStoreAction ToMTLStoreAction(StoreAction action) {
310 switch (action) {
312 return MTLStoreActionDontCare;
314 return MTLStoreActionStore;
316 return MTLStoreActionMultisampleResolve;
318 return MTLStoreActionStoreAndMultisampleResolve;
319 }
320 return MTLStoreActionDontCare;
321}
322
323constexpr StoreAction FromMTLStoreAction(MTLStoreAction action) {
324 switch (action) {
325 case MTLStoreActionDontCare:
327 case MTLStoreActionStore:
328 return StoreAction::kStore;
329 case MTLStoreActionMultisampleResolve:
331 case MTLStoreActionStoreAndMultisampleResolve:
333 default:
334 break;
335 }
337}
338
339constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter) {
340 switch (filter) {
342 return MTLSamplerMinMagFilterNearest;
344 return MTLSamplerMinMagFilterLinear;
345 }
346 return MTLSamplerMinMagFilterNearest;
347}
348
349constexpr MTLSamplerMipFilter ToMTLSamplerMipFilter(MipFilter filter) {
350 switch (filter) {
351 case MipFilter::kBase:
352 return MTLSamplerMipFilterNotMipmapped;
354 return MTLSamplerMipFilterNearest;
356 return MTLSamplerMipFilterLinear;
357 }
358 return MTLSamplerMipFilterNotMipmapped;
359}
360
361constexpr 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
376inline MTLClearColor ToMTLClearColor(const Color& color) {
377 return MTLClearColorMake(color.red, color.green, color.blue, color.alpha);
378}
379
380constexpr 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
395MTLRenderPipelineColorAttachmentDescriptor*
397 ColorAttachmentDescriptor descriptor);
398
399MTLDepthStencilDescriptor* ToMTLDepthStencilDescriptor(
400 std::optional<DepthAttachmentDescriptor> depth,
401 std::optional<StencilAttachmentDescriptor> front,
402 std::optional<StencilAttachmentDescriptor> back);
403
404MTLTextureDescriptor* ToMTLTextureDescriptor(const TextureDescriptor& desc);
405
406} // namespace impeller
407
408#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: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)
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: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)
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)
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:418
@ 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:199
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