Format.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. namespace Ryujinx.Graphics.GAL
  2. {
  3. public enum Format
  4. {
  5. R8Unorm,
  6. R8Snorm,
  7. R8Uint,
  8. R8Sint,
  9. R16Float,
  10. R16Unorm,
  11. R16Snorm,
  12. R16Uint,
  13. R16Sint,
  14. R32Float,
  15. R32Uint,
  16. R32Sint,
  17. R8G8Unorm,
  18. R8G8Snorm,
  19. R8G8Uint,
  20. R8G8Sint,
  21. R16G16Float,
  22. R16G16Unorm,
  23. R16G16Snorm,
  24. R16G16Uint,
  25. R16G16Sint,
  26. R32G32Float,
  27. R32G32Uint,
  28. R32G32Sint,
  29. R8G8B8Unorm,
  30. R8G8B8Snorm,
  31. R8G8B8Uint,
  32. R8G8B8Sint,
  33. R16G16B16Float,
  34. R16G16B16Unorm,
  35. R16G16B16Snorm,
  36. R16G16B16Uint,
  37. R16G16B16Sint,
  38. R32G32B32Float,
  39. R32G32B32Uint,
  40. R32G32B32Sint,
  41. R8G8B8A8Unorm,
  42. R8G8B8A8Snorm,
  43. R8G8B8A8Uint,
  44. R8G8B8A8Sint,
  45. R16G16B16A16Float,
  46. R16G16B16A16Unorm,
  47. R16G16B16A16Snorm,
  48. R16G16B16A16Uint,
  49. R16G16B16A16Sint,
  50. R32G32B32A32Float,
  51. R32G32B32A32Uint,
  52. R32G32B32A32Sint,
  53. S8Uint,
  54. D16Unorm,
  55. S8UintD24Unorm,
  56. D32Float,
  57. D24UnormS8Uint,
  58. D32FloatS8Uint,
  59. R8G8B8A8Srgb,
  60. R4G4Unorm,
  61. R4G4B4A4Unorm,
  62. R5G5B5X1Unorm,
  63. R5G5B5A1Unorm,
  64. R5G6B5Unorm,
  65. R10G10B10A2Unorm,
  66. R10G10B10A2Uint,
  67. R11G11B10Float,
  68. R9G9B9E5Float,
  69. Bc1RgbaUnorm,
  70. Bc2Unorm,
  71. Bc3Unorm,
  72. Bc1RgbaSrgb,
  73. Bc2Srgb,
  74. Bc3Srgb,
  75. Bc4Unorm,
  76. Bc4Snorm,
  77. Bc5Unorm,
  78. Bc5Snorm,
  79. Bc7Unorm,
  80. Bc7Srgb,
  81. Bc6HSfloat,
  82. Bc6HUfloat,
  83. Etc2RgbUnorm,
  84. Etc2RgbaUnorm,
  85. Etc2RgbPtaUnorm,
  86. Etc2RgbSrgb,
  87. Etc2RgbaSrgb,
  88. Etc2RgbPtaSrgb,
  89. R8Uscaled,
  90. R8Sscaled,
  91. R16Uscaled,
  92. R16Sscaled,
  93. R32Uscaled,
  94. R32Sscaled,
  95. R8G8Uscaled,
  96. R8G8Sscaled,
  97. R16G16Uscaled,
  98. R16G16Sscaled,
  99. R32G32Uscaled,
  100. R32G32Sscaled,
  101. R8G8B8Uscaled,
  102. R8G8B8Sscaled,
  103. R16G16B16Uscaled,
  104. R16G16B16Sscaled,
  105. R32G32B32Uscaled,
  106. R32G32B32Sscaled,
  107. R8G8B8A8Uscaled,
  108. R8G8B8A8Sscaled,
  109. R16G16B16A16Uscaled,
  110. R16G16B16A16Sscaled,
  111. R32G32B32A32Uscaled,
  112. R32G32B32A32Sscaled,
  113. R10G10B10A2Snorm,
  114. R10G10B10A2Sint,
  115. R10G10B10A2Uscaled,
  116. R10G10B10A2Sscaled,
  117. Astc4x4Unorm,
  118. Astc5x4Unorm,
  119. Astc5x5Unorm,
  120. Astc6x5Unorm,
  121. Astc6x6Unorm,
  122. Astc8x5Unorm,
  123. Astc8x6Unorm,
  124. Astc8x8Unorm,
  125. Astc10x5Unorm,
  126. Astc10x6Unorm,
  127. Astc10x8Unorm,
  128. Astc10x10Unorm,
  129. Astc12x10Unorm,
  130. Astc12x12Unorm,
  131. Astc4x4Srgb,
  132. Astc5x4Srgb,
  133. Astc5x5Srgb,
  134. Astc6x5Srgb,
  135. Astc6x6Srgb,
  136. Astc8x5Srgb,
  137. Astc8x6Srgb,
  138. Astc8x8Srgb,
  139. Astc10x5Srgb,
  140. Astc10x6Srgb,
  141. Astc10x8Srgb,
  142. Astc10x10Srgb,
  143. Astc12x10Srgb,
  144. Astc12x12Srgb,
  145. B5G6R5Unorm,
  146. B5G5R5A1Unorm,
  147. A1B5G5R5Unorm,
  148. B8G8R8A8Unorm,
  149. B8G8R8A8Srgb
  150. }
  151. public static class FormatExtensions
  152. {
  153. /// <summary>
  154. /// The largest scalar size for a buffer format.
  155. /// </summary>
  156. public const int MaxBufferFormatScalarSize = 4;
  157. /// <summary>
  158. /// Gets the byte size for a single component of this format, or its packed size.
  159. /// </summary>
  160. /// <param name="format">Texture format</param>
  161. /// <returns>Byte size for a single component, or packed size</returns>
  162. public static int GetScalarSize(this Format format)
  163. {
  164. switch (format)
  165. {
  166. case Format.R8Unorm:
  167. case Format.R8Snorm:
  168. case Format.R8Uint:
  169. case Format.R8Sint:
  170. case Format.R8G8Unorm:
  171. case Format.R8G8Snorm:
  172. case Format.R8G8Uint:
  173. case Format.R8G8Sint:
  174. case Format.R8G8B8Unorm:
  175. case Format.R8G8B8Snorm:
  176. case Format.R8G8B8Uint:
  177. case Format.R8G8B8Sint:
  178. case Format.R8G8B8A8Unorm:
  179. case Format.R8G8B8A8Snorm:
  180. case Format.R8G8B8A8Uint:
  181. case Format.R8G8B8A8Sint:
  182. case Format.R8G8B8A8Srgb:
  183. case Format.R4G4Unorm:
  184. case Format.R8Uscaled:
  185. case Format.R8Sscaled:
  186. case Format.R8G8Uscaled:
  187. case Format.R8G8Sscaled:
  188. case Format.R8G8B8Uscaled:
  189. case Format.R8G8B8Sscaled:
  190. case Format.R8G8B8A8Uscaled:
  191. case Format.R8G8B8A8Sscaled:
  192. case Format.B8G8R8A8Unorm:
  193. case Format.B8G8R8A8Srgb:
  194. return 1;
  195. case Format.R16Float:
  196. case Format.R16Unorm:
  197. case Format.R16Snorm:
  198. case Format.R16Uint:
  199. case Format.R16Sint:
  200. case Format.R16G16Float:
  201. case Format.R16G16Unorm:
  202. case Format.R16G16Snorm:
  203. case Format.R16G16Uint:
  204. case Format.R16G16Sint:
  205. case Format.R16G16B16Float:
  206. case Format.R16G16B16Unorm:
  207. case Format.R16G16B16Snorm:
  208. case Format.R16G16B16Uint:
  209. case Format.R16G16B16Sint:
  210. case Format.R16G16B16A16Float:
  211. case Format.R16G16B16A16Unorm:
  212. case Format.R16G16B16A16Snorm:
  213. case Format.R16G16B16A16Uint:
  214. case Format.R16G16B16A16Sint:
  215. case Format.R4G4B4A4Unorm:
  216. case Format.R5G5B5X1Unorm:
  217. case Format.R5G5B5A1Unorm:
  218. case Format.R5G6B5Unorm:
  219. case Format.R16Uscaled:
  220. case Format.R16Sscaled:
  221. case Format.R16G16Uscaled:
  222. case Format.R16G16Sscaled:
  223. case Format.R16G16B16Uscaled:
  224. case Format.R16G16B16Sscaled:
  225. case Format.R16G16B16A16Uscaled:
  226. case Format.R16G16B16A16Sscaled:
  227. case Format.B5G6R5Unorm:
  228. case Format.B5G5R5A1Unorm:
  229. case Format.A1B5G5R5Unorm:
  230. return 2;
  231. case Format.R32Float:
  232. case Format.R32Uint:
  233. case Format.R32Sint:
  234. case Format.R32G32Float:
  235. case Format.R32G32Uint:
  236. case Format.R32G32Sint:
  237. case Format.R32G32B32Float:
  238. case Format.R32G32B32Uint:
  239. case Format.R32G32B32Sint:
  240. case Format.R32G32B32A32Float:
  241. case Format.R32G32B32A32Uint:
  242. case Format.R32G32B32A32Sint:
  243. case Format.R10G10B10A2Unorm:
  244. case Format.R10G10B10A2Uint:
  245. case Format.R11G11B10Float:
  246. case Format.R9G9B9E5Float:
  247. case Format.R32Uscaled:
  248. case Format.R32Sscaled:
  249. case Format.R32G32Uscaled:
  250. case Format.R32G32Sscaled:
  251. case Format.R32G32B32Uscaled:
  252. case Format.R32G32B32Sscaled:
  253. case Format.R32G32B32A32Uscaled:
  254. case Format.R32G32B32A32Sscaled:
  255. case Format.R10G10B10A2Snorm:
  256. case Format.R10G10B10A2Sint:
  257. case Format.R10G10B10A2Uscaled:
  258. case Format.R10G10B10A2Sscaled:
  259. return 4;
  260. case Format.S8Uint:
  261. return 1;
  262. case Format.D16Unorm:
  263. return 2;
  264. case Format.S8UintD24Unorm:
  265. case Format.D32Float:
  266. case Format.D24UnormS8Uint:
  267. return 4;
  268. case Format.D32FloatS8Uint:
  269. return 8;
  270. case Format.Bc1RgbaUnorm:
  271. case Format.Bc1RgbaSrgb:
  272. return 8;
  273. case Format.Bc2Unorm:
  274. case Format.Bc3Unorm:
  275. case Format.Bc2Srgb:
  276. case Format.Bc3Srgb:
  277. case Format.Bc4Unorm:
  278. case Format.Bc4Snorm:
  279. case Format.Bc5Unorm:
  280. case Format.Bc5Snorm:
  281. case Format.Bc7Unorm:
  282. case Format.Bc7Srgb:
  283. case Format.Bc6HSfloat:
  284. case Format.Bc6HUfloat:
  285. return 16;
  286. case Format.Etc2RgbUnorm:
  287. case Format.Etc2RgbPtaUnorm:
  288. case Format.Etc2RgbSrgb:
  289. case Format.Etc2RgbPtaSrgb:
  290. return 8;
  291. case Format.Etc2RgbaUnorm:
  292. case Format.Etc2RgbaSrgb:
  293. return 16;
  294. case Format.Astc4x4Unorm:
  295. case Format.Astc5x4Unorm:
  296. case Format.Astc5x5Unorm:
  297. case Format.Astc6x5Unorm:
  298. case Format.Astc6x6Unorm:
  299. case Format.Astc8x5Unorm:
  300. case Format.Astc8x6Unorm:
  301. case Format.Astc8x8Unorm:
  302. case Format.Astc10x5Unorm:
  303. case Format.Astc10x6Unorm:
  304. case Format.Astc10x8Unorm:
  305. case Format.Astc10x10Unorm:
  306. case Format.Astc12x10Unorm:
  307. case Format.Astc12x12Unorm:
  308. case Format.Astc4x4Srgb:
  309. case Format.Astc5x4Srgb:
  310. case Format.Astc5x5Srgb:
  311. case Format.Astc6x5Srgb:
  312. case Format.Astc6x6Srgb:
  313. case Format.Astc8x5Srgb:
  314. case Format.Astc8x6Srgb:
  315. case Format.Astc8x8Srgb:
  316. case Format.Astc10x5Srgb:
  317. case Format.Astc10x6Srgb:
  318. case Format.Astc10x8Srgb:
  319. case Format.Astc10x10Srgb:
  320. case Format.Astc12x10Srgb:
  321. case Format.Astc12x12Srgb:
  322. return 16;
  323. }
  324. return 1;
  325. }
  326. /// <summary>
  327. /// Checks if the texture format is valid to use as image format.
  328. /// </summary>
  329. /// <param name="format">Texture format</param>
  330. /// <returns>True if the texture can be used as image, false otherwise</returns>
  331. public static bool IsImageCompatible(this Format format)
  332. {
  333. switch (format)
  334. {
  335. case Format.R8Unorm:
  336. case Format.R8Snorm:
  337. case Format.R8Uint:
  338. case Format.R8Sint:
  339. case Format.R16Float:
  340. case Format.R16Unorm:
  341. case Format.R16Snorm:
  342. case Format.R16Uint:
  343. case Format.R16Sint:
  344. case Format.R32Float:
  345. case Format.R32Uint:
  346. case Format.R32Sint:
  347. case Format.R8G8Unorm:
  348. case Format.R8G8Snorm:
  349. case Format.R8G8Uint:
  350. case Format.R8G8Sint:
  351. case Format.R16G16Float:
  352. case Format.R16G16Unorm:
  353. case Format.R16G16Snorm:
  354. case Format.R16G16Uint:
  355. case Format.R16G16Sint:
  356. case Format.R32G32Float:
  357. case Format.R32G32Uint:
  358. case Format.R32G32Sint:
  359. case Format.R8G8B8A8Unorm:
  360. case Format.R8G8B8A8Snorm:
  361. case Format.R8G8B8A8Uint:
  362. case Format.R8G8B8A8Sint:
  363. case Format.R16G16B16A16Float:
  364. case Format.R16G16B16A16Unorm:
  365. case Format.R16G16B16A16Snorm:
  366. case Format.R16G16B16A16Uint:
  367. case Format.R16G16B16A16Sint:
  368. case Format.R32G32B32A32Float:
  369. case Format.R32G32B32A32Uint:
  370. case Format.R32G32B32A32Sint:
  371. case Format.R10G10B10A2Unorm:
  372. case Format.R10G10B10A2Uint:
  373. case Format.R11G11B10Float:
  374. return true;
  375. }
  376. return false;
  377. }
  378. /// <summary>
  379. /// Checks if the texture format is valid to use as render target color format.
  380. /// </summary>
  381. /// <param name="format">Texture format</param>
  382. /// <returns>True if the texture can be used as render target, false otherwise</returns>
  383. public static bool IsRtColorCompatible(this Format format)
  384. {
  385. switch (format)
  386. {
  387. case Format.R32G32B32A32Float:
  388. case Format.R32G32B32A32Sint:
  389. case Format.R32G32B32A32Uint:
  390. case Format.R16G16B16A16Unorm:
  391. case Format.R16G16B16A16Snorm:
  392. case Format.R16G16B16A16Sint:
  393. case Format.R16G16B16A16Uint:
  394. case Format.R16G16B16A16Float:
  395. case Format.R32G32Float:
  396. case Format.R32G32Sint:
  397. case Format.R32G32Uint:
  398. case Format.B8G8R8A8Unorm:
  399. case Format.B8G8R8A8Srgb:
  400. case Format.R10G10B10A2Unorm:
  401. case Format.R10G10B10A2Uint:
  402. case Format.R8G8B8A8Unorm:
  403. case Format.R8G8B8A8Srgb:
  404. case Format.R8G8B8A8Snorm:
  405. case Format.R8G8B8A8Sint:
  406. case Format.R8G8B8A8Uint:
  407. case Format.R16G16Unorm:
  408. case Format.R16G16Snorm:
  409. case Format.R16G16Sint:
  410. case Format.R16G16Uint:
  411. case Format.R16G16Float:
  412. case Format.R11G11B10Float:
  413. case Format.R32Sint:
  414. case Format.R32Uint:
  415. case Format.R32Float:
  416. case Format.B5G6R5Unorm:
  417. case Format.B5G5R5A1Unorm:
  418. case Format.R8G8Unorm:
  419. case Format.R8G8Snorm:
  420. case Format.R8G8Sint:
  421. case Format.R8G8Uint:
  422. case Format.R16Unorm:
  423. case Format.R16Snorm:
  424. case Format.R16Sint:
  425. case Format.R16Uint:
  426. case Format.R16Float:
  427. case Format.R8Unorm:
  428. case Format.R8Snorm:
  429. case Format.R8Sint:
  430. case Format.R8Uint:
  431. return true;
  432. }
  433. return false;
  434. }
  435. /// <summary>
  436. /// Checks if the texture format is an ASTC format.
  437. /// </summary>
  438. /// <param name="format">Texture format</param>
  439. /// <returns>True if the texture format is an ASTC format, false otherwise</returns>
  440. public static bool IsAstc(this Format format)
  441. {
  442. return format.IsAstcUnorm() || format.IsAstcSrgb();
  443. }
  444. /// <summary>
  445. /// Checks if the texture format is an ASTC Unorm format.
  446. /// </summary>
  447. /// <param name="format">Texture format</param>
  448. /// <returns>True if the texture format is an ASTC Unorm format, false otherwise</returns>
  449. public static bool IsAstcUnorm(this Format format)
  450. {
  451. switch (format)
  452. {
  453. case Format.Astc4x4Unorm:
  454. case Format.Astc5x4Unorm:
  455. case Format.Astc5x5Unorm:
  456. case Format.Astc6x5Unorm:
  457. case Format.Astc6x6Unorm:
  458. case Format.Astc8x5Unorm:
  459. case Format.Astc8x6Unorm:
  460. case Format.Astc8x8Unorm:
  461. case Format.Astc10x5Unorm:
  462. case Format.Astc10x6Unorm:
  463. case Format.Astc10x8Unorm:
  464. case Format.Astc10x10Unorm:
  465. case Format.Astc12x10Unorm:
  466. case Format.Astc12x12Unorm:
  467. return true;
  468. }
  469. return false;
  470. }
  471. /// <summary>
  472. /// Checks if the texture format is an ASTC SRGB format.
  473. /// </summary>
  474. /// <param name="format">Texture format</param>
  475. /// <returns>True if the texture format is an ASTC SRGB format, false otherwise</returns>
  476. public static bool IsAstcSrgb(this Format format)
  477. {
  478. switch (format)
  479. {
  480. case Format.Astc4x4Srgb:
  481. case Format.Astc5x4Srgb:
  482. case Format.Astc5x5Srgb:
  483. case Format.Astc6x5Srgb:
  484. case Format.Astc6x6Srgb:
  485. case Format.Astc8x5Srgb:
  486. case Format.Astc8x6Srgb:
  487. case Format.Astc8x8Srgb:
  488. case Format.Astc10x5Srgb:
  489. case Format.Astc10x6Srgb:
  490. case Format.Astc10x8Srgb:
  491. case Format.Astc10x10Srgb:
  492. case Format.Astc12x10Srgb:
  493. case Format.Astc12x12Srgb:
  494. return true;
  495. }
  496. return false;
  497. }
  498. /// <summary>
  499. /// Checks if the texture format is a BGR format.
  500. /// </summary>
  501. /// <param name="format">Texture format</param>
  502. /// <returns>True if the texture format is a BGR format, false otherwise</returns>
  503. public static bool IsBgr(this Format format)
  504. {
  505. switch (format)
  506. {
  507. case Format.B5G6R5Unorm:
  508. case Format.B5G5R5A1Unorm:
  509. case Format.B8G8R8A8Unorm:
  510. case Format.B8G8R8A8Srgb:
  511. return true;
  512. }
  513. return false;
  514. }
  515. /// <summary>
  516. /// Checks if the texture format is a depth, stencil or depth-stencil format.
  517. /// </summary>
  518. /// <param name="format">Texture format</param>
  519. /// <returns>True if the format is a depth, stencil or depth-stencil format, false otherwise</returns>
  520. public static bool IsDepthOrStencil(this Format format)
  521. {
  522. switch (format)
  523. {
  524. case Format.D16Unorm:
  525. case Format.D24UnormS8Uint:
  526. case Format.S8UintD24Unorm:
  527. case Format.D32Float:
  528. case Format.D32FloatS8Uint:
  529. case Format.S8Uint:
  530. return true;
  531. }
  532. return false;
  533. }
  534. /// <summary>
  535. /// Checks if the texture format is an unsigned integer color format.
  536. /// </summary>
  537. /// <param name="format">Texture format</param>
  538. /// <returns>True if the texture format is an unsigned integer color format, false otherwise</returns>
  539. public static bool IsUint(this Format format)
  540. {
  541. switch (format)
  542. {
  543. case Format.R8Uint:
  544. case Format.R16Uint:
  545. case Format.R32Uint:
  546. case Format.R8G8Uint:
  547. case Format.R16G16Uint:
  548. case Format.R32G32Uint:
  549. case Format.R8G8B8Uint:
  550. case Format.R16G16B16Uint:
  551. case Format.R32G32B32Uint:
  552. case Format.R8G8B8A8Uint:
  553. case Format.R16G16B16A16Uint:
  554. case Format.R32G32B32A32Uint:
  555. case Format.R10G10B10A2Uint:
  556. return true;
  557. }
  558. return false;
  559. }
  560. /// <summary>
  561. /// Checks if the texture format is a signed integer color format.
  562. /// </summary>
  563. /// <param name="format">Texture format</param>
  564. /// <returns>True if the texture format is a signed integer color format, false otherwise</returns>
  565. public static bool IsSint(this Format format)
  566. {
  567. switch (format)
  568. {
  569. case Format.R8Sint:
  570. case Format.R16Sint:
  571. case Format.R32Sint:
  572. case Format.R8G8Sint:
  573. case Format.R16G16Sint:
  574. case Format.R32G32Sint:
  575. case Format.R8G8B8Sint:
  576. case Format.R16G16B16Sint:
  577. case Format.R32G32B32Sint:
  578. case Format.R8G8B8A8Sint:
  579. case Format.R16G16B16A16Sint:
  580. case Format.R32G32B32A32Sint:
  581. case Format.R10G10B10A2Sint:
  582. return true;
  583. }
  584. return false;
  585. }
  586. /// <summary>
  587. /// Checks if the texture format is an integer color format.
  588. /// </summary>
  589. /// <param name="format">Texture format</param>
  590. /// <returns>True if the texture format is an integer color format, false otherwise</returns>
  591. public static bool IsInteger(this Format format)
  592. {
  593. return format.IsUint() || format.IsSint();
  594. }
  595. }
  596. }