OffsetCalculator.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Ryujinx.Common;
  2. using System.Runtime.CompilerServices;
  3. using static Ryujinx.Graphics.Texture.BlockLinearConstants;
  4. namespace Ryujinx.Graphics.Texture
  5. {
  6. public class OffsetCalculator
  7. {
  8. private int _width;
  9. private int _height;
  10. private int _stride;
  11. private bool _isLinear;
  12. private int _bytesPerPixel;
  13. private BlockLinearLayout _layoutConverter;
  14. // Variables for built in iteration.
  15. private int _yPart;
  16. public OffsetCalculator(
  17. int width,
  18. int height,
  19. int stride,
  20. bool isLinear,
  21. int gobBlocksInY,
  22. int bytesPerPixel)
  23. {
  24. _width = width;
  25. _height = height;
  26. _stride = stride;
  27. _isLinear = isLinear;
  28. _bytesPerPixel = bytesPerPixel;
  29. int wAlignment = GobStride / bytesPerPixel;
  30. int wAligned = BitUtils.AlignUp(width, wAlignment);
  31. if (!isLinear)
  32. {
  33. _layoutConverter = new BlockLinearLayout(
  34. wAligned,
  35. height,
  36. 1,
  37. gobBlocksInY,
  38. 1,
  39. bytesPerPixel);
  40. }
  41. }
  42. public void SetY(int y)
  43. {
  44. if (_isLinear)
  45. {
  46. _yPart = y * _stride;
  47. }
  48. else
  49. {
  50. _layoutConverter.SetY(y);
  51. }
  52. }
  53. public int GetOffset(int x, int y)
  54. {
  55. if (_isLinear)
  56. {
  57. return x * _bytesPerPixel + y * _stride;
  58. }
  59. else
  60. {
  61. return _layoutConverter.GetOffset(x, y, 0);
  62. }
  63. }
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public int GetOffset(int x)
  66. {
  67. if (_isLinear)
  68. {
  69. return x * _bytesPerPixel + _yPart;
  70. }
  71. else
  72. {
  73. return _layoutConverter.GetOffset(x);
  74. }
  75. }
  76. public (int offset, int size) GetRectangleRange(int x, int y, int width, int height)
  77. {
  78. if (_isLinear)
  79. {
  80. int start = y * _stride + x * _bytesPerPixel;
  81. int end = (y + height - 1) * _stride + (x + width) * _bytesPerPixel;
  82. return (start, end - start);
  83. }
  84. else
  85. {
  86. return _layoutConverter.GetRectangleRange(x, y, width, height);
  87. }
  88. }
  89. public bool LayoutMatches(OffsetCalculator other)
  90. {
  91. if (_isLinear)
  92. {
  93. return other._isLinear &&
  94. _width == other._width &&
  95. _height == other._height &&
  96. _stride == other._stride &&
  97. _bytesPerPixel == other._bytesPerPixel;
  98. }
  99. else
  100. {
  101. return !other._isLinear && _layoutConverter.LayoutMatches(other._layoutConverter);
  102. }
  103. }
  104. }
  105. }