Inline2Memory.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 = _context.MemoryManager.Translate(_params.DstAddress.Pack());
  33. // Trigger read tracking, to flush any managed resources in the destination region.
  34. _context.PhysicalMemory.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();
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// Performs actual copy of the inline data after the transfer is finished.
  55. /// </summary>
  56. private void FinishTransfer()
  57. {
  58. Span<byte> data = MemoryMarshal.Cast<int, byte>(_buffer).Slice(0, _size);
  59. if (_isLinear && _params.LineCount == 1)
  60. {
  61. ulong address = _context.MemoryManager.Translate(_params.DstAddress.Pack());
  62. _context.PhysicalMemory.Write(address, data);
  63. }
  64. else
  65. {
  66. var dstCalculator = new OffsetCalculator(
  67. _params.DstWidth,
  68. _params.DstHeight,
  69. _params.DstStride,
  70. _isLinear,
  71. _params.DstMemoryLayout.UnpackGobBlocksInY(),
  72. 1);
  73. int srcOffset = 0;
  74. ulong dstBaseAddress = _context.MemoryManager.Translate(_params.DstAddress.Pack());
  75. for (int y = _params.DstY; y < _params.DstY + _params.LineCount; y++)
  76. {
  77. int x1 = _params.DstX;
  78. int x2 = _params.DstX + _params.LineLengthIn;
  79. int x2Trunc = _params.DstX + BitUtils.AlignDown(_params.LineLengthIn, 16);
  80. int x;
  81. for (x = x1; x < x2Trunc; x += 16, srcOffset += 16)
  82. {
  83. int dstOffset = dstCalculator.GetOffset(x, y);
  84. ulong dstAddress = dstBaseAddress + (ulong)dstOffset;
  85. Span<byte> pixel = data.Slice(srcOffset, 16);
  86. _context.PhysicalMemory.Write(dstAddress, pixel);
  87. }
  88. for (; x < x2; x++, srcOffset++)
  89. {
  90. int dstOffset = dstCalculator.GetOffset(x, y);
  91. ulong dstAddress = dstBaseAddress + (ulong)dstOffset;
  92. Span<byte> pixel = data.Slice(srcOffset, 1);
  93. _context.PhysicalMemory.Write(dstAddress, pixel);
  94. }
  95. }
  96. }
  97. _finished = true;
  98. _context.AdvanceSequence();
  99. }
  100. }
  101. }