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