other.cpp 10 KB

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