| 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
5 #include "impeller/renderer/backend/metal/formats_mtl.h"
6 #include <Metal/Metal.h>
7
8 #include <memory>
9
10 #include "impeller/renderer/render_pass.h"
11
12 namespace impeller {
13
14 MTLRenderPipelineColorAttachmentDescriptor*
15 ToMTLRenderPipelineColorAttachmentDescriptor(
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 =
23 ToMTLBlendFactor(descriptor.src_color_blend_factor);
24 des.rgbBlendOperation = ToMTLBlendOperation(descriptor.color_blend_op);
25 des.destinationRGBBlendFactor =
26 ToMTLBlendFactor(descriptor.dst_color_blend_factor);
27
28 des.sourceAlphaBlendFactor =
29 ToMTLBlendFactor(descriptor.src_alpha_blend_factor);
30 des.alphaBlendOperation = ToMTLBlendOperation(descriptor.alpha_blend_op);
31 des.destinationAlphaBlendFactor =
32 ToMTLBlendFactor(descriptor.dst_alpha_blend_factor);
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 =
43 ToMTLStencilOperation(descriptor.stencil_failure);
44 des.depthFailureOperation = ToMTLStencilOperation(descriptor.depth_failure);
45 des.depthStencilPassOperation =
46 ToMTLStencilOperation(descriptor.depth_stencil_pass);
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) {
59 depth = DepthAttachmentDescriptor{
60 // Always pass the depth test.
61 .depth_compare = CompareFunction::kAlways,
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
113 MTLPixelFormat SafeMTLPixelFormatDepth24Unorm_Stencil8() {
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
122 MTLPixelFormat SafeMTLPixelFormatBGR10_XR_sRGB() {
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
impeller::ToMTLColorWriteMask
constexpr MTLColorWriteMask ToMTLColorWriteMask(ColorWriteMask type)
Definition: formats_mtl.h:209
impeller::ColorAttachmentDescriptor::src_color_blend_factor
BlendFactor src_color_blend_factor
Definition: formats.h:516
impeller::TextureUsage::kShaderWrite
@ kShaderWrite
impeller::StencilAttachmentDescriptor::depth_failure
StencilOperation depth_failure
Definition: formats.h:619
impeller::StencilAttachmentDescriptor::stencil_compare
CompareFunction stencil_compare
Definition: formats.h:610
impeller::SafeMTLPixelFormatBGRA10_XR
MTLPixelFormat SafeMTLPixelFormatBGRA10_XR()
Definition: formats_mtl.mm:138
impeller::ToMTLStencilOperation
constexpr MTLStencilOperation ToMTLStencilOperation(StencilOperation op)
Definition: formats_mtl.h:253
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:41
impeller::TextureDescriptor::mip_count
size_t mip_count
Definition: texture_descriptor.h:43
impeller::ColorAttachmentDescriptor::alpha_blend_op
BlendOperation alpha_blend_op
Definition: formats.h:521
formats_mtl.h
impeller::SafeMTLPixelFormatDepth24Unorm_Stencil8
MTLPixelFormat SafeMTLPixelFormatDepth24Unorm_Stencil8()
Definition: formats_mtl.mm:113
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
impeller::StencilAttachmentDescriptor::write_mask
uint32_t write_mask
Definition: formats.h:634
impeller::ToMTLCompareFunction
constexpr MTLCompareFunction ToMTLCompareFunction(CompareFunction func)
Definition: formats_mtl.h:231
impeller::TextureDescriptor::sample_count
SampleCount sample_count
Definition: texture_descriptor.h:45
impeller::TextureDescriptor::usage
TextureUsageMask usage
Definition: texture_descriptor.h:44
impeller::ToMTLDepthStencilDescriptor
MTLDepthStencilDescriptor * ToMTLDepthStencilDescriptor(std::optional< DepthAttachmentDescriptor > depth, std::optional< StencilAttachmentDescriptor > front, std::optional< StencilAttachmentDescriptor > back)
Definition: formats_mtl.mm:54
impeller::ToMTLTextureDescriptor
MTLTextureDescriptor * ToMTLTextureDescriptor(const TextureDescriptor &desc)
Definition: formats_mtl.mm:86
impeller::TextureDescriptor::type
TextureType type
Definition: texture_descriptor.h:40
impeller::TextureDescriptor::IsValid
constexpr bool IsValid() const
Definition: texture_descriptor.h:98
render_pass.h
impeller::StencilAttachmentDescriptor::read_mask
uint32_t read_mask
Definition: formats.h:629
impeller::ToMTLBlendOperation
constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type)
Definition: formats_mtl.h:197
impeller::ToMTLStencilDescriptor
MTLStencilDescriptor * ToMTLStencilDescriptor(const StencilAttachmentDescriptor &descriptor)
Definition: formats_mtl.mm:38
impeller::ToMTLRenderPipelineColorAttachmentDescriptor
MTLRenderPipelineColorAttachmentDescriptor * ToMTLRenderPipelineColorAttachmentDescriptor(ColorAttachmentDescriptor descriptor)
Definition: formats_mtl.mm:15
impeller::StencilAttachmentDescriptor::stencil_failure
StencilOperation stencil_failure
Definition: formats.h:614
impeller::ColorAttachmentDescriptor::format
PixelFormat format
Definition: formats.h:513
impeller::StencilAttachmentDescriptor::depth_stencil_pass
StencilOperation depth_stencil_pass
Definition: formats.h:623
impeller::CompareFunction::kAlways
@ kAlways
Comparison test passes always passes.
impeller::TSize::width
Type width
Definition: size.h:22
impeller::TextureUsage::kUnknown
@ kUnknown
impeller::ToMTLPixelFormat
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format)
Definition: formats_mtl.h:76
impeller::SafeMTLPixelFormatBGR10_XR
MTLPixelFormat SafeMTLPixelFormatBGR10_XR()
Definition: formats_mtl.mm:130
impeller::ColorAttachmentDescriptor::src_alpha_blend_factor
BlendFactor src_alpha_blend_factor
Definition: formats.h:520
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:42
impeller::ToMTLTextureType
constexpr MTLTextureType ToMTLTextureType(TextureType type)
Definition: formats_mtl.h:374
impeller::ColorAttachmentDescriptor::dst_color_blend_factor
BlendFactor dst_color_blend_factor
Definition: formats.h:518
impeller::ColorAttachmentDescriptor::dst_alpha_blend_factor
BlendFactor dst_alpha_blend_factor
Definition: formats.h:522
impeller::SafeMTLPixelFormatBGR10_XR_sRGB
MTLPixelFormat SafeMTLPixelFormatBGR10_XR_sRGB()
Definition: formats_mtl.mm:122
impeller::DepthAttachmentDescriptor
Definition: formats.h:584
impeller::ToMTLBlendFactor
constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type)
Definition: formats_mtl.h:114
impeller::StencilAttachmentDescriptor
Definition: formats.h:604
impeller::TextureUsage::kShaderRead
@ kShaderRead
impeller::TSize::height
Type height
Definition: size.h:23
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:38
impeller::ColorAttachmentDescriptor::color_blend_op
BlendOperation color_blend_op
Definition: formats.h:517
impeller
Definition: aiks_blend_unittests.cc:18
impeller::ColorAttachmentDescriptor::blending_enabled
bool blending_enabled
Definition: formats.h:514
impeller::DepthAttachmentDescriptor::depth_compare
CompareFunction depth_compare
Definition: formats.h:588
impeller::ColorAttachmentDescriptor::write_mask
ColorWriteMask write_mask
Definition: formats.h:524
impeller::ColorAttachmentDescriptor
Describe the color attachment that will be used with this pipeline.
Definition: formats.h:512