/ src / Ryujinx.Graphics.Vulkan / FormatTable.cs
FormatTable.cs
  1  using Ryujinx.Graphics.GAL;
  2  using System;
  3  using System.Collections.Generic;
  4  using VkFormat = Silk.NET.Vulkan.Format;
  5  
  6  namespace Ryujinx.Graphics.Vulkan
  7  {
  8      static class FormatTable
  9      {
 10          private static readonly VkFormat[] _table;
 11          private static readonly Dictionary<VkFormat, Format> _reverseMap;
 12  
 13          static FormatTable()
 14          {
 15              _table = new VkFormat[Enum.GetNames(typeof(Format)).Length];
 16              _reverseMap = new Dictionary<VkFormat, Format>();
 17  
 18  #pragma warning disable IDE0055 // Disable formatting
 19              Add(Format.R8Unorm,             VkFormat.R8Unorm);
 20              Add(Format.R8Snorm,             VkFormat.R8SNorm);
 21              Add(Format.R8Uint,              VkFormat.R8Uint);
 22              Add(Format.R8Sint,              VkFormat.R8Sint);
 23              Add(Format.R16Float,            VkFormat.R16Sfloat);
 24              Add(Format.R16Unorm,            VkFormat.R16Unorm);
 25              Add(Format.R16Snorm,            VkFormat.R16SNorm);
 26              Add(Format.R16Uint,             VkFormat.R16Uint);
 27              Add(Format.R16Sint,             VkFormat.R16Sint);
 28              Add(Format.R32Float,            VkFormat.R32Sfloat);
 29              Add(Format.R32Uint,             VkFormat.R32Uint);
 30              Add(Format.R32Sint,             VkFormat.R32Sint);
 31              Add(Format.R8G8Unorm,           VkFormat.R8G8Unorm);
 32              Add(Format.R8G8Snorm,           VkFormat.R8G8SNorm);
 33              Add(Format.R8G8Uint,            VkFormat.R8G8Uint);
 34              Add(Format.R8G8Sint,            VkFormat.R8G8Sint);
 35              Add(Format.R16G16Float,         VkFormat.R16G16Sfloat);
 36              Add(Format.R16G16Unorm,         VkFormat.R16G16Unorm);
 37              Add(Format.R16G16Snorm,         VkFormat.R16G16SNorm);
 38              Add(Format.R16G16Uint,          VkFormat.R16G16Uint);
 39              Add(Format.R16G16Sint,          VkFormat.R16G16Sint);
 40              Add(Format.R32G32Float,         VkFormat.R32G32Sfloat);
 41              Add(Format.R32G32Uint,          VkFormat.R32G32Uint);
 42              Add(Format.R32G32Sint,          VkFormat.R32G32Sint);
 43              Add(Format.R8G8B8Unorm,         VkFormat.R8G8B8Unorm);
 44              Add(Format.R8G8B8Snorm,         VkFormat.R8G8B8SNorm);
 45              Add(Format.R8G8B8Uint,          VkFormat.R8G8B8Uint);
 46              Add(Format.R8G8B8Sint,          VkFormat.R8G8B8Sint);
 47              Add(Format.R16G16B16Float,      VkFormat.R16G16B16Sfloat);
 48              Add(Format.R16G16B16Unorm,      VkFormat.R16G16B16Unorm);
 49              Add(Format.R16G16B16Snorm,      VkFormat.R16G16B16SNorm);
 50              Add(Format.R16G16B16Uint,       VkFormat.R16G16B16Uint);
 51              Add(Format.R16G16B16Sint,       VkFormat.R16G16B16Sint);
 52              Add(Format.R32G32B32Float,      VkFormat.R32G32B32Sfloat);
 53              Add(Format.R32G32B32Uint,       VkFormat.R32G32B32Uint);
 54              Add(Format.R32G32B32Sint,       VkFormat.R32G32B32Sint);
 55              Add(Format.R8G8B8A8Unorm,       VkFormat.R8G8B8A8Unorm);
 56              Add(Format.R8G8B8A8Snorm,       VkFormat.R8G8B8A8SNorm);
 57              Add(Format.R8G8B8A8Uint,        VkFormat.R8G8B8A8Uint);
 58              Add(Format.R8G8B8A8Sint,        VkFormat.R8G8B8A8Sint);
 59              Add(Format.R16G16B16A16Float,   VkFormat.R16G16B16A16Sfloat);
 60              Add(Format.R16G16B16A16Unorm,   VkFormat.R16G16B16A16Unorm);
 61              Add(Format.R16G16B16A16Snorm,   VkFormat.R16G16B16A16SNorm);
 62              Add(Format.R16G16B16A16Uint,    VkFormat.R16G16B16A16Uint);
 63              Add(Format.R16G16B16A16Sint,    VkFormat.R16G16B16A16Sint);
 64              Add(Format.R32G32B32A32Float,   VkFormat.R32G32B32A32Sfloat);
 65              Add(Format.R32G32B32A32Uint,    VkFormat.R32G32B32A32Uint);
 66              Add(Format.R32G32B32A32Sint,    VkFormat.R32G32B32A32Sint);
 67              Add(Format.S8Uint,              VkFormat.S8Uint);
 68              Add(Format.D16Unorm,            VkFormat.D16Unorm);
 69              Add(Format.S8UintD24Unorm,      VkFormat.D24UnormS8Uint);
 70              Add(Format.X8UintD24Unorm,      VkFormat.X8D24UnormPack32);
 71              Add(Format.D32Float,            VkFormat.D32Sfloat);
 72              Add(Format.D24UnormS8Uint,      VkFormat.D24UnormS8Uint);
 73              Add(Format.D32FloatS8Uint,      VkFormat.D32SfloatS8Uint);
 74              Add(Format.R8G8B8A8Srgb,        VkFormat.R8G8B8A8Srgb);
 75              Add(Format.R4G4Unorm,           VkFormat.R4G4UnormPack8);
 76              Add(Format.R4G4B4A4Unorm,       VkFormat.A4B4G4R4UnormPack16Ext);
 77              Add(Format.R5G5B5X1Unorm,       VkFormat.A1R5G5B5UnormPack16);
 78              Add(Format.R5G5B5A1Unorm,       VkFormat.A1R5G5B5UnormPack16);
 79              Add(Format.R5G6B5Unorm,         VkFormat.R5G6B5UnormPack16);
 80              Add(Format.R10G10B10A2Unorm,    VkFormat.A2B10G10R10UnormPack32);
 81              Add(Format.R10G10B10A2Uint,     VkFormat.A2B10G10R10UintPack32);
 82              Add(Format.R11G11B10Float,      VkFormat.B10G11R11UfloatPack32);
 83              Add(Format.R9G9B9E5Float,       VkFormat.E5B9G9R9UfloatPack32);
 84              Add(Format.Bc1RgbaUnorm,        VkFormat.BC1RgbaUnormBlock);
 85              Add(Format.Bc2Unorm,            VkFormat.BC2UnormBlock);
 86              Add(Format.Bc3Unorm,            VkFormat.BC3UnormBlock);
 87              Add(Format.Bc1RgbaSrgb,         VkFormat.BC1RgbaSrgbBlock);
 88              Add(Format.Bc2Srgb,             VkFormat.BC2SrgbBlock);
 89              Add(Format.Bc3Srgb,             VkFormat.BC3SrgbBlock);
 90              Add(Format.Bc4Unorm,            VkFormat.BC4UnormBlock);
 91              Add(Format.Bc4Snorm,            VkFormat.BC4SNormBlock);
 92              Add(Format.Bc5Unorm,            VkFormat.BC5UnormBlock);
 93              Add(Format.Bc5Snorm,            VkFormat.BC5SNormBlock);
 94              Add(Format.Bc7Unorm,            VkFormat.BC7UnormBlock);
 95              Add(Format.Bc7Srgb,             VkFormat.BC7SrgbBlock);
 96              Add(Format.Bc6HSfloat,          VkFormat.BC6HSfloatBlock);
 97              Add(Format.Bc6HUfloat,          VkFormat.BC6HUfloatBlock);
 98              Add(Format.Etc2RgbUnorm,        VkFormat.Etc2R8G8B8UnormBlock);
 99              Add(Format.Etc2RgbaUnorm,       VkFormat.Etc2R8G8B8A8UnormBlock);
100              Add(Format.Etc2RgbPtaUnorm,     VkFormat.Etc2R8G8B8A1UnormBlock);
101              Add(Format.Etc2RgbSrgb,         VkFormat.Etc2R8G8B8SrgbBlock);
102              Add(Format.Etc2RgbaSrgb,        VkFormat.Etc2R8G8B8A8SrgbBlock);
103              Add(Format.Etc2RgbPtaSrgb,      VkFormat.Etc2R8G8B8A1SrgbBlock);
104              Add(Format.R8Uscaled,           VkFormat.R8Uscaled);
105              Add(Format.R8Sscaled,           VkFormat.R8Sscaled);
106              Add(Format.R16Uscaled,          VkFormat.R16Uscaled);
107              Add(Format.R16Sscaled,          VkFormat.R16Sscaled);
108              // Add(Format.R32Uscaled,          VkFormat.R32Uscaled);
109              // Add(Format.R32Sscaled,          VkFormat.R32Sscaled);
110              Add(Format.R8G8Uscaled,         VkFormat.R8G8Uscaled);
111              Add(Format.R8G8Sscaled,         VkFormat.R8G8Sscaled);
112              Add(Format.R16G16Uscaled,       VkFormat.R16G16Uscaled);
113              Add(Format.R16G16Sscaled,       VkFormat.R16G16Sscaled);
114              // Add(Format.R32G32Uscaled,       VkFormat.R32G32Uscaled);
115              // Add(Format.R32G32Sscaled,       VkFormat.R32G32Sscaled);
116              Add(Format.R8G8B8Uscaled,       VkFormat.R8G8B8Uscaled);
117              Add(Format.R8G8B8Sscaled,       VkFormat.R8G8B8Sscaled);
118              Add(Format.R16G16B16Uscaled,    VkFormat.R16G16B16Uscaled);
119              Add(Format.R16G16B16Sscaled,    VkFormat.R16G16B16Sscaled);
120              // Add(Format.R32G32B32Uscaled,    VkFormat.R32G32B32Uscaled);
121              // Add(Format.R32G32B32Sscaled,    VkFormat.R32G32B32Sscaled);
122              Add(Format.R8G8B8A8Uscaled,     VkFormat.R8G8B8A8Uscaled);
123              Add(Format.R8G8B8A8Sscaled,     VkFormat.R8G8B8A8Sscaled);
124              Add(Format.R16G16B16A16Uscaled, VkFormat.R16G16B16A16Uscaled);
125              Add(Format.R16G16B16A16Sscaled, VkFormat.R16G16B16A16Sscaled);
126              // Add(Format.R32G32B32A32Uscaled, VkFormat.R32G32B32A32Uscaled);
127              // Add(Format.R32G32B32A32Sscaled, VkFormat.R32G32B32A32Sscaled);
128              Add(Format.R10G10B10A2Snorm,    VkFormat.A2B10G10R10SNormPack32);
129              Add(Format.R10G10B10A2Sint,     VkFormat.A2B10G10R10SintPack32);
130              Add(Format.R10G10B10A2Uscaled,  VkFormat.A2B10G10R10UscaledPack32);
131              Add(Format.R10G10B10A2Sscaled,  VkFormat.A2B10G10R10SscaledPack32);
132              Add(Format.Astc4x4Unorm,        VkFormat.Astc4x4UnormBlock);
133              Add(Format.Astc5x4Unorm,        VkFormat.Astc5x4UnormBlock);
134              Add(Format.Astc5x5Unorm,        VkFormat.Astc5x5UnormBlock);
135              Add(Format.Astc6x5Unorm,        VkFormat.Astc6x5UnormBlock);
136              Add(Format.Astc6x6Unorm,        VkFormat.Astc6x6UnormBlock);
137              Add(Format.Astc8x5Unorm,        VkFormat.Astc8x5UnormBlock);
138              Add(Format.Astc8x6Unorm,        VkFormat.Astc8x6UnormBlock);
139              Add(Format.Astc8x8Unorm,        VkFormat.Astc8x8UnormBlock);
140              Add(Format.Astc10x5Unorm,       VkFormat.Astc10x5UnormBlock);
141              Add(Format.Astc10x6Unorm,       VkFormat.Astc10x6UnormBlock);
142              Add(Format.Astc10x8Unorm,       VkFormat.Astc10x8UnormBlock);
143              Add(Format.Astc10x10Unorm,      VkFormat.Astc10x10UnormBlock);
144              Add(Format.Astc12x10Unorm,      VkFormat.Astc12x10UnormBlock);
145              Add(Format.Astc12x12Unorm,      VkFormat.Astc12x12UnormBlock);
146              Add(Format.Astc4x4Srgb,         VkFormat.Astc4x4SrgbBlock);
147              Add(Format.Astc5x4Srgb,         VkFormat.Astc5x4SrgbBlock);
148              Add(Format.Astc5x5Srgb,         VkFormat.Astc5x5SrgbBlock);
149              Add(Format.Astc6x5Srgb,         VkFormat.Astc6x5SrgbBlock);
150              Add(Format.Astc6x6Srgb,         VkFormat.Astc6x6SrgbBlock);
151              Add(Format.Astc8x5Srgb,         VkFormat.Astc8x5SrgbBlock);
152              Add(Format.Astc8x6Srgb,         VkFormat.Astc8x6SrgbBlock);
153              Add(Format.Astc8x8Srgb,         VkFormat.Astc8x8SrgbBlock);
154              Add(Format.Astc10x5Srgb,        VkFormat.Astc10x5SrgbBlock);
155              Add(Format.Astc10x6Srgb,        VkFormat.Astc10x6SrgbBlock);
156              Add(Format.Astc10x8Srgb,        VkFormat.Astc10x8SrgbBlock);
157              Add(Format.Astc10x10Srgb,       VkFormat.Astc10x10SrgbBlock);
158              Add(Format.Astc12x10Srgb,       VkFormat.Astc12x10SrgbBlock);
159              Add(Format.Astc12x12Srgb,       VkFormat.Astc12x12SrgbBlock);
160              Add(Format.B5G6R5Unorm,         VkFormat.R5G6B5UnormPack16);
161              Add(Format.B5G5R5A1Unorm,       VkFormat.A1R5G5B5UnormPack16);
162              Add(Format.A1B5G5R5Unorm,       VkFormat.R5G5B5A1UnormPack16);
163              Add(Format.B8G8R8A8Unorm,       VkFormat.B8G8R8A8Unorm);
164              Add(Format.B8G8R8A8Srgb,        VkFormat.B8G8R8A8Srgb);
165              Add(Format.B10G10R10A2Unorm,    VkFormat.A2R10G10B10UnormPack32);
166  #pragma warning restore IDE0055
167          }
168  
169          private static void Add(Format format, VkFormat vkFormat)
170          {
171              _table[(int)format] = vkFormat;
172              _reverseMap[vkFormat] = format;
173          }
174  
175          public static VkFormat GetFormat(Format format)
176          {
177              return _table[(int)format];
178          }
179  
180          public static Format GetFormat(VkFormat format)
181          {
182              if (!_reverseMap.TryGetValue(format, out Format result))
183              {
184                  return Format.B8G8R8A8Unorm;
185              }
186  
187              return result;
188          }
189  
190          public static Format ConvertRgba8SrgbToUnorm(Format format)
191          {
192              return format switch
193              {
194                  Format.R8G8B8A8Srgb => Format.R8G8B8A8Unorm,
195                  Format.B8G8R8A8Srgb => Format.B8G8R8A8Unorm,
196                  _ => format,
197              };
198          }
199  
200          public static int GetAttributeFormatSize(VkFormat format)
201          {
202              switch (format)
203              {
204                  case VkFormat.R8Unorm:
205                  case VkFormat.R8SNorm:
206                  case VkFormat.R8Uint:
207                  case VkFormat.R8Sint:
208                  case VkFormat.R8Uscaled:
209                  case VkFormat.R8Sscaled:
210                      return 1;
211  
212                  case VkFormat.R8G8Unorm:
213                  case VkFormat.R8G8SNorm:
214                  case VkFormat.R8G8Uint:
215                  case VkFormat.R8G8Sint:
216                  case VkFormat.R8G8Uscaled:
217                  case VkFormat.R8G8Sscaled:
218                  case VkFormat.R16Sfloat:
219                  case VkFormat.R16Unorm:
220                  case VkFormat.R16SNorm:
221                  case VkFormat.R16Uint:
222                  case VkFormat.R16Sint:
223                  case VkFormat.R16Uscaled:
224                  case VkFormat.R16Sscaled:
225                      return 2;
226  
227                  case VkFormat.R8G8B8Unorm:
228                  case VkFormat.R8G8B8SNorm:
229                  case VkFormat.R8G8B8Uint:
230                  case VkFormat.R8G8B8Sint:
231                  case VkFormat.R8G8B8Uscaled:
232                  case VkFormat.R8G8B8Sscaled:
233                      return 3;
234  
235                  case VkFormat.R8G8B8A8Unorm:
236                  case VkFormat.R8G8B8A8SNorm:
237                  case VkFormat.R8G8B8A8Uint:
238                  case VkFormat.R8G8B8A8Sint:
239                  case VkFormat.R8G8B8A8Srgb:
240                  case VkFormat.R8G8B8A8Uscaled:
241                  case VkFormat.R8G8B8A8Sscaled:
242                  case VkFormat.B8G8R8A8Unorm:
243                  case VkFormat.B8G8R8A8Srgb:
244                  case VkFormat.R16G16Sfloat:
245                  case VkFormat.R16G16Unorm:
246                  case VkFormat.R16G16SNorm:
247                  case VkFormat.R16G16Uint:
248                  case VkFormat.R16G16Sint:
249                  case VkFormat.R16G16Uscaled:
250                  case VkFormat.R16G16Sscaled:
251                  case VkFormat.R32Sfloat:
252                  case VkFormat.R32Uint:
253                  case VkFormat.R32Sint:
254                  case VkFormat.A2B10G10R10UnormPack32:
255                  case VkFormat.A2B10G10R10UintPack32:
256                  case VkFormat.B10G11R11UfloatPack32:
257                  case VkFormat.E5B9G9R9UfloatPack32:
258                  case VkFormat.A2B10G10R10SNormPack32:
259                  case VkFormat.A2B10G10R10SintPack32:
260                  case VkFormat.A2B10G10R10UscaledPack32:
261                  case VkFormat.A2B10G10R10SscaledPack32:
262                      return 4;
263  
264                  case VkFormat.R16G16B16Sfloat:
265                  case VkFormat.R16G16B16Unorm:
266                  case VkFormat.R16G16B16SNorm:
267                  case VkFormat.R16G16B16Uint:
268                  case VkFormat.R16G16B16Sint:
269                  case VkFormat.R16G16B16Uscaled:
270                  case VkFormat.R16G16B16Sscaled:
271                      return 6;
272  
273                  case VkFormat.R16G16B16A16Sfloat:
274                  case VkFormat.R16G16B16A16Unorm:
275                  case VkFormat.R16G16B16A16SNorm:
276                  case VkFormat.R16G16B16A16Uint:
277                  case VkFormat.R16G16B16A16Sint:
278                  case VkFormat.R16G16B16A16Uscaled:
279                  case VkFormat.R16G16B16A16Sscaled:
280                  case VkFormat.R32G32Sfloat:
281                  case VkFormat.R32G32Uint:
282                  case VkFormat.R32G32Sint:
283                      return 8;
284  
285                  case VkFormat.R32G32B32Sfloat:
286                  case VkFormat.R32G32B32Uint:
287                  case VkFormat.R32G32B32Sint:
288                      return 12;
289  
290                  case VkFormat.R32G32B32A32Sfloat:
291                  case VkFormat.R32G32B32A32Uint:
292                  case VkFormat.R32G32B32A32Sint:
293                      return 16;
294              }
295  
296              return 1;
297          }
298  
299          public static VkFormat DropLastComponent(VkFormat format)
300          {
301              return format switch
302              {
303                  VkFormat.R8G8Unorm => VkFormat.R8Unorm,
304                  VkFormat.R8G8SNorm => VkFormat.R8SNorm,
305                  VkFormat.R8G8Uint => VkFormat.R8Uint,
306                  VkFormat.R8G8Sint => VkFormat.R8Sint,
307                  VkFormat.R8G8Uscaled => VkFormat.R8Uscaled,
308                  VkFormat.R8G8Sscaled => VkFormat.R8Sscaled,
309                  VkFormat.R8G8B8Unorm => VkFormat.R8G8Unorm,
310                  VkFormat.R8G8B8SNorm => VkFormat.R8G8SNorm,
311                  VkFormat.R8G8B8Uint => VkFormat.R8G8Uint,
312                  VkFormat.R8G8B8Sint => VkFormat.R8G8Sint,
313                  VkFormat.R8G8B8Uscaled => VkFormat.R8G8Uscaled,
314                  VkFormat.R8G8B8Sscaled => VkFormat.R8G8Sscaled,
315                  VkFormat.R8G8B8A8Unorm => VkFormat.R8G8B8Unorm,
316                  VkFormat.R8G8B8A8SNorm => VkFormat.R8G8B8SNorm,
317                  VkFormat.R8G8B8A8Uint => VkFormat.R8G8B8Uint,
318                  VkFormat.R8G8B8A8Sint => VkFormat.R8G8B8Sint,
319                  VkFormat.R8G8B8A8Srgb => VkFormat.R8G8B8Srgb,
320                  VkFormat.R8G8B8A8Uscaled => VkFormat.R8G8B8Uscaled,
321                  VkFormat.R8G8B8A8Sscaled => VkFormat.R8G8B8Sscaled,
322                  VkFormat.B8G8R8A8Unorm => VkFormat.B8G8R8Unorm,
323                  VkFormat.B8G8R8A8Srgb => VkFormat.B8G8R8Srgb,
324                  VkFormat.R16G16Sfloat => VkFormat.R16Sfloat,
325                  VkFormat.R16G16Unorm => VkFormat.R16Unorm,
326                  VkFormat.R16G16SNorm => VkFormat.R16SNorm,
327                  VkFormat.R16G16Uint => VkFormat.R16Uint,
328                  VkFormat.R16G16Sint => VkFormat.R16Sint,
329                  VkFormat.R16G16Uscaled => VkFormat.R16Uscaled,
330                  VkFormat.R16G16Sscaled => VkFormat.R16Sscaled,
331                  VkFormat.R16G16B16Sfloat => VkFormat.R16G16Sfloat,
332                  VkFormat.R16G16B16Unorm => VkFormat.R16G16Unorm,
333                  VkFormat.R16G16B16SNorm => VkFormat.R16G16SNorm,
334                  VkFormat.R16G16B16Uint => VkFormat.R16G16Uint,
335                  VkFormat.R16G16B16Sint => VkFormat.R16G16Sint,
336                  VkFormat.R16G16B16Uscaled => VkFormat.R16G16Uscaled,
337                  VkFormat.R16G16B16Sscaled => VkFormat.R16G16Sscaled,
338                  VkFormat.R16G16B16A16Sfloat => VkFormat.R16G16B16Sfloat,
339                  VkFormat.R16G16B16A16Unorm => VkFormat.R16G16B16Unorm,
340                  VkFormat.R16G16B16A16SNorm => VkFormat.R16G16B16SNorm,
341                  VkFormat.R16G16B16A16Uint => VkFormat.R16G16B16Uint,
342                  VkFormat.R16G16B16A16Sint => VkFormat.R16G16B16Sint,
343                  VkFormat.R16G16B16A16Uscaled => VkFormat.R16G16B16Uscaled,
344                  VkFormat.R16G16B16A16Sscaled => VkFormat.R16G16B16Sscaled,
345                  VkFormat.R32G32Sfloat => VkFormat.R32Sfloat,
346                  VkFormat.R32G32Uint => VkFormat.R32Uint,
347                  VkFormat.R32G32Sint => VkFormat.R32Sint,
348                  VkFormat.R32G32B32Sfloat => VkFormat.R32G32Sfloat,
349                  VkFormat.R32G32B32Uint => VkFormat.R32G32Uint,
350                  VkFormat.R32G32B32Sint => VkFormat.R32G32Sint,
351                  VkFormat.R32G32B32A32Sfloat => VkFormat.R32G32B32Sfloat,
352                  VkFormat.R32G32B32A32Uint => VkFormat.R32G32B32Uint,
353                  VkFormat.R32G32B32A32Sint => VkFormat.R32G32B32Sint,
354                  _ => format,
355              };
356          }
357      }
358  }