From 4dd85f86a89338ff84d05a3981c14f6de1be4606 Mon Sep 17 00:00:00 2001
From: Feng Chen <vonchenplus@gmail.com>
Date: Fri, 19 Nov 2021 15:32:53 +0800
Subject: [PATCH 1/4] Implement convert legacy to generic

---
 src/shader_recompiler/frontend/ir/attribute.h |  2 +
 .../frontend/maxwell/translate_program.cpp    | 95 +++++++++++++++++++
 .../frontend/maxwell/translate_program.h      |  4 +
 src/shader_recompiler/varying_state.h         |  3 +-
 .../renderer_opengl/gl_shader_cache.cpp       |  3 +
 .../renderer_vulkan/vk_pipeline_cache.cpp     |  2 +
 6 files changed, 108 insertions(+), 1 deletion(-)

diff --git a/src/shader_recompiler/frontend/ir/attribute.h b/src/shader_recompiler/frontend/ir/attribute.h
index ca11994943..5c5e7bb4ba 100644
--- a/src/shader_recompiler/frontend/ir/attribute.h
+++ b/src/shader_recompiler/frontend/ir/attribute.h
@@ -224,6 +224,8 @@ enum class Attribute : u64 {
 
 constexpr size_t NUM_GENERICS = 32;
 
+constexpr size_t NUM_FIXED_FNC_TEXTURES = 10;
+
 [[nodiscard]] bool IsGeneric(Attribute attribute) noexcept;
 
 [[nodiscard]] u32 GenericAttributeIndex(Attribute attribute);
diff --git a/src/shader_recompiler/frontend/maxwell/translate_program.cpp b/src/shader_recompiler/frontend/maxwell/translate_program.cpp
index 267ebe4af3..40c9307dbd 100644
--- a/src/shader_recompiler/frontend/maxwell/translate_program.cpp
+++ b/src/shader_recompiler/frontend/maxwell/translate_program.cpp
@@ -5,6 +5,7 @@
 #include <algorithm>
 #include <memory>
 #include <vector>
+#include <queue>
 
 #include "common/settings.h"
 #include "shader_recompiler/exception.h"
@@ -226,4 +227,98 @@ IR::Program MergeDualVertexPrograms(IR::Program& vertex_a, IR::Program& vertex_b
     return result;
 }
 
+bool IsLegacyAttribute(IR::Attribute attribute) {
+    return (attribute >= IR::Attribute::ColorFrontDiffuseR &&
+            attribute <= IR::Attribute::ColorBackSpecularA) ||
+           attribute == IR::Attribute::FogCoordinate ||
+           (attribute >= IR::Attribute::FixedFncTexture0S &&
+            attribute <= IR::Attribute::FixedFncTexture9Q);
+}
+
+std::map<IR::Attribute, IR::Attribute> GenerateLegacyToGenericMappings(
+    const VaryingState& state, std::queue<IR::Attribute> ununsed_generics) {
+    std::map<IR::Attribute, IR::Attribute> mapping;
+    for (size_t index = 0; index < 4; ++index) {
+        auto attr = IR::Attribute::ColorFrontDiffuseR + index * 4;
+        if (state.AnyComponent(attr)) {
+            for (size_t i = 0; i < 4; ++i) {
+                mapping.insert({attr + i, ununsed_generics.front() + i});
+            }
+            ununsed_generics.pop();
+        }
+    }
+    if (state[IR::Attribute::FogCoordinate]) {
+        mapping.insert({IR::Attribute::FogCoordinate, ununsed_generics.front()});
+        ununsed_generics.pop();
+    }
+    for (size_t index = 0; index < IR::NUM_FIXED_FNC_TEXTURES; ++index) {
+        auto attr = IR::Attribute::FixedFncTexture0S + index * 4;
+        if (state.AnyComponent(attr)) {
+            for (size_t i = 0; i < 4; ++i) {
+                mapping.insert({attr + i, ununsed_generics.front() + i});
+            }
+            ununsed_generics.pop();
+        }
+    }
+    return mapping;
+}
+
+void ConvertLegacyToGeneric(IR::Program& program, const Shader::RuntimeInfo& runtime_info) {
+    auto& stores = program.info.stores;
+    if (stores.Legacy()) {
+        std::queue<IR::Attribute> ununsed_output_generics{};
+        for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
+            if (!stores.Generic(index)) {
+                ununsed_output_generics.push(IR::Attribute::Generic0X + index * 4);
+            }
+        }
+        auto mappings = GenerateLegacyToGenericMappings(stores, ununsed_output_generics);
+        for (IR::Block* const block : program.post_order_blocks) {
+            for (IR::Inst& inst : block->Instructions()) {
+                switch (inst.GetOpcode()) {
+                case IR::Opcode::SetAttribute: {
+                    const auto attr = inst.Arg(0).Attribute();
+                    if (IsLegacyAttribute(attr)) {
+                        stores.Set(mappings[attr], true);
+                        inst.SetArg(0, Shader::IR::Value(mappings[attr]));
+                    }
+                    break;
+                }
+                default:
+                    break;
+                }
+            }
+        }
+    }
+
+    auto& loads = program.info.loads;
+    if (loads.Legacy()) {
+        std::queue<IR::Attribute> ununsed_input_generics{};
+        for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
+            const AttributeType input_type{runtime_info.generic_input_types[index]};
+            if (!runtime_info.previous_stage_stores.Generic(index) || !loads.Generic(index) ||
+                input_type == AttributeType::Disabled) {
+                ununsed_input_generics.push(IR::Attribute::Generic0X + index * 4);
+            }
+        }
+        auto mappings = GenerateLegacyToGenericMappings(loads, ununsed_input_generics);
+        for (IR::Block* const block : program.post_order_blocks) {
+            for (IR::Inst& inst : block->Instructions()) {
+                switch (inst.GetOpcode()) {
+                case IR::Opcode::GetAttribute: {
+                    const auto attr = inst.Arg(0).Attribute();
+                    if (IsLegacyAttribute(attr)) {
+                        loads.Set(mappings[attr], true);
+                        inst.SetArg(0, Shader::IR::Value(mappings[attr]));
+                    }
+                    break;
+                }
+                default:
+                    break;
+                }
+            }
+        }
+    }
+}
+
 } // namespace Shader::Maxwell
