video.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/node_helper.h"
  8. #include "video_core/shader/shader_ir.h"
  9. namespace VideoCommon::Shader {
  10. using Tegra::Shader::Instruction;
  11. using Tegra::Shader::OpCode;
  12. using Tegra::Shader::Pred;
  13. using Tegra::Shader::VideoType;
  14. using Tegra::Shader::VmadShr;
  15. u32 ShaderIR::DecodeVideo(NodeBlock& bb, u32 pc) {
  16. const Instruction instr = {program_code[pc]};
  17. const auto opcode = OpCode::Decode(instr);
  18. const Node op_a =
  19. GetVideoOperand(GetRegister(instr.gpr8), instr.video.is_byte_chunk_a, instr.video.signed_a,
  20. instr.video.type_a, instr.video.byte_height_a);
  21. const Node op_b = [this, instr] {
  22. if (instr.video.use_register_b) {
  23. return GetVideoOperand(GetRegister(instr.gpr20), instr.video.is_byte_chunk_b,
  24. instr.video.signed_b, instr.video.type_b,
  25. instr.video.byte_height_b);
  26. }
  27. if (instr.video.signed_b) {
  28. const auto imm = static_cast<s16>(instr.alu.GetImm20_16());
  29. return Immediate(static_cast<u32>(imm));
  30. } else {
  31. return Immediate(instr.alu.GetImm20_16());
  32. }
  33. }();
  34. switch (opcode->get().GetId()) {
  35. case OpCode::Id::VMAD: {
  36. const bool result_signed = instr.video.signed_a == 1 || instr.video.signed_b == 1;
  37. const Node op_c = GetRegister(instr.gpr39);
  38. Node value = SignedOperation(OperationCode::IMul, result_signed, NO_PRECISE, op_a, op_b);
  39. value = SignedOperation(OperationCode::IAdd, result_signed, NO_PRECISE, value, op_c);
  40. if (instr.vmad.shr == VmadShr::Shr7 || instr.vmad.shr == VmadShr::Shr15) {
  41. const Node shift = Immediate(instr.vmad.shr == VmadShr::Shr7 ? 7 : 15);
  42. value =
  43. SignedOperation(OperationCode::IArithmeticShiftRight, result_signed, value, shift);
  44. }
  45. SetInternalFlagsFromInteger(bb, value, instr.generates_cc);
  46. SetRegister(bb, instr.gpr0, value);
  47. break;
  48. }
  49. case OpCode::Id::VSETP: {
  50. // We can't use the constant predicate as destination.
  51. ASSERT(instr.vsetp.pred3 != static_cast<u64>(Pred::UnusedIndex));
  52. const bool sign = instr.video.signed_a == 1 || instr.video.signed_b == 1;
  53. const Node first_pred = GetPredicateComparisonInteger(instr.vsetp.cond, sign, op_a, op_b);
  54. const Node second_pred = GetPredicate(instr.vsetp.pred39, false);
  55. const OperationCode combiner = GetPredicateCombiner(instr.vsetp.op);
  56. // Set the primary predicate to the result of Predicate OP SecondPredicate
  57. SetPredicate(bb, instr.vsetp.pred3, Operation(combiner, first_pred, second_pred));
  58. if (instr.vsetp.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
  59. // Set the secondary predicate to the result of !Predicate OP SecondPredicate,
  60. // if enabled
  61. const Node negate_pred = Operation(OperationCode::LogicalNegate, first_pred);
  62. SetPredicate(bb, instr.vsetp.pred0, Operation(combiner, negate_pred, second_pred));
  63. }
  64. break;
  65. }
  66. default:
  67. UNIMPLEMENTED_MSG("Unhandled video instruction: {}", opcode->get().GetName());
  68. }
  69. return pc;
  70. }
  71. Node ShaderIR::GetVideoOperand(Node op, bool is_chunk, bool is_signed,
  72. Tegra::Shader::VideoType type, u64 byte_height) {
  73. if (!is_chunk) {
  74. return BitfieldExtract(op, static_cast<u32>(byte_height * 8), 8);
  75. }
  76. const Node zero = Immediate(0);
  77. switch (type) {
  78. case Tegra::Shader::VideoType::Size16_Low:
  79. return BitfieldExtract(op, 0, 16);
  80. case Tegra::Shader::VideoType::Size16_High:
  81. return BitfieldExtract(op, 16, 16);
  82. case Tegra::Shader::VideoType::Size32:
  83. // TODO(Rodrigo): From my hardware tests it becomes a bit "mad" when this type is used
  84. // (1 * 1 + 0 == 0x5b800000). Until a better explanation is found: abort.
  85. UNIMPLEMENTED();
  86. return zero;
  87. case Tegra::Shader::VideoType::Invalid:
  88. UNREACHABLE_MSG("Invalid instruction encoding");
  89. return zero;
  90. default:
  91. UNREACHABLE();
  92. return zero;
  93. }
  94. }
  95. } // namespace VideoCommon::Shader