LayoutConverter.cs 9.4 KB

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