hfma2.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/node_helper.h"
  9. #include "video_core/shader/shader_ir.h"
  10. namespace VideoCommon::Shader {
  11. using Tegra::Shader::HalfPrecision;
  12. using Tegra::Shader::HalfType;
  13. using Tegra::Shader::Instruction;
  14. using Tegra::Shader::OpCode;
  15. u32 ShaderIR::DecodeHfma2(NodeBlock& bb, u32 pc) {
  16. const Instruction instr = {program_code[pc]};
  17. const auto opcode = OpCode::Decode(instr);
  18. if (opcode->get().GetId() == OpCode::Id::HFMA2_RR) {
  19. DEBUG_ASSERT(instr.hfma2.rr.precision == HalfPrecision::None);
  20. } else {
  21. DEBUG_ASSERT(instr.hfma2.precision == HalfPrecision::None);
  22. }
  23. constexpr auto identity = HalfType::H0_H1;
  24. bool neg_b{}, neg_c{};
  25. auto [saturate, type_b, op_b, type_c,
  26. op_c] = [&]() -> std::tuple<bool, HalfType, Node, HalfType, Node> {
  27. switch (opcode->get().GetId()) {
  28. case OpCode::Id::HFMA2_CR:
  29. neg_b = instr.hfma2.negate_b;
  30. neg_c = instr.hfma2.negate_c;
  31. return {instr.hfma2.saturate, HalfType::F32,
  32. GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset()),
  33. instr.hfma2.type_reg39, GetRegister(instr.gpr39)};
  34. case OpCode::Id::HFMA2_RC:
  35. neg_b = instr.hfma2.negate_b;
  36. neg_c = instr.hfma2.negate_c;
  37. return {instr.hfma2.saturate, instr.hfma2.type_reg39, GetRegister(instr.gpr39),
  38. HalfType::F32, GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset())};
  39. case OpCode::Id::HFMA2_RR:
  40. neg_b = instr.hfma2.rr.negate_b;
  41. neg_c = instr.hfma2.rr.negate_c;
  42. return {instr.hfma2.rr.saturate, instr.hfma2.type_b, GetRegister(instr.gpr20),
  43. instr.hfma2.rr.type_c, GetRegister(instr.gpr39)};
  44. case OpCode::Id::HFMA2_IMM_R:
  45. neg_c = instr.hfma2.negate_c;
  46. return {instr.hfma2.saturate, identity, UnpackHalfImmediate(instr, true),
  47. instr.hfma2.type_reg39, GetRegister(instr.gpr39)};
  48. default:
  49. return {false, identity, Immediate(0), identity, Immediate(0)};
  50. }
  51. }();
  52. const Node op_a = UnpackHalfFloat(GetRegister(instr.gpr8), instr.hfma2.type_a);
  53. op_b = GetOperandAbsNegHalf(UnpackHalfFloat(op_b, type_b), false, neg_b);
  54. op_c = GetOperandAbsNegHalf(UnpackHalfFloat(op_c, type_c), false, neg_c);
  55. Node value = Operation(OperationCode::HFma, PRECISE, op_a, op_b, op_c);
  56. value = GetSaturatedHalfFloat(value, saturate);
  57. value = HalfMerge(GetRegister(instr.gpr0), value, instr.hfma2.merge);
  58. SetRegister(bb, instr.gpr0, value);
  59. return pc;
  60. }
  61. } // namespace VideoCommon::Shader