arithmetic_half.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "common/logging/log.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::HalfType;
  12. using Tegra::Shader::Instruction;
  13. using Tegra::Shader::OpCode;
  14. u32 ShaderIR::DecodeArithmeticHalf(NodeBlock& bb, u32 pc) {
  15. const Instruction instr = {program_code[pc]};
  16. const auto opcode = OpCode::Decode(instr);
  17. bool negate_a = false;
  18. bool negate_b = false;
  19. bool absolute_a = false;
  20. bool absolute_b = false;
  21. switch (opcode->get().GetId()) {
  22. case OpCode::Id::HADD2_R:
  23. if (instr.alu_half.ftz == 0) {
  24. LOG_DEBUG(HW_GPU, "{} without FTZ is not implemented", opcode->get().GetName());
  25. }
  26. negate_a = ((instr.value >> 43) & 1) != 0;
  27. negate_b = ((instr.value >> 31) & 1) != 0;
  28. absolute_a = ((instr.value >> 44) & 1) != 0;
  29. absolute_b = ((instr.value >> 30) & 1) != 0;
  30. break;
  31. case OpCode::Id::HADD2_C:
  32. if (instr.alu_half.ftz == 0) {
  33. LOG_DEBUG(HW_GPU, "{} without FTZ is not implemented", opcode->get().GetName());
  34. }
  35. negate_a = ((instr.value >> 43) & 1) != 0;
  36. negate_b = ((instr.value >> 56) & 1) != 0;
  37. absolute_a = ((instr.value >> 44) & 1) != 0;
  38. absolute_b = ((instr.value >> 54) & 1) != 0;
  39. break;
  40. case OpCode::Id::HMUL2_R:
  41. negate_a = ((instr.value >> 43) & 1) != 0;
  42. absolute_a = ((instr.value >> 44) & 1) != 0;
  43. absolute_b = ((instr.value >> 30) & 1) != 0;
  44. break;
  45. case OpCode::Id::HMUL2_C:
  46. negate_b = ((instr.value >> 31) & 1) != 0;
  47. absolute_a = ((instr.value >> 44) & 1) != 0;
  48. absolute_b = ((instr.value >> 54) & 1) != 0;
  49. break;
  50. default:
  51. UNREACHABLE();
  52. break;
  53. }
  54. Node op_a = UnpackHalfFloat(GetRegister(instr.gpr8), instr.alu_half.type_a);
  55. op_a = GetOperandAbsNegHalf(op_a, absolute_a, negate_a);
  56. auto [type_b, op_b] = [this, instr, opcode]() -> std::pair<HalfType, Node> {
  57. switch (opcode->get().GetId()) {
  58. case OpCode::Id::HADD2_C:
  59. case OpCode::Id::HMUL2_C:
  60. return {HalfType::F32, GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset())};
  61. case OpCode::Id::HADD2_R:
  62. case OpCode::Id::HMUL2_R:
  63. return {instr.alu_half.type_b, GetRegister(instr.gpr20)};
  64. default:
  65. UNREACHABLE();
  66. return {HalfType::F32, Immediate(0)};
  67. }
  68. }();
  69. op_b = UnpackHalfFloat(op_b, type_b);
  70. op_b = GetOperandAbsNegHalf(op_b, absolute_b, negate_b);
  71. Node value = [this, opcode, op_a, op_b = op_b] {
  72. switch (opcode->get().GetId()) {
  73. case OpCode::Id::HADD2_C:
  74. case OpCode::Id::HADD2_R:
  75. return Operation(OperationCode::HAdd, PRECISE, op_a, op_b);
  76. case OpCode::Id::HMUL2_C:
  77. case OpCode::Id::HMUL2_R:
  78. return Operation(OperationCode::HMul, PRECISE, op_a, op_b);
  79. default:
  80. UNIMPLEMENTED_MSG("Unhandled half float instruction: {}", opcode->get().GetName());
  81. return Immediate(0);
  82. }
  83. }();
  84. value = GetSaturatedHalfFloat(value, instr.alu_half.saturate);
  85. value = HalfMerge(GetRegister(instr.gpr0), value, instr.alu_half.merge);
  86. SetRegister(bb, instr.gpr0, value);
  87. return pc;
  88. }
  89. } // namespace VideoCommon::Shader