aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Shader/CodeGen
diff options
context:
space:
mode:
authorriperiperi <rhy3756547@hotmail.com>2020-07-07 03:41:07 +0100
committerGitHub <noreply@github.com>2020-07-07 04:41:07 +0200
commit484eb645ae0611f60fae845ed011ed6115352e06 (patch)
treeb15972f04dae0b1b6c644cbbbdfcd98856adee15 /Ryujinx.Graphics.Shader/CodeGen
parent43b78ae157eed5c9436dc19a6d498655891202d8 (diff)
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.
Diffstat (limited to 'Ryujinx.Graphics.Shader/CodeGen')
-rw-r--r--Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs17
-rw-r--r--Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs35
-rw-r--r--Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/TexelFetchScale_cp.glsl7
-rw-r--r--Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/TexelFetchScale_fp.glsl11
-rw-r--r--Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs29
-rw-r--r--Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs4
6 files changed, 100 insertions, 3 deletions
diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs
index 6bef8e6c..91ab7ad5 100644
--- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs
+++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs
@@ -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);
diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs
index bd947ab7..f9d61928 100644
--- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs
+++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs
@@ -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);
diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/TexelFetchScale_cp.glsl b/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/TexelFetchScale_cp.glsl
new file mode 100644
index 00000000..381566d3
--- /dev/null
+++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/TexelFetchScale_cp.glsl
@@ -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);
+} \ No newline at end of file
diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/TexelFetchScale_fp.glsl b/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/TexelFetchScale_fp.glsl
new file mode 100644
index 00000000..4efaa65a
--- /dev/null
+++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/TexelFetchScale_fp.glsl
@@ -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);
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs
index d05c77df..b951798d 100644
--- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs
+++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs
@@ -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)
{
diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs
index 4c3f802a..971284f4 100644
--- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs
+++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs
@@ -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";
}