LayoutConverter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 AlignmentSize = 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. Span<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 stride = BitUtils.AlignUp(w * bytesPerPixel, AlignmentSize);
  54. int wAligned = BitUtils.AlignUp(w, wAlignment);
  55. BlockLinearLayout layoutConverter = new BlockLinearLayout(
  56. wAligned,
  57. h,
  58. d,
  59. mipGobBlocksInY,
  60. mipGobBlocksInZ,
  61. bytesPerPixel);
  62. for (int layer = 0; layer < layers; layer++)
  63. {
  64. int inBaseOffset = layer * sizeInfo.LayerSize + sizeInfo.GetMipOffset(level);
  65. for (int z = 0; z < d; z++)
  66. for (int y = 0; y < h; y++)
  67. {
  68. for (int x = 0; x < w; x++)
  69. {
  70. int offset = inBaseOffset + layoutConverter.GetOffset(x, y, z);
  71. Span<byte> dest = output.Slice(outOffs + x * bytesPerPixel, bytesPerPixel);
  72. data.Slice(offset, bytesPerPixel).CopyTo(dest);
  73. }
  74. outOffs += stride;
  75. }
  76. }
  77. }
  78. return output;
  79. }
  80. public static Span<byte> ConvertLinearStridedToLinear(
  81. int width,
  82. int height,
  83. int blockWidth,
  84. int blockHeight,
  85. int stride,
  86. int bytesPerPixel,
  87. Span<byte> data)
  88. {
  89. int outOffs = 0;
  90. int w = BitUtils.DivRoundUp(width, blockWidth);
  91. int h = BitUtils.DivRoundUp(height, blockHeight);
  92. int outStride = BitUtils.AlignUp(w * bytesPerPixel, AlignmentSize);
  93. Span<byte> output = new byte[h * outStride];
  94. for (int y = 0; y < h; y++)
  95. {
  96. for (int x = 0; x < w; x++)
  97. {
  98. int offset = y * stride + x * bytesPerPixel;
  99. Span<byte> dest = output.Slice(outOffs + x * bytesPerPixel, bytesPerPixel);
  100. data.Slice(offset, bytesPerPixel).CopyTo(dest);
  101. }
  102. outOffs += outStride;
  103. }
  104. return output;
  105. }
  106. private static int GetTextureSize(
  107. int width,
  108. int height,
  109. int depth,
  110. int levels,
  111. int layers,
  112. int blockWidth,
  113. int blockHeight,
  114. int bytesPerPixel)
  115. {
  116. int layerSize = 0;
  117. for (int level = 0; level < levels; level++)
  118. {
  119. int w = Math.Max(1, width >> level);
  120. int h = Math.Max(1, height >> level);
  121. int d = Math.Max(1, depth >> level);
  122. w = BitUtils.DivRoundUp(w, blockWidth);
  123. h = BitUtils.DivRoundUp(h, blockHeight);
  124. int stride = BitUtils.AlignUp(w * bytesPerPixel, AlignmentSize);
  125. layerSize += stride * h * d;
  126. }
  127. return layerSize * layers;
  128. }
  129. }
  130. }