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