Inline2Memory.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.Gpu.State;
  3. using Ryujinx.Graphics.Texture;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. namespace Ryujinx.Graphics.Gpu.Engine
  7. {
  8. partial class Methods
  9. {
  10. private Inline2MemoryParams _params;
  11. private bool _isLinear;
  12. private int _offset;
  13. private int _size;
  14. private bool _finished;
  15. private int[] _buffer;
  16. /// <summary>
  17. /// Launches Inline-to-Memory engine DMA copy.
  18. /// </summary>
  19. /// <param name="state">Current GPU state</param>
  20. /// <param name="argument">Method call argument</param>
  21. public void LaunchDma(GpuState state, int argument)
  22. {
  23. _params = state.Get<Inline2MemoryParams>(MethodOffset.I2mParams);
  24. _isLinear = (argument & 1) != 0;
  25. _offset = 0;
  26. _size = _params.LineLengthIn * _params.LineCount;
  27. int count = BitUtils.DivRoundUp(_size, 4);
  28. if (_buffer == null || _buffer.Length < count)
  29. {
  30. _buffer = new int[count];
  31. }
  32. ulong dstBaseAddress = state.Channel.MemoryManager.Translate(_params.DstAddress.Pack());
  33. // Trigger read tracking, to flush any managed resources in the destination region.
  34. state.Channel.MemoryManager.Physical.GetSpan(dstBaseAddress, _size, true);
  35. _finished = false;
  36. }
  37. /// <summary>
  38. /// Pushes a word of data to the Inline-to-Memory engine.
  39. /// </summary>
  40. /// <param name="state">Current GPU state</param>
  41. /// <param name="argument">Method call argument</param>
  42. public void LoadInlineData(GpuState state, int argument)
  43. {
  44. if (!_finished)
  45. {
  46. _buffer[_offset++] = argument;
  47. if (_offset * 4 >= _size)
  48. {
  49. FinishTransfer(state);
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// Performs actual copy of the inline data after the transfer is finished.
  55. /// </summary>
  56. /// <param name="state">Current GPU state</param>
  57. private void FinishTransfer(GpuState state)
  58. {
  59. Span<byte> data = MemoryMarshal.Cast<int, byte>(_buffer).Slice(0, _size);
  60. if (_isLinear && _params.LineCount == 1)
  61. {
  62. ulong address = state.Channel.MemoryManager.Translate(_params.DstAddress.Pack());
  63. state.Channel.MemoryManager.Physical.Write(address, data);
  64. }
  65. else
  66. {
  67. var dstCalculator = new OffsetCalculator(
  68. _params.DstWidth,
  69. _params.DstHeight,
  70. _params.DstStride,
  71. _isLinear,
  72. _params.DstMemoryLayout.UnpackGobBlocksInY(),
  73. 1);
  74. int srcOffset = 0;
  75. ulong dstBaseAddress = state.Channel.MemoryManager.Translate(_params.DstAddress.Pack());
  76. for (int y = _params.DstY; y < _params.DstY + _params.LineCount; y++)
  77. {
  78. int x1 = _params.DstX;
  79. int x2 = _params.DstX + _params.LineLengthIn;
  80. int x2Trunc = _params.DstX + BitUtils.AlignDown(_params.LineLengthIn, 16);
  81. int x;
  82. for (x = x1; x < x2Trunc; x += 16, srcOffset += 16)
  83. {
  84. int dstOffset = dstCalculator.GetOffset(x, y);
  85. ulong dstAddress = dstBaseAddress + (ulong)dstOffset;
  86. Span<byte> pixel = data.Slice(srcOffset, 16);
  87. state.Channel.MemoryManager.Physical.Write(dstAddress, pixel);
  88. }
  89. for (; x < x2; x++, srcOffset++)
  90. {
  91. int dstOffset = dstCalculator.GetOffset(x, y);
  92. ulong dstAddress = dstBaseAddress + (ulong)dstOffset;
  93. Span<byte> pixel = data.Slice(srcOffset, 1);
  94. state.Channel.MemoryManager.Physical.Write(dstAddress, pixel);
  95. }
  96. }
  97. }
  98. _finished = true;
  99. _context.AdvanceSequence();
  100. }
  101. }
  102. }