FormatConverter.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Numerics;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.Intrinsics;
  5. using System.Runtime.Intrinsics.X86;
  6. namespace Ryujinx.Graphics.OpenGL.Image
  7. {
  8. static class FormatConverter
  9. {
  10. public unsafe static byte[] ConvertS8D24ToD24S8(ReadOnlySpan<byte> data)
  11. {
  12. byte[] output = new byte[data.Length];
  13. int start = 0;
  14. if (Avx2.IsSupported)
  15. {
  16. var mask = Vector256.Create(
  17. (byte)3, (byte)0, (byte)1, (byte)2,
  18. (byte)7, (byte)4, (byte)5, (byte)6,
  19. (byte)11, (byte)8, (byte)9, (byte)10,
  20. (byte)15, (byte)12, (byte)13, (byte)14,
  21. (byte)19, (byte)16, (byte)17, (byte)18,
  22. (byte)23, (byte)20, (byte)21, (byte)22,
  23. (byte)27, (byte)24, (byte)25, (byte)26,
  24. (byte)31, (byte)28, (byte)29, (byte)30);
  25. int sizeAligned = data.Length & ~31;
  26. fixed (byte* pInput = data, pOutput = output)
  27. {
  28. for (uint i = 0; i < sizeAligned; i += 32)
  29. {
  30. var dataVec = Avx.LoadVector256(pInput + i);
  31. dataVec = Avx2.Shuffle(dataVec, mask);
  32. Avx.Store(pOutput + i, dataVec);
  33. }
  34. }
  35. start = sizeAligned;
  36. }
  37. else if (Ssse3.IsSupported)
  38. {
  39. var mask = Vector128.Create(
  40. (byte)3, (byte)0, (byte)1, (byte)2,
  41. (byte)7, (byte)4, (byte)5, (byte)6,
  42. (byte)11, (byte)8, (byte)9, (byte)10,
  43. (byte)15, (byte)12, (byte)13, (byte)14);
  44. int sizeAligned = data.Length & ~15;
  45. fixed (byte* pInput = data, pOutput = output)
  46. {
  47. for (uint i = 0; i < sizeAligned; i += 16)
  48. {
  49. var dataVec = Sse2.LoadVector128(pInput + i);
  50. dataVec = Ssse3.Shuffle(dataVec, mask);
  51. Sse2.Store(pOutput + i, dataVec);
  52. }
  53. }
  54. start = sizeAligned;
  55. }
  56. var outSpan = MemoryMarshal.Cast<byte, uint>(output);
  57. var dataSpan = MemoryMarshal.Cast<byte, uint>(data);
  58. for (int i = start / sizeof(uint); i < dataSpan.Length; i++)
  59. {
  60. outSpan[i] = BitOperations.RotateLeft(dataSpan[i], 8);
  61. }
  62. return output;
  63. }
  64. public unsafe static byte[] ConvertD24S8ToS8D24(ReadOnlySpan<byte> data)
  65. {
  66. byte[] output = new byte[data.Length];
  67. int start = 0;
  68. if (Avx2.IsSupported)
  69. {
  70. var mask = Vector256.Create(
  71. (byte)1, (byte)2, (byte)3, (byte)0,
  72. (byte)5, (byte)6, (byte)7, (byte)4,
  73. (byte)9, (byte)10, (byte)11, (byte)8,
  74. (byte)13, (byte)14, (byte)15, (byte)12,
  75. (byte)17, (byte)18, (byte)19, (byte)16,
  76. (byte)21, (byte)22, (byte)23, (byte)20,
  77. (byte)25, (byte)26, (byte)27, (byte)24,
  78. (byte)29, (byte)30, (byte)31, (byte)28);
  79. int sizeAligned = data.Length & ~31;
  80. fixed (byte* pInput = data, pOutput = output)
  81. {
  82. for (uint i = 0; i < sizeAligned; i += 32)
  83. {
  84. var dataVec = Avx.LoadVector256(pInput + i);
  85. dataVec = Avx2.Shuffle(dataVec, mask);
  86. Avx.Store(pOutput + i, dataVec);
  87. }
  88. }
  89. start = sizeAligned;
  90. }
  91. else if (Ssse3.IsSupported)
  92. {
  93. var mask = Vector128.Create(
  94. (byte)1, (byte)2, (byte)3, (byte)0,
  95. (byte)5, (byte)6, (byte)7, (byte)4,
  96. (byte)9, (byte)10, (byte)11, (byte)8,
  97. (byte)13, (byte)14, (byte)15, (byte)12);
  98. int sizeAligned = data.Length & ~15;
  99. fixed (byte* pInput = data, pOutput = output)
  100. {
  101. for (uint i = 0; i < sizeAligned; i += 16)
  102. {
  103. var dataVec = Sse2.LoadVector128(pInput + i);
  104. dataVec = Ssse3.Shuffle(dataVec, mask);
  105. Sse2.Store(pOutput + i, dataVec);
  106. }
  107. }
  108. start = sizeAligned;
  109. }
  110. var outSpan = MemoryMarshal.Cast<byte, uint>(output);
  111. var dataSpan = MemoryMarshal.Cast<byte, uint>(data);
  112. for (int i = start / sizeof(uint); i < dataSpan.Length; i++)
  113. {
  114. outSpan[i] = BitOperations.RotateRight(dataSpan[i], 8);
  115. }
  116. return output;
  117. }
  118. }
  119. }