From d4cb0eac87c9dea121a0f6bd2355fa5e6c641274 Mon Sep 17 00:00:00 2001
From: FengChen <vonchenplus@gmail.com>
Date: Sat, 10 Sep 2022 17:09:45 +0800
Subject: video_core: Fix legacy to generic location unpaired

---
 .../frontend/maxwell/translate_program.cpp         | 39 +++++++++++++---------
 1 file changed, 24 insertions(+), 15 deletions(-)

(limited to 'src/shader_recompiler/frontend/maxwell/translate_program.cpp')

diff --git a/src/shader_recompiler/frontend/maxwell/translate_program.cpp b/src/shader_recompiler/frontend/maxwell/translate_program.cpp
index 77efb4f577..3aee33a961 100644
--- a/src/shader_recompiler/frontend/maxwell/translate_program.cpp
+++ b/src/shader_recompiler/frontend/maxwell/translate_program.cpp
@@ -137,28 +137,35 @@ bool IsLegacyAttribute(IR::Attribute attribute) {
 }
 
 std::map<IR::Attribute, IR::Attribute> GenerateLegacyToGenericMappings(
-    const VaryingState& state, std::queue<IR::Attribute> ununsed_generics) {
+    const VaryingState& state, std::queue<IR::Attribute> unused_generics,
+    const std::map<IR::Attribute, IR::Attribute>& previous_stage_mapping) {
     std::map<IR::Attribute, IR::Attribute> mapping;
+    auto update_mapping = [&mapping, &unused_generics, previous_stage_mapping](IR::Attribute attr,
+                                                                               size_t count) {
+        if (previous_stage_mapping.find(attr) != previous_stage_mapping.end()) {
+            for (size_t i = 0; i < count; ++i) {
+                mapping.insert({attr + i, previous_stage_mapping.at(attr + i)});
+            }
+        } else {
+            for (size_t i = 0; i < count; ++i) {
+                mapping.insert({attr + i, unused_generics.front() + i});
+            }
+            unused_generics.pop();
+        }
+    };
     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();
+            update_mapping(attr, 4);
         }
     }
     if (state[IR::Attribute::FogCoordinate]) {
-        mapping.insert({IR::Attribute::FogCoordinate, ununsed_generics.front()});
-        ununsed_generics.pop();
+        update_mapping(IR::Attribute::FogCoordinate, 1);
     }
     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();
+            update_mapping(attr, 4);
         }
     }
     return mapping;
@@ -271,15 +278,16 @@ void ConvertLegacyToGeneric(IR::Program& program, const Shader::RuntimeInfo& run
                 ununsed_output_generics.push(IR::Attribute::Generic0X + index * 4);
             }
         }
-        auto mappings = GenerateLegacyToGenericMappings(stores, ununsed_output_generics);
+        program.info.legacy_stores_mapping =
+            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]));
+                        stores.Set(program.info.legacy_stores_mapping[attr], true);
+                        inst.SetArg(0, Shader::IR::Value(program.info.legacy_stores_mapping[attr]));
                     }
                     break;
                 }
@@ -300,7 +308,8 @@ void ConvertLegacyToGeneric(IR::Program& program, const Shader::RuntimeInfo& run
                 ununsed_input_generics.push(IR::Attribute::Generic0X + index * 4);
             }
         }
-        auto mappings = GenerateLegacyToGenericMappings(loads, ununsed_input_generics);
+        auto mappings = GenerateLegacyToGenericMappings(
+            loads, ununsed_input_generics, runtime_info.previous_stage_legacy_stores_mapping);
         for (IR::Block* const block : program.post_order_blocks) {
             for (IR::Inst& inst : block->Instructions()) {
                 switch (inst.GetOpcode()) {
-- 
cgit v1.2.3-70-g09d2


From 20139f8a551098633e4ea3eab079b590c47ee0f5 Mon Sep 17 00:00:00 2001
From: FengChen <vonchenplus@gmail.com>
Date: Mon, 17 Oct 2022 09:40:44 +0800
Subject: Address feedback

---
 src/shader_recompiler/frontend/maxwell/translate_program.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

(limited to 'src/shader_recompiler/frontend/maxwell/translate_program.cpp')

diff --git a/src/shader_recompiler/frontend/maxwell/translate_program.cpp b/src/shader_recompiler/frontend/maxwell/translate_program.cpp
index 3aee33a961..b58741d4d0 100644
--- a/src/shader_recompiler/frontend/maxwell/translate_program.cpp
+++ b/src/shader_recompiler/frontend/maxwell/translate_program.cpp
@@ -272,14 +272,14 @@ IR::Program MergeDualVertexPrograms(IR::Program& vertex_a, IR::Program& vertex_b
 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{};
+        std::queue<IR::Attribute> unused_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);
+                unused_output_generics.push(IR::Attribute::Generic0X + index * 4);
             }
         }
         program.info.legacy_stores_mapping =
-            GenerateLegacyToGenericMappings(stores, ununsed_output_generics, {});
+            GenerateLegacyToGenericMappings(stores, unused_output_generics, {});
         for (IR::Block* const block : program.post_order_blocks) {
             for (IR::Inst& inst : block->Instructions()) {
                 switch (inst.GetOpcode()) {
@@ -300,16 +300,16 @@ void ConvertLegacyToGeneric(IR::Program& program, const Shader::RuntimeInfo& run
 
     auto& loads = program.info.loads;
     if (loads.Legacy()) {
-        std::queue<IR::Attribute> ununsed_input_generics{};
+        std::queue<IR::Attribute> unused_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);
+                unused_input_generics.push(IR::Attribute::Generic0X + index * 4);
             }
         }
         auto mappings = GenerateLegacyToGenericMappings(
-            loads, ununsed_input_generics, runtime_info.previous_stage_legacy_stores_mapping);
+            loads, unused_input_generics, runtime_info.previous_stage_legacy_stores_mapping);
         for (IR::Block* const block : program.post_order_blocks) {
             for (IR::Inst& inst : block->Instructions()) {
                 switch (inst.GetOpcode()) {
-- 
cgit v1.2.3-70-g09d2