PipelineUid.cs 5.3 KB

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