other.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. Node branch;
  84. if (instr.bra.constant_buffer == 0) {
  85. const u32 target = pc + instr.bra.GetBranchTarget();
  86. branch = Operation(OperationCode::Branch, Immediate(target));
  87. } else {
  88. const u32 target = pc + 1;
  89. const Node op_a = GetConstBuffer(instr.cbuf36.index, instr.cbuf36.GetOffset());
  90. const Node convert = SignedOperation(OperationCode::IArithmeticShiftRight, true,
  91. PRECISE, op_a, Immediate(3));
  92. const Node operand =
  93. Operation(OperationCode::IAdd, PRECISE, convert, Immediate(target));
  94. branch = Operation(OperationCode::BranchIndirect, operand);
  95. }
  96. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  97. if (cc != Tegra::Shader::ConditionCode::T) {
  98. bb.push_back(Conditional(GetConditionCode(cc), {branch}));
  99. } else {
  100. bb.push_back(branch);
  101. }
  102. break;
  103. }
  104. case OpCode::Id::BRX: {
  105. Node operand;
  106. if (instr.brx.constant_buffer != 0) {
  107. const s32 target = pc + 1;
  108. const Node index = GetRegister(instr.gpr8);
  109. const Node op_a =
  110. GetConstBufferIndirect(instr.cbuf36.index, instr.cbuf36.GetOffset() + 0, index);
  111. const Node convert = SignedOperation(OperationCode::IArithmeticShiftRight, true,
  112. PRECISE, op_a, Immediate(3));
  113. operand = Operation(OperationCode::IAdd, PRECISE, convert, Immediate(target));
  114. } else {
  115. const s32 target = pc + instr.brx.GetBranchExtend();
  116. const Node op_a = GetRegister(instr.gpr8);
  117. const Node convert = SignedOperation(OperationCode::IArithmeticShiftRight, true,
  118. PRECISE, op_a, Immediate(3));
  119. operand = Operation(OperationCode::IAdd, PRECISE, convert, Immediate(target));
  120. }
  121. const Node branch = Operation(OperationCode::BranchIndirect, operand);
  122. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  123. if (cc != Tegra::Shader::ConditionCode::T) {
  124. bb.push_back(Conditional(GetConditionCode(cc), {branch}));
  125. } else {
  126. bb.push_back(branch);
  127. }
  128. break;
  129. }
  130. case OpCode::Id::SSY: {
  131. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  132. "Constant buffer flow is not supported");
  133. if (disable_flow_stack) {
  134. break;
  135. }
  136. // The SSY opcode tells the GPU where to re-converge divergent execution paths with SYNC.
  137. const u32 target = pc + instr.bra.GetBranchTarget();
  138. bb.push_back(
  139. Operation(OperationCode::PushFlowStack, MetaStackClass::Ssy, Immediate(target)));
  140. break;
  141. }
  142. case OpCode::Id::PBK: {
  143. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  144. "Constant buffer PBK is not supported");
  145. if (disable_flow_stack) {
  146. break;
  147. }
  148. // PBK pushes to a stack the address where BRK will jump to.
  149. const u32 target = pc + instr.bra.GetBranchTarget();
  150. bb.push_back(
  151. Operation(OperationCode::PushFlowStack, MetaStackClass::Pbk, Immediate(target)));
  152. break;
  153. }
  154. case OpCode::Id::SYNC: {
  155. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  156. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "SYNC condition code used: {}",
  157. static_cast<u32>(cc));
  158. if (disable_flow_stack) {
  159. break;
  160. }
  161. // The SYNC opcode jumps to the address previously set by the SSY opcode
  162. bb.push_back(Operation(OperationCode::PopFlowStack, MetaStackClass::Ssy));
  163. break;
  164. }
  165. case OpCode::Id::BRK: {
  166. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  167. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "BRK condition code used: {}",
  168. static_cast<u32>(cc));
  169. if (disable_flow_stack) {
  170. break;
  171. }
  172. // The BRK opcode jumps to the address previously set by the PBK opcode
  173. bb.push_back(Operation(OperationCode::PopFlowStack, MetaStackClass::Pbk));
  174. break;
  175. }
  176. case OpCode::Id::IPA: {
  177. const bool is_physical = instr.ipa.idx && instr.gpr8.Value() != 0xff;
  178. const auto attribute = instr.attribute.fmt28;
  179. const Tegra::Shader::IpaMode input_mode{instr.ipa.interp_mode.Value(),
  180. instr.ipa.sample_mode.Value()};
  181. Node value = is_physical ? GetPhysicalInputAttribute(instr.gpr8)
  182. : GetInputAttribute(attribute.index, attribute.element);
  183. const Tegra::Shader::Attribute::Index index = attribute.index.Value();
  184. const bool is_generic = index >= Tegra::Shader::Attribute::Index::Attribute_0 &&
  185. index <= Tegra::Shader::Attribute::Index::Attribute_31;
  186. if (is_generic || is_physical) {
  187. // TODO(Blinkhawk): There are cases where a perspective attribute use PASS.
  188. // In theory by setting them as perspective, OpenGL does the perspective correction.
  189. // A way must figured to reverse the last step of it.
  190. if (input_mode.interpolation_mode == Tegra::Shader::IpaInterpMode::Multiply) {
  191. value = Operation(OperationCode::FMul, PRECISE, value, GetRegister(instr.gpr20));
  192. }
  193. }
  194. value = GetSaturatedFloat(value, instr.ipa.saturate);
  195. SetRegister(bb, instr.gpr0, value);
  196. break;
  197. }
  198. case OpCode::Id::OUT_R: {
  199. UNIMPLEMENTED_IF_MSG(instr.gpr20.Value() != Register::ZeroIndex,
  200. "Stream buffer is not supported");
  201. if (instr.out.emit) {
  202. // gpr0 is used to store the next address and gpr8 contains the address to emit.
  203. // Hardware uses pointers here but we just ignore it
  204. bb.push_back(Operation(OperationCode::EmitVertex));
  205. SetRegister(bb, instr.gpr0, Immediate(0));
  206. }
  207. if (instr.out.cut) {
  208. bb.push_back(Operation(OperationCode::EndPrimitive));
  209. }
  210. break;
  211. }
  212. case OpCode::Id::ISBERD: {
  213. UNIMPLEMENTED_IF(instr.isberd.o != 0);
  214. UNIMPLEMENTED_IF(instr.isberd.skew != 0);
  215. UNIMPLEMENTED_IF(instr.isberd.shift != Tegra::Shader::IsberdShift::None);
  216. UNIMPLEMENTED_IF(instr.isberd.mode != Tegra::Shader::IsberdMode::None);
  217. LOG_WARNING(HW_GPU, "ISBERD instruction is incomplete");
  218. SetRegister(bb, instr.gpr0, GetRegister(instr.gpr8));
  219. break;
  220. }
  221. case OpCode::Id::DEPBAR: {
  222. LOG_WARNING(HW_GPU, "DEPBAR instruction is stubbed");
  223. break;
  224. }
  225. default:
  226. UNIMPLEMENTED_MSG("Unhandled instruction: {}", opcode->get().GetName());
  227. }
  228. return pc;
  229. }
  230. } // namespace VideoCommon::Shader