PipelineUid.cs 5.4 KB

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