RgbaColor8.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.Intrinsics;
  4. using System.Runtime.Intrinsics.X86;
  5. namespace Ryujinx.Graphics.Texture.Utils
  6. {
  7. struct RgbaColor8 : IEquatable<RgbaColor8>
  8. {
  9. public byte R;
  10. public byte G;
  11. public byte B;
  12. public byte A;
  13. public RgbaColor8(byte r, byte g, byte b, byte a)
  14. {
  15. R = r;
  16. G = g;
  17. B = b;
  18. A = a;
  19. }
  20. public static RgbaColor8 FromUInt32(uint color)
  21. {
  22. return Unsafe.As<uint, RgbaColor8>(ref color);
  23. }
  24. public static bool operator ==(RgbaColor8 x, RgbaColor8 y)
  25. {
  26. return x.Equals(y);
  27. }
  28. public static bool operator !=(RgbaColor8 x, RgbaColor8 y)
  29. {
  30. return !x.Equals(y);
  31. }
  32. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  33. public RgbaColor32 GetColor32()
  34. {
  35. if (Sse41.IsSupported)
  36. {
  37. Vector128<byte> color = Vector128.CreateScalarUnsafe(Unsafe.As<RgbaColor8, uint>(ref this)).AsByte();
  38. return new RgbaColor32(Sse41.ConvertToVector128Int32(color));
  39. }
  40. else
  41. {
  42. return new RgbaColor32(R, G, B, A);
  43. }
  44. }
  45. public uint ToUInt32()
  46. {
  47. return Unsafe.As<RgbaColor8, uint>(ref this);
  48. }
  49. public override int GetHashCode()
  50. {
  51. return HashCode.Combine(R, G, B, A);
  52. }
  53. public override bool Equals(object obj)
  54. {
  55. return obj is RgbaColor8 other && Equals(other);
  56. }
  57. public bool Equals(RgbaColor8 other)
  58. {
  59. return R == other.R && G == other.G && B == other.B && A == other.A;
  60. }
  61. public byte GetComponent(int index)
  62. {
  63. return index switch
  64. {
  65. 0 => R,
  66. 1 => G,
  67. 2 => B,
  68. 3 => A,
  69. _ => throw new ArgumentOutOfRangeException(nameof(index))
  70. };
  71. }
  72. }
  73. }