PipelineUid.cs 5.2 KB

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