Debian Codegraph @

drivers/metal/pixel_formats.h

raw symbols SCIP
1 /**************************************************************************/
2 /* pixel_formats.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 /**************************************************************************/
34 /* */
35 /* Portions of this code were derived from MoltenVK. */
36 /* */
37 /* Copyright (c) 2015-2023 The Brenwill Workshop Ltd. */
38 /* (http://www.brenwill.com) */
39 /* */
40 /* Licensed under the Apache License, Version 2.0 (the "License"); */
41 /* you may not use this file except in compliance with the License. */
42 /* You may obtain a copy of the License at */
43 /* */
44 /* http://www.apache.org/licenses/LICENSE-2.0 */
45 /* */
46 /* Unless required by applicable law or agreed to in writing, software */
47 /* distributed under the License is distributed on an "AS IS" BASIS, */
48 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
49 /* implied. See the License for the specific language governing */
50 /* permissions and limitations under the License. */
51 /**************************************************************************/
52
53 #include "core/typedefs.h"
54
55 GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wdeprecated-declarations")
56
57 #import "inflection_map.h"
58 #import "metal_device_properties.h"
59
60 #include "servers/rendering/rendering_device.h"
61
62 #import <Metal/Metal.h>
63 #include <iterator>
64
65 #pragma mark -
66 #pragma mark Metal format capabilities
67
68 typedef enum : uint16_t {
69
70 kMTLFmtCapsNone = 0,
71 /*! The format can be used in a shader read operation. */
72 kMTLFmtCapsRead = (1 << 0),
73 /*! The format can be used in a shader filter operation during sampling. */
74 kMTLFmtCapsFilter = (1 << 1),
75 /*! The format can be used in a shader write operation. */
76 kMTLFmtCapsWrite = (1 << 2),
77 /*! The format can be used with atomic operations. */
78 kMTLFmtCapsAtomic = (1 << 3),
79 /*! The format can be used as a color attachment. */
80 kMTLFmtCapsColorAtt = (1 << 4),
81 /*! The format can be used as a depth-stencil attachment. */
82 kMTLFmtCapsDSAtt = (1 << 5),
83 /*! The format can be used with blend operations. */
84 kMTLFmtCapsBlend = (1 << 6),
85 /*! The format can be used as a destination for multisample antialias (MSAA) data. */
86 kMTLFmtCapsMSAA = (1 << 7),
87 /*! The format can be used as a resolve attachment. */
88 kMTLFmtCapsResolve = (1 << 8),
89 kMTLFmtCapsVertex = (1 << 9),
90
91 kMTLFmtCapsRF = (kMTLFmtCapsRead | kMTLFmtCapsFilter),
92 kMTLFmtCapsRC = (kMTLFmtCapsRead | kMTLFmtCapsColorAtt),
93 kMTLFmtCapsRCB = (kMTLFmtCapsRC | kMTLFmtCapsBlend),
94 kMTLFmtCapsRCM = (kMTLFmtCapsRC | kMTLFmtCapsMSAA),
95 kMTLFmtCapsRCMB = (kMTLFmtCapsRCM | kMTLFmtCapsBlend),
96 kMTLFmtCapsRWC = (kMTLFmtCapsRC | kMTLFmtCapsWrite),
97 kMTLFmtCapsRWCB = (kMTLFmtCapsRWC | kMTLFmtCapsBlend),
98 kMTLFmtCapsRWCM = (kMTLFmtCapsRWC | kMTLFmtCapsMSAA),
99 kMTLFmtCapsRWCMB = (kMTLFmtCapsRWCM | kMTLFmtCapsBlend),
100 kMTLFmtCapsRFCMRB = (kMTLFmtCapsRCMB | kMTLFmtCapsFilter | kMTLFmtCapsResolve),
101 kMTLFmtCapsRFWCMB = (kMTLFmtCapsRWCMB | kMTLFmtCapsFilter),
102 kMTLFmtCapsAll = (kMTLFmtCapsRFWCMB | kMTLFmtCapsResolve),
103
104 kMTLFmtCapsDRM = (kMTLFmtCapsDSAtt | kMTLFmtCapsRead | kMTLFmtCapsMSAA),
105 kMTLFmtCapsDRFM = (kMTLFmtCapsDRM | kMTLFmtCapsFilter),
106 kMTLFmtCapsDRMR = (kMTLFmtCapsDRM | kMTLFmtCapsResolve),
107 kMTLFmtCapsDRFMR = (kMTLFmtCapsDRFM | kMTLFmtCapsResolve),
108
109 kMTLFmtCapsChromaSubsampling = kMTLFmtCapsRF,
110 kMTLFmtCapsMultiPlanar = kMTLFmtCapsChromaSubsampling,
111 } MTLFmtCaps;
112
113 inline MTLFmtCaps operator|(MTLFmtCaps p_left, MTLFmtCaps p_right) {
114 return static_cast<MTLFmtCaps>(static_cast<uint32_t>(p_left) | p_right);
115 }
116
117 inline MTLFmtCaps &operator|=(MTLFmtCaps &p_left, MTLFmtCaps p_right) {
118 return (p_left = p_left | p_right);
119 }
120
121 #pragma mark -
122 #pragma mark Metal view classes
123
124 enum class MTLViewClass : uint8_t {
125 None,
126 Color8,
127 Color16,
128 Color32,
129 Color64,
130 Color128,
131 PVRTC_RGB_2BPP,
132 PVRTC_RGB_4BPP,
133 PVRTC_RGBA_2BPP,
134 PVRTC_RGBA_4BPP,
135 EAC_R11,
136 EAC_RG11,
137 EAC_RGBA8,
138 ETC2_RGB8,
139 ETC2_RGB8A1,
140 ASTC_4x4,
141 ASTC_5x4,
142 ASTC_5x5,
143 ASTC_6x5,
144 ASTC_6x6,
145 ASTC_8x5,
146 ASTC_8x6,
147 ASTC_8x8,
148 ASTC_10x5,
149 ASTC_10x6,
150 ASTC_10x8,
151 ASTC_10x10,
152 ASTC_12x10,
153 ASTC_12x12,
154 BC1_RGBA,
155 BC2_RGBA,
156 BC3_RGBA,
157 BC4_R,
158 BC5_RG,
159 BC6H_RGB,
160 BC7_RGBA,
161 Depth24_Stencil8,
162 Depth32_Stencil8,
163 BGRA10_XR,
164 BGR10_XR
165 };
166
167 #pragma mark -
168 #pragma mark Format descriptors
169
170 /** Enumerates the data type of a format. */
171 enum class MTLFormatType {
172 None, /**< Format type is unknown. */
173 ColorHalf, /**< A 16-bit floating point color. */
174 ColorFloat, /**< A 32-bit floating point color. */
175 ColorInt8, /**< A signed 8-bit integer color. */
176 ColorUInt8, /**< An unsigned 8-bit integer color. */
177 ColorInt16, /**< A signed 16-bit integer color. */
178 ColorUInt16, /**< An unsigned 16-bit integer color. */
179 ColorInt32, /**< A signed 32-bit integer color. */
180 ColorUInt32, /**< An unsigned 32-bit integer color. */
181 DepthStencil, /**< A depth and stencil value. */
182 Compressed, /**< A block-compressed color. */
183 };
184
185 struct Extent2D {
186 uint32_t width;
187 uint32_t height;
188 };
189
190 struct ComponentMapping {
191 RD::TextureSwizzle r = RD::TEXTURE_SWIZZLE_IDENTITY;
192 RD::TextureSwizzle g = RD::TEXTURE_SWIZZLE_IDENTITY;
193 RD::TextureSwizzle b = RD::TEXTURE_SWIZZLE_IDENTITY;
194 RD::TextureSwizzle a = RD::TEXTURE_SWIZZLE_IDENTITY;
195 };
196
197 /** Describes the properties of a DataFormat, including the corresponding Metal pixel and vertex format. */
198 struct DataFormatDesc {
199 RD::DataFormat dataFormat;
200 MTLPixelFormat mtlPixelFormat;
201 MTLPixelFormat mtlPixelFormatSubstitute;
202 MTLVertexFormat mtlVertexFormat;
203 MTLVertexFormat mtlVertexFormatSubstitute;
204 uint8_t chromaSubsamplingPlaneCount;
205 uint8_t chromaSubsamplingComponentBits;
206 Extent2D blockTexelSize;
207 uint32_t bytesPerBlock;
208 MTLFormatType formatType;
209 ComponentMapping componentMapping;
210 const char *name;
211 bool hasReportedSubstitution;
212
213 inline double bytesPerTexel() const { return (double)bytesPerBlock / (double)(blockTexelSize.width * blockTexelSize.height); }
214
215 inline bool isSupported() const { return (mtlPixelFormat != MTLPixelFormatInvalid || chromaSubsamplingPlaneCount > 1); }
216 inline bool isSupportedOrSubstitutable() const { return isSupported() || (mtlPixelFormatSubstitute != MTLPixelFormatInvalid); }
217
218 inline bool vertexIsSupported() const { return (mtlVertexFormat != MTLVertexFormatInvalid); }
219 inline bool vertexIsSupportedOrSubstitutable() const { return vertexIsSupported() || (mtlVertexFormatSubstitute != MTLVertexFormatInvalid); }
220
221 bool needsSwizzle() const {
222 return (componentMapping.r != RD::TEXTURE_SWIZZLE_IDENTITY ||
223 componentMapping.g != RD::TEXTURE_SWIZZLE_IDENTITY ||
224 componentMapping.b != RD::TEXTURE_SWIZZLE_IDENTITY ||
225 componentMapping.a != RD::TEXTURE_SWIZZLE_IDENTITY);
226 }
227 };
228
229 /** Describes the properties of a MTLPixelFormat or MTLVertexFormat. */
230 struct MTLFormatDesc {
231 union {
232 MTLPixelFormat mtlPixelFormat;
233 MTLVertexFormat mtlVertexFormat;
234 };
235 RD::DataFormat dataFormat = RD::DATA_FORMAT_MAX;
236 MTLFmtCaps mtlFmtCaps;
237 MTLViewClass mtlViewClass;
238 MTLPixelFormat mtlPixelFormatLinear;
239 const char *name = nullptr;
240
241 inline bool isSupported() const { return (mtlPixelFormat != MTLPixelFormatInvalid) && (mtlFmtCaps != kMTLFmtCapsNone); }
242 };
243
244 class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) PixelFormats {
245 using DataFormat = RD::DataFormat;
246
247 public:
248 /** Returns whether the DataFormat is supported by the GPU bound to this instance. */
249 bool isSupported(DataFormat p_format);
250
251 /** Returns whether the DataFormat is supported by this implementation, or can be substituted by one that is. */
252 bool isSupportedOrSubstitutable(DataFormat p_format);
253
254 /** Returns whether the specified Metal MTLPixelFormat can be used as a depth format. */
255 _FORCE_INLINE_ bool isDepthFormat(MTLPixelFormat p_format) {
256 switch (p_format) {
257 case MTLPixelFormatDepth32Float:
258 case MTLPixelFormatDepth16Unorm:
259 case MTLPixelFormatDepth32Float_Stencil8:
260 #if TARGET_OS_OSX
261 case MTLPixelFormatDepth24Unorm_Stencil8:
262 #endif
263 return true;
264 default:
265 return false;
266 }
267 }
268
269 /** Returns whether the specified Metal MTLPixelFormat can be used as a stencil format. */
270 _FORCE_INLINE_ bool isStencilFormat(MTLPixelFormat p_format) {
271 switch (p_format) {
272 case MTLPixelFormatStencil8:
273 #if TARGET_OS_OSX
274 case MTLPixelFormatDepth24Unorm_Stencil8:
275 case MTLPixelFormatX24_Stencil8:
276 #endif
277 case MTLPixelFormatDepth32Float_Stencil8:
278 case MTLPixelFormatX32_Stencil8:
279 return true;
280 default:
281 return false;
282 }
283 }
284
285 /** Returns whether the specified Metal MTLPixelFormat is a PVRTC format. */
286 bool isPVRTCFormat(MTLPixelFormat p_format);
287
288 /** Returns the format type corresponding to the specified Godot pixel format, */
289 MTLFormatType getFormatType(DataFormat p_format);
290
291 /** Returns the format type corresponding to the specified Metal MTLPixelFormat, */
292 MTLFormatType getFormatType(MTLPixelFormat p_format);
293
294 /**
295 * Returns the Metal MTLPixelFormat corresponding to the specified Godot pixel
296 * or returns MTLPixelFormatInvalid if no corresponding MTLPixelFormat exists.
297 */
298 MTLPixelFormat getMTLPixelFormat(DataFormat p_format);
299
300 /**
301 * Returns the DataFormat corresponding to the specified Metal MTLPixelFormat,
302 * or returns DATA_FORMAT_MAX if no corresponding DataFormat exists.
303 */
304 DataFormat getDataFormat(MTLPixelFormat p_format);
305
306 /**
307 * Returns the size, in bytes, of a texel block of the specified Godot pixel.
308 * For uncompressed formats, the returned value corresponds to the size in bytes of a single texel.
309 */
310 uint32_t getBytesPerBlock(DataFormat p_format);
311
312 /**
313 * Returns the size, in bytes, of a texel block of the specified Metal format.
314 * For uncompressed formats, the returned value corresponds to the size in bytes of a single texel.
315 */
316 uint32_t getBytesPerBlock(MTLPixelFormat p_format);
317
318 /** Returns the number of planes of the specified chroma-subsampling (YCbCr) DataFormat */
319 uint8_t getChromaSubsamplingPlaneCount(DataFormat p_format);
320
321 /** Returns the number of bits per channel of the specified chroma-subsampling (YCbCr) DataFormat */
322 uint8_t getChromaSubsamplingComponentBits(DataFormat p_format);
323
324 /**
325 * Returns the size, in bytes, of a texel of the specified Godot format.
326 * The returned value may be fractional for certain compressed formats.
327 */
328 float getBytesPerTexel(DataFormat p_format);
329
330 /**
331 * Returns the size, in bytes, of a texel of the specified Metal format.
332 * The returned value may be fractional for certain compressed formats.
333 */
334 float getBytesPerTexel(MTLPixelFormat p_format);
335
336 /**
337 * Returns the size, in bytes, of a row of texels of the specified Godot pixel format.
338 *
339 * For compressed formats, this takes into consideration the compression block size,
340 * and p_texels_per_row should specify the width in texels, not blocks. The result is rounded
341 * up if p_texels_per_row is not an integer multiple of the compression block width.
342 */
343 size_t getBytesPerRow(DataFormat p_format, uint32_t p_texels_per_row);
344
345 /**
346 * Returns the size, in bytes, of a row of texels of the specified Metal format.
347 *
348 * For compressed formats, this takes into consideration the compression block size,
349 * and texelsPerRow should specify the width in texels, not blocks. The result is rounded
350 * up if texelsPerRow is not an integer multiple of the compression block width.
351 */
352 size_t getBytesPerRow(MTLPixelFormat p_format, uint32_t p_texels_per_row);
353
354 /**
355 * Returns the size, in bytes, of a texture layer of the specified Godot pixel format.
356 *
357 * For compressed formats, this takes into consideration the compression block size,
358 * and p_texel_rows_per_layer should specify the height in texels, not blocks. The result is
359 * rounded up if p_texel_rows_per_layer is not an integer multiple of the compression block height.
360 */
361 size_t getBytesPerLayer(DataFormat p_format, size_t p_bytes_per_row, uint32_t p_texel_rows_per_layer);
362
363 /**
364 * Returns the size, in bytes, of a texture layer of the specified Metal format.
365 * For compressed formats, this takes into consideration the compression block size,
366 * and p_texel_rows_per_layer should specify the height in texels, not blocks. The result is
367 * rounded up if p_texel_rows_per_layer is not an integer multiple of the compression block height.
368 */
369 size_t getBytesPerLayer(MTLPixelFormat p_format, size_t p_bytes_per_row, uint32_t p_texel_rows_per_layer);
370
371 /** Returns whether or not the specified Godot format requires swizzling to use with Metal. */
372 bool needsSwizzle(DataFormat p_format);
373
374 /** Returns the Metal format capabilities supported by the specified Godot format, without substitution. */
375 MTLFmtCaps getCapabilities(DataFormat p_format, bool p_extended = false);
376
377 /** Returns the Metal format capabilities supported by the specified Metal format. */
378 MTLFmtCaps getCapabilities(MTLPixelFormat p_format, bool p_extended = false);
379
380 /**
381 * Returns the Metal MTLVertexFormat corresponding to the specified
382 * DataFormat as used as a vertex attribute format.
383 */
384 MTLVertexFormat getMTLVertexFormat(DataFormat p_format);
385
386 #pragma mark Construction
387
388 explicit PixelFormats(id<MTLDevice> p_device, const MetalFeatures &p_feat);
389
390 protected:
391 DataFormatDesc &getDataFormatDesc(DataFormat p_format);
392 DataFormatDesc &getDataFormatDesc(MTLPixelFormat p_format);
393 MTLFormatDesc &getMTLPixelFormatDesc(MTLPixelFormat p_format);
394 MTLFmtCaps &getMTLPixelFormatCapsIf(MTLPixelFormat mtlPixFmt, bool cond);
395 MTLFormatDesc &getMTLVertexFormatDesc(MTLVertexFormat p_format);
396
397 void initDataFormatCapabilities();
398 void initMTLPixelFormatCapabilities();
399 void initMTLVertexFormatCapabilities(const MetalFeatures &p_feat);
400 void modifyMTLFormatCapabilities(const MetalFeatures &p_feat);
401 void buildDFFormatMaps();
402 void addMTLPixelFormatDescImpl(MTLPixelFormat p_pix_fmt, MTLPixelFormat p_pix_fmt_linear,
403 MTLViewClass p_view_class, MTLFmtCaps p_fmt_caps, const char *p_name);
404 void addMTLVertexFormatDescImpl(MTLVertexFormat p_vert_fmt, MTLFmtCaps p_vert_caps, const char *name);
405
406 id<MTLDevice> device;
407 InflectionMap<DataFormat, DataFormatDesc, RD::DATA_FORMAT_MAX> _data_format_descs;
408 InflectionMap<uint16_t, MTLFormatDesc, MTLPixelFormatX32_Stencil8 + 2> _mtl_pixel_format_descs; // The actual last enum value is not available on iOS.
409 TightLocalVector<MTLFormatDesc> _mtl_vertex_format_descs;
410 };
411
412 GODOT_CLANG_WARNING_POP