Flutter Engine
The Flutter Engine
MtlUtils.mm
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
15
16namespace skgpu {
17
18bool MtlFormatIsDepthOrStencil(MTLPixelFormat format) {
19 switch (format) {
20 case MTLPixelFormatStencil8: // fallthrough
21 case MTLPixelFormatDepth16Unorm:
22 case MTLPixelFormatDepth32Float:
23#if defined(SK_BUILD_FOR_MAC)
24 case MTLPixelFormatDepth24Unorm_Stencil8:
25#endif
26 case MTLPixelFormatDepth32Float_Stencil8:
27 return true;
28 default:
29 return false;
30 }
31}
32
33bool MtlFormatIsDepth(MTLPixelFormat format) {
34 switch (format) {
35 case MTLPixelFormatDepth16Unorm:
36 case MTLPixelFormatDepth32Float:
37#if defined(SK_BUILD_FOR_MAC)
38 case MTLPixelFormatDepth24Unorm_Stencil8:
39#endif
40 case MTLPixelFormatDepth32Float_Stencil8:
41 return true;
42 default:
43 return false;
44 }
45}
46
47bool MtlFormatIsStencil(MTLPixelFormat format) {
48 switch (format) {
49 case MTLPixelFormatStencil8: // fallthrough
50#if defined(SK_BUILD_FOR_MAC)
51 case MTLPixelFormatDepth24Unorm_Stencil8:
52#endif
53 case MTLPixelFormatDepth32Float_Stencil8:
54 return true;
55 default:
56 return false;
57 }
58}
59
60bool MtlFormatIsCompressed(MTLPixelFormat mtlFormat) {
61 switch (mtlFormat) {
62 case MTLPixelFormatETC2_RGB8:
63 return true;
64#ifdef SK_BUILD_FOR_MAC
65 case MTLPixelFormatBC1_RGBA:
66 return true;
67#endif
68 default:
69 return false;
70 }
71}
72
73const char* MtlFormatToString(MTLPixelFormat mtlFormat) {
74 switch (mtlFormat) {
75 case MTLPixelFormatInvalid: return "Invalid";
76 case MTLPixelFormatRGBA8Unorm: return "RGBA8Unorm";
77 case MTLPixelFormatR8Unorm: return "R8Unorm";
78 case MTLPixelFormatA8Unorm: return "A8Unorm";
79 case MTLPixelFormatBGRA8Unorm: return "BGRA8Unorm";
80 case MTLPixelFormatB5G6R5Unorm: return "B5G6R5Unorm";
81 case MTLPixelFormatRGBA16Float: return "RGBA16Float";
82 case MTLPixelFormatR16Float: return "R16Float";
83 case MTLPixelFormatRG8Unorm: return "RG8Unorm";
84 case MTLPixelFormatRGB10A2Unorm: return "RGB10A2Unorm";
85 case MTLPixelFormatBGR10A2Unorm: return "BGR10A2Unorm";
86 case MTLPixelFormatABGR4Unorm: return "ABGR4Unorm";
87 case MTLPixelFormatRGBA8Unorm_sRGB: return "RGBA8Unorm_sRGB";
88 case MTLPixelFormatR16Unorm: return "R16Unorm";
89 case MTLPixelFormatRG16Unorm: return "RG16Unorm";
90 case MTLPixelFormatETC2_RGB8: return "ETC2_RGB8";
91#ifdef SK_BUILD_FOR_MAC
92 case MTLPixelFormatBC1_RGBA: return "BC1_RGBA";
93#endif
94 case MTLPixelFormatRGBA16Unorm: return "RGBA16Unorm";
95 case MTLPixelFormatRG16Float: return "RG16Float";
96 case MTLPixelFormatStencil8: return "Stencil8";
97
98 default: return "Unknown";
99 }
100}
101
102uint32_t MtlFormatChannels(MTLPixelFormat mtlFormat) {
103 switch (mtlFormat) {
104 case MTLPixelFormatRGBA8Unorm: return kRGBA_SkColorChannelFlags;
105 case MTLPixelFormatR8Unorm: return kRed_SkColorChannelFlag;
106 case MTLPixelFormatA8Unorm: return kAlpha_SkColorChannelFlag;
107 case MTLPixelFormatBGRA8Unorm: return kRGBA_SkColorChannelFlags;
108 case MTLPixelFormatB5G6R5Unorm: return kRGB_SkColorChannelFlags;
109 case MTLPixelFormatRGBA16Float: return kRGBA_SkColorChannelFlags;
110 case MTLPixelFormatR16Float: return kRed_SkColorChannelFlag;
111 case MTLPixelFormatRG8Unorm: return kRG_SkColorChannelFlags;
112 case MTLPixelFormatRGB10A2Unorm: return kRGBA_SkColorChannelFlags;
113 case MTLPixelFormatBGR10A2Unorm: return kRGBA_SkColorChannelFlags;
114 case MTLPixelFormatABGR4Unorm: return kRGBA_SkColorChannelFlags;
115 case MTLPixelFormatRGBA8Unorm_sRGB: return kRGBA_SkColorChannelFlags;
116 case MTLPixelFormatR16Unorm: return kRed_SkColorChannelFlag;
117 case MTLPixelFormatRG16Unorm: return kRG_SkColorChannelFlags;
118 case MTLPixelFormatETC2_RGB8: return kRGB_SkColorChannelFlags;
119#ifdef SK_BUILD_FOR_MAC
120 case MTLPixelFormatBC1_RGBA: return kRGBA_SkColorChannelFlags;
121#endif
122 case MTLPixelFormatRGBA16Unorm: return kRGBA_SkColorChannelFlags;
123 case MTLPixelFormatRG16Float: return kRG_SkColorChannelFlags;
124 case MTLPixelFormatStencil8: return 0;
125
126 default: return 0;
127 }
128}
129
130size_t MtlFormatBytesPerBlock(MTLPixelFormat mtlFormat) {
131 switch (mtlFormat) {
132 case MTLPixelFormatInvalid: return 0;
133 case MTLPixelFormatRGBA8Unorm: return 4;
134 case MTLPixelFormatR8Unorm: return 1;
135 case MTLPixelFormatA8Unorm: return 1;
136 case MTLPixelFormatBGRA8Unorm: return 4;
137 case MTLPixelFormatB5G6R5Unorm: return 2;
138 case MTLPixelFormatRGBA16Float: return 8;
139 case MTLPixelFormatR16Float: return 2;
140 case MTLPixelFormatRG8Unorm: return 2;
141 case MTLPixelFormatRGB10A2Unorm: return 4;
142 case MTLPixelFormatBGR10A2Unorm: return 4;
143 case MTLPixelFormatABGR4Unorm: return 2;
144 case MTLPixelFormatRGBA8Unorm_sRGB: return 4;
145 case MTLPixelFormatR16Unorm: return 2;
146 case MTLPixelFormatRG16Unorm: return 4;
147 case MTLPixelFormatETC2_RGB8: return 8;
148#ifdef SK_BUILD_FOR_MAC
149 case MTLPixelFormatBC1_RGBA: return 8;
150#endif
151 case MTLPixelFormatRGBA16Unorm: return 8;
152 case MTLPixelFormatRG16Float: return 4;
153 case MTLPixelFormatStencil8: return 1;
154
155 default: return 0;
156 }
157}
158
160 switch (mtlFormat) {
161 case MTLPixelFormatETC2_RGB8: return SkTextureCompressionType::kETC2_RGB8_UNORM;
162#ifdef SK_BUILD_FOR_MAC
163 case MTLPixelFormatBC1_RGBA: return SkTextureCompressionType::kBC1_RGBA8_UNORM;
164#endif
165 default: return SkTextureCompressionType::kNone;
166 }
167}
168
169} // namespace skgpu
@ kRGB_SkColorChannelFlags
Definition: SkColor.h:247
@ kRGBA_SkColorChannelFlags
Definition: SkColor.h:248
@ kRed_SkColorChannelFlag
Definition: SkColor.h:239
@ kRG_SkColorChannelFlags
Definition: SkColor.h:246
@ kAlpha_SkColorChannelFlag
Definition: SkColor.h:242
SkTextureCompressionType
uint32_t uint32_t * format
Definition: GpuTools.h:21
uint32_t MtlFormatChannels(MTLPixelFormat mtlFormat)
Definition: MtlUtils.mm:102
const char * MtlFormatToString(MTLPixelFormat mtlFormat)
Definition: MtlUtils.mm:73
bool MtlFormatIsStencil(MTLPixelFormat format)
Definition: MtlUtils.mm:47
bool MtlFormatIsDepthOrStencil(MTLPixelFormat format)
Definition: MtlUtils.mm:18
bool MtlFormatIsDepth(MTLPixelFormat format)
Definition: MtlUtils.mm:33
SkTextureCompressionType MtlFormatToCompressionType(MTLPixelFormat mtlFormat)
Definition: MtlUtils.mm:159
bool MtlFormatIsCompressed(MTLPixelFormat mtlFormat)
Definition: MtlUtils.mm:60
size_t MtlFormatBytesPerBlock(MTLPixelFormat mtlFormat)
Definition: MtlUtils.mm:130