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