From 636542d817b3403ae44b46a48a67cedc0c7b42c5 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Thu, 30 Jul 2020 02:53:23 -0300 Subject: [PATCH] Refactor shader translator ShaderConfig and reduce the number of out args (#1438) --- .../Instructions/InstEmitTexture.cs | 6 ++-- .../Translation/EmitterContext.cs | 4 +-- .../Translation/ShaderConfig.cs | 20 ++++++++--- .../Translation/Translator.cs | 34 +++++-------------- 4 files changed, 29 insertions(+), 35 deletions(-) diff --git a/Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs b/Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs index ea153b11..77e3fae7 100644 --- a/Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs +++ b/Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs @@ -283,14 +283,14 @@ namespace Ryujinx.Graphics.Shader.Instructions public static void Tld(EmitterContext context) { - context.UsedFeatures |= FeatureFlags.IntegerSampling; + context.Config.SetUsedFeature(FeatureFlags.IntegerSampling); EmitTextureSample(context, TextureFlags.IntCoords); } public static void TldB(EmitterContext context) { - context.UsedFeatures |= FeatureFlags.IntegerSampling; + context.Config.SetUsedFeature(FeatureFlags.IntegerSampling); EmitTextureSample(context, TextureFlags.IntCoords | TextureFlags.Bindless); } @@ -432,7 +432,7 @@ namespace Ryujinx.Graphics.Shader.Instructions return; } - context.UsedFeatures |= FeatureFlags.IntegerSampling; + context.Config.SetUsedFeature(FeatureFlags.IntegerSampling); flags = ConvertTextureFlags(tldsOp.Target) | TextureFlags.IntCoords; diff --git a/Ryujinx.Graphics.Shader/Translation/EmitterContext.cs b/Ryujinx.Graphics.Shader/Translation/EmitterContext.cs index 9b26fa4a..8cd59016 100644 --- a/Ryujinx.Graphics.Shader/Translation/EmitterContext.cs +++ b/Ryujinx.Graphics.Shader/Translation/EmitterContext.cs @@ -11,8 +11,6 @@ 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 _operations; @@ -50,7 +48,7 @@ namespace Ryujinx.Graphics.Shader.Translation { case AttributeConsts.PositionX: case AttributeConsts.PositionY: - UsedFeatures |= FeatureFlags.FragCoordXY; + Config.SetUsedFeature(FeatureFlags.FragCoordXY); break; } } diff --git a/Ryujinx.Graphics.Shader/Translation/ShaderConfig.cs b/Ryujinx.Graphics.Shader/Translation/ShaderConfig.cs index aabd9ca6..7b1587ae 100644 --- a/Ryujinx.Graphics.Shader/Translation/ShaderConfig.cs +++ b/Ryujinx.Graphics.Shader/Translation/ShaderConfig.cs @@ -1,8 +1,6 @@ -using System; - namespace Ryujinx.Graphics.Shader.Translation { - struct ShaderConfig + class ShaderConfig { public ShaderStage Stage { get; } @@ -22,7 +20,9 @@ namespace Ryujinx.Graphics.Shader.Translation public TranslationFlags Flags { get; } - public FeatureFlags UsedFeatures { get; set; } + public int Size { get; private set; } + + public FeatureFlags UsedFeatures { get; private set; } public ShaderConfig(IGpuAccessor gpuAccessor, TranslationFlags flags) { @@ -36,6 +36,7 @@ namespace Ryujinx.Graphics.Shader.Translation OmapDepth = false; GpuAccessor = gpuAccessor; Flags = flags; + Size = 0; UsedFeatures = FeatureFlags.None; } @@ -51,6 +52,7 @@ namespace Ryujinx.Graphics.Shader.Translation OmapDepth = header.OmapDepth; GpuAccessor = gpuAccessor; Flags = flags; + Size = 0; UsedFeatures = FeatureFlags.None; } @@ -93,5 +95,15 @@ namespace Ryujinx.Graphics.Shader.Translation return format; } + + public void SizeAdd(int size) + { + Size += size; + } + + public void SetUsedFeature(FeatureFlags flags) + { + UsedFeatures |= flags; + } } } \ No newline at end of file diff --git a/Ryujinx.Graphics.Shader/Translation/Translator.cs b/Ryujinx.Graphics.Shader/Translation/Translator.cs index 1d7aacdd..db0924b3 100644 --- a/Ryujinx.Graphics.Shader/Translation/Translator.cs +++ b/Ryujinx.Graphics.Shader/Translation/Translator.cs @@ -16,24 +16,20 @@ 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, out FeatureFlags featureFlags); - - config.UsedFeatures = featureFlags; - - return Translate(ops, config, size); + return Translate(DecodeShader(address, gpuAccessor, flags, out ShaderConfig config), config); } public static ShaderProgram Translate(ulong addressA, ulong addressB, IGpuAccessor gpuAccessor, TranslationFlags flags) { - 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); + Operation[] opsA = DecodeShader(addressA, gpuAccessor, flags | TranslationFlags.VertexA, out ShaderConfig configA); + Operation[] opsB = DecodeShader(addressB, gpuAccessor, flags, out ShaderConfig config); - config.UsedFeatures = featureFlagsA | featureFlagsB; + config.SetUsedFeature(configA.UsedFeatures); - return Translate(Combine(opsA, opsB), config, sizeB, sizeA); + return Translate(Combine(opsA, opsB), config, configA.Size); } - private static ShaderProgram Translate(Operation[] ops, ShaderConfig config, int size, int sizeA = 0) + private static ShaderProgram Translate(Operation[] ops, ShaderConfig config, int sizeA = 0) { BasicBlock[] blocks = ControlFlowGraph.MakeCfg(ops); @@ -63,16 +59,10 @@ namespace Ryujinx.Graphics.Shader.Translation string glslCode = program.Code; - return new ShaderProgram(spInfo, config.Stage, glslCode, size, sizeA); + return new ShaderProgram(spInfo, config.Stage, glslCode, config.Size, sizeA); } - private static Operation[] DecodeShader( - ulong address, - IGpuAccessor gpuAccessor, - TranslationFlags flags, - out ShaderConfig config, - out int size, - out FeatureFlags featureFlags) + private static Operation[] DecodeShader(ulong address, IGpuAccessor gpuAccessor, TranslationFlags flags, out ShaderConfig config) { Block[] cfg; @@ -93,10 +83,6 @@ namespace Ryujinx.Graphics.Shader.Translation { gpuAccessor.Log("Invalid branch detected, failed to build CFG."); - size = 0; - - featureFlags = FeatureFlags.None; - return Array.Empty(); } @@ -197,9 +183,7 @@ namespace Ryujinx.Graphics.Shader.Translation } } - size = (int)maxEndAddress + (((flags & TranslationFlags.Compute) != 0) ? 0 : HeaderSize); - - featureFlags = context.UsedFeatures; + config.SizeAdd((int)maxEndAddress + (flags.HasFlag(TranslationFlags.Compute) ? 0 : HeaderSize)); return context.GetOperations(); }