SamplerType.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using Ryujinx.Graphics.Shader.Translation;
  2. using System;
  3. namespace Ryujinx.Graphics.Shader
  4. {
  5. [Flags]
  6. public enum SamplerType
  7. {
  8. None = 0,
  9. Texture1D,
  10. TextureBuffer,
  11. Texture2D,
  12. Texture3D,
  13. TextureCube,
  14. Mask = 0xff,
  15. Array = 1 << 8,
  16. Multisample = 1 << 9,
  17. Shadow = 1 << 10,
  18. }
  19. static class SamplerTypeExtensions
  20. {
  21. public static int GetDimensions(this SamplerType type)
  22. {
  23. return (type & SamplerType.Mask) switch
  24. {
  25. SamplerType.Texture1D => 1,
  26. SamplerType.TextureBuffer => 1,
  27. SamplerType.Texture2D => 2,
  28. SamplerType.Texture3D => 3,
  29. SamplerType.TextureCube => 3,
  30. _ => throw new ArgumentException($"Invalid sampler type \"{type}\"."),
  31. };
  32. }
  33. public static string ToShortSamplerType(this SamplerType type)
  34. {
  35. string typeName = (type & SamplerType.Mask) switch
  36. {
  37. SamplerType.Texture1D => "1d",
  38. SamplerType.TextureBuffer => "b",
  39. SamplerType.Texture2D => "2d",
  40. SamplerType.Texture3D => "3d",
  41. SamplerType.TextureCube => "cube",
  42. _ => throw new ArgumentException($"Invalid sampler type \"{type}\"."),
  43. };
  44. if ((type & SamplerType.Multisample) != 0)
  45. {
  46. typeName += "ms";
  47. }
  48. if ((type & SamplerType.Array) != 0)
  49. {
  50. typeName += "a";
  51. }
  52. if ((type & SamplerType.Shadow) != 0)
  53. {
  54. typeName += "s";
  55. }
  56. return typeName;
  57. }
  58. public static string ToGlslSamplerType(this SamplerType type)
  59. {
  60. string typeName = (type & SamplerType.Mask) switch
  61. {
  62. SamplerType.None => "sampler",
  63. SamplerType.Texture1D => "sampler1D",
  64. SamplerType.TextureBuffer => "samplerBuffer",
  65. SamplerType.Texture2D => "sampler2D",
  66. SamplerType.Texture3D => "sampler3D",
  67. SamplerType.TextureCube => "samplerCube",
  68. _ => throw new ArgumentException($"Invalid sampler type \"{type}\"."),
  69. };
  70. if ((type & SamplerType.Multisample) != 0)
  71. {
  72. typeName += "MS";
  73. }
  74. if ((type & SamplerType.Array) != 0)
  75. {
  76. typeName += "Array";
  77. }
  78. if ((type & SamplerType.Shadow) != 0)
  79. {
  80. typeName += "Shadow";
  81. }
  82. return typeName;
  83. }
  84. public static string ToGlslTextureType(this SamplerType type)
  85. {
  86. string typeName = (type & SamplerType.Mask) switch
  87. {
  88. SamplerType.Texture1D => "texture1D",
  89. SamplerType.TextureBuffer => "textureBuffer",
  90. SamplerType.Texture2D => "texture2D",
  91. SamplerType.Texture3D => "texture3D",
  92. SamplerType.TextureCube => "textureCube",
  93. _ => throw new ArgumentException($"Invalid texture type \"{type}\"."),
  94. };
  95. if ((type & SamplerType.Multisample) != 0)
  96. {
  97. typeName += "MS";
  98. }
  99. if ((type & SamplerType.Array) != 0)
  100. {
  101. typeName += "Array";
  102. }
  103. return typeName;
  104. }
  105. public static string ToGlslImageType(this SamplerType type, AggregateType componentType)
  106. {
  107. string typeName = (type & SamplerType.Mask) switch
  108. {
  109. SamplerType.Texture1D => "image1D",
  110. SamplerType.TextureBuffer => "imageBuffer",
  111. SamplerType.Texture2D => "image2D",
  112. SamplerType.Texture3D => "image3D",
  113. SamplerType.TextureCube => "imageCube",
  114. _ => throw new ArgumentException($"Invalid sampler type \"{type}\"."),
  115. };
  116. if ((type & SamplerType.Multisample) != 0)
  117. {
  118. typeName += "MS";
  119. }
  120. if ((type & SamplerType.Array) != 0)
  121. {
  122. typeName += "Array";
  123. }
  124. switch (componentType)
  125. {
  126. case AggregateType.U32:
  127. typeName = 'u' + typeName;
  128. break;
  129. case AggregateType.S32:
  130. typeName = 'i' + typeName;
  131. break;
  132. }
  133. return typeName;
  134. }
  135. }
  136. }