pixel_formats.h 17 KB

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