ShaderTexture.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Image;
  4. using Ryujinx.Graphics.Shader;
  5. namespace Ryujinx.Graphics.Gpu.Engine
  6. {
  7. /// <summary>
  8. /// Shader texture properties conversion methods.
  9. /// </summary>
  10. static class ShaderTexture
  11. {
  12. /// <summary>
  13. /// Gets a texture target from a sampler type.
  14. /// </summary>
  15. /// <param name="type">Sampler type</param>
  16. /// <returns>Texture target value</returns>
  17. public static Target GetTarget(SamplerType type)
  18. {
  19. type &= ~SamplerType.Shadow;
  20. switch (type)
  21. {
  22. case SamplerType.Texture1D:
  23. return Target.Texture1D;
  24. case SamplerType.TextureBuffer:
  25. return Target.TextureBuffer;
  26. case SamplerType.Texture1D | SamplerType.Array:
  27. return Target.Texture1DArray;
  28. case SamplerType.Texture2D:
  29. return Target.Texture2D;
  30. case SamplerType.Texture2D | SamplerType.Array:
  31. return Target.Texture2DArray;
  32. case SamplerType.Texture2D | SamplerType.Multisample:
  33. return Target.Texture2DMultisample;
  34. case SamplerType.Texture2D | SamplerType.Multisample | SamplerType.Array:
  35. return Target.Texture2DMultisampleArray;
  36. case SamplerType.Texture3D:
  37. return Target.Texture3D;
  38. case SamplerType.TextureCube:
  39. return Target.Cubemap;
  40. case SamplerType.TextureCube | SamplerType.Array:
  41. return Target.CubemapArray;
  42. }
  43. Logger.Warning?.Print(LogClass.Gpu, $"Invalid sampler type \"{type}\".");
  44. return Target.Texture2D;
  45. }
  46. /// <summary>
  47. /// Gets a texture format from a shader image format.
  48. /// </summary>
  49. /// <param name="format">Shader image format</param>
  50. /// <returns>Texture format</returns>
  51. public static FormatInfo GetFormatInfo(TextureFormat format)
  52. {
  53. return format switch
  54. {
  55. #pragma warning disable IDE0055 // Disable formatting
  56. TextureFormat.R8Unorm => new(Format.R8Unorm, 1, 1, 1, 1),
  57. TextureFormat.R8Snorm => new(Format.R8Snorm, 1, 1, 1, 1),
  58. TextureFormat.R8Uint => new(Format.R8Uint, 1, 1, 1, 1),
  59. TextureFormat.R8Sint => new(Format.R8Sint, 1, 1, 1, 1),
  60. TextureFormat.R16Float => new(Format.R16Float, 1, 1, 2, 1),
  61. TextureFormat.R16Unorm => new(Format.R16Unorm, 1, 1, 2, 1),
  62. TextureFormat.R16Snorm => new(Format.R16Snorm, 1, 1, 2, 1),
  63. TextureFormat.R16Uint => new(Format.R16Uint, 1, 1, 2, 1),
  64. TextureFormat.R16Sint => new(Format.R16Sint, 1, 1, 2, 1),
  65. TextureFormat.R32Float => new(Format.R32Float, 1, 1, 4, 1),
  66. TextureFormat.R32Uint => new(Format.R32Uint, 1, 1, 4, 1),
  67. TextureFormat.R32Sint => new(Format.R32Sint, 1, 1, 4, 1),
  68. TextureFormat.R8G8Unorm => new(Format.R8G8Unorm, 1, 1, 2, 2),
  69. TextureFormat.R8G8Snorm => new(Format.R8G8Snorm, 1, 1, 2, 2),
  70. TextureFormat.R8G8Uint => new(Format.R8G8Uint, 1, 1, 2, 2),
  71. TextureFormat.R8G8Sint => new(Format.R8G8Sint, 1, 1, 2, 2),
  72. TextureFormat.R16G16Float => new(Format.R16G16Float, 1, 1, 4, 2),
  73. TextureFormat.R16G16Unorm => new(Format.R16G16Unorm, 1, 1, 4, 2),
  74. TextureFormat.R16G16Snorm => new(Format.R16G16Snorm, 1, 1, 4, 2),
  75. TextureFormat.R16G16Uint => new(Format.R16G16Uint, 1, 1, 4, 2),
  76. TextureFormat.R16G16Sint => new(Format.R16G16Sint, 1, 1, 4, 2),
  77. TextureFormat.R32G32Float => new(Format.R32G32Float, 1, 1, 8, 2),
  78. TextureFormat.R32G32Uint => new(Format.R32G32Uint, 1, 1, 8, 2),
  79. TextureFormat.R32G32Sint => new(Format.R32G32Sint, 1, 1, 8, 2),
  80. TextureFormat.R8G8B8A8Unorm => new(Format.R8G8B8A8Unorm, 1, 1, 4, 4),
  81. TextureFormat.R8G8B8A8Snorm => new(Format.R8G8B8A8Snorm, 1, 1, 4, 4),
  82. TextureFormat.R8G8B8A8Uint => new(Format.R8G8B8A8Uint, 1, 1, 4, 4),
  83. TextureFormat.R8G8B8A8Sint => new(Format.R8G8B8A8Sint, 1, 1, 4, 4),
  84. TextureFormat.R16G16B16A16Float => new(Format.R16G16B16A16Float, 1, 1, 8, 4),
  85. TextureFormat.R16G16B16A16Unorm => new(Format.R16G16B16A16Unorm, 1, 1, 8, 4),
  86. TextureFormat.R16G16B16A16Snorm => new(Format.R16G16B16A16Snorm, 1, 1, 8, 4),
  87. TextureFormat.R16G16B16A16Uint => new(Format.R16G16B16A16Uint, 1, 1, 8, 4),
  88. TextureFormat.R16G16B16A16Sint => new(Format.R16G16B16A16Sint, 1, 1, 8, 4),
  89. TextureFormat.R32G32B32A32Float => new(Format.R32G32B32A32Float, 1, 1, 16, 4),
  90. TextureFormat.R32G32B32A32Uint => new(Format.R32G32B32A32Uint, 1, 1, 16, 4),
  91. TextureFormat.R32G32B32A32Sint => new(Format.R32G32B32A32Sint, 1, 1, 16, 4),
  92. TextureFormat.R10G10B10A2Unorm => new(Format.R10G10B10A2Unorm, 1, 1, 4, 4),
  93. TextureFormat.R10G10B10A2Uint => new(Format.R10G10B10A2Uint, 1, 1, 4, 4),
  94. TextureFormat.R11G11B10Float => new(Format.R11G11B10Float, 1, 1, 4, 3),
  95. _ => FormatInfo.Invalid,
  96. #pragma warning restore IDE0055
  97. };
  98. }
  99. }
  100. }