PipelineUid.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Ryujinx.Common.Memory;
  2. using Silk.NET.Vulkan;
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. using System.Runtime.Intrinsics;
  7. namespace Ryujinx.Graphics.Vulkan
  8. {
  9. struct PipelineUid : IRefEquatable<PipelineUid>
  10. {
  11. public ulong Id0;
  12. public ulong Id1;
  13. public ulong Id2;
  14. public ulong Id3;
  15. public ulong Id4;
  16. public ulong Id5;
  17. public ulong Id6;
  18. public ulong Id7;
  19. public ulong Id8;
  20. public ulong Id9;
  21. private uint VertexAttributeDescriptionsCount => (byte)((Id6 >> 38) & 0xFF);
  22. private uint VertexBindingDescriptionsCount => (byte)((Id6 >> 46) & 0xFF);
  23. private uint ViewportsCount => (byte)((Id6 >> 54) & 0xFF);
  24. private uint ScissorsCount => (byte)((Id7 >> 0) & 0xFF);
  25. private uint ColorBlendAttachmentStateCount => (byte)((Id7 >> 8) & 0xFF);
  26. private bool HasDepthStencil => ((Id7 >> 63) & 0x1) != 0UL;
  27. public Array32<VertexInputAttributeDescription> VertexAttributeDescriptions;
  28. public Array33<VertexInputBindingDescription> VertexBindingDescriptions;
  29. public Array16<Viewport> Viewports;
  30. public Array16<Rect2D> Scissors;
  31. public Array8<PipelineColorBlendAttachmentState> ColorBlendAttachmentState;
  32. public Array9<Format> AttachmentFormats;
  33. public override bool Equals(object obj)
  34. {
  35. return obj is PipelineUid other && Equals(other);
  36. }
  37. public bool Equals(ref PipelineUid other)
  38. {
  39. if (!Unsafe.As<ulong, Vector256<byte>>(ref Id0).Equals(Unsafe.As<ulong, Vector256<byte>>(ref other.Id0)) ||
  40. !Unsafe.As<ulong, Vector256<byte>>(ref Id4).Equals(Unsafe.As<ulong, Vector256<byte>>(ref other.Id4)) ||
  41. !Unsafe.As<ulong, Vector128<byte>>(ref Id8).Equals(Unsafe.As<ulong, Vector128<byte>>(ref other.Id8)))
  42. {
  43. return false;
  44. }
  45. if (!SequenceEqual<VertexInputAttributeDescription>(VertexAttributeDescriptions.AsSpan(), other.VertexAttributeDescriptions.AsSpan(), VertexAttributeDescriptionsCount))
  46. {
  47. return false;
  48. }
  49. if (!SequenceEqual<VertexInputBindingDescription>(VertexBindingDescriptions.AsSpan(), other.VertexBindingDescriptions.AsSpan(), VertexBindingDescriptionsCount))
  50. {
  51. return false;
  52. }
  53. if (!SequenceEqual<PipelineColorBlendAttachmentState>(ColorBlendAttachmentState.AsSpan(), other.ColorBlendAttachmentState.AsSpan(), ColorBlendAttachmentStateCount))
  54. {
  55. return false;
  56. }
  57. if (!SequenceEqual<Format>(AttachmentFormats.AsSpan(), other.AttachmentFormats.AsSpan(), ColorBlendAttachmentStateCount + (HasDepthStencil ? 1u : 0u)))
  58. {
  59. return false;
  60. }
  61. return true;
  62. }
  63. private static bool SequenceEqual<T>(ReadOnlySpan<T> x, ReadOnlySpan<T> y, uint count) where T : unmanaged
  64. {
  65. return MemoryMarshal.Cast<T, byte>(x.Slice(0, (int)count)).SequenceEqual(MemoryMarshal.Cast<T, byte>(y.Slice(0, (int)count)));
  66. }
  67. public override int GetHashCode()
  68. {
  69. ulong hash64 = Id0 * 23 ^
  70. Id1 * 23 ^
  71. Id2 * 23 ^
  72. Id3 * 23 ^
  73. Id4 * 23 ^
  74. Id5 * 23 ^
  75. Id6 * 23 ^
  76. Id7 * 23 ^
  77. Id8 * 23 ^
  78. Id9 * 23;
  79. for (int i = 0; i < (int)VertexAttributeDescriptionsCount; i++)
  80. {
  81. hash64 ^= VertexAttributeDescriptions[i].Binding * 23;
  82. hash64 ^= (uint)VertexAttributeDescriptions[i].Format * 23;
  83. hash64 ^= VertexAttributeDescriptions[i].Location * 23;
  84. hash64 ^= VertexAttributeDescriptions[i].Offset * 23;
  85. }
  86. for (int i = 0; i < (int)VertexBindingDescriptionsCount; i++)
  87. {
  88. hash64 ^= VertexBindingDescriptions[i].Binding * 23;
  89. hash64 ^= (uint)VertexBindingDescriptions[i].InputRate * 23;
  90. hash64 ^= VertexBindingDescriptions[i].Stride * 23;
  91. }
  92. for (int i = 0; i < (int)ColorBlendAttachmentStateCount; i++)
  93. {
  94. hash64 ^= ColorBlendAttachmentState[i].BlendEnable * 23;
  95. hash64 ^= (uint)ColorBlendAttachmentState[i].SrcColorBlendFactor * 23;
  96. hash64 ^= (uint)ColorBlendAttachmentState[i].DstColorBlendFactor * 23;
  97. hash64 ^= (uint)ColorBlendAttachmentState[i].ColorBlendOp * 23;
  98. hash64 ^= (uint)ColorBlendAttachmentState[i].SrcAlphaBlendFactor * 23;
  99. hash64 ^= (uint)ColorBlendAttachmentState[i].DstAlphaBlendFactor * 23;
  100. hash64 ^= (uint)ColorBlendAttachmentState[i].AlphaBlendOp * 23;
  101. hash64 ^= (uint)ColorBlendAttachmentState[i].ColorWriteMask * 23;
  102. }
  103. for (int i = 0; i < (int)ColorBlendAttachmentStateCount; i++)
  104. {
  105. hash64 ^= (uint)AttachmentFormats[i] * 23;
  106. }
  107. return (int)hash64 ^ ((int)(hash64 >> 32) * 17);
  108. }
  109. }
  110. }