diff --git a/src/shader_recompiler/frontend/maxwell/translate_program.h b/src/shader_recompiler/frontend/maxwell/translate_program.h
index a84814811e..cd535f20d6 100644
--- a/src/shader_recompiler/frontend/maxwell/translate_program.h
+++ b/src/shader_recompiler/frontend/maxwell/translate_program.h
@@ -10,6 +10,7 @@
 #include "shader_recompiler/frontend/maxwell/control_flow.h"
 #include "shader_recompiler/host_translate_info.h"
 #include "shader_recompiler/object_pool.h"
+#include "shader_recompiler/runtime_info.h"
 
 namespace Shader::Maxwell {
 
@@ -20,4 +21,7 @@ namespace Shader::Maxwell {
 [[nodiscard]] IR::Program MergeDualVertexPrograms(IR::Program& vertex_a, IR::Program& vertex_b,
                                                   Environment& env_vertex_b);
 
+[[nodiscard]] void ConvertLegacyToGeneric(IR::Program& program,
+                                          const Shader::RuntimeInfo& runtime_info);
+
 } // namespace Shader::Maxwell
diff --git a/src/shader_recompiler/varying_state.h b/src/shader_recompiler/varying_state.h
index 9d7b24a763..bc4f273c8d 100644
--- a/src/shader_recompiler/varying_state.h
+++ b/src/shader_recompiler/varying_state.h
@@ -53,7 +53,8 @@ struct VaryingState {
         return AnyComponent(IR::Attribute::ColorFrontDiffuseR) ||
                AnyComponent(IR::Attribute::ColorFrontSpecularR) ||
                AnyComponent(IR::Attribute::ColorBackDiffuseR) ||
-               AnyComponent(IR::Attribute::ColorBackSpecularR) || FixedFunctionTexture();
+               AnyComponent(IR::Attribute::ColorBackSpecularR) || FixedFunctionTexture() ||
+               mask[static_cast<size_t>(IR::Attribute::FogCoordinate)];
     }
 
     [[nodiscard]] bool FixedFunctionTexture() const noexcept {
diff --git a/src/video_core/renderer_opengl/gl_shader_cache.cpp b/src/video_core/renderer_opengl/gl_shader_cache.cpp
index 42ef67628d..310cda6d8c 100644
--- a/src/video_core/renderer_opengl/gl_shader_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_shader_cache.cpp
@@ -44,6 +44,7 @@ using Shader::Backend::GLSL::EmitGLSL;
 using Shader::Backend::SPIRV::EmitSPIRV;
 using Shader::Maxwell::MergeDualVertexPrograms;
 using Shader::Maxwell::TranslateProgram;
+using Shader::Maxwell::ConvertLegacyToGeneric;
 using VideoCommon::ComputeEnvironment;
 using VideoCommon::FileEnvironment;
 using VideoCommon::GenericEnvironment;
@@ -462,12 +463,14 @@ std::unique_ptr<GraphicsPipeline> ShaderCache::CreateGraphicsPipeline(
             MakeRuntimeInfo(key, program, previous_program, glasm_use_storage_buffers, use_glasm)};
         switch (device.GetShaderBackend()) {
         case Settings::ShaderBackend::GLSL:
+            ConvertLegacyToGeneric(program, runtime_info);
             sources[stage_index] = EmitGLSL(profile, runtime_info, program, binding);
             break;
         case Settings::ShaderBackend::GLASM:
             sources[stage_index] = EmitGLASM(profile, runtime_info, program, binding);
             break;
         case Settings::ShaderBackend::SPIRV:
+            ConvertLegacyToGeneric(program, runtime_info);
             sources_spirv[stage_index] = EmitSPIRV(profile, runtime_info, program, binding);
             break;
         }
diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
index eb8b4e08b9..5efa4feb4b 100644
--- a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
+++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
@@ -50,6 +50,7 @@ namespace {
 using Shader::Backend::SPIRV::EmitSPIRV;
 using Shader::Maxwell::MergeDualVertexPrograms;
 using Shader::Maxwell::TranslateProgram;
+using Shader::Maxwell::ConvertLegacyToGeneric;
 using VideoCommon::ComputeEnvironment;
 using VideoCommon::FileEnvironment;
 using VideoCommon::GenericEnvironment;
@@ -543,6 +544,7 @@ std::unique_ptr<GraphicsPipeline> PipelineCache::CreateGraphicsPipeline(
         infos[stage_index] = &program.info;
 
         const auto runtime_info{MakeRuntimeInfo(programs, key, program, previous_stage)};
+        ConvertLegacyToGeneric(program, runtime_info);
         const std::vector<u32> code{EmitSPIRV(profile, runtime_info, program, binding)};
         device.SaveShader(code);
         modules[stage_index] = BuildShader(device, code);

From 94652e122dcf627b451fcd3d91156a663ae3dafe Mon Sep 17 00:00:00 2001
From: vonchenplus <vonchenplus@gmail.com>
Date: Sat, 18 Dec 2021 14:03:40 +0800
Subject: [PATCH 2/4] Remove glsl handle legacy related code

---
 .../backend/glsl/emit_glsl.cpp                |  2 +-
 .../glsl/emit_glsl_context_get_set.cpp        | 64 -------------------
 .../backend/glsl/glsl_emit_context.cpp        | 38 -----------
 3 files changed, 1 insertion(+), 103 deletions(-)

diff --git a/src/shader_recompiler/backend/glsl/emit_glsl.cpp b/src/shader_recompiler/backend/glsl/emit_glsl.cpp
index 78b2eeaa26..b6b17a3306 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl.cpp
@@ -176,7 +176,7 @@ void EmitCode(EmitContext& ctx, const IR::Program& program) {
 }
 
 std::string GlslVersionSpecifier(const EmitContext& ctx) {
-    if (ctx.uses_y_direction || ctx.info.stores.Legacy() || ctx.info.loads.Legacy()) {
+    if (ctx.uses_y_direction) {
         return " compatibility";
     }
     return "";
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp
index 1920047f46..6477bd1928 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp
@@ -98,10 +98,6 @@ void GetCbuf16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, const
         GetCbuf(ctx, ret, binding, offset, 16, cast, bit_offset);
     }
 }
-
-u32 TexCoordIndex(IR::Attribute attr) {
-    return (static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
-}
 } // Anonymous namespace
 
 void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
@@ -190,18 +186,6 @@ void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
         ctx.AddF32("{}=in_attr{}{}.{};", inst, index, InputVertexIndex(ctx, vertex), swizzle);
         return;
     }
-    // GLSL only exposes 8 legacy texcoords
-    if (attr >= IR::Attribute::FixedFncTexture8S && attr <= IR::Attribute::FixedFncTexture9Q) {
-        LOG_WARNING(Shader_GLSL, "GLSL does not allow access to gl_TexCoord[{}]",
-                    TexCoordIndex(attr));
-        ctx.AddF32("{}=0.f;", inst);
-        return;
-    }
-    if (attr >= IR::Attribute::FixedFncTexture0S && attr <= IR::Attribute::FixedFncTexture7Q) {
-        const u32 index{TexCoordIndex(attr)};
-        ctx.AddF32("{}=gl_TexCoord[{}].{};", inst, index, swizzle);
-        return;
-    }
     switch (attr) {
     case IR::Attribute::PrimitiveId:
         ctx.AddF32("{}=itof(gl_PrimitiveID);", inst);
@@ -215,16 +199,6 @@ void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
         ctx.AddF32("{}={}{}.{};", inst, input_decorator, ctx.position_name, swizzle);
         break;
     }
-    case IR::Attribute::ColorFrontDiffuseR:
-    case IR::Attribute::ColorFrontDiffuseG:
-    case IR::Attribute::ColorFrontDiffuseB:
-    case IR::Attribute::ColorFrontDiffuseA:
-        if (ctx.stage == Stage::Fragment) {
-            ctx.AddF32("{}=gl_Color.{};", inst, swizzle);
-        } else {
-            ctx.AddF32("{}=gl_FrontColor.{};", inst, swizzle);
-        }
-        break;
     case IR::Attribute::PointSpriteS:
     case IR::Attribute::PointSpriteT:
         ctx.AddF32("{}=gl_PointCoord.{};", inst, swizzle);
@@ -264,17 +238,6 @@ void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view val
     }
     const u32 element{static_cast<u32>(attr) % 4};
     const char swizzle{"xyzw"[element]};
-    // GLSL only exposes 8 legacy texcoords
-    if (attr >= IR::Attribute::FixedFncTexture8S && attr <= IR::Attribute::FixedFncTexture9Q) {
-        LOG_WARNING(Shader_GLSL, "GLSL does not allow access to gl_TexCoord[{}]",
-                    TexCoordIndex(attr));
-        return;
-    }
-    if (attr >= IR::Attribute::FixedFncTexture0S && attr <= IR::Attribute::FixedFncTexture7Q) {
-        const u32 index{TexCoordIndex(attr)};
-        ctx.Add("gl_TexCoord[{}].{}={};", index, swizzle, value);
-        return;
-    }
     switch (attr) {
     case IR::Attribute::Layer:
         if (ctx.stage != Stage::Geometry &&
@@ -312,33 +275,6 @@ void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view val
     case IR::Attribute::PositionW:
         ctx.Add("gl_Position.{}={};", swizzle, value);
         break;
-    case IR::Attribute::ColorFrontDiffuseR:
-    case IR::Attribute::ColorFrontDiffuseG:
-    case IR::Attribute::ColorFrontDiffuseB:
-    case IR::Attribute::ColorFrontDiffuseA:
-        ctx.Add("gl_FrontColor.{}={};", swizzle, value);
-        break;
-    case IR::Attribute::ColorFrontSpecularR:
-    case IR::Attribute::ColorFrontSpecularG:
-    case IR::Attribute::ColorFrontSpecularB:
-    case IR::Attribute::ColorFrontSpecularA:
-        ctx.Add("gl_FrontSecondaryColor.{}={};", swizzle, value);
-        break;
-    case IR::Attribute::ColorBackDiffuseR:
-    case IR::Attribute::ColorBackDiffuseG:
-    case IR::Attribute::ColorBackDiffuseB:
-    case IR::Attribute::ColorBackDiffuseA:
-        ctx.Add("gl_BackColor.{}={};", swizzle, value);
-        break;
-    case IR::Attribute::ColorBackSpecularR:
-    case IR::Attribute::ColorBackSpecularG:
-    case IR::Attribute::ColorBackSpecularB:
-    case IR::Attribute::ColorBackSpecularA:
-        ctx.Add("gl_BackSecondaryColor.{}={};", swizzle, value);
-        break;
-    case IR::Attribute::FogCoordinate:
-        ctx.Add("gl_FogFragCoord={};", value);
-        break;
     case IR::Attribute::ClipDistance0:
     case IR::Attribute::ClipDistance1:
     case IR::Attribute::ClipDistance2:
diff --git a/src/shader_recompiler/backend/glsl/glsl_emit_context.cpp b/src/shader_recompiler/backend/glsl/glsl_emit_context.cpp
index 1de017e76c..bc9d2a904a 100644
--- a/src/shader_recompiler/backend/glsl/glsl_emit_context.cpp
+++ b/src/shader_recompiler/backend/glsl/glsl_emit_context.cpp
@@ -211,27 +211,6 @@ std::string_view OutputPrimitive(OutputTopology topology) {
     throw InvalidArgument("Invalid output topology {}", topology);
 }
 
-void SetupLegacyOutPerVertex(EmitContext& ctx, std::string& header) {
-    if (!ctx.info.stores.Legacy()) {
-        return;
-    }
-    if (ctx.info.stores.FixedFunctionTexture()) {
-        header += "vec4 gl_TexCoord[8];";
-    }
-    if (ctx.info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
-        header += "vec4 gl_FrontColor;";
-    }
-    if (ctx.info.stores.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
-        header += "vec4 gl_FrontSecondaryColor;";
-    }
-    if (ctx.info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
-        header += "vec4 gl_BackColor;";
-    }
-    if (ctx.info.stores.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
-        header += "vec4 gl_BackSecondaryColor;";
-    }
-}
-
 void SetupOutPerVertex(EmitContext& ctx, std::string& header) {
     if (!StoresPerVertexAttributes(ctx.stage)) {
         return;
@@ -250,7 +229,6 @@ void SetupOutPerVertex(EmitContext& ctx, std::string& header) {
         ctx.profile.support_viewport_index_layer_non_geometry && ctx.stage != Stage::Geometry) {
         header += "int gl_ViewportIndex;";
     }
-    SetupLegacyOutPerVertex(ctx, header);
     header += "};";
     if (ctx.info.stores[IR::Attribute::ViewportIndex] && ctx.stage == Stage::Geometry) {
         header += "out int gl_ViewportIndex;";
@@ -282,21 +260,6 @@ void SetupInPerVertex(EmitContext& ctx, std::string& header) {
     }
     header += "}gl_in[gl_MaxPatchVertices];";
 }
-
-void SetupLegacyInPerFragment(EmitContext& ctx, std::string& header) {
-    if (!ctx.info.loads.Legacy()) {
-        return;
-    }
-    header += "in gl_PerFragment{";
-    if (ctx.info.loads.FixedFunctionTexture()) {
-        header += "vec4 gl_TexCoord[8];";
-    }
-    if (ctx.info.loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
-        header += "vec4 gl_Color;";
-    }
-    header += "};";
-}
-
 } // Anonymous namespace
 
 EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_,
@@ -361,7 +324,6 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile
     }
     SetupOutPerVertex(*this, header);
     SetupInPerVertex(*this, header);
-    SetupLegacyInPerFragment(*this, header);
 
     for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
         if (!info.loads.Generic(index) || !runtime_info.previous_stage_stores.Generic(index)) {

From 6ebc972c2b183b475c8f2e9b54a4fc5c2256f359 Mon Sep 17 00:00:00 2001
From: vonchenplus <vonchenplus@gmail.com>
Date: Sat, 18 Dec 2021 14:08:50 +0800
Subject: [PATCH 3/4] Remove spirv handle legacy related code

---
 .../spirv/emit_spirv_context_get_set.cpp      |  31 ----
 .../backend/spirv/spirv_emit_context.cpp      | 143 ------------------
 .../backend/spirv/spirv_emit_context.h        |  15 --
 src/shader_recompiler/frontend/ir/attribute.h |   2 +-
 4 files changed, 1 insertion(+), 190 deletions(-)

diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp
index ad84966b52..14f470812e 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp
+++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp
@@ -44,14 +44,6 @@ Id AttrPointer(EmitContext& ctx, Id pointer_type, Id vertex, Id base, Args&&...
     }
 }
 
-bool IsLegacyAttribute(IR::Attribute attribute) {
-    return (attribute >= IR::Attribute::ColorFrontDiffuseR &&
-            attribute <= IR::Attribute::ColorBackSpecularA) ||
-           attribute == IR::Attribute::FogCoordinate ||
-           (attribute >= IR::Attribute::FixedFncTexture0S &&
-            attribute <= IR::Attribute::FixedFncTexture9Q);
-}
-
 template <typename... Args>
 Id OutputAccessChain(EmitContext& ctx, Id result_type, Id base, Args&&... args) {
     if (ctx.stage == Stage::TessellationControl) {
@@ -83,17 +75,6 @@ std::optional<OutAttr> OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) {
             return OutputAccessChain(ctx, ctx.output_f32, info.id, index_id);
         }
     }
-    if (IsLegacyAttribute(attr)) {
-        if (attr == IR::Attribute::FogCoordinate) {
-            return OutputAccessChain(ctx, ctx.output_f32, ctx.OutputLegacyAttribute(attr),
-                                     ctx.Const(0u));
-        } else {
-            const u32 element{static_cast<u32>(attr) % 4};
-            const Id element_id{ctx.Const(element)};
-            return OutputAccessChain(ctx, ctx.output_f32, ctx.OutputLegacyAttribute(attr),
-                                     element_id);
-        }
-    }
     switch (attr) {
     case IR::Attribute::PointSize:
         return ctx.output_point_size;
@@ -327,18 +308,6 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) {
         const Id value{ctx.OpLoad(type->id, pointer)};
         return type->needs_cast ? ctx.OpBitcast(ctx.F32[1], value) : value;
     }
-    if (IsLegacyAttribute(attr)) {
-        if (attr == IR::Attribute::FogCoordinate) {
-            const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex,
-                                          ctx.InputLegacyAttribute(attr), ctx.Const(0u))};
-            return ctx.OpLoad(ctx.F32[1], attr_ptr);
-        } else {
-            const Id element_id{ctx.Const(element)};
-            const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex,
-                                          ctx.InputLegacyAttribute(attr), element_id)};
-            return ctx.OpLoad(ctx.F32[1], attr_ptr);
-        }
-    }
     switch (attr) {
     case IR::Attribute::PrimitiveId:
         return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.primitive_id));
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
index 4b6f792bf9..d3ba66569b 100644
--- a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
+++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
@@ -18,8 +18,6 @@
 
 namespace Shader::Backend::SPIRV {
 namespace {
-constexpr size_t NUM_FIXEDFNCTEXTURE = 10;
-
 enum class Operation {
     Increment,
     Decrement,
@@ -432,34 +430,6 @@ Id DescType(EmitContext& ctx, Id sampled_type, Id pointer_type, u32 count) {
         return pointer_type;
     }
 }
-
-size_t FindAndSetNextUnusedLocation(std::bitset<IR::NUM_GENERICS>& used_locations,
-                                    size_t& start_offset) {
-    for (size_t location = start_offset; location < used_locations.size(); ++location) {
-        if (!used_locations.test(location)) {
-            start_offset = location;
-            used_locations.set(location);
-            return location;
-        }
-    }
-    throw RuntimeError("Unable to get an unused location for legacy attribute");
-}
-
-Id DefineLegacyInput(EmitContext& ctx, std::bitset<IR::NUM_GENERICS>& used_locations,
-                     size_t& start_offset) {
-    const Id id{DefineInput(ctx, ctx.F32[4], true)};
-    const size_t location = FindAndSetNextUnusedLocation(used_locations, start_offset);
-    ctx.Decorate(id, spv::Decoration::Location, location);
-    return id;
-}
-
-Id DefineLegacyOutput(EmitContext& ctx, std::bitset<IR::NUM_GENERICS>& used_locations,
-                      size_t& start_offset, std::optional<u32> invocations) {
-    const Id id{DefineOutput(ctx, ctx.F32[4], invocations)};
-    const size_t location = FindAndSetNextUnusedLocation(used_locations, start_offset);
-    ctx.Decorate(id, spv::Decoration::Location, location);
-    return id;
-}
 } // Anonymous namespace
 
 void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) {
@@ -543,64 +513,6 @@ Id EmitContext::BitOffset16(const IR::Value& offset) {
     return OpBitwiseAnd(U32[1], OpShiftLeftLogical(U32[1], Def(offset), Const(3u)), Const(16u));
 }
 
-Id EmitContext::InputLegacyAttribute(IR::Attribute attribute) {
-    if (attribute >= IR::Attribute::ColorFrontDiffuseR &&
-        attribute <= IR::Attribute::ColorFrontDiffuseA) {
-        return input_front_color;
-    }
-    if (attribute >= IR::Attribute::ColorFrontSpecularR &&
-        attribute <= IR::Attribute::ColorFrontSpecularA) {
-        return input_front_secondary_color;
-    }
-    if (attribute >= IR::Attribute::ColorBackDiffuseR &&
-        attribute <= IR::Attribute::ColorBackDiffuseA) {
-        return input_back_color;
-    }
-    if (attribute >= IR::Attribute::ColorBackSpecularR &&
-        attribute <= IR::Attribute::ColorBackSpecularA) {
-        return input_back_secondary_color;
-    }
-    if (attribute == IR::Attribute::FogCoordinate) {
-        return input_fog_frag_coord;
-    }
-    if (attribute >= IR::Attribute::FixedFncTexture0S &&
-        attribute <= IR::Attribute::FixedFncTexture9Q) {
-        u32 index =
-            (static_cast<u32>(attribute) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
-        return input_fixed_fnc_textures[index];
-    }
-    throw InvalidArgument("Attribute is not legacy attribute {}", attribute);
-}
-
-Id EmitContext::OutputLegacyAttribute(IR::Attribute attribute) {
-    if (attribute >= IR::Attribute::ColorFrontDiffuseR &&
-        attribute <= IR::Attribute::ColorFrontDiffuseA) {
-        return output_front_color;
-    }
-    if (attribute >= IR::Attribute::ColorFrontSpecularR &&
-        attribute <= IR::Attribute::ColorFrontSpecularA) {
-        return output_front_secondary_color;
-    }
-    if (attribute >= IR::Attribute::ColorBackDiffuseR &&
-        attribute <= IR::Attribute::ColorBackDiffuseA) {
-        return output_back_color;
-    }
-    if (attribute >= IR::Attribute::ColorBackSpecularR &&
-        attribute <= IR::Attribute::ColorBackSpecularA) {
-        return output_back_secondary_color;
-    }
-    if (attribute == IR::Attribute::FogCoordinate) {
-        return output_fog_frag_coord;
-    }
-    if (attribute >= IR::Attribute::FixedFncTexture0S &&
-        attribute <= IR::Attribute::FixedFncTexture9Q) {
-        u32 index =
-            (static_cast<u32>(attribute) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
-        return output_fixed_fnc_textures[index];
-    }
-    throw InvalidArgument("Attribute is not legacy attribute {}", attribute);
-}
-
 void EmitContext::DefineCommonTypes(const Info& info) {
     void_id = TypeVoid();
 
@@ -1389,7 +1301,6 @@ void EmitContext::DefineInputs(const IR::Program& program) {
         loads[IR::Attribute::TessellationEvaluationPointV]) {
         tess_coord = DefineInput(*this, F32[3], false, spv::BuiltIn::TessCoord);
     }
-    std::bitset<IR::NUM_GENERICS> used_locations{};
     for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
         const AttributeType input_type{runtime_info.generic_input_types[index]};
         if (!runtime_info.previous_stage_stores.Generic(index)) {
@@ -1401,7 +1312,6 @@ void EmitContext::DefineInputs(const IR::Program& program) {
         if (input_type == AttributeType::Disabled) {
             continue;
         }
-        used_locations.set(index);
         const Id type{GetAttributeType(*this, input_type)};
         const Id id{DefineInput(*this, type, true)};
         Decorate(id, spv::Decoration::Location, static_cast<u32>(index));
@@ -1427,30 +1337,6 @@ void EmitContext::DefineInputs(const IR::Program& program) {
             break;
         }
     }
-    size_t previous_unused_location = 0;
-    if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
-        input_front_color = DefineLegacyInput(*this, used_locations, previous_unused_location);
-    }
-    if (loads.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
-        input_front_secondary_color =
-            DefineLegacyInput(*this, used_locations, previous_unused_location);
-    }
-    if (loads.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
-        input_back_color = DefineLegacyInput(*this, used_locations, previous_unused_location);
-    }
-    if (loads.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
-        input_back_secondary_color =
-            DefineLegacyInput(*this, used_locations, previous_unused_location);
-    }
-    if (loads.AnyComponent(IR::Attribute::FogCoordinate)) {
-        input_fog_frag_coord = DefineLegacyInput(*this, used_locations, previous_unused_location);
-    }
-    for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
-        if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
-            input_fixed_fnc_textures[index] =
-                DefineLegacyInput(*this, used_locations, previous_unused_location);
-        }
-    }
     if (stage == Stage::TessellationEval) {
         for (size_t index = 0; index < info.uses_patches.size(); ++index) {
             if (!info.uses_patches[index]) {
@@ -1501,38 +1387,9 @@ void EmitContext::DefineOutputs(const IR::Program& program) {
         viewport_mask = DefineOutput(*this, TypeArray(U32[1], Const(1u)), std::nullopt,
                                      spv::BuiltIn::ViewportMaskNV);
     }
-    std::bitset<IR::NUM_GENERICS> used_locations{};
     for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
         if (info.stores.Generic(index)) {
             DefineGenericOutput(*this, index, invocations);
-            used_locations.set(index);
-        }
-    }
-    size_t previous_unused_location = 0;
-    if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
-        output_front_color =
-            DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
-    }
-    if (info.stores.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
-        output_front_secondary_color =
-            DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
-    }
-    if (info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
-        output_back_color =
-            DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
-    }
-    if (info.stores.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
-        output_back_secondary_color =
-            DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
-    }
-    if (info.stores.AnyComponent(IR::Attribute::FogCoordinate)) {
-        output_fog_frag_coord =
-            DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
-    }
-    for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
-        if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
-            output_fixed_fnc_textures[index] =
-                DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
         }
     }
     switch (stage) {
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.h b/src/shader_recompiler/backend/spirv/spirv_emit_context.h
index 63f8185d99..f87138f7e7 100644
--- a/src/shader_recompiler/backend/spirv/spirv_emit_context.h
+++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.h
@@ -113,9 +113,6 @@ public:
     [[nodiscard]] Id BitOffset8(const IR::Value& offset);
     [[nodiscard]] Id BitOffset16(const IR::Value& offset);
 
-    Id InputLegacyAttribute(IR::Attribute attribute);
-    Id OutputLegacyAttribute(IR::Attribute attribute);
-
     Id Const(u32 value) {
         return Constant(U32[1], value);
     }
@@ -281,22 +278,10 @@ public:
     Id write_global_func_u32x4{};
 
     Id input_position{};
-    Id input_front_color{};
-    Id input_front_secondary_color{};
-    Id input_back_color{};
-    Id input_back_secondary_color{};
-    Id input_fog_frag_coord{};
-    std::array<Id, 10> input_fixed_fnc_textures{};
     std::array<Id, 32> input_generics{};
 
     Id output_point_size{};
     Id output_position{};
-    Id output_front_color{};
-    Id output_front_secondary_color{};
-    Id output_back_color{};
-    Id output_back_secondary_color{};
-    Id output_fog_frag_coord{};
-    std::array<Id, 10> output_fixed_fnc_textures{};
     std::array<std::array<GenericElementInfo, 4>, 32> output_generics{};
 
     Id output_tess_level_outer{};
diff --git a/src/shader_recompiler/frontend/ir/attribute.h b/src/shader_recompiler/frontend/ir/attribute.h
index 5c5e7bb4ba..3bbd38a03d 100644
--- a/src/shader_recompiler/frontend/ir/attribute.h
+++ b/src/shader_recompiler/frontend/ir/attribute.h
@@ -224,7 +224,7 @@ enum class Attribute : u64 {
 
 constexpr size_t NUM_GENERICS = 32;
 
-constexpr size_t NUM_FIXED_FNC_TEXTURES = 10;
+constexpr size_t NUM_FIXEDFNCTEXTURE = 10;
 
 [[nodiscard]] bool IsGeneric(Attribute attribute) noexcept;
 

From 4908a07c20f98f4b7dd604d1fc6865b47bc5c182 Mon Sep 17 00:00:00 2001
From: vonchenplus <vonchenplus@gmail.com>
Date: Sat, 18 Dec 2021 14:15:34 +0800
Subject: [PATCH 4/4] Address format clang

---
 .../frontend/maxwell/translate_program.cpp    | 72 +++++++++----------
 .../renderer_opengl/gl_shader_cache.cpp       |  2 +-
 .../renderer_vulkan/vk_pipeline_cache.cpp     |  2 +-
 3 files changed, 38 insertions(+), 38 deletions(-)

diff --git a/src/shader_recompiler/frontend/maxwell/translate_program.cpp b/src/shader_recompiler/frontend/maxwell/translate_program.cpp
index 40c9307dbd..248ad3ced5 100644
--- a/src/shader_recompiler/frontend/maxwell/translate_program.cpp
+++ b/src/shader_recompiler/frontend/maxwell/translate_program.cpp
@@ -128,6 +128,42 @@ void AddNVNStorageBuffers(IR::Program& program) {
         });
     }
 }
