OpcodeDecoding.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. // DL facts:
  4. // Ikaruga uses (nearly) NO display lists!
  5. // Zelda WW uses TONS of display lists
  6. // Zelda TP uses almost 100% display lists except menus (we like this!)
  7. // Super Mario Galaxy has nearly all geometry and more than half of the state in DLs (great!)
  8. // Note that it IS NOT GENERALLY POSSIBLE to precompile display lists! You can compile them as they
  9. // are while interpreting them, and hope that the vertex format doesn't change, though, if you do
  10. // it right when they are called. The reason is that the vertex format affects the sizes of the
  11. // vertices.
  12. #include "VideoCommon/OpcodeDecoding.h"
  13. #include "Common/Assert.h"
  14. #include "Common/Logging/Log.h"
  15. #include "Core/FifoPlayer/FifoRecorder.h"
  16. #include "Core/HW/Memmap.h"
  17. #include "Core/System.h"
  18. #include "VideoCommon/BPMemory.h"
  19. #include "VideoCommon/CPMemory.h"
  20. #include "VideoCommon/CommandProcessor.h"
  21. #include "VideoCommon/DataReader.h"
  22. #include "VideoCommon/Fifo.h"
  23. #include "VideoCommon/Statistics.h"
  24. #include "VideoCommon/VertexLoaderBase.h"
  25. #include "VideoCommon/VertexLoaderManager.h"
  26. #include "VideoCommon/XFMemory.h"
  27. #include "VideoCommon/XFStateManager.h"
  28. #include "VideoCommon/XFStructs.h"
  29. namespace OpcodeDecoder
  30. {
  31. bool g_record_fifo_data = false;
  32. template <bool is_preprocess>
  33. class RunCallback final : public Callback
  34. {
  35. public:
  36. OPCODE_CALLBACK(void OnXF(u16 address, u8 count, const u8* data))
  37. {
  38. m_cycles += 18 + 6 * count;
  39. if constexpr (!is_preprocess)
  40. {
  41. LoadXFReg(address, count, data);
  42. INCSTAT(g_stats.this_frame.num_xf_loads);
  43. }
  44. }
  45. OPCODE_CALLBACK(void OnCP(u8 command, u32 value))
  46. {
  47. m_cycles += 12;
  48. const u8 sub_command = command & CP_COMMAND_MASK;
  49. if constexpr (!is_preprocess)
  50. {
  51. if (sub_command == MATINDEX_A)
  52. {
  53. VertexLoaderManager::g_needs_cp_xf_consistency_check = true;
  54. auto& system = Core::System::GetInstance();
  55. system.GetXFStateManager().SetTexMatrixChangedA(value);
  56. }
  57. else if (sub_command == MATINDEX_B)
  58. {
  59. VertexLoaderManager::g_needs_cp_xf_consistency_check = true;
  60. auto& system = Core::System::GetInstance();
  61. system.GetXFStateManager().SetTexMatrixChangedB(value);
  62. }
  63. else if (sub_command == VCD_LO || sub_command == VCD_HI)
  64. {
  65. VertexLoaderManager::g_main_vat_dirty = BitSet8::AllTrue(CP_NUM_VAT_REG);
  66. VertexLoaderManager::g_bases_dirty = true;
  67. VertexLoaderManager::g_needs_cp_xf_consistency_check = true;
  68. }
  69. else if (sub_command == CP_VAT_REG_A || sub_command == CP_VAT_REG_B ||
  70. sub_command == CP_VAT_REG_C)
  71. {
  72. VertexLoaderManager::g_main_vat_dirty[command & CP_VAT_MASK] = true;
  73. VertexLoaderManager::g_needs_cp_xf_consistency_check = true;
  74. }
  75. else if (sub_command == ARRAY_BASE)
  76. {
  77. VertexLoaderManager::g_bases_dirty = true;
  78. }
  79. INCSTAT(g_stats.this_frame.num_cp_loads);
  80. }
  81. else if constexpr (is_preprocess)
  82. {
  83. if (sub_command == VCD_LO || sub_command == VCD_HI)
  84. {
  85. VertexLoaderManager::g_preprocess_vat_dirty = BitSet8::AllTrue(CP_NUM_VAT_REG);
  86. }
  87. else if (sub_command == CP_VAT_REG_A || sub_command == CP_VAT_REG_B ||
  88. sub_command == CP_VAT_REG_C)
  89. {
  90. VertexLoaderManager::g_preprocess_vat_dirty[command & CP_VAT_MASK] = true;
  91. }
  92. }
  93. GetCPState().LoadCPReg(command, value);
  94. }
  95. OPCODE_CALLBACK(void OnBP(u8 command, u32 value))
  96. {
  97. m_cycles += 12;
  98. if constexpr (is_preprocess)
  99. {
  100. LoadBPRegPreprocess(command, value, m_cycles);
  101. }
  102. else
  103. {
  104. LoadBPReg(command, value, m_cycles);
  105. INCSTAT(g_stats.this_frame.num_bp_loads);
  106. }
  107. }
  108. OPCODE_CALLBACK(void OnIndexedLoad(CPArray array, u32 index, u16 address, u8 size))
  109. {
  110. m_cycles += 6;
  111. if constexpr (is_preprocess)
  112. PreprocessIndexedXF(array, index, address, size);
  113. else
  114. LoadIndexedXF(array, index, address, size);
  115. }
  116. OPCODE_CALLBACK(void OnPrimitiveCommand(OpcodeDecoder::Primitive primitive, u8 vat,
  117. u32 vertex_size, u16 num_vertices, const u8* vertex_data))
  118. {
  119. // load vertices
  120. const u32 size = vertex_size * num_vertices;
  121. const u32 bytes =
  122. VertexLoaderManager::RunVertices<is_preprocess>(vat, primitive, num_vertices, vertex_data);
  123. ASSERT(bytes == size);
  124. // 4 GPU ticks per vertex, 3 CPU ticks per GPU tick
  125. m_cycles += num_vertices * 4 * 3 + 6;
  126. }
  127. // This can't be inlined since it calls Run, which makes it recursive
  128. // m_in_display_list prevents it from actually recursing infinitely, but there's no real benefit
  129. // to inlining Run for the display list directly.
  130. OPCODE_CALLBACK_NOINLINE(void OnDisplayList(u32 address, u32 size))
  131. {
  132. m_cycles += 6;
  133. if (m_in_display_list)
  134. {
  135. WARN_LOG_FMT(VIDEO, "recursive display list detected");
  136. }
  137. else
  138. {
  139. m_in_display_list = true;
  140. auto& system = Core::System::GetInstance();
  141. if constexpr (is_preprocess)
  142. {
  143. auto& memory = system.GetMemory();
  144. const u8* const start_address = memory.GetPointerForRange(address, size);
  145. system.GetFifo().PushFifoAuxBuffer(start_address, size);
  146. if (start_address != nullptr)
  147. {
  148. Run(start_address, size, *this);
  149. }
  150. }
  151. else
  152. {
  153. const u8* start_address;
  154. auto& fifo = system.GetFifo();
  155. if (fifo.UseDeterministicGPUThread())
  156. {
  157. start_address = static_cast<u8*>(fifo.PopFifoAuxBuffer(size));
  158. }
  159. else
  160. {
  161. auto& memory = system.GetMemory();
  162. start_address = memory.GetPointerForRange(address, size);
  163. }
  164. // Avoid the crash if memory.GetPointerForRange failed ..
  165. if (start_address != nullptr)
  166. {
  167. // temporarily swap dl and non-dl (small "hack" for the stats)
  168. g_stats.SwapDL();
  169. Run(start_address, size, *this);
  170. INCSTAT(g_stats.this_frame.num_dlists_called);
  171. // un-swap
  172. g_stats.SwapDL();
  173. }
  174. }
  175. m_in_display_list = false;
  176. }
  177. }
  178. OPCODE_CALLBACK(void OnNop(u32 count))
  179. {
  180. m_cycles += 6 * count; // Hm, this means that we scan over nop streams pretty slowly...
  181. }
  182. OPCODE_CALLBACK(void OnUnknown(u8 opcode, const u8* data))
  183. {
  184. if (static_cast<Opcode>(opcode) == Opcode::GX_CMD_UNKNOWN_METRICS)
  185. {
  186. // 'Zelda Four Swords' calls it and checks the metrics registers after that
  187. m_cycles += 6;
  188. DEBUG_LOG_FMT(VIDEO, "GX 0x44");
  189. }
  190. else if (static_cast<Opcode>(opcode) == Opcode::GX_CMD_INVL_VC)
  191. {
  192. // Invalidate Vertex Cache
  193. m_cycles += 6;
  194. DEBUG_LOG_FMT(VIDEO, "Invalidate (vertex cache?)");
  195. }
  196. else
  197. {
  198. auto& system = Core::System::GetInstance();
  199. system.GetCommandProcessor().HandleUnknownOpcode(opcode, data, is_preprocess);
  200. m_cycles += 1;
  201. }
  202. }
  203. OPCODE_CALLBACK(void OnCommand(const u8* data, u32 size))
  204. {
  205. ASSERT(size >= 1);
  206. if constexpr (!is_preprocess)
  207. {
  208. // Display lists get added directly into the FIFO stream since this same callback is used to
  209. // process them.
  210. if (g_record_fifo_data && static_cast<Opcode>(data[0]) != Opcode::GX_CMD_CALL_DL)
  211. {
  212. Core::System::GetInstance().GetFifoRecorder().WriteGPCommand(data, size);
  213. }
  214. }
  215. }
  216. OPCODE_CALLBACK(CPState& GetCPState())
  217. {
  218. if constexpr (is_preprocess)
  219. return g_preprocess_cp_state;
  220. else
  221. return g_main_cp_state;
  222. }
  223. OPCODE_CALLBACK(u32 GetVertexSize(u8 vat))
  224. {
  225. VertexLoaderBase* loader = VertexLoaderManager::RefreshLoader<is_preprocess>(vat);
  226. return loader->m_vertex_size;
  227. }
  228. u32 m_cycles = 0;
  229. bool m_in_display_list = false;
  230. };
  231. template <bool is_preprocess>
  232. u8* RunFifo(DataReader src, u32* cycles)
  233. {
  234. using CallbackT = RunCallback<is_preprocess>;
  235. auto callback = CallbackT{};
  236. u32 size = Run(src.GetPointer(), static_cast<u32>(src.size()), callback);
  237. if (cycles != nullptr)
  238. *cycles = callback.m_cycles;
  239. src.Skip(size);
  240. return src.GetPointer();
  241. }
  242. template u8* RunFifo<true>(DataReader src, u32* cycles);
  243. template u8* RunFifo<false>(DataReader src, u32* cycles);
  244. } // namespace OpcodeDecoder