PixelConverter.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Runtime.Intrinsics;
  4. using System.Runtime.Intrinsics.X86;
  5. namespace Ryujinx.Graphics.Texture
  6. {
  7. public static class PixelConverter
  8. {
  9. public unsafe static byte[] ConvertR4G4ToR4G4B4A4(ReadOnlySpan<byte> data)
  10. {
  11. byte[] output = new byte[data.Length * 2];
  12. int start = 0;
  13. if (Sse41.IsSupported)
  14. {
  15. int sizeTrunc = data.Length & ~7;
  16. start = sizeTrunc;
  17. fixed (byte* inputPtr = data, outputPtr = output)
  18. {
  19. for (ulong offset = 0; offset < (ulong)sizeTrunc; offset += 8)
  20. {
  21. Sse2.Store(outputPtr + offset * 2, Sse41.ConvertToVector128Int16(inputPtr + offset).AsByte());
  22. }
  23. }
  24. }
  25. Span<ushort> outputSpan = MemoryMarshal.Cast<byte, ushort>(output);
  26. for (int i = start; i < data.Length; i++)
  27. {
  28. outputSpan[i] = (ushort)data[i];
  29. }
  30. return output;
  31. }
  32. }
  33. }