2019-10-13 02:02:07 -04:00
|
|
|
using Ryujinx.Graphics.GAL;
|
2020-08-31 20:06:27 -04:00
|
|
|
using Ryujinx.Graphics.Gpu.Image;
|
2019-10-13 02:02:07 -04:00
|
|
|
using Ryujinx.Graphics.Gpu.State;
|
2020-11-09 19:41:13 -05:00
|
|
|
using Ryujinx.Graphics.Texture;
|
2020-07-06 22:41:07 -04:00
|
|
|
using System;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine
|
|
|
|
{
|
2019-12-29 12:41:50 -05:00
|
|
|
using Texture = Image.Texture;
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
partial class Methods
|
|
|
|
{
|
2019-12-31 14:19:44 -05: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-21 21:46:14 -05:00
|
|
|
private void CopyTexture(GpuState state, int argument)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
var dstCopyTexture = state.Get<CopyTexture>(MethodOffset.CopyDstTexture);
|
|
|
|
var srcCopyTexture = state.Get<CopyTexture>(MethodOffset.CopySrcTexture);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-11-09 19:41:13 -05:00
|
|
|
var region = state.Get<CopyRegion>(MethodOffset.CopyRegion);
|
|
|
|
|
|
|
|
var control = state.Get<CopyTextureControl>(MethodOffset.CopyTextureControl);
|
|
|
|
|
|
|
|
int srcX1 = (int)(region.SrcXF >> 32);
|
|
|
|
int srcY1 = (int)(region.SrcYF >> 32);
|
|
|
|
|
|
|
|
int srcX2 = (int)((region.SrcXF + region.SrcWidthRF * region.DstWidth) >> 32);
|
|
|
|
int srcY2 = (int)((region.SrcYF + region.SrcHeightRF * region.DstHeight) >> 32);
|
|
|
|
|
|
|
|
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 17:50:34 -05:00
|
|
|
var srcCopyTextureFormat = srcCopyTexture.Format.Convert();
|
|
|
|
|
2020-11-09 19:41:13 -05:00
|
|
|
Texture srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture, srcCopyTextureFormat, true, srcHint);
|
2019-10-13 02:02:07 -04: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 17:50:34 -05:00
|
|
|
FormatInfo dstCopyTextureFormat;
|
|
|
|
|
|
|
|
if (srcTexture.Format.IsDepthOrStencil())
|
|
|
|
{
|
2020-11-16 03:37:16 -05:00
|
|
|
dstCopyTextureFormat = srcTexture.Info.FormatInfo;
|
2020-11-05 17:50:34 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dstCopyTextureFormat = dstCopyTexture.Format.Convert();
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-11-09 19:41:13 -05:00
|
|
|
Texture dstTexture = TextureManager.FindOrCreateTexture(dstCopyTexture, dstCopyTextureFormat, srcTexture.ScaleMode == TextureScaleMode.Scaled, dstHint);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
if (dstTexture == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:41:07 -04:00
|
|
|
if (srcTexture.ScaleFactor != dstTexture.ScaleFactor)
|
|
|
|
{
|
|
|
|
srcTexture.PropagateScale(dstTexture);
|
|
|
|
}
|
|
|
|
|
|
|
|
float scale = srcTexture.ScaleFactor; // src and dest scales are identical now.
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
Extents2D srcRegion = new Extents2D(
|
2020-07-06 22:41:07 -04: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 02:02:07 -04:00
|
|
|
|
|
|
|
Extents2D dstRegion = new Extents2D(
|
2020-07-06 22:41:07 -04:00
|
|
|
(int)Math.Ceiling(scale * (dstX1 / dstTexture.Info.SamplesInX)),
|
|
|
|
(int)Math.Ceiling(scale * (dstY1 / dstTexture.Info.SamplesInY)),
|
|
|
|
(int)Math.Ceiling(scale * (dstX2 / dstTexture.Info.SamplesInX)),
|
|
|
|
(int)Math.Ceiling(scale * (dstY2 / dstTexture.Info.SamplesInY)));
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
bool linearFilter = control.UnpackLinearFilter();
|
|
|
|
|
|
|
|
srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
|
|
|
|
|
2019-12-05 15:34:47 -05:00
|
|
|
// 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 emulated with 2 copies (the first copy handles the region inside the bounds,
|
|
|
|
// the second handles the region outside of the bounds).
|
|
|
|
// We must also extend the source texture by one line to ensure we can wrap on the last line.
|
|
|
|
// This is required by the (guest) OpenGL driver.
|
2020-07-06 22:41:07 -04:00
|
|
|
if (srcX2 / srcTexture.Info.SamplesInX > srcTexture.Info.Width)
|
2019-12-05 15:34:47 -05:00
|
|
|
{
|
|
|
|
srcCopyTexture.Height++;
|
|
|
|
|
2020-11-09 19:41:13 -05:00
|
|
|
srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture, srcCopyTextureFormat, srcTexture.ScaleMode == TextureScaleMode.Scaled, srcHint);
|
2020-07-06 22:41:07 -04:00
|
|
|
if (srcTexture.ScaleFactor != dstTexture.ScaleFactor)
|
|
|
|
{
|
|
|
|
srcTexture.PropagateScale(dstTexture);
|
|
|
|
}
|
2019-12-05 15:34:47 -05:00
|
|
|
|
|
|
|
srcRegion = new Extents2D(
|
2020-07-06 22:41:07 -04:00
|
|
|
(int)Math.Ceiling(scale * ((srcX1 / srcTexture.Info.SamplesInX) - srcTexture.Info.Width)),
|
|
|
|
(int)Math.Ceiling(scale * ((srcY1 / srcTexture.Info.SamplesInY) + 1)),
|
|
|
|
(int)Math.Ceiling(scale * ((srcX2 / srcTexture.Info.SamplesInX) - srcTexture.Info.Width)),
|
|
|
|
(int)Math.Ceiling(scale * ((srcY2 / srcTexture.Info.SamplesInY) + 1)));
|
2019-12-05 15:34:47 -05:00
|
|
|
|
|
|
|
srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
|
|
|
|
}
|
|
|
|
|
2020-02-06 16:49:26 -05:00
|
|
|
dstTexture.SignalModified();
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|