graphics_cmdlists.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <QApplication>
  5. #include <QClipboard>
  6. #include <QComboBox>
  7. #include <QHeaderView>
  8. #include <QLabel>
  9. #include <QListView>
  10. #include <QMainWindow>
  11. #include <QPushButton>
  12. #include <QSpinBox>
  13. #include <QTreeView>
  14. #include <QVBoxLayout>
  15. #include "citra_qt/debugger/graphics_cmdlists.h"
  16. #include "citra_qt/util/spinbox.h"
  17. #include "citra_qt/util/util.h"
  18. #include "common/vector_math.h"
  19. #include "video_core/debug_utils/debug_utils.h"
  20. #include "video_core/pica.h"
  21. #include "video_core/pica_state.h"
  22. namespace {
  23. QImage LoadTexture(u8* src, const Pica::DebugUtils::TextureInfo& info) {
  24. QImage decoded_image(info.width, info.height, QImage::Format_ARGB32);
  25. for (int y = 0; y < info.height; ++y) {
  26. for (int x = 0; x < info.width; ++x) {
  27. Math::Vec4<u8> color = Pica::DebugUtils::LookupTexture(src, x, y, info, true);
  28. decoded_image.setPixel(x, y, qRgba(color.r(), color.g(), color.b(), color.a()));
  29. }
  30. }
  31. return decoded_image;
  32. }
  33. class TextureInfoWidget : public QWidget {
  34. public:
  35. TextureInfoWidget(u8* src, const Pica::DebugUtils::TextureInfo& info, QWidget* parent = nullptr)
  36. : QWidget(parent) {
  37. QLabel* image_widget = new QLabel;
  38. QPixmap image_pixmap = QPixmap::fromImage(LoadTexture(src, info));
  39. image_pixmap = image_pixmap.scaled(200, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  40. image_widget->setPixmap(image_pixmap);
  41. QVBoxLayout* layout = new QVBoxLayout;
  42. layout->addWidget(image_widget);
  43. setLayout(layout);
  44. }
  45. };
  46. } // Anonymous namespace
  47. GPUCommandListModel::GPUCommandListModel(QObject* parent) : QAbstractListModel(parent) {}
  48. int GPUCommandListModel::rowCount(const QModelIndex& parent) const {
  49. return static_cast<int>(pica_trace.writes.size());
  50. }
  51. int GPUCommandListModel::columnCount(const QModelIndex& parent) const {
  52. return 4;
  53. }
  54. QVariant GPUCommandListModel::data(const QModelIndex& index, int role) const {
  55. if (!index.isValid())
  56. return QVariant();
  57. const auto& write = pica_trace.writes[index.row()];
  58. if (role == Qt::DisplayRole) {
  59. QString content;
  60. switch (index.column()) {
  61. case 0:
  62. return QString::fromLatin1(Pica::Regs::GetCommandName(write.cmd_id).c_str());
  63. case 1:
  64. return QString("%1").arg(write.cmd_id, 3, 16, QLatin1Char('0'));
  65. case 2:
  66. return QString("%1").arg(write.mask, 4, 2, QLatin1Char('0'));
  67. case 3:
  68. return QString("%1").arg(write.value, 8, 16, QLatin1Char('0'));
  69. }
  70. } else if (role == CommandIdRole) {
  71. return QVariant::fromValue<int>(write.cmd_id);
  72. }
  73. return QVariant();
  74. }
  75. QVariant GPUCommandListModel::headerData(int section, Qt::Orientation orientation, int role) const {
  76. switch (role) {
  77. case Qt::DisplayRole: {
  78. switch (section) {
  79. case 0:
  80. return tr("Command Name");
  81. case 1:
  82. return tr("Register");
  83. case 2:
  84. return tr("Mask");
  85. case 3:
  86. return tr("New Value");
  87. }
  88. break;
  89. }
  90. }
  91. return QVariant();
  92. }
  93. void GPUCommandListModel::OnPicaTraceFinished(const Pica::DebugUtils::PicaTrace& trace) {
  94. beginResetModel();
  95. pica_trace = trace;
  96. endResetModel();
  97. }
  98. #define COMMAND_IN_RANGE(cmd_id, reg_name) \
  99. (cmd_id >= PICA_REG_INDEX(reg_name) && \
  100. cmd_id < PICA_REG_INDEX(reg_name) + sizeof(decltype(Pica::g_state.regs.reg_name)) / 4)
  101. void GPUCommandListWidget::OnCommandDoubleClicked(const QModelIndex& index) {
  102. const unsigned int command_id =
  103. list_widget->model()->data(index, GPUCommandListModel::CommandIdRole).toUInt();
  104. if (COMMAND_IN_RANGE(command_id, texture0) || COMMAND_IN_RANGE(command_id, texture1) ||
  105. COMMAND_IN_RANGE(command_id, texture2)) {
  106. unsigned index;
  107. if (COMMAND_IN_RANGE(command_id, texture0)) {
  108. index = 0;
  109. } else if (COMMAND_IN_RANGE(command_id, texture1)) {
  110. index = 1;
  111. } else if (COMMAND_IN_RANGE(command_id, texture2)) {
  112. index = 2;
  113. } else {
  114. UNREACHABLE_MSG("Unknown texture command");
  115. }
  116. auto config = Pica::g_state.regs.GetTextures()[index].config;
  117. auto format = Pica::g_state.regs.GetTextures()[index].format;
  118. auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(config, format);
  119. // TODO: Open a surface debugger
  120. }
  121. }
  122. void GPUCommandListWidget::SetCommandInfo(const QModelIndex& index) {
  123. QWidget* new_info_widget = nullptr;
  124. const unsigned int command_id =
  125. list_widget->model()->data(index, GPUCommandListModel::CommandIdRole).toUInt();
  126. if (COMMAND_IN_RANGE(command_id, texture0) || COMMAND_IN_RANGE(command_id, texture1) ||
  127. COMMAND_IN_RANGE(command_id, texture2)) {
  128. unsigned index;
  129. if (COMMAND_IN_RANGE(command_id, texture0)) {
  130. index = 0;
  131. } else if (COMMAND_IN_RANGE(command_id, texture1)) {
  132. index = 1;
  133. } else {
  134. index = 2;
  135. }
  136. auto config = Pica::g_state.regs.GetTextures()[index].config;
  137. auto format = Pica::g_state.regs.GetTextures()[index].format;
  138. auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(config, format);
  139. u8* src = Memory::GetPhysicalPointer(config.GetPhysicalAddress());
  140. new_info_widget = new TextureInfoWidget(src, info);
  141. }
  142. if (command_info_widget) {
  143. delete command_info_widget;
  144. command_info_widget = nullptr;
  145. }
  146. if (new_info_widget) {
  147. widget()->layout()->addWidget(new_info_widget);
  148. command_info_widget = new_info_widget;
  149. }
  150. }
  151. #undef COMMAND_IN_RANGE
  152. GPUCommandListWidget::GPUCommandListWidget(QWidget* parent)
  153. : QDockWidget(tr("Pica Command List"), parent) {
  154. setObjectName("Pica Command List");
  155. GPUCommandListModel* model = new GPUCommandListModel(this);
  156. QWidget* main_widget = new QWidget;
  157. list_widget = new QTreeView;
  158. list_widget->setModel(model);
  159. list_widget->setFont(GetMonospaceFont());
  160. list_widget->setRootIsDecorated(false);
  161. list_widget->setUniformRowHeights(true);
  162. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  163. list_widget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
  164. #else
  165. list_widget->header()->setResizeMode(QHeaderView::ResizeToContents);
  166. #endif
  167. connect(list_widget->selectionModel(),
  168. SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), this,
  169. SLOT(SetCommandInfo(const QModelIndex&)));
  170. connect(list_widget, SIGNAL(doubleClicked(const QModelIndex&)), this,
  171. SLOT(OnCommandDoubleClicked(const QModelIndex&)));
  172. toggle_tracing = new QPushButton(tr("Start Tracing"));
  173. QPushButton* copy_all = new QPushButton(tr("Copy All"));
  174. connect(toggle_tracing, SIGNAL(clicked()), this, SLOT(OnToggleTracing()));
  175. connect(this, SIGNAL(TracingFinished(const Pica::DebugUtils::PicaTrace&)), model,
  176. SLOT(OnPicaTraceFinished(const Pica::DebugUtils::PicaTrace&)));
  177. connect(copy_all, SIGNAL(clicked()), this, SLOT(CopyAllToClipboard()));
  178. command_info_widget = nullptr;
  179. QVBoxLayout* main_layout = new QVBoxLayout;
  180. main_layout->addWidget(list_widget);
  181. {
  182. QHBoxLayout* sub_layout = new QHBoxLayout;
  183. sub_layout->addWidget(toggle_tracing);
  184. sub_layout->addWidget(copy_all);
  185. main_layout->addLayout(sub_layout);
  186. }
  187. main_widget->setLayout(main_layout);
  188. setWidget(main_widget);
  189. }
  190. void GPUCommandListWidget::OnToggleTracing() {
  191. if (!Pica::DebugUtils::IsPicaTracing()) {
  192. Pica::DebugUtils::StartPicaTracing();
  193. toggle_tracing->setText(tr("Finish Tracing"));
  194. } else {
  195. pica_trace = Pica::DebugUtils::FinishPicaTracing();
  196. emit TracingFinished(*pica_trace);
  197. toggle_tracing->setText(tr("Start Tracing"));
  198. }
  199. }
  200. void GPUCommandListWidget::CopyAllToClipboard() {
  201. QClipboard* clipboard = QApplication::clipboard();
  202. QString text;
  203. QAbstractItemModel* model = static_cast<QAbstractItemModel*>(list_widget->model());
  204. for (int row = 0; row < model->rowCount({}); ++row) {
  205. for (int col = 0; col < model->columnCount({}); ++col) {
  206. QModelIndex index = model->index(row, col);
  207. text += model->data(index).value<QString>();
  208. text += '\t';
  209. }
  210. text += '\n';
  211. }
  212. clipboard->setText(text);
  213. }