Blender.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.X86;
  8. namespace Ryujinx.Graphics.Vic
  9. {
  10. static class Blender
  11. {
  12. public static void BlendOne(Surface dst, Surface src, ref SlotStruct slot)
  13. {
  14. if (Sse41.IsSupported && (dst.Width & 3) == 0)
  15. {
  16. BlendOneSse41(dst, src, ref slot);
  17. return;
  18. }
  19. for (int y = 0; y < dst.Height; y++)
  20. {
  21. for (int x = 0; x < dst.Width; x++)
  22. {
  23. int inR = src.GetR(x, y);
  24. int inG = src.GetG(x, y);
  25. int inB = src.GetB(x, y);
  26. MatrixMultiply(ref slot.ColorMatrixStruct, inR, inG, inB, out int r, out int g, out int b);
  27. r = Math.Clamp(r, slot.SlotConfig.SoftClampLow, slot.SlotConfig.SoftClampHigh);
  28. g = Math.Clamp(g, slot.SlotConfig.SoftClampLow, slot.SlotConfig.SoftClampHigh);
  29. b = Math.Clamp(b, slot.SlotConfig.SoftClampLow, slot.SlotConfig.SoftClampHigh);
  30. dst.SetR(x, y, (ushort)r);
  31. dst.SetG(x, y, (ushort)g);
  32. dst.SetB(x, y, (ushort)b);
  33. dst.SetA(x, y, src.GetA(x, y));
  34. }
  35. }
  36. }
  37. private unsafe static void BlendOneSse41(Surface dst, Surface src, ref SlotStruct slot)
  38. {
  39. Debug.Assert((dst.Width & 3) == 0);
  40. ref MatrixStruct mtx = ref slot.ColorMatrixStruct;
  41. int one = 1 << (mtx.MatrixRShift + 8);
  42. // NOTE: This is buggy on .NET 5.0.100, we use a workaround for now (see https://github.com/dotnet/runtime/issues/44704)
  43. // TODO: Uncomment this when fixed.
  44. //Vector128<int> col1 = Vector128.Create(mtx.MatrixCoeff00, mtx.MatrixCoeff10, mtx.MatrixCoeff20, 0);
  45. //Vector128<int> col2 = Vector128.Create(mtx.MatrixCoeff01, mtx.MatrixCoeff11, mtx.MatrixCoeff21, 0);
  46. //Vector128<int> col3 = Vector128.Create(mtx.MatrixCoeff02, mtx.MatrixCoeff12, mtx.MatrixCoeff22, one);
  47. //Vector128<int> col4 = Vector128.Create(mtx.MatrixCoeff03, mtx.MatrixCoeff13, mtx.MatrixCoeff23, 0);
  48. Vector128<int> col1 = new Vector128<int>();
  49. Vector128<int> col2 = new Vector128<int>();
  50. Vector128<int> col3 = new Vector128<int>();
  51. Vector128<int> col4 = new Vector128<int>();
  52. col1 = Sse41.Insert(col1, mtx.MatrixCoeff00, 0);
  53. col1 = Sse41.Insert(col1, mtx.MatrixCoeff10, 1);
  54. col1 = Sse41.Insert(col1, mtx.MatrixCoeff20, 2);
  55. col1 = Sse41.Insert(col1, 0, 3);
  56. col2 = Sse41.Insert(col2, mtx.MatrixCoeff01, 0);
  57. col2 = Sse41.Insert(col2, mtx.MatrixCoeff11, 1);
  58. col2 = Sse41.Insert(col2, mtx.MatrixCoeff21, 2);
  59. col2 = Sse41.Insert(col2, 0, 3);
  60. col3 = Sse41.Insert(col3, mtx.MatrixCoeff02, 0);
  61. col3 = Sse41.Insert(col3, mtx.MatrixCoeff12, 1);
  62. col3 = Sse41.Insert(col3, mtx.MatrixCoeff22, 2);
  63. col3 = Sse41.Insert(col3, one, 3);
  64. col4 = Sse41.Insert(col4, mtx.MatrixCoeff03, 0);
  65. col4 = Sse41.Insert(col4, mtx.MatrixCoeff13, 1);
  66. col4 = Sse41.Insert(col4, mtx.MatrixCoeff23, 2);
  67. col4 = Sse41.Insert(col4, 0, 3);
  68. Vector128<int> rShift = Vector128.CreateScalar(mtx.MatrixRShift);
  69. Vector128<ushort> clMin = Vector128.Create((ushort)slot.SlotConfig.SoftClampLow);
  70. Vector128<ushort> clMax = Vector128.Create((ushort)slot.SlotConfig.SoftClampHigh);
  71. fixed (Pixel* srcPtr = src.Data, dstPtr = dst.Data)
  72. {
  73. Pixel* ip = srcPtr;
  74. Pixel* op = dstPtr;
  75. for (int y = 0; y < dst.Height; y++, ip += src.Width, op += dst.Width)
  76. {
  77. for (int x = 0; x < dst.Width; x += 4)
  78. {
  79. Vector128<int> pixel1 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x));
  80. Vector128<int> pixel2 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 1));
  81. Vector128<int> pixel3 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 2));
  82. Vector128<int> pixel4 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 3));
  83. Vector128<ushort> pixel12, pixel34;
  84. if (mtx.MatrixEnable)
  85. {
  86. pixel12 = Sse41.PackUnsignedSaturate(
  87. MatrixMultiplySse41(pixel1, col1, col2, col3, col4, rShift),
  88. MatrixMultiplySse41(pixel2, col1, col2, col3, col4, rShift));
  89. pixel34 = Sse41.PackUnsignedSaturate(
  90. MatrixMultiplySse41(pixel3, col1, col2, col3, col4, rShift),
  91. MatrixMultiplySse41(pixel4, col1, col2, col3, col4, rShift));
  92. }
  93. else
  94. {
  95. pixel12 = Sse41.PackUnsignedSaturate(pixel1, pixel2);
  96. pixel34 = Sse41.PackUnsignedSaturate(pixel3, pixel4);
  97. }
  98. pixel12 = Sse41.Min(pixel12, clMax);
  99. pixel34 = Sse41.Min(pixel34, clMax);
  100. pixel12 = Sse41.Max(pixel12, clMin);
  101. pixel34 = Sse41.Max(pixel34, clMin);
  102. Sse2.Store((ushort*)(op + (uint)x + 0), pixel12);
  103. Sse2.Store((ushort*)(op + (uint)x + 2), pixel34);
  104. }
  105. }
  106. }
  107. }
  108. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  109. private static void MatrixMultiply(ref MatrixStruct mtx, int x, int y, int z, out int r, out int g, out int b)
  110. {
  111. if (mtx.MatrixEnable)
  112. {
  113. r = x * mtx.MatrixCoeff00 + y * mtx.MatrixCoeff01 + z * mtx.MatrixCoeff02;
  114. g = x * mtx.MatrixCoeff10 + y * mtx.MatrixCoeff11 + z * mtx.MatrixCoeff12;
  115. b = x * mtx.MatrixCoeff20 + y * mtx.MatrixCoeff21 + z * mtx.MatrixCoeff22;
  116. r >>= mtx.MatrixRShift;
  117. g >>= mtx.MatrixRShift;
  118. b >>= mtx.MatrixRShift;
  119. r += mtx.MatrixCoeff03;
  120. g += mtx.MatrixCoeff13;
  121. b += mtx.MatrixCoeff23;
  122. r >>= 8;
  123. g >>= 8;
  124. b >>= 8;
  125. }
  126. else
  127. {
  128. r = x;
  129. g = y;
  130. b = z;
  131. }
  132. }
  133. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  134. private static Vector128<int> MatrixMultiplySse41(
  135. Vector128<int> pixel,
  136. Vector128<int> col1,
  137. Vector128<int> col2,
  138. Vector128<int> col3,
  139. Vector128<int> col4,
  140. Vector128<int> rShift)
  141. {
  142. Vector128<int> x = Sse2.Shuffle(pixel, 0);
  143. Vector128<int> y = Sse2.Shuffle(pixel, 0x55);
  144. Vector128<int> z = Sse2.Shuffle(pixel, 0xea);
  145. col1 = Sse41.MultiplyLow(col1, x);
  146. col2 = Sse41.MultiplyLow(col2, y);
  147. col3 = Sse41.MultiplyLow(col3, z);
  148. Vector128<int> res = Sse2.Add(col3, Sse2.Add(col1, col2));
  149. res = Sse2.ShiftRightArithmetic(res, rShift);
  150. res = Sse2.Add(res, col4);
  151. res = Sse2.ShiftRightArithmetic(res, 8);
  152. return res;
  153. }
  154. }
  155. }