Inline2Memory.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. _context.Methods.TextureManager.Flush(dstBaseAddress, (ulong)_size);
  34. _finished = false;
  35. }
  36. /// <summary>
  37. /// Pushes a word of data to the Inline-to-Memory engine.
  38. /// </summary>
  39. /// <param name="state">Current GPU state</param>
  40. /// <param name="argument">Method call argument</param>
  41. public void LoadInlineData(GpuState state, int argument)
  42. {
  43. if (!_finished)
  44. {
  45. _buffer[_offset++] = argument;
  46. if (_offset * 4 >= _size)
  47. {
  48. FinishTransfer();
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// Performs actual copy of the inline data after the transfer is finished.
  54. /// </summary>
  55. private void FinishTransfer()
  56. {
  57. Span<byte> data = MemoryMarshal.Cast<int, byte>(_buffer).Slice(0, _size);
  58. if (_isLinear && _params.LineCount == 1)
  59. {
  60. ulong address = _context.MemoryManager.Translate( _params.DstAddress.Pack());
  61. _context.PhysicalMemory.Write(address, data);
  62. }
  63. else
  64. {
  65. var dstCalculator = new OffsetCalculator(
  66. _params.DstWidth,
  67. _params.DstHeight,
  68. _params.DstStride,
  69. _isLinear,
  70. _params.DstMemoryLayout.UnpackGobBlocksInY(),
  71. 1);
  72. int srcOffset = 0;
  73. ulong dstBaseAddress = _context.MemoryManager.Translate(_params.DstAddress.Pack());
  74. for (int y = _params.DstY; y < _params.DstY + _params.LineCount; y++)
  75. {
  76. int x1 = _params.DstX;
  77. int x2 = _params.DstX + _params.LineLengthIn;
  78. int x2Trunc = _params.DstX + BitUtils.AlignDown(_params.LineLengthIn, 16);
  79. int x;
  80. for (x = x1; x < x2Trunc; x += 16, srcOffset += 16)
  81. {
  82. int dstOffset = dstCalculator.GetOffset(x, y);
  83. ulong dstAddress = dstBaseAddress + (ulong)dstOffset;
  84. Span<byte> pixel = data.Slice(srcOffset, 16);
  85. _context.PhysicalMemory.Write(dstAddress, pixel);
  86. }
  87. for (; x < x2; x++, srcOffset++)
  88. {
  89. int dstOffset = dstCalculator.GetOffset(x, y);
  90. ulong dstAddress = dstBaseAddress + (ulong)dstOffset;
  91. Span<byte> pixel = data.Slice(srcOffset, 1);
  92. _context.PhysicalMemory.Write(dstAddress, pixel);
  93. }
  94. }
  95. }
  96. _finished = true;
  97. }
  98. }
  99. }