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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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";
}

View file

@ -32,6 +32,8 @@ namespace Ryujinx.Graphics.Shader.Instructions
Operand src = Attribute(op.AttributeOffset + index * 4);
context.FlagAttributeRead(src.Value);
context.Copy(Register(rd), context.LoadAttribute(src, primVertex));
}
}
@ -96,6 +98,8 @@ namespace Ryujinx.Graphics.Shader.Instructions
{
OpCodeIpa op = (OpCodeIpa)context.CurrOp;
context.FlagAttributeRead(op.AttributeOffset);
Operand res = Attribute(op.AttributeOffset);
if (op.AttributeOffset >= AttributeConsts.UserAttributeBase &&

View file

@ -283,11 +283,15 @@ namespace Ryujinx.Graphics.Shader.Instructions
public static void Tld(EmitterContext context)
{
context.UsedFeatures |= FeatureFlags.IntegerSampling;
EmitTextureSample(context, TextureFlags.IntCoords);
}
public static void TldB(EmitterContext context)
{
context.UsedFeatures |= FeatureFlags.IntegerSampling;
EmitTextureSample(context, TextureFlags.IntCoords | TextureFlags.Bindless);
}
@ -428,6 +432,8 @@ namespace Ryujinx.Graphics.Shader.Instructions
return;
}
context.UsedFeatures |= FeatureFlags.IntegerSampling;
flags = ConvertTextureFlags(tldsOp.Target) | TextureFlags.IntCoords;
if (tldsOp.Target == TexelLoadTarget.Texture1DLodZero && context.Config.GpuAccessor.QueryIsTextureBuffer(tldsOp.Immediate))

View file

@ -8,6 +8,8 @@
<EmbeddedResource Include="CodeGen\Glsl\HelperFunctions\ShuffleUp.glsl" />
<EmbeddedResource Include="CodeGen\Glsl\HelperFunctions\ShuffleXor.glsl" />
<EmbeddedResource Include="CodeGen\Glsl\HelperFunctions\SwizzleAdd.glsl" />
<EmbeddedResource Include="CodeGen\Glsl\HelperFunctions\TexelFetchScale_fp.glsl" />
<EmbeddedResource Include="CodeGen\Glsl\HelperFunctions\TexelFetchScale_cp.glsl" />
</ItemGroup>
<ItemGroup>

View file

@ -13,6 +13,8 @@ namespace Ryujinx.Graphics.Shader
public int CbufSlot { get; }
public int CbufOffset { get; }
public TextureUsageFlags Flags { get; set; }
public TextureDescriptor(string name, SamplerType type, int handleIndex)
{
Name = name;
@ -23,6 +25,8 @@ namespace Ryujinx.Graphics.Shader
CbufSlot = 0;
CbufOffset = 0;
Flags = TextureUsageFlags.None;
}
public TextureDescriptor(string name, SamplerType type, int cbufSlot, int cbufOffset)
@ -35,6 +39,8 @@ namespace Ryujinx.Graphics.Shader
CbufSlot = cbufSlot;
CbufOffset = cbufOffset;
Flags = TextureUsageFlags.None;
}
}
}

View file

@ -0,0 +1,16 @@
using System;
namespace Ryujinx.Graphics.Shader
{
/// <summary>
/// Flags that indicate how a texture will be used in a shader.
/// </summary>
[Flags]
public enum TextureUsageFlags
{
None = 0,
// Integer sampled textures must be noted for resolution scaling.
ResScaleUnsupported = 1 << 0
}
}

View file

