aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Texture/Utils/RgbaColor32.cs
blob: 582044d9702bf909c905f165fdb54409413c630d (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

namespace Ryujinx.Graphics.Texture.Utils
{
    struct RgbaColor32 : IEquatable<RgbaColor32>
    {
        private Vector128<int> _color;

        public int R
        {
            get => _color.GetElement(0);
            set => _color = _color.WithElement(0, value);
        }

        public int G
        {
            get => _color.GetElement(1);
            set => _color = _color.WithElement(1, value);
        }

        public int B
        {
            get => _color.GetElement(2);
            set => _color = _color.WithElement(2, value);
        }

        public int A
        {
            get => _color.GetElement(3);
            set => _color = _color.WithElement(3, value);
        }

        public RgbaColor32(Vector128<int> color)
        {
            _color = color;
        }

        public RgbaColor32(int r, int g, int b, int a)
        {
            _color = Vector128.Create(r, g, b, a);
        }

        public RgbaColor32(int scalar)
        {
            _color = Vector128.Create(scalar);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static RgbaColor32 operator +(RgbaColor32 x, RgbaColor32 y)
        {
            if (Sse2.IsSupported)
            {
                return new RgbaColor32(Sse2.Add(x._color, y._color));
            }
            else
            {
                return new RgbaColor32(x.R + y.R, x.G + y.G, x.B + y.B, x.A + y.A);
            }
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static RgbaColor32 operator -(RgbaColor32 x, RgbaColor32 y)
        {
            if (Sse2.IsSupported)
            {
                return new RgbaColor32(Sse2.Subtract(x._color, y._color));
            }
            else
            {
                return new RgbaColor32(x.R - y.R, x.G - y.G, x.B - y.B, x.A - y.A);
            }
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static RgbaColor32 operator *(RgbaColor32 x, RgbaColor32 y)
        {
            if (Sse41.IsSupported)
            {
                return new RgbaColor32(Sse41.MultiplyLow(x._color, y._color));
            }
            else
            {
                return new RgbaColor32(x.R * y.R, x.G * y.G, x.B * y.B, x.A * y.A);
            }
        }

        public static RgbaColor32 operator /(RgbaColor32 x, RgbaColor32 y)
        {
            return new RgbaColor32(x.R / y.R, x.G / y.G, x.B / y.B, x.A / y.A);
        }

        public static RgbaColor32 DivideGuarded(RgbaColor32 x, RgbaColor32 y, int resultIfZero)
        {
            return new RgbaColor32(
                DivideGuarded(x.R, y.R, resultIfZero),
                DivideGuarded(x.G, y.G, resultIfZero),
                DivideGuarded(x.B, y.B, resultIfZero),
                DivideGuarded(x.A, y.A, resultIfZero));
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static RgbaColor32 operator <<(RgbaColor32 x, int shift)
        {
            if (Sse2.IsSupported)
            {
                return new RgbaColor32(Sse2.ShiftLeftLogical(x._color, (byte)shift));
            }
            else
            {
                return new RgbaColor32(x.R << shift, x.G << shift, x.B << shift, x.A << shift);
            }
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static RgbaColor32 operator >>(RgbaColor32 x, int shift)
        {
            if (Sse2.IsSupported)
            {
                return new RgbaColor32(Sse2.ShiftRightLogical(x._color, (byte)shift));
            }
            else
            {
                return new RgbaColor32(x.R >> shift, x.G >> shift, x.B >> shift, x.A >> shift);
            }
        }

        public static bool operator ==(RgbaColor32 x, RgbaColor32 y)
        {
            return x.Equals(y);
        }

        public static bool operator !=(RgbaColor32 x, RgbaColor32 y)
        {
            return !x.Equals(y);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int Dot(RgbaColor32 x, RgbaColor32 y)
        {
            if (Sse41.IsSupported)
            {
                Vector128<int> product = Sse41.MultiplyLow(x._color, y._color);
                Vector128<int> sum = Ssse3.HorizontalAdd(product, product);
                sum = Ssse3.HorizontalAdd(sum, sum);
                return sum.GetElement(0);
            }
            else
            {
                return x.R * y.R + x.G * y.G + x.B * y.B + x.A * y.A;
            }
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static RgbaColor32 Max(RgbaColor32 x, RgbaColor32 y)
        {
            if (Sse41.IsSupported)
            {
                return new RgbaColor32(Sse41.Max(x._color, y._color));
            }
            else
            {
                return new RgbaColor32(Math.Max(x.R, y.R), Math.Max(x.G, y.G), Math.Max(x.B, y.B), Math.Max(x.A, y.A));
            }
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static RgbaColor32 Min(RgbaColor32 x, RgbaColor32 y)
        {
            if (Sse41.IsSupported)
            {
                return new RgbaColor32(Sse41.Min(x._color, y._color));
            }
            else
            {
                return new RgbaColor32(Math.Min(x.R, y.R), Math.Min(x.G, y.G), Math.Min(x.B, y.B), Math.Min(x.A, y.A));
            }
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public RgbaColor8 GetColor8()
        {
            if (Sse41.IsSupported)
            {
                Vector128<int> temp = _color;
                Vector128<ushort> color16 = Sse41.PackUnsignedSaturate(temp, temp);
                Vector128<byte> color8 = Sse2.PackUnsignedSaturate(color16.AsInt16(), color16.AsInt16());
                uint color = color8.AsUInt32().GetElement(0);
                return Unsafe.As<uint, RgbaColor8>(ref color);
            }
            else
            {
                return new RgbaColor8(ClampByte(R), ClampByte(G), ClampByte(B), ClampByte(A));
            }
        }

        private static int DivideGuarded(int dividend, int divisor, int resultIfZero)
        {
            if (divisor == 0)
            {
                return resultIfZero;
            }

            return dividend / divisor;
        }

        private static byte ClampByte(int value)
        {
            return (byte)Math.Clamp(value, 0, 255);
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(R, G, B, A);
        }

        public override bool Equals(object obj)
        {
            return obj is RgbaColor32 other && Equals(other);
        }

        public bool Equals(RgbaColor32 other)
        {
            return _color.Equals(other._color);
        }
    }
}