AstcPixel.cs 1.8 KB

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