shader_jit_x64.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. #include "video_core/pica_state.h"
  11. namespace Pica {
  12. namespace Shader {
  13. using namespace Gen;
  14. typedef void (JitCompiler::*JitFunction)(Instruction instr);
  15. const JitFunction instr_table[64] = {
  16. &JitCompiler::Compile_ADD, // add
  17. &JitCompiler::Compile_DP3, // dp3
  18. &JitCompiler::Compile_DP4, // dp4
  19. &JitCompiler::Compile_DPH, // dph
  20. nullptr, // unknown
  21. &JitCompiler::Compile_EX2, // ex2
  22. &JitCompiler::Compile_LG2, // lg2
  23. nullptr, // unknown
  24. &JitCompiler::Compile_MUL, // mul
  25. &JitCompiler::Compile_SGE, // sge
  26. &JitCompiler::Compile_SLT, // slt
  27. &JitCompiler::Compile_FLR, // flr
  28. &JitCompiler::Compile_MAX, // max
  29. &JitCompiler::Compile_MIN, // min
  30. &JitCompiler::Compile_RCP, // rcp
  31. &JitCompiler::Compile_RSQ, // rsq
  32. nullptr, // unknown
  33. nullptr, // unknown
  34. &JitCompiler::Compile_MOVA, // mova
  35. &JitCompiler::Compile_MOV, // mov
  36. nullptr, // unknown
  37. nullptr, // unknown
  38. nullptr, // unknown
  39. nullptr, // unknown
  40. &JitCompiler::Compile_DPH, // dphi
  41. nullptr, // unknown
  42. &JitCompiler::Compile_SGE, // sgei
  43. &JitCompiler::Compile_SLT, // slti
  44. nullptr, // unknown
  45. nullptr, // unknown
  46. nullptr, // unknown
  47. nullptr, // unknown
  48. nullptr, // unknown
  49. &JitCompiler::Compile_NOP, // nop
  50. &JitCompiler::Compile_END, // end
  51. nullptr, // break
  52. &JitCompiler::Compile_CALL, // call
  53. &JitCompiler::Compile_CALLC, // callc
  54. &JitCompiler::Compile_CALLU, // callu
  55. &JitCompiler::Compile_IF, // ifu
  56. &JitCompiler::Compile_IF, // ifc
  57. &JitCompiler::Compile_LOOP, // loop
  58. nullptr, // emit
  59. nullptr, // sete
  60. &JitCompiler::Compile_JMP, // jmpc
  61. &JitCompiler::Compile_JMP, // jmpu
  62. &JitCompiler::Compile_CMP, // cmp
  63. &JitCompiler::Compile_CMP, // cmp
  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, // madi
  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. &JitCompiler::Compile_MAD, // mad
  80. };
  81. // The following is used to alias some commonly used registers. Generally, RAX-RDX and XMM0-XMM3 can
  82. // be used as scratch registers within a compiler function. The other registers have designated
  83. // purposes, as documented below:
  84. /// Pointer to the uniform memory
  85. static const X64Reg UNIFORMS = R9;
  86. /// The two 32-bit VS address offset registers set by the MOVA instruction
  87. static const X64Reg ADDROFFS_REG_0 = R10;
  88. static const X64Reg ADDROFFS_REG_1 = R11;
  89. /// VS loop count register
  90. static const X64Reg LOOPCOUNT_REG = R12;
  91. /// Current VS loop iteration number (we could probably use LOOPCOUNT_REG, but this quicker)
  92. static const X64Reg LOOPCOUNT = RSI;
  93. /// Number to increment LOOPCOUNT_REG by on each loop iteration
  94. static const X64Reg LOOPINC = RDI;
  95. /// Result of the previous CMP instruction for the X-component comparison
  96. static const X64Reg COND0 = R13;
  97. /// Result of the previous CMP instruction for the Y-component comparison
  98. static const X64Reg COND1 = R14;
  99. /// Pointer to the UnitState instance for the current VS unit
  100. static const X64Reg REGISTERS = R15;
  101. /// SIMD scratch register
  102. static const X64Reg SCRATCH = XMM0;
  103. /// Loaded with the first swizzled source register, otherwise can be used as a scratch register
  104. static const X64Reg SRC1 = XMM1;
  105. /// Loaded with the second swizzled source register, otherwise can be used as a scratch register
  106. static const X64Reg SRC2 = XMM2;
  107. /// Loaded with the third swizzled source register, otherwise can be used as a scratch register
  108. static const X64Reg SRC3 = XMM3;
  109. /// Additional scratch register
  110. static const X64Reg SCRATCH2 = XMM4;
  111. /// Constant vector of [1.0f, 1.0f, 1.0f, 1.0f], used to efficiently set a vector to one
  112. static const X64Reg ONE = XMM14;
  113. /// Constant vector of [-0.f, -0.f, -0.f, -0.f], used to efficiently negate a vector with XOR
  114. static const X64Reg NEGBIT = XMM15;
  115. // State registers that must not be modified by external functions calls
  116. // Scratch registers, e.g., SRC1 and SCRATCH, have to be saved on the side if needed
  117. static const BitSet32 persistent_regs = {
  118. UNIFORMS, REGISTERS, // Pointers to register blocks
  119. ADDROFFS_REG_0, ADDROFFS_REG_1, LOOPCOUNT_REG, COND0, COND1, // Cached registers
  120. ONE+16, NEGBIT+16, // Constants
  121. };
  122. /// Raw constant for the source register selector that indicates no swizzling is performed
  123. static const u8 NO_SRC_REG_SWIZZLE = 0x1b;
  124. /// Raw constant for the destination register enable mask that indicates all components are enabled
  125. static const u8 NO_DEST_REG_MASK = 0xf;
  126. /**
  127. * Get the vertex shader instruction for a given offset in the current shader program
  128. * @param offset Offset in the current shader program of the instruction
  129. * @return Instruction at the specified offset
  130. */
  131. static Instruction GetVertexShaderInstruction(size_t offset) {
  132. return { g_state.vs.program_code[offset] };
  133. }
  134. /**
  135. * Loads and swizzles a source register into the specified XMM register.
  136. * @param instr VS instruction, used for determining how to load the source register
  137. * @param src_num Number indicating which source register to load (1 = src1, 2 = src2, 3 = src3)
  138. * @param src_reg SourceRegister object corresponding to the source register to load
  139. * @param dest Destination XMM register to store the loaded, swizzled source register
  140. */
  141. void JitCompiler::Compile_SwizzleSrc(Instruction instr, unsigned src_num, SourceRegister src_reg, X64Reg dest) {
  142. X64Reg src_ptr;
  143. size_t src_offset;
  144. if (src_reg.GetRegisterType() == RegisterType::FloatUniform) {
  145. src_ptr = UNIFORMS;
  146. src_offset = src_reg.GetIndex() * sizeof(float24) * 4;
  147. } else {
  148. src_ptr = REGISTERS;
  149. src_offset = UnitState<false>::InputOffset(src_reg);
  150. }
  151. int src_offset_disp = (int)src_offset;
  152. ASSERT_MSG(src_offset == src_offset_disp, "Source register offset too large for int type");
  153. unsigned operand_desc_id;
  154. const bool is_inverted = (0 != (instr.opcode.Value().GetInfo().subtype & OpCode::Info::SrcInversed));
  155. unsigned address_register_index;
  156. unsigned offset_src;
  157. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MAD ||
  158. instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  159. operand_desc_id = instr.mad.operand_desc_id;
  160. offset_src = is_inverted ? 3 : 2;
  161. address_register_index = instr.mad.address_register_index;
  162. } else {
  163. operand_desc_id = instr.common.operand_desc_id;
  164. offset_src = is_inverted ? 2 : 1;
  165. address_register_index = instr.common.address_register_index;
  166. }
  167. if (src_num == offset_src && address_register_index != 0) {
  168. switch (address_register_index) {
  169. case 1: // address offset 1
  170. MOVAPS(dest, MComplex(src_ptr, ADDROFFS_REG_0, SCALE_1, src_offset_disp));
  171. break;
  172. case 2: // address offset 2
  173. MOVAPS(dest, MComplex(src_ptr, ADDROFFS_REG_1, SCALE_1, src_offset_disp));
  174. break;
  175. case 3: // address offset 3
  176. MOVAPS(dest, MComplex(src_ptr, LOOPCOUNT_REG, SCALE_1, src_offset_disp));
  177. break;
  178. default:
  179. UNREACHABLE();
  180. break;
  181. }
  182. } else {
  183. // Load the source
  184. MOVAPS(dest, MDisp(src_ptr, src_offset_disp));
  185. }
  186. SwizzlePattern swiz = { g_state.vs.swizzle_data[operand_desc_id] };
  187. // Generate instructions for source register swizzling as needed
  188. u8 sel = swiz.GetRawSelector(src_num);
  189. if (sel != NO_SRC_REG_SWIZZLE) {
  190. // Selector component order needs to be reversed for the SHUFPS instruction
  191. sel = ((sel & 0xc0) >> 6) | ((sel & 3) << 6) | ((sel & 0xc) << 2) | ((sel & 0x30) >> 2);
  192. // Shuffle inputs for swizzle
  193. SHUFPS(dest, R(dest), sel);
  194. }
  195. // If the source register should be negated, flip the negative bit using XOR
  196. const bool negate[] = { swiz.negate_src1, swiz.negate_src2, swiz.negate_src3 };
  197. if (negate[src_num - 1]) {
  198. XORPS(dest, R(NEGBIT));
  199. }
  200. }
  201. void JitCompiler::Compile_DestEnable(Instruction instr,X64Reg src) {
  202. DestRegister dest;
  203. unsigned operand_desc_id;
  204. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MAD ||
  205. instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  206. operand_desc_id = instr.mad.operand_desc_id;
  207. dest = instr.mad.dest.Value();
  208. } else {
  209. operand_desc_id = instr.common.operand_desc_id;
  210. dest = instr.common.dest.Value();
  211. }
  212. SwizzlePattern swiz = { g_state.vs.swizzle_data[operand_desc_id] };
  213. int dest_offset_disp = (int)UnitState<false>::OutputOffset(dest);
  214. ASSERT_MSG(dest_offset_disp == UnitState<false>::OutputOffset(dest), "Destinaton offset too large for int type");
  215. // If all components are enabled, write the result to the destination register
  216. if (swiz.dest_mask == NO_DEST_REG_MASK) {
  217. // Store dest back to memory
  218. MOVAPS(MDisp(REGISTERS, dest_offset_disp), src);
  219. } else {
  220. // Not all components are enabled, so mask the result when storing to the destination register...
  221. MOVAPS(SCRATCH, MDisp(REGISTERS, dest_offset_disp));
  222. if (Common::GetCPUCaps().sse4_1) {
  223. u8 mask = ((swiz.dest_mask & 1) << 3) | ((swiz.dest_mask & 8) >> 3) | ((swiz.dest_mask & 2) << 1) | ((swiz.dest_mask & 4) >> 1);
  224. BLENDPS(SCRATCH, R(src), mask);
  225. } else {
  226. MOVAPS(SCRATCH2, R(src));
  227. UNPCKHPS(SCRATCH2, R(SCRATCH)); // Unpack X/Y components of source and destination
  228. UNPCKLPS(SCRATCH, R(src)); // Unpack Z/W components of source and destination
  229. // Compute selector to selectively copy source components to destination for SHUFPS instruction
  230. u8 sel = ((swiz.DestComponentEnabled(0) ? 1 : 0) << 0) |
  231. ((swiz.DestComponentEnabled(1) ? 3 : 2) << 2) |
  232. ((swiz.DestComponentEnabled(2) ? 0 : 1) << 4) |
  233. ((swiz.DestComponentEnabled(3) ? 2 : 3) << 6);
  234. SHUFPS(SCRATCH, R(SCRATCH2), sel);
  235. }
  236. // Store dest back to memory
  237. MOVAPS(MDisp(REGISTERS, dest_offset_disp), SCRATCH);
  238. }
  239. }
  240. void JitCompiler::Compile_SanitizedMul(Gen::X64Reg src1, Gen::X64Reg src2, Gen::X64Reg scratch) {
  241. MOVAPS(scratch, R(src1));
  242. CMPPS(scratch, R(src2), CMP_ORD);
  243. MULPS(src1, R(src2));
  244. MOVAPS(src2, R(src1));
  245. CMPPS(src2, R(src2), CMP_UNORD);
  246. XORPS(scratch, R(src2));
  247. ANDPS(src1, R(scratch));
  248. }
  249. void JitCompiler::Compile_EvaluateCondition(Instruction instr) {
  250. // Note: NXOR is used below to check for equality
  251. switch (instr.flow_control.op) {
  252. case Instruction::FlowControlType::Or:
  253. MOV(32, R(RAX), R(COND0));
  254. MOV(32, R(RBX), R(COND1));
  255. XOR(32, R(RAX), Imm32(instr.flow_control.refx.Value() ^ 1));
  256. XOR(32, R(RBX), Imm32(instr.flow_control.refy.Value() ^ 1));
  257. OR(32, R(RAX), R(RBX));
  258. break;
  259. case Instruction::FlowControlType::And:
  260. MOV(32, R(RAX), R(COND0));
  261. MOV(32, R(RBX), R(COND1));
  262. XOR(32, R(RAX), Imm32(instr.flow_control.refx.Value() ^ 1));
  263. XOR(32, R(RBX), Imm32(instr.flow_control.refy.Value() ^ 1));
  264. AND(32, R(RAX), R(RBX));
  265. break;
  266. case Instruction::FlowControlType::JustX:
  267. MOV(32, R(RAX), R(COND0));
  268. XOR(32, R(RAX), Imm32(instr.flow_control.refx.Value() ^ 1));
  269. break;
  270. case Instruction::FlowControlType::JustY:
  271. MOV(32, R(RAX), R(COND1));
  272. XOR(32, R(RAX), Imm32(instr.flow_control.refy.Value() ^ 1));
  273. break;
  274. }
  275. }
  276. void JitCompiler::Compile_UniformCondition(Instruction instr) {
  277. int offset = offsetof(decltype(g_state.vs.uniforms), b) + (instr.flow_control.bool_uniform_id * sizeof(bool));
  278. CMP(sizeof(bool) * 8, MDisp(UNIFORMS, offset), Imm8(0));
  279. }
  280. BitSet32 JitCompiler::PersistentCallerSavedRegs() {
  281. return persistent_regs & ABI_ALL_CALLER_SAVED;
  282. }
  283. void JitCompiler::Compile_ADD(Instruction instr) {
  284. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  285. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  286. ADDPS(SRC1, R(SRC2));
  287. Compile_DestEnable(instr, SRC1);
  288. }
  289. void JitCompiler::Compile_DP3(Instruction instr) {
  290. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  291. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  292. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  293. MOVAPS(SRC2, R(SRC1));
  294. SHUFPS(SRC2, R(SRC2), _MM_SHUFFLE(1, 1, 1, 1));
  295. MOVAPS(SRC3, R(SRC1));
  296. SHUFPS(SRC3, R(SRC3), _MM_SHUFFLE(2, 2, 2, 2));
  297. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0));
  298. ADDPS(SRC1, R(SRC2));
  299. ADDPS(SRC1, R(SRC3));
  300. Compile_DestEnable(instr, SRC1);
  301. }
  302. void JitCompiler::Compile_DP4(Instruction instr) {
  303. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  304. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  305. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  306. MOVAPS(SRC2, R(SRC1));
  307. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(2, 3, 0, 1)); // XYZW -> ZWXY
  308. ADDPS(SRC1, R(SRC2));
  309. MOVAPS(SRC2, R(SRC1));
  310. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 1, 2, 3)); // XYZW -> WZYX
  311. ADDPS(SRC1, R(SRC2));
  312. Compile_DestEnable(instr, SRC1);
  313. }
  314. void JitCompiler::Compile_DPH(Instruction instr) {
  315. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::DPHI) {
  316. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  317. Compile_SwizzleSrc(instr, 2, instr.common.src2i, SRC2);
  318. } else {
  319. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  320. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  321. }
  322. if (Common::GetCPUCaps().sse4_1) {
  323. // Set 4th component to 1.0
  324. BLENDPS(SRC1, R(ONE), 0x8); // 0b1000
  325. } else {
  326. // Set 4th component to 1.0
  327. MOVAPS(SCRATCH, R(SRC1));
  328. UNPCKHPS(SCRATCH, R(ONE)); // XYZW, 1111 -> Z1__
  329. UNPCKLPD(SRC1, R(SCRATCH)); // XYZW, Z1__ -> XYZ1
  330. }
  331. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  332. MOVAPS(SRC2, R(SRC1));
  333. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(2, 3, 0, 1)); // XYZW -> ZWXY
  334. ADDPS(SRC1, R(SRC2));
  335. MOVAPS(SRC2, R(SRC1));
  336. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 1, 2, 3)); // XYZW -> WZYX
  337. ADDPS(SRC1, R(SRC2));
  338. Compile_DestEnable(instr, SRC1);
  339. }
  340. void JitCompiler::Compile_EX2(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*>(exp2f));
  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_LG2(Instruction instr) {
  351. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  352. MOVSS(XMM0, R(SRC1));
  353. ABI_PushRegistersAndAdjustStack(PersistentCallerSavedRegs(), 0);
  354. ABI_CallFunction(reinterpret_cast<const void*>(log2f));
  355. ABI_PopRegistersAndAdjustStack(PersistentCallerSavedRegs(), 0);
  356. SHUFPS(XMM0, R(XMM0), _MM_SHUFFLE(0, 0, 0, 0));
  357. MOVAPS(SRC1, R(XMM0));
  358. Compile_DestEnable(instr, SRC1);
  359. }
  360. void JitCompiler::Compile_MUL(Instruction instr) {
  361. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  362. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  363. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  364. Compile_DestEnable(instr, SRC1);
  365. }
  366. void JitCompiler::Compile_SGE(Instruction instr) {
  367. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::SGEI) {
  368. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  369. Compile_SwizzleSrc(instr, 2, instr.common.src2i, SRC2);
  370. } else {
  371. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  372. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  373. }
  374. CMPPS(SRC2, R(SRC1), CMP_LE);
  375. ANDPS(SRC2, R(ONE));
  376. Compile_DestEnable(instr, SRC2);
  377. }
  378. void JitCompiler::Compile_SLT(Instruction instr) {
  379. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::SLTI) {
  380. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  381. Compile_SwizzleSrc(instr, 2, instr.common.src2i, SRC2);
  382. } else {
  383. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  384. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  385. }
  386. CMPPS(SRC1, R(SRC2), CMP_LT);
  387. ANDPS(SRC1, R(ONE));
  388. Compile_DestEnable(instr, SRC1);
  389. }
  390. void JitCompiler::Compile_FLR(Instruction instr) {
  391. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  392. if (Common::GetCPUCaps().sse4_1) {
  393. ROUNDFLOORPS(SRC1, R(SRC1));
  394. } else {
  395. CVTPS2DQ(SRC1, R(SRC1));
  396. CVTDQ2PS(SRC1, R(SRC1));
  397. }
  398. Compile_DestEnable(instr, SRC1);
  399. }
  400. void JitCompiler::Compile_MAX(Instruction instr) {
  401. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  402. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  403. // SSE semantics match PICA200 ones: In case of NaN, SRC2 is returned.
  404. MAXPS(SRC1, R(SRC2));
  405. Compile_DestEnable(instr, SRC1);
  406. }
  407. void JitCompiler::Compile_MIN(Instruction instr) {
  408. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  409. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  410. // SSE semantics match PICA200 ones: In case of NaN, SRC2 is returned.
  411. MINPS(SRC1, R(SRC2));
  412. Compile_DestEnable(instr, SRC1);
  413. }
  414. void JitCompiler::Compile_MOVA(Instruction instr) {
  415. SwizzlePattern swiz = { g_state.vs.swizzle_data[instr.common.operand_desc_id] };
  416. if (!swiz.DestComponentEnabled(0) && !swiz.DestComponentEnabled(1)) {
  417. return; // NoOp
  418. }
  419. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  420. // Convert floats to integers using truncation (only care about X and Y components)
  421. CVTTPS2DQ(SRC1, R(SRC1));
  422. // Get result
  423. MOVQ_xmm(R(RAX), SRC1);
  424. // Handle destination enable
  425. if (swiz.DestComponentEnabled(0) && swiz.DestComponentEnabled(1)) {
  426. // Move and sign-extend low 32 bits
  427. MOVSX(64, 32, ADDROFFS_REG_0, R(RAX));
  428. // Move and sign-extend high 32 bits
  429. SHR(64, R(RAX), Imm8(32));
  430. MOVSX(64, 32, ADDROFFS_REG_1, R(RAX));
  431. // Multiply by 16 to be used as an offset later
  432. SHL(64, R(ADDROFFS_REG_0), Imm8(4));
  433. SHL(64, R(ADDROFFS_REG_1), Imm8(4));
  434. } else {
  435. if (swiz.DestComponentEnabled(0)) {
  436. // Move and sign-extend low 32 bits
  437. MOVSX(64, 32, ADDROFFS_REG_0, R(RAX));
  438. // Multiply by 16 to be used as an offset later
  439. SHL(64, R(ADDROFFS_REG_0), Imm8(4));
  440. } else if (swiz.DestComponentEnabled(1)) {
  441. // Move and sign-extend high 32 bits
  442. SHR(64, R(RAX), Imm8(32));
  443. MOVSX(64, 32, ADDROFFS_REG_1, R(RAX));
  444. // Multiply by 16 to be used as an offset later
  445. SHL(64, R(ADDROFFS_REG_1), Imm8(4));
  446. }
  447. }
  448. }
  449. void JitCompiler::Compile_MOV(Instruction instr) {
  450. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  451. Compile_DestEnable(instr, SRC1);
  452. }
  453. void JitCompiler::Compile_RCP(Instruction instr) {
  454. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  455. // TODO(bunnei): RCPSS is a pretty rough approximation, this might cause problems if Pica
  456. // performs this operation more accurately. This should be checked on hardware.
  457. RCPSS(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_RSQ(Instruction instr) {
  462. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  463. // TODO(bunnei): RSQRTSS is a pretty rough approximation, this might cause problems if Pica
  464. // performs this operation more accurately. This should be checked on hardware.
  465. RSQRTSS(SRC1, R(SRC1));
  466. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0)); // XYWZ -> XXXX
  467. Compile_DestEnable(instr, SRC1);
  468. }
  469. void JitCompiler::Compile_NOP(Instruction instr) {
  470. }
  471. void JitCompiler::Compile_END(Instruction instr) {
  472. ABI_PopRegistersAndAdjustStack(ABI_ALL_CALLEE_SAVED, 8);
  473. RET();
  474. }
  475. void JitCompiler::Compile_CALL(Instruction instr) {
  476. // Need to advance the return address past the proceeding instructions, this is the number of bytes to skip
  477. constexpr unsigned SKIP = 21;
  478. const uintptr_t start = reinterpret_cast<uintptr_t>(GetCodePtr());
  479. // Push return address - not using CALL because we also want to push the offset of the return before jumping
  480. MOV(64, R(RAX), ImmPtr(GetCodePtr() + SKIP));
  481. PUSH(RAX);
  482. // Push offset of the return
  483. PUSH(32, Imm32(instr.flow_control.dest_offset + instr.flow_control.num_instructions));
  484. // Jump
  485. FixupBranch b = J(true);
  486. fixup_branches.push_back({ b, instr.flow_control.dest_offset });
  487. // Make sure that if the above code changes, SKIP gets updated
  488. ASSERT(reinterpret_cast<ptrdiff_t>(GetCodePtr()) - start == SKIP);
  489. }
  490. void JitCompiler::Compile_CALLC(Instruction instr) {
  491. Compile_EvaluateCondition(instr);
  492. FixupBranch b = J_CC(CC_Z, true);
  493. Compile_CALL(instr);
  494. SetJumpTarget(b);
  495. }
  496. void JitCompiler::Compile_CALLU(Instruction instr) {
  497. Compile_UniformCondition(instr);
  498. FixupBranch b = J_CC(CC_Z, true);
  499. Compile_CALL(instr);
  500. SetJumpTarget(b);
  501. }
  502. void JitCompiler::Compile_CMP(Instruction instr) {
  503. using Op = Instruction::Common::CompareOpType::Op;
  504. Op op_x = instr.common.compare_op.x;
  505. Op op_y = instr.common.compare_op.y;
  506. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  507. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  508. // SSE doesn't have greater-than (GT) or greater-equal (GE) comparison operators. You need to
  509. // emulate them by swapping the lhs and rhs and using LT and LE. NLT and NLE can't be used here
  510. // because they don't match when used with NaNs.
  511. static const u8 cmp[] = { CMP_EQ, CMP_NEQ, CMP_LT, CMP_LE, CMP_LT, CMP_LE };
  512. bool invert_op_x = (op_x == Op::GreaterThan || op_x == Op::GreaterEqual);
  513. Gen::X64Reg lhs_x = invert_op_x ? SRC2 : SRC1;
  514. Gen::X64Reg rhs_x = invert_op_x ? SRC1 : SRC2;
  515. if (op_x == op_y) {
  516. // Compare X-component and Y-component together
  517. CMPPS(lhs_x, R(rhs_x), cmp[op_x]);
  518. MOVQ_xmm(R(COND0), lhs_x);
  519. MOV(64, R(COND1), R(COND0));
  520. } else {
  521. bool invert_op_y = (op_y == Op::GreaterThan || op_y == Op::GreaterEqual);
  522. Gen::X64Reg lhs_y = invert_op_y ? SRC2 : SRC1;
  523. Gen::X64Reg rhs_y = invert_op_y ? SRC1 : SRC2;
  524. // Compare X-component
  525. MOVAPS(SCRATCH, R(lhs_x));
  526. CMPSS(SCRATCH, R(rhs_x), cmp[op_x]);
  527. // Compare Y-component
  528. CMPPS(lhs_y, R(rhs_y), cmp[op_y]);
  529. MOVQ_xmm(R(COND0), SCRATCH);
  530. MOVQ_xmm(R(COND1), lhs_y);
  531. }
  532. SHR(32, R(COND0), Imm8(31));
  533. SHR(64, R(COND1), Imm8(63));
  534. }
  535. void JitCompiler::Compile_MAD(Instruction instr) {
  536. Compile_SwizzleSrc(instr, 1, instr.mad.src1, SRC1);
  537. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  538. Compile_SwizzleSrc(instr, 2, instr.mad.src2i, SRC2);
  539. Compile_SwizzleSrc(instr, 3, instr.mad.src3i, SRC3);
  540. } else {
  541. Compile_SwizzleSrc(instr, 2, instr.mad.src2, SRC2);
  542. Compile_SwizzleSrc(instr, 3, instr.mad.src3, SRC3);
  543. }
  544. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  545. ADDPS(SRC1, R(SRC3));
  546. Compile_DestEnable(instr, SRC1);
  547. }
  548. void JitCompiler::Compile_IF(Instruction instr) {
  549. ASSERT_MSG(instr.flow_control.dest_offset > last_program_counter, "Backwards if-statements (%d -> %d) not supported",
  550. last_program_counter, instr.flow_control.dest_offset.Value());
  551. // Evaluate the "IF" condition
  552. if (instr.opcode.Value() == OpCode::Id::IFU) {
  553. Compile_UniformCondition(instr);
  554. } else if (instr.opcode.Value() == OpCode::Id::IFC) {
  555. Compile_EvaluateCondition(instr);
  556. }
  557. FixupBranch b = J_CC(CC_Z, true);
  558. // Compile the code that corresponds to the condition evaluating as true
  559. Compile_Block(instr.flow_control.dest_offset);
  560. // If there isn't an "ELSE" condition, we are done here
  561. if (instr.flow_control.num_instructions == 0) {
  562. SetJumpTarget(b);
  563. return;
  564. }
  565. FixupBranch b2 = J(true);
  566. SetJumpTarget(b);
  567. // This code corresponds to the "ELSE" condition
  568. // Comple the code that corresponds to the condition evaluating as false
  569. Compile_Block(instr.flow_control.dest_offset + instr.flow_control.num_instructions);
  570. SetJumpTarget(b2);
  571. }
  572. void JitCompiler::Compile_LOOP(Instruction instr) {
  573. ASSERT_MSG(instr.flow_control.dest_offset > last_program_counter, "Backwards loops (%d -> %d) not supported",
  574. last_program_counter, instr.flow_control.dest_offset.Value());
  575. ASSERT_MSG(!looping, "Nested loops not supported");
  576. looping = true;
  577. int offset = offsetof(decltype(g_state.vs.uniforms), i) + (instr.flow_control.int_uniform_id * sizeof(Math::Vec4<u8>));
  578. MOV(32, R(LOOPCOUNT), MDisp(UNIFORMS, offset));
  579. MOV(32, R(LOOPCOUNT_REG), R(LOOPCOUNT));
  580. SHR(32, R(LOOPCOUNT_REG), Imm8(8));
  581. AND(32, R(LOOPCOUNT_REG), Imm32(0xff)); // Y-component is the start
  582. MOV(32, R(LOOPINC), R(LOOPCOUNT));
  583. SHR(32, R(LOOPINC), Imm8(16));
  584. MOVZX(32, 8, LOOPINC, R(LOOPINC)); // Z-component is the incrementer
  585. MOVZX(32, 8, LOOPCOUNT, R(LOOPCOUNT)); // X-component is iteration count
  586. ADD(32, R(LOOPCOUNT), Imm8(1)); // Iteration count is X-component + 1
  587. auto loop_start = GetCodePtr();
  588. Compile_Block(instr.flow_control.dest_offset + 1);
  589. ADD(32, R(LOOPCOUNT_REG), R(LOOPINC)); // Increment LOOPCOUNT_REG by Z-component
  590. SUB(32, R(LOOPCOUNT), Imm8(1)); // Increment loop count by 1
  591. J_CC(CC_NZ, loop_start); // Loop if not equal
  592. looping = false;
  593. }
  594. void JitCompiler::Compile_JMP(Instruction instr) {
  595. if (instr.opcode.Value() == OpCode::Id::JMPC)
  596. Compile_EvaluateCondition(instr);
  597. else if (instr.opcode.Value() == OpCode::Id::JMPU)
  598. Compile_UniformCondition(instr);
  599. else
  600. UNREACHABLE();
  601. bool inverted_condition = (instr.opcode.Value() == OpCode::Id::JMPU) &&
  602. (instr.flow_control.num_instructions & 1);
  603. FixupBranch b = J_CC(inverted_condition ? CC_Z : CC_NZ, true);
  604. fixup_branches.push_back({ b, instr.flow_control.dest_offset });
  605. }
  606. void JitCompiler::Compile_Block(unsigned end) {
  607. while (program_counter < end) {
  608. Compile_NextInstr();
  609. }
  610. }
  611. void JitCompiler::Compile_Return() {
  612. // Peek return offset on the stack and check if we're at that offset
  613. MOV(64, R(RAX), MDisp(RSP, 0));
  614. CMP(32, R(RAX), Imm32(program_counter));
  615. // If so, jump back to before CALL
  616. FixupBranch b = J_CC(CC_NZ, true);
  617. ADD(64, R(RSP), Imm32(8)); // Ignore return offset that's on the stack
  618. POP(RAX); // Pop off return address
  619. JMPptr(R(RAX));
  620. SetJumpTarget(b);
  621. }
  622. void JitCompiler::Compile_NextInstr() {
  623. last_program_counter = program_counter;
  624. auto search = return_offsets.find(program_counter);
  625. if (search != return_offsets.end()) {
  626. Compile_Return();
  627. }
  628. ASSERT_MSG(code_ptr[program_counter] == nullptr, "Tried to compile already compiled shader location!");
  629. code_ptr[program_counter] = GetCodePtr();
  630. Instruction instr = GetVertexShaderInstruction(program_counter++);
  631. OpCode::Id opcode = instr.opcode.Value();
  632. auto instr_func = instr_table[static_cast<unsigned>(opcode)];
  633. if (instr_func) {
  634. // JIT the instruction!
  635. ((*this).*instr_func)(instr);
  636. } else {
  637. // Unhandled instruction
  638. LOG_CRITICAL(HW_GPU, "Unhandled instruction: 0x%02x (0x%08x)",
  639. instr.opcode.Value().EffectiveOpCode(), instr.hex);
  640. }
  641. }
  642. void JitCompiler::FindReturnOffsets() {
  643. return_offsets.clear();
  644. for (size_t offset = 0; offset < g_state.vs.program_code.size(); ++offset) {
  645. Instruction instr = GetVertexShaderInstruction(offset);
  646. switch (instr.opcode.Value()) {
  647. case OpCode::Id::CALL:
  648. case OpCode::Id::CALLC:
  649. case OpCode::Id::CALLU:
  650. return_offsets.insert(instr.flow_control.dest_offset + instr.flow_control.num_instructions);
  651. break;
  652. }
  653. }
  654. }
  655. void JitCompiler::Compile() {
  656. program = (CompiledShader*)GetCodePtr();
  657. // The stack pointer is 8 modulo 16 at the entry of a procedure
  658. ABI_PushRegistersAndAdjustStack(ABI_ALL_CALLEE_SAVED, 8);
  659. MOV(PTRBITS, R(REGISTERS), R(ABI_PARAM1));
  660. MOV(PTRBITS, R(UNIFORMS), ImmPtr(&g_state.vs.uniforms));
  661. // Zero address/loop registers
  662. XOR(64, R(ADDROFFS_REG_0), R(ADDROFFS_REG_0));
  663. XOR(64, R(ADDROFFS_REG_1), R(ADDROFFS_REG_1));
  664. XOR(64, R(LOOPCOUNT_REG), R(LOOPCOUNT_REG));
  665. // Used to set a register to one
  666. static const __m128 one = { 1.f, 1.f, 1.f, 1.f };
  667. MOV(PTRBITS, R(RAX), ImmPtr(&one));
  668. MOVAPS(ONE, MatR(RAX));
  669. // Used to negate registers
  670. static const __m128 neg = { -0.f, -0.f, -0.f, -0.f };
  671. MOV(PTRBITS, R(RAX), ImmPtr(&neg));
  672. MOVAPS(NEGBIT, MatR(RAX));
  673. // Find all `CALL` instructions and identify return locations
  674. FindReturnOffsets();
  675. // Reset flow control state
  676. last_program_counter = 0;
  677. program_counter = 0;
  678. looping = false;
  679. code_ptr.fill(nullptr);
  680. fixup_branches.clear();
  681. // Jump to start of the shader program
  682. if (g_state.regs.vs.main_offset != 0) {
  683. fixup_branches.push_back({ J(true), g_state.regs.vs.main_offset });
  684. }
  685. // Compile entire program
  686. Compile_Block(static_cast<unsigned>(g_state.vs.program_code.size()));
  687. // Set the target for any incomplete branches now that the entire shader program has been emitted
  688. for (const auto& branch : fixup_branches) {
  689. SetJumpTarget(branch.first, code_ptr[branch.second]);
  690. }
  691. uintptr_t size = reinterpret_cast<uintptr_t>(GetCodePtr()) - reinterpret_cast<uintptr_t>(program);
  692. ASSERT_MSG(size <= MAX_SHADER_SIZE, "Compiled a shader that exceeds the allocated size!");
  693. LOG_DEBUG(HW_GPU, "Compiled shader size=%d", size);
  694. }
  695. JitCompiler::JitCompiler() {
  696. AllocCodeSpace(MAX_SHADER_SIZE);
  697. }
  698. } // namespace Shader
  699. } // namespace Pica