Ryujinx/Ryujinx.Graphics.Gpu/Engine/MethodCopyTexture.cs
riperiperi 484eb645ae
Implement Zero-Configuration Resolution Scaling (#1365)
* Initial implementation of Render Target Scaling

Works with most games I have. No GUI option right now, it is hardcoded.

Missing handling for texelFetch operation.

* Realtime Configuration, refactoring.

* texelFetch scaling on fragment shader (WIP)

* Improve Shader-Side changes.

* Fix potential crash when no color/depth bound

* Workaround random uses of textures in compute.

This was blacklisting textures in a few games despite causing no bugs. Will eventually add full support so this doesn't break anything.

* Fix scales oscillating when changing between non-native scales.

* Scaled textures on compute, cleanup, lazier uniform update.

* Cleanup.

* Fix stupidity

* Address Thog Feedback.

* Cover most of GDK's feedback (two comments remain)

* Fix bad rename

* Move IsDepthStencil to FormatExtensions, add docs.

* Fix default config, square texture detection.

* Three final fixes:

- Nearest copy when texture is integer format.
- Texture2D -> Texture3D copy correctly blacklists the texture before trying an unscaled copy (caused driver error)
- Discount small textures.

* Remove scale threshold.

Not needed right now - we'll see if we run into problems.

* All CPU modification blacklists scale.

* Fix comment.
2020-07-07 04:41:07 +02:00

112 lines
No EOL
4.9 KiB
C#

using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Gpu.State;
using System;
namespace Ryujinx.Graphics.Gpu.Engine
{
using Texture = Image.Texture;
partial class Methods
{
/// <summary>
/// Performs a texture to texture copy.
/// </summary>
/// <param name="state">Current GPU state</param>
/// <param name="argument">Method call argument</param>
private void CopyTexture(GpuState state, int argument)
{
var dstCopyTexture = state.Get<CopyTexture>(MethodOffset.CopyDstTexture);
var srcCopyTexture = state.Get<CopyTexture>(MethodOffset.CopySrcTexture);
Texture srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture);
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.
if (srcTexture.Format == Format.D32Float)
{
dstCopyTexture.Format = RtFormat.D32Float;
}
Texture dstTexture = TextureManager.FindOrCreateTexture(dstCopyTexture, srcTexture.ScaleMode == Image.TextureScaleMode.Scaled);
if (dstTexture == null)
{
return;
}
if (srcTexture.ScaleFactor != dstTexture.ScaleFactor)
{
srcTexture.PropagateScale(dstTexture);
}
var control = state.Get<CopyTextureControl>(MethodOffset.CopyTextureControl);
var region = state.Get<CopyRegion>(MethodOffset.CopyRegion);
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;
float scale = srcTexture.ScaleFactor; // src and dest scales are identical now.
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(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)));
bool linearFilter = control.UnpackLinearFilter();
srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
// 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.
if (srcX2 / srcTexture.Info.SamplesInX > srcTexture.Info.Width)
{
srcCopyTexture.Height++;
srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture, srcTexture.ScaleMode == Image.TextureScaleMode.Scaled);
if (srcTexture.ScaleFactor != dstTexture.ScaleFactor)
{
srcTexture.PropagateScale(dstTexture);
}
srcRegion = new Extents2D(
(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)));
srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
}
dstTexture.SignalModified();
}
}
}