MacroHLETable.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. 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.MultiDrawElementsIndirectCount, new Hash128(0x890AF57ED3FB1C37, 0x35D0C95C61F5386F), 0x19C)
  47. };
  48. private static bool IsMacroHLESupported(Capabilities caps, MacroHLEFunctionName name)
  49. {
  50. if (name == MacroHLEFunctionName.ClearColor ||
  51. name == MacroHLEFunctionName.ClearDepthStencil)
  52. {
  53. return true;
  54. }
  55. else if (name == MacroHLEFunctionName.MultiDrawElementsIndirectCount)
  56. {
  57. return caps.SupportsIndirectParameters;
  58. }
  59. return false;
  60. }
  61. /// <summary>
  62. /// Checks if there's a fast, High-level implementation of the specified Macro code available.
  63. /// </summary>
  64. /// <param name="code">Macro code to be checked</param>
  65. /// <param name="caps">Renderer capabilities to check for this macro HLE support</param>
  66. /// <param name="name">Name of the function if a implementation is available and supported, otherwise <see cref="MacroHLEFunctionName.None"/></param>
  67. /// <returns>True if there is a implementation available and supported, false otherwise</returns>
  68. public static bool TryGetMacroHLEFunction(ReadOnlySpan<int> code, Capabilities caps, out MacroHLEFunctionName name)
  69. {
  70. var mc = MemoryMarshal.Cast<int, byte>(code);
  71. for (int i = 0; i < Table.Length; i++)
  72. {
  73. ref var entry = ref Table[i];
  74. var hash = XXHash128.ComputeHash(mc.Slice(0, entry.Length));
  75. if (hash == entry.Hash)
  76. {
  77. name = entry.Name;
  78. return IsMacroHLESupported(caps, name);
  79. }
  80. }
  81. name = MacroHLEFunctionName.None;
  82. return false;
  83. }
  84. }
  85. }