xmad.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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::Instruction;
  10. using Tegra::Shader::OpCode;
  11. u32 ShaderIR::DecodeXmad(NodeBlock& bb, u32 pc) {
  12. const Instruction instr = {program_code[pc]};
  13. const auto opcode = OpCode::Decode(instr);
  14. UNIMPLEMENTED_IF(instr.xmad.sign_a);
  15. UNIMPLEMENTED_IF(instr.xmad.sign_b);
  16. UNIMPLEMENTED_IF_MSG(instr.generates_cc,
  17. "Condition codes generation in XMAD is not implemented");
  18. Node op_a = GetRegister(instr.gpr8);
  19. // TODO(bunnei): Needs to be fixed once op_a or op_b is signed
  20. UNIMPLEMENTED_IF(instr.xmad.sign_a != instr.xmad.sign_b);
  21. const bool is_signed_a = instr.xmad.sign_a == 1;
  22. const bool is_signed_b = instr.xmad.sign_b == 1;
  23. const bool is_signed_c = is_signed_a;
  24. auto [is_merge, is_psl, is_high_b, mode, op_b,
  25. op_c] = [&]() -> std::tuple<bool, bool, bool, Tegra::Shader::XmadMode, Node, Node> {
  26. switch (opcode->get().GetId()) {
  27. case OpCode::Id::XMAD_CR:
  28. return {instr.xmad.merge_56,
  29. instr.xmad.product_shift_left_second,
  30. instr.xmad.high_b,
  31. instr.xmad.mode_cbf,
  32. GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset()),
  33. GetRegister(instr.gpr39)};
  34. case OpCode::Id::XMAD_RR:
  35. return {instr.xmad.merge_37, instr.xmad.product_shift_left, instr.xmad.high_b_rr,
  36. instr.xmad.mode, GetRegister(instr.gpr20), GetRegister(instr.gpr39)};
  37. case OpCode::Id::XMAD_RC:
  38. return {false,
  39. false,
  40. instr.xmad.high_b,
  41. instr.xmad.mode_cbf,
  42. GetRegister(instr.gpr39),
  43. GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset())};
  44. case OpCode::Id::XMAD_IMM:
  45. return {instr.xmad.merge_37,
  46. instr.xmad.product_shift_left,
  47. false,
  48. instr.xmad.mode,
  49. Immediate(static_cast<u32>(instr.xmad.imm20_16)),
  50. GetRegister(instr.gpr39)};
  51. }
  52. UNIMPLEMENTED_MSG("Unhandled XMAD instruction: {}", opcode->get().GetName());
  53. return {false, false, false, Tegra::Shader::XmadMode::None, Immediate(0), Immediate(0)};
  54. }();
  55. op_a = BitfieldExtract(op_a, instr.xmad.high_a ? 16 : 0, 16);
  56. const Node original_b = op_b;
  57. op_b = BitfieldExtract(op_b, is_high_b ? 16 : 0, 16);
  58. // TODO(Rodrigo): Use an appropiate sign for this operation
  59. Node product = Operation(OperationCode::IMul, NO_PRECISE, op_a, op_b);
  60. if (is_psl) {
  61. product = Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, product, Immediate(16));
  62. }
  63. SetTemporal(bb, 0, product);
  64. product = GetTemporal(0);
  65. const Node original_c = op_c;
  66. const Tegra::Shader::XmadMode set_mode = mode; // Workaround to clang compile error
  67. op_c = [&]() {
  68. switch (set_mode) {
  69. case Tegra::Shader::XmadMode::None:
  70. return original_c;
  71. case Tegra::Shader::XmadMode::CLo:
  72. return BitfieldExtract(original_c, 0, 16);
  73. case Tegra::Shader::XmadMode::CHi:
  74. return BitfieldExtract(original_c, 16, 16);
  75. case Tegra::Shader::XmadMode::CBcc: {
  76. const Node shifted_b = SignedOperation(OperationCode::ILogicalShiftLeft, is_signed_b,
  77. NO_PRECISE, original_b, Immediate(16));
  78. return SignedOperation(OperationCode::IAdd, is_signed_c, NO_PRECISE, original_c,
  79. shifted_b);
  80. }
  81. default:
  82. UNIMPLEMENTED_MSG("Unhandled XMAD mode: {}", static_cast<u32>(instr.xmad.mode.Value()));
  83. return Immediate(0);
  84. }
  85. }();
  86. SetTemporal(bb, 1, op_c);
  87. op_c = GetTemporal(1);
  88. // TODO(Rodrigo): Use an appropiate sign for this operation
  89. Node sum = Operation(OperationCode::IAdd, product, op_c);
  90. SetTemporal(bb, 2, sum);
  91. sum = GetTemporal(2);
  92. if (is_merge) {
  93. const Node a = BitfieldExtract(sum, 0, 16);
  94. const Node b =
  95. Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, original_b, Immediate(16));
  96. sum = Operation(OperationCode::IBitwiseOr, NO_PRECISE, a, b);
  97. }
  98. SetInternalFlagsFromInteger(bb, sum, instr.generates_cc);
  99. SetRegister(bb, instr.gpr0, sum);
  100. return pc;
  101. }
  102. } // namespace VideoCommon::Shader