graphics_cmdlists.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.h"
  16. #include "util/spinbox.h"
  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("RGB5A1"));
  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->addItem(tr("ETC1"));
  65. format_choice->addItem(tr("ETC1A4"));
  66. format_choice->setCurrentIndex(static_cast<int>(info.format));
  67. connect(format_choice, SIGNAL(currentIndexChanged(int)), this, SLOT(OnFormatChanged(int)));
  68. QSpinBox* width_spinbox = new QSpinBox;
  69. width_spinbox->setMaximum(65535);
  70. width_spinbox->setValue(info.width);
  71. connect(width_spinbox, SIGNAL(valueChanged(int)), this, SLOT(OnWidthChanged(int)));
  72. QSpinBox* height_spinbox = new QSpinBox;
  73. height_spinbox->setMaximum(65535);
  74. height_spinbox->setValue(info.height);
  75. connect(height_spinbox, SIGNAL(valueChanged(int)), this, SLOT(OnHeightChanged(int)));
  76. QSpinBox* stride_spinbox = new QSpinBox;
  77. stride_spinbox->setMaximum(65535 * 4);
  78. stride_spinbox->setValue(info.stride);
  79. connect(stride_spinbox, SIGNAL(valueChanged(int)), this, SLOT(OnStrideChanged(int)));
  80. QVBoxLayout* main_layout = new QVBoxLayout;
  81. main_layout->addWidget(image_widget);
  82. {
  83. QHBoxLayout* sub_layout = new QHBoxLayout;
  84. sub_layout->addWidget(new QLabel(tr("Source Address:")));
  85. sub_layout->addWidget(phys_address_spinbox);
  86. main_layout->addLayout(sub_layout);
  87. }
  88. {
  89. QHBoxLayout* sub_layout = new QHBoxLayout;
  90. sub_layout->addWidget(new QLabel(tr("Format")));
  91. sub_layout->addWidget(format_choice);
  92. main_layout->addLayout(sub_layout);
  93. }
  94. {
  95. QHBoxLayout* sub_layout = new QHBoxLayout;
  96. sub_layout->addWidget(new QLabel(tr("Width:")));
  97. sub_layout->addWidget(width_spinbox);
  98. sub_layout->addStretch();
  99. sub_layout->addWidget(new QLabel(tr("Height:")));
  100. sub_layout->addWidget(height_spinbox);
  101. sub_layout->addStretch();
  102. sub_layout->addWidget(new QLabel(tr("Stride:")));
  103. sub_layout->addWidget(stride_spinbox);
  104. main_layout->addLayout(sub_layout);
  105. }
  106. main_widget->setLayout(main_layout);
  107. emit UpdatePixmap(ReloadPixmap());
  108. setWidget(main_widget);
  109. }
  110. void TextureInfoDockWidget::OnAddressChanged(qint64 value) {
  111. info.physical_address = value;
  112. emit UpdatePixmap(ReloadPixmap());
  113. }
  114. void TextureInfoDockWidget::OnFormatChanged(int value) {
  115. info.format = static_cast<Pica::Regs::TextureFormat>(value);
  116. emit UpdatePixmap(ReloadPixmap());
  117. }
  118. void TextureInfoDockWidget::OnWidthChanged(int value) {
  119. info.width = value;
  120. emit UpdatePixmap(ReloadPixmap());
  121. }
  122. void TextureInfoDockWidget::OnHeightChanged(int value) {
  123. info.height = value;
  124. emit UpdatePixmap(ReloadPixmap());
  125. }
  126. void TextureInfoDockWidget::OnStrideChanged(int value) {
  127. info.stride = value;
  128. emit UpdatePixmap(ReloadPixmap());
  129. }
  130. QPixmap TextureInfoDockWidget::ReloadPixmap() const {
  131. u8* src = Memory::GetPhysicalPointer(info.physical_address);
  132. return QPixmap::fromImage(LoadTexture(src, info));
  133. }
  134. GPUCommandListModel::GPUCommandListModel(QObject* parent) : QAbstractListModel(parent) {
  135. }
  136. int GPUCommandListModel::rowCount(const QModelIndex& parent) const {
  137. return pica_trace.writes.size();
  138. }
  139. int GPUCommandListModel::columnCount(const QModelIndex& parent) const {
  140. return 2;
  141. }
  142. QVariant GPUCommandListModel::data(const QModelIndex& index, int role) const {
  143. if (!index.isValid())
  144. return QVariant();
  145. const auto& writes = pica_trace.writes;
  146. const Pica::CommandProcessor::CommandHeader cmd{writes[index.row()].Id()};
  147. const u32 val{writes[index.row()].Value()};
  148. if (role == Qt::DisplayRole) {
  149. QString content;
  150. if (index.column() == 0) {
  151. QString content = QString::fromLatin1(Pica::Regs::GetCommandName(cmd.cmd_id).c_str());
  152. content.append(" ");
  153. return content;
  154. } else if (index.column() == 1) {
  155. QString content = QString("%1 ").arg(cmd.hex, 8, 16, QLatin1Char('0'));
  156. content.append(QString("%1 ").arg(val, 8, 16, QLatin1Char('0')));
  157. return content;
  158. }
  159. } else if (role == CommandIdRole) {
  160. return QVariant::fromValue<int>(cmd.cmd_id.Value());
  161. }
  162. return QVariant();
  163. }
  164. QVariant GPUCommandListModel::headerData(int section, Qt::Orientation orientation, int role) const {
  165. switch(role) {
  166. case Qt::DisplayRole:
  167. {
  168. if (section == 0) {
  169. return tr("Command Name");
  170. } else if (section == 1) {
  171. return tr("Data");
  172. }
  173. break;
  174. }
  175. }
  176. return QVariant();
  177. }
  178. void GPUCommandListModel::OnPicaTraceFinished(const Pica::DebugUtils::PicaTrace& trace) {
  179. beginResetModel();
  180. pica_trace = trace;
  181. endResetModel();
  182. }
  183. #define COMMAND_IN_RANGE(cmd_id, reg_name) \
  184. (cmd_id >= PICA_REG_INDEX(reg_name) && \
  185. cmd_id < PICA_REG_INDEX(reg_name) + sizeof(decltype(Pica::registers.reg_name)) / 4)
  186. void GPUCommandListWidget::OnCommandDoubleClicked(const QModelIndex& index) {
  187. const unsigned int command_id = list_widget->model()->data(index, GPUCommandListModel::CommandIdRole).toUInt();
  188. if (COMMAND_IN_RANGE(command_id, texture0) ||
  189. COMMAND_IN_RANGE(command_id, texture1) ||
  190. COMMAND_IN_RANGE(command_id, texture2)) {
  191. unsigned index;
  192. if (COMMAND_IN_RANGE(command_id, texture0)) {
  193. index = 0;
  194. } else if (COMMAND_IN_RANGE(command_id, texture1)) {
  195. index = 1;
  196. } else {
  197. index = 2;
  198. }
  199. auto config = Pica::registers.GetTextures()[index].config;
  200. auto format = Pica::registers.GetTextures()[index].format;
  201. auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(config, format);
  202. // TODO: Instead, emit a signal here to be caught by the main window widget.
  203. auto main_window = static_cast<QMainWindow*>(parent());
  204. main_window->tabifyDockWidget(this, new TextureInfoDockWidget(info, main_window));
  205. }
  206. }
  207. void GPUCommandListWidget::SetCommandInfo(const QModelIndex& index) {
  208. QWidget* new_info_widget;
  209. const unsigned int command_id = list_widget->model()->data(index, GPUCommandListModel::CommandIdRole).toUInt();
  210. if (COMMAND_IN_RANGE(command_id, texture0) ||
  211. COMMAND_IN_RANGE(command_id, texture1) ||
  212. COMMAND_IN_RANGE(command_id, texture2)) {
  213. unsigned index;
  214. if (COMMAND_IN_RANGE(command_id, texture0)) {
  215. index = 0;
  216. } else if (COMMAND_IN_RANGE(command_id, texture1)) {
  217. index = 1;
  218. } else {
  219. index = 2;
  220. }
  221. auto config = Pica::registers.GetTextures()[index].config;
  222. auto format = Pica::registers.GetTextures()[index].format;
  223. auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(config, format);
  224. u8* src = Memory::GetPhysicalPointer(config.GetPhysicalAddress());
  225. new_info_widget = new TextureInfoWidget(src, info);
  226. } else {
  227. new_info_widget = new QWidget;
  228. }
  229. widget()->layout()->removeWidget(command_info_widget);
  230. delete command_info_widget;
  231. widget()->layout()->addWidget(new_info_widget);
  232. command_info_widget = new_info_widget;
  233. }
  234. #undef COMMAND_IN_RANGE
  235. GPUCommandListWidget::GPUCommandListWidget(QWidget* parent) : QDockWidget(tr("Pica Command List"), parent) {
  236. setObjectName("Pica Command List");
  237. GPUCommandListModel* model = new GPUCommandListModel(this);
  238. QWidget* main_widget = new QWidget;
  239. list_widget = new QTreeView;
  240. list_widget->setModel(model);
  241. list_widget->setFont(QFont("monospace"));
  242. list_widget->setRootIsDecorated(false);
  243. connect(list_widget->selectionModel(), SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)),
  244. this, SLOT(SetCommandInfo(const QModelIndex&)));
  245. connect(list_widget, SIGNAL(doubleClicked(const QModelIndex&)),
  246. this, SLOT(OnCommandDoubleClicked(const QModelIndex&)));
  247. toggle_tracing = new QPushButton(tr("Start Tracing"));
  248. connect(toggle_tracing, SIGNAL(clicked()), this, SLOT(OnToggleTracing()));
  249. connect(this, SIGNAL(TracingFinished(const Pica::DebugUtils::PicaTrace&)),
  250. model, SLOT(OnPicaTraceFinished(const Pica::DebugUtils::PicaTrace&)));
  251. command_info_widget = new QWidget;
  252. QVBoxLayout* main_layout = new QVBoxLayout;
  253. main_layout->addWidget(list_widget);
  254. main_layout->addWidget(toggle_tracing);
  255. main_layout->addWidget(command_info_widget);
  256. main_widget->setLayout(main_layout);
  257. setWidget(main_widget);
  258. }
  259. void GPUCommandListWidget::OnToggleTracing() {
  260. if (!Pica::DebugUtils::IsPicaTracing()) {
  261. Pica::DebugUtils::StartPicaTracing();
  262. toggle_tracing->setText(tr("Finish Tracing"));
  263. } else {
  264. pica_trace = Pica::DebugUtils::FinishPicaTracing();
  265. emit TracingFinished(*pica_trace);
  266. toggle_tracing->setText(tr("Start Tracing"));
  267. }
  268. }