other.cpp 11 KB

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