IbUtils.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Memory;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. using System.Runtime.Intrinsics;
  6. using System.Runtime.Intrinsics.X86;
  7. namespace Ryujinx.Graphics.Gpu.Engine.Threed
  8. {
  9. /// <summary>
  10. /// Index buffer utility methods.
  11. /// </summary>
  12. static class IbUtils
  13. {
  14. /// <summary>
  15. /// Minimum size that the vertex buffer must have, in bytes, to make the index counting profitable.
  16. /// </summary>
  17. private const ulong MinimumVbSizeThreshold = 0x200000; // 2 MB
  18. /// <summary>
  19. /// Maximum number of indices that the index buffer may have to make the index counting profitable.
  20. /// </summary>
  21. private const int MaximumIndexCountThreshold = 65536;
  22. /// <summary>
  23. /// Checks if getting the vertex buffer size from the maximum index buffer index is worth it.
  24. /// </summary>
  25. /// <param name="vbSizeMax">Maximum size that the vertex buffer may possibly have, in bytes</param>
  26. /// <param name="indexCount">Total number of indices on the index buffer</param>
  27. /// <returns>True if getting the vertex buffer size from the index buffer may yield performance improvements</returns>
  28. public static bool IsIbCountingProfitable(ulong vbSizeMax, int indexCount)
  29. {
  30. return vbSizeMax >= MinimumVbSizeThreshold && indexCount <= MaximumIndexCountThreshold;
  31. }
  32. /// <summary>
  33. /// Gets the vertex count of the vertex buffer accessed with the indices from the current index buffer.
  34. /// </summary>
  35. /// <param name="mm">GPU memory manager</param>
  36. /// <param name="type">Index buffer element integer type</param>
  37. /// <param name="gpuVa">GPU virtual address of the index buffer</param>
  38. /// <param name="firstIndex">Index of the first index buffer element used on the draw</param>
  39. /// <param name="indexCount">Number of index buffer elements used on the draw</param>
  40. /// <returns>Vertex count</returns>
  41. public static ulong GetVertexCount(MemoryManager mm, IndexType type, ulong gpuVa, int firstIndex, int indexCount)
  42. {
  43. return type switch
  44. {
  45. IndexType.UShort => CountU16(mm, gpuVa, firstIndex, indexCount),
  46. IndexType.UInt => CountU32(mm, gpuVa, firstIndex, indexCount),
  47. _ => CountU8(mm, gpuVa, firstIndex, indexCount)
  48. };
  49. }
  50. /// <summary>
  51. /// Gets the vertex count of the vertex buffer accessed with the indices from the current index buffer, with 8-bit indices.
  52. /// </summary>
  53. /// <param name="mm">GPU memory manager</param>
  54. /// <param name="gpuVa">GPU virtual address of the index buffer</param>
  55. /// <param name="firstIndex">Index of the first index buffer element used on the draw</param>
  56. /// <param name="indexCount">Number of index buffer elements used on the draw</param>
  57. /// <returns>Vertex count</returns>
  58. private unsafe static ulong CountU8(MemoryManager mm, ulong gpuVa, int firstIndex, int indexCount)
  59. {
  60. uint max = 0;
  61. ReadOnlySpan<byte> data = mm.GetSpan(gpuVa, firstIndex + indexCount);
  62. if (Avx2.IsSupported)
  63. {
  64. fixed (byte* pInput = data)
  65. {
  66. int endAligned = firstIndex + ((data.Length - firstIndex) & ~127);
  67. var result = Vector256<byte>.Zero;
  68. for (int i = firstIndex; i < endAligned; i += 128)
  69. {
  70. var dataVec0 = Avx.LoadVector256(pInput + (nuint)(uint)i);
  71. var dataVec1 = Avx.LoadVector256(pInput + (nuint)(uint)i + 32);
  72. var dataVec2 = Avx.LoadVector256(pInput + (nuint)(uint)i + 64);
  73. var dataVec3 = Avx.LoadVector256(pInput + (nuint)(uint)i + 96);
  74. var max01 = Avx2.Max(dataVec0, dataVec1);
  75. var max23 = Avx2.Max(dataVec2, dataVec3);
  76. var max0123 = Avx2.Max(max01, max23);
  77. result = Avx2.Max(result, max0123);
  78. }
  79. result = Avx2.Max(result, Avx2.Shuffle(result.AsInt32(), 0xee).AsByte());
  80. result = Avx2.Max(result, Avx2.Shuffle(result.AsInt32(), 0x55).AsByte());
  81. result = Avx2.Max(result, Avx2.ShuffleLow(result.AsUInt16(), 0x55).AsByte());
  82. result = Avx2.Max(result, Avx2.ShiftRightLogical(result.AsUInt16(), 8).AsByte());
  83. max = Math.Max(result.GetElement(0), result.GetElement(16));
  84. firstIndex = endAligned;
  85. }
  86. }
  87. else if (Sse2.IsSupported)
  88. {
  89. fixed (byte* pInput = data)
  90. {
  91. int endAligned = firstIndex + ((data.Length - firstIndex) & ~63);
  92. var result = Vector128<byte>.Zero;
  93. for (int i = firstIndex; i < endAligned; i += 64)
  94. {
  95. var dataVec0 = Sse2.LoadVector128(pInput + (nuint)(uint)i);
  96. var dataVec1 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 16);
  97. var dataVec2 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 32);
  98. var dataVec3 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 48);
  99. var max01 = Sse2.Max(dataVec0, dataVec1);
  100. var max23 = Sse2.Max(dataVec2, dataVec3);
  101. var max0123 = Sse2.Max(max01, max23);
  102. result = Sse2.Max(result, max0123);
  103. }
  104. result = Sse2.Max(result, Sse2.Shuffle(result.AsInt32(), 0xee).AsByte());
  105. result = Sse2.Max(result, Sse2.Shuffle(result.AsInt32(), 0x55).AsByte());
  106. result = Sse2.Max(result, Sse2.ShuffleLow(result.AsUInt16(), 0x55).AsByte());
  107. result = Sse2.Max(result, Sse2.ShiftRightLogical(result.AsUInt16(), 8).AsByte());
  108. max = result.GetElement(0);
  109. firstIndex = endAligned;
  110. }
  111. }
  112. for (int i = firstIndex; i < data.Length; i++)
  113. {
  114. if (max < data[i]) max = data[i];
  115. }
  116. return (ulong)max + 1;
  117. }
  118. /// <summary>
  119. /// Gets the vertex count of the vertex buffer accessed with the indices from the current index buffer, with 16-bit indices.
  120. /// </summary>
  121. /// <param name="mm">GPU memory manager</param>
  122. /// <param name="gpuVa">GPU virtual address of the index buffer</param>
  123. /// <param name="firstIndex">Index of the first index buffer element used on the draw</param>
  124. /// <param name="indexCount">Number of index buffer elements used on the draw</param>
  125. /// <returns>Vertex count</returns>
  126. private unsafe static ulong CountU16(MemoryManager mm, ulong gpuVa, int firstIndex, int indexCount)
  127. {
  128. uint max = 0;
  129. ReadOnlySpan<ushort> data = MemoryMarshal.Cast<byte, ushort>(mm.GetSpan(gpuVa, (firstIndex + indexCount) * 2));
  130. if (Avx2.IsSupported)
  131. {
  132. fixed (ushort* pInput = data)
  133. {
  134. int endAligned = firstIndex + ((data.Length - firstIndex) & ~63);
  135. var result = Vector256<ushort>.Zero;
  136. for (int i = firstIndex; i < endAligned; i += 64)
  137. {
  138. var dataVec0 = Avx.LoadVector256(pInput + (nuint)(uint)i);
  139. var dataVec1 = Avx.LoadVector256(pInput + (nuint)(uint)i + 16);
  140. var dataVec2 = Avx.LoadVector256(pInput + (nuint)(uint)i + 32);
  141. var dataVec3 = Avx.LoadVector256(pInput + (nuint)(uint)i + 48);
  142. var max01 = Avx2.Max(dataVec0, dataVec1);
  143. var max23 = Avx2.Max(dataVec2, dataVec3);
  144. var max0123 = Avx2.Max(max01, max23);
  145. result = Avx2.Max(result, max0123);
  146. }
  147. result = Avx2.Max(result, Avx2.Shuffle(result.AsInt32(), 0xee).AsUInt16());
  148. result = Avx2.Max(result, Avx2.Shuffle(result.AsInt32(), 0x55).AsUInt16());
  149. result = Avx2.Max(result, Avx2.ShuffleLow(result, 0x55));
  150. max = Math.Max(result.GetElement(0), result.GetElement(8));
  151. firstIndex = endAligned;
  152. }
  153. }
  154. else if (Sse41.IsSupported)
  155. {
  156. fixed (ushort* pInput = data)
  157. {
  158. int endAligned = firstIndex + ((data.Length - firstIndex) & ~31);
  159. var result = Vector128<ushort>.Zero;
  160. for (int i = firstIndex; i < endAligned; i += 32)
  161. {
  162. var dataVec0 = Sse2.LoadVector128(pInput + (nuint)(uint)i);
  163. var dataVec1 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 8);
  164. var dataVec2 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 16);
  165. var dataVec3 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 24);
  166. var max01 = Sse41.Max(dataVec0, dataVec1);
  167. var max23 = Sse41.Max(dataVec2, dataVec3);
  168. var max0123 = Sse41.Max(max01, max23);
  169. result = Sse41.Max(result, max0123);
  170. }
  171. result = Sse41.Max(result, Sse2.Shuffle(result.AsInt32(), 0xee).AsUInt16());
  172. result = Sse41.Max(result, Sse2.Shuffle(result.AsInt32(), 0x55).AsUInt16());
  173. result = Sse41.Max(result, Sse2.ShuffleLow(result, 0x55));
  174. max = result.GetElement(0);
  175. firstIndex = endAligned;
  176. }
  177. }
  178. for (int i = firstIndex; i < data.Length; i++)
  179. {
  180. if (max < data[i]) max = data[i];
  181. }
  182. return (ulong)max + 1;
  183. }
  184. /// <summary>
  185. /// Gets the vertex count of the vertex buffer accessed with the indices from the current index buffer, with 32-bit indices.
  186. /// </summary>
  187. /// <param name="mm">GPU memory manager</param>
  188. /// <param name="gpuVa">GPU virtual address of the index buffer</param>
  189. /// <param name="firstIndex">Index of the first index buffer element used on the draw</param>
  190. /// <param name="indexCount">Number of index buffer elements used on the draw</param>
  191. /// <returns>Vertex count</returns>
  192. private unsafe static ulong CountU32(MemoryManager mm, ulong gpuVa, int firstIndex, int indexCount)
  193. {
  194. uint max = 0;
  195. ReadOnlySpan<uint> data = MemoryMarshal.Cast<byte, uint>(mm.GetSpan(gpuVa, (firstIndex + indexCount) * 4));
  196. if (Avx2.IsSupported)
  197. {
  198. fixed (uint* pInput = data)
  199. {
  200. int endAligned = firstIndex + ((data.Length - firstIndex) & ~31);
  201. var result = Vector256<uint>.Zero;
  202. for (int i = firstIndex; i < endAligned; i += 32)
  203. {
  204. var dataVec0 = Avx.LoadVector256(pInput + (nuint)(uint)i);
  205. var dataVec1 = Avx.LoadVector256(pInput + (nuint)(uint)i + 8);
  206. var dataVec2 = Avx.LoadVector256(pInput + (nuint)(uint)i + 16);
  207. var dataVec3 = Avx.LoadVector256(pInput + (nuint)(uint)i + 24);
  208. var max01 = Avx2.Max(dataVec0, dataVec1);
  209. var max23 = Avx2.Max(dataVec2, dataVec3);
  210. var max0123 = Avx2.Max(max01, max23);
  211. result = Avx2.Max(result, max0123);
  212. }
  213. result = Avx2.Max(result, Avx2.Shuffle(result, 0xee));
  214. result = Avx2.Max(result, Avx2.Shuffle(result, 0x55));
  215. max = Math.Max(result.GetElement(0), result.GetElement(4));
  216. firstIndex = endAligned;
  217. }
  218. }
  219. else if (Sse41.IsSupported)
  220. {
  221. fixed (uint* pInput = data)
  222. {
  223. int endAligned = firstIndex + ((data.Length - firstIndex) & ~15);
  224. var result = Vector128<uint>.Zero;
  225. for (int i = firstIndex; i < endAligned; i += 16)
  226. {
  227. var dataVec0 = Sse2.LoadVector128(pInput + (nuint)(uint)i);
  228. var dataVec1 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 4);
  229. var dataVec2 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 8);
  230. var dataVec3 = Sse2.LoadVector128(pInput + (nuint)(uint)i + 12);
  231. var max01 = Sse41.Max(dataVec0, dataVec1);
  232. var max23 = Sse41.Max(dataVec2, dataVec3);
  233. var max0123 = Sse41.Max(max01, max23);
  234. result = Sse41.Max(result, max0123);
  235. }
  236. result = Sse41.Max(result, Sse2.Shuffle(result, 0xee));
  237. result = Sse41.Max(result, Sse2.Shuffle(result, 0x55));
  238. max = result.GetElement(0);
  239. firstIndex = endAligned;
  240. }
  241. }
  242. for (int i = firstIndex; i < data.Length; i++)
  243. {
  244. if (max < data[i]) max = data[i];
  245. }
  246. return (ulong)max + 1;
  247. }
  248. }
  249. }