graphics_cmdlists.cpp 11 KB

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