shader_jit_x64.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <cstddef>
  7. #include <utility>
  8. #include <vector>
  9. #include <nihstro/shader_bytecode.h>
  10. #include "common/bit_set.h"
  11. #include "common/common_types.h"
  12. #include "common/x64/emitter.h"
  13. #include "video_core/shader/shader.h"
  14. using nihstro::Instruction;
  15. using nihstro::OpCode;
  16. using nihstro::SwizzlePattern;
  17. namespace Pica {
  18. namespace Shader {
  19. /// Memory allocated for each compiled shader (64Kb)
  20. constexpr size_t MAX_SHADER_SIZE = 1024 * 64;
  21. /**
  22. * This class implements the shader JIT compiler. It recompiles a Pica shader program into x86_64
  23. * code that can be executed on the host machine directly.
  24. */
  25. class JitShader : public Gen::XCodeBlock {
  26. public:
  27. JitShader();
  28. void Run(const ShaderSetup& setup, UnitState<false>& state, unsigned offset) const {
  29. program(&setup, &state, code_ptr[offset]);
  30. }
  31. void Compile();
  32. void Compile_ADD(Instruction instr);
  33. void Compile_DP3(Instruction instr);
  34. void Compile_DP4(Instruction instr);
  35. void Compile_DPH(Instruction instr);
  36. void Compile_EX2(Instruction instr);
  37. void Compile_LG2(Instruction instr);
  38. void Compile_MUL(Instruction instr);
  39. void Compile_SGE(Instruction instr);
  40. void Compile_SLT(Instruction instr);
  41. void Compile_FLR(Instruction instr);
  42. void Compile_MAX(Instruction instr);
  43. void Compile_MIN(Instruction instr);
  44. void Compile_RCP(Instruction instr);
  45. void Compile_RSQ(Instruction instr);
  46. void Compile_MOVA(Instruction instr);
  47. void Compile_MOV(Instruction instr);
  48. void Compile_NOP(Instruction instr);
  49. void Compile_END(Instruction instr);
  50. void Compile_CALL(Instruction instr);
  51. void Compile_CALLC(Instruction instr);
  52. void Compile_CALLU(Instruction instr);
  53. void Compile_IF(Instruction instr);
  54. void Compile_LOOP(Instruction instr);
  55. void Compile_JMP(Instruction instr);
  56. void Compile_CMP(Instruction instr);
  57. void Compile_MAD(Instruction instr);
  58. private:
  59. void Compile_Block(unsigned end);
  60. void Compile_NextInstr();
  61. void Compile_SwizzleSrc(Instruction instr, unsigned src_num, SourceRegister src_reg,
  62. Gen::X64Reg dest);
  63. void Compile_DestEnable(Instruction instr, Gen::X64Reg dest);
  64. /**
  65. * Compiles a `MUL src1, src2` operation, properly handling the PICA semantics when multiplying
  66. * zero by inf. Clobbers `src2` and `scratch`.
  67. */
  68. void Compile_SanitizedMul(Gen::X64Reg src1, Gen::X64Reg src2, Gen::X64Reg scratch);
  69. void Compile_EvaluateCondition(Instruction instr);
  70. void Compile_UniformCondition(Instruction instr);
  71. /**
  72. * Emits the code to conditionally return from a subroutine envoked by the `CALL` instruction.
  73. */
  74. void Compile_Return();
  75. BitSet32 PersistentCallerSavedRegs();
  76. /**
  77. * Assertion evaluated at compile-time, but only triggered if executed at runtime.
  78. * @param msg Message to be logged if the assertion fails.
  79. */
  80. void Compile_Assert(bool condition, const char* msg);
  81. /**
  82. * Analyzes the entire shader program for `CALL` instructions before emitting any code,
  83. * identifying the locations where a return needs to be inserted.
  84. */
  85. void FindReturnOffsets();
  86. /// Mapping of Pica VS instructions to pointers in the emitted code
  87. std::array<const u8*, 1024> code_ptr;
  88. /// Offsets in code where a return needs to be inserted
  89. std::vector<unsigned> return_offsets;
  90. unsigned program_counter = 0; ///< Offset of the next instruction to decode
  91. bool looping = false; ///< True if compiling a loop, used to check for nested loops
  92. /// Branches that need to be fixed up once the entire shader program is compiled
  93. std::vector<std::pair<Gen::FixupBranch, unsigned>> fixup_branches;
  94. using CompiledShader = void(const void* setup, void* state, const u8* start_addr);
  95. CompiledShader* program = nullptr;
  96. };
  97. } // Shader
  98. } // Pica