Blender.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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, Rectangle targetRect)
  13. {
  14. int x1 = targetRect.X;
  15. int y1 = targetRect.Y;
  16. int x2 = Math.Min(src.Width, x1 + targetRect.Width);
  17. int y2 = Math.Min(src.Height, y1 + targetRect.Height);
  18. if (Sse41.IsSupported && ((x1 | x2) & 3) == 0)
  19. {
  20. BlendOneSse41(dst, src, ref slot, x1, y1, x2, y2);
  21. return;
  22. }
  23. for (int y = y1; y < y2; y++)
  24. {
  25. for (int x = x1; x < x2; x++)
  26. {
  27. int inR = src.GetR(x, y);
  28. int inG = src.GetG(x, y);
  29. int inB = src.GetB(x, y);
  30. MatrixMultiply(ref slot.ColorMatrixStruct, inR, inG, inB, out int r, out int g, out int b);
  31. r = Math.Clamp(r, slot.SlotConfig.SoftClampLow, slot.SlotConfig.SoftClampHigh);
  32. g = Math.Clamp(g, slot.SlotConfig.SoftClampLow, slot.SlotConfig.SoftClampHigh);
  33. b = Math.Clamp(b, slot.SlotConfig.SoftClampLow, slot.SlotConfig.SoftClampHigh);
  34. dst.SetR(x, y, (ushort)r);
  35. dst.SetG(x, y, (ushort)g);
  36. dst.SetB(x, y, (ushort)b);
  37. dst.SetA(x, y, src.GetA(x, y));
  38. }
  39. }
  40. }
  41. private unsafe static void BlendOneSse41(Surface dst, Surface src, ref SlotStruct slot, int x1, int y1, int x2, int y2)
  42. {
  43. Debug.Assert(((x1 | x2) & 3) == 0);
  44. ref MatrixStruct mtx = ref slot.ColorMatrixStruct;
  45. int one = 1 << (mtx.MatrixRShift + 8);
  46. Vector128<int> col1 = Vector128.Create(mtx.MatrixCoeff00, mtx.MatrixCoeff10, mtx.MatrixCoeff20, 0);
  47. Vector128<int> col2 = Vector128.Create(mtx.MatrixCoeff01, mtx.MatrixCoeff11, mtx.MatrixCoeff21, 0);
  48. Vector128<int> col3 = Vector128.Create(mtx.MatrixCoeff02, mtx.MatrixCoeff12, mtx.MatrixCoeff22, one);
  49. Vector128<int> col4 = Vector128.Create(mtx.MatrixCoeff03, mtx.MatrixCoeff13, mtx.MatrixCoeff23, 0);
  50. Vector128<int> rShift = Vector128.CreateScalar(mtx.MatrixRShift);
  51. Vector128<ushort> clMin = Vector128.Create((ushort)slot.SlotConfig.SoftClampLow);
  52. Vector128<ushort> clMax = Vector128.Create((ushort)slot.SlotConfig.SoftClampHigh);
  53. fixed (Pixel* srcPtr = src.Data, dstPtr = dst.Data)
  54. {
  55. Pixel* ip = srcPtr;
  56. Pixel* op = dstPtr;
  57. for (int y = y1; y < y2; y++, ip += src.Width, op += dst.Width)
  58. {
  59. for (int x = x1; x < x2; x += 4)
  60. {
  61. Vector128<int> pixel1 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x));
  62. Vector128<int> pixel2 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 1));
  63. Vector128<int> pixel3 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 2));
  64. Vector128<int> pixel4 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 3));
  65. Vector128<ushort> pixel12, pixel34;
  66. if (mtx.MatrixEnable)
  67. {
  68. pixel12 = Sse41.PackUnsignedSaturate(
  69. MatrixMultiplySse41(pixel1, col1, col2, col3, col4, rShift),
  70. MatrixMultiplySse41(pixel2, col1, col2, col3, col4, rShift));
  71. pixel34 = Sse41.PackUnsignedSaturate(
  72. MatrixMultiplySse41(pixel3, col1, col2, col3, col4, rShift),
  73. MatrixMultiplySse41(pixel4, col1, col2, col3, col4, rShift));
  74. }
  75. else
  76. {
  77. pixel12 = Sse41.PackUnsignedSaturate(pixel1, pixel2);
  78. pixel34 = Sse41.PackUnsignedSaturate(pixel3, pixel4);
  79. }
  80. pixel12 = Sse41.Min(pixel12, clMax);
  81. pixel34 = Sse41.Min(pixel34, clMax);
  82. pixel12 = Sse41.Max(pixel12, clMin);
  83. pixel34 = Sse41.Max(pixel34, clMin);
  84. Sse2.Store((ushort*)(op + (uint)x + 0), pixel12);
  85. Sse2.Store((ushort*)(op + (uint)x + 2), pixel34);
  86. }
  87. }
  88. }
  89. }
  90. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  91. private static void MatrixMultiply(ref MatrixStruct mtx, int x, int y, int z, out int r, out int g, out int b)
  92. {
  93. if (mtx.MatrixEnable)
  94. {
  95. r = x * mtx.MatrixCoeff00 + y * mtx.MatrixCoeff01 + z * mtx.MatrixCoeff02;
  96. g = x * mtx.MatrixCoeff10 + y * mtx.MatrixCoeff11 + z * mtx.MatrixCoeff12;
  97. b = x * mtx.MatrixCoeff20 + y * mtx.MatrixCoeff21 + z * mtx.MatrixCoeff22;
  98. r >>= mtx.MatrixRShift;
  99. g >>= mtx.MatrixRShift;
  100. b >>= mtx.MatrixRShift;
  101. r += mtx.MatrixCoeff03;
  102. g += mtx.MatrixCoeff13;
  103. b += mtx.MatrixCoeff23;
  104. r >>= 8;
  105. g >>= 8;
  106. b >>= 8;
  107. }
  108. else
  109. {
  110. r = x;
  111. g = y;
  112. b = z;
  113. }
  114. }
  115. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  116. private static Vector128<int> MatrixMultiplySse41(
  117. Vector128<int> pixel,
  118. Vector128<int> col1,
  119. Vector128<int> col2,
  120. Vector128<int> col3,
  121. Vector128<int> col4,
  122. Vector128<int> rShift)
  123. {
  124. Vector128<int> x = Sse2.Shuffle(pixel, 0);
  125. Vector128<int> y = Sse2.Shuffle(pixel, 0x55);
  126. Vector128<int> z = Sse2.Shuffle(pixel, 0xea);
  127. col1 = Sse41.MultiplyLow(col1, x);
  128. col2 = Sse41.MultiplyLow(col2, y);
  129. col3 = Sse41.MultiplyLow(col3, z);
  130. Vector128<int> res = Sse2.Add(col3, Sse2.Add(col1, col2));
  131. res = Sse2.ShiftRightArithmetic(res, rShift);
  132. res = Sse2.Add(res, col4);
  133. res = Sse2.ShiftRightArithmetic(res, 8);
  134. return res;
  135. }
  136. }
  137. }