BlockLinearSwizzle.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using Ryujinx.Common;
  2. using System;
  3. namespace Ryujinx.Graphics.Texture
  4. {
  5. class BlockLinearSwizzle : ISwizzle
  6. {
  7. private const int GobWidth = 64;
  8. private const int GobHeight = 8;
  9. private const int GobSize = GobWidth * GobHeight;
  10. private int TexWidth;
  11. private int TexHeight;
  12. private int TexDepth;
  13. private int TexGobBlockHeight;
  14. private int TexGobBlockDepth;
  15. private int TexBpp;
  16. private int BhMask;
  17. private int BdMask;
  18. private int BhShift;
  19. private int BdShift;
  20. private int BppShift;
  21. private int XShift;
  22. private int RobSize;
  23. private int SliceSize;
  24. private int BaseOffset;
  25. public BlockLinearSwizzle(
  26. int Width,
  27. int Height,
  28. int Depth,
  29. int GobBlockHeight,
  30. int GobBlockDepth,
  31. int Bpp)
  32. {
  33. TexWidth = Width;
  34. TexHeight = Height;
  35. TexDepth = Depth;
  36. TexGobBlockHeight = GobBlockHeight;
  37. TexGobBlockDepth = GobBlockDepth;
  38. TexBpp = Bpp;
  39. BppShift = BitUtils.CountTrailingZeros32(Bpp);
  40. SetMipLevel(0);
  41. }
  42. public void SetMipLevel(int Level)
  43. {
  44. BaseOffset = GetMipOffset(Level);
  45. int Width = Math.Max(1, TexWidth >> Level);
  46. int Height = Math.Max(1, TexHeight >> Level);
  47. int Depth = Math.Max(1, TexDepth >> Level);
  48. GobBlockSizes GbSizes = AdjustGobBlockSizes(Height, Depth);
  49. BhMask = GbSizes.Height - 1;
  50. BdMask = GbSizes.Depth - 1;
  51. BhShift = BitUtils.CountTrailingZeros32(GbSizes.Height);
  52. BdShift = BitUtils.CountTrailingZeros32(GbSizes.Depth);
  53. XShift = BitUtils.CountTrailingZeros32(GobSize * GbSizes.Height * GbSizes.Depth);
  54. RobAndSliceSizes GsSizes = GetRobAndSliceSizes(Width, Height, GbSizes);
  55. RobSize = GsSizes.RobSize;
  56. SliceSize = GsSizes.SliceSize;
  57. }
  58. public int GetImageSize(int MipsCount)
  59. {
  60. int Size = GetMipOffset(MipsCount);
  61. Size = (Size + 0x1fff) & ~0x1fff;
  62. return Size;
  63. }
  64. public int GetMipOffset(int Level)
  65. {
  66. int TotalSize = 0;
  67. for (int Index = 0; Index < Level; Index++)
  68. {
  69. int Width = Math.Max(1, TexWidth >> Index);
  70. int Height = Math.Max(1, TexHeight >> Index);
  71. int Depth = Math.Max(1, TexDepth >> Index);
  72. GobBlockSizes GbSizes = AdjustGobBlockSizes(Height, Depth);
  73. RobAndSliceSizes RsSizes = GetRobAndSliceSizes(Width, Height, GbSizes);
  74. TotalSize += BitUtils.DivRoundUp(Depth, GbSizes.Depth) * RsSizes.SliceSize;
  75. }
  76. return TotalSize;
  77. }
  78. private struct GobBlockSizes
  79. {
  80. public int Height;
  81. public int Depth;
  82. public GobBlockSizes(int GobBlockHeight, int GobBlockDepth)
  83. {
  84. this.Height = GobBlockHeight;
  85. this.Depth = GobBlockDepth;
  86. }
  87. }
  88. private GobBlockSizes AdjustGobBlockSizes(int Height, int Depth)
  89. {
  90. int GobBlockHeight = TexGobBlockHeight;
  91. int GobBlockDepth = TexGobBlockDepth;
  92. int Pow2Height = BitUtils.Pow2RoundUp(Height);
  93. int Pow2Depth = BitUtils.Pow2RoundUp(Depth);
  94. while (GobBlockHeight * GobHeight > Pow2Height && GobBlockHeight > 1)
  95. {
  96. GobBlockHeight >>= 1;
  97. }
  98. while (GobBlockDepth > Pow2Depth && GobBlockDepth > 1)
  99. {
  100. GobBlockDepth >>= 1;
  101. }
  102. return new GobBlockSizes(GobBlockHeight, GobBlockDepth);
  103. }
  104. private struct RobAndSliceSizes
  105. {
  106. public int RobSize;
  107. public int SliceSize;
  108. public RobAndSliceSizes(int RobSize, int SliceSize)
  109. {
  110. this.RobSize = RobSize;
  111. this.SliceSize = SliceSize;
  112. }
  113. }
  114. private RobAndSliceSizes GetRobAndSliceSizes(int Width, int Height, GobBlockSizes GbSizes)
  115. {
  116. int WidthInGobs = BitUtils.DivRoundUp(Width * TexBpp, GobWidth);
  117. int RobSize = GobSize * GbSizes.Height * GbSizes.Depth * WidthInGobs;
  118. int SliceSize = BitUtils.DivRoundUp(Height, GbSizes.Height * GobHeight) * RobSize;
  119. return new RobAndSliceSizes(RobSize, SliceSize);
  120. }
  121. public int GetSwizzleOffset(int X, int Y, int Z)
  122. {
  123. X <<= BppShift;
  124. int YH = Y / GobHeight;
  125. int Position = (Z >> BdShift) * SliceSize + (YH >> BhShift) * RobSize;
  126. Position += (X / GobWidth) << XShift;
  127. Position += (YH & BhMask) * GobSize;
  128. Position += ((Z & BdMask) * GobSize) << BhShift;
  129. Position += ((X & 0x3f) >> 5) << 8;
  130. Position += ((Y & 0x07) >> 1) << 6;
  131. Position += ((X & 0x1f) >> 4) << 5;
  132. Position += ((Y & 0x01) >> 0) << 4;
  133. Position += ((X & 0x0f) >> 0) << 0;
  134. return BaseOffset + Position;
  135. }
  136. }
  137. }