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] 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 ad84966b5..14f470812 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 4b6f792bf..d3ba66569 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 63f8185d9..f87138f7e 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 5c5e7bb4b..3bbd38a03 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;