arithmetic_half.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  51. Node op_a = UnpackHalfFloat(GetRegister(instr.gpr8), instr.alu_half.type_a);
  52. op_a = GetOperandAbsNegHalf(op_a, absolute_a, negate_a);
  53. auto [type_b, op_b] = [this, instr, opcode]() -> std::pair<HalfType, Node> {
  54. switch (opcode->get().GetId()) {
  55. case OpCode::Id::HADD2_C:
  56. case OpCode::Id::HMUL2_C:
  57. return {HalfType::F32, GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset())};
  58. case OpCode::Id::HADD2_R:
  59. case OpCode::Id::HMUL2_R:
  60. return {instr.alu_half.type_b, GetRegister(instr.gpr20)};
  61. default:
  62. UNREACHABLE();
  63. return {HalfType::F32, Immediate(0)};
  64. }
  65. }();
  66. op_b = UnpackHalfFloat(op_b, type_b);
  67. op_b = GetOperandAbsNegHalf(op_b, absolute_b, negate_b);
  68. Node value = [this, opcode, op_a, op_b = op_b] {
  69. switch (opcode->get().GetId()) {
  70. case OpCode::Id::HADD2_C:
  71. case OpCode::Id::HADD2_R:
  72. return Operation(OperationCode::HAdd, PRECISE, op_a, op_b);
  73. case OpCode::Id::HMUL2_C:
  74. case OpCode::Id::HMUL2_R:
  75. return Operation(OperationCode::HMul, PRECISE, op_a, op_b);
  76. default:
  77. UNIMPLEMENTED_MSG("Unhandled half float instruction: {}", opcode->get().GetName());
  78. return Immediate(0);
  79. }
  80. }();
  81. value = GetSaturatedHalfFloat(value, instr.alu_half.saturate);
  82. value = HalfMerge(GetRegister(instr.gpr0), value, instr.alu_half.merge);
  83. SetRegister(bb, instr.gpr0, value);
  84. return pc;
  85. }
  86. } // namespace VideoCommon::Shader