+
+bool IsLegacyAttribute(IR::Attribute attribute) {
+    return (attribute >= IR::Attribute::ColorFrontDiffuseR &&
+            attribute <= IR::Attribute::ColorBackSpecularA) ||
+           attribute == IR::Attribute::FogCoordinate ||
+           (attribute >= IR::Attribute::FixedFncTexture0S &&
+            attribute <= IR::Attribute::FixedFncTexture9Q);
+}
+
+std::map<IR::Attribute, IR::Attribute> GenerateLegacyToGenericMappings(
+    const VaryingState& state, std::queue<IR::Attribute> ununsed_generics) {
+    std::map<IR::Attribute, IR::Attribute> mapping;
+    for (size_t index = 0; index < 4; ++index) {
+        auto attr = IR::Attribute::ColorFrontDiffuseR + index * 4;
+        if (state.AnyComponent(attr)) {
+            for (size_t i = 0; i < 4; ++i) {
+                mapping.insert({attr + i, ununsed_generics.front() + i});
+            }
+            ununsed_generics.pop();
+        }
+    }
+    if (state[IR::Attribute::FogCoordinate]) {
+        mapping.insert({IR::Attribute::FogCoordinate, ununsed_generics.front()});
+        ununsed_generics.pop();
+    }
+    for (size_t index = 0; index < IR::NUM_FIXEDFNCTEXTURE; ++index) {
+        auto attr = IR::Attribute::FixedFncTexture0S + index * 4;
+        if (state.AnyComponent(attr)) {
+            for (size_t i = 0; i < 4; ++i) {
+                mapping.insert({attr + i, ununsed_generics.front() + i});
+            }
+            ununsed_generics.pop();
+        }
+    }
+    return mapping;
+}
 } // Anonymous namespace
 
 IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
