| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/FIFO/FIFOAnalyzer.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <bit> |
| 8 | #include <ranges> |
| 9 | #include <utility> |
| 10 | |
| 11 | #include <QGroupBox> |
| 12 | #include <QHBoxLayout> |
| 13 | #include <QHeaderView> |
| 14 | #include <QLabel> |
| 15 | #include <QLineEdit> |
| 16 | #include <QListWidget> |
| 17 | #include <QPushButton> |
| 18 | #include <QSplitter> |
| 19 | #include <QTextBrowser> |
| 20 | #include <QTreeWidget> |
| 21 | #include <QTreeWidgetItem> |
| 22 | |
| 23 | #include "Common/Assert.h" |
| 24 | #include "Common/Swap.h" |
| 25 | #include "Core/FifoPlayer/FifoPlayer.h" |
| 26 | |
| 27 | #include "DolphinQt/QtUtils/NonDefaultQPushButton.h" |
| 28 | #include "DolphinQt/Settings.h" |
| 29 | |
| 30 | #include "VideoCommon/BPMemory.h" |
| 31 | #include "VideoCommon/CPMemory.h" |
| 32 | #include "VideoCommon/OpcodeDecoding.h" |
| 33 | #include "VideoCommon/XFStructs.h" |
| 34 | |
| 35 | // Values range from 0 to number of frames - 1 |
| 36 | constexpr int FRAME_ROLE = Qt::UserRole; |
| 37 | // Values range from 0 to number of parts - 1 |
| 38 | constexpr int PART_START_ROLE = Qt::UserRole + 1; |
| 39 | // Values range from 1 to number of parts |
| 40 | constexpr int PART_END_ROLE = Qt::UserRole + 2; |
| 41 | |
| 42 | FIFOAnalyzer::FIFOAnalyzer(FifoPlayer& fifo_player) : m_fifo_player(fifo_player) |
| 43 | { |
| 44 | CreateWidgets(); |
| 45 | ConnectWidgets(); |
| 46 | |
| 47 | UpdateTree(); |
| 48 | |
| 49 | const auto& settings = Settings::GetQSettings(); |
| 50 | |
| 51 | m_object_splitter->restoreState( |
| 52 | settings.value(QStringLiteral("fifoanalyzer/objectsplitter")).toByteArray()); |
| 53 | m_search_splitter->restoreState( |
| 54 | settings.value(QStringLiteral("fifoanalyzer/searchsplitter")).toByteArray()); |
| 55 | |
| 56 | OnDebugFontChanged(Settings::Instance().GetDebugFont()); |
| 57 | connect(&Settings::Instance(), &Settings::DebugFontChanged, this, |
| 58 | &FIFOAnalyzer::OnDebugFontChanged); |
| 59 | } |
| 60 | |
| 61 | FIFOAnalyzer::~FIFOAnalyzer() |
| 62 | { |
| 63 | auto& settings = Settings::GetQSettings(); |
| 64 | |
| 65 | settings.setValue(QStringLiteral("fifoanalyzer/objectsplitter"), m_object_splitter->saveState()); |
| 66 | settings.setValue(QStringLiteral("fifoanalyzer/searchsplitter"), m_search_splitter->saveState()); |
| 67 | } |
| 68 | |
| 69 | void FIFOAnalyzer::CreateWidgets() |
| 70 | { |
| 71 | m_tree_widget = new QTreeWidget; |
| 72 | m_detail_list = new QListWidget; |
| 73 | m_entry_detail_browser = new QTextBrowser; |
| 74 | |
| 75 | m_object_splitter = new QSplitter(Qt::Horizontal); |
| 76 | |
| 77 | m_object_splitter->addWidget(m_tree_widget); |
| 78 | m_object_splitter->addWidget(m_detail_list); |
| 79 | |
| 80 | m_tree_widget->header()->hide(); |
| 81 | |
| 82 | m_search_box = new QGroupBox(tr("Search Current Object")); |
| 83 | m_search_edit = new QLineEdit; |
| 84 | m_search_new = new NonDefaultQPushButton(tr("Search")); |
| 85 | m_search_next = new NonDefaultQPushButton(tr("Next Match")); |
| 86 | m_search_previous = new NonDefaultQPushButton(tr("Previous Match")); |
| 87 | m_search_label = new QLabel; |
| 88 | |
| 89 | m_search_next->setEnabled(false); |
| 90 | m_search_previous->setEnabled(false); |
| 91 | |
| 92 | auto* box_layout = new QHBoxLayout; |
| 93 | |
| 94 | box_layout->addWidget(m_search_edit); |
| 95 | box_layout->addWidget(m_search_new); |
| 96 | box_layout->addWidget(m_search_next); |
| 97 | box_layout->addWidget(m_search_previous); |
| 98 | box_layout->addWidget(m_search_label); |
| 99 | |
| 100 | m_search_box->setLayout(box_layout); |
| 101 | |
| 102 | m_search_box->setMaximumHeight(m_search_box->minimumSizeHint().height()); |
| 103 | |
| 104 | m_search_splitter = new QSplitter(Qt::Vertical); |
| 105 | |
| 106 | m_search_splitter->addWidget(m_object_splitter); |
| 107 | m_search_splitter->addWidget(m_entry_detail_browser); |
| 108 | m_search_splitter->addWidget(m_search_box); |
| 109 | |
| 110 | auto* layout = new QHBoxLayout; |
| 111 | layout->addWidget(m_search_splitter); |
| 112 | |
| 113 | setLayout(layout); |
| 114 | } |
| 115 | |
| 116 | void FIFOAnalyzer::ConnectWidgets() |
| 117 | { |
| 118 | connect(m_tree_widget, &QTreeWidget::itemSelectionChanged, this, &FIFOAnalyzer::UpdateDetails); |
| 119 | connect(m_detail_list, &QListWidget::currentRowChanged, this, &FIFOAnalyzer::UpdateDescription); |
| 120 | |
| 121 | connect(m_search_edit, &QLineEdit::returnPressed, this, &FIFOAnalyzer::BeginSearch); |
| 122 | connect(m_search_new, &QPushButton::clicked, this, &FIFOAnalyzer::BeginSearch); |
| 123 | connect(m_search_next, &QPushButton::clicked, this, &FIFOAnalyzer::FindNext); |
| 124 | connect(m_search_previous, &QPushButton::clicked, this, &FIFOAnalyzer::FindPrevious); |
| 125 | } |
| 126 | |
| 127 | void FIFOAnalyzer::Update() |
| 128 | { |
| 129 | UpdateTree(); |
| 130 | UpdateDetails(); |
| 131 | UpdateDescription(); |
| 132 | } |
| 133 | |
| 134 | void FIFOAnalyzer::UpdateTree() |
| 135 | { |
| 136 | m_tree_widget->clear(); |
| 137 | |
| 138 | if (!m_fifo_player.IsPlaying()) |
| 139 | { |
| 140 | m_tree_widget->addTopLevelItem(new QTreeWidgetItem({tr("No recording loaded.")})); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | auto* recording_item = new QTreeWidgetItem({tr("Recording")}); |
| 145 | |
| 146 | m_tree_widget->addTopLevelItem(recording_item); |
| 147 | |
| 148 | const auto* const file = m_fifo_player.GetFile(); |
| 149 | |
| 150 | const u32 frame_count = file->GetFrameCount(); |
| 151 | |
| 152 | for (u32 frame = 0; frame < frame_count; frame++) |
| 153 | { |
| 154 | auto* frame_item = new QTreeWidgetItem({tr("Frame %1").arg(frame)}); |
| 155 | |
| 156 | recording_item->addChild(frame_item); |
| 157 | |
| 158 | const AnalyzedFrameInfo& frame_info = m_fifo_player.GetAnalyzedFrameInfo(frame); |
| 159 | ASSERT(frame_info.parts.size() != 0); |
| 160 | |
| 161 | Common::EnumMap<u32, FramePartType::EFBCopy> part_counts; |
| 162 | u32 part_start = 0; |
| 163 | |
| 164 | for (u32 part_nr = 0; part_nr < frame_info.parts.size(); part_nr++) |
| 165 | { |
| 166 | const auto& part = frame_info.parts[part_nr]; |
| 167 | |
| 168 | const u32 part_type_nr = part_counts[part.m_type]; |
| 169 | part_counts[part.m_type]++; |
| 170 | |
| 171 | QTreeWidgetItem* object_item = nullptr; |
| 172 | if (part.m_type == FramePartType::PrimitiveData) |
| 173 | object_item = new QTreeWidgetItem({tr("Object %1").arg(part_type_nr)}); |
| 174 | else if (part.m_type == FramePartType::EFBCopy) |
| 175 | object_item = new QTreeWidgetItem({tr("EFB copy %1").arg(part_type_nr)}); |
| 176 | // We don't create dedicated labels for FramePartType::Command; |
| 177 | // those are grouped with the primitive |
| 178 | |
| 179 | if (object_item != nullptr) |
| 180 | { |
| 181 | frame_item->addChild(object_item); |
| 182 | |
| 183 | object_item->setData(0, FRAME_ROLE, frame); |
| 184 | object_item->setData(0, PART_START_ROLE, part_start); |
| 185 | object_item->setData(0, PART_END_ROLE, part_nr); |
| 186 | |
| 187 | part_start = part_nr + 1; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // We shouldn't end on a Command (it should end with an EFB copy) |
| 192 | ASSERT(part_start == frame_info.parts.size()); |
| 193 | // The counts we computed should match the frame's counts |
| 194 | ASSERT(std::ranges::equal(frame_info.part_type_counts, part_counts)); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | namespace |
| 199 | { |
| 200 | class DetailCallback : public OpcodeDecoder::Callback |
| 201 | { |
| 202 | public: |
| 203 | explicit DetailCallback(const CPState& cpmem) : m_cpmem(cpmem) {} |
| 204 | |
| 205 | OPCODE_CALLBACK(void OnCP(const u8 command, const u32 value)) |
| 206 | { |
| 207 | // Note: No need to update m_cpmem as it already has the final value for this object |
| 208 | |
| 209 | const auto [name, desc] = GetCPRegInfo(command, value); |
| 210 | ASSERT(!name.empty()); |
| 211 | |
| 212 | text = QStringLiteral("CP %1 %2 %3") |
| 213 | .arg(command, 2, 16, QLatin1Char('0')) |
| 214 | .arg(value, 8, 16, QLatin1Char('0')) |
| 215 | .arg(QString::fromStdString(name)); |
| 216 | } |
| 217 | |
| 218 | OPCODE_CALLBACK(void OnXF(const u16 address, const u8 count, const u8* data)) |
| 219 | { |
| 220 | const auto [name, desc] = GetXFTransferInfo(address, count, data); |
| 221 | ASSERT(!name.empty()); |
| 222 | |
| 223 | const u32 command = address | ((count - 1) << 16); |
| 224 | |
| 225 | text = QStringLiteral("XF %1 ").arg(command, 8, 16, QLatin1Char('0')); |
| 226 | |
| 227 | for (u8 i = 0; i < count; i++) |
| 228 | { |
| 229 | const u32 value = Common::swap32(&data[i * 4]); |
| 230 | |
| 231 | text += QStringLiteral("%1 ").arg(value, 8, 16, QLatin1Char('0')); |
| 232 | } |
| 233 | |
| 234 | text += QStringLiteral(" ") + QString::fromStdString(name); |
| 235 | } |
| 236 | |
| 237 | OPCODE_CALLBACK(void OnBP(const u8 command, const u32 value)) |
| 238 | { |
| 239 | const auto [name, desc] = GetBPRegInfo(command, value); |
| 240 | ASSERT(!name.empty()); |
| 241 | |
| 242 | text = QStringLiteral("BP %1 %2 %3") |
| 243 | .arg(command, 2, 16, QLatin1Char('0')) |
| 244 | .arg(value, 6, 16, QLatin1Char('0')) |
| 245 | .arg(QString::fromStdString(name)); |
| 246 | } |
| 247 | OPCODE_CALLBACK(void OnIndexedLoad(const CPArray array, const u32 index, const u16 address, |
| 248 | const u8 size)) |
| 249 | { |
| 250 | const auto [desc, written] = GetXFIndexedLoadInfo(array, index, address, size); |
| 251 | text = QStringLiteral("LOAD INDX %1 %2") |
| 252 | .arg(QString::fromStdString(fmt::to_string(array))) |
| 253 | .arg(QString::fromStdString(desc)); |
| 254 | } |
| 255 | OPCODE_CALLBACK(void OnPrimitiveCommand(const OpcodeDecoder::Primitive primitive, const u8 vat, |
| 256 | const u32 vertex_size, const u16 num_vertices, |
| 257 | const u8* vertex_data)) |
| 258 | { |
| 259 | const auto name = fmt::to_string(primitive); |
| 260 | |
| 261 | // Note that vertex_count is allowed to be 0, with no special treatment |
| 262 | // (another command just comes right after the current command, with no vertices in between) |
| 263 | const u32 object_prim_size = num_vertices * vertex_size; |
| 264 | |
| 265 | const u8 opcode = |
| 266 | 0x80 | std::to_underlying(primitive) << OpcodeDecoder::GX_PRIMITIVE_SHIFT | vat; |
| 267 | text = QStringLiteral("PRIMITIVE %1 (%2) %3 vertices %4 bytes/vertex %5 total bytes") |
| 268 | .arg(QString::fromStdString(name)) |
| 269 | .arg(opcode, 2, 16, QLatin1Char('0')) |
| 270 | .arg(num_vertices) |
| 271 | .arg(vertex_size) |
| 272 | .arg(object_prim_size); |
| 273 | |
| 274 | // It's not really useful to have a massive unreadable hex string for the object primitives. |
| 275 | // Put it in the description instead. |
| 276 | |
| 277 | // #define INCLUDE_HEX_IN_PRIMITIVES |
| 278 | #ifdef INCLUDE_HEX_IN_PRIMITIVES |
| 279 | text += QStringLiteral(" "); |
| 280 | for (u32 i = 0; i < object_prim_size; i++) |
| 281 | { |
| 282 | text += QStringLiteral("%1").arg(vertex_data[i], 2, 16, QLatin1Char('0')); |
| 283 | } |
| 284 | #endif |
| 285 | } |
| 286 | |
| 287 | OPCODE_CALLBACK(void OnDisplayList(const u32 address, const u32 size)) |
| 288 | { |
| 289 | text = QObject::tr("Call display list at %1 with size %2") |
| 290 | .arg(address, 8, 16, QLatin1Char('0')) |
| 291 | .arg(size, 8, 16, QLatin1Char('0')); |
| 292 | } |
| 293 | |
| 294 | OPCODE_CALLBACK(void OnNop(const u32 count)) |
| 295 | { |
| 296 | if (count > 1) |
| 297 | text = QStringLiteral("NOP (%1x)").arg(count); |
| 298 | else |
| 299 | text = QStringLiteral("NOP"); |
| 300 | } |
| 301 | |
| 302 | OPCODE_CALLBACK(void OnUnknown(u8 opcode, const u8* data)) |
| 303 | { |
| 304 | using OpcodeDecoder::Opcode; |
| 305 | if (static_cast<Opcode>(opcode) == Opcode::GX_CMD_UNKNOWN_METRICS) |
| 306 | text = QStringLiteral("GX_CMD_UNKNOWN_METRICS"); |
| 307 | else if (static_cast<Opcode>(opcode) == Opcode::GX_CMD_INVL_VC) |
| 308 | text = QStringLiteral("GX_CMD_INVL_VC"); |
| 309 | else |
| 310 | text = QStringLiteral("Unknown opcode %1").arg(opcode, 2, 16); |
| 311 | } |
| 312 | |
| 313 | OPCODE_CALLBACK(void OnCommand(const u8* data, u32 size)) {} |
| 314 | |
| 315 | OPCODE_CALLBACK(CPState& GetCPState()) { return m_cpmem; } |
| 316 | |
| 317 | QString text; |
| 318 | CPState m_cpmem; |
| 319 | }; |
| 320 | } // namespace |
| 321 | |
| 322 | void FIFOAnalyzer::UpdateDetails() |
| 323 | { |
| 324 | // Clearing the detail list can update the selection, which causes UpdateDescription to be called |
| 325 | // immediately. However, the object data offsets have not been recalculated yet, which can cause |
| 326 | // the wrong data to be used, potentially leading to out of bounds data or other bad things. |
| 327 | // Clear m_object_data_offsets first, so that UpdateDescription exits immediately. |
| 328 | m_object_data_offsets.clear(); |
| 329 | m_detail_list->clear(); |
| 330 | m_search_results.clear(); |
| 331 | m_search_next->setEnabled(false); |
| 332 | m_search_previous->setEnabled(false); |
| 333 | m_search_label->clear(); |
| 334 | |
| 335 | if (!m_fifo_player.IsPlaying()) |
| 336 | return; |
| 337 | |
| 338 | const auto items = m_tree_widget->selectedItems(); |
| 339 | |
| 340 | if (items.isEmpty() || items[0]->data(0, PART_START_ROLE).isNull()) |
| 341 | return; |
| 342 | |
| 343 | const u32 frame_nr = items[0]->data(0, FRAME_ROLE).toUInt(); |
| 344 | const u32 start_part_nr = items[0]->data(0, PART_START_ROLE).toUInt(); |
| 345 | const u32 end_part_nr = items[0]->data(0, PART_END_ROLE).toUInt(); |
| 346 | |
| 347 | const AnalyzedFrameInfo& frame_info = m_fifo_player.GetAnalyzedFrameInfo(frame_nr); |
| 348 | const auto& fifo_frame = m_fifo_player.GetFile()->GetFrame(frame_nr); |
| 349 | |
| 350 | const u32 object_start = frame_info.parts[start_part_nr].m_start; |
| 351 | const u32 object_end = frame_info.parts[end_part_nr].m_end; |
| 352 | const u32 object_size = object_end - object_start; |
| 353 | |
| 354 | u32 object_offset = 0; |
| 355 | // NOTE: object_info.m_cpmem is the state of cpmem _after_ all of the commands in this object. |
| 356 | // However, it doesn't matter that it doesn't match the start, since it will match by the time |
| 357 | // primitives are reached. |
| 358 | auto callback = DetailCallback(frame_info.parts[end_part_nr].m_cpmem); |
| 359 | |
| 360 | while (object_offset < object_size) |
| 361 | { |
| 362 | const u32 start_offset = object_offset; |
| 363 | m_object_data_offsets.push_back(start_offset); |
| 364 | |
| 365 | object_offset += OpcodeDecoder::RunCommand(&fifo_frame.fifoData[object_start + start_offset], |
| 366 | object_size - start_offset, callback); |
| 367 | |
| 368 | QString new_label = |
| 369 | QStringLiteral("%1: ").arg(object_start + start_offset, 8, 16, QLatin1Char('0')) + |
| 370 | callback.text; |
| 371 | m_detail_list->addItem(new_label); |
| 372 | } |
| 373 | |
| 374 | // Needed to ensure the description updates when changing objects |
| 375 | m_detail_list->setCurrentRow(0); |
| 376 | } |
| 377 | |
| 378 | void FIFOAnalyzer::BeginSearch() |
| 379 | { |
| 380 | const QString search_str = m_search_edit->text(); |
| 381 | |
| 382 | if (!m_fifo_player.IsPlaying()) |
| 383 | return; |
| 384 | |
| 385 | const auto items = m_tree_widget->selectedItems(); |
| 386 | |
| 387 | if (items.isEmpty() || items[0]->data(0, FRAME_ROLE).isNull() || |
| 388 | items[0]->data(0, PART_START_ROLE).isNull()) |
| 389 | { |
| 390 | m_search_label->setText(tr("Invalid search parameters (no object selected)")); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | // Having PART_START_ROLE indicates that this is valid |
| 395 | const int object_idx = items[0]->parent()->indexOfChild(items[0]); |
| 396 | |
| 397 | // TODO: Remove even string length limit |
| 398 | if (search_str.length() % 2) |
| 399 | { |
| 400 | m_search_label->setText(tr("Invalid search string (only even string lengths supported)")); |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | const size_t length = search_str.length() / 2; |
| 405 | |
| 406 | std::vector<u8> search_val; |
| 407 | |
| 408 | for (size_t i = 0; i < length; i++) |
| 409 | { |
| 410 | const QString byte_str = search_str.mid(static_cast<int>(i * 2), 2); |
| 411 | |
| 412 | bool good; |
| 413 | u8 value = byte_str.toUInt(&good, 16); |
| 414 | |
| 415 | if (!good) |
| 416 | { |
| 417 | m_search_label->setText(tr("Invalid search string (couldn't convert to number)")); |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | search_val.push_back(value); |
| 422 | } |
| 423 | |
| 424 | m_search_results.clear(); |
| 425 | |
| 426 | const u32 frame_nr = items[0]->data(0, FRAME_ROLE).toUInt(); |
| 427 | const u32 start_part_nr = items[0]->data(0, PART_START_ROLE).toUInt(); |
| 428 | const u32 end_part_nr = items[0]->data(0, PART_END_ROLE).toUInt(); |
| 429 | |
| 430 | const AnalyzedFrameInfo& frame_info = m_fifo_player.GetAnalyzedFrameInfo(frame_nr); |
| 431 | const FifoFrameInfo& fifo_frame = m_fifo_player.GetFile()->GetFrame(frame_nr); |
| 432 | |
| 433 | const u32 object_start = frame_info.parts[start_part_nr].m_start; |
| 434 | const u32 object_end = frame_info.parts[end_part_nr].m_end; |
| 435 | const u32 object_size = object_end - object_start; |
| 436 | |
| 437 | const u8* const object = &fifo_frame.fifoData[object_start]; |
| 438 | |
| 439 | // TODO: Support searching for bit patterns |
| 440 | for (u32 cmd_nr = 0; cmd_nr < m_object_data_offsets.size(); cmd_nr++) |
| 441 | { |
| 442 | const u32 cmd_start = m_object_data_offsets[cmd_nr]; |
| 443 | const u32 cmd_end = (cmd_nr + 1 == m_object_data_offsets.size()) ? |
| 444 | object_size : |
| 445 | m_object_data_offsets[cmd_nr + 1]; |
| 446 | |
| 447 | const u8* const cmd_start_ptr = &object[cmd_start]; |
| 448 | const u8* const cmd_end_ptr = &object[cmd_end]; |
| 449 | |
| 450 | for (const u8* ptr = cmd_start_ptr; ptr < cmd_end_ptr - length + 1; ptr++) |
| 451 | { |
| 452 | if (std::equal(search_val.begin(), search_val.end(), ptr)) |
| 453 | { |
| 454 | m_search_results.emplace_back(frame_nr, object_idx, cmd_nr); |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | ShowSearchResult(0); |
| 461 | |
| 462 | m_search_label->setText( |
| 463 | tr("Found %1 results for \"%2\"").arg(m_search_results.size()).arg(search_str)); |
| 464 | } |
| 465 | |
| 466 | void FIFOAnalyzer::FindNext() |
| 467 | { |
| 468 | const int index = m_detail_list->currentRow(); |
| 469 | ASSERT(index >= 0); |
| 470 | |
| 471 | const auto next_result = std::ranges::find_if( |
| 472 | m_search_results, [index](auto& result) { return result.m_cmd > static_cast<u32>(index); }); |
| 473 | if (next_result != m_search_results.end()) |
| 474 | { |
| 475 | ShowSearchResult(next_result - m_search_results.begin()); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | void FIFOAnalyzer::FindPrevious() |
| 480 | { |
| 481 | const int index = m_detail_list->currentRow(); |
| 482 | ASSERT(index >= 0); |
| 483 | |
| 484 | const auto prev_result = |
| 485 | std::ranges::find_if(m_search_results | std::views::reverse, [index](auto& result) { |
| 486 | return result.m_cmd < static_cast<u32>(index); |
| 487 | }); |
| 488 | if (prev_result != m_search_results.rend()) |
| 489 | { |
| 490 | ShowSearchResult((m_search_results.rend() - prev_result) - 1); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | void FIFOAnalyzer::ShowSearchResult(const size_t index) |
| 495 | { |
| 496 | if (m_search_results.empty()) |
| 497 | return; |
| 498 | |
| 499 | if (index >= m_search_results.size()) |
| 500 | { |
| 501 | ShowSearchResult(m_search_results.size() - 1); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | const auto& result = m_search_results[index]; |
| 506 | |
| 507 | QTreeWidgetItem* object_item = |
| 508 | m_tree_widget->topLevelItem(0)->child(result.m_frame)->child(result.m_object_idx); |
| 509 | |
| 510 | m_tree_widget->setCurrentItem(object_item); |
| 511 | m_detail_list->setCurrentRow(result.m_cmd); |
| 512 | |
| 513 | m_search_next->setEnabled(index + 1 < m_search_results.size()); |
| 514 | m_search_previous->setEnabled(index > 0); |
| 515 | } |
| 516 | |
| 517 | namespace |
| 518 | { |
| 519 | // TODO: Not sure whether we should bother translating the descriptions |
| 520 | class DescriptionCallback : public OpcodeDecoder::Callback |
| 521 | { |
| 522 | public: |
| 523 | explicit DescriptionCallback(const CPState& cpmem) : m_cpmem(cpmem) {} |
| 524 | |
| 525 | OPCODE_CALLBACK(void OnBP(const u8 command, const u32 value)) |
| 526 | { |
| 527 | const auto [name, desc] = GetBPRegInfo(command, value); |
| 528 | ASSERT(!name.empty()); |
| 529 | |
| 530 | text = QObject::tr("BP register "); |
| 531 | text += QString::fromStdString(name); |
| 532 | text += QLatin1Char{'\n'}; |
| 533 | |
| 534 | if (desc.empty()) |
| 535 | text += QObject::tr("No description available"); |
| 536 | else |
| 537 | text += QString::fromStdString(desc); |
| 538 | } |
| 539 | |
| 540 | OPCODE_CALLBACK(void OnCP(const u8 command, const u32 value)) |
| 541 | { |
| 542 | // Note: No need to update m_cpmem as it already has the final value for this object |
| 543 | |
| 544 | const auto [name, desc] = GetCPRegInfo(command, value); |
| 545 | ASSERT(!name.empty()); |
| 546 | |
| 547 | text = QObject::tr("CP register "); |
| 548 | text += QString::fromStdString(name); |
| 549 | text += QLatin1Char{'\n'}; |
| 550 | |
| 551 | if (desc.empty()) |
| 552 | text += QObject::tr("No description available"); |
| 553 | else |
| 554 | text += QString::fromStdString(desc); |
| 555 | } |
| 556 | |
| 557 | OPCODE_CALLBACK(void OnXF(const u16 address, const u8 count, const u8* data)) |
| 558 | { |
| 559 | const auto [name, desc] = GetXFTransferInfo(address, count, data); |
| 560 | ASSERT(!name.empty()); |
| 561 | |
| 562 | text = QObject::tr("XF register "); |
| 563 | text += QString::fromStdString(name); |
| 564 | text += QLatin1Char{'\n'}; |
| 565 | |
| 566 | if (desc.empty()) |
| 567 | text += QObject::tr("No description available"); |
| 568 | else |
| 569 | text += QString::fromStdString(desc); |
| 570 | } |
| 571 | |
| 572 | OPCODE_CALLBACK(void OnIndexedLoad(const CPArray array, const u32 index, const u16 address, |
| 573 | const u8 size)) |
| 574 | { |
| 575 | const auto [desc, written] = GetXFIndexedLoadInfo(array, index, address, size); |
| 576 | |
| 577 | text = QString::fromStdString(desc); |
| 578 | text += QLatin1Char{'\n'}; |
| 579 | switch (array) |
| 580 | { |
| 581 | case CPArray::XF_A: |
| 582 | text += QObject::tr("Usually used for position matrices"); |
| 583 | break; |
| 584 | case CPArray::XF_B: |
| 585 | // i18n: A normal matrix is a matrix used for transforming normal vectors. The word "normal" |
| 586 | // does not have its usual meaning here, but rather the meaning of "perpendicular to a |
| 587 | // surface". |
| 588 | text += QObject::tr("Usually used for normal matrices"); |
| 589 | break; |
| 590 | case CPArray::XF_C: |
| 591 | // i18n: Tex coord is short for texture coordinate |
| 592 | text += QObject::tr("Usually used for tex coord matrices"); |
| 593 | break; |
| 594 | case CPArray::XF_D: |
| 595 | text += QObject::tr("Usually used for light objects"); |
| 596 | break; |
| 597 | default: |
| 598 | break; |
| 599 | } |
| 600 | text += QLatin1Char{'\n'}; |
| 601 | text += QString::fromStdString(written); |
| 602 | } |
| 603 | |
| 604 | OPCODE_CALLBACK(void OnPrimitiveCommand(OpcodeDecoder::Primitive primitive, u8 vat, |
| 605 | const u32 vertex_size, const u16 num_vertices, |
| 606 | const u8* vertex_data)) |
| 607 | { |
| 608 | const auto name = fmt::format("{} VAT {}", primitive, vat); |
| 609 | |
| 610 | // i18n: In this context, a primitive means a point, line, triangle or rectangle. |
| 611 | // Do not translate the word primitive as if it was an adjective. |
| 612 | text = QObject::tr("Primitive %1").arg(QString::fromStdString(name)); |
| 613 | text += QLatin1Char{'\n'}; |
| 614 | |
| 615 | const auto& vtx_desc = m_cpmem.vtx_desc; |
| 616 | const auto& vtx_attr = m_cpmem.vtx_attr[vat]; |
| 617 | |
| 618 | u32 i = 0; |
| 619 | const auto process_component = [&](const VertexComponentFormat cformat, ComponentFormat format, |
| 620 | const u32 non_indexed_count, const u32 indexed_count = 1) { |
| 621 | u32 count; |
| 622 | if (cformat == VertexComponentFormat::NotPresent) |
| 623 | return; |
| 624 | else if (cformat == VertexComponentFormat::Index8) |
| 625 | { |
| 626 | format = ComponentFormat::UByte; |
| 627 | count = indexed_count; |
| 628 | } |
| 629 | else if (cformat == VertexComponentFormat::Index16) |
| 630 | { |
| 631 | format = ComponentFormat::UShort; |
| 632 | count = indexed_count; |
| 633 | } |
| 634 | else |
| 635 | { |
| 636 | count = non_indexed_count; |
| 637 | } |
| 638 | |
| 639 | const u32 component_size = GetElementSize(format); |
| 640 | for (u32 j = 0; j < count; j++) |
| 641 | { |
| 642 | for (u32 component_off = 0; component_off < component_size; component_off++) |
| 643 | { |
| 644 | text += QStringLiteral("%1").arg(vertex_data[i + component_off], 2, 16, QLatin1Char('0')); |
| 645 | } |
| 646 | if (format == ComponentFormat::Float) |
| 647 | { |
| 648 | const float value = std::bit_cast<float>(Common::swap32(&vertex_data[i])); |
| 649 | text += QStringLiteral(" (%1)").arg(value); |
| 650 | } |
| 651 | i += component_size; |
| 652 | text += QLatin1Char{' '}; |
| 653 | } |
| 654 | text += QLatin1Char{' '}; |
| 655 | }; |
| 656 | const auto process_simple_component = [&](const u32 size) { |
| 657 | for (u32 component_off = 0; component_off < size; component_off++) |
| 658 | { |
| 659 | text += QStringLiteral("%1").arg(vertex_data[i + component_off], 2, 16, QLatin1Char('0')); |
| 660 | } |
| 661 | i += size; |
| 662 | text += QLatin1Char{' '}; |
| 663 | text += QLatin1Char{' '}; |
| 664 | }; |
| 665 | |
| 666 | for (u32 vertex_num = 0; vertex_num < num_vertices; vertex_num++) |
| 667 | { |
| 668 | ASSERT(i == vertex_num * vertex_size); |
| 669 | |
| 670 | text += QLatin1Char{'\n'}; |
| 671 | if (vtx_desc.low.PosMatIdx) |
| 672 | process_simple_component(1); |
| 673 | for (auto texmtxidx : vtx_desc.low.TexMatIdx) |
| 674 | { |
| 675 | if (texmtxidx) |
| 676 | process_simple_component(1); |
| 677 | } |
| 678 | process_component(vtx_desc.low.Position, vtx_attr.g0.PosFormat, |
| 679 | vtx_attr.g0.PosElements == CoordComponentCount::XY ? 2 : 3); |
| 680 | const u32 normal_component_count = |
| 681 | vtx_desc.low.Normal == VertexComponentFormat::Direct ? 3 : 1; |
| 682 | const u32 normal_elements = vtx_attr.g0.NormalElements == NormalComponentCount::NTB ? 3 : 1; |
| 683 | process_component(vtx_desc.low.Normal, vtx_attr.g0.NormalFormat, |
| 684 | normal_component_count * normal_elements, |
| 685 | vtx_attr.g0.NormalIndex3 ? normal_elements : 1); |
| 686 | for (u32 c = 0; c < vtx_desc.low.Color.Size(); c++) |
| 687 | { |
| 688 | static constexpr Common::EnumMap<u32, ColorFormat::RGBA8888> component_sizes = { |
| 689 | 2, // RGB565 |
| 690 | 3, // RGB888 |
| 691 | 4, // RGB888x |
| 692 | 2, // RGBA4444 |
| 693 | 3, // RGBA6666 |
| 694 | 4, // RGBA8888 |
| 695 | }; |
| 696 | switch (vtx_desc.low.Color[c]) |
| 697 | { |
| 698 | case VertexComponentFormat::Index8: |
| 699 | process_simple_component(1); |
| 700 | break; |
| 701 | case VertexComponentFormat::Index16: |
| 702 | process_simple_component(2); |
| 703 | break; |
| 704 | case VertexComponentFormat::Direct: |
| 705 | process_simple_component(component_sizes[vtx_attr.GetColorFormat(c)]); |
| 706 | break; |
| 707 | case VertexComponentFormat::NotPresent: |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | for (u32 t = 0; t < vtx_desc.high.TexCoord.Size(); t++) |
| 712 | { |
| 713 | process_component(vtx_desc.high.TexCoord[t], vtx_attr.GetTexFormat(t), |
| 714 | vtx_attr.GetTexElements(t) == TexComponentCount::ST ? 2 : 1); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | OPCODE_CALLBACK(void OnDisplayList(u32 address, u32 size)) |
| 720 | { |
| 721 | text = QObject::tr("No description available"); |
| 722 | } |
| 723 | |
| 724 | OPCODE_CALLBACK(void OnNop(u32 count)) { text = QObject::tr("No description available"); } |
| 725 | OPCODE_CALLBACK(void OnUnknown(u8 opcode, const u8* data)) |
| 726 | { |
| 727 | text = QObject::tr("No description available"); |
| 728 | } |
| 729 | |
| 730 | OPCODE_CALLBACK(void OnCommand(const u8* data, u32 size)) {} |
| 731 | |
| 732 | OPCODE_CALLBACK(CPState& GetCPState()) { return m_cpmem; } |
| 733 | |
| 734 | QString text; |
| 735 | CPState m_cpmem; |
| 736 | }; |
| 737 | } // namespace |
| 738 | |
| 739 | void FIFOAnalyzer::UpdateDescription() |
| 740 | { |
| 741 | m_entry_detail_browser->clear(); |
| 742 | |
| 743 | if (!m_fifo_player.IsPlaying()) |
| 744 | return; |
| 745 | |
| 746 | const auto items = m_tree_widget->selectedItems(); |
| 747 | |
| 748 | if (items.isEmpty() || m_object_data_offsets.empty()) |
| 749 | return; |
| 750 | |
| 751 | if (items[0]->data(0, FRAME_ROLE).isNull() || items[0]->data(0, PART_START_ROLE).isNull()) |
| 752 | return; |
| 753 | |
| 754 | const u32 frame_nr = items[0]->data(0, FRAME_ROLE).toUInt(); |
| 755 | const u32 start_part_nr = items[0]->data(0, PART_START_ROLE).toUInt(); |
| 756 | const u32 end_part_nr = items[0]->data(0, PART_END_ROLE).toUInt(); |
| 757 | const u32 entry_nr = m_detail_list->currentRow(); |
| 758 | |
| 759 | const AnalyzedFrameInfo& frame_info = m_fifo_player.GetAnalyzedFrameInfo(frame_nr); |
| 760 | const FifoFrameInfo& fifo_frame = m_fifo_player.GetFile()->GetFrame(frame_nr); |
| 761 | |
| 762 | const u32 object_start = frame_info.parts[start_part_nr].m_start; |
| 763 | const u32 object_end = frame_info.parts[end_part_nr].m_end; |
| 764 | const u32 object_size = object_end - object_start; |
| 765 | const u32 entry_start = m_object_data_offsets[entry_nr]; |
| 766 | |
| 767 | auto callback = DescriptionCallback(frame_info.parts[end_part_nr].m_cpmem); |
| 768 | OpcodeDecoder::RunCommand(&fifo_frame.fifoData[object_start + entry_start], |
| 769 | object_size - entry_start, callback); |
| 770 | m_entry_detail_browser->setText(callback.text); |
| 771 | } |
| 772 | |
| 773 | void FIFOAnalyzer::OnDebugFontChanged(const QFont& font) |
| 774 | { |
| 775 | m_detail_list->setFont(font); |
| 776 | m_entry_detail_browser->setFont(font); |
| 777 | } |