shader_jit_x64.cpp 28 KB

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