Flutter Impeller
formats_mtl.mm
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 
6 #include <Metal/Metal.h>
7 
8 #include <memory>
9 
11 
12 namespace impeller {
13 
14 MTLRenderPipelineColorAttachmentDescriptor*
16  ColorAttachmentDescriptor descriptor) {
17  auto des = [[MTLRenderPipelineColorAttachmentDescriptor alloc] init];
18  des.pixelFormat = ToMTLPixelFormat(descriptor.format);
19 
20  des.blendingEnabled = descriptor.blending_enabled;
21 
22  des.sourceRGBBlendFactor =
24  des.rgbBlendOperation = ToMTLBlendOperation(descriptor.color_blend_op);
25  des.destinationRGBBlendFactor =
27 
28  des.sourceAlphaBlendFactor =
30  des.alphaBlendOperation = ToMTLBlendOperation(descriptor.alpha_blend_op);
31  des.destinationAlphaBlendFactor =
33 
34  des.writeMask = ToMTLColorWriteMask(descriptor.write_mask);
35  return des;
36 }
37 
38 MTLStencilDescriptor* ToMTLStencilDescriptor(
39  const StencilAttachmentDescriptor& descriptor) {
40  auto des = [[MTLStencilDescriptor alloc] init];
41  des.stencilCompareFunction = ToMTLCompareFunction(descriptor.stencil_compare);
42  des.stencilFailureOperation =
44  des.depthFailureOperation = ToMTLStencilOperation(descriptor.depth_failure);
45  des.depthStencilPassOperation =
47 
48  des.readMask = descriptor.read_mask;
49  des.writeMask = descriptor.write_mask;
50 
51  return des;
52 }
53 
54 MTLDepthStencilDescriptor* ToMTLDepthStencilDescriptor(
55  std::optional<DepthAttachmentDescriptor> depth,
56  std::optional<StencilAttachmentDescriptor> front,
57  std::optional<StencilAttachmentDescriptor> back) {
58  if (!depth) {
60  // Always pass the depth test.
62  .depth_write_enabled = false,
63  };
64  }
65 
66  auto des = [[MTLDepthStencilDescriptor alloc] init];
67 
68  // These temporary variables are necessary for clang-tidy (Fuchsia LLVM
69  // version 17.0.0git) to not crash.
70  auto compare_function = ToMTLCompareFunction(depth->depth_compare);
71  auto depth_write_enabled = depth->depth_write_enabled;
72 
73  des.depthCompareFunction = compare_function;
74  des.depthWriteEnabled = depth_write_enabled;
75 
76  if (front.has_value()) {
77  des.frontFaceStencil = ToMTLStencilDescriptor(front.value());
78  }
79  if (back.has_value()) {
80  des.backFaceStencil = ToMTLStencilDescriptor(back.value());
81  }
82 
83  return des;
84 }
85 
86 MTLTextureDescriptor* ToMTLTextureDescriptor(const TextureDescriptor& desc) {
87  if (!desc.IsValid()) {
88  return nil;
89  }
90  auto mtl_desc = [[MTLTextureDescriptor alloc] init];
91  mtl_desc.textureType = ToMTLTextureType(desc.type);
92  mtl_desc.pixelFormat = ToMTLPixelFormat(desc.format);
93  mtl_desc.sampleCount = static_cast<NSUInteger>(desc.sample_count);
94  mtl_desc.width = desc.size.width;
95  mtl_desc.height = desc.size.height;
96  mtl_desc.mipmapLevelCount = desc.mip_count;
97  mtl_desc.usage = MTLTextureUsageUnknown;
98  if (desc.usage & TextureUsage::kUnknown) {
99  mtl_desc.usage |= MTLTextureUsageUnknown;
100  }
101  if (desc.usage & TextureUsage::kShaderRead) {
102  mtl_desc.usage |= MTLTextureUsageShaderRead;
103  }
104  if (desc.usage & TextureUsage::kShaderWrite) {
105  mtl_desc.usage |= MTLTextureUsageShaderWrite;
106  }
107  if (desc.usage & TextureUsage::kRenderTarget) {
108  mtl_desc.usage |= MTLTextureUsageRenderTarget;
109  }
110  return mtl_desc;
111 }
112 
114 #if !FML_OS_IOS
115  if (@available(macOS 10.11, *)) {
116  return MTLPixelFormatDepth24Unorm_Stencil8;
117  }
118 #endif // FML_OS_IOS
119  return MTLPixelFormatInvalid;
120 }
121 
123  if (@available(iOS 11, macOS 11.0, *)) {
124  return MTLPixelFormatBGR10_XR_sRGB;
125  } else {
126  return MTLPixelFormatInvalid;
127  }
128 }
129 
130 MTLPixelFormat SafeMTLPixelFormatBGR10_XR() {
131  if (@available(iOS 10, macOS 11.0, *)) {
132  return MTLPixelFormatBGR10_XR;
133  } else {
134  return MTLPixelFormatInvalid;
135  }
136 }
137 
138 MTLPixelFormat SafeMTLPixelFormatBGRA10_XR() {
139  if (@available(iOS 10, macOS 11.0, *)) {
140  return MTLPixelFormatBGRA10_XR;
141  } else {
142  return MTLPixelFormatInvalid;
143  }
144 }
145 
146 // BC formats are only available on macOS; referencing the enum values when
147 // compiling for iOS is a hard error, so they are guarded at compile time.
148 MTLPixelFormat SafeMTLPixelFormatBC1_RGBA() {
149 #if !FML_OS_IOS
150  if (@available(macOS 10.11, *)) {
151  return MTLPixelFormatBC1_RGBA;
152  }
153 #endif // FML_OS_IOS
154  return MTLPixelFormatInvalid;
155 }
156 
158 #if !FML_OS_IOS
159  if (@available(macOS 10.11, *)) {
160  return MTLPixelFormatBC1_RGBA_sRGB;
161  }
162 #endif // FML_OS_IOS
163  return MTLPixelFormatInvalid;
164 }
165 
166 MTLPixelFormat SafeMTLPixelFormatBC3_RGBA() {
167 #if !FML_OS_IOS
168  if (@available(macOS 10.11, *)) {
169  return MTLPixelFormatBC3_RGBA;
170  }
171 #endif // FML_OS_IOS
172  return MTLPixelFormatInvalid;
173 }
174 
176 #if !FML_OS_IOS
177  if (@available(macOS 10.11, *)) {
178  return MTLPixelFormatBC3_RGBA_sRGB;
179  }
180 #endif // FML_OS_IOS
181  return MTLPixelFormatInvalid;
182 }
183 
185 #if !FML_OS_IOS
186  if (@available(macOS 10.11, *)) {
187  return MTLPixelFormatBC5_RGUnorm;
188  }
189 #endif // FML_OS_IOS
190  return MTLPixelFormatInvalid;
191 }
192 
194 #if !FML_OS_IOS
195  if (@available(macOS 10.11, *)) {
196  return MTLPixelFormatBC7_RGBAUnorm;
197  }
198 #endif // FML_OS_IOS
199  return MTLPixelFormatInvalid;
200 }
201 
203 #if !FML_OS_IOS
204  if (@available(macOS 10.11, *)) {
205  return MTLPixelFormatBC7_RGBAUnorm_sRGB;
206  }
207 #endif // FML_OS_IOS
208  return MTLPixelFormatInvalid;
209 }
210 
211 // ETC2 and ASTC are available on iOS and on macOS 11.0+. The macOS deployment
212 // target is below 11.0, so the references are version-guarded.
213 MTLPixelFormat SafeMTLPixelFormatETC2_RGB8() {
214  if (@available(macOS 11.0, *)) {
215  return MTLPixelFormatETC2_RGB8;
216  }
217  return MTLPixelFormatInvalid;
218 }
219 
221  if (@available(macOS 11.0, *)) {
222  return MTLPixelFormatETC2_RGB8_sRGB;
223  }
224  return MTLPixelFormatInvalid;
225 }
226 
227 MTLPixelFormat SafeMTLPixelFormatEAC_RGBA8() {
228  if (@available(macOS 11.0, *)) {
229  return MTLPixelFormatEAC_RGBA8;
230  }
231  return MTLPixelFormatInvalid;
232 }
233 
235  if (@available(macOS 11.0, *)) {
236  return MTLPixelFormatEAC_RGBA8_sRGB;
237  }
238  return MTLPixelFormatInvalid;
239 }
240 
242  if (@available(macOS 11.0, *)) {
243  return MTLPixelFormatASTC_4x4_LDR;
244  }
245  return MTLPixelFormatInvalid;
246 }
247 
249  if (@available(macOS 11.0, *)) {
250  return MTLPixelFormatASTC_4x4_sRGB;
251  }
252  return MTLPixelFormatInvalid;
253 }
254 
256  if (@available(macOS 11.0, *)) {
257  return MTLPixelFormatASTC_8x8_LDR;
258  }
259  return MTLPixelFormatInvalid;
260 }
261 
263  if (@available(macOS 11.0, *)) {
264  return MTLPixelFormatASTC_8x8_sRGB;
265  }
266  return MTLPixelFormatInvalid;
267 }
268 
270  if (@available(macOS 11.0, *)) {
271  return MTLPixelFormatASTC_4x4_HDR;
272  }
273  return MTLPixelFormatInvalid;
274 }
275 
277  if (@available(macOS 11.0, *)) {
278  return MTLPixelFormatASTC_8x8_HDR;
279  }
280  return MTLPixelFormatInvalid;
281 }
282 
283 } // namespace impeller
MTLPixelFormat SafeMTLPixelFormatETC2_RGB8_sRGB()
Definition: formats_mtl.mm:220
MTLPixelFormat SafeMTLPixelFormatEAC_RGBA8()
Definition: formats_mtl.mm:227
MTLPixelFormat SafeMTLPixelFormatASTC_4x4_HDR()
Definition: formats_mtl.mm:269
MTLPixelFormat SafeMTLPixelFormatETC2_RGB8()
Definition: formats_mtl.mm:213
MTLPixelFormat SafeMTLPixelFormatASTC_4x4_sRGB()
Definition: formats_mtl.mm:248
constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type)
Definition: formats_mtl.h:270
MTLPixelFormat SafeMTLPixelFormatASTC_4x4_LDR()
Definition: formats_mtl.mm:241
MTLPixelFormat SafeMTLPixelFormatASTC_8x8_HDR()
Definition: formats_mtl.mm:276
constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op)
Definition: formats_mtl.h:314
MTLStencilDescriptor * ToMTLStencilDescriptor(const StencilAttachmentDescriptor &descriptor)
Definition: formats_mtl.mm:38
MTLPixelFormat SafeMTLPixelFormatBC7_RGBAUnorm_sRGB()
Definition: formats_mtl.mm:202
MTLPixelFormat SafeMTLPixelFormatBGR10_XR_sRGB()
Definition: formats_mtl.mm:122
@ kAlways
Comparison test passes always passes.
MTLDepthStencilDescriptor * ToMTLDepthStencilDescriptor(std::optional< DepthAttachmentDescriptor > depth, std::optional< StencilAttachmentDescriptor > front, std::optional< StencilAttachmentDescriptor > back)
Definition: formats_mtl.mm:54
MTLPixelFormat SafeMTLPixelFormatBC1_RGBA()
Definition: formats_mtl.mm:148
MTLPixelFormat SafeMTLPixelFormatBC7_RGBAUnorm()
Definition: formats_mtl.mm:193
constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type)
Definition: formats_mtl.h:258
MTLPixelFormat SafeMTLPixelFormatBC3_RGBA()
Definition: formats_mtl.mm:166
MTLPixelFormat SafeMTLPixelFormatASTC_8x8_LDR()
Definition: formats_mtl.mm:255
MTLPixelFormat SafeMTLPixelFormatBGR10_XR()
Definition: formats_mtl.mm:130
MTLPixelFormat SafeMTLPixelFormatBC3_RGBA_sRGB()
Definition: formats_mtl.mm:175
MTLPixelFormat SafeMTLPixelFormatDepth24Unorm_Stencil8()
Definition: formats_mtl.mm:113
MTLPixelFormat SafeMTLPixelFormatBGRA10_XR()
Definition: formats_mtl.mm:138
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format)
Definition: formats_mtl.h:97
constexpr MTLTextureType ToMTLTextureType(TextureType type)
Definition: formats_mtl.h:435
constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type)
Definition: formats_mtl.h:171
MTLRenderPipelineColorAttachmentDescriptor * ToMTLRenderPipelineColorAttachmentDescriptor(ColorAttachmentDescriptor descriptor)
Definition: formats_mtl.mm:15
MTLPixelFormat SafeMTLPixelFormatBC1_RGBA_sRGB()
Definition: formats_mtl.mm:157
constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func)
Definition: formats_mtl.h:292
MTLPixelFormat SafeMTLPixelFormatASTC_8x8_sRGB()
Definition: formats_mtl.mm:262
MTLTextureDescriptor * ToMTLTextureDescriptor(const TextureDescriptor &desc)
Definition: formats_mtl.mm:86
MTLPixelFormat SafeMTLPixelFormatBC5_RGUnorm()
Definition: formats_mtl.mm:184
MTLPixelFormat SafeMTLPixelFormatEAC_RGBA8_sRGB()
Definition: formats_mtl.mm:234
Describe the color attachment that will be used with this pipeline.
Definition: formats.h:770
StencilOperation depth_stencil_pass
Definition: formats.h:881
Type height
Definition: size.h:29
Type width
Definition: size.h:28
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
constexpr bool IsValid() const