RgbaColor32.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 RgbaColor32 : IEquatable<RgbaColor32>
  8. {
  9. private Vector128<int> _color;
  10. public int R
  11. {
  12. get => _color.GetElement(0);
  13. set => _color = _color.WithElement(0, value);
  14. }
  15. public int G
  16. {
  17. get => _color.GetElement(1);
  18. set => _color = _color.WithElement(1, value);
  19. }
  20. public int B
  21. {
  22. get => _color.GetElement(2);
  23. set => _color = _color.WithElement(2, value);
  24. }
  25. public int A
  26. {
  27. get => _color.GetElement(3);
  28. set => _color = _color.WithElement(3, value);
  29. }
  30. public RgbaColor32(Vector128<int> color)
  31. {
  32. _color = color;
  33. }
  34. public RgbaColor32(int r, int g, int b, int a)
  35. {
  36. _color = Vector128.Create(r, g, b, a);
  37. }
  38. public RgbaColor32(int scalar)
  39. {
  40. _color = Vector128.Create(scalar);
  41. }
  42. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  43. public static RgbaColor32 operator +(RgbaColor32 x, RgbaColor32 y)
  44. {
  45. if (Sse2.IsSupported)
  46. {
  47. return new RgbaColor32(Sse2.Add(x._color, y._color));
  48. }
  49. else
  50. {
  51. return new RgbaColor32(x.R + y.R, x.G + y.G, x.B + y.B, x.A + y.A);
  52. }
  53. }
  54. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  55. public static RgbaColor32 operator -(RgbaColor32 x, RgbaColor32 y)
  56. {
  57. if (Sse2.IsSupported)
  58. {
  59. return new RgbaColor32(Sse2.Subtract(x._color, y._color));
  60. }
  61. else
  62. {
  63. return new RgbaColor32(x.R - y.R, x.G - y.G, x.B - y.B, x.A - y.A);
  64. }
  65. }
  66. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  67. public static RgbaColor32 operator *(RgbaColor32 x, RgbaColor32 y)
  68. {
  69. if (Sse41.IsSupported)
  70. {
  71. return new RgbaColor32(Sse41.MultiplyLow(x._color, y._color));
  72. }
  73. else
  74. {
  75. return new RgbaColor32(x.R * y.R, x.G * y.G, x.B * y.B, x.A * y.A);
  76. }
  77. }
  78. public static RgbaColor32 operator /(RgbaColor32 x, RgbaColor32 y)
  79. {
  80. return new RgbaColor32(x.R / y.R, x.G / y.G, x.B / y.B, x.A / y.A);
  81. }
  82. public static RgbaColor32 DivideGuarded(RgbaColor32 x, RgbaColor32 y, int resultIfZero)
  83. {
  84. return new RgbaColor32(
  85. DivideGuarded(x.R, y.R, resultIfZero),
  86. DivideGuarded(x.G, y.G, resultIfZero),
  87. DivideGuarded(x.B, y.B, resultIfZero),
  88. DivideGuarded(x.A, y.A, resultIfZero));
  89. }
  90. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  91. public static RgbaColor32 operator <<(RgbaColor32 x, int shift)
  92. {
  93. if (Sse2.IsSupported)
  94. {
  95. return new RgbaColor32(Sse2.ShiftLeftLogical(x._color, (byte)shift));
  96. }
  97. else
  98. {
  99. return new RgbaColor32(x.R << shift, x.G << shift, x.B << shift, x.A << shift);
  100. }
  101. }
  102. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  103. public static RgbaColor32 operator >>(RgbaColor32 x, int shift)
  104. {
  105. if (Sse2.IsSupported)
  106. {
  107. return new RgbaColor32(Sse2.ShiftRightLogical(x._color, (byte)shift));
  108. }
  109. else
  110. {
  111. return new RgbaColor32(x.R >> shift, x.G >> shift, x.B >> shift, x.A >> shift);
  112. }
  113. }
  114. public static bool operator ==(RgbaColor32 x, RgbaColor32 y)
  115. {
  116. return x.Equals(y);
  117. }
  118. public static bool operator !=(RgbaColor32 x, RgbaColor32 y)
  119. {
  120. return !x.Equals(y);
  121. }
  122. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  123. public static int Dot(RgbaColor32 x, RgbaColor32 y)
  124. {
  125. if (Sse41.IsSupported)
  126. {
  127. Vector128<int> product = Sse41.MultiplyLow(x._color, y._color);
  128. Vector128<int> sum = Ssse3.HorizontalAdd(product, product);
  129. sum = Ssse3.HorizontalAdd(sum, sum);
  130. return sum.GetElement(0);
  131. }
  132. else
  133. {
  134. return x.R * y.R + x.G * y.G + x.B * y.B + x.A * y.A;
  135. }
  136. }
  137. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  138. public static RgbaColor32 Max(RgbaColor32 x, RgbaColor32 y)
  139. {
  140. if (Sse41.IsSupported)
  141. {
  142. return new RgbaColor32(Sse41.Max(x._color, y._color));
  143. }
  144. else
  145. {
  146. return new RgbaColor32(Math.Max(x.R, y.R), Math.Max(x.G, y.G), Math.Max(x.B, y.B), Math.Max(x.A, y.A));
  147. }
  148. }
  149. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  150. public static RgbaColor32 Min(RgbaColor32 x, RgbaColor32 y)
  151. {
  152. if (Sse41.IsSupported)
  153. {
  154. return new RgbaColor32(Sse41.Min(x._color, y._color));
  155. }
  156. else
  157. {
  158. return new RgbaColor32(Math.Min(x.R, y.R), Math.Min(x.G, y.G), Math.Min(x.B, y.B), Math.Min(x.A, y.A));
  159. }
  160. }
  161. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  162. public RgbaColor8 GetColor8()
  163. {
  164. if (Sse41.IsSupported)
  165. {
  166. Vector128<int> temp = _color;
  167. Vector128<ushort> color16 = Sse41.PackUnsignedSaturate(temp, temp);
  168. Vector128<byte> color8 = Sse2.PackUnsignedSaturate(color16.AsInt16(), color16.AsInt16());
  169. uint color = color8.AsUInt32().GetElement(0);
  170. return Unsafe.As<uint, RgbaColor8>(ref color);
  171. }
  172. else
  173. {
  174. return new RgbaColor8(ClampByte(R), ClampByte(G), ClampByte(B), ClampByte(A));
  175. }
  176. }
  177. private static int DivideGuarded(int dividend, int divisor, int resultIfZero)
  178. {
  179. if (divisor == 0)
  180. {
  181. return resultIfZero;
  182. }
  183. return dividend / divisor;
  184. }
  185. private static byte ClampByte(int value)
  186. {
  187. return (byte)Math.Clamp(value, 0, 255);
  188. }
  189. public override int GetHashCode()
  190. {
  191. return HashCode.Combine(R, G, B, A);
  192. }
  193. public override bool Equals(object obj)
  194. {
  195. return obj is RgbaColor32 other && Equals(other);
  196. }
  197. public bool Equals(RgbaColor32 other)
  198. {
  199. return _color.Equals(other._color);
  200. }
  201. }
  202. }