mirror of
https://github.com/Ryujinx/Ryujinx.git
synced 2025-06-07 16:30:58 -07:00
* dotnet format style --severity info Some changes were manually reverted. * Restore a few unused methods and variables * Silence dotnet format IDE0060 warnings * Silence dotnet format IDE0059 warnings * Address or silence dotnet format CA2208 warnings * Address most dotnet format whitespace warnings * Apply dotnet format whitespace formatting A few of them have been manually reverted and the corresponding warning was silenced * Format if-blocks correctly * Add comments to disabled warnings * Simplify properties and array initialization, Use const when possible, Remove trailing commas * Address IDE0251 warnings * Silence IDE0060 in .editorconfig * Revert "Simplify properties and array initialization, Use const when possible, Remove trailing commas" This reverts commit 9462e4136c0a2100dc28b20cf9542e06790aa67e. * dotnet format whitespace after rebase * First dotnet format pass * Apply suggestions from code review Co-authored-by: Ac_K <Acoustik666@gmail.com> * Address review feedback * Update src/Ryujinx.Graphics.Texture/Astc/AstcDecoder.cs Co-authored-by: Ac_K <Acoustik666@gmail.com> --------- Co-authored-by: Ac_K <Acoustik666@gmail.com>
230 lines
6.9 KiB
C#
230 lines
6.9 KiB
C#
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
|
|
{
|
|
readonly get => _color.GetElement(0);
|
|
set => _color = _color.WithElement(0, value);
|
|
}
|
|
|
|
public int G
|
|
{
|
|
readonly get => _color.GetElement(1);
|
|
set => _color = _color.WithElement(1, value);
|
|
}
|
|
|
|
public int B
|
|
{
|
|
readonly get => _color.GetElement(2);
|
|
set => _color = _color.WithElement(2, value);
|
|
}
|
|
|
|
public int A
|
|
{
|
|
readonly 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 readonly 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 readonly override int GetHashCode()
|
|
{
|
|
return HashCode.Combine(R, G, B, A);
|
|
}
|
|
|
|
public readonly override bool Equals(object obj)
|
|
{
|
|
return obj is RgbaColor32 other && Equals(other);
|
|
}
|
|
|
|
public readonly bool Equals(RgbaColor32 other)
|
|
{
|
|
return _color.Equals(other._color);
|
|
}
|
|
}
|
|
}
|