2019-10-13 08:02:07 +02:00
|
|
|
using Ryujinx.Graphics.GAL;
|
2020-09-01 02:06:27 +02:00
|
|
|
using Ryujinx.Graphics.Gpu.Image;
|
2019-10-13 08:02:07 +02:00
|
|
|
using Ryujinx.Graphics.Gpu.State;
|
2020-11-10 01:41:13 +01:00
|
|
|
using Ryujinx.Graphics.Texture;
|
2020-07-07 04:41:07 +02:00
|
|
|
using System;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine
|
|
|
|
{
|
2019-12-29 18:41:50 +01:00
|
|
|
using Texture = Image.Texture;
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
partial class Methods
|
|
|
|
{
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Performs a texture to texture copy.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void CopyTexture(GpuState state, int argument)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
var dstCopyTexture = state.Get<CopyTexture>(MethodOffset.CopyDstTexture);
|
|
|
|
var srcCopyTexture = state.Get<CopyTexture>(MethodOffset.CopySrcTexture);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-11-10 01:41:13 +01:00
|
|
|
var region = state.Get<CopyRegion>(MethodOffset.CopyRegion);
|
|
|
|
|
|
|
|
var control = state.Get<CopyTextureControl>(MethodOffset.CopyTextureControl);
|
|
|
|
|
2021-06-03 01:30:48 +02:00
|
|
|
bool originCorner = control.UnpackOriginCorner();
|
2020-11-10 01:41:13 +01:00
|
|
|
|
2021-06-03 01:30:48 +02:00
|
|
|
long srcX = region.SrcXF;
|
|
|
|
long srcY = region.SrcYF;
|
|
|
|
|
|
|
|
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 -= (region.SrcWidthRF >> 33) << 32;
|
|
|
|
srcY -= (region.SrcHeightRF >> 33) << 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
int srcX1 = (int)(srcX >> 32);
|
|
|
|
int srcY1 = (int)(srcY >> 32);
|
|
|
|
|
|
|
|
int srcX2 = srcX1 + (int)((region.SrcWidthRF * region.DstWidth + uint.MaxValue) >> 32);
|
|
|
|
int srcY2 = srcY1 + (int)((region.SrcHeightRF * region.DstHeight + uint.MaxValue) >> 32);
|
2020-11-10 01:41:13 +01:00
|
|
|
|
|
|
|
int dstX1 = region.DstX;
|
|
|
|
int dstY1 = region.DstY;
|
|
|
|
|
|
|
|
int dstX2 = region.DstX + region.DstWidth;
|
|
|
|
int dstY2 = region.DstY + region.DstHeight;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2020-11-05 23:50:34 +01:00
|
|
|
var srcCopyTextureFormat = srcCopyTexture.Format.Convert();
|
|
|
|
|
2021-05-22 01:26:49 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture, offset, srcCopyTextureFormat, true, srcHint);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
if (srcTexture == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2020-11-05 23:50:34 +01:00
|
|
|
FormatInfo dstCopyTextureFormat;
|
|
|
|
|
|
|
|
if (srcTexture.Format.IsDepthOrStencil())
|
|
|
|
{
|
2020-11-16 09:37:16 +01:00
|
|
|
dstCopyTextureFormat = srcTexture.Info.FormatInfo;
|
2020-11-05 23:50:34 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dstCopyTextureFormat = dstCopyTexture.Format.Convert();
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-05-22 01:26:49 +02:00
|
|
|
Texture dstTexture = TextureManager.FindOrCreateTexture(dstCopyTexture, 0, dstCopyTextureFormat, srcTexture.ScaleMode == TextureScaleMode.Scaled, dstHint);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
if (dstTexture == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-20 21:14:45 +01:00
|
|
|
float scale = srcTexture.ScaleFactor;
|
|
|
|
float dstScale = dstTexture.ScaleFactor;
|
2020-07-07 04:41:07 +02:00
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
Extents2D srcRegion = new Extents2D(
|
2020-07-07 04:41:07 +02:00
|
|
|
(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)));
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
Extents2D dstRegion = new Extents2D(
|
2020-11-20 21:14:45 +01:00
|
|
|
(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)));
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
bool linearFilter = control.UnpackLinearFilter();
|
|
|
|
|
|
|
|
srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
|
|
|
|
|
2020-02-06 22:49:26 +01:00
|
|
|
dstTexture.SignalModified();
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|