pixel_formats.h 16 KB

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