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