aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.OpenGL/Image/FormatConverter.cs
blob: c4bbf745661c6abd975dae43c9f114af2bf20a82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

namespace Ryujinx.Graphics.OpenGL.Image
{
    static class FormatConverter
    {
        public unsafe static byte[] ConvertS8D24ToD24S8(ReadOnlySpan<byte> data)
        {
            byte[] output = new byte[data.Length];

            int start = 0;

            if (Avx2.IsSupported)
            {
                var mask = Vector256.Create(
                    (byte)3, (byte)0, (byte)1, (byte)2,
                    (byte)7, (byte)4, (byte)5, (byte)6,
                    (byte)11, (byte)8, (byte)9, (byte)10,
                    (byte)15, (byte)12, (byte)13, (byte)14,
                    (byte)19, (byte)16, (byte)17, (byte)18,
                    (byte)23, (byte)20, (byte)21, (byte)22,
                    (byte)27, (byte)24, (byte)25, (byte)26,
                    (byte)31, (byte)28, (byte)29, (byte)30);

                int sizeAligned = data.Length & ~31;

                fixed (byte* pInput = data, pOutput = output)
                {
                    for (uint i = 0; i < sizeAligned; i += 32)
                    {
                        var dataVec = Avx.LoadVector256(pInput + i);

                        dataVec = Avx2.Shuffle(dataVec, mask);

                        Avx.Store(pOutput + i, dataVec);
                    }
                }

                start = sizeAligned;
            }
            else if (Ssse3.IsSupported)
            {
                var mask = Vector128.Create(
                    (byte)3, (byte)0, (byte)1, (byte)2,
                    (byte)7, (byte)4, (byte)5, (byte)6,
                    (byte)11, (byte)8, (byte)9, (byte)10,
                    (byte)15, (byte)12, (byte)13, (byte)14);

                int sizeAligned = data.Length & ~15;

                fixed (byte* pInput = data, pOutput = output)
                {
                    for (uint i = 0; i < sizeAligned; i += 16)
                    {
                        var dataVec = Sse2.LoadVector128(pInput + i);

                        dataVec = Ssse3.Shuffle(dataVec, mask);

                        Sse2.Store(pOutput + i, dataVec);
                    }
                }

                start = sizeAligned;
            }

            var outSpan = MemoryMarshal.Cast<byte, uint>(output);
            var dataSpan = MemoryMarshal.Cast<byte, uint>(data);
            for (int i = start / sizeof(uint); i < dataSpan.Length; i++)
            {
                outSpan[i] = BitOperations.RotateLeft(dataSpan[i], 8);
            }

            return output;
        }

        public unsafe static byte[] ConvertD24S8ToS8D24(ReadOnlySpan<byte> data)
        {
            byte[] output = new byte[data.Length];

            int start = 0;

            if (Avx2.IsSupported)
            {
                var mask = Vector256.Create(
                    (byte)1, (byte)2, (byte)3, (byte)0,
                    (byte)5, (byte)6, (byte)7, (byte)4,
                    (byte)9, (byte)10, (byte)11, (byte)8,
                    (byte)13, (byte)14, (byte)15, (byte)12,
                    (byte)17, (byte)18, (byte)19, (byte)16,
                    (byte)21, (byte)22, (byte)23, (byte)20,
                    (byte)25, (byte)26, (byte)27, (byte)24,
                    (byte)29, (byte)30, (byte)31, (byte)28);

                int sizeAligned = data.Length & ~31;

                fixed (byte* pInput = data, pOutput = output)
                {
                    for (uint i = 0; i < sizeAligned; i += 32)
                    {
                        var dataVec = Avx.LoadVector256(pInput + i);

                        dataVec = Avx2.Shuffle(dataVec, mask);

                        Avx.Store(pOutput + i, dataVec);
                    }
                }

                start = sizeAligned;
            }
            else if (Ssse3.IsSupported)
            {
                var mask = Vector128.Create(
                    (byte)1, (byte)2, (byte)3, (byte)0,
                    (byte)5, (byte)6, (byte)7, (byte)4,
                    (byte)9, (byte)10, (byte)11, (byte)8,
                    (byte)13, (byte)14, (byte)15, (byte)12);

                int sizeAligned = data.Length & ~15;

                fixed (byte* pInput = data, pOutput = output)
                {
                    for (uint i = 0; i < sizeAligned; i += 16)
                    {
                        var dataVec = Sse2.LoadVector128(pInput + i);

                        dataVec = Ssse3.Shuffle(dataVec, mask);

                        Sse2.Store(pOutput + i, dataVec);
                    }
                }

                start = sizeAligned;
            }

            var outSpan = MemoryMarshal.Cast<byte, uint>(output);
            var dataSpan = MemoryMarshal.Cast<byte, uint>(data);
            for (int i = start / sizeof(uint); i < dataSpan.Length; i++)
            {
                outSpan[i] = BitOperations.RotateRight(dataSpan[i], 8);
            }

            return output;
        }
    }
}