arithmetic_integer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/assert.h"
  5. #include "common/common_types.h"
  6. #include "video_core/engines/shader_bytecode.h"
  7. #include "video_core/shader/shader_ir.h"
  8. namespace VideoCommon::Shader {
  9. using Tegra::Shader::IAdd3Height;
  10. using Tegra::Shader::Instruction;
  11. using Tegra::Shader::OpCode;
  12. using Tegra::Shader::Pred;
  13. using Tegra::Shader::Register;
  14. u32 ShaderIR::DecodeArithmeticInteger(BasicBlock& bb, u32 pc) {
  15. const Instruction instr = {program_code[pc]};
  16. const auto opcode = OpCode::Decode(instr);
  17. Node op_a = GetRegister(instr.gpr8);
  18. Node op_b = [&]() {
  19. if (instr.is_b_imm) {
  20. return Immediate(instr.alu.GetSignedImm20_20());
  21. } else if (instr.is_b_gpr) {
  22. return GetRegister(instr.gpr20);
  23. } else {
  24. return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.offset);
  25. }
  26. }();
  27. switch (opcode->get().GetId()) {
  28. case OpCode::Id::IADD_C:
  29. case OpCode::Id::IADD_R:
  30. case OpCode::Id::IADD_IMM: {
  31. UNIMPLEMENTED_IF_MSG(instr.generates_cc,
  32. "Condition codes generation in IADD is not implemented");
  33. UNIMPLEMENTED_IF_MSG(instr.alu.saturate_d, "IADD saturation not implemented");
  34. op_a = GetOperandAbsNegInteger(op_a, false, instr.alu_integer.negate_a, true);
  35. op_b = GetOperandAbsNegInteger(op_b, false, instr.alu_integer.negate_b, true);
  36. SetRegister(bb, instr.gpr0, Operation(OperationCode::IAdd, PRECISE, op_a, op_b));
  37. break;
  38. }
  39. case OpCode::Id::IADD3_C:
  40. case OpCode::Id::IADD3_R:
  41. case OpCode::Id::IADD3_IMM: {
  42. UNIMPLEMENTED_IF_MSG(instr.generates_cc,
  43. "Condition codes generation in IADD3 is not implemented");
  44. Node op_c = GetRegister(instr.gpr39);
  45. const auto ApplyHeight = [&](IAdd3Height height, Node value) {
  46. switch (height) {
  47. case IAdd3Height::None:
  48. return value;
  49. case IAdd3Height::LowerHalfWord:
  50. return Operation(OperationCode::IBitwiseAnd, NO_PRECISE, value, Immediate(0xffff));
  51. case IAdd3Height::UpperHalfWord:
  52. return Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, value,
  53. Immediate(16));
  54. default:
  55. UNIMPLEMENTED_MSG("Unhandled IADD3 height: {}", static_cast<u32>(height));
  56. return Immediate(0);
  57. }
  58. };
  59. if (opcode->get().GetId() == OpCode::Id::IADD3_R) {
  60. op_a = ApplyHeight(instr.iadd3.height_a, op_a);
  61. op_b = ApplyHeight(instr.iadd3.height_b, op_b);
  62. op_c = ApplyHeight(instr.iadd3.height_c, op_c);
  63. }
  64. op_a = GetOperandAbsNegInteger(op_a, false, instr.iadd3.neg_a, true);
  65. op_b = GetOperandAbsNegInteger(op_b, false, instr.iadd3.neg_b, true);
  66. op_c = GetOperandAbsNegInteger(op_c, false, instr.iadd3.neg_c, true);
  67. const Node value = [&]() {
  68. const Node add_ab = Operation(OperationCode::IAdd, NO_PRECISE, op_a, op_b);
  69. if (opcode->get().GetId() != OpCode::Id::IADD3_R) {
  70. return Operation(OperationCode::IAdd, NO_PRECISE, add_ab, op_c);
  71. }
  72. const Node shifted = [&]() {
  73. switch (instr.iadd3.mode) {
  74. case Tegra::Shader::IAdd3Mode::RightShift:
  75. // TODO(tech4me): According to
  76. // https://envytools.readthedocs.io/en/latest/hw/graph/maxwell/cuda/int.html?highlight=iadd3
  77. // The addition between op_a and op_b should be done in uint33, more
  78. // investigation required
  79. return Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, add_ab,
  80. Immediate(16));
  81. case Tegra::Shader::IAdd3Mode::LeftShift:
  82. return Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, add_ab,
  83. Immediate(16));
  84. default:
  85. return add_ab;
  86. }
  87. }();
  88. return Operation(OperationCode::IAdd, NO_PRECISE, shifted, op_c);
  89. }();
  90. SetRegister(bb, instr.gpr0, value);
  91. break;
  92. }
  93. case OpCode::Id::ISCADD_C:
  94. case OpCode::Id::ISCADD_R:
  95. case OpCode::Id::ISCADD_IMM: {
  96. UNIMPLEMENTED_IF_MSG(instr.generates_cc,
  97. "Condition codes generation in ISCADD is not implemented");
  98. op_a = GetOperandAbsNegInteger(op_a, false, instr.alu_integer.negate_a, true);
  99. op_b = GetOperandAbsNegInteger(op_b, false, instr.alu_integer.negate_b, true);
  100. const Node shift = Immediate(static_cast<u32>(instr.alu_integer.shift_amount));
  101. const Node shifted_a = Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, op_a, shift);
  102. const Node value = Operation(OperationCode::IAdd, NO_PRECISE, shifted_a, op_b);
  103. SetRegister(bb, instr.gpr0, value);
  104. break;
  105. }
  106. case OpCode::Id::POPC_C:
  107. case OpCode::Id::POPC_R:
  108. case OpCode::Id::POPC_IMM: {
  109. if (instr.popc.invert) {
  110. op_b = Operation(OperationCode::IBitwiseNot, NO_PRECISE, op_b);
  111. }
  112. const Node value = Operation(OperationCode::IBitCount, PRECISE, op_b);
  113. SetRegister(bb, instr.gpr0, value);
  114. break;
  115. }
  116. case OpCode::Id::SEL_C:
  117. case OpCode::Id::SEL_R:
  118. case OpCode::Id::SEL_IMM: {
  119. const Node condition = GetPredicate(instr.sel.pred, instr.sel.neg_pred != 0);
  120. const Node value = Operation(OperationCode::Select, PRECISE, condition, op_a, op_b);
  121. SetRegister(bb, instr.gpr0, value);
  122. break;
  123. }
  124. case OpCode::Id::LOP_C:
  125. case OpCode::Id::LOP_R:
  126. case OpCode::Id::LOP_IMM: {
  127. UNIMPLEMENTED_IF_MSG(instr.generates_cc,
  128. "Condition codes generation in LOP is not implemented");
  129. if (instr.alu.lop.invert_a)
  130. op_a = Operation(OperationCode::IBitwiseNot, NO_PRECISE, op_a);
  131. if (instr.alu.lop.invert_b)
  132. op_b = Operation(OperationCode::IBitwiseNot, NO_PRECISE, op_b);
  133. WriteLogicOperation(bb, instr.gpr0, instr.alu.lop.operation, op_a, op_b,
  134. instr.alu.lop.pred_result_mode, instr.alu.lop.pred48);
  135. break;
  136. }
  137. case OpCode::Id::LOP3_C:
  138. case OpCode::Id::LOP3_R:
  139. case OpCode::Id::LOP3_IMM: {
  140. UNIMPLEMENTED_IF_MSG(instr.generates_cc,
  141. "Condition codes generation in LOP3 is not implemented");
  142. const Node op_c = GetRegister(instr.gpr39);
  143. const Node lut = [&]() {
  144. if (opcode->get().GetId() == OpCode::Id::LOP3_R) {
  145. return Immediate(instr.alu.lop3.GetImmLut28());
  146. } else {
  147. return Immediate(instr.alu.lop3.GetImmLut48());
  148. }
  149. }();
  150. WriteLop3Instruction(bb, instr.gpr0, op_a, op_b, op_c, lut);
  151. break;
  152. }
  153. case OpCode::Id::IMNMX_C:
  154. case OpCode::Id::IMNMX_R:
  155. case OpCode::Id::IMNMX_IMM: {
  156. UNIMPLEMENTED_IF(instr.imnmx.exchange != Tegra::Shader::IMinMaxExchange::None);
  157. UNIMPLEMENTED_IF_MSG(instr.generates_cc,
  158. "Condition codes generation in IMNMX is not implemented");
  159. const bool is_signed = instr.imnmx.is_signed;
  160. const Node condition = GetPredicate(instr.imnmx.pred, instr.imnmx.negate_pred != 0);
  161. const Node min = SignedOperation(OperationCode::IMin, is_signed, NO_PRECISE, op_a, op_b);
  162. const Node max = SignedOperation(OperationCode::IMax, is_signed, NO_PRECISE, op_a, op_b);
  163. const Node value = Operation(OperationCode::Select, NO_PRECISE, condition, min, max);
  164. SetRegister(bb, instr.gpr0, value);
  165. break;
  166. }
  167. case OpCode::Id::LEA_R2:
  168. case OpCode::Id::LEA_R1:
  169. case OpCode::Id::LEA_IMM:
  170. case OpCode::Id::LEA_RZ:
  171. case OpCode::Id::LEA_HI: {
  172. const auto [op_a, op_b, op_c] = [&]() -> std::tuple<Node, Node, Node> {
  173. switch (opcode->get().GetId()) {
  174. case OpCode::Id::LEA_R2: {
  175. return {GetRegister(instr.gpr20), GetRegister(instr.gpr39),
  176. Immediate(static_cast<u32>(instr.lea.r2.entry_a))};
  177. }
  178. case OpCode::Id::LEA_R1: {
  179. const bool neg = instr.lea.r1.neg != 0;
  180. return {GetOperandAbsNegInteger(GetRegister(instr.gpr8), false, neg, true),
  181. GetRegister(instr.gpr20),
  182. Immediate(static_cast<u32>(instr.lea.r1.entry_a))};
  183. }
  184. case OpCode::Id::LEA_IMM: {
  185. const bool neg = instr.lea.imm.neg != 0;
  186. return {Immediate(static_cast<u32>(instr.lea.imm.entry_a)),
  187. GetOperandAbsNegInteger(GetRegister(instr.gpr8), false, neg, true),
  188. Immediate(static_cast<u32>(instr.lea.imm.entry_b))};
  189. }
  190. case OpCode::Id::LEA_RZ: {
  191. const bool neg = instr.lea.rz.neg != 0;
  192. return {GetConstBuffer(instr.lea.rz.cb_index, instr.lea.rz.cb_offset),
  193. GetOperandAbsNegInteger(GetRegister(instr.gpr8), false, neg, true),
  194. Immediate(static_cast<u32>(instr.lea.rz.entry_a))};
  195. }
  196. case OpCode::Id::LEA_HI:
  197. default:
  198. UNIMPLEMENTED_MSG("Unhandled LEA subinstruction: {}", opcode->get().GetName());
  199. return {Immediate(static_cast<u32>(instr.lea.imm.entry_a)), GetRegister(instr.gpr8),
  200. Immediate(static_cast<u32>(instr.lea.imm.entry_b))};
  201. }
  202. }();
  203. UNIMPLEMENTED_IF_MSG(instr.lea.pred48 != static_cast<u64>(Pred::UnusedIndex),
  204. "Unhandled LEA Predicate");
  205. const Node shifted_c =
  206. Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, Immediate(1), op_c);
  207. const Node mul_bc = Operation(OperationCode::IMul, NO_PRECISE, op_b, shifted_c);
  208. const Node value = Operation(OperationCode::IAdd, NO_PRECISE, op_a, mul_bc);
  209. SetRegister(bb, instr.gpr0, value);
  210. break;
  211. }
  212. default:
  213. UNIMPLEMENTED_MSG("Unhandled ArithmeticInteger instruction: {}", opcode->get().GetName());
  214. }
  215. return pc;
  216. }
  217. void ShaderIR::WriteLop3Instruction(BasicBlock& bb, Register dest, Node op_a, Node op_b, Node op_c,
  218. Node imm_lut) {
  219. constexpr u32 lop_iterations = 32;
  220. const Node one = Immediate(1);
  221. const Node two = Immediate(2);
  222. Node value{};
  223. for (u32 i = 0; i < lop_iterations; ++i) {
  224. const Node shift_amount = Immediate(i);
  225. const Node a = Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, op_c, shift_amount);
  226. const Node pack_0 = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, a, one);
  227. const Node b = Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, op_b, shift_amount);
  228. const Node c = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, b, one);
  229. const Node pack_1 = Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, c, one);
  230. const Node d = Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, op_a, shift_amount);
  231. const Node e = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, d, one);
  232. const Node pack_2 = Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, e, two);
  233. const Node pack_01 = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, pack_0, pack_1);
  234. const Node pack_012 = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, pack_01, pack_2);
  235. const Node shifted_bit =
  236. Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, imm_lut, pack_012);
  237. const Node bit = Operation(OperationCode::IBitwiseAnd, NO_PRECISE, shifted_bit, one);
  238. const Node right =
  239. Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, bit, shift_amount);
  240. if (i > 0) {
  241. value = Operation(OperationCode::IBitwiseOr, NO_PRECISE, value, right);
  242. } else {
  243. value = right;
  244. }
  245. }
  246. SetRegister(bb, dest, value);
  247. }
  248. } // namespace VideoCommon::Shader