hfma2.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <tuple>
  5. #include "common/assert.h"
  6. #include "common/common_types.h"
  7. #include "video_core/engines/shader_bytecode.h"
  8. #include "video_core/shader/shader_ir.h"
  9. namespace VideoCommon::Shader {
  10. using Tegra::Shader::HalfPrecision;
  11. using Tegra::Shader::HalfType;
  12. using Tegra::Shader::Instruction;
  13. using Tegra::Shader::OpCode;
  14. u32 ShaderIR::DecodeHfma2(BasicBlock& bb, u32 pc) {
  15. const Instruction instr = {program_code[pc]};
  16. const auto opcode = OpCode::Decode(instr);
  17. if (opcode->get().GetId() == OpCode::Id::HFMA2_RR) {
  18. UNIMPLEMENTED_IF(instr.hfma2.rr.precision != HalfPrecision::None);
  19. } else {
  20. UNIMPLEMENTED_IF(instr.hfma2.precision != HalfPrecision::None);
  21. }
  22. constexpr auto identity = HalfType::H0_H1;
  23. const HalfType type_a = instr.hfma2.type_a;
  24. const Node op_a = GetRegister(instr.gpr8);
  25. bool neg_b{}, neg_c{};
  26. auto [saturate, type_b, op_b, type_c,
  27. op_c] = [&]() -> std::tuple<bool, HalfType, Node, HalfType, Node> {
  28. switch (opcode->get().GetId()) {
  29. case OpCode::Id::HFMA2_CR:
  30. neg_b = instr.hfma2.negate_b;
  31. neg_c = instr.hfma2.negate_c;
  32. return {instr.hfma2.saturate, instr.hfma2.type_b,
  33. GetConstBuffer(instr.cbuf34.index, instr.cbuf34.offset), instr.hfma2.type_reg39,
  34. GetRegister(instr.gpr39)};
  35. case OpCode::Id::HFMA2_RC:
  36. neg_b = instr.hfma2.negate_b;
  37. neg_c = instr.hfma2.negate_c;
  38. return {instr.hfma2.saturate, instr.hfma2.type_reg39, GetRegister(instr.gpr39),
  39. instr.hfma2.type_b, GetConstBuffer(instr.cbuf34.index, instr.cbuf34.offset)};
  40. case OpCode::Id::HFMA2_RR:
  41. neg_b = instr.hfma2.rr.negate_b;
  42. neg_c = instr.hfma2.rr.negate_c;
  43. return {instr.hfma2.rr.saturate, instr.hfma2.type_b, GetRegister(instr.gpr20),
  44. instr.hfma2.rr.type_c, GetRegister(instr.gpr39)};
  45. case OpCode::Id::HFMA2_IMM_R:
  46. neg_c = instr.hfma2.negate_c;
  47. return {instr.hfma2.saturate, identity, UnpackHalfImmediate(instr, true),
  48. instr.hfma2.type_reg39, GetRegister(instr.gpr39)};
  49. default:
  50. return {false, identity, Immediate(0), identity, Immediate(0)};
  51. }
  52. }();
  53. UNIMPLEMENTED_IF_MSG(saturate, "HFMA2 saturation is not implemented");
  54. op_b = GetOperandAbsNegHalf(op_b, false, neg_b);
  55. op_c = GetOperandAbsNegHalf(op_c, false, neg_c);
  56. MetaHalfArithmetic meta{true, {type_a, type_b, type_c}};
  57. Node value = Operation(OperationCode::HFma, meta, op_a, op_b, op_c);
  58. value = HalfMerge(GetRegister(instr.gpr0), value, instr.hfma2.merge);
  59. SetRegister(bb, instr.gpr0, value);
  60. return pc;
  61. }
  62. } // namespace VideoCommon::Shader