Blender.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using Ryujinx.Graphics.Vic.Image;
  2. using Ryujinx.Graphics.Vic.Types;
  3. using System;
  4. using System.Diagnostics;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.Intrinsics;
  7. using System.Runtime.Intrinsics.Arm;
  8. using System.Runtime.Intrinsics.X86;
  9. namespace Ryujinx.Graphics.Vic
  10. {
  11. static class Blender
  12. {
  13. public static void BlendOne(Surface dst, Surface src, ref SlotStruct slot, Rectangle targetRect)
  14. {
  15. int x1 = targetRect.X;
  16. int y1 = targetRect.Y;
  17. int x2 = Math.Min(src.Width, x1 + targetRect.Width);
  18. int y2 = Math.Min(src.Height, y1 + targetRect.Height);
  19. if (((x1 | x2) & 3) == 0)
  20. {
  21. if (Sse41.IsSupported)
  22. {
  23. BlendOneSse41(dst, src, ref slot, x1, y1, x2, y2);
  24. return;
  25. }
  26. else if (AdvSimd.IsSupported)
  27. {
  28. BlendOneAdvSimd(dst, src, ref slot, x1, y1, x2, y2);
  29. return;
  30. }
  31. }
  32. for (int y = y1; y < y2; y++)
  33. {
  34. for (int x = x1; x < x2; x++)
  35. {
  36. int inR = src.GetR(x, y);
  37. int inG = src.GetG(x, y);
  38. int inB = src.GetB(x, y);
  39. MatrixMultiply(ref slot.ColorMatrixStruct, inR, inG, inB, out int r, out int g, out int b);
  40. r = Math.Clamp(r, slot.SlotConfig.SoftClampLow, slot.SlotConfig.SoftClampHigh);
  41. g = Math.Clamp(g, slot.SlotConfig.SoftClampLow, slot.SlotConfig.SoftClampHigh);
  42. b = Math.Clamp(b, slot.SlotConfig.SoftClampLow, slot.SlotConfig.SoftClampHigh);
  43. dst.SetR(x, y, (ushort)r);
  44. dst.SetG(x, y, (ushort)g);
  45. dst.SetB(x, y, (ushort)b);
  46. dst.SetA(x, y, src.GetA(x, y));
  47. }
  48. }
  49. }
  50. private unsafe static void BlendOneSse41(Surface dst, Surface src, ref SlotStruct slot, int x1, int y1, int x2, int y2)
  51. {
  52. Debug.Assert(((x1 | x2) & 3) == 0);
  53. ref MatrixStruct mtx = ref slot.ColorMatrixStruct;
  54. int one = 1 << (mtx.MatrixRShift + 8);
  55. Vector128<int> col1 = Vector128.Create(mtx.MatrixCoeff00, mtx.MatrixCoeff10, mtx.MatrixCoeff20, 0);
  56. Vector128<int> col2 = Vector128.Create(mtx.MatrixCoeff01, mtx.MatrixCoeff11, mtx.MatrixCoeff21, 0);
  57. Vector128<int> col3 = Vector128.Create(mtx.MatrixCoeff02, mtx.MatrixCoeff12, mtx.MatrixCoeff22, one);
  58. Vector128<int> col4 = Vector128.Create(mtx.MatrixCoeff03, mtx.MatrixCoeff13, mtx.MatrixCoeff23, 0);
  59. Vector128<int> rShift = Vector128.CreateScalar(mtx.MatrixRShift);
  60. Vector128<ushort> clMin = Vector128.Create((ushort)slot.SlotConfig.SoftClampLow);
  61. Vector128<ushort> clMax = Vector128.Create((ushort)slot.SlotConfig.SoftClampHigh);
  62. fixed (Pixel* srcPtr = src.Data, dstPtr = dst.Data)
  63. {
  64. Pixel* ip = srcPtr;
  65. Pixel* op = dstPtr;
  66. for (int y = y1; y < y2; y++, ip += src.Width, op += dst.Width)
  67. {
  68. for (int x = x1; x < x2; x += 4)
  69. {
  70. Vector128<int> pixel1 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x));
  71. Vector128<int> pixel2 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 1));
  72. Vector128<int> pixel3 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 2));
  73. Vector128<int> pixel4 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 3));
  74. Vector128<ushort> pixel12, pixel34;
  75. if (mtx.MatrixEnable)
  76. {
  77. pixel12 = Sse41.PackUnsignedSaturate(
  78. MatrixMultiplySse41(pixel1, col1, col2, col3, col4, rShift),
  79. MatrixMultiplySse41(pixel2, col1, col2, col3, col4, rShift));
  80. pixel34 = Sse41.PackUnsignedSaturate(
  81. MatrixMultiplySse41(pixel3, col1, col2, col3, col4, rShift),
  82. MatrixMultiplySse41(pixel4, col1, col2, col3, col4, rShift));
  83. }
  84. else
  85. {
  86. pixel12 = Sse41.PackUnsignedSaturate(pixel1, pixel2);
  87. pixel34 = Sse41.PackUnsignedSaturate(pixel3, pixel4);
  88. }
  89. pixel12 = Sse41.Min(pixel12, clMax);
  90. pixel34 = Sse41.Min(pixel34, clMax);
  91. pixel12 = Sse41.Max(pixel12, clMin);
  92. pixel34 = Sse41.Max(pixel34, clMin);
  93. Sse2.Store((ushort*)(op + (uint)x + 0), pixel12);
  94. Sse2.Store((ushort*)(op + (uint)x + 2), pixel34);
  95. }
  96. }
  97. }
  98. }
  99. private unsafe static void BlendOneAdvSimd(Surface dst, Surface src, ref SlotStruct slot, int x1, int y1, int x2, int y2)
  100. {
  101. Debug.Assert(((x1 | x2) & 3) == 0);
  102. ref MatrixStruct mtx = ref slot.ColorMatrixStruct;
  103. Vector128<int> col1 = Vector128.Create(mtx.MatrixCoeff00, mtx.MatrixCoeff10, mtx.MatrixCoeff20, 0);
  104. Vector128<int> col2 = Vector128.Create(mtx.MatrixCoeff01, mtx.MatrixCoeff11, mtx.MatrixCoeff21, 0);
  105. Vector128<int> col3 = Vector128.Create(mtx.MatrixCoeff02, mtx.MatrixCoeff12, mtx.MatrixCoeff22, 0);
  106. Vector128<int> col4 = Vector128.Create(mtx.MatrixCoeff03, mtx.MatrixCoeff13, mtx.MatrixCoeff23, 0);
  107. Vector128<int> rShift = Vector128.Create(-mtx.MatrixRShift);
  108. Vector128<int> selMask = Vector128.Create(0, 0, 0, -1);
  109. Vector128<ushort> clMin = Vector128.Create((ushort)slot.SlotConfig.SoftClampLow);
  110. Vector128<ushort> clMax = Vector128.Create((ushort)slot.SlotConfig.SoftClampHigh);
  111. fixed (Pixel* srcPtr = src.Data, dstPtr = dst.Data)
  112. {
  113. Pixel* ip = srcPtr;
  114. Pixel* op = dstPtr;
  115. if (mtx.MatrixEnable)
  116. {
  117. for (int y = y1; y < y2; y++, ip += src.Width, op += dst.Width)
  118. {
  119. for (int x = x1; x < x2; x += 4)
  120. {
  121. Vector128<ushort> pixel12 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x));
  122. Vector128<ushort> pixel34 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x + 2));
  123. Vector128<uint> pixel1 = AdvSimd.ZeroExtendWideningLower(pixel12.GetLower());
  124. Vector128<uint> pixel2 = AdvSimd.ZeroExtendWideningUpper(pixel12);
  125. Vector128<uint> pixel3 = AdvSimd.ZeroExtendWideningLower(pixel34.GetLower());
  126. Vector128<uint> pixel4 = AdvSimd.ZeroExtendWideningUpper(pixel34);
  127. Vector128<int> t1 = MatrixMultiplyAdvSimd(pixel1.AsInt32(), col1, col2, col3, col4, rShift, selMask);
  128. Vector128<int> t2 = MatrixMultiplyAdvSimd(pixel2.AsInt32(), col1, col2, col3, col4, rShift, selMask);
  129. Vector128<int> t3 = MatrixMultiplyAdvSimd(pixel3.AsInt32(), col1, col2, col3, col4, rShift, selMask);
  130. Vector128<int> t4 = MatrixMultiplyAdvSimd(pixel4.AsInt32(), col1, col2, col3, col4, rShift, selMask);
  131. Vector64<ushort> lower1 = AdvSimd.ExtractNarrowingSaturateUnsignedLower(t1);
  132. Vector64<ushort> lower3 = AdvSimd.ExtractNarrowingSaturateUnsignedLower(t3);
  133. pixel12 = AdvSimd.ExtractNarrowingSaturateUnsignedUpper(lower1, t2);
  134. pixel34 = AdvSimd.ExtractNarrowingSaturateUnsignedUpper(lower3, t4);
  135. pixel12 = AdvSimd.Min(pixel12, clMax);
  136. pixel34 = AdvSimd.Min(pixel34, clMax);
  137. pixel12 = AdvSimd.Max(pixel12, clMin);
  138. pixel34 = AdvSimd.Max(pixel34, clMin);
  139. AdvSimd.Store((ushort*)(op + (uint)x + 0), pixel12);
  140. AdvSimd.Store((ushort*)(op + (uint)x + 2), pixel34);
  141. }
  142. }
  143. }
  144. else
  145. {
  146. for (int y = y1; y < y2; y++, ip += src.Width, op += dst.Width)
  147. {
  148. for (int x = x1; x < x2; x += 4)
  149. {
  150. Vector128<ushort> pixel12 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x));
  151. Vector128<ushort> pixel34 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x + 2));
  152. pixel12 = AdvSimd.Min(pixel12, clMax);
  153. pixel34 = AdvSimd.Min(pixel34, clMax);
  154. pixel12 = AdvSimd.Max(pixel12, clMin);
  155. pixel34 = AdvSimd.Max(pixel34, clMin);
  156. AdvSimd.Store((ushort*)(op + (uint)x + 0), pixel12);
  157. AdvSimd.Store((ushort*)(op + (uint)x + 2), pixel34);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  164. private static void MatrixMultiply(ref MatrixStruct mtx, int x, int y, int z, out int r, out int g, out int b)
  165. {
  166. if (mtx.MatrixEnable)
  167. {
  168. r = x * mtx.MatrixCoeff00 + y * mtx.MatrixCoeff01 + z * mtx.MatrixCoeff02;
  169. g = x * mtx.MatrixCoeff10 + y * mtx.MatrixCoeff11 + z * mtx.MatrixCoeff12;
  170. b = x * mtx.MatrixCoeff20 + y * mtx.MatrixCoeff21 + z * mtx.MatrixCoeff22;
  171. r >>= mtx.MatrixRShift;
  172. g >>= mtx.MatrixRShift;
  173. b >>= mtx.MatrixRShift;
  174. r += mtx.MatrixCoeff03;
  175. g += mtx.MatrixCoeff13;
  176. b += mtx.MatrixCoeff23;
  177. r >>= 8;
  178. g >>= 8;
  179. b >>= 8;
  180. }
  181. else
  182. {
  183. r = x;
  184. g = y;
  185. b = z;
  186. }
  187. }
  188. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  189. private static Vector128<int> MatrixMultiplySse41(
  190. Vector128<int> pixel,
  191. Vector128<int> col1,
  192. Vector128<int> col2,
  193. Vector128<int> col3,
  194. Vector128<int> col4,
  195. Vector128<int> rShift)
  196. {
  197. Vector128<int> x = Sse2.Shuffle(pixel, 0);
  198. Vector128<int> y = Sse2.Shuffle(pixel, 0x55);
  199. Vector128<int> z = Sse2.Shuffle(pixel, 0xea);
  200. col1 = Sse41.MultiplyLow(col1, x);
  201. col2 = Sse41.MultiplyLow(col2, y);
  202. col3 = Sse41.MultiplyLow(col3, z);
  203. Vector128<int> res = Sse2.Add(col3, Sse2.Add(col1, col2));
  204. res = Sse2.ShiftRightArithmetic(res, rShift);
  205. res = Sse2.Add(res, col4);
  206. res = Sse2.ShiftRightArithmetic(res, 8);
  207. return res;
  208. }
  209. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  210. private static Vector128<int> MatrixMultiplyAdvSimd(
  211. Vector128<int> pixel,
  212. Vector128<int> col1,
  213. Vector128<int> col2,
  214. Vector128<int> col3,
  215. Vector128<int> col4,
  216. Vector128<int> rShift,
  217. Vector128<int> selectMask)
  218. {
  219. Vector128<int> x = AdvSimd.DuplicateSelectedScalarToVector128(pixel, 0);
  220. Vector128<int> y = AdvSimd.DuplicateSelectedScalarToVector128(pixel, 1);
  221. Vector128<int> z = AdvSimd.DuplicateSelectedScalarToVector128(pixel, 2);
  222. col1 = AdvSimd.Multiply(col1, x);
  223. col2 = AdvSimd.Multiply(col2, y);
  224. col3 = AdvSimd.Multiply(col3, z);
  225. Vector128<int> res = AdvSimd.Add(col3, AdvSimd.Add(col1, col2));
  226. res = AdvSimd.ShiftArithmetic(res, rShift);
  227. res = AdvSimd.Add(res, col4);
  228. res = AdvSimd.ShiftRightArithmetic(res, 8);
  229. res = AdvSimd.BitwiseSelect(selectMask, pixel, res);
  230. return res;
  231. }
  232. }
  233. }