graphics_cmdlists.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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(const 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(const u8* src, const Pica::DebugUtils::TextureInfo& info,
  36. QWidget* parent = nullptr)
  37. : QWidget(parent) {
  38. QLabel* image_widget = new QLabel;
  39. QPixmap image_pixmap = QPixmap::fromImage(LoadTexture(src, info));
  40. image_pixmap = image_pixmap.scaled(200, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  41. image_widget->setPixmap(image_pixmap);
  42. QVBoxLayout* layout = new QVBoxLayout;
  43. layout->addWidget(image_widget);
  44. setLayout(layout);
  45. }
  46. };
  47. } // Anonymous namespace
  48. GPUCommandListModel::GPUCommandListModel(QObject* parent) : QAbstractListModel(parent) {}
  49. int GPUCommandListModel::rowCount(const QModelIndex& parent) const {
  50. return static_cast<int>(pica_trace.writes.size());
  51. }
  52. int GPUCommandListModel::columnCount(const QModelIndex& parent) const {
  53. return 4;
  54. }
  55. QVariant GPUCommandListModel::data(const QModelIndex& index, int role) const {
  56. if (!index.isValid())
  57. return QVariant();
  58. const auto& write = pica_trace.writes[index.row()];
  59. if (role == Qt::DisplayRole) {
  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 texture_index;
  107. if (COMMAND_IN_RANGE(command_id, texture0)) {
  108. texture_index = 0;
  109. } else if (COMMAND_IN_RANGE(command_id, texture1)) {
  110. texture_index = 1;
  111. } else if (COMMAND_IN_RANGE(command_id, texture2)) {
  112. texture_index = 2;
  113. } else {
  114. UNREACHABLE_MSG("Unknown texture command");
  115. }
  116. const auto texture = Pica::g_state.regs.GetTextures()[texture_index];
  117. const auto config = texture.config;
  118. const auto format = texture.format;
  119. const auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(config, format);
  120. // TODO: Open a surface debugger
  121. }
  122. }
  123. void GPUCommandListWidget::SetCommandInfo(const QModelIndex& index) {
  124. QWidget* new_info_widget = nullptr;
  125. const unsigned int command_id =
  126. list_widget->model()->data(index, GPUCommandListModel::CommandIdRole).toUInt();
  127. if (COMMAND_IN_RANGE(command_id, texture0) || COMMAND_IN_RANGE(command_id, texture1) ||
  128. COMMAND_IN_RANGE(command_id, texture2)) {
  129. unsigned texture_index;
  130. if (COMMAND_IN_RANGE(command_id, texture0)) {
  131. texture_index = 0;
  132. } else if (COMMAND_IN_RANGE(command_id, texture1)) {
  133. texture_index = 1;
  134. } else {
  135. texture_index = 2;
  136. }
  137. const auto texture = Pica::g_state.regs.GetTextures()[texture_index];
  138. const auto config = texture.config;
  139. const auto format = texture.format;
  140. const auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(config, format);
  141. const u8* src = Memory::GetPhysicalPointer(config.GetPhysicalAddress());
  142. new_info_widget = new TextureInfoWidget(src, info);
  143. }
  144. if (command_info_widget) {
  145. delete command_info_widget;
  146. command_info_widget = nullptr;
  147. }
  148. if (new_info_widget) {
  149. widget()->layout()->addWidget(new_info_widget);
  150. command_info_widget = new_info_widget;
  151. }
  152. }
  153. #undef COMMAND_IN_RANGE
  154. GPUCommandListWidget::GPUCommandListWidget(QWidget* parent)
  155. : QDockWidget(tr("Pica Command List"), parent) {
  156. setObjectName("Pica Command List");
  157. GPUCommandListModel* model = new GPUCommandListModel(this);
  158. QWidget* main_widget = new QWidget;
  159. list_widget = new QTreeView;
  160. list_widget->setModel(model);
  161. list_widget->setFont(GetMonospaceFont());
  162. list_widget->setRootIsDecorated(false);
  163. list_widget->setUniformRowHeights(true);
  164. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  165. list_widget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
  166. #else
  167. list_widget->header()->setResizeMode(QHeaderView::ResizeToContents);
  168. #endif
  169. connect(list_widget->selectionModel(),
  170. SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), this,
  171. SLOT(SetCommandInfo(const QModelIndex&)));
  172. connect(list_widget, SIGNAL(doubleClicked(const QModelIndex&)), this,
  173. SLOT(OnCommandDoubleClicked(const QModelIndex&)));
  174. toggle_tracing = new QPushButton(tr("Start Tracing"));
  175. QPushButton* copy_all = new QPushButton(tr("Copy All"));
  176. connect(toggle_tracing, SIGNAL(clicked()), this, SLOT(OnToggleTracing()));
  177. connect(this, SIGNAL(TracingFinished(const Pica::DebugUtils::PicaTrace&)), model,
  178. SLOT(OnPicaTraceFinished(const Pica::DebugUtils::PicaTrace&)));
  179. connect(copy_all, SIGNAL(clicked()), this, SLOT(CopyAllToClipboard()));
  180. command_info_widget = nullptr;
  181. QVBoxLayout* main_layout = new QVBoxLayout;
  182. main_layout->addWidget(list_widget);
  183. {
  184. QHBoxLayout* sub_layout = new QHBoxLayout;
  185. sub_layout->addWidget(toggle_tracing);
  186. sub_layout->addWidget(copy_all);
  187. main_layout->addLayout(sub_layout);
  188. }
  189. main_widget->setLayout(main_layout);
  190. setWidget(main_widget);
  191. }
  192. void GPUCommandListWidget::OnToggleTracing() {
  193. if (!Pica::DebugUtils::IsPicaTracing()) {
  194. Pica::DebugUtils::StartPicaTracing();
  195. toggle_tracing->setText(tr("Finish Tracing"));
  196. } else {
  197. pica_trace = Pica::DebugUtils::FinishPicaTracing();
  198. emit TracingFinished(*pica_trace);
  199. toggle_tracing->setText(tr("Start Tracing"));
  200. }
  201. }
  202. void GPUCommandListWidget::CopyAllToClipboard() {
  203. QClipboard* clipboard = QApplication::clipboard();
  204. QString text;
  205. QAbstractItemModel* model = static_cast<QAbstractItemModel*>(list_widget->model());
  206. for (int row = 0; row < model->rowCount({}); ++row) {
  207. for (int col = 0; col < model->columnCount({}); ++col) {
  208. QModelIndex index = model->index(row, col);
  209. text += model->data(index).value<QString>();
  210. text += '\t';
  211. }
  212. text += '\n';
  213. }
  214. clipboard->setText(text);
  215. }