graphics_cmdlists.cpp 12 KB

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