Ryujinx/Ryujinx.Graphics.Texture/Astc/Bits.cs
Alex Barney d1ab9fb42c ASTC optimizations (#845)
* ASTC optimizations

* Move code to Ryujinx.Common

* Support 3D textures

* Address feedback

* Remove ASTC logging

* Use stackalloc instead of a Buffer20 struct

* Code style and cleanup

* Respond to feedback

* Rearrange public/private property ordering
2020-01-09 02:13:00 +01:00

67 lines
1.8 KiB
C#

namespace Ryujinx.Graphics.Texture.Astc
{
internal static class Bits
{
public static readonly ushort[] Replicate8_16Table;
public static readonly byte[] Replicate1_7Table;
static Bits()
{
Replicate8_16Table = new ushort[0x200];
Replicate1_7Table = new byte[0x200];
for (int i = 0; i < 0x200; i++)
{
Replicate8_16Table[i] = (ushort)Replicate(i, 8, 16);
Replicate1_7Table[i] = (byte)Replicate(i, 1, 7);
}
}
public static int Replicate8_16(int value)
{
return Replicate8_16Table[value];
}
public static int Replicate1_7(int value)
{
return Replicate1_7Table[value];
}
public static int Replicate(int value, int numberBits, int toBit)
{
if (numberBits == 0) return 0;
if (toBit == 0) return 0;
int tempValue = value & ((1 << numberBits) - 1);
int retValue = tempValue;
int resLength = numberBits;
while (resLength < toBit)
{
int comp = 0;
if (numberBits > toBit - resLength)
{
int newShift = toBit - resLength;
comp = numberBits - newShift;
numberBits = newShift;
}
retValue <<= numberBits;
retValue |= tempValue >> comp;
resLength += numberBits;
}
return retValue;
}
// Transfers a bit as described in C.2.14
public static void BitTransferSigned(ref int a, ref int b)
{
b >>= 1;
b |= a & 0x80;
a >>= 1;
a &= 0x3F;
if ((a & 0x20) != 0) a -= 0x40;
}
}
}