shader_jit_x64.cpp 30 KB

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