LayoutConverter.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using Ryujinx.Common;
  2. using System;
  3. using static Ryujinx.Graphics.Texture.BlockLinearConstants;
  4. namespace Ryujinx.Graphics.Texture
  5. {
  6. public static class LayoutConverter
  7. {
  8. private const int HostStrideAlignment = 4;
  9. public static Span<byte> ConvertBlockLinearToLinear(
  10. int width,
  11. int height,
  12. int depth,
  13. int levels,
  14. int layers,
  15. int blockWidth,
  16. int blockHeight,
  17. int bytesPerPixel,
  18. int gobBlocksInY,
  19. int gobBlocksInZ,
  20. int gobBlocksInTileX,
  21. SizeInfo sizeInfo,
  22. ReadOnlySpan<byte> data)
  23. {
  24. int outSize = GetTextureSize(
  25. width,
  26. height,
  27. depth,
  28. levels,
  29. layers,
  30. blockWidth,
  31. blockHeight,
  32. bytesPerPixel);
  33. Span<byte> output = new byte[outSize];
  34. int outOffs = 0;
  35. int wAlignment = gobBlocksInTileX * (GobStride / bytesPerPixel);
  36. int mipGobBlocksInY = gobBlocksInY;
  37. int mipGobBlocksInZ = gobBlocksInZ;
  38. for (int level = 0; level < levels; level++)
  39. {
  40. int w = Math.Max(1, width >> level);
  41. int h = Math.Max(1, height >> level);
  42. int d = Math.Max(1, depth >> level);
  43. w = BitUtils.DivRoundUp(w, blockWidth);
  44. h = BitUtils.DivRoundUp(h, blockHeight);
  45. while (h <= (mipGobBlocksInY >> 1) * GobHeight && mipGobBlocksInY != 1)
  46. {
  47. mipGobBlocksInY >>= 1;
  48. }
  49. while (d <= (mipGobBlocksInZ >> 1) && mipGobBlocksInZ != 1)
  50. {
  51. mipGobBlocksInZ >>= 1;
  52. }
  53. int strideTrunc = BitUtils.AlignDown(w * bytesPerPixel, 16);
  54. int xStart = strideTrunc / bytesPerPixel;
  55. int stride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  56. int wAligned = BitUtils.AlignUp(w, wAlignment);
  57. BlockLinearLayout layoutConverter = new BlockLinearLayout(
  58. wAligned,
  59. h,
  60. d,
  61. mipGobBlocksInY,
  62. mipGobBlocksInZ,
  63. bytesPerPixel);
  64. for (int layer = 0; layer < layers; layer++)
  65. {
  66. int inBaseOffset = layer * sizeInfo.LayerSize + sizeInfo.GetMipOffset(level);
  67. for (int z = 0; z < d; z++)
  68. for (int y = 0; y < h; y++)
  69. {
  70. for (int x = 0; x < strideTrunc; x += 16)
  71. {
  72. int offset = inBaseOffset + layoutConverter.GetOffsetWithLineOffset(x, y, z);
  73. Span<byte> dest = output.Slice(outOffs + x, 16);
  74. data.Slice(offset, 16).CopyTo(dest);
  75. }
  76. for (int x = xStart; x < w; x++)
  77. {
  78. int offset = inBaseOffset + layoutConverter.GetOffset(x, y, z);
  79. Span<byte> dest = output.Slice(outOffs + x * bytesPerPixel, bytesPerPixel);
  80. data.Slice(offset, bytesPerPixel).CopyTo(dest);
  81. }
  82. outOffs += stride;
  83. }
  84. }
  85. }
  86. return output;
  87. }
  88. public static Span<byte> ConvertLinearStridedToLinear(
  89. int width,
  90. int height,
  91. int blockWidth,
  92. int blockHeight,
  93. int stride,
  94. int bytesPerPixel,
  95. ReadOnlySpan<byte> data)
  96. {
  97. int w = BitUtils.DivRoundUp(width, blockWidth);
  98. int h = BitUtils.DivRoundUp(height, blockHeight);
  99. int outStride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  100. Span<byte> output = new byte[h * outStride];
  101. int outOffs = 0;
  102. for (int y = 0; y < h; y++)
  103. {
  104. for (int x = 0; x < w; x++)
  105. {
  106. int offset = y * stride + x * bytesPerPixel;
  107. Span<byte> dest = output.Slice(outOffs + x * bytesPerPixel, bytesPerPixel);
  108. data.Slice(offset, bytesPerPixel).CopyTo(dest);
  109. }
  110. outOffs += outStride;
  111. }
  112. return output;
  113. }
  114. public static Span<byte> ConvertLinearToBlockLinear(
  115. int width,
  116. int height,
  117. int depth,
  118. int levels,
  119. int layers,
  120. int blockWidth,
  121. int blockHeight,
  122. int bytesPerPixel,
  123. int gobBlocksInY,
  124. int gobBlocksInZ,
  125. int gobBlocksInTileX,
  126. SizeInfo sizeInfo,
  127. ReadOnlySpan<byte> data)
  128. {
  129. Span<byte> output = new byte[sizeInfo.TotalSize];
  130. int inOffs = 0;
  131. int wAlignment = gobBlocksInTileX * (GobStride / bytesPerPixel);
  132. int mipGobBlocksInY = gobBlocksInY;
  133. int mipGobBlocksInZ = gobBlocksInZ;
  134. for (int level = 0; level < levels; level++)
  135. {
  136. int w = Math.Max(1, width >> level);
  137. int h = Math.Max(1, height >> level);
  138. int d = Math.Max(1, depth >> level);
  139. w = BitUtils.DivRoundUp(w, blockWidth);
  140. h = BitUtils.DivRoundUp(h, blockHeight);
  141. while (h <= (mipGobBlocksInY >> 1) * GobHeight && mipGobBlocksInY != 1)
  142. {
  143. mipGobBlocksInY >>= 1;
  144. }
  145. while (d <= (mipGobBlocksInZ >> 1) && mipGobBlocksInZ != 1)
  146. {
  147. mipGobBlocksInZ >>= 1;
  148. }
  149. int stride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  150. int wAligned = BitUtils.AlignUp(w, wAlignment);
  151. BlockLinearLayout layoutConverter = new BlockLinearLayout(
  152. wAligned,
  153. h,
  154. d,
  155. mipGobBlocksInY,
  156. mipGobBlocksInZ,
  157. bytesPerPixel);
  158. for (int layer = 0; layer < layers; layer++)
  159. {
  160. int outBaseOffset = layer * sizeInfo.LayerSize + sizeInfo.GetMipOffset(level);
  161. for (int z = 0; z < d; z++)
  162. for (int y = 0; y < h; y++)
  163. {
  164. for (int x = 0; x < w; x++)
  165. {
  166. int offset = outBaseOffset + layoutConverter.GetOffset(x, y, z);
  167. Span<byte> dest = output.Slice(offset, bytesPerPixel);
  168. data.Slice(inOffs + x * bytesPerPixel, bytesPerPixel).CopyTo(dest);
  169. }
  170. inOffs += stride;
  171. }
  172. }
  173. }
  174. return output;
  175. }
  176. public static Span<byte> ConvertLinearToLinearStrided(
  177. int width,
  178. int height,
  179. int blockWidth,
  180. int blockHeight,
  181. int stride,
  182. int bytesPerPixel,
  183. ReadOnlySpan<byte> data)
  184. {
  185. int w = BitUtils.DivRoundUp(width, blockWidth);
  186. int h = BitUtils.DivRoundUp(height, blockHeight);
  187. int inStride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  188. Span<byte> output = new byte[h * stride];
  189. int inOffs = 0;
  190. for (int y = 0; y < h; y++)
  191. {
  192. for (int x = 0; x < w; x++)
  193. {
  194. int offset = y * stride + x * bytesPerPixel;
  195. Span<byte> dest = output.Slice(offset, bytesPerPixel);
  196. data.Slice(inOffs + x * bytesPerPixel, bytesPerPixel).CopyTo(dest);
  197. }
  198. inOffs += inStride;
  199. }
  200. return output;
  201. }
  202. private static int GetTextureSize(
  203. int width,
  204. int height,
  205. int depth,
  206. int levels,
  207. int layers,
  208. int blockWidth,
  209. int blockHeight,
  210. int bytesPerPixel)
  211. {
  212. int layerSize = 0;
  213. for (int level = 0; level < levels; level++)
  214. {
  215. int w = Math.Max(1, width >> level);
  216. int h = Math.Max(1, height >> level);
  217. int d = Math.Max(1, depth >> level);
  218. w = BitUtils.DivRoundUp(w, blockWidth);
  219. h = BitUtils.DivRoundUp(h, blockHeight);
  220. int stride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment);
  221. layerSize += stride * h * d;
  222. }
  223. return layerSize * layers;
  224. }
  225. }
  226. }