other.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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::ConditionCode;
  12. using Tegra::Shader::Instruction;
  13. using Tegra::Shader::OpCode;
  14. using Tegra::Shader::Register;
  15. using Tegra::Shader::SystemVariable;
  16. u32 ShaderIR::DecodeOther(NodeBlock& bb, u32 pc) {
  17. const Instruction instr = {program_code[pc]};
  18. const auto opcode = OpCode::Decode(instr);
  19. switch (opcode->get().GetId()) {
  20. case OpCode::Id::EXIT: {
  21. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  22. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "EXIT condition code used: {}",
  23. static_cast<u32>(cc));
  24. switch (instr.flow.cond) {
  25. case Tegra::Shader::FlowCondition::Always:
  26. bb.push_back(Operation(OperationCode::Exit));
  27. if (instr.pred.pred_index == static_cast<u64>(Tegra::Shader::Pred::UnusedIndex)) {
  28. // If this is an unconditional exit then just end processing here,
  29. // otherwise we have to account for the possibility of the condition
  30. // not being met, so continue processing the next instruction.
  31. pc = MAX_PROGRAM_LENGTH - 1;
  32. }
  33. break;
  34. case Tegra::Shader::FlowCondition::Fcsm_Tr:
  35. // TODO(bunnei): What is this used for? If we assume this conditon is not
  36. // satisifed, dual vertex shaders in Farming Simulator make more sense
  37. UNIMPLEMENTED_MSG("Skipping unknown FlowCondition::Fcsm_Tr");
  38. break;
  39. default:
  40. UNIMPLEMENTED_MSG("Unhandled flow condition: {}",
  41. static_cast<u32>(instr.flow.cond.Value()));
  42. }
  43. break;
  44. }
  45. case OpCode::Id::KIL: {
  46. UNIMPLEMENTED_IF(instr.flow.cond != Tegra::Shader::FlowCondition::Always);
  47. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  48. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "KIL condition code used: {}",
  49. static_cast<u32>(cc));
  50. bb.push_back(Operation(OperationCode::Discard));
  51. break;
  52. }
  53. case OpCode::Id::MOV_SYS: {
  54. const Node value = [&]() {
  55. switch (instr.sys20) {
  56. case SystemVariable::Ydirection:
  57. return Operation(OperationCode::YNegate);
  58. case SystemVariable::InvocationInfo:
  59. LOG_WARNING(HW_GPU, "MOV_SYS instruction with InvocationInfo is incomplete");
  60. return Immediate(0u);
  61. case SystemVariable::TidX:
  62. return Operation(OperationCode::LocalInvocationIdX);
  63. case SystemVariable::TidY:
  64. return Operation(OperationCode::LocalInvocationIdY);
  65. case SystemVariable::TidZ:
  66. return Operation(OperationCode::LocalInvocationIdZ);
  67. case SystemVariable::CtaIdX:
  68. return Operation(OperationCode::WorkGroupIdX);
  69. case SystemVariable::CtaIdY:
  70. return Operation(OperationCode::WorkGroupIdY);
  71. case SystemVariable::CtaIdZ:
  72. return Operation(OperationCode::WorkGroupIdZ);
  73. default:
  74. UNIMPLEMENTED_MSG("Unhandled system move: {}",
  75. static_cast<u32>(instr.sys20.Value()));
  76. return Immediate(0u);
  77. }
  78. }();
  79. SetRegister(bb, instr.gpr0, value);
  80. break;
  81. }
  82. case OpCode::Id::BRA: {
  83. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  84. "BRA with constant buffers are not implemented");
  85. const u32 target = pc + instr.bra.GetBranchTarget();
  86. const Node branch = Operation(OperationCode::Branch, Immediate(target));
  87. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  88. if (cc != Tegra::Shader::ConditionCode::T) {
  89. bb.push_back(Conditional(GetConditionCode(cc), {branch}));
  90. } else {
  91. bb.push_back(branch);
  92. }
  93. break;
  94. }
  95. case OpCode::Id::SSY: {
  96. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  97. "Constant buffer flow is not supported");
  98. // The SSY opcode tells the GPU where to re-converge divergent execution paths, it sets the
  99. // target of the jump that the SYNC instruction will make. The SSY opcode has a similar
  100. // structure to the BRA opcode.
  101. const u32 target = pc + instr.bra.GetBranchTarget();
  102. bb.push_back(Operation(OperationCode::PushFlowStack, Immediate(target)));
  103. break;
  104. }
  105. case OpCode::Id::PBK: {
  106. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  107. "Constant buffer PBK is not supported");
  108. // PBK pushes to a stack the address where BRK will jump to. This shares stack with SSY but
  109. // using SYNC on a PBK address will kill the shader execution. We don't emulate this because
  110. // it's very unlikely a driver will emit such invalid shader.
  111. const u32 target = pc + instr.bra.GetBranchTarget();
  112. bb.push_back(Operation(OperationCode::PushFlowStack, Immediate(target)));
  113. break;
  114. }
  115. case OpCode::Id::SYNC: {
  116. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  117. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "SYNC condition code used: {}",
  118. static_cast<u32>(cc));
  119. // The SYNC opcode jumps to the address previously set by the SSY opcode
  120. bb.push_back(Operation(OperationCode::PopFlowStack));
  121. break;
  122. }
  123. case OpCode::Id::BRK: {
  124. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  125. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "BRK condition code used: {}",
  126. static_cast<u32>(cc));
  127. // The BRK opcode jumps to the address previously set by the PBK opcode
  128. bb.push_back(Operation(OperationCode::PopFlowStack));
  129. break;
  130. }
  131. case OpCode::Id::IPA: {
  132. const bool is_physical = instr.ipa.idx && instr.gpr8.Value() != 0xff;
  133. const auto attribute = instr.attribute.fmt28;
  134. const Tegra::Shader::IpaMode input_mode{instr.ipa.interp_mode.Value(),
  135. instr.ipa.sample_mode.Value()};
  136. Node value = is_physical ? GetPhysicalInputAttribute(instr.gpr8)
  137. : GetInputAttribute(attribute.index, attribute.element);
  138. const Tegra::Shader::Attribute::Index index = attribute.index.Value();
  139. const bool is_generic = index >= Tegra::Shader::Attribute::Index::Attribute_0 &&
  140. index <= Tegra::Shader::Attribute::Index::Attribute_31;
  141. if (is_generic || is_physical) {
  142. // TODO(Blinkhawk): There are cases where a perspective attribute use PASS.
  143. // In theory by setting them as perspective, OpenGL does the perspective correction.
  144. // A way must figured to reverse the last step of it.
  145. if (input_mode.interpolation_mode == Tegra::Shader::IpaInterpMode::Multiply) {
  146. value = Operation(OperationCode::FMul, PRECISE, value, GetRegister(instr.gpr20));
  147. }
  148. }
  149. value = GetSaturatedFloat(value, instr.ipa.saturate);
  150. SetRegister(bb, instr.gpr0, value);
  151. break;
  152. }
  153. case OpCode::Id::OUT_R: {
  154. UNIMPLEMENTED_IF_MSG(instr.gpr20.Value() != Register::ZeroIndex,
  155. "Stream buffer is not supported");
  156. if (instr.out.emit) {
  157. // gpr0 is used to store the next address and gpr8 contains the address to emit.
  158. // Hardware uses pointers here but we just ignore it
  159. bb.push_back(Operation(OperationCode::EmitVertex));
  160. SetRegister(bb, instr.gpr0, Immediate(0));
  161. }
  162. if (instr.out.cut) {
  163. bb.push_back(Operation(OperationCode::EndPrimitive));
  164. }
  165. break;
  166. }
  167. case OpCode::Id::ISBERD: {
  168. UNIMPLEMENTED_IF(instr.isberd.o != 0);
  169. UNIMPLEMENTED_IF(instr.isberd.skew != 0);
  170. UNIMPLEMENTED_IF(instr.isberd.shift != Tegra::Shader::IsberdShift::None);
  171. UNIMPLEMENTED_IF(instr.isberd.mode != Tegra::Shader::IsberdMode::None);
  172. LOG_WARNING(HW_GPU, "ISBERD instruction is incomplete");
  173. SetRegister(bb, instr.gpr0, GetRegister(instr.gpr8));
  174. break;
  175. }
  176. case OpCode::Id::DEPBAR: {
  177. LOG_WARNING(HW_GPU, "DEPBAR instruction is stubbed");
  178. break;
  179. }
  180. default:
  181. UNIMPLEMENTED_MSG("Unhandled instruction: {}", opcode->get().GetName());
  182. }
  183. return pc;
  184. }
  185. } // namespace VideoCommon::Shader