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 } // namespace impeller
constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type)
Definition: formats_mtl.h:213
constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op)
Definition: formats_mtl.h:257
MTLStencilDescriptor * ToMTLStencilDescriptor(const StencilAttachmentDescriptor &descriptor)
Definition: formats_mtl.mm:38
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
constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type)
Definition: formats_mtl.h:201
MTLPixelFormat SafeMTLPixelFormatBGR10_XR()
Definition: formats_mtl.mm:130
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:76
constexpr MTLTextureType ToMTLTextureType(TextureType type)
Definition: formats_mtl.h:378
constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type)
Definition: formats_mtl.h:114
MTLRenderPipelineColorAttachmentDescriptor * ToMTLRenderPipelineColorAttachmentDescriptor(ColorAttachmentDescriptor descriptor)
Definition: formats_mtl.mm:15
constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func)
Definition: formats_mtl.h:235
MTLTextureDescriptor * ToMTLTextureDescriptor(const TextureDescriptor &desc)
Definition: formats_mtl.mm:86
Describe the color attachment that will be used with this pipeline.
Definition: formats.h:518
StencilOperation depth_stencil_pass
Definition: formats.h:629
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