graphics_cmdlists.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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/debug_utils/debug_utils.h"
  21. QImage LoadTexture(u8* src, const Pica::DebugUtils::TextureInfo& info) {
  22. QImage decoded_image(info.width, info.height, QImage::Format_ARGB32);
  23. for (int y = 0; y < info.height; ++y) {
  24. for (int x = 0; x < info.width; ++x) {
  25. Math::Vec4<u8> color = Pica::DebugUtils::LookupTexture(src, x, y, info, true);
  26. decoded_image.setPixel(x, y, qRgba(color.r(), color.g(), color.b(), color.a()));
  27. }
  28. }
  29. return decoded_image;
  30. }
  31. class TextureInfoWidget : public QWidget {
  32. public:
  33. TextureInfoWidget(u8* src, const Pica::DebugUtils::TextureInfo& info, QWidget* parent = nullptr) : QWidget(parent) {
  34. QLabel* image_widget = new QLabel;
  35. QPixmap image_pixmap = QPixmap::fromImage(LoadTexture(src, info));
  36. image_pixmap = image_pixmap.scaled(200, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  37. image_widget->setPixmap(image_pixmap);
  38. QVBoxLayout* layout = new QVBoxLayout;
  39. layout->addWidget(image_widget);
  40. setLayout(layout);
  41. }
  42. };
  43. TextureInfoDockWidget::TextureInfoDockWidget(const Pica::DebugUtils::TextureInfo& info, QWidget* parent)
  44. : QDockWidget(tr("Texture 0x%1").arg(info.physical_address, 8, 16, QLatin1Char('0'))),
  45. info(info) {
  46. QWidget* main_widget = new QWidget;
  47. QLabel* image_widget = new QLabel;
  48. connect(this, SIGNAL(UpdatePixmap(const QPixmap&)), image_widget, SLOT(setPixmap(const QPixmap&)));
  49. CSpinBox* phys_address_spinbox = new CSpinBox;
  50. phys_address_spinbox->SetBase(16);
  51. phys_address_spinbox->SetRange(0, 0xFFFFFFFF);
  52. phys_address_spinbox->SetPrefix("0x");
  53. phys_address_spinbox->SetValue(info.physical_address);
  54. connect(phys_address_spinbox, SIGNAL(ValueChanged(qint64)), this, SLOT(OnAddressChanged(qint64)));
  55. QComboBox* format_choice = new QComboBox;
  56. format_choice->addItem(tr("RGBA8"));
  57. format_choice->addItem(tr("RGB8"));
  58. format_choice->addItem(tr("RGB5A1"));
  59. format_choice->addItem(tr("RGB565"));
  60. format_choice->addItem(tr("RGBA4"));
  61. format_choice->addItem(tr("IA8"));
  62. format_choice->addItem(tr("RG8"));
  63. format_choice->addItem(tr("I8"));
  64. format_choice->addItem(tr("A8"));
  65. format_choice->addItem(tr("IA4"));
  66. format_choice->addItem(tr("I4"));
  67. format_choice->addItem(tr("A4"));
  68. format_choice->addItem(tr("ETC1"));
  69. format_choice->addItem(tr("ETC1A4"));
  70. format_choice->setCurrentIndex(static_cast<int>(info.format));
  71. connect(format_choice, SIGNAL(currentIndexChanged(int)), this, SLOT(OnFormatChanged(int)));
  72. QSpinBox* width_spinbox = new QSpinBox;
  73. width_spinbox->setMaximum(65535);
  74. width_spinbox->setValue(info.width);
  75. connect(width_spinbox, SIGNAL(valueChanged(int)), this, SLOT(OnWidthChanged(int)));
  76. QSpinBox* height_spinbox = new QSpinBox;
  77. height_spinbox->setMaximum(65535);
  78. height_spinbox->setValue(info.height);
  79. connect(height_spinbox, SIGNAL(valueChanged(int)), this, SLOT(OnHeightChanged(int)));
  80. QSpinBox* stride_spinbox = new QSpinBox;
  81. stride_spinbox->setMaximum(65535 * 4);
  82. stride_spinbox->setValue(info.stride);
  83. connect(stride_spinbox, SIGNAL(valueChanged(int)), this, SLOT(OnStrideChanged(int)));
  84. QVBoxLayout* main_layout = new QVBoxLayout;
  85. main_layout->addWidget(image_widget);
  86. {
  87. QHBoxLayout* sub_layout = new QHBoxLayout;
  88. sub_layout->addWidget(new QLabel(tr("Source Address:")));
  89. sub_layout->addWidget(phys_address_spinbox);
  90. main_layout->addLayout(sub_layout);
  91. }
  92. {
  93. QHBoxLayout* sub_layout = new QHBoxLayout;
  94. sub_layout->addWidget(new QLabel(tr("Format")));
  95. sub_layout->addWidget(format_choice);
  96. main_layout->addLayout(sub_layout);
  97. }
  98. {
  99. QHBoxLayout* sub_layout = new QHBoxLayout;
  100. sub_layout->addWidget(new QLabel(tr("Width:")));
  101. sub_layout->addWidget(width_spinbox);
  102. sub_layout->addStretch();
  103. sub_layout->addWidget(new QLabel(tr("Height:")));
  104. sub_layout->addWidget(height_spinbox);
  105. sub_layout->addStretch();
  106. sub_layout->addWidget(new QLabel(tr("Stride:")));
  107. sub_layout->addWidget(stride_spinbox);
  108. main_layout->addLayout(sub_layout);
  109. }
  110. main_widget->setLayout(main_layout);
  111. emit UpdatePixmap(ReloadPixmap());
  112. setWidget(main_widget);
  113. }
  114. void TextureInfoDockWidget::OnAddressChanged(qint64 value) {
  115. info.physical_address = value;
  116. emit UpdatePixmap(ReloadPixmap());
  117. }
  118. void TextureInfoDockWidget::OnFormatChanged(int value) {
  119. info.format = static_cast<Pica::Regs::TextureFormat>(value);
  120. emit UpdatePixmap(ReloadPixmap());
  121. }
  122. void TextureInfoDockWidget::OnWidthChanged(int value) {
  123. info.width = value;
  124. emit UpdatePixmap(ReloadPixmap());
  125. }
  126. void TextureInfoDockWidget::OnHeightChanged(int value) {
  127. info.height = value;
  128. emit UpdatePixmap(ReloadPixmap());
  129. }
  130. void TextureInfoDockWidget::OnStrideChanged(int value) {
  131. info.stride = value;
  132. emit UpdatePixmap(ReloadPixmap());
  133. }
  134. QPixmap TextureInfoDockWidget::ReloadPixmap() const {
  135. u8* src = Memory::GetPhysicalPointer(info.physical_address);
  136. return QPixmap::fromImage(LoadTexture(src, info));
  137. }
  138. GPUCommandListModel::GPUCommandListModel(QObject* parent) : QAbstractListModel(parent) {
  139. }
  140. int GPUCommandListModel::rowCount(const QModelIndex& parent) const {
  141. return static_cast<int>(pica_trace.writes.size());
  142. }
  143. int GPUCommandListModel::columnCount(const QModelIndex& parent) const {
  144. return 4;
  145. }
  146. QVariant GPUCommandListModel::data(const QModelIndex& index, int role) const {
  147. if (!index.isValid())
  148. return QVariant();
  149. const auto& write = pica_trace.writes[index.row()];
  150. if (role == Qt::DisplayRole) {
  151. QString content;
  152. switch ( index.column() ) {
  153. case 0:
  154. return QString::fromLatin1(Pica::Regs::GetCommandName(write.cmd_id).c_str());
  155. case 1:
  156. return QString("%1").arg(write.cmd_id, 3, 16, QLatin1Char('0'));
  157. case 2:
  158. return QString("%1").arg(write.mask, 4, 2, QLatin1Char('0'));
  159. case 3:
  160. return QString("%1").arg(write.value, 8, 16, QLatin1Char('0'));
  161. }
  162. } else if (role == CommandIdRole) {
  163. return QVariant::fromValue<int>(write.cmd_id);
  164. }
  165. return QVariant();
  166. }
  167. QVariant GPUCommandListModel::headerData(int section, Qt::Orientation orientation, int role) const {
  168. switch(role) {
  169. case Qt::DisplayRole:
  170. {
  171. switch (section) {
  172. case 0:
  173. return tr("Command Name");
  174. case 1:
  175. return tr("Register");
  176. case 2:
  177. return tr("Mask");
  178. case 3:
  179. return tr("New Value");
  180. }
  181. break;
  182. }
  183. }
  184. return QVariant();
  185. }
  186. void GPUCommandListModel::OnPicaTraceFinished(const Pica::DebugUtils::PicaTrace& trace) {
  187. beginResetModel();
  188. pica_trace = trace;
  189. endResetModel();
  190. }
  191. #define COMMAND_IN_RANGE(cmd_id, reg_name) \
  192. (cmd_id >= PICA_REG_INDEX(reg_name) && \
  193. cmd_id < PICA_REG_INDEX(reg_name) + sizeof(decltype(Pica::g_state.regs.reg_name)) / 4)
  194. void GPUCommandListWidget::OnCommandDoubleClicked(const QModelIndex& index) {
  195. const unsigned int command_id = list_widget->model()->data(index, GPUCommandListModel::CommandIdRole).toUInt();
  196. if (COMMAND_IN_RANGE(command_id, texture0) ||
  197. COMMAND_IN_RANGE(command_id, texture1) ||
  198. COMMAND_IN_RANGE(command_id, texture2)) {
  199. unsigned index;
  200. if (COMMAND_IN_RANGE(command_id, texture0)) {
  201. index = 0;
  202. } else if (COMMAND_IN_RANGE(command_id, texture1)) {
  203. index = 1;
  204. } else {
  205. index = 2;
  206. }
  207. auto config = Pica::g_state.regs.GetTextures()[index].config;
  208. auto format = Pica::g_state.regs.GetTextures()[index].format;
  209. auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(config, format);
  210. // TODO: Instead, emit a signal here to be caught by the main window widget.
  211. auto main_window = static_cast<QMainWindow*>(parent());
  212. main_window->tabifyDockWidget(this, new TextureInfoDockWidget(info, main_window));
  213. }
  214. }
  215. void GPUCommandListWidget::SetCommandInfo(const QModelIndex& index) {
  216. QWidget* new_info_widget = nullptr;
  217. const unsigned int command_id = list_widget->model()->data(index, GPUCommandListModel::CommandIdRole).toUInt();
  218. if (COMMAND_IN_RANGE(command_id, texture0) ||
  219. COMMAND_IN_RANGE(command_id, texture1) ||
  220. COMMAND_IN_RANGE(command_id, texture2)) {
  221. unsigned index;
  222. if (COMMAND_IN_RANGE(command_id, texture0)) {
  223. index = 0;
  224. } else if (COMMAND_IN_RANGE(command_id, texture1)) {
  225. index = 1;
  226. } else {
  227. index = 2;
  228. }
  229. auto config = Pica::g_state.regs.GetTextures()[index].config;
  230. auto format = Pica::g_state.regs.GetTextures()[index].format;
  231. auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(config, format);
  232. u8* src = Memory::GetPhysicalPointer(config.GetPhysicalAddress());
  233. new_info_widget = new TextureInfoWidget(src, info);
  234. }
  235. if (command_info_widget) {
  236. delete command_info_widget;
  237. command_info_widget = nullptr;
  238. }
  239. if (new_info_widget) {
  240. widget()->layout()->addWidget(new_info_widget);
  241. command_info_widget = new_info_widget;
  242. }
  243. }
  244. #undef COMMAND_IN_RANGE
  245. GPUCommandListWidget::GPUCommandListWidget(QWidget* parent) : QDockWidget(tr("Pica Command List"), parent) {
  246. setObjectName("Pica Command List");
  247. GPUCommandListModel* model = new GPUCommandListModel(this);
  248. QWidget* main_widget = new QWidget;
  249. list_widget = new QTreeView;
  250. list_widget->setModel(model);
  251. list_widget->setFont(GetMonospaceFont());
  252. list_widget->setRootIsDecorated(false);
  253. list_widget->setUniformRowHeights(true);
  254. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  255. list_widget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
  256. #else
  257. list_widget->header()->setResizeMode(QHeaderView::ResizeToContents);
  258. #endif
  259. connect(list_widget->selectionModel(), SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)),
  260. this, SLOT(SetCommandInfo(const QModelIndex&)));
  261. connect(list_widget, SIGNAL(doubleClicked(const QModelIndex&)),
  262. this, SLOT(OnCommandDoubleClicked(const QModelIndex&)));
  263. toggle_tracing = new QPushButton(tr("Start Tracing"));
  264. QPushButton* copy_all = new QPushButton(tr("Copy All"));
  265. connect(toggle_tracing, SIGNAL(clicked()), this, SLOT(OnToggleTracing()));
  266. connect(this, SIGNAL(TracingFinished(const Pica::DebugUtils::PicaTrace&)),
  267. model, SLOT(OnPicaTraceFinished(const Pica::DebugUtils::PicaTrace&)));
  268. connect(copy_all, SIGNAL(clicked()), this, SLOT(CopyAllToClipboard()));
  269. command_info_widget = nullptr;
  270. QVBoxLayout* main_layout = new QVBoxLayout;
  271. main_layout->addWidget(list_widget);
  272. {
  273. QHBoxLayout* sub_layout = new QHBoxLayout;
  274. sub_layout->addWidget(toggle_tracing);
  275. sub_layout->addWidget(copy_all);
  276. main_layout->addLayout(sub_layout);
  277. }
  278. main_widget->setLayout(main_layout);
  279. setWidget(main_widget);
  280. }
  281. void GPUCommandListWidget::OnToggleTracing() {
  282. if (!Pica::DebugUtils::IsPicaTracing()) {
  283. Pica::DebugUtils::StartPicaTracing();
  284. toggle_tracing->setText(tr("Finish Tracing"));
  285. } else {
  286. pica_trace = Pica::DebugUtils::FinishPicaTracing();
  287. emit TracingFinished(*pica_trace);
  288. toggle_tracing->setText(tr("Start Tracing"));
  289. }
  290. }
  291. void GPUCommandListWidget::CopyAllToClipboard() {
  292. QClipboard* clipboard = QApplication::clipboard();
  293. QString text;
  294. QAbstractItemModel* model = static_cast<QAbstractItemModel*>(list_widget->model());
  295. for (int row = 0; row < model->rowCount({}); ++row) {
  296. for (int col = 0; col < model->columnCount({}); ++col) {
  297. QModelIndex index = model->index(row, col);
  298. text += model->data(index).value<QString>();
  299. text += '\t';
  300. }
  301. text += '\n';
  302. }
  303. clipboard->setText(text);
  304. }