Block.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. namespace Ryujinx.Graphics.Texture.Utils
  2. {
  3. struct Block
  4. {
  5. public ulong Low;
  6. public ulong High;
  7. public void Encode(ulong value, ref int offset, int bits)
  8. {
  9. if (offset >= 64)
  10. {
  11. High |= value << (offset - 64);
  12. }
  13. else
  14. {
  15. Low |= value << offset;
  16. if (offset + bits > 64)
  17. {
  18. int remainder = 64 - offset;
  19. High |= value >> remainder;
  20. }
  21. }
  22. offset += bits;
  23. }
  24. public ulong Decode(ref int offset, int bits)
  25. {
  26. ulong value;
  27. ulong mask = bits == 64 ? ulong.MaxValue : (1UL << bits) - 1;
  28. if (offset >= 64)
  29. {
  30. value = (High >> (offset - 64)) & mask;
  31. }
  32. else
  33. {
  34. value = Low >> offset;
  35. if (offset + bits > 64)
  36. {
  37. int remainder = 64 - offset;
  38. value |= High << remainder;
  39. }
  40. value &= mask;
  41. }
  42. offset += bits;
  43. return value;
  44. }
  45. }
  46. }