Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/metal/pixel_formats.h
21572 views
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
#include "inflection_map.h"
58
#include "metal_device_properties.h"
59
60
#include "servers/rendering/rendering_device_commons.h"
61
62
#ifdef __OBJC__
63
#include <Metal/Metal.h>
64
#endif
65
#include <Metal/Metal.hpp>
66
#include <iterator>
67
68
#pragma mark -
69
#pragma mark Metal format capabilities
70
71
using RDC = RenderingDeviceCommons;
72
73
typedef enum : uint16_t {
74
75
kMTLFmtCapsNone = 0,
76
/*! The format can be used in a shader read operation. */
77
kMTLFmtCapsRead = (1 << 0),
78
/*! The format can be used in a shader filter operation during sampling. */
79
kMTLFmtCapsFilter = (1 << 1),
80
/*! The format can be used in a shader write operation. */
81
kMTLFmtCapsWrite = (1 << 2),
82
/*! The format can be used with atomic operations. */
83
kMTLFmtCapsAtomic = (1 << 3),
84
/*! The format can be used as a color attachment. */
85
kMTLFmtCapsColorAtt = (1 << 4),
86
/*! The format can be used as a depth-stencil attachment. */
87
kMTLFmtCapsDSAtt = (1 << 5),
88
/*! The format can be used with blend operations. */
89
kMTLFmtCapsBlend = (1 << 6),
90
/*! The format can be used as a destination for multisample antialias (MSAA) data. */
91
kMTLFmtCapsMSAA = (1 << 7),
92
/*! The format can be used as a resolve attachment. */
93
kMTLFmtCapsResolve = (1 << 8),
94
kMTLFmtCapsVertex = (1 << 9),
95
96
kMTLFmtCapsRF = (kMTLFmtCapsRead | kMTLFmtCapsFilter),
97
kMTLFmtCapsRC = (kMTLFmtCapsRead | kMTLFmtCapsColorAtt),
98
kMTLFmtCapsRCB = (kMTLFmtCapsRC | kMTLFmtCapsBlend),
99
kMTLFmtCapsRCM = (kMTLFmtCapsRC | kMTLFmtCapsMSAA),
100
kMTLFmtCapsRCMB = (kMTLFmtCapsRCM | kMTLFmtCapsBlend),
101
kMTLFmtCapsRWC = (kMTLFmtCapsRC | kMTLFmtCapsWrite),
102
kMTLFmtCapsRWCB = (kMTLFmtCapsRWC | kMTLFmtCapsBlend),
103
kMTLFmtCapsRWCM = (kMTLFmtCapsRWC | kMTLFmtCapsMSAA),
104
kMTLFmtCapsRWCMB = (kMTLFmtCapsRWCM | kMTLFmtCapsBlend),
105
kMTLFmtCapsRFCMRB = (kMTLFmtCapsRCMB | kMTLFmtCapsFilter | kMTLFmtCapsResolve),
106
kMTLFmtCapsRFWCMB = (kMTLFmtCapsRWCMB | kMTLFmtCapsFilter),
107
kMTLFmtCapsAll = (kMTLFmtCapsRFWCMB | kMTLFmtCapsResolve),
108
109
kMTLFmtCapsDRM = (kMTLFmtCapsDSAtt | kMTLFmtCapsRead | kMTLFmtCapsMSAA),
110
kMTLFmtCapsDRFM = (kMTLFmtCapsDRM | kMTLFmtCapsFilter),
111
kMTLFmtCapsDRMR = (kMTLFmtCapsDRM | kMTLFmtCapsResolve),
112
kMTLFmtCapsDRFMR = (kMTLFmtCapsDRFM | kMTLFmtCapsResolve),
113
114
kMTLFmtCapsChromaSubsampling = kMTLFmtCapsRF,
115
kMTLFmtCapsMultiPlanar = kMTLFmtCapsChromaSubsampling,
116
} MTLFmtCaps;
117
118
inline MTLFmtCaps operator|(MTLFmtCaps p_left, MTLFmtCaps p_right) {
119
return static_cast<MTLFmtCaps>(static_cast<uint32_t>(p_left) | p_right);
120
}
121
122
inline MTLFmtCaps &operator|=(MTLFmtCaps &p_left, MTLFmtCaps p_right) {
123
return (p_left = p_left | p_right);
124
}
125
126
#pragma mark -
127
#pragma mark Metal view classes
128
129
enum class MTLViewClass : uint8_t {
130
None,
131
Color8,
132
Color16,
133
Color32,
134
Color64,
135
Color128,
136
PVRTC_RGB_2BPP,
137
PVRTC_RGB_4BPP,
138
PVRTC_RGBA_2BPP,
139
PVRTC_RGBA_4BPP,
140
EAC_R11,
141
EAC_RG11,
142
EAC_RGBA8,
143
ETC2_RGB8,
144
ETC2_RGB8A1,
145
ASTC_4x4,
146
ASTC_5x4,
147
ASTC_5x5,
148
ASTC_6x5,
149
ASTC_6x6,
150
ASTC_8x5,
151
ASTC_8x6,
152
ASTC_8x8,
153
ASTC_10x5,
154
ASTC_10x6,
155
ASTC_10x8,
156
ASTC_10x10,
157
ASTC_12x10,
158
ASTC_12x12,
159
BC1_RGBA,
160
BC2_RGBA,
161
BC3_RGBA,
162
BC4_R,
163
BC5_RG,
164
BC6H_RGB,
165
BC7_RGBA,
166
Depth24_Stencil8,
167
Depth32_Stencil8,
168
BGRA10_XR,
169
BGR10_XR
170
};
171
172
#pragma mark -
173
#pragma mark Format descriptors
174
175
/** Enumerates the data type of a format. */
176
enum class MTLFormatType {
177
None, /**< Format type is unknown. */
178
ColorHalf, /**< A 16-bit floating point color. */
179
ColorFloat, /**< A 32-bit floating point color. */
180
ColorInt8, /**< A signed 8-bit integer color. */
181
ColorUInt8, /**< An unsigned 8-bit integer color. */
182
ColorInt16, /**< A signed 16-bit integer color. */
183
ColorUInt16, /**< An unsigned 16-bit integer color. */
184
ColorInt32, /**< A signed 32-bit integer color. */
185
ColorUInt32, /**< An unsigned 32-bit integer color. */
186
DepthStencil, /**< A depth and stencil value. */
187
Compressed, /**< A block-compressed color. */
188
};
189
190
struct Extent2D {
191
uint32_t width;
192
uint32_t height;
193
};
194
195
struct ComponentMapping {
196
RDC::TextureSwizzle r = RDC::TEXTURE_SWIZZLE_IDENTITY;
197
RDC::TextureSwizzle g = RDC::TEXTURE_SWIZZLE_IDENTITY;
198
RDC::TextureSwizzle b = RDC::TEXTURE_SWIZZLE_IDENTITY;
199
RDC::TextureSwizzle a = RDC::TEXTURE_SWIZZLE_IDENTITY;
200
};
201
202
/** Describes the properties of a DataFormat, including the corresponding Metal pixel and vertex format. */
203
struct DataFormatDesc {
204
RDC::DataFormat dataFormat;
205
MTL::PixelFormat mtlPixelFormat;
206
MTL::PixelFormat mtlPixelFormatSubstitute;
207
MTL::VertexFormat mtlVertexFormat;
208
MTL::VertexFormat mtlVertexFormatSubstitute;
209
uint8_t chromaSubsamplingPlaneCount;
210
uint8_t chromaSubsamplingComponentBits;
211
Extent2D blockTexelSize;
212
uint32_t bytesPerBlock;
213
MTLFormatType formatType;
214
ComponentMapping componentMapping;
215
const char *name;
216
bool hasReportedSubstitution;
217
218
inline double bytesPerTexel() const { return (double)bytesPerBlock / (double)(blockTexelSize.width * blockTexelSize.height); }
219
220
inline bool isSupported() const { return (mtlPixelFormat != MTL::PixelFormatInvalid || chromaSubsamplingPlaneCount > 1); }
221
inline bool isSupportedOrSubstitutable() const { return isSupported() || (mtlPixelFormatSubstitute != MTL::PixelFormatInvalid); }
222
223
inline bool vertexIsSupported() const { return (mtlVertexFormat != MTL::VertexFormatInvalid); }
224
inline bool vertexIsSupportedOrSubstitutable() const { return vertexIsSupported() || (mtlVertexFormatSubstitute != MTL::VertexFormatInvalid); }
225
226
bool needsSwizzle() const {
227
return (componentMapping.r != RDC::TEXTURE_SWIZZLE_IDENTITY ||
228
componentMapping.g != RDC::TEXTURE_SWIZZLE_IDENTITY ||
229
componentMapping.b != RDC::TEXTURE_SWIZZLE_IDENTITY ||
230
componentMapping.a != RDC::TEXTURE_SWIZZLE_IDENTITY);
231
}
232
};
233
234
/** Describes the properties of a MTL::PixelFormat or MTL::VertexFormat. */
235
struct MTLFormatDesc {
236
union {
237
MTL::PixelFormat mtlPixelFormat;
238
MTL::VertexFormat mtlVertexFormat;
239
};
240
RDC::DataFormat dataFormat = RDC::DATA_FORMAT_MAX;
241
MTLFmtCaps mtlFmtCaps;
242
MTLViewClass mtlViewClass;
243
MTL::PixelFormat mtlPixelFormatLinear;
244
const char *name = nullptr;
245
246
inline bool isSupported() const { return (mtlPixelFormat != MTL::PixelFormatInvalid) && (mtlFmtCaps != kMTLFmtCapsNone); }
247
};
248
249
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) PixelFormats {
250
using DataFormat = RDC::DataFormat;
251
252
public:
253
/** Returns whether the DataFormat is supported by the GPU bound to this instance. */
254
bool isSupported(DataFormat p_format);
255
256
/** Returns whether the DataFormat is supported by this implementation, or can be substituted by one that is. */
257
bool isSupportedOrSubstitutable(DataFormat p_format);
258
259
/** Returns whether the specified Metal MTL::PixelFormat can be used as a depth format. */
260
_FORCE_INLINE_ bool isDepthFormat(MTL::PixelFormat p_format) {
261
switch (p_format) {
262
case MTL::PixelFormatDepth32Float:
263
case MTL::PixelFormatDepth16Unorm:
264
case MTL::PixelFormatDepth32Float_Stencil8:
265
#if TARGET_OS_OSX
266
case MTL::PixelFormatDepth24Unorm_Stencil8:
267
#endif
268
return true;
269
default:
270
return false;
271
}
272
}
273
274
/** Returns whether the specified Metal MTL::PixelFormat can be used as a stencil format. */
275
_FORCE_INLINE_ bool isStencilFormat(MTL::PixelFormat p_format) {
276
switch (p_format) {
277
case MTL::PixelFormatStencil8:
278
#if TARGET_OS_OSX
279
case MTL::PixelFormatDepth24Unorm_Stencil8:
280
case MTL::PixelFormatX24_Stencil8:
281
#endif
282
case MTL::PixelFormatDepth32Float_Stencil8:
283
case MTL::PixelFormatX32_Stencil8:
284
return true;
285
default:
286
return false;
287
}
288
}
289
290
/** Returns whether the specified Metal MTL::PixelFormat is a PVRTC format. */
291
bool isPVRTCFormat(MTL::PixelFormat p_format);
292
293
/** Returns the format type corresponding to the specified Godot pixel format, */
294
MTLFormatType getFormatType(DataFormat p_format);
295
296
/** Returns the format type corresponding to the specified Metal MTL::PixelFormat, */
297
MTLFormatType getFormatType(MTL::PixelFormat p_format);
298
299
/**
300
* Returns the Metal MTL::PixelFormat corresponding to the specified Godot pixel
301
* or returns MTL::PixelFormatInvalid if no corresponding MTL::PixelFormat exists.
302
*/
303
MTL::PixelFormat getMTLPixelFormat(DataFormat p_format);
304
305
/**
306
* Returns the DataFormat corresponding to the specified Metal MTL::PixelFormat,
307
* or returns DATA_FORMAT_MAX if no corresponding DataFormat exists.
308
*/
309
DataFormat getDataFormat(MTL::PixelFormat p_format);
310
311
/**
312
* Returns the size, in bytes, of a texel block of the specified Godot pixel.
313
* For uncompressed formats, the returned value corresponds to the size in bytes of a single texel.
314
*/
315
uint32_t getBytesPerBlock(DataFormat p_format);
316
317
/**
318
* Returns the size, in bytes, of a texel block of the specified Metal format.
319
* For uncompressed formats, the returned value corresponds to the size in bytes of a single texel.
320
*/
321
uint32_t getBytesPerBlock(MTL::PixelFormat p_format);
322
323
/** Returns the number of planes of the specified chroma-subsampling (YCbCr) DataFormat */
324
uint8_t getChromaSubsamplingPlaneCount(DataFormat p_format);
325
326
/** Returns the number of bits per channel of the specified chroma-subsampling (YCbCr) DataFormat */
327
uint8_t getChromaSubsamplingComponentBits(DataFormat p_format);
328
329
/**
330
* Returns the size, in bytes, of a texel of the specified Godot format.
331
* The returned value may be fractional for certain compressed formats.
332
*/
333
float getBytesPerTexel(DataFormat p_format);
334
335
/**
336
* Returns the size, in bytes, of a texel of the specified Metal format.
337
* The returned value may be fractional for certain compressed formats.
338
*/
339
float getBytesPerTexel(MTL::PixelFormat p_format);
340
341
/**
342
* Returns the size, in bytes, of a row of texels of the specified Godot pixel format.
343
*
344
* For compressed formats, this takes into consideration the compression block size,
345
* and p_texels_per_row should specify the width in texels, not blocks. The result is rounded
346
* up if p_texels_per_row is not an integer multiple of the compression block width.
347
*/
348
size_t getBytesPerRow(DataFormat p_format, uint32_t p_texels_per_row);
349
350
/**
351
* Returns the size, in bytes, of a row of texels of the specified Metal format.
352
*
353
* For compressed formats, this takes into consideration the compression block size,
354
* and texelsPerRow should specify the width in texels, not blocks. The result is rounded
355
* up if texelsPerRow is not an integer multiple of the compression block width.
356
*/
357
size_t getBytesPerRow(MTL::PixelFormat p_format, uint32_t p_texels_per_row);
358
359
/**
360
* Returns the size, in bytes, of a texture layer of the specified Godot pixel format.
361
*
362
* For compressed formats, this takes into consideration the compression block size,
363
* and p_texel_rows_per_layer should specify the height in texels, not blocks. The result is
364
* rounded up if p_texel_rows_per_layer is not an integer multiple of the compression block height.
365
*/
366
size_t getBytesPerLayer(DataFormat p_format, size_t p_bytes_per_row, uint32_t p_texel_rows_per_layer);
367
368
/**
369
* Returns the size, in bytes, of a texture layer of the specified Metal format.
370
* For compressed formats, this takes into consideration the compression block size,
371
* and p_texel_rows_per_layer should specify the height in texels, not blocks. The result is
372
* rounded up if p_texel_rows_per_layer is not an integer multiple of the compression block height.
373
*/
374
size_t getBytesPerLayer(MTL::PixelFormat p_format, size_t p_bytes_per_row, uint32_t p_texel_rows_per_layer);
375
376
/** Returns whether or not the specified Godot format requires swizzling to use with Metal. */
377
bool needsSwizzle(DataFormat p_format);
378
379
/** Returns the Metal format capabilities supported by the specified Godot format, without substitution. */
380
MTLFmtCaps getCapabilities(DataFormat p_format, bool p_extended = false);
381
382
/** Returns the Metal format capabilities supported by the specified Metal format. */
383
MTLFmtCaps getCapabilities(MTL::PixelFormat p_format, bool p_extended = false);
384
385
/**
386
* Returns the Metal MTL::VertexFormat corresponding to the specified
387
* DataFormat as used as a vertex attribute format.
388
*/
389
MTL::VertexFormat getMTLVertexFormat(DataFormat p_format);
390
391
#pragma mark Construction
392
393
explicit PixelFormats(MTL::Device *p_device, const MetalFeatures &p_feat);
394
~PixelFormats();
395
396
protected:
397
DataFormatDesc &getDataFormatDesc(DataFormat p_format);
398
DataFormatDesc &getDataFormatDesc(MTL::PixelFormat p_format);
399
MTLFormatDesc &getMTLPixelFormatDesc(MTL::PixelFormat p_format);
400
MTLFmtCaps &getMTLPixelFormatCapsIf(MTL::PixelFormat mtlPixFmt, bool cond);
401
MTLFormatDesc &getMTLVertexFormatDesc(MTL::VertexFormat p_format);
402
403
void initDataFormatCapabilities();
404
void initMTLPixelFormatCapabilities();
405
void initMTLVertexFormatCapabilities(const MetalFeatures &p_feat);
406
void modifyMTLFormatCapabilities(const MetalFeatures &p_feat);
407
void buildDFFormatMaps();
408
void addMTLPixelFormatDescImpl(MTL::PixelFormat p_pix_fmt, MTL::PixelFormat p_pix_fmt_linear,
409
MTLViewClass p_view_class, MTLFmtCaps p_fmt_caps, const char *p_name);
410
void addMTLVertexFormatDescImpl(MTL::VertexFormat p_vert_fmt, MTLFmtCaps p_vert_caps, const char *name);
411
412
MTL::Device *device;
413
InflectionMap<DataFormat, DataFormatDesc, RDC::DATA_FORMAT_MAX> _data_format_descs;
414
InflectionMap<uint16_t, MTLFormatDesc, MTL::PixelFormatX32_Stencil8 + 2> _mtl_pixel_format_descs; // The actual last enum value is not available on iOS.
415
TightLocalVector<MTLFormatDesc> _mtl_vertex_format_descs;
416
};
417
418
GODOT_CLANG_WARNING_POP
419
420