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.
This commit is contained in:
riperiperi
2020-07-07 03:41:07 +01:00
committed by GitHub
parent 43b78ae157
commit 484eb645ae
49 changed files with 1163 additions and 131 deletions

View File

@ -1,3 +1,5 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.StructuredIr;
using Ryujinx.Graphics.Shader.Translation;
using System.Collections.Generic;
using System.Text;
@ -75,6 +77,21 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
AppendLine("}" + suffix);
}
public int FindTextureDescriptorIndex(AstTextureOperation texOp)
{
AstOperand operand = texOp.GetSource(0) as AstOperand;
bool bindless = (texOp.Flags & TextureFlags.Bindless) > 0;
int cBufSlot = bindless ? operand.CbufSlot : 0;
int cBufOffset = bindless ? operand.CbufOffset : 0;
return TextureDescriptors.FindIndex(descriptor =>
descriptor.Type == texOp.Type &&
descriptor.HandleIndex == texOp.Handle &&
descriptor.CbufSlot == cBufSlot &&
descriptor.CbufOffset == cBufOffset);
}
private void UpdateIndentation()
{
_indentation = GetIndentation(_level);

View File

@ -137,6 +137,14 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
context.AppendLine();
}
if (context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute)
{
if (DeclareRenderScale(context))
{
context.AppendLine();
}
}
if ((info.HelperFunctionsMask & HelperFunctionsMask.MultiplyHighS32) != 0)
{
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/MultiplyHighS32.glsl");
@ -219,6 +227,33 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
}
}
private static bool DeclareRenderScale(CodeGenContext context)
{
if ((context.Config.UsedFeatures & (FeatureFlags.FragCoordXY | FeatureFlags.IntegerSampling)) != 0)
{
string stage = OperandManager.GetShaderStagePrefix(context.Config.Stage);
int scaleElements = context.TextureDescriptors.Count;
if (context.Config.Stage == ShaderStage.Fragment)
{
scaleElements++; // Also includes render target scale, for gl_FragCoord.
}
context.AppendLine($"uniform float {stage}_renderScale[{scaleElements}];");
if (context.Config.UsedFeatures.HasFlag(FeatureFlags.IntegerSampling))
{
context.AppendLine();
AppendHelperFunction(context, $"Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/TexelFetchScale_{stage}.glsl");
}
return true;
}
return false;
}
private static void DeclareStorages(CodeGenContext context, StructuredProgramInfo info)
{
string sbName = OperandManager.GetShaderStagePrefix(context.Config.Stage);

View File

@ -0,0 +1,7 @@
ivec2 Helper_TexelFetchScale(ivec2 inputVec, int samplerIndex) {
float scale = cp_renderScale[samplerIndex];
if (scale == 1.0) {
return inputVec;
}
return ivec2(vec2(inputVec) * scale);
}

View File

@ -0,0 +1,11 @@
ivec2 Helper_TexelFetchScale(ivec2 inputVec, int samplerIndex) {
float scale = fp_renderScale[1 + samplerIndex];
if (scale == 1.0) {
return inputVec;
}
if (scale < 0.0) { // If less than 0, try interpolate between texels by using the screen position.
return ivec2(vec2(inputVec) * (-scale) + mod(gl_FragCoord.xy, -scale));
} else {
return ivec2(vec2(inputVec) * scale);
}
}

View File

@ -390,7 +390,34 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
}
}
Append(AssemblePVector(pCount));
string ApplyScaling(string vector)
{
if (intCoords)
{
int index = context.FindTextureDescriptorIndex(texOp);
if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) &&
(texOp.Flags & TextureFlags.Bindless) == 0 &&
texOp.Type != SamplerType.Indexed &&
pCount == 2)
{
return "Helper_TexelFetchScale(" + vector + ", " + index + ")";
}
else
{
// Resolution scaling cannot be applied to this texture right now.
// Flag so that we know to blacklist scaling on related textures when binding them.
TextureDescriptor descriptor = context.TextureDescriptors[index];
descriptor.Flags |= TextureUsageFlags.ResScaleUnsupported;
context.TextureDescriptors[index] = descriptor;
}
}
return vector;
}
Append(ApplyScaling(AssemblePVector(pCount)));
string AssembleDerivativesVector(int count)
{

View File

@ -185,8 +185,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
{
switch (value & ~3)
{
case AttributeConsts.PositionX: return "gl_FragCoord.x";
case AttributeConsts.PositionY: return "gl_FragCoord.y";
case AttributeConsts.PositionX: return "(gl_FragCoord.x / fp_renderScale[0])";
case AttributeConsts.PositionY: return "(gl_FragCoord.y / fp_renderScale[0])";
case AttributeConsts.PositionZ: return "gl_FragCoord.z";
case AttributeConsts.PositionW: return "gl_FragCoord.w";
}