graphics_cmdlists.cpp 8.3 KB

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