shader_jit_x64.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. /// Additional scratch register
  109. static const X64Reg SCRATCH2 = XMM4;
  110. /// Constant vector of [1.0f, 1.0f, 1.0f, 1.0f], used to efficiently set a vector to one
  111. static const X64Reg ONE = XMM14;
  112. /// Constant vector of [-0.f, -0.f, -0.f, -0.f], used to efficiently negate a vector with XOR
  113. static const X64Reg NEGBIT = XMM15;
  114. // State registers that must not be modified by external functions calls
  115. // Scratch registers, e.g., SRC1 and SCRATCH, have to be saved on the side if needed
  116. static const BitSet32 persistent_regs = {
  117. UNIFORMS, REGISTERS, // Pointers to register blocks
  118. ADDROFFS_REG_0, ADDROFFS_REG_1, LOOPCOUNT_REG, COND0, COND1, // Cached registers
  119. ONE+16, NEGBIT+16, // Constants
  120. };
  121. /// Raw constant for the source register selector that indicates no swizzling is performed
  122. static const u8 NO_SRC_REG_SWIZZLE = 0x1b;
  123. /// Raw constant for the destination register enable mask that indicates all components are enabled
  124. static const u8 NO_DEST_REG_MASK = 0xf;
  125. /**
  126. * Loads and swizzles a source register into the specified XMM register.
  127. * @param instr VS instruction, used for determining how to load the source register
  128. * @param src_num Number indicating which source register to load (1 = src1, 2 = src2, 3 = src3)
  129. * @param src_reg SourceRegister object corresponding to the source register to load
  130. * @param dest Destination XMM register to store the loaded, swizzled source register
  131. */
  132. void JitCompiler::Compile_SwizzleSrc(Instruction instr, unsigned src_num, SourceRegister src_reg, X64Reg dest) {
  133. X64Reg src_ptr;
  134. size_t src_offset;
  135. if (src_reg.GetRegisterType() == RegisterType::FloatUniform) {
  136. src_ptr = UNIFORMS;
  137. src_offset = src_reg.GetIndex() * sizeof(float24) * 4;
  138. } else {
  139. src_ptr = REGISTERS;
  140. src_offset = UnitState<false>::InputOffset(src_reg);
  141. }
  142. int src_offset_disp = (int)src_offset;
  143. ASSERT_MSG(src_offset == src_offset_disp, "Source register offset too large for int type");
  144. unsigned operand_desc_id;
  145. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MAD ||
  146. instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  147. // The MAD and MADI instructions do not use the address offset registers, so loading the
  148. // source is a bit simpler here
  149. operand_desc_id = instr.mad.operand_desc_id;
  150. // Load the source
  151. MOVAPS(dest, MDisp(src_ptr, src_offset_disp));
  152. } else {
  153. operand_desc_id = instr.common.operand_desc_id;
  154. const bool is_inverted = (0 != (instr.opcode.Value().GetInfo().subtype & OpCode::Info::SrcInversed));
  155. unsigned offset_src = is_inverted ? 2 : 1;
  156. if (src_num == offset_src && instr.common.address_register_index != 0) {
  157. switch (instr.common.address_register_index) {
  158. case 1: // address offset 1
  159. MOVAPS(dest, MComplex(src_ptr, ADDROFFS_REG_0, SCALE_1, src_offset_disp));
  160. break;
  161. case 2: // address offset 2
  162. MOVAPS(dest, MComplex(src_ptr, ADDROFFS_REG_1, SCALE_1, src_offset_disp));
  163. break;
  164. case 3: // address offset 3
  165. MOVAPS(dest, MComplex(src_ptr, LOOPCOUNT_REG, SCALE_1, src_offset_disp));
  166. break;
  167. default:
  168. UNREACHABLE();
  169. break;
  170. }
  171. } else {
  172. // Load the source
  173. MOVAPS(dest, MDisp(src_ptr, src_offset_disp));
  174. }
  175. }
  176. SwizzlePattern swiz = { g_state.vs.swizzle_data[operand_desc_id] };
  177. // Generate instructions for source register swizzling as needed
  178. u8 sel = swiz.GetRawSelector(src_num);
  179. if (sel != NO_SRC_REG_SWIZZLE) {
  180. // Selector component order needs to be reversed for the SHUFPS instruction
  181. sel = ((sel & 0xc0) >> 6) | ((sel & 3) << 6) | ((sel & 0xc) << 2) | ((sel & 0x30) >> 2);
  182. // Shuffle inputs for swizzle
  183. SHUFPS(dest, R(dest), sel);
  184. }
  185. // If the source register should be negated, flip the negative bit using XOR
  186. const bool negate[] = { swiz.negate_src1, swiz.negate_src2, swiz.negate_src3 };
  187. if (negate[src_num - 1]) {
  188. XORPS(dest, R(NEGBIT));
  189. }
  190. }
  191. void JitCompiler::Compile_DestEnable(Instruction instr,X64Reg src) {
  192. DestRegister dest;
  193. unsigned operand_desc_id;
  194. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MAD ||
  195. instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  196. operand_desc_id = instr.mad.operand_desc_id;
  197. dest = instr.mad.dest.Value();
  198. } else {
  199. operand_desc_id = instr.common.operand_desc_id;
  200. dest = instr.common.dest.Value();
  201. }
  202. SwizzlePattern swiz = { g_state.vs.swizzle_data[operand_desc_id] };
  203. int dest_offset_disp = (int)UnitState<false>::OutputOffset(dest);
  204. ASSERT_MSG(dest_offset_disp == UnitState<false>::OutputOffset(dest), "Destinaton offset too large for int type");
  205. // If all components are enabled, write the result to the destination register
  206. if (swiz.dest_mask == NO_DEST_REG_MASK) {
  207. // Store dest back to memory
  208. MOVAPS(MDisp(REGISTERS, dest_offset_disp), src);
  209. } else {
  210. // Not all components are enabled, so mask the result when storing to the destination register...
  211. MOVAPS(SCRATCH, MDisp(REGISTERS, dest_offset_disp));
  212. if (Common::GetCPUCaps().sse4_1) {
  213. u8 mask = ((swiz.dest_mask & 1) << 3) | ((swiz.dest_mask & 8) >> 3) | ((swiz.dest_mask & 2) << 1) | ((swiz.dest_mask & 4) >> 1);
  214. BLENDPS(SCRATCH, R(src), mask);
  215. } else {
  216. MOVAPS(SCRATCH2, R(src));
  217. UNPCKHPS(SCRATCH2, R(SCRATCH)); // Unpack X/Y components of source and destination
  218. UNPCKLPS(SCRATCH, R(src)); // Unpack Z/W components of source and destination
  219. // Compute selector to selectively copy source components to destination for SHUFPS instruction
  220. u8 sel = ((swiz.DestComponentEnabled(0) ? 1 : 0) << 0) |
  221. ((swiz.DestComponentEnabled(1) ? 3 : 2) << 2) |
  222. ((swiz.DestComponentEnabled(2) ? 0 : 1) << 4) |
  223. ((swiz.DestComponentEnabled(3) ? 2 : 3) << 6);
  224. SHUFPS(SCRATCH, R(SCRATCH2), sel);
  225. }
  226. // Store dest back to memory
  227. MOVAPS(MDisp(REGISTERS, dest_offset_disp), SCRATCH);
  228. }
  229. }
  230. void JitCompiler::Compile_SanitizedMul(Gen::X64Reg src1, Gen::X64Reg src2, Gen::X64Reg scratch) {
  231. MOVAPS(scratch, R(src1));
  232. CMPPS(scratch, R(src2), CMP_ORD);
  233. MULPS(src1, R(src2));
  234. MOVAPS(src2, R(src1));
  235. CMPPS(src2, R(src2), CMP_UNORD);
  236. XORPS(scratch, R(src2));
  237. ANDPS(src1, R(scratch));
  238. }
  239. void JitCompiler::Compile_EvaluateCondition(Instruction instr) {
  240. // Note: NXOR is used below to check for equality
  241. switch (instr.flow_control.op) {
  242. case Instruction::FlowControlType::Or:
  243. MOV(32, R(RAX), R(COND0));
  244. MOV(32, R(RBX), R(COND1));
  245. XOR(32, R(RAX), Imm32(instr.flow_control.refx.Value() ^ 1));
  246. XOR(32, R(RBX), Imm32(instr.flow_control.refy.Value() ^ 1));
  247. OR(32, R(RAX), R(RBX));
  248. break;
  249. case Instruction::FlowControlType::And:
  250. MOV(32, R(RAX), R(COND0));
  251. MOV(32, R(RBX), R(COND1));
  252. XOR(32, R(RAX), Imm32(instr.flow_control.refx.Value() ^ 1));
  253. XOR(32, R(RBX), Imm32(instr.flow_control.refy.Value() ^ 1));
  254. AND(32, R(RAX), R(RBX));
  255. break;
  256. case Instruction::FlowControlType::JustX:
  257. MOV(32, R(RAX), R(COND0));
  258. XOR(32, R(RAX), Imm32(instr.flow_control.refx.Value() ^ 1));
  259. break;
  260. case Instruction::FlowControlType::JustY:
  261. MOV(32, R(RAX), R(COND1));
  262. XOR(32, R(RAX), Imm32(instr.flow_control.refy.Value() ^ 1));
  263. break;
  264. }
  265. }
  266. void JitCompiler::Compile_UniformCondition(Instruction instr) {
  267. int offset = offsetof(decltype(g_state.vs.uniforms), b) + (instr.flow_control.bool_uniform_id * sizeof(bool));
  268. CMP(sizeof(bool) * 8, MDisp(UNIFORMS, offset), Imm8(0));
  269. }
  270. BitSet32 JitCompiler::PersistentCallerSavedRegs() {
  271. return persistent_regs & ABI_ALL_CALLER_SAVED;
  272. }
  273. void JitCompiler::Compile_ADD(Instruction instr) {
  274. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  275. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  276. ADDPS(SRC1, R(SRC2));
  277. Compile_DestEnable(instr, SRC1);
  278. }
  279. void JitCompiler::Compile_DP3(Instruction instr) {
  280. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  281. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  282. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  283. MOVAPS(SRC2, R(SRC1));
  284. SHUFPS(SRC2, R(SRC2), _MM_SHUFFLE(1, 1, 1, 1));
  285. MOVAPS(SRC3, R(SRC1));
  286. SHUFPS(SRC3, R(SRC3), _MM_SHUFFLE(2, 2, 2, 2));
  287. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0));
  288. ADDPS(SRC1, R(SRC2));
  289. ADDPS(SRC1, R(SRC3));
  290. Compile_DestEnable(instr, SRC1);
  291. }
  292. void JitCompiler::Compile_DP4(Instruction instr) {
  293. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  294. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  295. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  296. MOVAPS(SRC2, R(SRC1));
  297. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(2, 3, 0, 1)); // XYZW -> ZWXY
  298. ADDPS(SRC1, R(SRC2));
  299. MOVAPS(SRC2, R(SRC1));
  300. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 1, 2, 3)); // XYZW -> WZYX
  301. ADDPS(SRC1, R(SRC2));
  302. Compile_DestEnable(instr, SRC1);
  303. }
  304. void JitCompiler::Compile_DPH(Instruction instr) {
  305. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::DPHI) {
  306. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  307. Compile_SwizzleSrc(instr, 2, instr.common.src2i, SRC2);
  308. } else {
  309. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  310. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  311. }
  312. if (Common::GetCPUCaps().sse4_1) {
  313. // Set 4th component to 1.0
  314. BLENDPS(SRC1, R(ONE), 0x8); // 0b1000
  315. } else {
  316. // Set 4th component to 1.0
  317. MOVAPS(SCRATCH, R(SRC1));
  318. UNPCKHPS(SCRATCH, R(ONE)); // XYZW, 1111 -> Z1__
  319. UNPCKLPD(SRC1, R(SCRATCH)); // XYZW, Z1__ -> XYZ1
  320. }
  321. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  322. MOVAPS(SRC2, R(SRC1));
  323. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(2, 3, 0, 1)); // XYZW -> ZWXY
  324. ADDPS(SRC1, R(SRC2));
  325. MOVAPS(SRC2, R(SRC1));
  326. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 1, 2, 3)); // XYZW -> WZYX
  327. ADDPS(SRC1, R(SRC2));
  328. Compile_DestEnable(instr, SRC1);
  329. }
  330. void JitCompiler::Compile_EX2(Instruction instr) {
  331. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  332. MOVSS(XMM0, R(SRC1));
  333. ABI_PushRegistersAndAdjustStack(PersistentCallerSavedRegs(), 0);
  334. ABI_CallFunction(reinterpret_cast<const void*>(exp2f));
  335. ABI_PopRegistersAndAdjustStack(PersistentCallerSavedRegs(), 0);
  336. SHUFPS(XMM0, R(XMM0), _MM_SHUFFLE(0, 0, 0, 0));
  337. MOVAPS(SRC1, R(XMM0));
  338. Compile_DestEnable(instr, SRC1);
  339. }
  340. void JitCompiler::Compile_LG2(Instruction instr) {
  341. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  342. MOVSS(XMM0, R(SRC1));
  343. ABI_PushRegistersAndAdjustStack(PersistentCallerSavedRegs(), 0);
  344. ABI_CallFunction(reinterpret_cast<const void*>(log2f));
  345. ABI_PopRegistersAndAdjustStack(PersistentCallerSavedRegs(), 0);
  346. SHUFPS(XMM0, R(XMM0), _MM_SHUFFLE(0, 0, 0, 0));
  347. MOVAPS(SRC1, R(XMM0));
  348. Compile_DestEnable(instr, SRC1);
  349. }
  350. void JitCompiler::Compile_MUL(Instruction instr) {
  351. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  352. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  353. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  354. Compile_DestEnable(instr, SRC1);
  355. }
  356. void JitCompiler::Compile_SGE(Instruction instr) {
  357. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::SGEI) {
  358. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  359. Compile_SwizzleSrc(instr, 2, instr.common.src2i, SRC2);
  360. } else {
  361. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  362. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  363. }
  364. CMPPS(SRC2, R(SRC1), CMP_LE);
  365. ANDPS(SRC2, R(ONE));
  366. Compile_DestEnable(instr, SRC2);
  367. }
  368. void JitCompiler::Compile_SLT(Instruction instr) {
  369. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::SLTI) {
  370. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  371. Compile_SwizzleSrc(instr, 2, instr.common.src2i, SRC2);
  372. } else {
  373. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  374. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  375. }
  376. CMPPS(SRC1, R(SRC2), CMP_LT);
  377. ANDPS(SRC1, R(ONE));
  378. Compile_DestEnable(instr, SRC1);
  379. }
  380. void JitCompiler::Compile_FLR(Instruction instr) {
  381. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  382. if (Common::GetCPUCaps().sse4_1) {
  383. ROUNDFLOORPS(SRC1, R(SRC1));
  384. } else {
  385. CVTPS2DQ(SRC1, R(SRC1));
  386. CVTDQ2PS(SRC1, R(SRC1));
  387. }
  388. Compile_DestEnable(instr, SRC1);
  389. }
  390. void JitCompiler::Compile_MAX(Instruction instr) {
  391. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  392. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  393. // SSE semantics match PICA200 ones: In case of NaN, SRC2 is returned.
  394. MAXPS(SRC1, R(SRC2));
  395. Compile_DestEnable(instr, SRC1);
  396. }
  397. void JitCompiler::Compile_MIN(Instruction instr) {
  398. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  399. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  400. // SSE semantics match PICA200 ones: In case of NaN, SRC2 is returned.
  401. MINPS(SRC1, R(SRC2));
  402. Compile_DestEnable(instr, SRC1);
  403. }
  404. void JitCompiler::Compile_MOVA(Instruction instr) {
  405. SwizzlePattern swiz = { g_state.vs.swizzle_data[instr.common.operand_desc_id] };
  406. if (!swiz.DestComponentEnabled(0) && !swiz.DestComponentEnabled(1)) {
  407. return; // NoOp
  408. }
  409. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  410. // Convert floats to integers using truncation (only care about X and Y components)
  411. CVTTPS2DQ(SRC1, R(SRC1));
  412. // Get result
  413. MOVQ_xmm(R(RAX), SRC1);
  414. // Handle destination enable
  415. if (swiz.DestComponentEnabled(0) && swiz.DestComponentEnabled(1)) {
  416. // Move and sign-extend low 32 bits
  417. MOVSX(64, 32, ADDROFFS_REG_0, R(RAX));
  418. // Move and sign-extend high 32 bits
  419. SHR(64, R(RAX), Imm8(32));
  420. MOVSX(64, 32, ADDROFFS_REG_1, R(RAX));
  421. // Multiply by 16 to be used as an offset later
  422. SHL(64, R(ADDROFFS_REG_0), Imm8(4));
  423. SHL(64, R(ADDROFFS_REG_1), Imm8(4));
  424. } else {
  425. if (swiz.DestComponentEnabled(0)) {
  426. // Move and sign-extend low 32 bits
  427. MOVSX(64, 32, ADDROFFS_REG_0, R(RAX));
  428. // Multiply by 16 to be used as an offset later
  429. SHL(64, R(ADDROFFS_REG_0), Imm8(4));
  430. } else if (swiz.DestComponentEnabled(1)) {
  431. // Move and sign-extend high 32 bits
  432. SHR(64, R(RAX), Imm8(32));
  433. MOVSX(64, 32, ADDROFFS_REG_1, R(RAX));
  434. // Multiply by 16 to be used as an offset later
  435. SHL(64, R(ADDROFFS_REG_1), Imm8(4));
  436. }
  437. }
  438. }
  439. void JitCompiler::Compile_MOV(Instruction instr) {
  440. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  441. Compile_DestEnable(instr, SRC1);
  442. }
  443. void JitCompiler::Compile_RCP(Instruction instr) {
  444. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  445. // TODO(bunnei): RCPSS is a pretty rough approximation, this might cause problems if Pica
  446. // performs this operation more accurately. This should be checked on hardware.
  447. RCPSS(SRC1, R(SRC1));
  448. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0)); // XYWZ -> XXXX
  449. Compile_DestEnable(instr, SRC1);
  450. }
  451. void JitCompiler::Compile_RSQ(Instruction instr) {
  452. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  453. // TODO(bunnei): RSQRTSS is a pretty rough approximation, this might cause problems if Pica
  454. // performs this operation more accurately. This should be checked on hardware.
  455. RSQRTSS(SRC1, R(SRC1));
  456. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0)); // XYWZ -> XXXX
  457. Compile_DestEnable(instr, SRC1);
  458. }
  459. void JitCompiler::Compile_NOP(Instruction instr) {
  460. }
  461. void JitCompiler::Compile_END(Instruction instr) {
  462. ABI_PopRegistersAndAdjustStack(ABI_ALL_CALLEE_SAVED, 8);
  463. RET();
  464. }
  465. void JitCompiler::Compile_CALL(Instruction instr) {
  466. unsigned offset = instr.flow_control.dest_offset;
  467. while (offset < (instr.flow_control.dest_offset + instr.flow_control.num_instructions)) {
  468. Compile_NextInstr(&offset);
  469. }
  470. }
  471. void JitCompiler::Compile_CALLC(Instruction instr) {
  472. Compile_EvaluateCondition(instr);
  473. FixupBranch b = J_CC(CC_Z, true);
  474. Compile_CALL(instr);
  475. SetJumpTarget(b);
  476. }
  477. void JitCompiler::Compile_CALLU(Instruction instr) {
  478. Compile_UniformCondition(instr);
  479. FixupBranch b = J_CC(CC_Z, true);
  480. Compile_CALL(instr);
  481. SetJumpTarget(b);
  482. }
  483. void JitCompiler::Compile_CMP(Instruction instr) {
  484. using Op = Instruction::Common::CompareOpType::Op;
  485. Op op_x = instr.common.compare_op.x;
  486. Op op_y = instr.common.compare_op.y;
  487. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  488. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  489. // SSE doesn't have greater-than (GT) or greater-equal (GE) comparison operators. You need to
  490. // emulate them by swapping the lhs and rhs and using LT and LE. NLT and NLE can't be used here
  491. // because they don't match when used with NaNs.
  492. static const u8 cmp[] = { CMP_EQ, CMP_NEQ, CMP_LT, CMP_LE, CMP_LT, CMP_LE };
  493. bool invert_op_x = (op_x == Op::GreaterThan || op_x == Op::GreaterEqual);
  494. Gen::X64Reg lhs_x = invert_op_x ? SRC2 : SRC1;
  495. Gen::X64Reg rhs_x = invert_op_x ? SRC1 : SRC2;
  496. if (op_x == op_y) {
  497. // Compare X-component and Y-component together
  498. CMPPS(lhs_x, R(rhs_x), cmp[op_x]);
  499. MOVQ_xmm(R(COND0), lhs_x);
  500. MOV(64, R(COND1), R(COND0));
  501. } else {
  502. bool invert_op_y = (op_y == Op::GreaterThan || op_y == Op::GreaterEqual);
  503. Gen::X64Reg lhs_y = invert_op_y ? SRC2 : SRC1;
  504. Gen::X64Reg rhs_y = invert_op_y ? SRC1 : SRC2;
  505. // Compare X-component
  506. MOVAPS(SCRATCH, R(lhs_x));
  507. CMPSS(SCRATCH, R(rhs_x), cmp[op_x]);
  508. // Compare Y-component
  509. CMPPS(lhs_y, R(rhs_y), cmp[op_y]);
  510. MOVQ_xmm(R(COND0), SCRATCH);
  511. MOVQ_xmm(R(COND1), lhs_y);
  512. }
  513. SHR(32, R(COND0), Imm8(31));
  514. SHR(64, R(COND1), Imm8(63));
  515. }
  516. void JitCompiler::Compile_MAD(Instruction instr) {
  517. Compile_SwizzleSrc(instr, 1, instr.mad.src1, SRC1);
  518. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  519. Compile_SwizzleSrc(instr, 2, instr.mad.src2i, SRC2);
  520. Compile_SwizzleSrc(instr, 3, instr.mad.src3i, SRC3);
  521. } else {
  522. Compile_SwizzleSrc(instr, 2, instr.mad.src2, SRC2);
  523. Compile_SwizzleSrc(instr, 3, instr.mad.src3, SRC3);
  524. }
  525. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  526. ADDPS(SRC1, R(SRC3));
  527. Compile_DestEnable(instr, SRC1);
  528. }
  529. void JitCompiler::Compile_IF(Instruction instr) {
  530. ASSERT_MSG(instr.flow_control.dest_offset > *offset_ptr, "Backwards if-statements not supported");
  531. // Evaluate the "IF" condition
  532. if (instr.opcode.Value() == OpCode::Id::IFU) {
  533. Compile_UniformCondition(instr);
  534. } else if (instr.opcode.Value() == OpCode::Id::IFC) {
  535. Compile_EvaluateCondition(instr);
  536. }
  537. FixupBranch b = J_CC(CC_Z, true);
  538. // Compile the code that corresponds to the condition evaluating as true
  539. Compile_Block(instr.flow_control.dest_offset - 1);
  540. // If there isn't an "ELSE" condition, we are done here
  541. if (instr.flow_control.num_instructions == 0) {
  542. SetJumpTarget(b);
  543. return;
  544. }
  545. FixupBranch b2 = J(true);
  546. SetJumpTarget(b);
  547. // This code corresponds to the "ELSE" condition
  548. // Comple the code that corresponds to the condition evaluating as false
  549. Compile_Block(instr.flow_control.dest_offset + instr.flow_control.num_instructions - 1);
  550. SetJumpTarget(b2);
  551. }
  552. void JitCompiler::Compile_LOOP(Instruction instr) {
  553. ASSERT_MSG(instr.flow_control.dest_offset > *offset_ptr, "Backwards loops not supported");
  554. ASSERT_MSG(!looping, "Nested loops not supported");
  555. looping = true;
  556. int offset = offsetof(decltype(g_state.vs.uniforms), i) + (instr.flow_control.int_uniform_id * sizeof(Math::Vec4<u8>));
  557. MOV(32, R(LOOPCOUNT), MDisp(UNIFORMS, offset));
  558. MOV(32, R(LOOPCOUNT_REG), R(LOOPCOUNT));
  559. SHR(32, R(LOOPCOUNT_REG), Imm8(8));
  560. AND(32, R(LOOPCOUNT_REG), Imm32(0xff)); // Y-component is the start
  561. MOV(32, R(LOOPINC), R(LOOPCOUNT));
  562. SHR(32, R(LOOPINC), Imm8(16));
  563. MOVZX(32, 8, LOOPINC, R(LOOPINC)); // Z-component is the incrementer
  564. MOVZX(32, 8, LOOPCOUNT, R(LOOPCOUNT)); // X-component is iteration count
  565. ADD(32, R(LOOPCOUNT), Imm8(1)); // Iteration count is X-component + 1
  566. auto loop_start = GetCodePtr();
  567. Compile_Block(instr.flow_control.dest_offset);
  568. ADD(32, R(LOOPCOUNT_REG), R(LOOPINC)); // Increment LOOPCOUNT_REG by Z-component
  569. SUB(32, R(LOOPCOUNT), Imm8(1)); // Increment loop count by 1
  570. J_CC(CC_NZ, loop_start); // Loop if not equal
  571. looping = false;
  572. }
  573. void JitCompiler::Compile_JMP(Instruction instr) {
  574. ASSERT_MSG(instr.flow_control.dest_offset > *offset_ptr, "Backwards jumps not supported");
  575. if (instr.opcode.Value() == OpCode::Id::JMPC)
  576. Compile_EvaluateCondition(instr);
  577. else if (instr.opcode.Value() == OpCode::Id::JMPU)
  578. Compile_UniformCondition(instr);
  579. else
  580. UNREACHABLE();
  581. FixupBranch b = J_CC(CC_NZ, true);
  582. Compile_Block(instr.flow_control.dest_offset);
  583. SetJumpTarget(b);
  584. }
  585. void JitCompiler::Compile_Block(unsigned stop) {
  586. // Save current offset pointer
  587. unsigned* prev_offset_ptr = offset_ptr;
  588. unsigned offset = *prev_offset_ptr;
  589. while (offset <= stop)
  590. Compile_NextInstr(&offset);
  591. // Restore current offset pointer
  592. offset_ptr = prev_offset_ptr;
  593. *offset_ptr = offset;
  594. }
  595. void JitCompiler::Compile_NextInstr(unsigned* offset) {
  596. offset_ptr = offset;
  597. Instruction instr = *(Instruction*)&g_state.vs.program_code[(*offset_ptr)++];
  598. OpCode::Id opcode = instr.opcode.Value();
  599. auto instr_func = instr_table[static_cast<unsigned>(opcode)];
  600. if (instr_func) {
  601. // JIT the instruction!
  602. ((*this).*instr_func)(instr);
  603. } else {
  604. // Unhandled instruction
  605. LOG_CRITICAL(HW_GPU, "Unhandled instruction: 0x%02x (0x%08x)",
  606. instr.opcode.Value().EffectiveOpCode(), instr.hex);
  607. }
  608. }
  609. CompiledShader* JitCompiler::Compile() {
  610. const u8* start = GetCodePtr();
  611. unsigned offset = g_state.regs.vs.main_offset;
  612. // The stack pointer is 8 modulo 16 at the entry of a procedure
  613. ABI_PushRegistersAndAdjustStack(ABI_ALL_CALLEE_SAVED, 8);
  614. MOV(PTRBITS, R(REGISTERS), R(ABI_PARAM1));
  615. MOV(PTRBITS, R(UNIFORMS), ImmPtr(&g_state.vs.uniforms));
  616. // Zero address/loop registers
  617. XOR(64, R(ADDROFFS_REG_0), R(ADDROFFS_REG_0));
  618. XOR(64, R(ADDROFFS_REG_1), R(ADDROFFS_REG_1));
  619. XOR(64, R(LOOPCOUNT_REG), R(LOOPCOUNT_REG));
  620. // Used to set a register to one
  621. static const __m128 one = { 1.f, 1.f, 1.f, 1.f };
  622. MOV(PTRBITS, R(RAX), ImmPtr(&one));
  623. MOVAPS(ONE, MatR(RAX));
  624. // Used to negate registers
  625. static const __m128 neg = { -0.f, -0.f, -0.f, -0.f };
  626. MOV(PTRBITS, R(RAX), ImmPtr(&neg));
  627. MOVAPS(NEGBIT, MatR(RAX));
  628. looping = false;
  629. while (offset < g_state.vs.program_code.size()) {
  630. Compile_NextInstr(&offset);
  631. }
  632. return (CompiledShader*)start;
  633. }
  634. JitCompiler::JitCompiler() {
  635. AllocCodeSpace(1024 * 1024 * 4);
  636. }
  637. void JitCompiler::Clear() {
  638. ClearCodeSpace();
  639. }
  640. } // namespace Shader
  641. } // namespace Pica