MethodCopyBuffer.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Ryujinx.Graphics.Gpu.State;
  2. using Ryujinx.Graphics.Texture;
  3. using System;
  4. namespace Ryujinx.Graphics.Gpu.Engine
  5. {
  6. partial class Methods
  7. {
  8. /// <summary>
  9. /// Performs a buffer to buffer, or buffer to texture copy.
  10. /// </summary>
  11. /// <param name="state">Current GPU state</param>
  12. /// <param name="argument">Method call argument</param>
  13. private void CopyBuffer(GpuState state, int argument)
  14. {
  15. var cbp = state.Get<CopyBufferParams>(MethodOffset.CopyBufferParams);
  16. var swizzle = state.Get<CopyBufferSwizzle>(MethodOffset.CopyBufferSwizzle);
  17. bool srcLinear = (argument & (1 << 7)) != 0;
  18. bool dstLinear = (argument & (1 << 8)) != 0;
  19. bool copy2D = (argument & (1 << 9)) != 0;
  20. int size = cbp.XCount;
  21. if (size == 0)
  22. {
  23. return;
  24. }
  25. if (copy2D)
  26. {
  27. // Buffer to texture copy.
  28. int srcBpp = swizzle.UnpackSrcComponentsCount() * swizzle.UnpackComponentSize();
  29. int dstBpp = swizzle.UnpackDstComponentsCount() * swizzle.UnpackComponentSize();
  30. var dst = state.Get<CopyBufferTexture>(MethodOffset.CopyBufferDstTexture);
  31. var src = state.Get<CopyBufferTexture>(MethodOffset.CopyBufferSrcTexture);
  32. var srcCalculator = new OffsetCalculator(
  33. src.Width,
  34. src.Height,
  35. cbp.SrcStride,
  36. srcLinear,
  37. src.MemoryLayout.UnpackGobBlocksInY(),
  38. srcBpp);
  39. var dstCalculator = new OffsetCalculator(
  40. dst.Width,
  41. dst.Height,
  42. cbp.DstStride,
  43. dstLinear,
  44. dst.MemoryLayout.UnpackGobBlocksInY(),
  45. dstBpp);
  46. ulong srcBaseAddress = _context.MemoryManager.Translate(cbp.SrcAddress.Pack());
  47. ulong dstBaseAddress = _context.MemoryManager.Translate(cbp.DstAddress.Pack());
  48. for (int y = 0; y < cbp.YCount; y++)
  49. for (int x = 0; x < cbp.XCount; x++)
  50. {
  51. int srcOffset = srcCalculator.GetOffset(src.RegionX + x, src.RegionY + y);
  52. int dstOffset = dstCalculator.GetOffset(dst.RegionX + x, dst.RegionY + y);
  53. ulong srcAddress = srcBaseAddress + (ulong)srcOffset;
  54. ulong dstAddress = dstBaseAddress + (ulong)dstOffset;
  55. Span<byte> pixel = _context.PhysicalMemory.Read(srcAddress, (ulong)srcBpp);
  56. _context.PhysicalMemory.Write(dstAddress, pixel);
  57. }
  58. }
  59. else
  60. {
  61. // Buffer to buffer copy.
  62. BufferManager.CopyBuffer(cbp.SrcAddress, cbp.DstAddress, (uint)size);
  63. }
  64. }
  65. }
  66. }