AstcPixel.cs
1 using System; 2 using System.Runtime.CompilerServices; 3 using System.Runtime.InteropServices; 4 5 namespace Ryujinx.Graphics.Texture.Astc 6 { 7 [StructLayout(LayoutKind.Sequential)] 8 struct AstcPixel 9 { 10 internal const int StructSize = 12; 11 12 public short A; 13 public short R; 14 public short G; 15 public short B; 16 17 private uint _bitDepthInt; 18 19 private Span<byte> BitDepth => MemoryMarshal.CreateSpan(ref Unsafe.As<uint, byte>(ref _bitDepthInt), 4); 20 private Span<short> Components => MemoryMarshal.CreateSpan(ref A, 4); 21 22 public AstcPixel(short a, short r, short g, short b) 23 { 24 A = a; 25 R = r; 26 G = g; 27 B = b; 28 29 _bitDepthInt = 0x08080808; 30 } 31 32 public void ClampByte() 33 { 34 R = Math.Min(Math.Max(R, (short)0), (short)255); 35 G = Math.Min(Math.Max(G, (short)0), (short)255); 36 B = Math.Min(Math.Max(B, (short)0), (short)255); 37 A = Math.Min(Math.Max(A, (short)0), (short)255); 38 } 39 40 public short GetComponent(int index) 41 { 42 return Components[index]; 43 } 44 45 public void SetComponent(int index, int value) 46 { 47 Components[index] = (short)value; 48 } 49 50 public readonly int Pack() 51 { 52 return A << 24 | 53 B << 16 | 54 G << 8 | 55 R << 0; 56 } 57 58 // Adds more precision to the blue channel as described 59 // in C.2.14 60 public static AstcPixel BlueContract(int a, int r, int g, int b) 61 { 62 return new AstcPixel((short)(a), 63 (short)((r + b) >> 1), 64 (short)((g + b) >> 1), 65 (short)(b)); 66 } 67 } 68 }