@ -11,6 +11,8 @@ namespace Ryujinx.Graphics.Shader.Translation
public Block CurrBlock { get; set; }
public OpCode CurrOp { get; set; }
public FeatureFlags UsedFeatures { get; set; }
public ShaderConfig Config { get; }
private List<Operation> _operations;
@ -40,6 +42,20 @@ namespace Ryujinx.Graphics.Shader.Translation
_operations.Add(operation);
}
public void FlagAttributeRead(int attribute)
{
if (Config.Stage == ShaderStage.Fragment)
{
switch (attribute)
{
case AttributeConsts.PositionX:
case AttributeConsts.PositionY:
UsedFeatures |= FeatureFlags.FragCoordXY;
break;
}
}
}
public void MarkLabel(Operand label)
{
Add(Instruction.MarkLabel, label);

View file

@ -0,0 +1,18 @@
using System;
namespace Ryujinx.Graphics.Shader.Translation
{
/// <summary>
/// Features used by the shader that are important for the code generator to know in advance.
/// These typically change the declarations in the shader header.
/// </summary>
[Flags]
public enum FeatureFlags
{
None = 0,
// Affected by resolution scaling.
FragCoordXY = 1 << 1,
IntegerSampling = 1 << 0
}
}

View file

@ -22,6 +22,8 @@ namespace Ryujinx.Graphics.Shader.Translation
public TranslationFlags Flags { get; }
public FeatureFlags UsedFeatures { get; set; }
public ShaderConfig(IGpuAccessor gpuAccessor, TranslationFlags flags)
{
Stage = ShaderStage.Compute;
@ -34,6 +36,7 @@ namespace Ryujinx.Graphics.Shader.Translation
OmapDepth = false;
GpuAccessor = gpuAccessor;
Flags = flags;
UsedFeatures = FeatureFlags.None;
}
public ShaderConfig(ShaderHeader header, IGpuAccessor gpuAccessor, TranslationFlags flags)
@ -48,6 +51,7 @@ namespace Ryujinx.Graphics.Shader.Translation
OmapDepth = header.OmapDepth;
GpuAccessor = gpuAccessor;
Flags = flags;
UsedFeatures = FeatureFlags.None;
}
public int GetDepthRegister()

View file

@ -16,15 +16,19 @@ namespace Ryujinx.Graphics.Shader.Translation
public static ShaderProgram Translate(ulong address, IGpuAccessor gpuAccessor, TranslationFlags flags)
{
Operation[] ops = DecodeShader(address, gpuAccessor, flags, out ShaderConfig config, out int size);
Operation[] ops = DecodeShader(address, gpuAccessor, flags, out ShaderConfig config, out int size, out FeatureFlags featureFlags);
config.UsedFeatures = featureFlags;
return Translate(ops, config, size);
}
public static ShaderProgram Translate(ulong addressA, ulong addressB, IGpuAccessor gpuAccessor, TranslationFlags flags)
{
Operation[] opsA = DecodeShader(addressA, gpuAccessor, flags | TranslationFlags.VertexA, out _, out int sizeA);
Operation[] opsB = DecodeShader(addressB, gpuAccessor, flags, out ShaderConfig config, out int sizeB);
Operation[] opsA = DecodeShader(addressA, gpuAccessor, flags | TranslationFlags.VertexA, out _, out int sizeA, out FeatureFlags featureFlagsA);
Operation[] opsB = DecodeShader(addressB, gpuAccessor, flags, out ShaderConfig config, out int sizeB, out FeatureFlags featureFlagsB);
config.UsedFeatures = featureFlagsA | featureFlagsB;
return Translate(Combine(opsA, opsB), config, sizeB, sizeA);
}
@ -67,7 +71,8 @@ namespace Ryujinx.Graphics.Shader.Translation
IGpuAccessor gpuAccessor,
TranslationFlags flags,
out ShaderConfig config,
out int size)
out int size,
out FeatureFlags featureFlags)
{
Block[] cfg;
@ -90,6 +95,8 @@ namespace Ryujinx.Graphics.Shader.Translation
size = 0;
featureFlags = FeatureFlags.None;
return Array.Empty<Operation>();
}
@ -192,6 +199,8 @@ namespace Ryujinx.Graphics.Shader.Translation
size = (int)maxEndAddress + (((flags & TranslationFlags.Compute) != 0) ? 0 : HeaderSize);
featureFlags = context.UsedFeatures;
return context.GetOperations();
}