aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Shader
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
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')
-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
-rw-r--r--Ryujinx.Graphics.Shader/Instructions/InstEmitMemory.cs4
-rw-r--r--Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs6
-rw-r--r--Ryujinx.Graphics.Shader/Ryujinx.Graphics.Shader.csproj2
-rw-r--r--Ryujinx.Graphics.Shader/TextureDescriptor.cs6
-rw-r--r--Ryujinx.Graphics.Shader/TextureUsageFlags.cs16
-rw-r--r--Ryujinx.Graphics.Shader/Translation/EmitterContext.cs16
-rw-r--r--Ryujinx.Graphics.Shader/Translation/FeatureFlags.cs18
-rw-r--r--Ryujinx.Graphics.Shader/Translation/ShaderConfig.cs4
-rw-r--r--Ryujinx.Graphics.Shader/Translation/Translator.cs17
15 files changed, 185 insertions, 7 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";
}
diff --git a/Ryujinx.Graphics.Shader/Instructions/InstEmitMemory.cs b/Ryujinx.Graphics.Shader/Instructions/InstEmitMemory.cs
index 2418293d..24ba9a06 100644
--- a/Ryujinx.Graphics.Shader/Instructions/InstEmitMemory.cs
+++ b/Ryujinx.Graphics.Shader/Instructions/InstEmitMemory.cs
@@ -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 &&
diff --git a/Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs b/Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs
index 304906d0..ea153b11 100644
--- a/Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs
+++ b/Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs
@@ -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))
diff --git a/Ryujinx.Graphics.Shader/Ryujinx.Graphics.Shader.csproj b/Ryujinx.Graphics.Shader/Ryujinx.Graphics.Shader.csproj
index d53bdce9..b2d8a2a7 100644
--- a/Ryujinx.Graphics.Shader/Ryujinx.Graphics.Shader.csproj
+++ b/Ryujinx.Graphics.Shader/Ryujinx.Graphics.Shader.csproj
@@ -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>
diff --git a/Ryujinx.Graphics.Shader/TextureDescriptor.cs b/Ryujinx.Graphics.Shader/TextureDescriptor.cs
index fae9b58c..a9900fb8 100644
--- a/Ryujinx.Graphics.Shader/TextureDescriptor.cs
+++ b/Ryujinx.Graphics.Shader/TextureDescriptor.cs
@@ -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;
}
}
} \ No newline at end of file
diff --git a/Ryujinx.Graphics.Shader/TextureUsageFlags.cs b/Ryujinx.Graphics.Shader/TextureUsageFlags.cs
new file mode 100644
index 00000000..d9fa1a50
--- /dev/null
+++ b/Ryujinx.Graphics.Shader/TextureUsageFlags.cs
@@ -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
+ }
+}
diff --git a/Ryujinx.Graphics.Shader/Translation/EmitterContext.cs b/Ryujinx.Graphics.Shader/Translation/EmitterContext.cs
index 70476dcd..39532a64 100644
--- a/Ryujinx.Graphics.Shader/Translation/EmitterContext.cs
+++ b/Ryujinx.Graphics.Shader/Translation/EmitterContext.cs
@@ -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);
diff --git a/Ryujinx.Graphics.Shader/Translation/FeatureFlags.cs b/Ryujinx.Graphics.Shader/Translation/FeatureFlags.cs
new file mode 100644
index 00000000..9c65038a
--- /dev/null
+++ b/Ryujinx.Graphics.Shader/Translation/FeatureFlags.cs
@@ -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
+ }
+}
diff --git a/Ryujinx.Graphics.Shader/Translation/ShaderConfig.cs b/Ryujinx.Graphics.Shader/Translation/ShaderConfig.cs
index 8b38afb9..aabd9ca6 100644
--- a/Ryujinx.Graphics.Shader/Translation/ShaderConfig.cs
+++ b/Ryujinx.Graphics.Shader/Translation/ShaderConfig.cs
@@ -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()
diff --git a/Ryujinx.Graphics.Shader/Translation/Translator.cs b/Ryujinx.Graphics.Shader/Translation/Translator.cs
index 1a69c511..1d7aacdd 100644
--- a/Ryujinx.Graphics.Shader/Translation/Translator.cs
+++ b/Ryujinx.Graphics.Shader/Translation/Translator.cs
@@ -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();
}