ImageFormatConverter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. namespace Ryujinx.Graphics.Gal
  3. {
  4. public static class ImageFormatConverter
  5. {
  6. public static GalImageFormat ConvertTexture(
  7. GalTextureFormat Format,
  8. GalTextureType RType,
  9. GalTextureType GType,
  10. GalTextureType BType,
  11. GalTextureType AType)
  12. {
  13. if (RType != GType || RType != BType || RType != AType)
  14. {
  15. throw new NotImplementedException("Per component types are not implemented");
  16. }
  17. GalTextureType Type = RType;
  18. switch (Type)
  19. {
  20. case GalTextureType.Snorm:
  21. switch (Format)
  22. {
  23. case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_SNORM;
  24. case GalTextureFormat.A8B8G8R8: return GalImageFormat.A8B8G8R8_SNORM_PACK32;
  25. case GalTextureFormat.A2B10G10R10: return GalImageFormat.A2B10G10R10_SNORM_PACK32;
  26. case GalTextureFormat.G8R8: return GalImageFormat.R8G8_SNORM;
  27. case GalTextureFormat.R16: return GalImageFormat.R16_SNORM;
  28. case GalTextureFormat.R8: return GalImageFormat.R8_SNORM;
  29. case GalTextureFormat.BC4: return GalImageFormat.BC4_SNORM_BLOCK;
  30. case GalTextureFormat.BC5: return GalImageFormat.BC5_SNORM_BLOCK;
  31. }
  32. break;
  33. case GalTextureType.Unorm:
  34. switch (Format)
  35. {
  36. case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_UNORM;
  37. case GalTextureFormat.A8B8G8R8: return GalImageFormat.A8B8G8R8_UNORM_PACK32;
  38. case GalTextureFormat.A2B10G10R10: return GalImageFormat.A2B10G10R10_UNORM_PACK32;
  39. case GalTextureFormat.A4B4G4R4: return GalImageFormat.R4G4B4A4_UNORM_PACK16_REVERSED;
  40. case GalTextureFormat.A1B5G5R5: return GalImageFormat.A1R5G5B5_UNORM_PACK16;
  41. case GalTextureFormat.B5G6R5: return GalImageFormat.B5G6R5_UNORM_PACK16;
  42. case GalTextureFormat.BC7U: return GalImageFormat.BC7_UNORM_BLOCK;
  43. case GalTextureFormat.G8R8: return GalImageFormat.R8G8_UNORM;
  44. case GalTextureFormat.R16: return GalImageFormat.R16_UNORM;
  45. case GalTextureFormat.R8: return GalImageFormat.R8_UNORM;
  46. case GalTextureFormat.BC1: return GalImageFormat.BC1_RGBA_UNORM_BLOCK;
  47. case GalTextureFormat.BC2: return GalImageFormat.BC2_UNORM_BLOCK;
  48. case GalTextureFormat.BC3: return GalImageFormat.BC3_UNORM_BLOCK;
  49. case GalTextureFormat.BC4: return GalImageFormat.BC4_UNORM_BLOCK;
  50. case GalTextureFormat.BC5: return GalImageFormat.BC5_UNORM_BLOCK;
  51. case GalTextureFormat.Z24S8: return GalImageFormat.D24_UNORM_S8_UINT;
  52. case GalTextureFormat.Astc2D4x4: return GalImageFormat.ASTC_4x4_UNORM_BLOCK;
  53. case GalTextureFormat.Astc2D5x5: return GalImageFormat.ASTC_5x5_UNORM_BLOCK;
  54. case GalTextureFormat.Astc2D6x6: return GalImageFormat.ASTC_6x6_UNORM_BLOCK;
  55. case GalTextureFormat.Astc2D8x8: return GalImageFormat.ASTC_8x8_UNORM_BLOCK;
  56. case GalTextureFormat.Astc2D10x10: return GalImageFormat.ASTC_10x10_UNORM_BLOCK;
  57. case GalTextureFormat.Astc2D12x12: return GalImageFormat.ASTC_12x12_UNORM_BLOCK;
  58. case GalTextureFormat.Astc2D5x4: return GalImageFormat.ASTC_5x4_UNORM_BLOCK;
  59. case GalTextureFormat.Astc2D6x5: return GalImageFormat.ASTC_6x5_UNORM_BLOCK;
  60. case GalTextureFormat.Astc2D8x6: return GalImageFormat.ASTC_8x6_UNORM_BLOCK;
  61. case GalTextureFormat.Astc2D10x8: return GalImageFormat.ASTC_10x8_UNORM_BLOCK;
  62. case GalTextureFormat.Astc2D12x10: return GalImageFormat.ASTC_12x10_UNORM_BLOCK;
  63. case GalTextureFormat.Astc2D8x5: return GalImageFormat.ASTC_8x5_UNORM_BLOCK;
  64. case GalTextureFormat.Astc2D10x5: return GalImageFormat.ASTC_10x5_UNORM_BLOCK;
  65. case GalTextureFormat.Astc2D10x6: return GalImageFormat.ASTC_10x6_UNORM_BLOCK;
  66. }
  67. break;
  68. case GalTextureType.Sint:
  69. switch (Format)
  70. {
  71. case GalTextureFormat.R32G32B32A32: return GalImageFormat.R32G32B32A32_SINT;
  72. case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_SINT;
  73. case GalTextureFormat.R32G32: return GalImageFormat.R32G32_SINT;
  74. case GalTextureFormat.A8B8G8R8: return GalImageFormat.A8B8G8R8_SINT_PACK32;
  75. case GalTextureFormat.A2B10G10R10: return GalImageFormat.A2B10G10R10_SINT_PACK32;
  76. case GalTextureFormat.R32: return GalImageFormat.R32_SINT;
  77. case GalTextureFormat.G8R8: return GalImageFormat.R8G8_SINT;
  78. case GalTextureFormat.R16: return GalImageFormat.R16_SINT;
  79. case GalTextureFormat.R8: return GalImageFormat.R8_SINT;
  80. }
  81. break;
  82. case GalTextureType.Uint:
  83. switch (Format)
  84. {
  85. case GalTextureFormat.R32G32B32A32: return GalImageFormat.R32G32B32A32_UINT;
  86. case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_UINT;
  87. case GalTextureFormat.R32G32: return GalImageFormat.R32G32_UINT;
  88. case GalTextureFormat.A8B8G8R8: return GalImageFormat.A8B8G8R8_UINT_PACK32;
  89. case GalTextureFormat.A2B10G10R10: return GalImageFormat.A2B10G10R10_UINT_PACK32;
  90. case GalTextureFormat.R32: return GalImageFormat.R32_UINT;
  91. case GalTextureFormat.G8R8: return GalImageFormat.R8G8_UINT;
  92. case GalTextureFormat.R16: return GalImageFormat.R16_UINT;
  93. case GalTextureFormat.R8: return GalImageFormat.R8_UINT;
  94. }
  95. break;
  96. case GalTextureType.Snorm_Force_Fp16:
  97. //TODO
  98. break;
  99. case GalTextureType.Unorm_Force_Fp16:
  100. //TODO
  101. break;
  102. case GalTextureType.Float:
  103. switch (Format)
  104. {
  105. case GalTextureFormat.R32G32B32A32: return GalImageFormat.R32G32B32A32_SFLOAT;
  106. case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_SFLOAT;
  107. case GalTextureFormat.R32G32: return GalImageFormat.R32G32_SFLOAT;
  108. case GalTextureFormat.R32: return GalImageFormat.R32_SFLOAT;
  109. case GalTextureFormat.BC6H_SF16: return GalImageFormat.BC6H_SFLOAT_BLOCK;
  110. case GalTextureFormat.BC6H_UF16: return GalImageFormat.BC6H_UFLOAT_BLOCK;
  111. case GalTextureFormat.R16: return GalImageFormat.R16_SFLOAT;
  112. case GalTextureFormat.BF10GF11RF11: return GalImageFormat.B10G11R11_UFLOAT_PACK32;
  113. case GalTextureFormat.ZF32: return GalImageFormat.D32_SFLOAT;
  114. }
  115. break;
  116. }
  117. throw new NotImplementedException("0x" + Format.ToString("x2") + " " + Type.ToString());
  118. }
  119. public static GalImageFormat ConvertFrameBuffer(GalFrameBufferFormat Format)
  120. {
  121. switch (Format)
  122. {
  123. case GalFrameBufferFormat.R32Float: return GalImageFormat.R32_SFLOAT;
  124. case GalFrameBufferFormat.RGB10A2Unorm: return GalImageFormat.A2B10G10R10_UNORM_PACK32;
  125. case GalFrameBufferFormat.RGBA8Srgb: return GalImageFormat.A8B8G8R8_SRGB_PACK32;
  126. case GalFrameBufferFormat.RGBA16Float: return GalImageFormat.R16G16B16A16_SFLOAT;
  127. case GalFrameBufferFormat.R16Float: return GalImageFormat.R16_SFLOAT;
  128. case GalFrameBufferFormat.R8Unorm: return GalImageFormat.R8_UNORM;
  129. case GalFrameBufferFormat.RGBA8Unorm: return GalImageFormat.A8B8G8R8_UNORM_PACK32;
  130. case GalFrameBufferFormat.R11G11B10Float: return GalImageFormat.B10G11R11_UFLOAT_PACK32;
  131. case GalFrameBufferFormat.RGBA32Float: return GalImageFormat.R32G32B32A32_SFLOAT;
  132. case GalFrameBufferFormat.RG16Snorm: return GalImageFormat.R16G16_SNORM;
  133. case GalFrameBufferFormat.RG16Float: return GalImageFormat.R16G16_SFLOAT;
  134. case GalFrameBufferFormat.RG8Snorm: return GalImageFormat.R8_SNORM;
  135. case GalFrameBufferFormat.RGBA8Snorm: return GalImageFormat.A8B8G8R8_SNORM_PACK32;
  136. case GalFrameBufferFormat.RG8Unorm: return GalImageFormat.R8G8_UNORM;
  137. case GalFrameBufferFormat.RG32Float: return GalImageFormat.R32G32_SFLOAT;
  138. case GalFrameBufferFormat.RG32Sint: return GalImageFormat.R32G32_SINT;
  139. case GalFrameBufferFormat.RG32Uint: return GalImageFormat.R32G32_UINT;
  140. }
  141. throw new NotImplementedException(Format.ToString());
  142. }
  143. public static GalImageFormat ConvertZeta(GalZetaFormat Format)
  144. {
  145. switch (Format)
  146. {
  147. case GalZetaFormat.Z32Float: return GalImageFormat.D32_SFLOAT;
  148. case GalZetaFormat.S8Z24Unorm: return GalImageFormat.D24_UNORM_S8_UINT;
  149. case GalZetaFormat.Z16Unorm: return GalImageFormat.D16_UNORM;
  150. }
  151. throw new NotImplementedException(Format.ToString());
  152. }
  153. public static bool HasColor(GalImageFormat Format)
  154. {
  155. switch (Format)
  156. {
  157. case GalImageFormat.R32G32B32A32_SFLOAT:
  158. case GalImageFormat.R32G32B32A32_SINT:
  159. case GalImageFormat.R32G32B32A32_UINT:
  160. case GalImageFormat.R16G16B16A16_SFLOAT:
  161. case GalImageFormat.R16G16B16A16_SINT:
  162. case GalImageFormat.R16G16B16A16_UINT:
  163. case GalImageFormat.R32G32_SFLOAT:
  164. case GalImageFormat.R32G32_SINT:
  165. case GalImageFormat.R32G32_UINT:
  166. case GalImageFormat.A8B8G8R8_SNORM_PACK32:
  167. case GalImageFormat.A8B8G8R8_UNORM_PACK32:
  168. case GalImageFormat.A8B8G8R8_SINT_PACK32:
  169. case GalImageFormat.A8B8G8R8_UINT_PACK32:
  170. case GalImageFormat.A2B10G10R10_SINT_PACK32:
  171. case GalImageFormat.A2B10G10R10_SNORM_PACK32:
  172. case GalImageFormat.A2B10G10R10_UINT_PACK32:
  173. case GalImageFormat.A2B10G10R10_UNORM_PACK32:
  174. case GalImageFormat.R32_SFLOAT:
  175. case GalImageFormat.R32_SINT:
  176. case GalImageFormat.R32_UINT:
  177. case GalImageFormat.BC6H_SFLOAT_BLOCK:
  178. case GalImageFormat.BC6H_UFLOAT_BLOCK:
  179. case GalImageFormat.A1R5G5B5_UNORM_PACK16:
  180. case GalImageFormat.B5G6R5_UNORM_PACK16:
  181. case GalImageFormat.BC7_UNORM_BLOCK:
  182. case GalImageFormat.R16G16_SFLOAT:
  183. case GalImageFormat.R16G16_SINT:
  184. case GalImageFormat.R16G16_SNORM:
  185. case GalImageFormat.R16G16_UNORM:
  186. case GalImageFormat.R8G8_SINT:
  187. case GalImageFormat.R8G8_SNORM:
  188. case GalImageFormat.R8G8_UINT:
  189. case GalImageFormat.R8G8_UNORM:
  190. case GalImageFormat.R16_SFLOAT:
  191. case GalImageFormat.R16_SINT:
  192. case GalImageFormat.R16_SNORM:
  193. case GalImageFormat.R16_UINT:
  194. case GalImageFormat.R16_UNORM:
  195. case GalImageFormat.R8_SINT:
  196. case GalImageFormat.R8_SNORM:
  197. case GalImageFormat.R8_UINT:
  198. case GalImageFormat.R8_UNORM:
  199. case GalImageFormat.B10G11R11_UFLOAT_PACK32:
  200. case GalImageFormat.BC1_RGBA_UNORM_BLOCK:
  201. case GalImageFormat.BC2_UNORM_BLOCK:
  202. case GalImageFormat.BC3_UNORM_BLOCK:
  203. case GalImageFormat.BC4_UNORM_BLOCK:
  204. case GalImageFormat.BC5_UNORM_BLOCK:
  205. case GalImageFormat.ASTC_4x4_UNORM_BLOCK:
  206. case GalImageFormat.ASTC_5x5_UNORM_BLOCK:
  207. case GalImageFormat.ASTC_6x6_UNORM_BLOCK:
  208. case GalImageFormat.ASTC_8x8_UNORM_BLOCK:
  209. case GalImageFormat.ASTC_10x10_UNORM_BLOCK:
  210. case GalImageFormat.ASTC_12x12_UNORM_BLOCK:
  211. case GalImageFormat.ASTC_5x4_UNORM_BLOCK:
  212. case GalImageFormat.ASTC_6x5_UNORM_BLOCK:
  213. case GalImageFormat.ASTC_8x6_UNORM_BLOCK:
  214. case GalImageFormat.ASTC_10x8_UNORM_BLOCK:
  215. case GalImageFormat.ASTC_12x10_UNORM_BLOCK:
  216. case GalImageFormat.ASTC_8x5_UNORM_BLOCK:
  217. case GalImageFormat.ASTC_10x5_UNORM_BLOCK:
  218. case GalImageFormat.ASTC_10x6_UNORM_BLOCK:
  219. case GalImageFormat.R4G4B4A4_UNORM_PACK16_REVERSED:
  220. return true;
  221. case GalImageFormat.D24_UNORM_S8_UINT:
  222. case GalImageFormat.D32_SFLOAT:
  223. case GalImageFormat.D16_UNORM:
  224. return false;
  225. }
  226. throw new NotImplementedException(Format.ToString());
  227. }
  228. public static bool HasDepth(GalImageFormat Format)
  229. {
  230. switch (Format)
  231. {
  232. case GalImageFormat.D24_UNORM_S8_UINT:
  233. case GalImageFormat.D32_SFLOAT:
  234. case GalImageFormat.D16_UNORM:
  235. return true;
  236. }
  237. //Depth formats are fewer than colors, so it's harder to miss one
  238. //Instead of checking for individual formats, return false
  239. return false;
  240. }
  241. public static bool HasStencil(GalImageFormat Format)
  242. {
  243. switch (Format)
  244. {
  245. case GalImageFormat.D24_UNORM_S8_UINT:
  246. return true;
  247. }
  248. return false;
  249. }
  250. }
  251. }