From 0c01c34eff9456e1341fcc0f7a048325e6929cd3 Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Sat, 28 Apr 2018 20:01:36 -0400
Subject: [PATCH] gl_shader_decompiler: Partially implement I2I_R, and I2F_R.

---
 src/video_core/engines/shader_bytecode.h      | 16 ++++++------
 .../renderer_opengl/gl_shader_decompiler.cpp  | 26 +++++++++++++++++++
 2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/src/video_core/engines/shader_bytecode.h b/src/video_core/engines/shader_bytecode.h
index 0cbe5e94d5..f20c2cd419 100644
--- a/src/video_core/engines/shader_bytecode.h
+++ b/src/video_core/engines/shader_bytecode.h
@@ -250,7 +250,7 @@ union Instruction {
         BitField<45, 1, u64> negate_a;
         BitField<49, 1, u64> abs_a;
         BitField<50, 1, u64> saturate_a;
-    } i2i;
+    } conversion;
 
     BitField<61, 1, u64> is_b_imm;
     BitField<60, 1, u64> is_b_gpr;
@@ -330,7 +330,7 @@ public:
         FloatSet,
         FloatSetPredicate,
         IntegerSetPredicate,
-        I2I,
+        Conversion,
         Unknown,
     };
 
@@ -452,9 +452,6 @@ private:
             INST("0100110010110---", Id::F2I_C, Type::Arithmetic, "F2I_C"),
             INST("0101110010110---", Id::F2I_R, Type::Arithmetic, "F2I_R"),
             INST("0011100-10110---", Id::F2I_IMM, Type::Arithmetic, "F2I_IMM"),
-            INST("0100110010111---", Id::I2F_C, Type::Arithmetic, "I2F_C"),
-            INST("0101110010111---", Id::I2F_R, Type::Arithmetic, "I2F_R"),
-            INST("0011100-10111---", Id::I2F_IMM, Type::Arithmetic, "I2F_IMM"),
             INST("000001----------", Id::LOP32I, Type::Arithmetic, "LOP32I"),
             INST("0100110010011---", Id::MOV_C, Type::Arithmetic, "MOV_C"),
             INST("0101110010011---", Id::MOV_R, Type::Arithmetic, "MOV_R"),
@@ -463,9 +460,12 @@ private:
             INST("0100110000101---", Id::SHR_C, Type::Arithmetic, "SHR_C"),
             INST("0101110000101---", Id::SHR_R, Type::Arithmetic, "SHR_R"),
             INST("0011100-00101---", Id::SHR_IMM, Type::Arithmetic, "SHR_IMM"),
-            INST("0100110011100---", Id::I2I_C, Type::I2I, "I2I_C"),
-            INST("0101110011100---", Id::I2I_R, Type::I2I, "I2I_R"),
-            INST("01110001-1000---", Id::I2I_IMM, Type::I2I, "I2I_IMM"),
+            INST("0100110011100---", Id::I2I_C, Type::Conversion, "I2I_C"),
+            INST("0101110011100---", Id::I2I_R, Type::Conversion, "I2I_R"),
+            INST("01110001-1000---", Id::I2I_IMM, Type::Conversion, "I2I_IMM"),
+            INST("0100110010111---", Id::I2F_C, Type::Conversion, "I2F_C"),
+            INST("0101110010111---", Id::I2F_R, Type::Conversion, "I2F_R"),
+            INST("0011100-10111---", Id::I2F_IMM, Type::Conversion, "I2F_IMM"),
             INST("01011000--------", Id::FSET_R, Type::FloatSet, "FSET_R"),
             INST("0100100---------", Id::FSET_C, Type::FloatSet, "FSET_C"),
             INST("0011000---------", Id::FSET_IMM, Type::FloatSet, "FSET_IMM"),
diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp
index f90f74a48c..27190cc450 100644
--- a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp
+++ b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp
@@ -770,6 +770,32 @@ private:
             regs.SetRegisterToFloat(instr.gpr0, 0, op_a + " * " + op_b + " + " + op_c, 1, 1);
             break;
         }
+        case OpCode::Type::Conversion: {
+            ASSERT_MSG(instr.conversion.size == Register::Size::Word, "Unimplemented");
+            ASSERT_MSG(!instr.conversion.selector, "Unimplemented");
+            ASSERT_MSG(!instr.conversion.negate_a, "Unimplemented");
+            ASSERT_MSG(!instr.conversion.saturate_a, "Unimplemented");
+
+            switch (opcode->GetId()) {
+            case OpCode::Id::I2I_R:
+            case OpCode::Id::I2F_R: {
+                std::string op_a =
+                    regs.GetRegisterAsInteger(instr.gpr20, 0, instr.conversion.is_signed);
+
+                if (instr.conversion.abs_a) {
+                    op_a = "abs(" + op_a + ')';
+                }
+
+                regs.SetRegisterToInteger(instr.gpr0, instr.conversion.is_signed, 0, op_a, 1, 1);
+                break;
+            }
+            default: {
+                NGLOG_CRITICAL(HW_GPU, "Unhandled conversion instruction: {}", opcode->GetName());
+                UNREACHABLE();
+            }
+            }
+            break;
+        }
         case OpCode::Type::Memory: {
             const Attribute::Index attribute = instr.attribute.fmt20.index;