shader_jit_x64.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. nullptr, // dph
  19. nullptr, // unknown
  20. &JitCompiler::Compile_EX2, // ex2
  21. &JitCompiler::Compile_LG2, // lg2
  22. nullptr, // unknown
  23. &JitCompiler::Compile_MUL, // mul
  24. nullptr, // lge
  25. nullptr, // 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. nullptr, // dphi
  40. nullptr, // unknown
  41. nullptr, // sgei
  42. &JitCompiler::Compile_SLTI, // 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_EX2(Instruction instr) {
  302. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  303. MOVSS(XMM0, R(SRC1));
  304. // The following will actually break the stack alignment
  305. ABI_PushAllCallerSavedRegsAndAdjustStack();
  306. Compile_PushCallerSavedXMM();
  307. ABI_CallFunction(reinterpret_cast<const void*>(exp2f));
  308. Compile_PopCallerSavedXMM();
  309. ABI_PopAllCallerSavedRegsAndAdjustStack();
  310. SHUFPS(XMM0, R(XMM0), _MM_SHUFFLE(0, 0, 0, 0));
  311. MOVAPS(SRC1, R(XMM0));
  312. Compile_DestEnable(instr, SRC1);
  313. }
  314. void JitCompiler::Compile_LG2(Instruction instr) {
  315. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  316. MOVSS(XMM0, R(SRC1));
  317. // The following will actually break the stack alignment
  318. ABI_PushAllCallerSavedRegsAndAdjustStack();
  319. Compile_PushCallerSavedXMM();
  320. ABI_CallFunction(reinterpret_cast<const void*>(log2f));
  321. Compile_PopCallerSavedXMM();
  322. ABI_PopAllCallerSavedRegsAndAdjustStack();
  323. SHUFPS(XMM0, R(XMM0), _MM_SHUFFLE(0, 0, 0, 0));
  324. MOVAPS(SRC1, R(XMM0));
  325. Compile_DestEnable(instr, SRC1);
  326. }
  327. void JitCompiler::Compile_MUL(Instruction instr) {
  328. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  329. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  330. MULPS(SRC1, R(SRC2));
  331. Compile_DestEnable(instr, SRC1);
  332. }
  333. void JitCompiler::Compile_FLR(Instruction instr) {
  334. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  335. if (Common::GetCPUCaps().sse4_1) {
  336. ROUNDFLOORPS(SRC1, R(SRC1));
  337. } else {
  338. CVTPS2DQ(SRC1, R(SRC1));
  339. CVTDQ2PS(SRC1, R(SRC1));
  340. }
  341. Compile_DestEnable(instr, SRC1);
  342. }
  343. void JitCompiler::Compile_MAX(Instruction instr) {
  344. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  345. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  346. MAXPS(SRC1, R(SRC2));
  347. Compile_DestEnable(instr, SRC1);
  348. }
  349. void JitCompiler::Compile_MIN(Instruction instr) {
  350. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  351. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  352. MINPS(SRC1, R(SRC2));
  353. Compile_DestEnable(instr, SRC1);
  354. }
  355. void JitCompiler::Compile_MOVA(Instruction instr) {
  356. SwizzlePattern swiz = { g_state.vs.swizzle_data[instr.common.operand_desc_id] };
  357. if (!swiz.DestComponentEnabled(0) && !swiz.DestComponentEnabled(1)) {
  358. return; // NoOp
  359. }
  360. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  361. // Convert floats to integers (only care about X and Y components)
  362. CVTPS2DQ(SRC1, R(SRC1));
  363. // Get result
  364. MOVQ_xmm(R(RAX), SRC1);
  365. // Handle destination enable
  366. if (swiz.DestComponentEnabled(0) && swiz.DestComponentEnabled(1)) {
  367. // Move and sign-extend low 32 bits
  368. MOVSX(64, 32, ADDROFFS_REG_0, R(RAX));
  369. // Move and sign-extend high 32 bits
  370. SHR(64, R(RAX), Imm8(32));
  371. MOVSX(64, 32, ADDROFFS_REG_1, R(RAX));
  372. // Multiply by 16 to be used as an offset later
  373. SHL(64, R(ADDROFFS_REG_0), Imm8(4));
  374. SHL(64, R(ADDROFFS_REG_1), Imm8(4));
  375. } else {
  376. if (swiz.DestComponentEnabled(0)) {
  377. // Move and sign-extend low 32 bits
  378. MOVSX(64, 32, ADDROFFS_REG_0, R(RAX));
  379. // Multiply by 16 to be used as an offset later
  380. SHL(64, R(ADDROFFS_REG_0), Imm8(4));
  381. } else if (swiz.DestComponentEnabled(1)) {
  382. // Move and sign-extend high 32 bits
  383. SHR(64, R(RAX), Imm8(32));
  384. MOVSX(64, 32, ADDROFFS_REG_1, R(RAX));
  385. // Multiply by 16 to be used as an offset later
  386. SHL(64, R(ADDROFFS_REG_1), Imm8(4));
  387. }
  388. }
  389. }
  390. void JitCompiler::Compile_MOV(Instruction instr) {
  391. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  392. Compile_DestEnable(instr, SRC1);
  393. }
  394. void JitCompiler::Compile_SLTI(Instruction instr) {
  395. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  396. Compile_SwizzleSrc(instr, 1, instr.common.src2i, SRC2);
  397. CMPSS(SRC1, R(SRC2), CMP_LT);
  398. ANDPS(SRC1, R(ONE));
  399. Compile_DestEnable(instr, SRC1);
  400. }
  401. void JitCompiler::Compile_RCP(Instruction instr) {
  402. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  403. // TODO(bunnei): RCPPS is a pretty rough approximation, this might cause problems if Pica
  404. // performs this operation more accurately. This should be checked on hardware.
  405. RCPPS(SRC1, R(SRC1));
  406. Compile_DestEnable(instr, SRC1);
  407. }
  408. void JitCompiler::Compile_RSQ(Instruction instr) {
  409. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  410. // TODO(bunnei): RSQRTPS is a pretty rough approximation, this might cause problems if Pica
  411. // performs this operation more accurately. This should be checked on hardware.
  412. RSQRTPS(SRC1, R(SRC1));
  413. Compile_DestEnable(instr, SRC1);
  414. }
  415. void JitCompiler::Compile_NOP(Instruction instr) {
  416. }
  417. void JitCompiler::Compile_END(Instruction instr) {
  418. ABI_PopAllCalleeSavedRegsAndAdjustStack();
  419. RET();
  420. }
  421. void JitCompiler::Compile_CALL(Instruction instr) {
  422. unsigned offset = instr.flow_control.dest_offset;
  423. while (offset < (instr.flow_control.dest_offset + instr.flow_control.num_instructions)) {
  424. Compile_NextInstr(&offset);
  425. }
  426. }
  427. void JitCompiler::Compile_CALLC(Instruction instr) {
  428. Compile_EvaluateCondition(instr);
  429. FixupBranch b = J_CC(CC_Z, true);
  430. Compile_CALL(instr);
  431. SetJumpTarget(b);
  432. }
  433. void JitCompiler::Compile_CALLU(Instruction instr) {
  434. Compile_UniformCondition(instr);
  435. FixupBranch b = J_CC(CC_Z, true);
  436. Compile_CALL(instr);
  437. SetJumpTarget(b);
  438. }
  439. void JitCompiler::Compile_CMP(Instruction instr) {
  440. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  441. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  442. static const u8 cmp[] = { CMP_EQ, CMP_NEQ, CMP_LT, CMP_LE, CMP_NLE, CMP_NLT };
  443. if (instr.common.compare_op.x == instr.common.compare_op.y) {
  444. // Compare X-component and Y-component together
  445. CMPPS(SRC1, R(SRC2), cmp[instr.common.compare_op.x]);
  446. MOVQ_xmm(R(COND0), SRC1);
  447. MOV(64, R(COND1), R(COND0));
  448. } else {
  449. // Compare X-component
  450. MOVAPS(SCRATCH, R(SRC1));
  451. CMPSS(SCRATCH, R(SRC2), cmp[instr.common.compare_op.x]);
  452. // Compare Y-component
  453. CMPPS(SRC1, R(SRC2), cmp[instr.common.compare_op.y]);
  454. MOVQ_xmm(R(COND0), SCRATCH);
  455. MOVQ_xmm(R(COND1), SRC1);
  456. }
  457. SHR(32, R(COND0), Imm8(31));
  458. SHR(64, R(COND1), Imm8(63));
  459. }
  460. void JitCompiler::Compile_MAD(Instruction instr) {
  461. Compile_SwizzleSrc(instr, 1, instr.mad.src1, SRC1);
  462. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  463. Compile_SwizzleSrc(instr, 2, instr.mad.src2i, SRC2);
  464. Compile_SwizzleSrc(instr, 3, instr.mad.src3i, SRC3);
  465. } else {
  466. Compile_SwizzleSrc(instr, 2, instr.mad.src2, SRC2);
  467. Compile_SwizzleSrc(instr, 3, instr.mad.src3, SRC3);
  468. }
  469. if (Common::GetCPUCaps().fma) {
  470. VFMADD213PS(SRC1, SRC2, R(SRC3));
  471. } else {
  472. MULPS(SRC1, R(SRC2));
  473. ADDPS(SRC1, R(SRC3));
  474. }
  475. Compile_DestEnable(instr, SRC1);
  476. }
  477. void JitCompiler::Compile_IF(Instruction instr) {
  478. ASSERT_MSG(instr.flow_control.dest_offset > *offset_ptr, "Backwards if-statements not supported");
  479. // Evaluate the "IF" condition
  480. if (instr.opcode.Value() == OpCode::Id::IFU) {
  481. Compile_UniformCondition(instr);
  482. } else if (instr.opcode.Value() == OpCode::Id::IFC) {
  483. Compile_EvaluateCondition(instr);
  484. }
  485. FixupBranch b = J_CC(CC_Z, true);
  486. // Compile the code that corresponds to the condition evaluating as true
  487. Compile_Block(instr.flow_control.dest_offset - 1);
  488. // If there isn't an "ELSE" condition, we are done here
  489. if (instr.flow_control.num_instructions == 0) {
  490. SetJumpTarget(b);
  491. return;
  492. }
  493. FixupBranch b2 = J(true);
  494. SetJumpTarget(b);
  495. // This code corresponds to the "ELSE" condition
  496. // Comple the code that corresponds to the condition evaluating as false
  497. Compile_Block(instr.flow_control.dest_offset + instr.flow_control.num_instructions - 1);
  498. SetJumpTarget(b2);
  499. }
  500. void JitCompiler::Compile_LOOP(Instruction instr) {
  501. ASSERT_MSG(instr.flow_control.dest_offset > *offset_ptr, "Backwards loops not supported");
  502. ASSERT_MSG(!looping, "Nested loops not supported");
  503. looping = true;
  504. int offset = offsetof(decltype(g_state.vs.uniforms), i) + (instr.flow_control.int_uniform_id * sizeof(Math::Vec4<u8>));
  505. MOV(32, R(LOOPCOUNT), MDisp(UNIFORMS, offset));
  506. MOV(32, R(LOOPCOUNT_REG), R(LOOPCOUNT));
  507. SHR(32, R(LOOPCOUNT_REG), Imm8(8));
  508. AND(32, R(LOOPCOUNT_REG), Imm32(0xff)); // Y-component is the start
  509. MOV(32, R(LOOPINC), R(LOOPCOUNT));
  510. SHR(32, R(LOOPINC), Imm8(16));
  511. MOVZX(32, 8, LOOPINC, R(LOOPINC)); // Z-component is the incrementer
  512. MOVZX(32, 8, LOOPCOUNT, R(LOOPCOUNT)); // X-component is iteration count
  513. ADD(32, R(LOOPCOUNT), Imm8(1)); // Iteration count is X-component + 1
  514. auto loop_start = GetCodePtr();
  515. Compile_Block(instr.flow_control.dest_offset);
  516. ADD(32, R(LOOPCOUNT_REG), R(LOOPINC)); // Increment LOOPCOUNT_REG by Z-component
  517. SUB(32, R(LOOPCOUNT), Imm8(1)); // Increment loop count by 1
  518. J_CC(CC_NZ, loop_start); // Loop if not equal
  519. looping = false;
  520. }
  521. void JitCompiler::Compile_JMP(Instruction instr) {
  522. ASSERT_MSG(instr.flow_control.dest_offset > *offset_ptr, "Backwards jumps not supported");
  523. if (instr.opcode.Value() == OpCode::Id::JMPC)
  524. Compile_EvaluateCondition(instr);
  525. else if (instr.opcode.Value() == OpCode::Id::JMPU)
  526. Compile_UniformCondition(instr);
  527. else
  528. UNREACHABLE();
  529. FixupBranch b = J_CC(CC_NZ, true);
  530. Compile_Block(instr.flow_control.dest_offset);
  531. SetJumpTarget(b);
  532. }
  533. void JitCompiler::Compile_Block(unsigned stop) {
  534. // Save current offset pointer
  535. unsigned* prev_offset_ptr = offset_ptr;
  536. unsigned offset = *prev_offset_ptr;
  537. while (offset <= stop)
  538. Compile_NextInstr(&offset);
  539. // Restore current offset pointer
  540. offset_ptr = prev_offset_ptr;
  541. *offset_ptr = offset;
  542. }
  543. void JitCompiler::Compile_NextInstr(unsigned* offset) {
  544. offset_ptr = offset;
  545. Instruction instr = *(Instruction*)&g_state.vs.program_code[(*offset_ptr)++];
  546. OpCode::Id opcode = instr.opcode.Value();
  547. auto instr_func = instr_table[static_cast<unsigned>(opcode)];
  548. if (instr_func) {
  549. // JIT the instruction!
  550. ((*this).*instr_func)(instr);
  551. } else {
  552. // Unhandled instruction
  553. LOG_CRITICAL(HW_GPU, "Unhandled instruction: 0x%02x (0x%08x)", instr.opcode.Value(), instr.hex);
  554. }
  555. }
  556. CompiledShader* JitCompiler::Compile() {
  557. const u8* start = GetCodePtr();
  558. const auto& code = g_state.vs.program_code;
  559. unsigned offset = g_state.regs.vs.main_offset;
  560. ABI_PushAllCalleeSavedRegsAndAdjustStack();
  561. MOV(PTRBITS, R(REGISTERS), R(ABI_PARAM1));
  562. MOV(PTRBITS, R(UNIFORMS), ImmPtr(&g_state.vs.uniforms));
  563. // Zero address/loop registers
  564. XOR(64, R(ADDROFFS_REG_0), R(ADDROFFS_REG_0));
  565. XOR(64, R(ADDROFFS_REG_1), R(ADDROFFS_REG_1));
  566. XOR(64, R(LOOPCOUNT_REG), R(LOOPCOUNT_REG));
  567. // Used to set a register to one
  568. static const __m128 one = { 1.f, 1.f, 1.f, 1.f };
  569. MOV(PTRBITS, R(RAX), ImmPtr(&one));
  570. MOVAPS(ONE, MDisp(RAX, 0));
  571. // Used to negate registers
  572. static const __m128 neg = { -0.f, -0.f, -0.f, -0.f };
  573. MOV(PTRBITS, R(RAX), ImmPtr(&neg));
  574. MOVAPS(NEGBIT, MDisp(RAX, 0));
  575. looping = false;
  576. while (offset < g_state.vs.program_code.size()) {
  577. Compile_NextInstr(&offset);
  578. }
  579. return (CompiledShader*)start;
  580. }
  581. JitCompiler::JitCompiler() {
  582. AllocCodeSpace(1024 * 1024 * 4);
  583. }
  584. void JitCompiler::Clear() {
  585. ClearCodeSpace();
  586. }
  587. } // namespace Shader
  588. } // namespace Pica