using Ryujinx.Common; using Ryujinx.Graphics.Device; using Ryujinx.Graphics.Gpu.Engine.Threed; using Ryujinx.Graphics.Texture; using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime.Intrinsics; namespace Ryujinx.Graphics.Gpu.Engine.Dma { /// /// Represents a DMA copy engine class. /// class DmaClass : IDeviceState { private readonly GpuContext _context; private readonly GpuChannel _channel; private readonly ThreedClass _3dEngine; private readonly DeviceState _state; /// /// Copy flags passed on DMA launch. /// [Flags] private enum CopyFlags { SrcLinear = 1 << 7, DstLinear = 1 << 8, MultiLineEnable = 1 << 9, RemapEnable = 1 << 10 } /// /// Creates a new instance of the DMA copy engine class. /// /// GPU context /// GPU channel /// 3D engine public DmaClass(GpuContext context, GpuChannel channel, ThreedClass threedEngine) { _context = context; _channel = channel; _3dEngine = threedEngine; _state = new DeviceState(new Dictionary { { nameof(DmaClassState.LaunchDma), new RwCallback(LaunchDma, null) } }); } /// /// Reads data from the class registers. /// /// Register byte offset /// Data at the specified offset public int Read(int offset) => _state.Read(offset); /// /// Writes data to the class registers. /// /// Register byte offset /// Data to be written public void Write(int offset, int data) => _state.Write(offset, data); /// /// Determine if a buffer-to-texture region covers the entirety of a texture. /// /// Texture to compare /// True if the texture is linear, false if block linear /// Texture bytes per pixel /// Texture stride /// Number of pixels to be copied /// Number of lines to be copied /// private static bool IsTextureCopyComplete(DmaTexture tex, bool linear, int bpp, int stride, int xCount, int yCount) { if (linear) { int alignWidth = Constants.StrideAlignment / bpp; return tex.RegionX == 0 && tex.RegionY == 0 && stride / bpp == BitUtils.AlignUp(xCount, alignWidth); } else { int alignWidth = Constants.GobAlignment / bpp; return tex.RegionX == 0 && tex.RegionY == 0 && tex.Width == BitUtils.AlignUp(xCount, alignWidth) && tex.Height == yCount; } } /// /// Performs a buffer to buffer, or buffer to texture copy. /// /// Method call argument private void LaunchDma(int argument) { var memoryManager = _channel.MemoryManager; CopyFlags copyFlags = (CopyFlags)argument; bool srcLinear = copyFlags.HasFlag(CopyFlags.SrcLinear); bool dstLinear = copyFlags.HasFlag(CopyFlags.DstLinear); bool copy2D = copyFlags.HasFlag(CopyFlags.MultiLineEnable); bool remap = copyFlags.HasFlag(CopyFlags.RemapEnable); uint size = _state.State.LineLengthIn; if (size == 0) { return; } ulong srcGpuVa = ((ulong)_state.State.OffsetInUpperUpper << 32) | _state.State.OffsetInLower; ulong dstGpuVa = ((ulong)_state.State.OffsetOutUpperUpper << 32) | _state.State.OffsetOutLower; int xCount = (int)_state.State.LineLengthIn; int yCount = (int)_state.State.LineCount; _3dEngine.FlushUboDirty(); if (copy2D) { // Buffer to texture copy. int componentSize = (int)_state.State.SetRemapComponentsComponentSize + 1; int srcBpp = remap ? ((int)_state.State.SetRemapComponentsNumSrcComponents + 1) * componentSize : 1; int dstBpp = remap ? ((int)_state.State.SetRemapComponentsNumDstComponents + 1) * componentSize : 1; var dst = Unsafe.As(ref _state.State.SetDstBlockSize); var src = Unsafe.As(ref _state.State.SetSrcBlockSize); int srcStride = (int)_state.State.PitchIn; int dstStride = (int)_state.State.PitchOut; var srcCalculator = new OffsetCalculator( src.Width, src.Height, srcStride, srcLinear, src.MemoryLayout.UnpackGobBlocksInY(), src.MemoryLayout.UnpackGobBlocksInZ(), srcBpp); var dstCalculator = new OffsetCalculator( dst.Width, dst.Height, dstStride, dstLinear, dst.MemoryLayout.UnpackGobBlocksInY(), dst.MemoryLayout.UnpackGobBlocksInZ(), dstBpp); (int srcBaseOffset, int srcSize) = srcCalculator.GetRectangleRange(src.RegionX, src.RegionY, xCount, yCount); (int dstBaseOffset, int dstSize) = dstCalculator.GetRectangleRange(dst.RegionX, dst.RegionY, xCount, yCount); ReadOnlySpan srcSpan = memoryManager.GetSpan(srcGpuVa + (uint)srcBaseOffset, srcSize, true); Span dstSpan = memoryManager.GetSpan(dstGpuVa + (uint)dstBaseOffset, dstSize).ToArray(); bool completeSource = IsTextureCopyComplete(src, srcLinear, srcBpp, srcStride, xCount, yCount); bool completeDest = IsTextureCopyComplete(dst, dstLinear, dstBpp, dstStride, xCount, yCount); if (completeSource && completeDest) { var target = memoryManager.Physical.TextureCache.FindTexture( memoryManager, dst, dstGpuVa, dstBpp, dstStride, xCount, yCount, dstLinear); if (target != null) { ReadOnlySpan data; if (srcLinear) { data = LayoutConverter.ConvertLinearStridedToLinear( target.Info.Width, target.Info.Height, 1, 1, srcStride, target.Info.FormatInfo.BytesPerPixel, srcSpan); } else { data = LayoutConverter.ConvertBlockLinearToLinear( src.Width, src.Height, 1, target.Info.Levels, 1, 1, 1, srcBpp, src.MemoryLayout.UnpackGobBlocksInY(), src.MemoryLayout.UnpackGobBlocksInZ(), 1, new SizeInfo((int)target.Size), srcSpan); } target.SetData(data); target.SignalModified(); return; } else if (srcCalculator.LayoutMatches(dstCalculator)) { srcSpan.CopyTo(dstSpan); // No layout conversion has to be performed, just copy the data entirely. memoryManager.Write(dstGpuVa + (uint)dstBaseOffset, dstSpan); return; } } unsafe bool Convert(Span dstSpan, ReadOnlySpan srcSpan) where T : unmanaged { fixed (byte* dstPtr = dstSpan, srcPtr = srcSpan) { byte* dstBase = dstPtr - dstBaseOffset; // Layout offset is relative to the base, so we need to subtract the span's offset. byte* srcBase = srcPtr - srcBaseOffset; for (int y = 0; y < yCount; y++) { srcCalculator.SetY(src.RegionY + y); dstCalculator.SetY(dst.RegionY + y); for (int x = 0; x < xCount; x++) { int srcOffset = srcCalculator.GetOffset(src.RegionX + x); int dstOffset = dstCalculator.GetOffset(dst.RegionX + x); *(T*)(dstBase + dstOffset) = *(T*)(srcBase + srcOffset); } } } return true; } bool _ = srcBpp switch { 1 => Convert(dstSpan, srcSpan), 2 => Convert(dstSpan, srcSpan), 4 => Convert(dstSpan, srcSpan), 8 => Convert(dstSpan, srcSpan), 12 => Convert(dstSpan, srcSpan), 16 => Convert>(dstSpan, srcSpan), _ => throw new NotSupportedException($"Unable to copy ${srcBpp} bpp pixel format.") }; memoryManager.Write(dstGpuVa + (uint)dstBaseOffset, dstSpan); } else { if (remap && _state.State.SetRemapComponentsDstX == SetRemapComponentsDst.ConstA && _state.State.SetRemapComponentsDstY == SetRemapComponentsDst.ConstA && _state.State.SetRemapComponentsDstZ == SetRemapComponentsDst.ConstA && _state.State.SetRemapComponentsDstW == SetRemapComponentsDst.ConstA && _state.State.SetRemapComponentsNumSrcComponents == SetRemapComponentsNumComponents.One && _state.State.SetRemapComponentsNumDstComponents == SetRemapComponentsNumComponents.One && _state.State.SetRemapComponentsComponentSize == SetRemapComponentsComponentSize.Four) { // Fast path for clears when remap is enabled. memoryManager.Physical.BufferCache.ClearBuffer(memoryManager, dstGpuVa, size * 4, _state.State.SetRemapConstA); } else { // TODO: Implement remap functionality. // Buffer to buffer copy. memoryManager.Physical.BufferCache.CopyBuffer(memoryManager, srcGpuVa, dstGpuVa, size); } } } } }