BCnDecoder.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using Ryujinx.Common;
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Graphics.Texture
  6. {
  7. public static class BCnDecoder
  8. {
  9. private const int BlockWidth = 4;
  10. private const int BlockHeight = 4;
  11. public static byte[] DecodeBC4(ReadOnlySpan<byte> data, int width, int height, int depth, int levels, int layers, bool signed)
  12. {
  13. int size = 0;
  14. for (int l = 0; l < levels; l++)
  15. {
  16. size += Math.Max(1, width >> l) * Math.Max(1, height >> l) * Math.Max(1, depth >> l) * layers;
  17. }
  18. byte[] output = new byte[size];
  19. ReadOnlySpan<ulong> data64 = MemoryMarshal.Cast<byte, ulong>(data);
  20. Span<byte> rPal = stackalloc byte[8];
  21. int baseOOffs = 0;
  22. for (int l = 0; l < levels; l++)
  23. {
  24. int w = BitUtils.DivRoundUp(width, BlockWidth);
  25. int h = BitUtils.DivRoundUp(height, BlockHeight);
  26. for (int l2 = 0; l2 < layers; l2++)
  27. {
  28. for (int z = 0; z < depth; z++)
  29. {
  30. for (int y = 0; y < h; y++)
  31. {
  32. int baseY = y * BlockHeight;
  33. for (int x = 0; x < w; x++)
  34. {
  35. int baseX = x * BlockWidth;
  36. int lineBaseOOffs = baseOOffs + baseX;
  37. ulong block = data64[0];
  38. rPal[0] = (byte)block;
  39. rPal[1] = (byte)(block >> 8);
  40. if (signed)
  41. {
  42. CalculateBC3AlphaS(rPal);
  43. }
  44. else
  45. {
  46. CalculateBC3Alpha(rPal);
  47. }
  48. ulong rI = block >> 16;
  49. for (int texel = 0; texel < BlockWidth * BlockHeight; texel++)
  50. {
  51. int tX = texel & 3;
  52. int tY = texel >> 2;
  53. if (baseX + tX >= width || baseY + tY >= height)
  54. {
  55. continue;
  56. }
  57. int shift = texel * 3;
  58. byte r = rPal[(int)((rI >> shift) & 7)];
  59. int oOffs = lineBaseOOffs + tY * width + tX;
  60. output[oOffs] = r;
  61. }
  62. data64 = data64.Slice(1);
  63. }
  64. baseOOffs += width * (baseY + BlockHeight > height ? (height & (BlockHeight - 1)) : BlockHeight);
  65. }
  66. }
  67. }
  68. width = Math.Max(1, width >> 1);
  69. height = Math.Max(1, height >> 1);
  70. depth = Math.Max(1, depth >> 1);
  71. }
  72. return output;
  73. }
  74. public static byte[] DecodeBC5(ReadOnlySpan<byte> data, int width, int height, int depth, int levels, int layers, bool signed)
  75. {
  76. int size = 0;
  77. for (int l = 0; l < levels; l++)
  78. {
  79. size += Math.Max(1, width >> l) * Math.Max(1, height >> l) * Math.Max(1, depth >> l) * layers * 2;
  80. }
  81. byte[] output = new byte[size];
  82. ReadOnlySpan<ulong> data64 = MemoryMarshal.Cast<byte, ulong>(data);
  83. Span<byte> rPal = stackalloc byte[8];
  84. Span<byte> gPal = stackalloc byte[8];
  85. int baseOOffs = 0;
  86. for (int l = 0; l < levels; l++)
  87. {
  88. int w = BitUtils.DivRoundUp(width, BlockWidth);
  89. int h = BitUtils.DivRoundUp(height, BlockHeight);
  90. for (int l2 = 0; l2 < layers; l2++)
  91. {
  92. for (int z = 0; z < depth; z++)
  93. {
  94. for (int y = 0; y < h; y++)
  95. {
  96. int baseY = y * BlockHeight;
  97. for (int x = 0; x < w; x++)
  98. {
  99. int baseX = x * BlockWidth;
  100. int lineBaseOOffs = baseOOffs + baseX;
  101. ulong blockL = data64[0];
  102. ulong blockH = data64[1];
  103. rPal[0] = (byte)blockL;
  104. rPal[1] = (byte)(blockL >> 8);
  105. gPal[0] = (byte)blockH;
  106. gPal[1] = (byte)(blockH >> 8);
  107. if (signed)
  108. {
  109. CalculateBC3AlphaS(rPal);
  110. CalculateBC3AlphaS(gPal);
  111. }
  112. else
  113. {
  114. CalculateBC3Alpha(rPal);
  115. CalculateBC3Alpha(gPal);
  116. }
  117. ulong rI = blockL >> 16;
  118. ulong gI = blockH >> 16;
  119. for (int texel = 0; texel < BlockWidth * BlockHeight; texel++)
  120. {
  121. int tX = texel & 3;
  122. int tY = texel >> 2;
  123. if (baseX + tX >= width || baseY + tY >= height)
  124. {
  125. continue;
  126. }
  127. int shift = texel * 3;
  128. byte r = rPal[(int)((rI >> shift) & 7)];
  129. byte g = gPal[(int)((gI >> shift) & 7)];
  130. int oOffs = (lineBaseOOffs + tY * width + tX) * 2;
  131. output[oOffs + 0] = r;
  132. output[oOffs + 1] = g;
  133. }
  134. data64 = data64.Slice(2);
  135. }
  136. baseOOffs += width * (baseY + BlockHeight > height ? (height & (BlockHeight - 1)) : BlockHeight);
  137. }
  138. }
  139. }
  140. width = Math.Max(1, width >> 1);
  141. height = Math.Max(1, height >> 1);
  142. depth = Math.Max(1, depth >> 1);
  143. }
  144. return output;
  145. }
  146. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  147. private static void CalculateBC3Alpha(Span<byte> alpha)
  148. {
  149. for (int i = 2; i < 8; i++)
  150. {
  151. if (alpha[0] > alpha[1])
  152. {
  153. alpha[i] = (byte)(((8 - i) * alpha[0] + (i - 1) * alpha[1]) / 7);
  154. }
  155. else if (i < 6)
  156. {
  157. alpha[i] = (byte)(((6 - i) * alpha[0] + (i - 1) * alpha[1]) / 7);
  158. }
  159. else if (i == 6)
  160. {
  161. alpha[i] = 0;
  162. }
  163. else /* i == 7 */
  164. {
  165. alpha[i] = 0xff;
  166. }
  167. }
  168. }
  169. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  170. private static void CalculateBC3AlphaS(Span<byte> alpha)
  171. {
  172. for (int i = 2; i < 8; i++)
  173. {
  174. if ((sbyte)alpha[0] > (sbyte)alpha[1])
  175. {
  176. alpha[i] = (byte)(((8 - i) * (sbyte)alpha[0] + (i - 1) * (sbyte)alpha[1]) / 7);
  177. }
  178. else if (i < 6)
  179. {
  180. alpha[i] = (byte)(((6 - i) * (sbyte)alpha[0] + (i - 1) * (sbyte)alpha[1]) / 7);
  181. }
  182. else if (i == 6)
  183. {
  184. alpha[i] = 0x80;
  185. }
  186. else /* i == 7 */
  187. {
  188. alpha[i] = 0x7f;
  189. }
  190. }
  191. }
  192. }
  193. }