using Ryujinx.Common; using Ryujinx.Graphics.Device; using Ryujinx.Graphics.GAL; using Ryujinx.Graphics.Gpu.Engine.Types; using Ryujinx.Graphics.Gpu.Image; using Ryujinx.Graphics.Texture; using Ryujinx.Memory; using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; namespace Ryujinx.Graphics.Gpu.Engine.Twod { /// /// Represents a 2D engine class. /// class TwodClass : IDeviceState { private readonly GpuChannel _channel; private readonly DeviceState _state; /// /// Creates a new instance of the 2D engine class. /// /// The channel that will make use of the engine public TwodClass(GpuChannel channel) { _channel = channel; _state = new DeviceState(new Dictionary { { nameof(TwodClassState.PixelsFromMemorySrcY0Int), new RwCallback(PixelsFromMemorySrcY0Int, 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); /// /// Determines if data is compatible between the source and destination texture. /// The two textures must have the same size, layout, and bytes per pixel. /// /// Info for the first texture /// Info for the second texture /// Format of the first texture /// Format of the second texture /// True if the data is compatible, false otherwise private bool IsDataCompatible(TwodTexture lhs, TwodTexture rhs, FormatInfo lhsFormat, FormatInfo rhsFormat) { if (lhsFormat.BytesPerPixel != rhsFormat.BytesPerPixel || lhs.Height != rhs.Height || lhs.Depth != rhs.Depth || lhs.LinearLayout != rhs.LinearLayout || lhs.MemoryLayout.Packed != rhs.MemoryLayout.Packed) { return false; } if (lhs.LinearLayout) { return lhs.Stride == rhs.Stride; } else { return lhs.Width == rhs.Width; } } /// /// Determine if the given region covers the full texture, also considering width alignment. /// /// The texture to check /// /// Region start x /// Region start y /// Region end x /// Region end y /// True if the region covers the full texture, false otherwise private bool IsCopyRegionComplete(TwodTexture texture, FormatInfo formatInfo, int x1, int y1, int x2, int y2) { if (x1 != 0 || y1 != 0 || y2 != texture.Height) { return false; } int width; int widthAlignment; if (texture.LinearLayout) { widthAlignment = 1; width = texture.Stride / formatInfo.BytesPerPixel; } else { widthAlignment = Constants.GobAlignment / formatInfo.BytesPerPixel; width = texture.Width; } return width == BitUtils.AlignUp(x2, widthAlignment); } /// /// Performs a full data copy between two textures, reading and writing guest memory directly. /// The textures must have a matching layout, size, and bytes per pixel. /// /// The source texture /// The destination texture /// Copy width /// Copy height /// Bytes per pixel private void UnscaledFullCopy(TwodTexture src, TwodTexture dst, int w, int h, int bpp) { var srcCalculator = new OffsetCalculator( w, h, src.Stride, src.LinearLayout, src.MemoryLayout.UnpackGobBlocksInY(), src.MemoryLayout.UnpackGobBlocksInZ(), bpp); (int _, int srcSize) = srcCalculator.GetRectangleRange(0, 0, w, h); var memoryManager = _channel.MemoryManager; ulong srcGpuVa = src.Address.Pack(); ulong dstGpuVa = dst.Address.Pack(); ReadOnlySpan srcSpan = memoryManager.GetSpan(srcGpuVa, srcSize, true); int width; int height = src.Height; if (src.LinearLayout) { width = src.Stride / bpp; } else { width = src.Width; } // If the copy is not equal to the width and height of the texture, we will need to copy partially. // It's worth noting that it has already been established that the src and dst are the same size. if (w == width && h == height) { memoryManager.Write(dstGpuVa, srcSpan); } else { using WritableRegion dstRegion = memoryManager.GetWritableRegion(dstGpuVa, srcSize, true); Span dstSpan = dstRegion.Memory.Span; if (src.LinearLayout) { int stride = src.Stride; int offset = 0; int lineSize = width * bpp; for (int y = 0; y < height; y++) { srcSpan.Slice(offset, lineSize).CopyTo(dstSpan.Slice(offset)); offset += stride; } } else { // Copy with the block linear layout in mind. // Recreate the offset calculate with bpp 1 for copy. int stride = w * bpp; srcCalculator = new OffsetCalculator( stride, h, 0, false, src.MemoryLayout.UnpackGobBlocksInY(), src.MemoryLayout.UnpackGobBlocksInZ(), 1); int strideTrunc = BitUtils.AlignDown(stride, 16); ReadOnlySpan> srcVec = MemoryMarshal.Cast>(srcSpan); Span> dstVec = MemoryMarshal.Cast>(dstSpan); for (int y = 0; y < h; y++) { int x = 0; srcCalculator.SetY(y); for (; x < strideTrunc; x += 16) { int offset = srcCalculator.GetOffset(x) >> 4; dstVec[offset] = srcVec[offset]; } for (; x < stride; x++) { int offset = srcCalculator.GetOffset(x); dstSpan[offset] = srcSpan[offset]; } } } } } /// /// Performs the blit operation, triggered by the register write. /// /// Method call argument private void PixelsFromMemorySrcY0Int(int argument) { var memoryManager = _channel.MemoryManager; var dstCopyTexture = Unsafe.As(ref _state.State.SetDstFormat); var srcCopyTexture = Unsafe.As(ref _state.State.SetSrcFormat); long srcX = ((long)_state.State.SetPixelsFromMemorySrcX0Int << 32) | (long)(ulong)_state.State.SetPixelsFromMemorySrcX0Frac; long srcY = ((long)_state.State.PixelsFromMemorySrcY0Int << 32) | (long)(ulong)_state.State.SetPixelsFromMemorySrcY0Frac; long duDx = ((long)_state.State.SetPixelsFromMemoryDuDxInt << 32) | (long)(ulong)_state.State.SetPixelsFromMemoryDuDxFrac; long dvDy = ((long)_state.State.SetPixelsFromMemoryDvDyInt << 32) | (long)(ulong)_state.State.SetPixelsFromMemoryDvDyFrac; bool originCorner = _state.State.SetPixelsFromMemorySampleModeOrigin == SetPixelsFromMemorySampleModeOrigin.Corner; if (originCorner) { // If the origin is corner, it is assumed that the guest API // is manually centering the origin by adding a offset to the // source region X/Y coordinates. // Here we attempt to remove such offset to ensure we have the correct region. // The offset is calculated as FactorXY / 2.0, where FactorXY = SrcXY / DstXY, // so we do the same here by dividing the fixed point value by 2, while // throwing away the fractional part to avoid rounding errors. srcX -= (duDx >> 33) << 32; srcY -= (dvDy >> 33) << 32; } int srcX1 = (int)(srcX >> 32); int srcY1 = (int)(srcY >> 32); int srcX2 = srcX1 + (int)((duDx * _state.State.SetPixelsFromMemoryDstWidth + uint.MaxValue) >> 32); int srcY2 = srcY1 + (int)((dvDy * _state.State.SetPixelsFromMemoryDstHeight + uint.MaxValue) >> 32); int dstX1 = (int)_state.State.SetPixelsFromMemoryDstX0; int dstY1 = (int)_state.State.SetPixelsFromMemoryDstY0; int dstX2 = dstX1 + (int)_state.State.SetPixelsFromMemoryDstWidth; int dstY2 = dstY1 + (int)_state.State.SetPixelsFromMemoryDstHeight; // The source and destination textures should at least be as big as the region being requested. // The hints will only resize within alignment constraints, so out of bound copies won't resize in most cases. var srcHint = new Size(srcX2, srcY2, 1); var dstHint = new Size(dstX2, dstY2, 1); var srcCopyTextureFormat = srcCopyTexture.Format.Convert(); int srcWidthAligned = srcCopyTexture.Stride / srcCopyTextureFormat.BytesPerPixel; ulong offset = 0; // For an out of bounds copy, we must ensure that the copy wraps to the next line, // so for a copy from a 64x64 texture, in the region [32, 96[, there are 32 pixels that are // outside the bounds of the texture. We fill the destination with the first 32 pixels // of the next line on the source texture. // This can be done by simply adding an offset to the texture address, so that the initial // gap is skipped and the copy is inside bounds again. // This is required by the proprietary guest OpenGL driver. if (srcCopyTexture.LinearLayout && srcCopyTexture.Width == srcX2 && srcX2 > srcWidthAligned && srcX1 > 0) { offset = (ulong)(srcX1 * srcCopyTextureFormat.BytesPerPixel); srcCopyTexture.Width -= srcX1; srcX2 -= srcX1; srcX1 = 0; } FormatInfo dstCopyTextureFormat = dstCopyTexture.Format.Convert(); bool canDirectCopy = GraphicsConfig.Fast2DCopy && srcX2 == dstX2 && srcY2 == dstY2 && IsDataCompatible(srcCopyTexture, dstCopyTexture, srcCopyTextureFormat, dstCopyTextureFormat) && IsCopyRegionComplete(srcCopyTexture, srcCopyTextureFormat, srcX1, srcY1, srcX2, srcY2) && IsCopyRegionComplete(dstCopyTexture, dstCopyTextureFormat, dstX1, dstY1, dstX2, dstY2); var srcTexture = memoryManager.Physical.TextureCache.FindOrCreateTexture( memoryManager, srcCopyTexture, offset, srcCopyTextureFormat, !canDirectCopy, false, srcHint); if (srcTexture == null) { if (canDirectCopy) { // Directly copy the data on CPU. UnscaledFullCopy(srcCopyTexture, dstCopyTexture, srcX2, srcY2, srcCopyTextureFormat.BytesPerPixel); } return; } memoryManager.Physical.TextureCache.Lift(srcTexture); // When the source texture that was found has a depth format, // we must enforce the target texture also has a depth format, // as copies between depth and color formats are not allowed. if (srcTexture.Format.IsDepthOrStencil()) { dstCopyTextureFormat = srcTexture.Info.FormatInfo; } else { dstCopyTextureFormat = dstCopyTexture.Format.Convert(); } var dstTexture = memoryManager.Physical.TextureCache.FindOrCreateTexture( memoryManager, dstCopyTexture, 0, dstCopyTextureFormat, true, srcTexture.ScaleMode == TextureScaleMode.Scaled, dstHint); if (dstTexture == null) { return; } if (srcTexture.Info.Samples > 1 || dstTexture.Info.Samples > 1) { srcTexture.PropagateScale(dstTexture); } float scale = srcTexture.ScaleFactor; float dstScale = dstTexture.ScaleFactor; Extents2D srcRegion = new Extents2D( (int)Math.Ceiling(scale * (srcX1 / srcTexture.Info.SamplesInX)), (int)Math.Ceiling(scale * (srcY1 / srcTexture.Info.SamplesInY)), (int)Math.Ceiling(scale * (srcX2 / srcTexture.Info.SamplesInX)), (int)Math.Ceiling(scale * (srcY2 / srcTexture.Info.SamplesInY))); Extents2D dstRegion = new Extents2D( (int)Math.Ceiling(dstScale * (dstX1 / dstTexture.Info.SamplesInX)), (int)Math.Ceiling(dstScale * (dstY1 / dstTexture.Info.SamplesInY)), (int)Math.Ceiling(dstScale * (dstX2 / dstTexture.Info.SamplesInX)), (int)Math.Ceiling(dstScale * (dstY2 / dstTexture.Info.SamplesInY))); bool linearFilter = _state.State.SetPixelsFromMemorySampleModeFilter == SetPixelsFromMemorySampleModeFilter.Bilinear; srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter); dstTexture.SignalModified(); } } }