/ src / Ryujinx.Graphics.GAL / TextureCreateInfo.cs
TextureCreateInfo.cs
  1  using Ryujinx.Common;
  2  using System;
  3  
  4  namespace Ryujinx.Graphics.GAL
  5  {
  6      public readonly struct TextureCreateInfo : IEquatable<TextureCreateInfo>
  7      {
  8          public int Width { get; }
  9          public int Height { get; }
 10          public int Depth { get; }
 11          public int Levels { get; }
 12          public int Samples { get; }
 13          public int BlockWidth { get; }
 14          public int BlockHeight { get; }
 15          public int BytesPerPixel { get; }
 16  
 17          public bool IsCompressed => (BlockWidth | BlockHeight) != 1;
 18  
 19          public Format Format { get; }
 20  
 21          public DepthStencilMode DepthStencilMode { get; }
 22  
 23          public Target Target { get; }
 24  
 25          public SwizzleComponent SwizzleR { get; }
 26          public SwizzleComponent SwizzleG { get; }
 27          public SwizzleComponent SwizzleB { get; }
 28          public SwizzleComponent SwizzleA { get; }
 29  
 30          public TextureCreateInfo(
 31              int width,
 32              int height,
 33              int depth,
 34              int levels,
 35              int samples,
 36              int blockWidth,
 37              int blockHeight,
 38              int bytesPerPixel,
 39              Format format,
 40              DepthStencilMode depthStencilMode,
 41              Target target,
 42              SwizzleComponent swizzleR,
 43              SwizzleComponent swizzleG,
 44              SwizzleComponent swizzleB,
 45              SwizzleComponent swizzleA)
 46          {
 47              Width = width;
 48              Height = height;
 49              Depth = depth;
 50              Levels = levels;
 51              Samples = samples;
 52              BlockWidth = blockWidth;
 53              BlockHeight = blockHeight;
 54              BytesPerPixel = bytesPerPixel;
 55              Format = format;
 56              DepthStencilMode = depthStencilMode;
 57              Target = target;
 58              SwizzleR = swizzleR;
 59              SwizzleG = swizzleG;
 60              SwizzleB = swizzleB;
 61              SwizzleA = swizzleA;
 62          }
 63  
 64          public int GetMipSize(int level)
 65          {
 66              return GetMipStride(level) * GetLevelHeight(level) * GetLevelDepth(level);
 67          }
 68  
 69          public int GetMipSize2D(int level)
 70          {
 71              return GetMipStride(level) * GetLevelHeight(level);
 72          }
 73  
 74          public int GetMipStride(int level)
 75          {
 76              return BitUtils.AlignUp(GetLevelWidth(level) * BytesPerPixel, 4);
 77          }
 78  
 79          private int GetLevelWidth(int level)
 80          {
 81              return BitUtils.DivRoundUp(GetLevelSize(Width, level), BlockWidth);
 82          }
 83  
 84          private int GetLevelHeight(int level)
 85          {
 86              return BitUtils.DivRoundUp(GetLevelSize(Height, level), BlockHeight);
 87          }
 88  
 89          private int GetLevelDepth(int level)
 90          {
 91              return Target == Target.Texture3D ? GetLevelSize(Depth, level) : GetLayers();
 92          }
 93  
 94          public int GetDepthOrLayers()
 95          {
 96              return Target == Target.Texture3D ? Depth : GetLayers();
 97          }
 98  
 99          public int GetLayers()
100          {
101              if (Target == Target.Texture2DArray ||
102                  Target == Target.Texture2DMultisampleArray ||
103                  Target == Target.CubemapArray)
104              {
105                  return Depth;
106              }
107              else if (Target == Target.Cubemap)
108              {
109                  return 6;
110              }
111  
112              return 1;
113          }
114  
115          private static int GetLevelSize(int size, int level)
116          {
117              return Math.Max(1, size >> level);
118          }
119  
120          public override int GetHashCode()
121          {
122              return HashCode.Combine(Width, Height);
123          }
124  
125          public bool Equals(TextureCreateInfo other)
126          {
127              return Width == other.Width &&
128                     Height == other.Height &&
129                     Depth == other.Depth &&
130                     Levels == other.Levels &&
131                     Samples == other.Samples &&
132                     BlockWidth == other.BlockWidth &&
133                     BlockHeight == other.BlockHeight &&
134                     BytesPerPixel == other.BytesPerPixel &&
135                     Format == other.Format &&
136                     DepthStencilMode == other.DepthStencilMode &&
137                     Target == other.Target &&
138                     SwizzleR == other.SwizzleR &&
139                     SwizzleG == other.SwizzleG &&
140                     SwizzleB == other.SwizzleB &&
141                     SwizzleA == other.SwizzleA;
142          }
143  
144          public override bool Equals(object obj)
145          {
146              return obj is TextureCreateInfo info && this.Equals(info);
147          }
148  
149          public static bool operator ==(TextureCreateInfo left, TextureCreateInfo right)
150          {
151              return left.Equals(right);
152          }
153  
154          public static bool operator !=(TextureCreateInfo left, TextureCreateInfo right)
155          {
156              return !(left == right);
157          }
158      }
159  }