Ryujinx/Ryujinx.Graphics.OpenGL/Image/TextureCopyUnscaled.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

94 lines
3.2 KiB
C#

using OpenTK.Graphics.OpenGL;
using Ryujinx.Common;
using Ryujinx.Graphics.GAL;
using System;
namespace Ryujinx.Graphics.OpenGL.Image
{
static class TextureCopyUnscaled
{
public static void Copy(
TextureCreateInfo srcInfo,
TextureCreateInfo dstInfo,
int srcHandle,
int dstHandle,
int srcLayer,
int dstLayer,
int srcLevel,
int dstLevel,
float scaleFactor = 1f)
{
int srcWidth = (int)Math.Ceiling(srcInfo.Width * scaleFactor);
int srcHeight = (int)Math.Ceiling(srcInfo.Height * scaleFactor);
int srcDepth = srcInfo.GetDepthOrLayers();
int srcLevels = srcInfo.Levels;
int dstWidth = (int)Math.Ceiling(dstInfo.Width * scaleFactor);
int dstHeight = (int)Math.Ceiling(dstInfo.Height * scaleFactor);
int dstDepth = dstInfo.GetDepthOrLayers();
int dstLevels = dstInfo.Levels;
dstWidth = Math.Max(1, dstWidth >> dstLevel);
dstHeight = Math.Max(1, dstHeight >> dstLevel);
if (dstInfo.Target == Target.Texture3D)
{
dstDepth = Math.Max(1, dstDepth >> dstLevel);
}
// When copying from a compressed to a non-compressed format,
// the non-compressed texture will have the size of the texture
// in blocks (not in texels), so we must adjust that size to
// match the size in texels of the compressed texture.
if (!srcInfo.IsCompressed && dstInfo.IsCompressed)
{
dstWidth = BitUtils.DivRoundUp(dstWidth, dstInfo.BlockWidth);
dstHeight = BitUtils.DivRoundUp(dstHeight, dstInfo.BlockHeight);
}
else if (srcInfo.IsCompressed && !dstInfo.IsCompressed)
{
dstWidth *= dstInfo.BlockWidth;
dstHeight *= dstInfo.BlockHeight;
}
int width = Math.Min(srcWidth, dstWidth);
int height = Math.Min(srcHeight, dstHeight);
int depth = Math.Min(srcDepth, dstDepth);
int levels = Math.Min(srcLevels, dstLevels);
for (int level = 0; level < levels; level++)
{
// Stop copy if we are already out of the levels range.
if (level >= srcInfo.Levels || dstLevel + level >= dstInfo.Levels)
{
break;
}
GL.CopyImageSubData(
srcHandle,
srcInfo.Target.ConvertToImageTarget(),
srcLevel + level,
0,
0,
srcLayer,
dstHandle,
dstInfo.Target.ConvertToImageTarget(),
dstLevel + level,
0,
0,
dstLayer,
width,
height,
depth);
width = Math.Max(1, width >> 1);
height = Math.Max(1, height >> 1);
if (srcInfo.Target == Target.Texture3D)
{
depth = Math.Max(1, depth >> 1);
}
}
}
}
}