other.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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::NOP: {
  21. UNIMPLEMENTED_IF(instr.nop.cc != Tegra::Shader::ConditionCode::T);
  22. UNIMPLEMENTED_IF(instr.nop.trigger != 0);
  23. // With the previous preconditions, this instruction is a no-operation.
  24. break;
  25. }
  26. case OpCode::Id::EXIT: {
  27. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  28. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "EXIT condition code used: {}",
  29. static_cast<u32>(cc));
  30. switch (instr.flow.cond) {
  31. case Tegra::Shader::FlowCondition::Always:
  32. bb.push_back(Operation(OperationCode::Exit));
  33. if (instr.pred.pred_index == static_cast<u64>(Tegra::Shader::Pred::UnusedIndex)) {
  34. // If this is an unconditional exit then just end processing here,
  35. // otherwise we have to account for the possibility of the condition
  36. // not being met, so continue processing the next instruction.
  37. pc = MAX_PROGRAM_LENGTH - 1;
  38. }
  39. break;
  40. case Tegra::Shader::FlowCondition::Fcsm_Tr:
  41. // TODO(bunnei): What is this used for? If we assume this conditon is not
  42. // satisifed, dual vertex shaders in Farming Simulator make more sense
  43. UNIMPLEMENTED_MSG("Skipping unknown FlowCondition::Fcsm_Tr");
  44. break;
  45. default:
  46. UNIMPLEMENTED_MSG("Unhandled flow condition: {}",
  47. static_cast<u32>(instr.flow.cond.Value()));
  48. }
  49. break;
  50. }
  51. case OpCode::Id::KIL: {
  52. UNIMPLEMENTED_IF(instr.flow.cond != Tegra::Shader::FlowCondition::Always);
  53. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  54. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "KIL condition code used: {}",
  55. static_cast<u32>(cc));
  56. bb.push_back(Operation(OperationCode::Discard));
  57. break;
  58. }
  59. case OpCode::Id::MOV_SYS: {
  60. const Node value = [&]() {
  61. switch (instr.sys20) {
  62. case SystemVariable::Ydirection:
  63. return Operation(OperationCode::YNegate);
  64. case SystemVariable::InvocationInfo:
  65. LOG_WARNING(HW_GPU, "MOV_SYS instruction with InvocationInfo is incomplete");
  66. return Immediate(0u);
  67. case SystemVariable::Tid: {
  68. Node value = Immediate(0);
  69. value = BitfieldInsert(value, Operation(OperationCode::LocalInvocationIdX), 0, 9);
  70. value = BitfieldInsert(value, Operation(OperationCode::LocalInvocationIdY), 16, 9);
  71. value = BitfieldInsert(value, Operation(OperationCode::LocalInvocationIdZ), 26, 5);
  72. return value;
  73. }
  74. case SystemVariable::TidX:
  75. return Operation(OperationCode::LocalInvocationIdX);
  76. case SystemVariable::TidY:
  77. return Operation(OperationCode::LocalInvocationIdY);
  78. case SystemVariable::TidZ:
  79. return Operation(OperationCode::LocalInvocationIdZ);
  80. case SystemVariable::CtaIdX:
  81. return Operation(OperationCode::WorkGroupIdX);
  82. case SystemVariable::CtaIdY:
  83. return Operation(OperationCode::WorkGroupIdY);
  84. case SystemVariable::CtaIdZ:
  85. return Operation(OperationCode::WorkGroupIdZ);
  86. default:
  87. UNIMPLEMENTED_MSG("Unhandled system move: {}",
  88. static_cast<u32>(instr.sys20.Value()));
  89. return Immediate(0u);
  90. }
  91. }();
  92. SetRegister(bb, instr.gpr0, value);
  93. break;
  94. }
  95. case OpCode::Id::BRA: {
  96. Node branch;
  97. if (instr.bra.constant_buffer == 0) {
  98. const u32 target = pc + instr.bra.GetBranchTarget();
  99. branch = Operation(OperationCode::Branch, Immediate(target));
  100. } else {
  101. const u32 target = pc + 1;
  102. const Node op_a = GetConstBuffer(instr.cbuf36.index, instr.cbuf36.GetOffset());
  103. const Node convert = SignedOperation(OperationCode::IArithmeticShiftRight, true,
  104. PRECISE, op_a, Immediate(3));
  105. const Node operand =
  106. Operation(OperationCode::IAdd, PRECISE, convert, Immediate(target));
  107. branch = Operation(OperationCode::BranchIndirect, operand);
  108. }
  109. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  110. if (cc != Tegra::Shader::ConditionCode::T) {
  111. bb.push_back(Conditional(GetConditionCode(cc), {branch}));
  112. } else {
  113. bb.push_back(branch);
  114. }
  115. break;
  116. }
  117. case OpCode::Id::BRX: {
  118. Node operand;
  119. if (instr.brx.constant_buffer != 0) {
  120. const s32 target = pc + 1;
  121. const Node index = GetRegister(instr.gpr8);
  122. const Node op_a =
  123. GetConstBufferIndirect(instr.cbuf36.index, instr.cbuf36.GetOffset() + 0, index);
  124. const Node convert = SignedOperation(OperationCode::IArithmeticShiftRight, true,
  125. PRECISE, op_a, Immediate(3));
  126. operand = Operation(OperationCode::IAdd, PRECISE, convert, Immediate(target));
  127. } else {
  128. const s32 target = pc + instr.brx.GetBranchExtend();
  129. const Node op_a = GetRegister(instr.gpr8);
  130. const Node convert = SignedOperation(OperationCode::IArithmeticShiftRight, true,
  131. PRECISE, op_a, Immediate(3));
  132. operand = Operation(OperationCode::IAdd, PRECISE, convert, Immediate(target));
  133. }
  134. const Node branch = Operation(OperationCode::BranchIndirect, operand);
  135. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  136. if (cc != Tegra::Shader::ConditionCode::T) {
  137. bb.push_back(Conditional(GetConditionCode(cc), {branch}));
  138. } else {
  139. bb.push_back(branch);
  140. }
  141. break;
  142. }
  143. case OpCode::Id::SSY: {
  144. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  145. "Constant buffer flow is not supported");
  146. if (disable_flow_stack) {
  147. break;
  148. }
  149. // The SSY opcode tells the GPU where to re-converge divergent execution paths with SYNC.
  150. const u32 target = pc + instr.bra.GetBranchTarget();
  151. bb.push_back(
  152. Operation(OperationCode::PushFlowStack, MetaStackClass::Ssy, Immediate(target)));
  153. break;
  154. }
  155. case OpCode::Id::PBK: {
  156. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  157. "Constant buffer PBK is not supported");
  158. if (disable_flow_stack) {
  159. break;
  160. }
  161. // PBK pushes to a stack the address where BRK will jump to.
  162. const u32 target = pc + instr.bra.GetBranchTarget();
  163. bb.push_back(
  164. Operation(OperationCode::PushFlowStack, MetaStackClass::Pbk, Immediate(target)));
  165. break;
  166. }
  167. case OpCode::Id::SYNC: {
  168. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  169. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "SYNC condition code used: {}",
  170. static_cast<u32>(cc));
  171. if (disable_flow_stack) {
  172. break;
  173. }
  174. // The SYNC opcode jumps to the address previously set by the SSY opcode
  175. bb.push_back(Operation(OperationCode::PopFlowStack, MetaStackClass::Ssy));
  176. break;
  177. }
  178. case OpCode::Id::BRK: {
  179. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  180. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "BRK condition code used: {}",
  181. static_cast<u32>(cc));
  182. if (disable_flow_stack) {
  183. break;
  184. }
  185. // The BRK opcode jumps to the address previously set by the PBK opcode
  186. bb.push_back(Operation(OperationCode::PopFlowStack, MetaStackClass::Pbk));
  187. break;
  188. }
  189. case OpCode::Id::IPA: {
  190. const bool is_physical = instr.ipa.idx && instr.gpr8.Value() != 0xff;
  191. const auto attribute = instr.attribute.fmt28;
  192. const Tegra::Shader::IpaMode input_mode{instr.ipa.interp_mode.Value(),
  193. instr.ipa.sample_mode.Value()};
  194. Node value = is_physical ? GetPhysicalInputAttribute(instr.gpr8)
  195. : GetInputAttribute(attribute.index, attribute.element);
  196. const Tegra::Shader::Attribute::Index index = attribute.index.Value();
  197. const bool is_generic = index >= Tegra::Shader::Attribute::Index::Attribute_0 &&
  198. index <= Tegra::Shader::Attribute::Index::Attribute_31;
  199. if (is_generic || is_physical) {
  200. // TODO(Blinkhawk): There are cases where a perspective attribute use PASS.
  201. // In theory by setting them as perspective, OpenGL does the perspective correction.
  202. // A way must figured to reverse the last step of it.
  203. if (input_mode.interpolation_mode == Tegra::Shader::IpaInterpMode::Multiply) {
  204. value = Operation(OperationCode::FMul, PRECISE, value, GetRegister(instr.gpr20));
  205. }
  206. }
  207. value = GetSaturatedFloat(value, instr.ipa.saturate);
  208. SetRegister(bb, instr.gpr0, value);
  209. break;
  210. }
  211. case OpCode::Id::OUT_R: {
  212. UNIMPLEMENTED_IF_MSG(instr.gpr20.Value() != Register::ZeroIndex,
  213. "Stream buffer is not supported");
  214. if (instr.out.emit) {
  215. // gpr0 is used to store the next address and gpr8 contains the address to emit.
  216. // Hardware uses pointers here but we just ignore it
  217. bb.push_back(Operation(OperationCode::EmitVertex));
  218. SetRegister(bb, instr.gpr0, Immediate(0));
  219. }
  220. if (instr.out.cut) {
  221. bb.push_back(Operation(OperationCode::EndPrimitive));
  222. }
  223. break;
  224. }
  225. case OpCode::Id::ISBERD: {
  226. UNIMPLEMENTED_IF(instr.isberd.o != 0);
  227. UNIMPLEMENTED_IF(instr.isberd.skew != 0);
  228. UNIMPLEMENTED_IF(instr.isberd.shift != Tegra::Shader::IsberdShift::None);
  229. UNIMPLEMENTED_IF(instr.isberd.mode != Tegra::Shader::IsberdMode::None);
  230. LOG_WARNING(HW_GPU, "ISBERD instruction is incomplete");
  231. SetRegister(bb, instr.gpr0, GetRegister(instr.gpr8));
  232. break;
  233. }
  234. case OpCode::Id::DEPBAR: {
  235. LOG_WARNING(HW_GPU, "DEPBAR instruction is stubbed");
  236. break;
  237. }
  238. default:
  239. UNIMPLEMENTED_MSG("Unhandled instruction: {}", opcode->get().GetName());
  240. }
  241. return pc;
  242. }
  243. } // namespace VideoCommon::Shader