@@ -227,42 +263,6 @@ IR::Program MergeDualVertexPrograms(IR::Program& vertex_a, IR::Program& vertex_b
     return result;
 }
 
-bool IsLegacyAttribute(IR::Attribute attribute) {
-    return (attribute >= IR::Attribute::ColorFrontDiffuseR &&
-            attribute <= IR::Attribute::ColorBackSpecularA) ||
-           attribute == IR::Attribute::FogCoordinate ||
-           (attribute >= IR::Attribute::FixedFncTexture0S &&
-            attribute <= IR::Attribute::FixedFncTexture9Q);
-}
-
-std::map<IR::Attribute, IR::Attribute> GenerateLegacyToGenericMappings(
-    const VaryingState& state, std::queue<IR::Attribute> ununsed_generics) {
-    std::map<IR::Attribute, IR::Attribute> mapping;
-    for (size_t index = 0; index < 4; ++index) {
-        auto attr = IR::Attribute::ColorFrontDiffuseR + index * 4;
-        if (state.AnyComponent(attr)) {
-            for (size_t i = 0; i < 4; ++i) {
-                mapping.insert({attr + i, ununsed_generics.front() + i});
-            }
-            ununsed_generics.pop();
-        }
-    }
-    if (state[IR::Attribute::FogCoordinate]) {
-        mapping.insert({IR::Attribute::FogCoordinate, ununsed_generics.front()});
-        ununsed_generics.pop();
-    }
-    for (size_t index = 0; index < IR::NUM_FIXED_FNC_TEXTURES; ++index) {
-        auto attr = IR::Attribute::FixedFncTexture0S + index * 4;
-        if (state.AnyComponent(attr)) {
-            for (size_t i = 0; i < 4; ++i) {
-                mapping.insert({attr + i, ununsed_generics.front() + i});
-            }
-            ununsed_generics.pop();
-        }
-    }
-    return mapping;
-}
-
 void ConvertLegacyToGeneric(IR::Program& program, const Shader::RuntimeInfo& runtime_info) {
     auto& stores = program.info.stores;
     if (stores.Legacy()) {
diff --git a/src/video_core/renderer_opengl/gl_shader_cache.cpp b/src/video_core/renderer_opengl/gl_shader_cache.cpp
index 310cda6d8c..29c6e1a5f3 100644
--- a/src/video_core/renderer_opengl/gl_shader_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_shader_cache.cpp
@@ -42,9 +42,9 @@ namespace {
 using Shader::Backend::GLASM::EmitGLASM;
 using Shader::Backend::GLSL::EmitGLSL;
 using Shader::Backend::SPIRV::EmitSPIRV;
+using Shader::Maxwell::ConvertLegacyToGeneric;
 using Shader::Maxwell::MergeDualVertexPrograms;
 using Shader::Maxwell::TranslateProgram;
-using Shader::Maxwell::ConvertLegacyToGeneric;
 using VideoCommon::ComputeEnvironment;
 using VideoCommon::FileEnvironment;
 using VideoCommon::GenericEnvironment;
diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
index 5efa4feb4b..2728353c8a 100644
--- a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
+++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
@@ -48,9 +48,9 @@ MICROPROFILE_DECLARE(Vulkan_PipelineCache);
 
 namespace {
 using Shader::Backend::SPIRV::EmitSPIRV;
+using Shader::Maxwell::ConvertLegacyToGeneric;
 using Shader::Maxwell::MergeDualVertexPrograms;
 using Shader::Maxwell::TranslateProgram;
-using Shader::Maxwell::ConvertLegacyToGeneric;
 using VideoCommon::ComputeEnvironment;
 using VideoCommon::FileEnvironment;
 using VideoCommon::GenericEnvironment;