other.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 std::move;
  12. using Tegra::Shader::ConditionCode;
  13. using Tegra::Shader::Instruction;
  14. using Tegra::Shader::IpaInterpMode;
  15. using Tegra::Shader::OpCode;
  16. using Tegra::Shader::PixelImap;
  17. using Tegra::Shader::Register;
  18. using Tegra::Shader::SystemVariable;
  19. using Index = Tegra::Shader::Attribute::Index;
  20. u32 ShaderIR::DecodeOther(NodeBlock& bb, u32 pc) {
  21. const Instruction instr = {program_code[pc]};
  22. const auto opcode = OpCode::Decode(instr);
  23. switch (opcode->get().GetId()) {
  24. case OpCode::Id::NOP: {
  25. UNIMPLEMENTED_IF(instr.nop.cc != Tegra::Shader::ConditionCode::T);
  26. UNIMPLEMENTED_IF(instr.nop.trigger != 0);
  27. // With the previous preconditions, this instruction is a no-operation.
  28. break;
  29. }
  30. case OpCode::Id::EXIT: {
  31. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  32. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "EXIT condition code used: {}",
  33. static_cast<u32>(cc));
  34. switch (instr.flow.cond) {
  35. case Tegra::Shader::FlowCondition::Always:
  36. bb.push_back(Operation(OperationCode::Exit));
  37. if (instr.pred.pred_index == static_cast<u64>(Tegra::Shader::Pred::UnusedIndex)) {
  38. // If this is an unconditional exit then just end processing here,
  39. // otherwise we have to account for the possibility of the condition
  40. // not being met, so continue processing the next instruction.
  41. pc = MAX_PROGRAM_LENGTH - 1;
  42. }
  43. break;
  44. case Tegra::Shader::FlowCondition::Fcsm_Tr:
  45. // TODO(bunnei): What is this used for? If we assume this conditon is not
  46. // satisifed, dual vertex shaders in Farming Simulator make more sense
  47. UNIMPLEMENTED_MSG("Skipping unknown FlowCondition::Fcsm_Tr");
  48. break;
  49. default:
  50. UNIMPLEMENTED_MSG("Unhandled flow condition: {}",
  51. static_cast<u32>(instr.flow.cond.Value()));
  52. }
  53. break;
  54. }
  55. case OpCode::Id::KIL: {
  56. UNIMPLEMENTED_IF(instr.flow.cond != Tegra::Shader::FlowCondition::Always);
  57. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  58. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "KIL condition code used: {}",
  59. static_cast<u32>(cc));
  60. bb.push_back(Operation(OperationCode::Discard));
  61. break;
  62. }
  63. case OpCode::Id::S2R: {
  64. const Node value = [this, instr] {
  65. switch (instr.sys20) {
  66. case SystemVariable::LaneId:
  67. return Operation(OperationCode::ThreadId);
  68. case SystemVariable::InvocationId:
  69. return Operation(OperationCode::InvocationId);
  70. case SystemVariable::Ydirection:
  71. return Operation(OperationCode::YNegate);
  72. case SystemVariable::InvocationInfo:
  73. LOG_WARNING(HW_GPU, "S2R instruction with InvocationInfo is incomplete");
  74. return Immediate(0x00ff'0000U);
  75. case SystemVariable::WscaleFactorXY:
  76. UNIMPLEMENTED_MSG("S2R WscaleFactorXY is not implemented");
  77. return Immediate(0U);
  78. case SystemVariable::WscaleFactorZ:
  79. UNIMPLEMENTED_MSG("S2R WscaleFactorZ is not implemented");
  80. return Immediate(0U);
  81. case SystemVariable::Tid: {
  82. Node value = Immediate(0);
  83. value = BitfieldInsert(value, Operation(OperationCode::LocalInvocationIdX), 0, 9);
  84. value = BitfieldInsert(value, Operation(OperationCode::LocalInvocationIdY), 16, 9);
  85. value = BitfieldInsert(value, Operation(OperationCode::LocalInvocationIdZ), 26, 5);
  86. return value;
  87. }
  88. case SystemVariable::TidX:
  89. return Operation(OperationCode::LocalInvocationIdX);
  90. case SystemVariable::TidY:
  91. return Operation(OperationCode::LocalInvocationIdY);
  92. case SystemVariable::TidZ:
  93. return Operation(OperationCode::LocalInvocationIdZ);
  94. case SystemVariable::CtaIdX:
  95. return Operation(OperationCode::WorkGroupIdX);
  96. case SystemVariable::CtaIdY:
  97. return Operation(OperationCode::WorkGroupIdY);
  98. case SystemVariable::CtaIdZ:
  99. return Operation(OperationCode::WorkGroupIdZ);
  100. case SystemVariable::EqMask:
  101. case SystemVariable::LtMask:
  102. case SystemVariable::LeMask:
  103. case SystemVariable::GtMask:
  104. case SystemVariable::GeMask:
  105. uses_warps = true;
  106. switch (instr.sys20) {
  107. case SystemVariable::EqMask:
  108. return Operation(OperationCode::ThreadEqMask);
  109. case SystemVariable::LtMask:
  110. return Operation(OperationCode::ThreadLtMask);
  111. case SystemVariable::LeMask:
  112. return Operation(OperationCode::ThreadLeMask);
  113. case SystemVariable::GtMask:
  114. return Operation(OperationCode::ThreadGtMask);
  115. case SystemVariable::GeMask:
  116. return Operation(OperationCode::ThreadGeMask);
  117. default:
  118. UNREACHABLE();
  119. return Immediate(0u);
  120. }
  121. default:
  122. UNIMPLEMENTED_MSG("Unhandled system move: {}",
  123. static_cast<u32>(instr.sys20.Value()));
  124. return Immediate(0u);
  125. }
  126. }();
  127. SetRegister(bb, instr.gpr0, value);
  128. break;
  129. }
  130. case OpCode::Id::BRA: {
  131. Node branch;
  132. if (instr.bra.constant_buffer == 0) {
  133. const u32 target = pc + instr.bra.GetBranchTarget();
  134. branch = Operation(OperationCode::Branch, Immediate(target));
  135. } else {
  136. const u32 target = pc + 1;
  137. const Node op_a = GetConstBuffer(instr.cbuf36.index, instr.cbuf36.GetOffset());
  138. const Node convert = SignedOperation(OperationCode::IArithmeticShiftRight, true,
  139. PRECISE, op_a, Immediate(3));
  140. const Node operand =
  141. Operation(OperationCode::IAdd, PRECISE, convert, Immediate(target));
  142. branch = Operation(OperationCode::BranchIndirect, operand);
  143. }
  144. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  145. if (cc != Tegra::Shader::ConditionCode::T) {
  146. bb.push_back(Conditional(GetConditionCode(cc), {branch}));
  147. } else {
  148. bb.push_back(branch);
  149. }
  150. break;
  151. }
  152. case OpCode::Id::BRX: {
  153. Node operand;
  154. if (instr.brx.constant_buffer != 0) {
  155. const s32 target = pc + 1;
  156. const Node index = GetRegister(instr.gpr8);
  157. const Node op_a =
  158. GetConstBufferIndirect(instr.cbuf36.index, instr.cbuf36.GetOffset() + 0, index);
  159. const Node convert = SignedOperation(OperationCode::IArithmeticShiftRight, true,
  160. PRECISE, op_a, Immediate(3));
  161. operand = Operation(OperationCode::IAdd, PRECISE, convert, Immediate(target));
  162. } else {
  163. const s32 target = pc + instr.brx.GetBranchExtend();
  164. const Node op_a = GetRegister(instr.gpr8);
  165. const Node convert = SignedOperation(OperationCode::IArithmeticShiftRight, true,
  166. PRECISE, op_a, Immediate(3));
  167. operand = Operation(OperationCode::IAdd, PRECISE, convert, Immediate(target));
  168. }
  169. const Node branch = Operation(OperationCode::BranchIndirect, operand);
  170. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  171. if (cc != Tegra::Shader::ConditionCode::T) {
  172. bb.push_back(Conditional(GetConditionCode(cc), {branch}));
  173. } else {
  174. bb.push_back(branch);
  175. }
  176. break;
  177. }
  178. case OpCode::Id::SSY: {
  179. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  180. "Constant buffer flow is not supported");
  181. if (disable_flow_stack) {
  182. break;
  183. }
  184. // The SSY opcode tells the GPU where to re-converge divergent execution paths with SYNC.
  185. const u32 target = pc + instr.bra.GetBranchTarget();
  186. bb.push_back(
  187. Operation(OperationCode::PushFlowStack, MetaStackClass::Ssy, Immediate(target)));
  188. break;
  189. }
  190. case OpCode::Id::PBK: {
  191. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  192. "Constant buffer PBK is not supported");
  193. if (disable_flow_stack) {
  194. break;
  195. }
  196. // PBK pushes to a stack the address where BRK will jump to.
  197. const u32 target = pc + instr.bra.GetBranchTarget();
  198. bb.push_back(
  199. Operation(OperationCode::PushFlowStack, MetaStackClass::Pbk, Immediate(target)));
  200. break;
  201. }
  202. case OpCode::Id::SYNC: {
  203. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  204. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "SYNC condition code used: {}",
  205. static_cast<u32>(cc));
  206. if (decompiled) {
  207. break;
  208. }
  209. // The SYNC opcode jumps to the address previously set by the SSY opcode
  210. bb.push_back(Operation(OperationCode::PopFlowStack, MetaStackClass::Ssy));
  211. break;
  212. }
  213. case OpCode::Id::BRK: {
  214. const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
  215. UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T, "BRK condition code used: {}",
  216. static_cast<u32>(cc));
  217. if (decompiled) {
  218. break;
  219. }
  220. // The BRK opcode jumps to the address previously set by the PBK opcode
  221. bb.push_back(Operation(OperationCode::PopFlowStack, MetaStackClass::Pbk));
  222. break;
  223. }
  224. case OpCode::Id::IPA: {
  225. const bool is_physical = instr.ipa.idx && instr.gpr8.Value() != 0xff;
  226. const auto attribute = instr.attribute.fmt28;
  227. const Index index = attribute.index;
  228. Node value = is_physical ? GetPhysicalInputAttribute(instr.gpr8)
  229. : GetInputAttribute(index, attribute.element);
  230. // Code taken from Ryujinx.
  231. if (index >= Index::Attribute_0 && index <= Index::Attribute_31) {
  232. const u32 location = static_cast<u32>(index) - static_cast<u32>(Index::Attribute_0);
  233. if (header.ps.GetPixelImap(location) == PixelImap::Perspective) {
  234. Node position_w = GetInputAttribute(Index::Position, 3);
  235. value = Operation(OperationCode::FMul, move(value), move(position_w));
  236. }
  237. }
  238. if (instr.ipa.interp_mode == IpaInterpMode::Multiply) {
  239. value = Operation(OperationCode::FMul, move(value), GetRegister(instr.gpr20));
  240. }
  241. value = GetSaturatedFloat(move(value), instr.ipa.saturate);
  242. SetRegister(bb, instr.gpr0, move(value));
  243. break;
  244. }
  245. case OpCode::Id::OUT_R: {
  246. UNIMPLEMENTED_IF_MSG(instr.gpr20.Value() != Register::ZeroIndex,
  247. "Stream buffer is not supported");
  248. if (instr.out.emit) {
  249. // gpr0 is used to store the next address and gpr8 contains the address to emit.
  250. // Hardware uses pointers here but we just ignore it
  251. bb.push_back(Operation(OperationCode::EmitVertex));
  252. SetRegister(bb, instr.gpr0, Immediate(0));
  253. }
  254. if (instr.out.cut) {
  255. bb.push_back(Operation(OperationCode::EndPrimitive));
  256. }
  257. break;
  258. }
  259. case OpCode::Id::ISBERD: {
  260. UNIMPLEMENTED_IF(instr.isberd.o != 0);
  261. UNIMPLEMENTED_IF(instr.isberd.skew != 0);
  262. UNIMPLEMENTED_IF(instr.isberd.shift != Tegra::Shader::IsberdShift::None);
  263. UNIMPLEMENTED_IF(instr.isberd.mode != Tegra::Shader::IsberdMode::None);
  264. LOG_WARNING(HW_GPU, "ISBERD instruction is incomplete");
  265. SetRegister(bb, instr.gpr0, GetRegister(instr.gpr8));
  266. break;
  267. }
  268. case OpCode::Id::BAR: {
  269. UNIMPLEMENTED_IF_MSG(instr.value != 0xF0A81B8000070000ULL, "BAR is not BAR.SYNC 0x0");
  270. bb.push_back(Operation(OperationCode::Barrier));
  271. break;
  272. }
  273. case OpCode::Id::MEMBAR: {
  274. UNIMPLEMENTED_IF(instr.membar.unknown != Tegra::Shader::MembarUnknown::Default);
  275. const OperationCode type = [instr] {
  276. switch (instr.membar.type) {
  277. case Tegra::Shader::MembarType::CTA:
  278. return OperationCode::MemoryBarrierGroup;
  279. case Tegra::Shader::MembarType::GL:
  280. return OperationCode::MemoryBarrierGlobal;
  281. default:
  282. UNIMPLEMENTED_MSG("MEMBAR type={}", static_cast<int>(instr.membar.type.Value()));
  283. return OperationCode::MemoryBarrierGlobal;
  284. }
  285. }();
  286. bb.push_back(Operation(type));
  287. break;
  288. }
  289. case OpCode::Id::DEPBAR: {
  290. LOG_DEBUG(HW_GPU, "DEPBAR instruction is stubbed");
  291. break;
  292. }
  293. default:
  294. UNIMPLEMENTED_MSG("Unhandled instruction: {}", opcode->get().GetName());
  295. }
  296. return pc;
  297. }
  298. } // namespace VideoCommon::Shader