MacroHLETable.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.GAL;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Graphics.Gpu.Engine.MME
  6. {
  7. /// <summary>
  8. /// Table with information about High-level implementations of GPU Macro code.
  9. /// </summary>
  10. static class MacroHLETable
  11. {
  12. /// <summary>
  13. /// Macroo High-level implementation table entry.
  14. /// </summary>
  15. readonly struct TableEntry
  16. {
  17. /// <summary>
  18. /// Name of the Macro function.
  19. /// </summary>
  20. public MacroHLEFunctionName Name { get; }
  21. /// <summary>
  22. /// Hash of the original binary Macro function code.
  23. /// </summary>
  24. public Hash128 Hash { get; }
  25. /// <summary>
  26. /// Size (in bytes) of the original binary Macro function code.
  27. /// </summary>
  28. public int Length { get; }
  29. /// <summary>
  30. /// Creates a new table entry.
  31. /// </summary>
  32. /// <param name="name">Name of the Macro function</param>
  33. /// <param name="hash">Hash of the original binary Macro function code</param>
  34. /// <param name="length">Size (in bytes) of the original binary Macro function code</param>
  35. public TableEntry(MacroHLEFunctionName name, Hash128 hash, int length)
  36. {
  37. Name = name;
  38. Hash = hash;
  39. Length = length;
  40. }
  41. }
  42. private static readonly TableEntry[] _table = new TableEntry[]
  43. {
  44. new TableEntry(MacroHLEFunctionName.ClearColor, new Hash128(0xA9FB28D1DC43645A, 0xB177E5D2EAE67FB0), 0x28),
  45. new TableEntry(MacroHLEFunctionName.ClearDepthStencil, new Hash128(0x1B96CB77D4879F4F, 0x8557032FE0C965FB), 0x24),
  46. new TableEntry(MacroHLEFunctionName.DrawArraysInstanced, new Hash128(0x197FB416269DBC26, 0x34288C01DDA82202), 0x48),
  47. new TableEntry(MacroHLEFunctionName.DrawElementsInstanced, new Hash128(0x1A501FD3D54EC8E0, 0x6CF570CF79DA74D6), 0x5c),
  48. new TableEntry(MacroHLEFunctionName.DrawElementsIndirect, new Hash128(0x86A3E8E903AF8F45, 0xD35BBA07C23860A4), 0x7c),
  49. new TableEntry(MacroHLEFunctionName.MultiDrawElementsIndirectCount, new Hash128(0x890AF57ED3FB1C37, 0x35D0C95C61F5386F), 0x19C)
  50. };
  51. /// <summary>
  52. /// Checks if the host supports all features required by the HLE macro.
  53. /// </summary>
  54. /// <param name="caps">Host capabilities</param>
  55. /// <param name="name">Name of the HLE macro to be checked</param>
  56. /// <returns>True if the host supports the HLE macro, false otherwise</returns>
  57. private static bool IsMacroHLESupported(Capabilities caps, MacroHLEFunctionName name)
  58. {
  59. if (name == MacroHLEFunctionName.ClearColor ||
  60. name == MacroHLEFunctionName.ClearDepthStencil ||
  61. name == MacroHLEFunctionName.DrawArraysInstanced ||
  62. name == MacroHLEFunctionName.DrawElementsInstanced ||
  63. name == MacroHLEFunctionName.DrawElementsIndirect)
  64. {
  65. return true;
  66. }
  67. else if (name == MacroHLEFunctionName.MultiDrawElementsIndirectCount)
  68. {
  69. return caps.SupportsIndirectParameters;
  70. }
  71. return false;
  72. }
  73. /// <summary>
  74. /// Checks if there's a fast, High-level implementation of the specified Macro code available.
  75. /// </summary>
  76. /// <param name="code">Macro code to be checked</param>
  77. /// <param name="caps">Renderer capabilities to check for this macro HLE support</param>
  78. /// <param name="name">Name of the function if a implementation is available and supported, otherwise <see cref="MacroHLEFunctionName.None"/></param>
  79. /// <returns>True if there is a implementation available and supported, false otherwise</returns>
  80. public static bool TryGetMacroHLEFunction(ReadOnlySpan<int> code, Capabilities caps, out MacroHLEFunctionName name)
  81. {
  82. var mc = MemoryMarshal.Cast<int, byte>(code);
  83. for (int i = 0; i < _table.Length; i++)
  84. {
  85. ref var entry = ref _table[i];
  86. var hash = XXHash128.ComputeHash(mc.Slice(0, entry.Length));
  87. if (hash == entry.Hash)
  88. {
  89. if (IsMacroHLESupported(caps, entry.Name))
  90. {
  91. name = entry.Name;
  92. return true;
  93. }
  94. break;
  95. }
  96. }
  97. name = MacroHLEFunctionName.None;
  98. return false;
  99. }
  100. }
  101. }