pixel_formats.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. /* */
  32. /* Portions of this code were derived from MoltenVK. */
  33. /* */
  34. /* Copyright (c) 2015-2023 The Brenwill Workshop Ltd. */
  35. /* (http://www.brenwill.com) */
  36. /* */
  37. /* Licensed under the Apache License, Version 2.0 (the "License"); */
  38. /* you may not use this file except in compliance with the License. */
  39. /* You may obtain a copy of the License at */
  40. /* */
  41. /* http://www.apache.org/licenses/LICENSE-2.0 */
  42. /* */
  43. /* Unless required by applicable law or agreed to in writing, software */
  44. /* distributed under the License is distributed on an "AS IS" BASIS, */
  45. /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
  46. /* implied. See the License for the specific language governing */
  47. /* permissions and limitations under the License. */
  48. /**************************************************************************/
  49. #ifndef PIXEL_FORMATS_H
  50. #define PIXEL_FORMATS_H
  51. #pragma clang diagnostic push
  52. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  53. #import "inflection_map.h"
  54. #import "metal_device_properties.h"
  55. #import "servers/rendering/rendering_device.h"
  56. #import <Metal/Metal.h>
  57. #pragma mark -
  58. #pragma mark Metal format capabilities
  59. typedef enum : uint16_t {
  60. kMTLFmtCapsNone = 0,
  61. /*! The format can be used in a shader read operation. */
  62. kMTLFmtCapsRead = (1 << 0),
  63. /*! The format can be used in a shader filter operation during sampling. */
  64. kMTLFmtCapsFilter = (1 << 1),
  65. /*! The format can be used in a shader write operation. */
  66. kMTLFmtCapsWrite = (1 << 2),
  67. /*! The format can be used with atomic operations. */
  68. kMTLFmtCapsAtomic = (1 << 3),
  69. /*! The format can be used as a color attachment. */
  70. kMTLFmtCapsColorAtt = (1 << 4),
  71. /*! The format can be used as a depth-stencil attachment. */
  72. kMTLFmtCapsDSAtt = (1 << 5),
  73. /*! The format can be used with blend operations. */
  74. kMTLFmtCapsBlend = (1 << 6),
  75. /*! The format can be used as a destination for multisample antialias (MSAA) data. */
  76. kMTLFmtCapsMSAA = (1 << 7),
  77. /*! The format can be used as a resolve attachment. */
  78. kMTLFmtCapsResolve = (1 << 8),
  79. kMTLFmtCapsVertex = (1 << 9),
  80. kMTLFmtCapsRF = (kMTLFmtCapsRead | kMTLFmtCapsFilter),
  81. kMTLFmtCapsRC = (kMTLFmtCapsRead | kMTLFmtCapsColorAtt),
  82. kMTLFmtCapsRCB = (kMTLFmtCapsRC | kMTLFmtCapsBlend),
  83. kMTLFmtCapsRCM = (kMTLFmtCapsRC | kMTLFmtCapsMSAA),
  84. kMTLFmtCapsRCMB = (kMTLFmtCapsRCM | kMTLFmtCapsBlend),
  85. kMTLFmtCapsRWC = (kMTLFmtCapsRC | kMTLFmtCapsWrite),
  86. kMTLFmtCapsRWCB = (kMTLFmtCapsRWC | kMTLFmtCapsBlend),
  87. kMTLFmtCapsRWCM = (kMTLFmtCapsRWC | kMTLFmtCapsMSAA),
  88. kMTLFmtCapsRWCMB = (kMTLFmtCapsRWCM | kMTLFmtCapsBlend),
  89. kMTLFmtCapsRFCMRB = (kMTLFmtCapsRCMB | kMTLFmtCapsFilter | kMTLFmtCapsResolve),
  90. kMTLFmtCapsRFWCMB = (kMTLFmtCapsRWCMB | kMTLFmtCapsFilter),
  91. kMTLFmtCapsAll = (kMTLFmtCapsRFWCMB | kMTLFmtCapsResolve),
  92. kMTLFmtCapsDRM = (kMTLFmtCapsDSAtt | kMTLFmtCapsRead | kMTLFmtCapsMSAA),
  93. kMTLFmtCapsDRFM = (kMTLFmtCapsDRM | kMTLFmtCapsFilter),
  94. kMTLFmtCapsDRMR = (kMTLFmtCapsDRM | kMTLFmtCapsResolve),
  95. kMTLFmtCapsDRFMR = (kMTLFmtCapsDRFM | kMTLFmtCapsResolve),
  96. kMTLFmtCapsChromaSubsampling = kMTLFmtCapsRF,
  97. kMTLFmtCapsMultiPlanar = kMTLFmtCapsChromaSubsampling,
  98. } MTLFmtCaps;
  99. inline MTLFmtCaps operator|(MTLFmtCaps p_left, MTLFmtCaps p_right) {
  100. return static_cast<MTLFmtCaps>(static_cast<uint32_t>(p_left) | p_right);
  101. }
  102. inline MTLFmtCaps &operator|=(MTLFmtCaps &p_left, MTLFmtCaps p_right) {
  103. return (p_left = p_left | p_right);
  104. }
  105. #pragma mark -
  106. #pragma mark Metal view classes
  107. enum class MTLViewClass : uint8_t {
  108. None,
  109. Color8,
  110. Color16,
  111. Color32,
  112. Color64,
  113. Color128,
  114. PVRTC_RGB_2BPP,
  115. PVRTC_RGB_4BPP,
  116. PVRTC_RGBA_2BPP,
  117. PVRTC_RGBA_4BPP,
  118. EAC_R11,
  119. EAC_RG11,
  120. EAC_RGBA8,
  121. ETC2_RGB8,
  122. ETC2_RGB8A1,
  123. ASTC_4x4,
  124. ASTC_5x4,
  125. ASTC_5x5,
  126. ASTC_6x5,
  127. ASTC_6x6,
  128. ASTC_8x5,
  129. ASTC_8x6,
  130. ASTC_8x8,
  131. ASTC_10x5,
  132. ASTC_10x6,
  133. ASTC_10x8,
  134. ASTC_10x10,
  135. ASTC_12x10,
  136. ASTC_12x12,
  137. BC1_RGBA,
  138. BC2_RGBA,
  139. BC3_RGBA,
  140. BC4_R,
  141. BC5_RG,
  142. BC6H_RGB,
  143. BC7_RGBA,
  144. Depth24_Stencil8,
  145. Depth32_Stencil8,
  146. BGRA10_XR,
  147. BGR10_XR
  148. };
  149. #pragma mark -
  150. #pragma mark Format descriptors
  151. /** Enumerates the data type of a format. */
  152. enum class MTLFormatType {
  153. None, /**< Format type is unknown. */
  154. ColorHalf, /**< A 16-bit floating point color. */
  155. ColorFloat, /**< A 32-bit floating point color. */
  156. ColorInt8, /**< A signed 8-bit integer color. */
  157. ColorUInt8, /**< An unsigned 8-bit integer color. */
  158. ColorInt16, /**< A signed 16-bit integer color. */
  159. ColorUInt16, /**< An unsigned 16-bit integer color. */
  160. ColorInt32, /**< A signed 32-bit integer color. */
  161. ColorUInt32, /**< An unsigned 32-bit integer color. */
  162. DepthStencil, /**< A depth and stencil value. */
  163. Compressed, /**< A block-compressed color. */
  164. };
  165. struct Extent2D {
  166. uint32_t width;
  167. uint32_t height;
  168. };
  169. struct ComponentMapping {
  170. RD::TextureSwizzle r = RD::TEXTURE_SWIZZLE_IDENTITY;
  171. RD::TextureSwizzle g = RD::TEXTURE_SWIZZLE_IDENTITY;
  172. RD::TextureSwizzle b = RD::TEXTURE_SWIZZLE_IDENTITY;
  173. RD::TextureSwizzle a = RD::TEXTURE_SWIZZLE_IDENTITY;
  174. };
  175. /** Describes the properties of a DataFormat, including the corresponding Metal pixel and vertex format. */
  176. struct DataFormatDesc {
  177. RD::DataFormat dataFormat;
  178. MTLPixelFormat mtlPixelFormat;
  179. MTLPixelFormat mtlPixelFormatSubstitute;
  180. MTLVertexFormat mtlVertexFormat;
  181. MTLVertexFormat mtlVertexFormatSubstitute;
  182. uint8_t chromaSubsamplingPlaneCount;
  183. uint8_t chromaSubsamplingComponentBits;
  184. Extent2D blockTexelSize;
  185. uint32_t bytesPerBlock;
  186. MTLFormatType formatType;
  187. ComponentMapping componentMapping;
  188. const char *name;
  189. bool hasReportedSubstitution;
  190. inline double bytesPerTexel() const { return (double)bytesPerBlock / (double)(blockTexelSize.width * blockTexelSize.height); }
  191. inline bool isSupported() const { return (mtlPixelFormat != MTLPixelFormatInvalid || chromaSubsamplingPlaneCount > 1); }
  192. inline bool isSupportedOrSubstitutable() const { return isSupported() || (mtlPixelFormatSubstitute != MTLPixelFormatInvalid); }
  193. inline bool vertexIsSupported() const { return (mtlVertexFormat != MTLVertexFormatInvalid); }
  194. inline bool vertexIsSupportedOrSubstitutable() const { return vertexIsSupported() || (mtlVertexFormatSubstitute != MTLVertexFormatInvalid); }
  195. bool needsSwizzle() const {
  196. return (componentMapping.r != RD::TEXTURE_SWIZZLE_IDENTITY ||
  197. componentMapping.g != RD::TEXTURE_SWIZZLE_IDENTITY ||
  198. componentMapping.b != RD::TEXTURE_SWIZZLE_IDENTITY ||
  199. componentMapping.a != RD::TEXTURE_SWIZZLE_IDENTITY);
  200. }
  201. };
  202. /** Describes the properties of a MTLPixelFormat or MTLVertexFormat. */
  203. struct MTLFormatDesc {
  204. union {
  205. MTLPixelFormat mtlPixelFormat;
  206. MTLVertexFormat mtlVertexFormat;
  207. };
  208. RD::DataFormat dataFormat = RD::DATA_FORMAT_MAX;
  209. MTLFmtCaps mtlFmtCaps;
  210. MTLViewClass mtlViewClass;
  211. MTLPixelFormat mtlPixelFormatLinear;
  212. const char *name = nullptr;
  213. inline bool isSupported() const { return (mtlPixelFormat != MTLPixelFormatInvalid) && (mtlFmtCaps != kMTLFmtCapsNone); }
  214. };
  215. class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) PixelFormats {
  216. using DataFormat = RD::DataFormat;
  217. public:
  218. /** Returns whether the DataFormat is supported by the GPU bound to this instance. */
  219. bool isSupported(DataFormat p_format);
  220. /** Returns whether the DataFormat is supported by this implementation, or can be substituted by one that is. */
  221. bool isSupportedOrSubstitutable(DataFormat p_format);
  222. /** Returns whether the specified Metal MTLPixelFormat can be used as a depth format. */
  223. _FORCE_INLINE_ bool isDepthFormat(MTLPixelFormat p_format) {
  224. switch (p_format) {
  225. case MTLPixelFormatDepth32Float:
  226. case MTLPixelFormatDepth16Unorm:
  227. case MTLPixelFormatDepth32Float_Stencil8:
  228. #if TARGET_OS_OSX
  229. case MTLPixelFormatDepth24Unorm_Stencil8:
  230. #endif
  231. return true;
  232. default:
  233. return false;
  234. }
  235. }
  236. /** Returns whether the specified Metal MTLPixelFormat can be used as a stencil format. */
  237. _FORCE_INLINE_ bool isStencilFormat(MTLPixelFormat p_format) {
  238. switch (p_format) {
  239. case MTLPixelFormatStencil8:
  240. #if TARGET_OS_OSX
  241. case MTLPixelFormatDepth24Unorm_Stencil8:
  242. case MTLPixelFormatX24_Stencil8:
  243. #endif
  244. case MTLPixelFormatDepth32Float_Stencil8:
  245. case MTLPixelFormatX32_Stencil8:
  246. return true;
  247. default:
  248. return false;
  249. }
  250. }
  251. /** Returns whether the specified Metal MTLPixelFormat is a PVRTC format. */
  252. bool isPVRTCFormat(MTLPixelFormat p_format);
  253. /** Returns the format type corresponding to the specified Godot pixel format, */
  254. MTLFormatType getFormatType(DataFormat p_format);
  255. /** Returns the format type corresponding to the specified Metal MTLPixelFormat, */
  256. MTLFormatType getFormatType(MTLPixelFormat p_format);
  257. /**
  258. * Returns the Metal MTLPixelFormat corresponding to the specified Godot pixel
  259. * or returns MTLPixelFormatInvalid if no corresponding MTLPixelFormat exists.
  260. */
  261. MTLPixelFormat getMTLPixelFormat(DataFormat p_format);
  262. /**
  263. * Returns the DataFormat corresponding to the specified Metal MTLPixelFormat,
  264. * or returns DATA_FORMAT_MAX if no corresponding DataFormat exists.
  265. */
  266. DataFormat getDataFormat(MTLPixelFormat p_format);
  267. /**
  268. * Returns the size, in bytes, of a texel block of the specified Godot pixel.
  269. * For uncompressed formats, the returned value corresponds to the size in bytes of a single texel.
  270. */
  271. uint32_t getBytesPerBlock(DataFormat p_format);
  272. /**
  273. * Returns the size, in bytes, of a texel block of the specified Metal format.
  274. * For uncompressed formats, the returned value corresponds to the size in bytes of a single texel.
  275. */
  276. uint32_t getBytesPerBlock(MTLPixelFormat p_format);
  277. /** Returns the number of planes of the specified chroma-subsampling (YCbCr) DataFormat */
  278. uint8_t getChromaSubsamplingPlaneCount(DataFormat p_format);
  279. /** Returns the number of bits per channel of the specified chroma-subsampling (YCbCr) DataFormat */
  280. uint8_t getChromaSubsamplingComponentBits(DataFormat p_format);
  281. /**
  282. * Returns the size, in bytes, of a texel of the specified Godot format.
  283. * The returned value may be fractional for certain compressed formats.
  284. */
  285. float getBytesPerTexel(DataFormat p_format);
  286. /**
  287. * Returns the size, in bytes, of a texel of the specified Metal format.
  288. * The returned value may be fractional for certain compressed formats.
  289. */
  290. float getBytesPerTexel(MTLPixelFormat p_format);
  291. /**
  292. * Returns the size, in bytes, of a row of texels of the specified Godot pixel format.
  293. *
  294. * For compressed formats, this takes into consideration the compression block size,
  295. * and p_texels_per_row should specify the width in texels, not blocks. The result is rounded
  296. * up if p_texels_per_row is not an integer multiple of the compression block width.
  297. */
  298. size_t getBytesPerRow(DataFormat p_format, uint32_t p_texels_per_row);
  299. /**
  300. * Returns the size, in bytes, of a row of texels of the specified Metal format.
  301. *
  302. * For compressed formats, this takes into consideration the compression block size,
  303. * and texelsPerRow should specify the width in texels, not blocks. The result is rounded
  304. * up if texelsPerRow is not an integer multiple of the compression block width.
  305. */
  306. size_t getBytesPerRow(MTLPixelFormat p_format, uint32_t p_texels_per_row);
  307. /**
  308. * Returns the size, in bytes, of a texture layer of the specified Godot pixel format.
  309. *
  310. * For compressed formats, this takes into consideration the compression block size,
  311. * and p_texel_rows_per_layer should specify the height in texels, not blocks. The result is
  312. * rounded up if p_texel_rows_per_layer is not an integer multiple of the compression block height.
  313. */
  314. size_t getBytesPerLayer(DataFormat p_format, size_t p_bytes_per_row, uint32_t p_texel_rows_per_layer);
  315. /**
  316. * Returns the size, in bytes, of a texture layer of the specified Metal format.
  317. * For compressed formats, this takes into consideration the compression block size,
  318. * and p_texel_rows_per_layer should specify the height in texels, not blocks. The result is
  319. * rounded up if p_texel_rows_per_layer is not an integer multiple of the compression block height.
  320. */
  321. size_t getBytesPerLayer(MTLPixelFormat p_format, size_t p_bytes_per_row, uint32_t p_texel_rows_per_layer);
  322. /** Returns whether or not the specified Godot format requires swizzling to use with Metal. */
  323. bool needsSwizzle(DataFormat p_format);
  324. /** Returns the Metal format capabilities supported by the specified Godot format, without substitution. */
  325. MTLFmtCaps getCapabilities(DataFormat p_format, bool p_extended = false);
  326. /** Returns the Metal format capabilities supported by the specified Metal format. */
  327. MTLFmtCaps getCapabilities(MTLPixelFormat p_format, bool p_extended = false);
  328. /**
  329. * Returns the Metal MTLVertexFormat corresponding to the specified
  330. * DataFormat as used as a vertex attribute format.
  331. */
  332. MTLVertexFormat getMTLVertexFormat(DataFormat p_format);
  333. #pragma mark Construction
  334. explicit PixelFormats(id<MTLDevice> p_device, const MetalFeatures &p_feat);
  335. protected:
  336. DataFormatDesc &getDataFormatDesc(DataFormat p_format);
  337. DataFormatDesc &getDataFormatDesc(MTLPixelFormat p_format);
  338. MTLFormatDesc &getMTLPixelFormatDesc(MTLPixelFormat p_format);
  339. MTLFmtCaps &getMTLPixelFormatCapsIf(MTLPixelFormat mtlPixFmt, bool cond);
  340. MTLFormatDesc &getMTLVertexFormatDesc(MTLVertexFormat p_format);
  341. void initDataFormatCapabilities();
  342. void initMTLPixelFormatCapabilities();
  343. void initMTLVertexFormatCapabilities(const MetalFeatures &p_feat);
  344. void modifyMTLFormatCapabilities(const MetalFeatures &p_feat);
  345. void buildDFFormatMaps();
  346. void addMTLPixelFormatDescImpl(MTLPixelFormat p_pix_fmt, MTLPixelFormat p_pix_fmt_linear,
  347. MTLViewClass p_view_class, MTLFmtCaps p_fmt_caps, const char *p_name);
  348. void addMTLVertexFormatDescImpl(MTLVertexFormat p_vert_fmt, MTLFmtCaps p_vert_caps, const char *name);
  349. id<MTLDevice> device;
  350. InflectionMap<DataFormat, DataFormatDesc, RD::DATA_FORMAT_MAX> _data_format_descs;
  351. InflectionMap<uint16_t, MTLFormatDesc, MTLPixelFormatX32_Stencil8 + 2> _mtl_pixel_format_descs; // The actual last enum value is not available on iOS.
  352. TightLocalVector<MTLFormatDesc> _mtl_vertex_format_descs;
  353. };
  354. #pragma clang diagnostic pop
  355. #endif // PIXEL_FORMATS_H