shader_jit_x64.cpp 30 KB

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