shader_jit_x64.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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 <nihstro/shader_bytecode.h>
  8. #include <xmmintrin.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,
  158. X64Reg dest) {
  159. X64Reg src_ptr;
  160. size_t src_offset;
  161. if (src_reg.GetRegisterType() == RegisterType::FloatUniform) {
  162. src_ptr = SETUP;
  163. src_offset = ShaderSetup::UniformOffset(RegisterType::FloatUniform, src_reg.GetIndex());
  164. } else {
  165. src_ptr = STATE;
  166. src_offset = UnitState<false>::InputOffset(src_reg);
  167. }
  168. int src_offset_disp = (int)src_offset;
  169. ASSERT_MSG(src_offset == src_offset_disp, "Source register offset too large for int type");
  170. unsigned operand_desc_id;
  171. const bool is_inverted =
  172. (0 != (instr.opcode.Value().GetInfo().subtype & OpCode::Info::SrcInversed));
  173. unsigned address_register_index;
  174. unsigned offset_src;
  175. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MAD ||
  176. instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  177. operand_desc_id = instr.mad.operand_desc_id;
  178. offset_src = is_inverted ? 3 : 2;
  179. address_register_index = instr.mad.address_register_index;
  180. } else {
  181. operand_desc_id = instr.common.operand_desc_id;
  182. offset_src = is_inverted ? 2 : 1;
  183. address_register_index = instr.common.address_register_index;
  184. }
  185. if (src_num == offset_src && address_register_index != 0) {
  186. switch (address_register_index) {
  187. case 1: // address offset 1
  188. MOVAPS(dest, MComplex(src_ptr, ADDROFFS_REG_0, SCALE_1, src_offset_disp));
  189. break;
  190. case 2: // address offset 2
  191. MOVAPS(dest, MComplex(src_ptr, ADDROFFS_REG_1, SCALE_1, src_offset_disp));
  192. break;
  193. case 3: // address offset 3
  194. MOVAPS(dest, MComplex(src_ptr, LOOPCOUNT_REG, SCALE_1, src_offset_disp));
  195. break;
  196. default:
  197. UNREACHABLE();
  198. break;
  199. }
  200. } else {
  201. // Load the source
  202. MOVAPS(dest, MDisp(src_ptr, src_offset_disp));
  203. }
  204. SwizzlePattern swiz = {g_state.vs.swizzle_data[operand_desc_id]};
  205. // Generate instructions for source register swizzling as needed
  206. u8 sel = swiz.GetRawSelector(src_num);
  207. if (sel != NO_SRC_REG_SWIZZLE) {
  208. // Selector component order needs to be reversed for the SHUFPS instruction
  209. sel = ((sel & 0xc0) >> 6) | ((sel & 3) << 6) | ((sel & 0xc) << 2) | ((sel & 0x30) >> 2);
  210. // Shuffle inputs for swizzle
  211. SHUFPS(dest, R(dest), sel);
  212. }
  213. // If the source register should be negated, flip the negative bit using XOR
  214. const bool negate[] = {swiz.negate_src1, swiz.negate_src2, swiz.negate_src3};
  215. if (negate[src_num - 1]) {
  216. XORPS(dest, R(NEGBIT));
  217. }
  218. }
  219. void JitShader::Compile_DestEnable(Instruction instr, X64Reg src) {
  220. DestRegister dest;
  221. unsigned operand_desc_id;
  222. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MAD ||
  223. instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  224. operand_desc_id = instr.mad.operand_desc_id;
  225. dest = instr.mad.dest.Value();
  226. } else {
  227. operand_desc_id = instr.common.operand_desc_id;
  228. dest = instr.common.dest.Value();
  229. }
  230. SwizzlePattern swiz = {g_state.vs.swizzle_data[operand_desc_id]};
  231. int dest_offset_disp = (int)UnitState<false>::OutputOffset(dest);
  232. ASSERT_MSG(dest_offset_disp == UnitState<false>::OutputOffset(dest),
  233. "Destinaton offset too large for int type");
  234. // If all components are enabled, write the result to the destination register
  235. if (swiz.dest_mask == NO_DEST_REG_MASK) {
  236. // Store dest back to memory
  237. MOVAPS(MDisp(STATE, dest_offset_disp), src);
  238. } else {
  239. // Not all components are enabled, so mask the result when storing to the destination
  240. // register...
  241. MOVAPS(SCRATCH, MDisp(STATE, dest_offset_disp));
  242. if (Common::GetCPUCaps().sse4_1) {
  243. u8 mask = ((swiz.dest_mask & 1) << 3) | ((swiz.dest_mask & 8) >> 3) |
  244. ((swiz.dest_mask & 2) << 1) | ((swiz.dest_mask & 4) >> 1);
  245. BLENDPS(SCRATCH, R(src), mask);
  246. } else {
  247. MOVAPS(SCRATCH2, R(src));
  248. UNPCKHPS(SCRATCH2, R(SCRATCH)); // Unpack X/Y components of source and destination
  249. UNPCKLPS(SCRATCH, R(src)); // Unpack Z/W components of source and destination
  250. // Compute selector to selectively copy source components to destination for SHUFPS
  251. // instruction
  252. u8 sel = ((swiz.DestComponentEnabled(0) ? 1 : 0) << 0) |
  253. ((swiz.DestComponentEnabled(1) ? 3 : 2) << 2) |
  254. ((swiz.DestComponentEnabled(2) ? 0 : 1) << 4) |
  255. ((swiz.DestComponentEnabled(3) ? 2 : 3) << 6);
  256. SHUFPS(SCRATCH, R(SCRATCH2), sel);
  257. }
  258. // Store dest back to memory
  259. MOVAPS(MDisp(STATE, dest_offset_disp), SCRATCH);
  260. }
  261. }
  262. void JitShader::Compile_SanitizedMul(Gen::X64Reg src1, Gen::X64Reg src2, Gen::X64Reg scratch) {
  263. MOVAPS(scratch, R(src1));
  264. CMPPS(scratch, R(src2), CMP_ORD);
  265. MULPS(src1, R(src2));
  266. MOVAPS(src2, R(src1));
  267. CMPPS(src2, R(src2), CMP_UNORD);
  268. XORPS(scratch, R(src2));
  269. ANDPS(src1, R(scratch));
  270. }
  271. void JitShader::Compile_EvaluateCondition(Instruction instr) {
  272. // Note: NXOR is used below to check for equality
  273. switch (instr.flow_control.op) {
  274. case Instruction::FlowControlType::Or:
  275. MOV(32, R(RAX), R(COND0));
  276. MOV(32, R(RBX), R(COND1));
  277. XOR(32, R(RAX), Imm32(instr.flow_control.refx.Value() ^ 1));
  278. XOR(32, R(RBX), Imm32(instr.flow_control.refy.Value() ^ 1));
  279. OR(32, R(RAX), R(RBX));
  280. break;
  281. case Instruction::FlowControlType::And:
  282. MOV(32, R(RAX), R(COND0));
  283. MOV(32, R(RBX), R(COND1));
  284. XOR(32, R(RAX), Imm32(instr.flow_control.refx.Value() ^ 1));
  285. XOR(32, R(RBX), Imm32(instr.flow_control.refy.Value() ^ 1));
  286. AND(32, R(RAX), R(RBX));
  287. break;
  288. case Instruction::FlowControlType::JustX:
  289. MOV(32, R(RAX), R(COND0));
  290. XOR(32, R(RAX), Imm32(instr.flow_control.refx.Value() ^ 1));
  291. break;
  292. case Instruction::FlowControlType::JustY:
  293. MOV(32, R(RAX), R(COND1));
  294. XOR(32, R(RAX), Imm32(instr.flow_control.refy.Value() ^ 1));
  295. break;
  296. }
  297. }
  298. void JitShader::Compile_UniformCondition(Instruction instr) {
  299. int offset =
  300. ShaderSetup::UniformOffset(RegisterType::BoolUniform, instr.flow_control.bool_uniform_id);
  301. CMP(sizeof(bool) * 8, MDisp(SETUP, offset), Imm8(0));
  302. }
  303. BitSet32 JitShader::PersistentCallerSavedRegs() {
  304. return persistent_regs & ABI_ALL_CALLER_SAVED;
  305. }
  306. void JitShader::Compile_ADD(Instruction instr) {
  307. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  308. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  309. ADDPS(SRC1, R(SRC2));
  310. Compile_DestEnable(instr, SRC1);
  311. }
  312. void JitShader::Compile_DP3(Instruction instr) {
  313. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  314. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  315. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  316. MOVAPS(SRC2, R(SRC1));
  317. SHUFPS(SRC2, R(SRC2), _MM_SHUFFLE(1, 1, 1, 1));
  318. MOVAPS(SRC3, R(SRC1));
  319. SHUFPS(SRC3, R(SRC3), _MM_SHUFFLE(2, 2, 2, 2));
  320. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0));
  321. ADDPS(SRC1, R(SRC2));
  322. ADDPS(SRC1, R(SRC3));
  323. Compile_DestEnable(instr, SRC1);
  324. }
  325. void JitShader::Compile_DP4(Instruction instr) {
  326. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  327. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  328. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  329. MOVAPS(SRC2, R(SRC1));
  330. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(2, 3, 0, 1)); // XYZW -> ZWXY
  331. ADDPS(SRC1, R(SRC2));
  332. MOVAPS(SRC2, R(SRC1));
  333. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 1, 2, 3)); // XYZW -> WZYX
  334. ADDPS(SRC1, R(SRC2));
  335. Compile_DestEnable(instr, SRC1);
  336. }
  337. void JitShader::Compile_DPH(Instruction instr) {
  338. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::DPHI) {
  339. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  340. Compile_SwizzleSrc(instr, 2, instr.common.src2i, SRC2);
  341. } else {
  342. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  343. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  344. }
  345. if (Common::GetCPUCaps().sse4_1) {
  346. // Set 4th component to 1.0
  347. BLENDPS(SRC1, R(ONE), 0x8); // 0b1000
  348. } else {
  349. // Set 4th component to 1.0
  350. MOVAPS(SCRATCH, R(SRC1));
  351. UNPCKHPS(SCRATCH, R(ONE)); // XYZW, 1111 -> Z1__
  352. UNPCKLPD(SRC1, R(SCRATCH)); // XYZW, Z1__ -> XYZ1
  353. }
  354. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  355. MOVAPS(SRC2, R(SRC1));
  356. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(2, 3, 0, 1)); // XYZW -> ZWXY
  357. ADDPS(SRC1, R(SRC2));
  358. MOVAPS(SRC2, R(SRC1));
  359. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 1, 2, 3)); // XYZW -> WZYX
  360. ADDPS(SRC1, R(SRC2));
  361. Compile_DestEnable(instr, SRC1);
  362. }
  363. void JitShader::Compile_EX2(Instruction instr) {
  364. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  365. MOVSS(XMM0, R(SRC1));
  366. ABI_PushRegistersAndAdjustStack(PersistentCallerSavedRegs(), 0);
  367. ABI_CallFunction(reinterpret_cast<const void*>(exp2f));
  368. ABI_PopRegistersAndAdjustStack(PersistentCallerSavedRegs(), 0);
  369. SHUFPS(XMM0, R(XMM0), _MM_SHUFFLE(0, 0, 0, 0));
  370. MOVAPS(SRC1, R(XMM0));
  371. Compile_DestEnable(instr, SRC1);
  372. }
  373. void JitShader::Compile_LG2(Instruction instr) {
  374. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  375. MOVSS(XMM0, R(SRC1));
  376. ABI_PushRegistersAndAdjustStack(PersistentCallerSavedRegs(), 0);
  377. ABI_CallFunction(reinterpret_cast<const void*>(log2f));
  378. ABI_PopRegistersAndAdjustStack(PersistentCallerSavedRegs(), 0);
  379. SHUFPS(XMM0, R(XMM0), _MM_SHUFFLE(0, 0, 0, 0));
  380. MOVAPS(SRC1, R(XMM0));
  381. Compile_DestEnable(instr, SRC1);
  382. }
  383. void JitShader::Compile_MUL(Instruction instr) {
  384. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  385. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  386. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  387. Compile_DestEnable(instr, SRC1);
  388. }
  389. void JitShader::Compile_SGE(Instruction instr) {
  390. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::SGEI) {
  391. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  392. Compile_SwizzleSrc(instr, 2, instr.common.src2i, SRC2);
  393. } else {
  394. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  395. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  396. }
  397. CMPPS(SRC2, R(SRC1), CMP_LE);
  398. ANDPS(SRC2, R(ONE));
  399. Compile_DestEnable(instr, SRC2);
  400. }
  401. void JitShader::Compile_SLT(Instruction instr) {
  402. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::SLTI) {
  403. Compile_SwizzleSrc(instr, 1, instr.common.src1i, SRC1);
  404. Compile_SwizzleSrc(instr, 2, instr.common.src2i, SRC2);
  405. } else {
  406. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  407. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  408. }
  409. CMPPS(SRC1, R(SRC2), CMP_LT);
  410. ANDPS(SRC1, R(ONE));
  411. Compile_DestEnable(instr, SRC1);
  412. }
  413. void JitShader::Compile_FLR(Instruction instr) {
  414. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  415. if (Common::GetCPUCaps().sse4_1) {
  416. ROUNDFLOORPS(SRC1, R(SRC1));
  417. } else {
  418. CVTPS2DQ(SRC1, R(SRC1));
  419. CVTDQ2PS(SRC1, R(SRC1));
  420. }
  421. Compile_DestEnable(instr, SRC1);
  422. }
  423. void JitShader::Compile_MAX(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. MAXPS(SRC1, R(SRC2));
  428. Compile_DestEnable(instr, SRC1);
  429. }
  430. void JitShader::Compile_MIN(Instruction instr) {
  431. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  432. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  433. // SSE semantics match PICA200 ones: In case of NaN, SRC2 is returned.
  434. MINPS(SRC1, R(SRC2));
  435. Compile_DestEnable(instr, SRC1);
  436. }
  437. void JitShader::Compile_MOVA(Instruction instr) {
  438. SwizzlePattern swiz = {g_state.vs.swizzle_data[instr.common.operand_desc_id]};
  439. if (!swiz.DestComponentEnabled(0) && !swiz.DestComponentEnabled(1)) {
  440. return; // NoOp
  441. }
  442. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  443. // Convert floats to integers using truncation (only care about X and Y components)
  444. CVTTPS2DQ(SRC1, R(SRC1));
  445. // Get result
  446. MOVQ_xmm(R(RAX), SRC1);
  447. // Handle destination enable
  448. if (swiz.DestComponentEnabled(0) && swiz.DestComponentEnabled(1)) {
  449. // Move and sign-extend low 32 bits
  450. MOVSX(64, 32, ADDROFFS_REG_0, R(RAX));
  451. // Move and sign-extend high 32 bits
  452. SHR(64, R(RAX), Imm8(32));
  453. MOVSX(64, 32, ADDROFFS_REG_1, R(RAX));
  454. // Multiply by 16 to be used as an offset later
  455. SHL(64, R(ADDROFFS_REG_0), Imm8(4));
  456. SHL(64, R(ADDROFFS_REG_1), Imm8(4));
  457. } else {
  458. if (swiz.DestComponentEnabled(0)) {
  459. // Move and sign-extend low 32 bits
  460. MOVSX(64, 32, ADDROFFS_REG_0, R(RAX));
  461. // Multiply by 16 to be used as an offset later
  462. SHL(64, R(ADDROFFS_REG_0), Imm8(4));
  463. } else if (swiz.DestComponentEnabled(1)) {
  464. // Move and sign-extend high 32 bits
  465. SHR(64, R(RAX), Imm8(32));
  466. MOVSX(64, 32, ADDROFFS_REG_1, R(RAX));
  467. // Multiply by 16 to be used as an offset later
  468. SHL(64, R(ADDROFFS_REG_1), Imm8(4));
  469. }
  470. }
  471. }
  472. void JitShader::Compile_MOV(Instruction instr) {
  473. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  474. Compile_DestEnable(instr, SRC1);
  475. }
  476. void JitShader::Compile_RCP(Instruction instr) {
  477. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  478. // TODO(bunnei): RCPSS is a pretty rough approximation, this might cause problems if Pica
  479. // performs this operation more accurately. This should be checked on hardware.
  480. RCPSS(SRC1, R(SRC1));
  481. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0)); // XYWZ -> XXXX
  482. Compile_DestEnable(instr, SRC1);
  483. }
  484. void JitShader::Compile_RSQ(Instruction instr) {
  485. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  486. // TODO(bunnei): RSQRTSS is a pretty rough approximation, this might cause problems if Pica
  487. // performs this operation more accurately. This should be checked on hardware.
  488. RSQRTSS(SRC1, R(SRC1));
  489. SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0)); // XYWZ -> XXXX
  490. Compile_DestEnable(instr, SRC1);
  491. }
  492. void JitShader::Compile_NOP(Instruction instr) {}
  493. void JitShader::Compile_END(Instruction instr) {
  494. ABI_PopRegistersAndAdjustStack(ABI_ALL_CALLEE_SAVED, 8);
  495. RET();
  496. }
  497. void JitShader::Compile_CALL(Instruction instr) {
  498. // Push offset of the return
  499. PUSH(64, Imm32(instr.flow_control.dest_offset + instr.flow_control.num_instructions));
  500. // Call the subroutine
  501. FixupBranch b = CALL();
  502. fixup_branches.push_back({b, instr.flow_control.dest_offset});
  503. // Skip over the return offset that's on the stack
  504. ADD(64, R(RSP), Imm32(8));
  505. }
  506. void JitShader::Compile_CALLC(Instruction instr) {
  507. Compile_EvaluateCondition(instr);
  508. FixupBranch b = J_CC(CC_Z, true);
  509. Compile_CALL(instr);
  510. SetJumpTarget(b);
  511. }
  512. void JitShader::Compile_CALLU(Instruction instr) {
  513. Compile_UniformCondition(instr);
  514. FixupBranch b = J_CC(CC_Z, true);
  515. Compile_CALL(instr);
  516. SetJumpTarget(b);
  517. }
  518. void JitShader::Compile_CMP(Instruction instr) {
  519. using Op = Instruction::Common::CompareOpType::Op;
  520. Op op_x = instr.common.compare_op.x;
  521. Op op_y = instr.common.compare_op.y;
  522. Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
  523. Compile_SwizzleSrc(instr, 2, instr.common.src2, SRC2);
  524. // SSE doesn't have greater-than (GT) or greater-equal (GE) comparison operators. You need to
  525. // emulate them by swapping the lhs and rhs and using LT and LE. NLT and NLE can't be used here
  526. // because they don't match when used with NaNs.
  527. static const u8 cmp[] = {CMP_EQ, CMP_NEQ, CMP_LT, CMP_LE, CMP_LT, CMP_LE};
  528. bool invert_op_x = (op_x == Op::GreaterThan || op_x == Op::GreaterEqual);
  529. Gen::X64Reg lhs_x = invert_op_x ? SRC2 : SRC1;
  530. Gen::X64Reg rhs_x = invert_op_x ? SRC1 : SRC2;
  531. if (op_x == op_y) {
  532. // Compare X-component and Y-component together
  533. CMPPS(lhs_x, R(rhs_x), cmp[op_x]);
  534. MOVQ_xmm(R(COND0), lhs_x);
  535. MOV(64, R(COND1), R(COND0));
  536. } else {
  537. bool invert_op_y = (op_y == Op::GreaterThan || op_y == Op::GreaterEqual);
  538. Gen::X64Reg lhs_y = invert_op_y ? SRC2 : SRC1;
  539. Gen::X64Reg rhs_y = invert_op_y ? SRC1 : SRC2;
  540. // Compare X-component
  541. MOVAPS(SCRATCH, R(lhs_x));
  542. CMPSS(SCRATCH, R(rhs_x), cmp[op_x]);
  543. // Compare Y-component
  544. CMPPS(lhs_y, R(rhs_y), cmp[op_y]);
  545. MOVQ_xmm(R(COND0), SCRATCH);
  546. MOVQ_xmm(R(COND1), lhs_y);
  547. }
  548. SHR(32, R(COND0), Imm8(31));
  549. SHR(64, R(COND1), Imm8(63));
  550. }
  551. void JitShader::Compile_MAD(Instruction instr) {
  552. Compile_SwizzleSrc(instr, 1, instr.mad.src1, SRC1);
  553. if (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI) {
  554. Compile_SwizzleSrc(instr, 2, instr.mad.src2i, SRC2);
  555. Compile_SwizzleSrc(instr, 3, instr.mad.src3i, SRC3);
  556. } else {
  557. Compile_SwizzleSrc(instr, 2, instr.mad.src2, SRC2);
  558. Compile_SwizzleSrc(instr, 3, instr.mad.src3, SRC3);
  559. }
  560. Compile_SanitizedMul(SRC1, SRC2, SCRATCH);
  561. ADDPS(SRC1, R(SRC3));
  562. Compile_DestEnable(instr, SRC1);
  563. }
  564. void JitShader::Compile_IF(Instruction instr) {
  565. Compile_Assert(instr.flow_control.dest_offset >= program_counter,
  566. "Backwards if-statements not supported");
  567. // Evaluate the "IF" condition
  568. if (instr.opcode.Value() == OpCode::Id::IFU) {
  569. Compile_UniformCondition(instr);
  570. } else if (instr.opcode.Value() == OpCode::Id::IFC) {
  571. Compile_EvaluateCondition(instr);
  572. }
  573. FixupBranch b = J_CC(CC_Z, true);
  574. // Compile the code that corresponds to the condition evaluating as true
  575. Compile_Block(instr.flow_control.dest_offset);
  576. // If there isn't an "ELSE" condition, we are done here
  577. if (instr.flow_control.num_instructions == 0) {
  578. SetJumpTarget(b);
  579. return;
  580. }
  581. FixupBranch b2 = J(true);
  582. SetJumpTarget(b);
  583. // This code corresponds to the "ELSE" condition
  584. // Comple the code that corresponds to the condition evaluating as false
  585. Compile_Block(instr.flow_control.dest_offset + instr.flow_control.num_instructions);
  586. SetJumpTarget(b2);
  587. }
  588. void JitShader::Compile_LOOP(Instruction instr) {
  589. Compile_Assert(instr.flow_control.dest_offset >= program_counter,
  590. "Backwards loops not supported");
  591. Compile_Assert(!looping, "Nested loops not supported");
  592. looping = true;
  593. int offset =
  594. ShaderSetup::UniformOffset(RegisterType::IntUniform, instr.flow_control.int_uniform_id);
  595. MOV(32, R(LOOPCOUNT), MDisp(SETUP, offset));
  596. MOV(32, R(LOOPCOUNT_REG), R(LOOPCOUNT));
  597. SHR(32, R(LOOPCOUNT_REG), Imm8(8));
  598. AND(32, R(LOOPCOUNT_REG), Imm32(0xff)); // Y-component is the start
  599. MOV(32, R(LOOPINC), R(LOOPCOUNT));
  600. SHR(32, R(LOOPINC), Imm8(16));
  601. MOVZX(32, 8, LOOPINC, R(LOOPINC)); // Z-component is the incrementer
  602. MOVZX(32, 8, LOOPCOUNT, R(LOOPCOUNT)); // X-component is iteration count
  603. ADD(32, R(LOOPCOUNT), Imm8(1)); // Iteration count is X-component + 1
  604. auto loop_start = GetCodePtr();
  605. Compile_Block(instr.flow_control.dest_offset + 1);
  606. ADD(32, R(LOOPCOUNT_REG), R(LOOPINC)); // Increment LOOPCOUNT_REG by Z-component
  607. SUB(32, R(LOOPCOUNT), Imm8(1)); // Increment loop count by 1
  608. J_CC(CC_NZ, loop_start); // Loop if not equal
  609. looping = false;
  610. }
  611. void JitShader::Compile_JMP(Instruction instr) {
  612. if (instr.opcode.Value() == OpCode::Id::JMPC)
  613. Compile_EvaluateCondition(instr);
  614. else if (instr.opcode.Value() == OpCode::Id::JMPU)
  615. Compile_UniformCondition(instr);
  616. else
  617. UNREACHABLE();
  618. bool inverted_condition =
  619. (instr.opcode.Value() == OpCode::Id::JMPU) && (instr.flow_control.num_instructions & 1);
  620. FixupBranch b = J_CC(inverted_condition ? CC_Z : CC_NZ, true);
  621. fixup_branches.push_back({b, instr.flow_control.dest_offset});
  622. }
  623. void JitShader::Compile_Block(unsigned end) {
  624. while (program_counter < end) {
  625. Compile_NextInstr();
  626. }
  627. }
  628. void JitShader::Compile_Return() {
  629. // Peek return offset on the stack and check if we're at that offset
  630. MOV(64, R(RAX), MDisp(RSP, 8));
  631. CMP(32, R(RAX), Imm32(program_counter));
  632. // If so, jump back to before CALL
  633. FixupBranch b = J_CC(CC_NZ, true);
  634. RET();
  635. SetJumpTarget(b);
  636. }
  637. void JitShader::Compile_NextInstr() {
  638. if (std::binary_search(return_offsets.begin(), return_offsets.end(), program_counter)) {
  639. Compile_Return();
  640. }
  641. ASSERT_MSG(code_ptr[program_counter] == nullptr,
  642. "Tried to compile already compiled shader location!");
  643. code_ptr[program_counter] = GetCodePtr();
  644. Instruction instr = GetVertexShaderInstruction(program_counter++);
  645. OpCode::Id opcode = instr.opcode.Value();
  646. auto instr_func = instr_table[static_cast<unsigned>(opcode)];
  647. if (instr_func) {
  648. // JIT the instruction!
  649. ((*this).*instr_func)(instr);
  650. } else {
  651. // Unhandled instruction
  652. LOG_CRITICAL(HW_GPU, "Unhandled instruction: 0x%02x (0x%08x)",
  653. instr.opcode.Value().EffectiveOpCode(), instr.hex);
  654. }
  655. }
  656. void JitShader::FindReturnOffsets() {
  657. return_offsets.clear();
  658. for (size_t offset = 0; offset < g_state.vs.program_code.size(); ++offset) {
  659. Instruction instr = GetVertexShaderInstruction(offset);
  660. switch (instr.opcode.Value()) {
  661. case OpCode::Id::CALL:
  662. case OpCode::Id::CALLC:
  663. case OpCode::Id::CALLU:
  664. return_offsets.push_back(instr.flow_control.dest_offset +
  665. instr.flow_control.num_instructions);
  666. break;
  667. default:
  668. break;
  669. }
  670. }
  671. // Sort for efficient binary search later
  672. std::sort(return_offsets.begin(), return_offsets.end());
  673. }
  674. void JitShader::Compile() {
  675. // Reset flow control state
  676. program = (CompiledShader*)GetCodePtr();
  677. program_counter = 0;
  678. looping = false;
  679. code_ptr.fill(nullptr);
  680. fixup_branches.clear();
  681. // Find all `CALL` instructions and identify return locations
  682. FindReturnOffsets();
  683. // The stack pointer is 8 modulo 16 at the entry of a procedure
  684. ABI_PushRegistersAndAdjustStack(ABI_ALL_CALLEE_SAVED, 8);
  685. MOV(PTRBITS, R(SETUP), R(ABI_PARAM1));
  686. MOV(PTRBITS, R(STATE), R(ABI_PARAM2));
  687. // Zero address/loop registers
  688. XOR(64, R(ADDROFFS_REG_0), R(ADDROFFS_REG_0));
  689. XOR(64, R(ADDROFFS_REG_1), R(ADDROFFS_REG_1));
  690. XOR(64, R(LOOPCOUNT_REG), R(LOOPCOUNT_REG));
  691. // Used to set a register to one
  692. static const __m128 one = {1.f, 1.f, 1.f, 1.f};
  693. MOV(PTRBITS, R(RAX), ImmPtr(&one));
  694. MOVAPS(ONE, MatR(RAX));
  695. // Used to negate registers
  696. static const __m128 neg = {-0.f, -0.f, -0.f, -0.f};
  697. MOV(PTRBITS, R(RAX), ImmPtr(&neg));
  698. MOVAPS(NEGBIT, MatR(RAX));
  699. // Jump to start of the shader program
  700. JMPptr(R(ABI_PARAM3));
  701. // Compile entire program
  702. Compile_Block(static_cast<unsigned>(g_state.vs.program_code.size()));
  703. // Set the target for any incomplete branches now that the entire shader program has been
  704. // emitted
  705. for (const auto& branch : fixup_branches) {
  706. SetJumpTarget(branch.first, code_ptr[branch.second]);
  707. }
  708. // Free memory that's no longer needed
  709. return_offsets.clear();
  710. return_offsets.shrink_to_fit();
  711. fixup_branches.clear();
  712. fixup_branches.shrink_to_fit();
  713. uintptr_t size =
  714. reinterpret_cast<uintptr_t>(GetCodePtr()) - reinterpret_cast<uintptr_t>(program);
  715. ASSERT_MSG(size <= MAX_SHADER_SIZE, "Compiled a shader that exceeds the allocated size!");
  716. LOG_DEBUG(HW_GPU, "Compiled shader size=%lu", size);
  717. }
  718. JitShader::JitShader() {
  719. AllocCodeSpace(MAX_SHADER_SIZE);
  720. }
  721. } // namespace Shader
  722. } // namespace Pica