Blender.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. Vector128<int> col1 = Vector128.Create(mtx.MatrixCoeff00, mtx.MatrixCoeff10, mtx.MatrixCoeff20, 0);
  43. Vector128<int> col2 = Vector128.Create(mtx.MatrixCoeff01, mtx.MatrixCoeff11, mtx.MatrixCoeff21, 0);
  44. Vector128<int> col3 = Vector128.Create(mtx.MatrixCoeff02, mtx.MatrixCoeff12, mtx.MatrixCoeff22, one);
  45. Vector128<int> col4 = Vector128.Create(mtx.MatrixCoeff03, mtx.MatrixCoeff13, mtx.MatrixCoeff23, 0);
  46. Vector128<int> rShift = Vector128.CreateScalar(mtx.MatrixRShift);
  47. Vector128<ushort> clMin = Vector128.Create((ushort)slot.SlotConfig.SoftClampLow);
  48. Vector128<ushort> clMax = Vector128.Create((ushort)slot.SlotConfig.SoftClampHigh);
  49. fixed (Pixel* srcPtr = src.Data, dstPtr = dst.Data)
  50. {
  51. Pixel* ip = srcPtr;
  52. Pixel* op = dstPtr;
  53. for (int y = 0; y < dst.Height; y++, ip += src.Width, op += dst.Width)
  54. {
  55. for (int x = 0; x < dst.Width; x += 4)
  56. {
  57. Vector128<int> pixel1 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x));
  58. Vector128<int> pixel2 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 1));
  59. Vector128<int> pixel3 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 2));
  60. Vector128<int> pixel4 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 3));
  61. Vector128<ushort> pixel12, pixel34;
  62. if (mtx.MatrixEnable)
  63. {
  64. pixel12 = Sse41.PackUnsignedSaturate(
  65. MatrixMultiplySse41(pixel1, col1, col2, col3, col4, rShift),
  66. MatrixMultiplySse41(pixel2, col1, col2, col3, col4, rShift));
  67. pixel34 = Sse41.PackUnsignedSaturate(
  68. MatrixMultiplySse41(pixel3, col1, col2, col3, col4, rShift),
  69. MatrixMultiplySse41(pixel4, col1, col2, col3, col4, rShift));
  70. }
  71. else
  72. {
  73. pixel12 = Sse41.PackUnsignedSaturate(pixel1, pixel2);
  74. pixel34 = Sse41.PackUnsignedSaturate(pixel3, pixel4);
  75. }
  76. pixel12 = Sse41.Min(pixel12, clMax);
  77. pixel34 = Sse41.Min(pixel34, clMax);
  78. pixel12 = Sse41.Max(pixel12, clMin);
  79. pixel34 = Sse41.Max(pixel34, clMin);
  80. Sse2.Store((ushort*)(op + (uint)x + 0), pixel12);
  81. Sse2.Store((ushort*)(op + (uint)x + 2), pixel34);
  82. }
  83. }
  84. }
  85. }
  86. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  87. private static void MatrixMultiply(ref MatrixStruct mtx, int x, int y, int z, out int r, out int g, out int b)
  88. {
  89. if (mtx.MatrixEnable)
  90. {
  91. r = x * mtx.MatrixCoeff00 + y * mtx.MatrixCoeff01 + z * mtx.MatrixCoeff02;
  92. g = x * mtx.MatrixCoeff10 + y * mtx.MatrixCoeff11 + z * mtx.MatrixCoeff12;
  93. b = x * mtx.MatrixCoeff20 + y * mtx.MatrixCoeff21 + z * mtx.MatrixCoeff22;
  94. r >>= mtx.MatrixRShift;
  95. g >>= mtx.MatrixRShift;
  96. b >>= mtx.MatrixRShift;
  97. r += mtx.MatrixCoeff03;
  98. g += mtx.MatrixCoeff13;
  99. b += mtx.MatrixCoeff23;
  100. r >>= 8;
  101. g >>= 8;
  102. b >>= 8;
  103. }
  104. else
  105. {
  106. r = x;
  107. g = y;
  108. b = z;
  109. }
  110. }
  111. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  112. private static Vector128<int> MatrixMultiplySse41(
  113. Vector128<int> pixel,
  114. Vector128<int> col1,
  115. Vector128<int> col2,
  116. Vector128<int> col3,
  117. Vector128<int> col4,
  118. Vector128<int> rShift)
  119. {
  120. Vector128<int> x = Sse2.Shuffle(pixel, 0);
  121. Vector128<int> y = Sse2.Shuffle(pixel, 0x55);
  122. Vector128<int> z = Sse2.Shuffle(pixel, 0xea);
  123. col1 = Sse41.MultiplyLow(col1, x);
  124. col2 = Sse41.MultiplyLow(col2, y);
  125. col3 = Sse41.MultiplyLow(col3, z);
  126. Vector128<int> res = Sse2.Add(col3, Sse2.Add(col1, col2));
  127. res = Sse2.ShiftRightArithmetic(res, rShift);
  128. res = Sse2.Add(res, col4);
  129. res = Sse2.ShiftRightArithmetic(res, 8);
  130. return res;
  131. }
  132. }
  133. }