shader_jit_x64.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <smmintrin.h>
  5. #include "common/x64/abi.h"
  6. #include "common/x64/cpu_detect.h"
  7. #include "common/x64/emitter.h"
  8. #include "shader.h"
  9. #include "shader_jit_x64.h"
  10. namespace Pica {
  11. namespace Shader {
  12. using namespace Gen;
  13. typedef void (JitCompiler::*JitFunction)(Instruction instr);
  14. const JitFunction instr_table[64] = {
  15. &JitCompiler::Compile_ADD, // add
  16. &JitCompiler::Compile_DP3, // dp3
  17. &JitCompiler::Compile_DP4, // dp4
  18. &JitCompiler::Compile_DPH, // dph
  19. nullptr, // unknown
  20. &JitCompiler::Compile_EX2, // ex2
  21. &JitCompiler::Compile_LG2, // lg2
  22. nullptr, // unknown
  23. &JitCompiler::Compile_MUL, // mul
  24. &JitCompiler::Compile_SGE, // sge
  25. &JitCompiler::Compile_SLT, // slt
  26. &JitCompiler::Compile_FLR, // flr
  27. &JitCompiler::Compile_MAX, // max
  28. &JitCompiler::Compile_MIN, // min
  29. &JitCompiler::Compile_RCP, // rcp
  30. &JitCompiler::Compile_RSQ, // rsq
  31. nullptr, // unknown
  32. nullptr, // unknown
  33. &JitCompiler::Compile_MOVA, // mova
  34. &JitCompiler::Compile_MOV, // mov
  35. nullptr, // unknown
  36. nullptr, // unknown
  37. nullptr, // unknown
  38. nullptr, // unknown
  39. &JitCompiler::Compile_DPH, // dphi
  40. nullptr, // unknown
  41. &JitCompiler::Compile_SGE, // sgei
  42. &JitCompiler::Compile_SLT, // slti
  43. nullptr, // unknown
  44. nullptr, // unknown
  45. nullptr, // unknown
  46. nullptr, // unknown
  47. nullptr, // unknown
  48. &JitCompiler::Compile_NOP, // nop
  49. &JitCompiler::Compile_END, // end
  50. nullptr, // break
  51. &JitCompiler::Compile_CALL, // call
  52. &JitCompiler::Compile_CALLC, // callc
  53. &JitCompiler::Compile_CALLU, // callu
  54. &JitCompiler::Compile_IF, // ifu
  55. &JitCompiler::Compile_IF, // ifc
  56. &JitCompiler::Compile_LOOP, // loop
  57. nullptr, // emit
  58. nullptr, // sete
  59. &JitCompiler::Compile_JMP, // jmpc
  60. &JitCompiler::Compile_JMP, // jmpu
  61. &JitCompiler::Compile_CMP, // cmp
  62. &JitCompiler::Compile_CMP, // cmp
  63. &JitCompiler::Compile_MAD, // madi
  64. &JitCompiler::Compile_MAD, // madi
  65. &JitCompiler::Compile_MAD, // madi
  66. &JitCompiler::Compile_MAD, // madi
  67. &JitCompiler::Compile_MAD, // madi
  68. &JitCompiler::Compile_MAD, // madi
  69. &JitCompiler::Compile_MAD, // madi
  70. &JitCompiler::Compile_MAD, // madi
  71. &JitCompiler::Compile_MAD, // mad
  72. &JitCompiler::Compile_MAD, // mad
  73. &JitCompiler::Compile_MAD, // mad
  74. &JitCompiler::Compile_MAD, // mad
  75. &JitCompiler::Compile_MAD, // mad
  76. &JitCompiler::Compile_MAD, // mad
  77. &JitCompiler::Compile_MAD, // mad
  78. &JitCompiler::Compile_MAD, // mad
  79. };
  80. // The following is used to alias some commonly used registers. Generally, RAX-RDX and XMM0-XMM3 can
  81. // be used as scratch registers within a compiler function. The other registers have designated
  82. // purposes, as documented below:
  83. /// Pointer to the uniform memory
  84. static const X64Reg UNIFORMS = R9;
  85. /// The two 32-bit VS address offset registers set by the MOVA instruction
  86. static const X64Reg ADDROFFS_REG_0 = R10;
  87. static const X64Reg ADDROFFS_REG_1 = R11;
  88. /// VS loop count register
  89. static const X64Reg LOOPCOUNT_REG = R12;
  90. /// Current VS loop iteration number (we could probably use LOOPCOUNT_REG, but this quicker)
  91. static const X64Reg LOOPCOUNT = RSI;
  92. /// Number to increment LOOPCOUNT_REG by on each loop iteration
  93. static const X64Reg LOOPINC = RDI;
  94. /// Result of the previous CMP instruction for the X-component comparison
  95. static const X64Reg COND0 = R13;
  96. /// Result of the previous CMP instruction for the Y-component comparison
  97. static const X64Reg COND1 = R14;
  98. /// Pointer to the UnitState instance for the current VS unit
  99. static const X64Reg REGISTERS = R15;
  100. /// SIMD scratch register
  101. static const X64Reg SCRATCH = XMM0;
  102. /// Loaded with the first swizzled source register, otherwise can be used as a scratch register
  103. static const X64Reg SRC1 = XMM1;
  104. /// Loaded with the second swizzled source register, otherwise can be used as a scratch register
  105. static const X64Reg SRC2 = XMM2;
  106. /// Loaded with the third swizzled source register, otherwise can be used as a scratch register
  107. static const X64Reg SRC3 = XMM3;
  108. /// Constant vector of [1.0f, 1.0f, 1.0f, 1.0f], used to efficiently set a vector to one
  109. static const X64Reg ONE = XMM14;
  110. /// Constant vector of [-0.f, -0.f, -0.f, -0.f], used to efficiently negate a vector with XOR
  111. static const X64Reg NEGBIT = XMM15;
  112. /// Raw constant for the source register selector that indicates no swizzling is performed
  113. static const u8 NO_SRC_REG_SWIZZLE = 0x1b;
  114. /// Raw constant for the destination register enable mask that indicates all components are enabled
  115. static const u8 NO_DEST_REG_MASK = 0xf;
  116. /**
  117. * Loads and swizzles a source register into the specified XMM register.
  118. * @param instr VS instruction, used for determining how to load the source register
  119. * @param src_num Number indicating which source register to load (1 = src1, 2 = src2, 3 = src3)
  120. * @param src_reg SourceRegister object corresponding to the source register to load
  121. * @param dest Destination XMM register to store the loaded, swizzled source register
  122. */
  123. void JitCompiler::Compile_SwizzleSrc(Instruction instr, unsigned src_num, SourceRegister src_reg, X64Reg dest) {
  124. X64Reg src_ptr;
  125. int src_offset;
  126. if (src_reg.GetRegisterType() == RegisterType::FloatUniform) {
  127. src_ptr = UNIFORMS;
  128. src_offset = src_reg.GetIndex() * sizeof(float24) * 4;
  129. } else {
  130. src_ptr = REGISTERS;
  131. src_offset = UnitState<false>::InputOffset(src_reg);
  132. }
  133. unsigned operand_desc_id;
  134. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MAD ||
  135. instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  136. // The MAD and MADI instructions do not use the address offset registers, so loading the
  137. // source is a bit simpler here
  138. operand_desc_id = instr.mad.operand_desc_id;
  139. // Load the source
  140. MOVAPS(dest, MDisp(src_ptr, src_offset));
  141. } else {
  142. operand_desc_id = instr.common.operand_desc_id;
  143. const bool is_inverted = (0 != (instr.opcode.Value().GetInfo().subtype & OpCode::Info::SrcInversed));
  144. unsigned offset_src = is_inverted ? 2 : 1;
  145. if (src_num == offset_src && instr.common.address_register_index != 0) {
  146. switch (instr.common.address_register_index) {
  147. case 1: // address offset 1
  148. MOVAPS(dest, MComplex(src_ptr, ADDROFFS_REG_0, 1, src_offset));
  149. break;
  150. case 2: // address offset 2
  151. MOVAPS(dest, MComplex(src_ptr, ADDROFFS_REG_1, 1, src_offset));
  152. break;
  153. case 3: // adddress offet 3
  154. MOVAPS(dest, MComplex(src_ptr, LOOPCOUNT_REG, 1, src_offset));
  155. break;
  156. default:
  157. UNREACHABLE();
  158. break;
  159. }
  160. } else {
  161. // Load the source
  162. MOVAPS(dest, MDisp(src_ptr, src_offset));
  163. }
  164. }
  165. SwizzlePattern swiz = { g_state.vs.swizzle_data[operand_desc_id] };
  166. // Generate instructions for source register swizzling as needed
  167. u8 sel = swiz.GetRawSelector(src_num);
  168. if (sel != NO_SRC_REG_SWIZZLE) {
  169. // Selector component order needs to be reversed for the SHUFPS instruction
  170. sel = ((sel & 0xc0) >> 6) | ((sel & 3) << 6) | ((sel & 0xc) << 2) | ((sel & 0x30) >> 2);
  171. // Shuffle inputs for swizzle
  172. SHUFPS(dest, R(dest), sel);
  173. }
  174. // If the source register should be negated, flip the negative bit using XOR
  175. const bool negate[] = { swiz.negate_src1, swiz.negate_src2, swiz.negate_src3 };
  176. if (negate[src_num - 1]) {
  177. XORPS(dest, R(NEGBIT));
  178. }
  179. }
  180. void JitCompiler::Compile_DestEnable(Instruction instr,X64Reg src) {
  181. DestRegister dest;
  182. unsigned operand_desc_id;
  183. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MAD ||
  184. instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  185. operand_desc_id = instr.mad.operand_desc_id;
  186. dest = instr.mad.dest.Value();
  187. } else {
  188. operand_desc_id = instr.common.operand_desc_id;
  189. dest = instr.common.dest.Value();
  190. }
  191. SwizzlePattern swiz = { g_state.vs.swizzle_data[operand_desc_id] };
  192. // If all components are enabled, write the result to the destination register
  193. if (swiz.dest_mask == NO_DEST_REG_MASK) {
  194. // Store dest back to memory
  195. MOVAPS(MDisp(REGISTERS, UnitState<false>::OutputOffset(dest)), src);
  196. } else {
  197. // Not all components are enabled, so mask the result when storing to the destination register...
  198. MOVAPS(SCRATCH, MDisp(REGISTERS, UnitState<false>::OutputOffset(dest)));
  199. if (Common::GetCPUCaps().sse4_1) {
  200. u8 mask = ((swiz.dest_mask & 1) << 3) | ((swiz.dest_mask & 8) >> 3) | ((swiz.dest_mask & 2) << 1) | ((swiz.dest_mask & 4) >> 1);
  201. BLENDPS(SCRATCH, R(src), mask);
  202. } else {
  203. MOVAPS(XMM4, R(src));
  204. UNPCKHPS(XMM4, R(SCRATCH)); // Unpack X/Y components of source and destination
  205. UNPCKLPS(SCRATCH, R(src)); // Unpack Z/W components of source and destination
  206. // Compute selector to selectively copy source components to destination for SHUFPS instruction
  207. u8 sel = ((swiz.DestComponentEnabled(0) ? 1 : 0) << 0) |
  208. ((swiz.DestComponentEnabled(1) ? 3 : 2) << 2) |
  209. ((swiz.DestComponentEnabled(2) ? 0 : 1) << 4) |
  210. ((swiz.DestComponentEnabled(3) ? 2 : 3) << 6);
  211. SHUFPS(SCRATCH, R(XMM4), sel);
  212. }
  213. // Store dest back to memory
  214. MOVAPS(MDisp(REGISTERS, UnitState<false>::OutputOffset(dest)), SCRATCH);
  215. }
  216. }
  217. void JitCompiler::Compile_EvaluateCondition(Instruction instr) {
  218. // Note: NXOR is used below to check for equality
  219. switch (instr.flow_control.op) {
  220. case Instruction::FlowControlType::Or:
  221. MOV(32, R(RAX), R(COND0));
  222. MOV(32, R(RBX), R(COND1));
  223. XOR(32, R(RAX), Imm32(instr.flow_control.refx.Value() ^ 1));
  224. XOR(32, R(RBX), Imm32(instr.flow_control.refy.Value() ^ 1));
  225. OR(32, R(RAX), R(RBX));
  226. break;
  227. case Instruction::FlowControlType::And:
  228. MOV(32, R(RAX), R(COND0));
  229. MOV(32, R(RBX), R(COND1));
  230. XOR(32, R(RAX), Imm32(instr.flow_control.refx.Value() ^ 1));
  231. XOR(32, R(RBX), Imm32(instr.flow_control.refy.Value() ^ 1));
  232. AND(32, R(RAX), R(RBX));
  233. break;
  234. case Instruction::FlowControlType::JustX:
  235. MOV(32, R(RAX), R(COND0));
  236. XOR(32, R(RAX), Imm32(instr.flow_control.refx.Value() ^ 1));
  237. break;
  238. case Instruction::FlowControlType::JustY:
  239. MOV(32, R(RAX), R(COND1));
  240. XOR(32, R(RAX), Imm32(instr.flow_control.refy.Value() ^ 1));
  241. break;
  242. }
  243. }
  244. void JitCompiler::Compile_UniformCondition(Instruction instr) {
  245. int offset = offsetof(decltype(g_state.vs.uniforms), b) + (instr.flow_control.bool_uniform_id * sizeof(bool));
  246. CMP(sizeof(bool) * 8, MDisp(UNIFORMS, offset), Imm8(0));
  247. }
  248. void JitCompiler::Compile_PushCallerSavedXMM() {
  249. #ifndef _WIN32
  250. SUB(64, R(RSP), Imm8(2 * 16));
  251. MOVUPS(MDisp(RSP, 16), ONE);
  252. MOVUPS(MDisp(RSP, 0), NEGBIT);
  253. #endif
  254. }
  255. void JitCompiler::Compile_PopCallerSavedXMM() {
  256. #ifndef _WIN32
  257. MOVUPS(NEGBIT, MDisp(RSP, 0));
  258. MOVUPS(ONE, MDisp(RSP, 16));
  259. ADD(64, R(RSP), Imm8(2 * 16));
  260. #endif
  261. }
  262. void JitCompiler::Compile_ADD(Instruction instr) {
  263. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  264. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  265. ADDPS(SRC1, R(SRC2));
  266. Compile_DestEnable(instr, SRC1);
  267. }
  268. void JitCompiler::Compile_DP3(Instruction instr) {
  269. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  270. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  271. if (Common::GetCPUCaps().sse4_1) {
  272. DPPS(SRC1, R(SRC2), 0x7f);
  273. } else {
  274. MULPS(SRC1, R(SRC2));
  275. MOVAPS(SRC2, R(SRC1));
  276. SHUFPS(SRC2, R(SRC2), _MM_SHUFFLE(1, 1, 1, 1));
  277. MOVAPS(SRC3, R(SRC1));
  278. SHUFPS(SRC3, R(SRC3), _MM_SHUFFLE(2, 2, 2, 2));
  279. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0));
  280. ADDPS(SRC1, R(SRC2));
  281. ADDPS(SRC1, R(SRC3));
  282. }
  283. Compile_DestEnable(instr, SRC1);
  284. }
  285. void JitCompiler::Compile_DP4(Instruction instr) {
  286. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  287. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  288. if (Common::GetCPUCaps().sse4_1) {
  289. DPPS(SRC1, R(SRC2), 0xff);
  290. } else {
  291. MULPS(SRC1, R(SRC2));
  292. MOVAPS(SRC2, R(SRC1));
  293. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(2, 3, 0, 1)); // XYZW -> ZWXY
  294. ADDPS(SRC1, R(SRC2));
  295. MOVAPS(SRC2, R(SRC1));
  296. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 1, 2, 3)); // XYZW -> WZYX
  297. ADDPS(SRC1, R(SRC2));
  298. }
  299. Compile_DestEnable(instr, SRC1);
  300. }
  301. void JitCompiler::Compile_DPH(Instruction instr) {
  302. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::DPHI) {
  303. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  304. Compile_SwizzleSrc(instr, 2, instr.common.src2i, SRC2);
  305. } else {
  306. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  307. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  308. }
  309. if (Common::GetCPUCaps().sse4_1) {
  310. // Set 4th component to 1.0
  311. BLENDPS(SRC1, R(ONE), 0x8); // 0b1000
  312. DPPS(SRC1, R(SRC2), 0xff);
  313. } else {
  314. // Reverse to set the 4th component to 1.0
  315. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 1, 2, 3));
  316. MOVSS(SRC1, R(ONE));
  317. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 1, 2, 3));
  318. MULPS(SRC1, R(SRC2));
  319. MOVAPS(SRC2, R(SRC1));
  320. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(2, 3, 0, 1)); // XYZW -> ZWXY
  321. ADDPS(SRC1, R(SRC2));
  322. MOVAPS(SRC2, R(SRC1));
  323. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 1, 2, 3)); // XYZW -> WZYX
  324. ADDPS(SRC1, R(SRC2));
  325. }
  326. Compile_DestEnable(instr, SRC1);
  327. }
  328. void JitCompiler::Compile_EX2(Instruction instr) {
  329. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  330. MOVSS(XMM0, R(SRC1));
  331. // The following will actually break the stack alignment
  332. ABI_PushAllCallerSavedRegsAndAdjustStack();
  333. Compile_PushCallerSavedXMM();
  334. ABI_CallFunction(reinterpret_cast<const void*>(exp2f));
  335. Compile_PopCallerSavedXMM();
  336. ABI_PopAllCallerSavedRegsAndAdjustStack();
  337. SHUFPS(XMM0, R(XMM0), _MM_SHUFFLE(0, 0, 0, 0));
  338. MOVAPS(SRC1, R(XMM0));
  339. Compile_DestEnable(instr, SRC1);
  340. }
  341. void JitCompiler::Compile_LG2(Instruction instr) {
  342. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  343. MOVSS(XMM0, R(SRC1));
  344. // The following will actually break the stack alignment
  345. ABI_PushAllCallerSavedRegsAndAdjustStack();
  346. Compile_PushCallerSavedXMM();
  347. ABI_CallFunction(reinterpret_cast<const void*>(log2f));
  348. Compile_PopCallerSavedXMM();
  349. ABI_PopAllCallerSavedRegsAndAdjustStack();
  350. SHUFPS(XMM0, R(XMM0), _MM_SHUFFLE(0, 0, 0, 0));
  351. MOVAPS(SRC1, R(XMM0));
  352. Compile_DestEnable(instr, SRC1);
  353. }
  354. void JitCompiler::Compile_MUL(Instruction instr) {
  355. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  356. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  357. MULPS(SRC1, R(SRC2));
  358. Compile_DestEnable(instr, SRC1);
  359. }
  360. void JitCompiler::Compile_SGE(Instruction instr) {
  361. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::SGEI) {
  362. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  363. Compile_SwizzleSrc(instr, 2, instr.common.src2i, SRC2);
  364. } else {
  365. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  366. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  367. }
  368. CMPPS(SRC1, R(SRC2), CMP_NLT);
  369. ANDPS(SRC1, R(ONE));
  370. Compile_DestEnable(instr, SRC1);
  371. }
  372. void JitCompiler::Compile_SLT(Instruction instr) {
  373. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::SLTI) {
  374. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  375. Compile_SwizzleSrc(instr, 2, instr.common.src2i, SRC2);
  376. } else {
  377. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  378. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  379. }
  380. CMPPS(SRC1, R(SRC2), CMP_LT);
  381. ANDPS(SRC1, R(ONE));
  382. Compile_DestEnable(instr, SRC1);
  383. }
  384. void JitCompiler::Compile_FLR(Instruction instr) {
  385. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  386. if (Common::GetCPUCaps().sse4_1) {
  387. ROUNDFLOORPS(SRC1, R(SRC1));
  388. } else {
  389. CVTPS2DQ(SRC1, R(SRC1));
  390. CVTDQ2PS(SRC1, R(SRC1));
  391. }
  392. Compile_DestEnable(instr, SRC1);
  393. }
  394. void JitCompiler::Compile_MAX(Instruction instr) {
  395. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  396. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  397. MAXPS(SRC1, R(SRC2));
  398. Compile_DestEnable(instr, SRC1);
  399. }
  400. void JitCompiler::Compile_MIN(Instruction instr) {
  401. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  402. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  403. MINPS(SRC1, R(SRC2));
  404. Compile_DestEnable(instr, SRC1);
  405. }
  406. void JitCompiler::Compile_MOVA(Instruction instr) {
  407. SwizzlePattern swiz = { g_state.vs.swizzle_data[instr.common.operand_desc_id] };
  408. if (!swiz.DestComponentEnabled(0) && !swiz.DestComponentEnabled(1)) {
  409. return; // NoOp
  410. }
  411. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  412. // Convert floats to integers (only care about X and Y components)
  413. CVTPS2DQ(SRC1, R(SRC1));
  414. // Get result
  415. MOVQ_xmm(R(RAX), SRC1);
  416. // Handle destination enable
  417. if (swiz.DestComponentEnabled(0) && swiz.DestComponentEnabled(1)) {
  418. // Move and sign-extend low 32 bits
  419. MOVSX(64, 32, ADDROFFS_REG_0, R(RAX));
  420. // Move and sign-extend high 32 bits
  421. SHR(64, R(RAX), Imm8(32));
  422. MOVSX(64, 32, ADDROFFS_REG_1, R(RAX));
  423. // Multiply by 16 to be used as an offset later
  424. SHL(64, R(ADDROFFS_REG_0), Imm8(4));
  425. SHL(64, R(ADDROFFS_REG_1), Imm8(4));
  426. } else {
  427. if (swiz.DestComponentEnabled(0)) {
  428. // Move and sign-extend low 32 bits
  429. MOVSX(64, 32, ADDROFFS_REG_0, R(RAX));
  430. // Multiply by 16 to be used as an offset later
  431. SHL(64, R(ADDROFFS_REG_0), Imm8(4));
  432. } else if (swiz.DestComponentEnabled(1)) {
  433. // Move and sign-extend high 32 bits
  434. SHR(64, R(RAX), Imm8(32));
  435. MOVSX(64, 32, ADDROFFS_REG_1, R(RAX));
  436. // Multiply by 16 to be used as an offset later
  437. SHL(64, R(ADDROFFS_REG_1), Imm8(4));
  438. }
  439. }
  440. }
  441. void JitCompiler::Compile_MOV(Instruction instr) {
  442. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  443. Compile_DestEnable(instr, SRC1);
  444. }
  445. void JitCompiler::Compile_RCP(Instruction instr) {
  446. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  447. // TODO(bunnei): RCPSS is a pretty rough approximation, this might cause problems if Pica
  448. // performs this operation more accurately. This should be checked on hardware.
  449. RCPSS(SRC1, R(SRC1));
  450. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0)); // XYWZ -> XXXX
  451. Compile_DestEnable(instr, SRC1);
  452. }
  453. void JitCompiler::Compile_RSQ(Instruction instr) {
  454. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  455. // TODO(bunnei): RSQRTSS is a pretty rough approximation, this might cause problems if Pica
  456. // performs this operation more accurately. This should be checked on hardware.
  457. RSQRTSS(SRC1, R(SRC1));
  458. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0)); // XYWZ -> XXXX
  459. Compile_DestEnable(instr, SRC1);
  460. }
  461. void JitCompiler::Compile_NOP(Instruction instr) {
  462. }
  463. void JitCompiler::Compile_END(Instruction instr) {
  464. ABI_PopAllCalleeSavedRegsAndAdjustStack();
  465. RET();
  466. }
  467. void JitCompiler::Compile_CALL(Instruction instr) {
  468. unsigned offset = instr.flow_control.dest_offset;
  469. while (offset < (instr.flow_control.dest_offset + instr.flow_control.num_instructions)) {
  470. Compile_NextInstr(&offset);
  471. }
  472. }
  473. void JitCompiler::Compile_CALLC(Instruction instr) {
  474. Compile_EvaluateCondition(instr);
  475. FixupBranch b = J_CC(CC_Z, true);
  476. Compile_CALL(instr);
  477. SetJumpTarget(b);
  478. }
  479. void JitCompiler::Compile_CALLU(Instruction instr) {
  480. Compile_UniformCondition(instr);
  481. FixupBranch b = J_CC(CC_Z, true);
  482. Compile_CALL(instr);
  483. SetJumpTarget(b);
  484. }
  485. void JitCompiler::Compile_CMP(Instruction instr) {
  486. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  487. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  488. static const u8 cmp[] = { CMP_EQ, CMP_NEQ, CMP_LT, CMP_LE, CMP_NLE, CMP_NLT };
  489. if (instr.common.compare_op.x == instr.common.compare_op.y) {
  490. // Compare X-component and Y-component together
  491. CMPPS(SRC1, R(SRC2), cmp[instr.common.compare_op.x]);
  492. MOVQ_xmm(R(COND0), SRC1);
  493. MOV(64, R(COND1), R(COND0));
  494. } else {
  495. // Compare X-component
  496. MOVAPS(SCRATCH, R(SRC1));
  497. CMPSS(SCRATCH, R(SRC2), cmp[instr.common.compare_op.x]);
  498. // Compare Y-component
  499. CMPPS(SRC1, R(SRC2), cmp[instr.common.compare_op.y]);
  500. MOVQ_xmm(R(COND0), SCRATCH);
  501. MOVQ_xmm(R(COND1), SRC1);
  502. }
  503. SHR(32, R(COND0), Imm8(31));
  504. SHR(64, R(COND1), Imm8(63));
  505. }
  506. void JitCompiler::Compile_MAD(Instruction instr) {
  507. Compile_SwizzleSrc(instr, 1, instr.mad.src1, SRC1);
  508. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  509. Compile_SwizzleSrc(instr, 2, instr.mad.src2i, SRC2);
  510. Compile_SwizzleSrc(instr, 3, instr.mad.src3i, SRC3);
  511. } else {
  512. Compile_SwizzleSrc(instr, 2, instr.mad.src2, SRC2);
  513. Compile_SwizzleSrc(instr, 3, instr.mad.src3, SRC3);
  514. }
  515. if (Common::GetCPUCaps().fma) {
  516. VFMADD213PS(SRC1, SRC2, R(SRC3));
  517. } else {
  518. MULPS(SRC1, R(SRC2));
  519. ADDPS(SRC1, R(SRC3));
  520. }
  521. Compile_DestEnable(instr, SRC1);
  522. }
  523. void JitCompiler::Compile_IF(Instruction instr) {
  524. ASSERT_MSG(instr.flow_control.dest_offset > *offset_ptr, "Backwards if-statements not supported");
  525. // Evaluate the "IF" condition
  526. if (instr.opcode.Value() == OpCode::Id::IFU) {
  527. Compile_UniformCondition(instr);
  528. } else if (instr.opcode.Value() == OpCode::Id::IFC) {
  529. Compile_EvaluateCondition(instr);
  530. }
  531. FixupBranch b = J_CC(CC_Z, true);
  532. // Compile the code that corresponds to the condition evaluating as true
  533. Compile_Block(instr.flow_control.dest_offset - 1);
  534. // If there isn't an "ELSE" condition, we are done here
  535. if (instr.flow_control.num_instructions == 0) {
  536. SetJumpTarget(b);
  537. return;
  538. }
  539. FixupBranch b2 = J(true);
  540. SetJumpTarget(b);
  541. // This code corresponds to the "ELSE" condition
  542. // Comple the code that corresponds to the condition evaluating as false
  543. Compile_Block(instr.flow_control.dest_offset + instr.flow_control.num_instructions - 1);
  544. SetJumpTarget(b2);
  545. }
  546. void JitCompiler::Compile_LOOP(Instruction instr) {
  547. ASSERT_MSG(instr.flow_control.dest_offset > *offset_ptr, "Backwards loops not supported");
  548. ASSERT_MSG(!looping, "Nested loops not supported");
  549. looping = true;
  550. int offset = offsetof(decltype(g_state.vs.uniforms), i) + (instr.flow_control.int_uniform_id * sizeof(Math::Vec4<u8>));
  551. MOV(32, R(LOOPCOUNT), MDisp(UNIFORMS, offset));
  552. MOV(32, R(LOOPCOUNT_REG), R(LOOPCOUNT));
  553. SHR(32, R(LOOPCOUNT_REG), Imm8(8));
  554. AND(32, R(LOOPCOUNT_REG), Imm32(0xff)); // Y-component is the start
  555. MOV(32, R(LOOPINC), R(LOOPCOUNT));
  556. SHR(32, R(LOOPINC), Imm8(16));
  557. MOVZX(32, 8, LOOPINC, R(LOOPINC)); // Z-component is the incrementer
  558. MOVZX(32, 8, LOOPCOUNT, R(LOOPCOUNT)); // X-component is iteration count
  559. ADD(32, R(LOOPCOUNT), Imm8(1)); // Iteration count is X-component + 1
  560. auto loop_start = GetCodePtr();
  561. Compile_Block(instr.flow_control.dest_offset);
  562. ADD(32, R(LOOPCOUNT_REG), R(LOOPINC)); // Increment LOOPCOUNT_REG by Z-component
  563. SUB(32, R(LOOPCOUNT), Imm8(1)); // Increment loop count by 1
  564. J_CC(CC_NZ, loop_start); // Loop if not equal
  565. looping = false;
  566. }
  567. void JitCompiler::Compile_JMP(Instruction instr) {
  568. ASSERT_MSG(instr.flow_control.dest_offset > *offset_ptr, "Backwards jumps not supported");
  569. if (instr.opcode.Value() == OpCode::Id::JMPC)
  570. Compile_EvaluateCondition(instr);
  571. else if (instr.opcode.Value() == OpCode::Id::JMPU)
  572. Compile_UniformCondition(instr);
  573. else
  574. UNREACHABLE();
  575. FixupBranch b = J_CC(CC_NZ, true);
  576. Compile_Block(instr.flow_control.dest_offset);
  577. SetJumpTarget(b);
  578. }
  579. void JitCompiler::Compile_Block(unsigned stop) {
  580. // Save current offset pointer
  581. unsigned* prev_offset_ptr = offset_ptr;
  582. unsigned offset = *prev_offset_ptr;
  583. while (offset <= stop)
  584. Compile_NextInstr(&offset);
  585. // Restore current offset pointer
  586. offset_ptr = prev_offset_ptr;
  587. *offset_ptr = offset;
  588. }
  589. void JitCompiler::Compile_NextInstr(unsigned* offset) {
  590. offset_ptr = offset;
  591. Instruction instr = *(Instruction*)&g_state.vs.program_code[(*offset_ptr)++];
  592. OpCode::Id opcode = instr.opcode.Value();
  593. auto instr_func = instr_table[static_cast<unsigned>(opcode)];
  594. if (instr_func) {
  595. // JIT the instruction!
  596. ((*this).*instr_func)(instr);
  597. } else {
  598. // Unhandled instruction
  599. LOG_CRITICAL(HW_GPU, "Unhandled instruction: 0x%02x (0x%08x)", instr.opcode.Value(), instr.hex);
  600. }
  601. }
  602. CompiledShader* JitCompiler::Compile() {
  603. const u8* start = GetCodePtr();
  604. const auto& code = g_state.vs.program_code;
  605. unsigned offset = g_state.regs.vs.main_offset;
  606. ABI_PushAllCalleeSavedRegsAndAdjustStack();
  607. MOV(PTRBITS, R(REGISTERS), R(ABI_PARAM1));
  608. MOV(PTRBITS, R(UNIFORMS), ImmPtr(&g_state.vs.uniforms));
  609. // Zero address/loop registers
  610. XOR(64, R(ADDROFFS_REG_0), R(ADDROFFS_REG_0));
  611. XOR(64, R(ADDROFFS_REG_1), R(ADDROFFS_REG_1));
  612. XOR(64, R(LOOPCOUNT_REG), R(LOOPCOUNT_REG));
  613. // Used to set a register to one
  614. static const __m128 one = { 1.f, 1.f, 1.f, 1.f };
  615. MOV(PTRBITS, R(RAX), ImmPtr(&one));
  616. MOVAPS(ONE, MatR(RAX));
  617. // Used to negate registers
  618. static const __m128 neg = { -0.f, -0.f, -0.f, -0.f };
  619. MOV(PTRBITS, R(RAX), ImmPtr(&neg));
  620. MOVAPS(NEGBIT, MatR(RAX));
  621. looping = false;
  622. while (offset < g_state.vs.program_code.size()) {
  623. Compile_NextInstr(&offset);
  624. }
  625. return (CompiledShader*)start;
  626. }
  627. JitCompiler::JitCompiler() {
  628. AllocCodeSpace(1024 * 1024 * 4);
  629. }
  630. void JitCompiler::Clear() {
  631. ClearCodeSpace();
  632. }
  633. } // namespace Shader
  634. } // namespace Pica