other.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/shader_ir.h"
  8. namespace VideoCommon::Shader {
  9. using Tegra::Shader::ConditionCode;
  10. using Tegra::Shader::Instruction;
  11. using Tegra::Shader::OpCode;
  12. using Tegra::Shader::Register;
  13. u32 ShaderIR::DecodeOther(BasicBlock& bb, u32 pc) {
  14. const Instruction instr = {program_code[pc]};
  15. const auto opcode = OpCode::Decode(instr);
  16. switch (opcode->get().GetId()) {
  17. case OpCode::Id::EXIT: {
  18. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  19. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "EXIT condition code used: {}",
  20. static_cast<u32>(cc));
  21. switch (instr.flow.cond) {
  22. case Tegra::Shader::FlowCondition::Always:
  23. bb.push_back(Operation(OperationCode::Exit));
  24. if (instr.pred.pred_index == static_cast<u64>(Tegra::Shader::Pred::UnusedIndex)) {
  25. // If this is an unconditional exit then just end processing here,
  26. // otherwise we have to account for the possibility of the condition
  27. // not being met, so continue processing the next instruction.
  28. pc = MAX_PROGRAM_LENGTH - 1;
  29. }
  30. break;
  31. case Tegra::Shader::FlowCondition::Fcsm_Tr:
  32. // TODO(bunnei): What is this used for? If we assume this conditon is not
  33. // satisifed, dual vertex shaders in Farming Simulator make more sense
  34. UNIMPLEMENTED_MSG("Skipping unknown FlowCondition::Fcsm_Tr");
  35. break;
  36. default:
  37. UNIMPLEMENTED_MSG("Unhandled flow condition: {}",
  38. static_cast<u32>(instr.flow.cond.Value()));
  39. }
  40. break;
  41. }
  42. case OpCode::Id::KIL: {
  43. UNIMPLEMENTED_IF(instr.flow.cond != Tegra::Shader::FlowCondition::Always);
  44. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  45. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "KIL condition code used: {}",
  46. static_cast<u32>(cc));
  47. bb.push_back(Operation(OperationCode::Kil));
  48. break;
  49. }
  50. case OpCode::Id::MOV_SYS: {
  51. switch (instr.sys20) {
  52. case Tegra::Shader::SystemVariable::InvocationInfo: {
  53. LOG_WARNING(HW_GPU, "MOV_SYS instruction with InvocationInfo is incomplete");
  54. SetRegister(bb, instr.gpr0, Immediate(0u));
  55. break;
  56. }
  57. case Tegra::Shader::SystemVariable::Ydirection: {
  58. // Config pack's third value is Y_NEGATE's state.
  59. SetRegister(bb, instr.gpr0, Operation(OperationCode::YNegate));
  60. break;
  61. }
  62. default:
  63. UNIMPLEMENTED_MSG("Unhandled system move: {}", static_cast<u32>(instr.sys20.Value()));
  64. }
  65. break;
  66. }
  67. case OpCode::Id::BRA: {
  68. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  69. "BRA with constant buffers are not implemented");
  70. const u32 target = pc + instr.bra.GetBranchTarget();
  71. const Node branch = Operation(OperationCode::Bra, Immediate(target));
  72. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  73. if (cc != Tegra::Shader::ConditionCode::T) {
  74. bb.push_back(Conditional(GetConditionCode(cc), {branch}));
  75. } else {
  76. bb.push_back(branch);
  77. }
  78. break;
  79. }
  80. case OpCode::Id::SSY: {
  81. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  82. "Constant buffer flow is not supported");
  83. // The SSY opcode tells the GPU where to re-converge divergent execution paths, it sets the
  84. // target of the jump that the SYNC instruction will make. The SSY opcode has a similar
  85. // structure to the BRA opcode.
  86. const u32 target = pc + instr.bra.GetBranchTarget();
  87. bb.push_back(Operation(OperationCode::Ssy, Immediate(target)));
  88. break;
  89. }
  90. case OpCode::Id::PBK: {
  91. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  92. "Constant buffer PBK is not supported");
  93. // PBK pushes to a stack the address where BRK will jump to. This shares stack with SSY but
  94. // using SYNC on a PBK address will kill the shader execution. We don't emulate this because
  95. // it's very unlikely a driver will emit such invalid shader.
  96. const u32 target = pc + instr.bra.GetBranchTarget();
  97. bb.push_back(Operation(OperationCode::Pbk, Immediate(target)));
  98. break;
  99. }
  100. case OpCode::Id::SYNC: {
  101. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  102. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "SYNC condition code used: {}",
  103. static_cast<u32>(cc));
  104. // The SYNC opcode jumps to the address previously set by the SSY opcode
  105. bb.push_back(Operation(OperationCode::Sync));
  106. break;
  107. }
  108. case OpCode::Id::BRK: {
  109. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  110. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "BRK condition code used: {}",
  111. static_cast<u32>(cc));
  112. // The BRK opcode jumps to the address previously set by the PBK opcode
  113. bb.push_back(Operation(OperationCode::Brk));
  114. break;
  115. }
  116. case OpCode::Id::IPA: {
  117. const auto& attribute = instr.attribute.fmt28;
  118. const Tegra::Shader::IpaMode input_mode{instr.ipa.interp_mode.Value(),
  119. instr.ipa.sample_mode.Value()};
  120. const Node input_attr = GetInputAttribute(attribute.index, attribute.element, input_mode);
  121. const Node ipa = Operation(OperationCode::Ipa, input_attr);
  122. const Node value = GetSaturatedFloat(ipa, instr.ipa.saturate);
  123. SetRegister(bb, instr.gpr0, value);
  124. break;
  125. }
  126. case OpCode::Id::OUT_R: {
  127. UNIMPLEMENTED_IF_MSG(instr.gpr20.Value() != Register::ZeroIndex,
  128. "Stream buffer is not supported");
  129. if (instr.out.emit) {
  130. // gpr0 is used to store the next address and gpr8 contains the address to emit.
  131. // Hardware uses pointers here but we just ignore it
  132. bb.push_back(Operation(OperationCode::EmitVertex));
  133. SetRegister(bb, instr.gpr0, Immediate(0));
  134. }
  135. if (instr.out.cut) {
  136. bb.push_back(Operation(OperationCode::EndPrimitive));
  137. }
  138. break;
  139. }
  140. case OpCode::Id::ISBERD: {
  141. UNIMPLEMENTED_IF(instr.isberd.o != 0);
  142. UNIMPLEMENTED_IF(instr.isberd.skew != 0);
  143. UNIMPLEMENTED_IF(instr.isberd.shift != Tegra::Shader::IsberdShift::None);
  144. UNIMPLEMENTED_IF(instr.isberd.mode != Tegra::Shader::IsberdMode::None);
  145. LOG_WARNING(HW_GPU, "ISBERD instruction is incomplete");
  146. SetRegister(bb, instr.gpr0, GetRegister(instr.gpr8));
  147. break;
  148. }
  149. case OpCode::Id::DEPBAR: {
  150. LOG_WARNING(HW_GPU, "DEPBAR instruction is stubbed");
  151. break;
  152. }
  153. default:
  154. UNIMPLEMENTED_MSG("Unhandled instruction: {}", opcode->get().GetName());
  155. }
  156. return pc;
  157. }
  158. } // namespace VideoCommon::Shader