graphics_surface.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <QBoxLayout>
  5. #include <QComboBox>
  6. #include <QDebug>
  7. #include <QFileDialog>
  8. #include <QLabel>
  9. #include <QMouseEvent>
  10. #include <QPushButton>
  11. #include <QScrollArea>
  12. #include <QSpinBox>
  13. #include "common/vector_math.h"
  14. #include "core/core.h"
  15. #include "core/memory.h"
  16. #include "video_core/engines/maxwell_3d.h"
  17. #include "video_core/gpu.h"
  18. #include "video_core/textures/decoders.h"
  19. #include "video_core/textures/texture.h"
  20. #include "yuzu/debugger/graphics/graphics_surface.h"
  21. #include "yuzu/util/spinbox.h"
  22. static Tegra::Texture::TextureFormat ConvertToTextureFormat(
  23. Tegra::RenderTargetFormat render_target_format) {
  24. switch (render_target_format) {
  25. case Tegra::RenderTargetFormat::RGBA8_UNORM:
  26. return Tegra::Texture::TextureFormat::A8R8G8B8;
  27. case Tegra::RenderTargetFormat::RGB10_A2_UNORM:
  28. return Tegra::Texture::TextureFormat::A2B10G10R10;
  29. default:
  30. UNIMPLEMENTED_MSG("Unimplemented RT format");
  31. }
  32. }
  33. SurfacePicture::SurfacePicture(QWidget* parent, GraphicsSurfaceWidget* surface_widget_)
  34. : QLabel(parent), surface_widget(surface_widget_) {}
  35. SurfacePicture::~SurfacePicture() = default;
  36. void SurfacePicture::mousePressEvent(QMouseEvent* event) {
  37. // Only do something while the left mouse button is held down
  38. if (!(event->buttons() & Qt::LeftButton))
  39. return;
  40. if (pixmap() == nullptr)
  41. return;
  42. if (surface_widget)
  43. surface_widget->Pick(event->x() * pixmap()->width() / width(),
  44. event->y() * pixmap()->height() / height());
  45. }
  46. void SurfacePicture::mouseMoveEvent(QMouseEvent* event) {
  47. // We also want to handle the event if the user moves the mouse while holding down the LMB
  48. mousePressEvent(event);
  49. }
  50. GraphicsSurfaceWidget::GraphicsSurfaceWidget(std::shared_ptr<Tegra::DebugContext> debug_context,
  51. QWidget* parent)
  52. : BreakPointObserverDock(debug_context, tr("Maxwell Surface Viewer"), parent),
  53. surface_source(Source::RenderTarget0) {
  54. setObjectName("MaxwellSurface");
  55. surface_source_list = new QComboBox;
  56. surface_source_list->addItem(tr("Render Target 0"));
  57. surface_source_list->addItem(tr("Render Target 1"));
  58. surface_source_list->addItem(tr("Render Target 2"));
  59. surface_source_list->addItem(tr("Render Target 3"));
  60. surface_source_list->addItem(tr("Render Target 4"));
  61. surface_source_list->addItem(tr("Render Target 5"));
  62. surface_source_list->addItem(tr("Render Target 6"));
  63. surface_source_list->addItem(tr("Render Target 7"));
  64. surface_source_list->addItem(tr("Z Buffer"));
  65. surface_source_list->addItem(tr("Custom"));
  66. surface_source_list->setCurrentIndex(static_cast<int>(surface_source));
  67. surface_address_control = new CSpinBox;
  68. surface_address_control->SetBase(16);
  69. surface_address_control->SetRange(0, 0x7FFFFFFFFFFFFFFF);
  70. surface_address_control->SetPrefix("0x");
  71. unsigned max_dimension = 16384; // TODO: Find actual maximum
  72. surface_width_control = new QSpinBox;
  73. surface_width_control->setRange(0, max_dimension);
  74. surface_height_control = new QSpinBox;
  75. surface_height_control->setRange(0, max_dimension);
  76. surface_picker_x_control = new QSpinBox;
  77. surface_picker_x_control->setRange(0, max_dimension - 1);
  78. surface_picker_y_control = new QSpinBox;
  79. surface_picker_y_control->setRange(0, max_dimension - 1);
  80. surface_format_control = new QComboBox;
  81. // Color formats sorted by Maxwell texture format index
  82. surface_format_control->addItem(tr("None"));
  83. surface_format_control->addItem(tr("Unknown"));
  84. surface_format_control->addItem(tr("Unknown"));
  85. surface_format_control->addItem(tr("Unknown"));
  86. surface_format_control->addItem(tr("Unknown"));
  87. surface_format_control->addItem(tr("Unknown"));
  88. surface_format_control->addItem(tr("Unknown"));
  89. surface_format_control->addItem(tr("Unknown"));
  90. surface_format_control->addItem(tr("A8R8G8B8"));
  91. surface_format_control->addItem(tr("Unknown"));
  92. surface_format_control->addItem(tr("Unknown"));
  93. surface_format_control->addItem(tr("Unknown"));
  94. surface_format_control->addItem(tr("Unknown"));
  95. surface_format_control->addItem(tr("Unknown"));
  96. surface_format_control->addItem(tr("Unknown"));
  97. surface_format_control->addItem(tr("Unknown"));
  98. surface_format_control->addItem(tr("Unknown"));
  99. surface_format_control->addItem(tr("Unknown"));
  100. surface_format_control->addItem(tr("Unknown"));
  101. surface_format_control->addItem(tr("Unknown"));
  102. surface_format_control->addItem(tr("Unknown"));
  103. surface_format_control->addItem(tr("Unknown"));
  104. surface_format_control->addItem(tr("Unknown"));
  105. surface_format_control->addItem(tr("Unknown"));
  106. surface_format_control->addItem(tr("Unknown"));
  107. surface_format_control->addItem(tr("Unknown"));
  108. surface_format_control->addItem(tr("Unknown"));
  109. surface_format_control->addItem(tr("Unknown"));
  110. surface_format_control->addItem(tr("Unknown"));
  111. surface_format_control->addItem(tr("Unknown"));
  112. surface_format_control->addItem(tr("Unknown"));
  113. surface_format_control->addItem(tr("Unknown"));
  114. surface_format_control->addItem(tr("Unknown"));
  115. surface_format_control->addItem(tr("Unknown"));
  116. surface_format_control->addItem(tr("Unknown"));
  117. surface_format_control->addItem(tr("Unknown"));
  118. surface_format_control->addItem(tr("DXT1"));
  119. surface_format_control->addItem(tr("DXT23"));
  120. surface_format_control->addItem(tr("DXT45"));
  121. surface_format_control->addItem(tr("DXN1"));
  122. surface_format_control->addItem(tr("DXN2"));
  123. surface_info_label = new QLabel();
  124. surface_info_label->setWordWrap(true);
  125. surface_picture_label = new SurfacePicture(0, this);
  126. surface_picture_label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  127. surface_picture_label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
  128. surface_picture_label->setScaledContents(false);
  129. auto scroll_area = new QScrollArea();
  130. scroll_area->setBackgroundRole(QPalette::Dark);
  131. scroll_area->setWidgetResizable(false);
  132. scroll_area->setWidget(surface_picture_label);
  133. save_surface = new QPushButton(QIcon::fromTheme("document-save"), tr("Save"));
  134. // Connections
  135. connect(this, &GraphicsSurfaceWidget::Update, this, &GraphicsSurfaceWidget::OnUpdate);
  136. connect(surface_source_list,
  137. static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
  138. &GraphicsSurfaceWidget::OnSurfaceSourceChanged);
  139. connect(surface_address_control, &CSpinBox::ValueChanged, this,
  140. &GraphicsSurfaceWidget::OnSurfaceAddressChanged);
  141. connect(surface_width_control, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
  142. this, &GraphicsSurfaceWidget::OnSurfaceWidthChanged);
  143. connect(surface_height_control, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
  144. this, &GraphicsSurfaceWidget::OnSurfaceHeightChanged);
  145. connect(surface_format_control,
  146. static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
  147. &GraphicsSurfaceWidget::OnSurfaceFormatChanged);
  148. connect(surface_picker_x_control, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
  149. this, &GraphicsSurfaceWidget::OnSurfacePickerXChanged);
  150. connect(surface_picker_y_control, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
  151. this, &GraphicsSurfaceWidget::OnSurfacePickerYChanged);
  152. connect(save_surface, &QPushButton::clicked, this, &GraphicsSurfaceWidget::SaveSurface);
  153. auto main_widget = new QWidget;
  154. auto main_layout = new QVBoxLayout;
  155. {
  156. auto sub_layout = new QHBoxLayout;
  157. sub_layout->addWidget(new QLabel(tr("Source:")));
  158. sub_layout->addWidget(surface_source_list);
  159. main_layout->addLayout(sub_layout);
  160. }
  161. {
  162. auto sub_layout = new QHBoxLayout;
  163. sub_layout->addWidget(new QLabel(tr("GPU Address:")));
  164. sub_layout->addWidget(surface_address_control);
  165. main_layout->addLayout(sub_layout);
  166. }
  167. {
  168. auto sub_layout = new QHBoxLayout;
  169. sub_layout->addWidget(new QLabel(tr("Width:")));
  170. sub_layout->addWidget(surface_width_control);
  171. main_layout->addLayout(sub_layout);
  172. }
  173. {
  174. auto sub_layout = new QHBoxLayout;
  175. sub_layout->addWidget(new QLabel(tr("Height:")));
  176. sub_layout->addWidget(surface_height_control);
  177. main_layout->addLayout(sub_layout);
  178. }
  179. {
  180. auto sub_layout = new QHBoxLayout;
  181. sub_layout->addWidget(new QLabel(tr("Format:")));
  182. sub_layout->addWidget(surface_format_control);
  183. main_layout->addLayout(sub_layout);
  184. }
  185. main_layout->addWidget(scroll_area);
  186. auto info_layout = new QHBoxLayout;
  187. {
  188. auto xy_layout = new QVBoxLayout;
  189. {
  190. {
  191. auto sub_layout = new QHBoxLayout;
  192. sub_layout->addWidget(new QLabel(tr("X:")));
  193. sub_layout->addWidget(surface_picker_x_control);
  194. xy_layout->addLayout(sub_layout);
  195. }
  196. {
  197. auto sub_layout = new QHBoxLayout;
  198. sub_layout->addWidget(new QLabel(tr("Y:")));
  199. sub_layout->addWidget(surface_picker_y_control);
  200. xy_layout->addLayout(sub_layout);
  201. }
  202. }
  203. info_layout->addLayout(xy_layout);
  204. surface_info_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
  205. info_layout->addWidget(surface_info_label);
  206. }
  207. main_layout->addLayout(info_layout);
  208. main_layout->addWidget(save_surface);
  209. main_widget->setLayout(main_layout);
  210. setWidget(main_widget);
  211. // Load current data - TODO: Make sure this works when emulation is not running
  212. if (debug_context && debug_context->at_breakpoint) {
  213. emit Update();
  214. widget()->setEnabled(debug_context->at_breakpoint);
  215. } else {
  216. widget()->setEnabled(false);
  217. }
  218. }
  219. void GraphicsSurfaceWidget::OnBreakPointHit(Tegra::DebugContext::Event event, void* data) {
  220. emit Update();
  221. widget()->setEnabled(true);
  222. }
  223. void GraphicsSurfaceWidget::OnResumed() {
  224. widget()->setEnabled(false);
  225. }
  226. void GraphicsSurfaceWidget::OnSurfaceSourceChanged(int new_value) {
  227. surface_source = static_cast<Source>(new_value);
  228. emit Update();
  229. }
  230. void GraphicsSurfaceWidget::OnSurfaceAddressChanged(qint64 new_value) {
  231. if (surface_address != new_value) {
  232. surface_address = static_cast<Tegra::GPUVAddr>(new_value);
  233. surface_source_list->setCurrentIndex(static_cast<int>(Source::Custom));
  234. emit Update();
  235. }
  236. }
  237. void GraphicsSurfaceWidget::OnSurfaceWidthChanged(int new_value) {
  238. if (surface_width != static_cast<unsigned>(new_value)) {
  239. surface_width = static_cast<unsigned>(new_value);
  240. surface_source_list->setCurrentIndex(static_cast<int>(Source::Custom));
  241. emit Update();
  242. }
  243. }
  244. void GraphicsSurfaceWidget::OnSurfaceHeightChanged(int new_value) {
  245. if (surface_height != static_cast<unsigned>(new_value)) {
  246. surface_height = static_cast<unsigned>(new_value);
  247. surface_source_list->setCurrentIndex(static_cast<int>(Source::Custom));
  248. emit Update();
  249. }
  250. }
  251. void GraphicsSurfaceWidget::OnSurfaceFormatChanged(int new_value) {
  252. if (surface_format != static_cast<Tegra::Texture::TextureFormat>(new_value)) {
  253. surface_format = static_cast<Tegra::Texture::TextureFormat>(new_value);
  254. surface_source_list->setCurrentIndex(static_cast<int>(Source::Custom));
  255. emit Update();
  256. }
  257. }
  258. void GraphicsSurfaceWidget::OnSurfacePickerXChanged(int new_value) {
  259. if (surface_picker_x != new_value) {
  260. surface_picker_x = new_value;
  261. Pick(surface_picker_x, surface_picker_y);
  262. }
  263. }
  264. void GraphicsSurfaceWidget::OnSurfacePickerYChanged(int new_value) {
  265. if (surface_picker_y != new_value) {
  266. surface_picker_y = new_value;
  267. Pick(surface_picker_x, surface_picker_y);
  268. }
  269. }
  270. void GraphicsSurfaceWidget::Pick(int x, int y) {
  271. surface_picker_x_control->setValue(x);
  272. surface_picker_y_control->setValue(y);
  273. if (x < 0 || x >= static_cast<int>(surface_width) || y < 0 ||
  274. y >= static_cast<int>(surface_height)) {
  275. surface_info_label->setText(tr("Pixel out of bounds"));
  276. surface_info_label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  277. return;
  278. }
  279. surface_info_label->setText(QString("Raw: <Unimplemented>\n(%1)").arg("<Unimplemented>"));
  280. surface_info_label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  281. }
  282. void GraphicsSurfaceWidget::OnUpdate() {
  283. auto& gpu = Core::System::GetInstance().GPU();
  284. QPixmap pixmap;
  285. switch (surface_source) {
  286. case Source::RenderTarget0:
  287. case Source::RenderTarget1:
  288. case Source::RenderTarget2:
  289. case Source::RenderTarget3:
  290. case Source::RenderTarget4:
  291. case Source::RenderTarget5:
  292. case Source::RenderTarget6:
  293. case Source::RenderTarget7: {
  294. // TODO: Store a reference to the registers in the debug context instead of accessing them
  295. // directly...
  296. const auto& registers = gpu.Maxwell3D().regs;
  297. const auto& rt = registers.rt[static_cast<std::size_t>(surface_source) -
  298. static_cast<std::size_t>(Source::RenderTarget0)];
  299. surface_address = rt.Address();
  300. surface_width = rt.width;
  301. surface_height = rt.height;
  302. if (rt.format != Tegra::RenderTargetFormat::NONE) {
  303. surface_format = ConvertToTextureFormat(rt.format);
  304. }
  305. break;
  306. }
  307. case Source::Custom: {
  308. // Keep user-specified values
  309. break;
  310. }
  311. default:
  312. qDebug() << "Unknown surface source " << static_cast<int>(surface_source);
  313. break;
  314. }
  315. surface_address_control->SetValue(surface_address);
  316. surface_width_control->setValue(surface_width);
  317. surface_height_control->setValue(surface_height);
  318. surface_format_control->setCurrentIndex(static_cast<int>(surface_format));
  319. if (surface_address == 0) {
  320. surface_picture_label->hide();
  321. surface_info_label->setText(tr("(invalid surface address)"));
  322. surface_info_label->setAlignment(Qt::AlignCenter);
  323. surface_picker_x_control->setEnabled(false);
  324. surface_picker_y_control->setEnabled(false);
  325. save_surface->setEnabled(false);
  326. return;
  327. }
  328. // TODO: Implement a good way to visualize alpha components!
  329. QImage decoded_image(surface_width, surface_height, QImage::Format_ARGB32);
  330. std::optional<VAddr> address = gpu.MemoryManager().GpuToCpuAddress(surface_address);
  331. // TODO(bunnei): Will not work with BCn formats that swizzle 4x4 tiles.
  332. // Needs to be fixed if we plan to use this feature more, otherwise we may remove it.
  333. auto unswizzled_data =
  334. Tegra::Texture::UnswizzleTexture(*address, 1, Tegra::Texture::BytesPerPixel(surface_format),
  335. surface_width, surface_height, 1U);
  336. auto texture_data = Tegra::Texture::DecodeTexture(unswizzled_data, surface_format,
  337. surface_width, surface_height);
  338. surface_picture_label->show();
  339. for (unsigned int y = 0; y < surface_height; ++y) {
  340. for (unsigned int x = 0; x < surface_width; ++x) {
  341. Math::Vec4<u8> color;
  342. color[0] = texture_data[x + y * surface_width + 0];
  343. color[1] = texture_data[x + y * surface_width + 1];
  344. color[2] = texture_data[x + y * surface_width + 2];
  345. color[3] = texture_data[x + y * surface_width + 3];
  346. decoded_image.setPixel(x, y, qRgba(color.r(), color.g(), color.b(), color.a()));
  347. }
  348. }
  349. pixmap = QPixmap::fromImage(decoded_image);
  350. surface_picture_label->setPixmap(pixmap);
  351. surface_picture_label->resize(pixmap.size());
  352. // Update the info with pixel data
  353. surface_picker_x_control->setEnabled(true);
  354. surface_picker_y_control->setEnabled(true);
  355. Pick(surface_picker_x, surface_picker_y);
  356. // Enable saving the converted pixmap to file
  357. save_surface->setEnabled(true);
  358. }
  359. void GraphicsSurfaceWidget::SaveSurface() {
  360. QString png_filter = tr("Portable Network Graphic (*.png)");
  361. QString bin_filter = tr("Binary data (*.bin)");
  362. QString selectedFilter;
  363. QString filename = QFileDialog::getSaveFileName(
  364. this, tr("Save Surface"),
  365. QString("texture-0x%1.png").arg(QString::number(surface_address, 16)),
  366. QString("%1;;%2").arg(png_filter, bin_filter), &selectedFilter);
  367. if (filename.isEmpty()) {
  368. // If the user canceled the dialog, don't save anything.
  369. return;
  370. }
  371. if (selectedFilter == png_filter) {
  372. const QPixmap* pixmap = surface_picture_label->pixmap();
  373. ASSERT_MSG(pixmap != nullptr, "No pixmap set");
  374. QFile file(filename);
  375. file.open(QIODevice::WriteOnly);
  376. if (pixmap)
  377. pixmap->save(&file, "PNG");
  378. } else if (selectedFilter == bin_filter) {
  379. auto& gpu = Core::System::GetInstance().GPU();
  380. std::optional<VAddr> address = gpu.MemoryManager().GpuToCpuAddress(surface_address);
  381. const u8* buffer = Memory::GetPointer(*address);
  382. ASSERT_MSG(buffer != nullptr, "Memory not accessible");
  383. QFile file(filename);
  384. file.open(QIODevice::WriteOnly);
  385. int size = surface_width * surface_height * Tegra::Texture::BytesPerPixel(surface_format);
  386. QByteArray data(reinterpret_cast<const char*>(buffer), size);
  387. file.write(data);
  388. } else {
  389. UNREACHABLE_MSG("Unhandled filter selected");
  390. }
  391. }