summaryrefslogtreecommitdiff
path: root/src/citra_qt/debugger/graphics/graphics_surface.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/citra_qt/debugger/graphics/graphics_surface.cpp')
-rw-r--r--src/citra_qt/debugger/graphics/graphics_surface.cpp710
1 files changed, 710 insertions, 0 deletions
diff --git a/src/citra_qt/debugger/graphics/graphics_surface.cpp b/src/citra_qt/debugger/graphics/graphics_surface.cpp
new file mode 100644
index 000000000..4efd95d3c
--- /dev/null
+++ b/src/citra_qt/debugger/graphics/graphics_surface.cpp
@@ -0,0 +1,710 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <QBoxLayout>
6#include <QComboBox>
7#include <QDebug>
8#include <QFileDialog>
9#include <QLabel>
10#include <QMouseEvent>
11#include <QPushButton>
12#include <QScrollArea>
13#include <QSpinBox>
14#include "citra_qt/debugger/graphics/graphics_surface.h"
15#include "citra_qt/util/spinbox.h"
16#include "common/color.h"
17#include "core/hw/gpu.h"
18#include "core/memory.h"
19#include "video_core/pica.h"
20#include "video_core/pica_state.h"
21#include "video_core/utils.h"
22
23SurfacePicture::SurfacePicture(QWidget* parent, GraphicsSurfaceWidget* surface_widget_)
24 : QLabel(parent), surface_widget(surface_widget_) {}
25SurfacePicture::~SurfacePicture() {}
26
27void SurfacePicture::mousePressEvent(QMouseEvent* event) {
28 // Only do something while the left mouse button is held down
29 if (!(event->buttons() & Qt::LeftButton))
30 return;
31
32 if (pixmap() == nullptr)
33 return;
34
35 if (surface_widget)
36 surface_widget->Pick(event->x() * pixmap()->width() / width(),
37 event->y() * pixmap()->height() / height());
38}
39
40void SurfacePicture::mouseMoveEvent(QMouseEvent* event) {
41 // We also want to handle the event if the user moves the mouse while holding down the LMB
42 mousePressEvent(event);
43}
44
45GraphicsSurfaceWidget::GraphicsSurfaceWidget(std::shared_ptr<Pica::DebugContext> debug_context,
46 QWidget* parent)
47 : BreakPointObserverDock(debug_context, tr("Pica Surface Viewer"), parent),
48 surface_source(Source::ColorBuffer) {
49 setObjectName("PicaSurface");
50
51 surface_source_list = new QComboBox;
52 surface_source_list->addItem(tr("Color Buffer"));
53 surface_source_list->addItem(tr("Depth Buffer"));
54 surface_source_list->addItem(tr("Stencil Buffer"));
55 surface_source_list->addItem(tr("Texture 0"));
56 surface_source_list->addItem(tr("Texture 1"));
57 surface_source_list->addItem(tr("Texture 2"));
58 surface_source_list->addItem(tr("Custom"));
59 surface_source_list->setCurrentIndex(static_cast<int>(surface_source));
60
61 surface_address_control = new CSpinBox;
62 surface_address_control->SetBase(16);
63 surface_address_control->SetRange(0, 0xFFFFFFFF);
64 surface_address_control->SetPrefix("0x");
65
66 unsigned max_dimension = 16384; // TODO: Find actual maximum
67
68 surface_width_control = new QSpinBox;
69 surface_width_control->setRange(0, max_dimension);
70
71 surface_height_control = new QSpinBox;
72 surface_height_control->setRange(0, max_dimension);
73
74 surface_picker_x_control = new QSpinBox;
75 surface_picker_x_control->setRange(0, max_dimension - 1);
76
77 surface_picker_y_control = new QSpinBox;
78 surface_picker_y_control->setRange(0, max_dimension - 1);
79
80 surface_format_control = new QComboBox;
81
82 // Color formats sorted by Pica texture format index
83 surface_format_control->addItem(tr("RGBA8"));
84 surface_format_control->addItem(tr("RGB8"));
85 surface_format_control->addItem(tr("RGB5A1"));
86 surface_format_control->addItem(tr("RGB565"));
87 surface_format_control->addItem(tr("RGBA4"));
88 surface_format_control->addItem(tr("IA8"));
89 surface_format_control->addItem(tr("RG8"));
90 surface_format_control->addItem(tr("I8"));
91 surface_format_control->addItem(tr("A8"));
92 surface_format_control->addItem(tr("IA4"));
93 surface_format_control->addItem(tr("I4"));
94 surface_format_control->addItem(tr("A4"));
95 surface_format_control->addItem(tr("ETC1"));
96 surface_format_control->addItem(tr("ETC1A4"));
97 surface_format_control->addItem(tr("D16"));
98 surface_format_control->addItem(tr("D24"));
99 surface_format_control->addItem(tr("D24X8"));
100 surface_format_control->addItem(tr("X24S8"));
101 surface_format_control->addItem(tr("Unknown"));
102
103 surface_info_label = new QLabel();
104 surface_info_label->setWordWrap(true);
105
106 surface_picture_label = new SurfacePicture(0, this);
107 surface_picture_label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
108 surface_picture_label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
109 surface_picture_label->setScaledContents(false);
110
111 auto scroll_area = new QScrollArea();
112 scroll_area->setBackgroundRole(QPalette::Dark);
113 scroll_area->setWidgetResizable(false);
114 scroll_area->setWidget(surface_picture_label);
115
116 save_surface = new QPushButton(QIcon::fromTheme("document-save"), tr("Save"));
117
118 // Connections
119 connect(this, SIGNAL(Update()), this, SLOT(OnUpdate()));
120 connect(surface_source_list, SIGNAL(currentIndexChanged(int)), this,
121 SLOT(OnSurfaceSourceChanged(int)));
122 connect(surface_address_control, SIGNAL(ValueChanged(qint64)), this,
123 SLOT(OnSurfaceAddressChanged(qint64)));
124 connect(surface_width_control, SIGNAL(valueChanged(int)), this,
125 SLOT(OnSurfaceWidthChanged(int)));
126 connect(surface_height_control, SIGNAL(valueChanged(int)), this,
127 SLOT(OnSurfaceHeightChanged(int)));
128 connect(surface_format_control, SIGNAL(currentIndexChanged(int)), this,
129 SLOT(OnSurfaceFormatChanged(int)));
130 connect(surface_picker_x_control, SIGNAL(valueChanged(int)), this,
131 SLOT(OnSurfacePickerXChanged(int)));
132 connect(surface_picker_y_control, SIGNAL(valueChanged(int)), this,
133 SLOT(OnSurfacePickerYChanged(int)));
134 connect(save_surface, SIGNAL(clicked()), this, SLOT(SaveSurface()));
135
136 auto main_widget = new QWidget;
137 auto main_layout = new QVBoxLayout;
138 {
139 auto sub_layout = new QHBoxLayout;
140 sub_layout->addWidget(new QLabel(tr("Source:")));
141 sub_layout->addWidget(surface_source_list);
142 main_layout->addLayout(sub_layout);
143 }
144 {
145 auto sub_layout = new QHBoxLayout;
146 sub_layout->addWidget(new QLabel(tr("Physical Address:")));
147 sub_layout->addWidget(surface_address_control);
148 main_layout->addLayout(sub_layout);
149 }
150 {
151 auto sub_layout = new QHBoxLayout;
152 sub_layout->addWidget(new QLabel(tr("Width:")));
153 sub_layout->addWidget(surface_width_control);
154 main_layout->addLayout(sub_layout);
155 }
156 {
157 auto sub_layout = new QHBoxLayout;
158 sub_layout->addWidget(new QLabel(tr("Height:")));
159 sub_layout->addWidget(surface_height_control);
160 main_layout->addLayout(sub_layout);
161 }
162 {
163 auto sub_layout = new QHBoxLayout;
164 sub_layout->addWidget(new QLabel(tr("Format:")));
165 sub_layout->addWidget(surface_format_control);
166 main_layout->addLayout(sub_layout);
167 }
168 main_layout->addWidget(scroll_area);
169
170 auto info_layout = new QHBoxLayout;
171 {
172 auto xy_layout = new QVBoxLayout;
173 {
174 {
175 auto sub_layout = new QHBoxLayout;
176 sub_layout->addWidget(new QLabel(tr("X:")));
177 sub_layout->addWidget(surface_picker_x_control);
178 xy_layout->addLayout(sub_layout);
179 }
180 {
181 auto sub_layout = new QHBoxLayout;
182 sub_layout->addWidget(new QLabel(tr("Y:")));
183 sub_layout->addWidget(surface_picker_y_control);
184 xy_layout->addLayout(sub_layout);
185 }
186 }
187 info_layout->addLayout(xy_layout);
188 surface_info_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
189 info_layout->addWidget(surface_info_label);
190 }
191 main_layout->addLayout(info_layout);
192
193 main_layout->addWidget(save_surface);
194 main_widget->setLayout(main_layout);
195 setWidget(main_widget);
196
197 // Load current data - TODO: Make sure this works when emulation is not running
198 if (debug_context && debug_context->at_breakpoint) {
199 emit Update();
200 widget()->setEnabled(debug_context->at_breakpoint);
201 } else {
202 widget()->setEnabled(false);
203 }
204}
205
206void GraphicsSurfaceWidget::OnBreakPointHit(Pica::DebugContext::Event event, void* data) {
207 emit Update();
208 widget()->setEnabled(true);
209}
210
211void GraphicsSurfaceWidget::OnResumed() {
212 widget()->setEnabled(false);
213}
214
215void GraphicsSurfaceWidget::OnSurfaceSourceChanged(int new_value) {
216 surface_source = static_cast<Source>(new_value);
217 emit Update();
218}
219
220void GraphicsSurfaceWidget::OnSurfaceAddressChanged(qint64 new_value) {
221 if (surface_address != new_value) {
222 surface_address = static_cast<unsigned>(new_value);
223
224 surface_source_list->setCurrentIndex(static_cast<int>(Source::Custom));
225 emit Update();
226 }
227}
228
229void GraphicsSurfaceWidget::OnSurfaceWidthChanged(int new_value) {
230 if (surface_width != static_cast<unsigned>(new_value)) {
231 surface_width = static_cast<unsigned>(new_value);
232
233 surface_source_list->setCurrentIndex(static_cast<int>(Source::Custom));
234 emit Update();
235 }
236}
237
238void GraphicsSurfaceWidget::OnSurfaceHeightChanged(int new_value) {
239 if (surface_height != static_cast<unsigned>(new_value)) {
240 surface_height = static_cast<unsigned>(new_value);
241
242 surface_source_list->setCurrentIndex(static_cast<int>(Source::Custom));
243 emit Update();
244 }
245}
246
247void GraphicsSurfaceWidget::OnSurfaceFormatChanged(int new_value) {
248 if (surface_format != static_cast<Format>(new_value)) {
249 surface_format = static_cast<Format>(new_value);
250
251 surface_source_list->setCurrentIndex(static_cast<int>(Source::Custom));
252 emit Update();
253 }
254}
255
256void GraphicsSurfaceWidget::OnSurfacePickerXChanged(int new_value) {
257 if (surface_picker_x != new_value) {
258 surface_picker_x = new_value;
259 Pick(surface_picker_x, surface_picker_y);
260 }
261}
262
263void GraphicsSurfaceWidget::OnSurfacePickerYChanged(int new_value) {
264 if (surface_picker_y != new_value) {
265 surface_picker_y = new_value;
266 Pick(surface_picker_x, surface_picker_y);
267 }
268}
269
270void GraphicsSurfaceWidget::Pick(int x, int y) {
271 surface_picker_x_control->setValue(x);
272 surface_picker_y_control->setValue(y);
273
274 if (x < 0 || x >= surface_width || y < 0 || y >= 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
280 u8* buffer = Memory::GetPhysicalPointer(surface_address);
281 if (buffer == nullptr) {
282 surface_info_label->setText(tr("(unable to access pixel data)"));
283 surface_info_label->setAlignment(Qt::AlignCenter);
284 return;
285 }
286
287 unsigned nibbles_per_pixel = GraphicsSurfaceWidget::NibblesPerPixel(surface_format);
288 unsigned stride = nibbles_per_pixel * surface_width / 2;
289
290 unsigned bytes_per_pixel;
291 bool nibble_mode = (nibbles_per_pixel == 1);
292 if (nibble_mode) {
293 // As nibbles are contained in a byte we still need to access one byte per nibble
294 bytes_per_pixel = 1;
295 } else {
296 bytes_per_pixel = nibbles_per_pixel / 2;
297 }
298
299 const u32 coarse_y = y & ~7;
300 u32 offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * stride;
301 const u8* pixel = buffer + (nibble_mode ? (offset / 2) : offset);
302
303 auto GetText = [offset](Format format, const u8* pixel) {
304 switch (format) {
305 case Format::RGBA8: {
306 auto value = Color::DecodeRGBA8(pixel) / 255.0f;
307 return QString("Red: %1, Green: %2, Blue: %3, Alpha: %4")
308 .arg(QString::number(value.r(), 'f', 2))
309 .arg(QString::number(value.g(), 'f', 2))
310 .arg(QString::number(value.b(), 'f', 2))
311 .arg(QString::number(value.a(), 'f', 2));
312 }
313 case Format::RGB8: {
314 auto value = Color::DecodeRGB8(pixel) / 255.0f;
315 return QString("Red: %1, Green: %2, Blue: %3")
316 .arg(QString::number(value.r(), 'f', 2))
317 .arg(QString::number(value.g(), 'f', 2))
318 .arg(QString::number(value.b(), 'f', 2));
319 }
320 case Format::RGB5A1: {
321 auto value = Color::DecodeRGB5A1(pixel) / 255.0f;
322 return QString("Red: %1, Green: %2, Blue: %3, Alpha: %4")
323 .arg(QString::number(value.r(), 'f', 2))
324 .arg(QString::number(value.g(), 'f', 2))
325 .arg(QString::number(value.b(), 'f', 2))
326 .arg(QString::number(value.a(), 'f', 2));
327 }
328 case Format::RGB565: {
329 auto value = Color::DecodeRGB565(pixel) / 255.0f;
330 return QString("Red: %1, Green: %2, Blue: %3")
331 .arg(QString::number(value.r(), 'f', 2))
332 .arg(QString::number(value.g(), 'f', 2))
333 .arg(QString::number(value.b(), 'f', 2));
334 }
335 case Format::RGBA4: {
336 auto value = Color::DecodeRGBA4(pixel) / 255.0f;
337 return QString("Red: %1, Green: %2, Blue: %3, Alpha: %4")
338 .arg(QString::number(value.r(), 'f', 2))
339 .arg(QString::number(value.g(), 'f', 2))
340 .arg(QString::number(value.b(), 'f', 2))
341 .arg(QString::number(value.a(), 'f', 2));
342 }
343 case Format::IA8:
344 return QString("Index: %1, Alpha: %2").arg(pixel[0]).arg(pixel[1]);
345 case Format::RG8: {
346 auto value = Color::DecodeRG8(pixel) / 255.0f;
347 return QString("Red: %1, Green: %2")
348 .arg(QString::number(value.r(), 'f', 2))
349 .arg(QString::number(value.g(), 'f', 2));
350 }
351 case Format::I8:
352 return QString("Index: %1").arg(*pixel);
353 case Format::A8:
354 return QString("Alpha: %1").arg(QString::number(*pixel / 255.0f, 'f', 2));
355 case Format::IA4:
356 return QString("Index: %1, Alpha: %2").arg(*pixel & 0xF).arg((*pixel & 0xF0) >> 4);
357 case Format::I4: {
358 u8 i = (*pixel >> ((offset % 2) ? 4 : 0)) & 0xF;
359 return QString("Index: %1").arg(i);
360 }
361 case Format::A4: {
362 u8 a = (*pixel >> ((offset % 2) ? 4 : 0)) & 0xF;
363 return QString("Alpha: %1").arg(QString::number(a / 15.0f, 'f', 2));
364 }
365 case Format::ETC1:
366 case Format::ETC1A4:
367 // TODO: Display block information or channel values?
368 return QString("Compressed data");
369 case Format::D16: {
370 auto value = Color::DecodeD16(pixel);
371 return QString("Depth: %1").arg(QString::number(value / (float)0xFFFF, 'f', 4));
372 }
373 case Format::D24: {
374 auto value = Color::DecodeD24(pixel);
375 return QString("Depth: %1").arg(QString::number(value / (float)0xFFFFFF, 'f', 4));
376 }
377 case Format::D24X8:
378 case Format::X24S8: {
379 auto values = Color::DecodeD24S8(pixel);
380 return QString("Depth: %1, Stencil: %2")
381 .arg(QString::number(values[0] / (float)0xFFFFFF, 'f', 4))
382 .arg(values[1]);
383 }
384 case Format::Unknown:
385 return QString("Unknown format");
386 default:
387 return QString("Unhandled format");
388 }
389 return QString("");
390 };
391
392 QString nibbles = "";
393 for (unsigned i = 0; i < nibbles_per_pixel; i++) {
394 unsigned nibble_index = i;
395 if (nibble_mode) {
396 nibble_index += (offset % 2) ? 0 : 1;
397 }
398 u8 byte = pixel[nibble_index / 2];
399 u8 nibble = (byte >> ((nibble_index % 2) ? 0 : 4)) & 0xF;
400 nibbles.append(QString::number(nibble, 16).toUpper());
401 }
402
403 surface_info_label->setText(
404 QString("Raw: 0x%3\n(%4)").arg(nibbles).arg(GetText(surface_format, pixel)));
405 surface_info_label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
406}
407
408void GraphicsSurfaceWidget::OnUpdate() {
409 QPixmap pixmap;
410
411 switch (surface_source) {
412 case Source::ColorBuffer: {
413 // TODO: Store a reference to the registers in the debug context instead of accessing them
414 // directly...
415
416 const auto& framebuffer = Pica::g_state.regs.framebuffer;
417
418 surface_address = framebuffer.GetColorBufferPhysicalAddress();
419 surface_width = framebuffer.GetWidth();
420 surface_height = framebuffer.GetHeight();
421
422 switch (framebuffer.color_format) {
423 case Pica::Regs::ColorFormat::RGBA8:
424 surface_format = Format::RGBA8;
425 break;
426
427 case Pica::Regs::ColorFormat::RGB8:
428 surface_format = Format::RGB8;
429 break;
430
431 case Pica::Regs::ColorFormat::RGB5A1:
432 surface_format = Format::RGB5A1;
433 break;
434
435 case Pica::Regs::ColorFormat::RGB565:
436 surface_format = Format::RGB565;
437 break;
438
439 case Pica::Regs::ColorFormat::RGBA4:
440 surface_format = Format::RGBA4;
441 break;
442
443 default:
444 surface_format = Format::Unknown;
445 break;
446 }
447
448 break;
449 }
450
451 case Source::DepthBuffer: {
452 const auto& framebuffer = Pica::g_state.regs.framebuffer;
453
454 surface_address = framebuffer.GetDepthBufferPhysicalAddress();
455 surface_width = framebuffer.GetWidth();
456 surface_height = framebuffer.GetHeight();
457
458 switch (framebuffer.depth_format) {
459 case Pica::Regs::DepthFormat::D16:
460 surface_format = Format::D16;
461 break;
462
463 case Pica::Regs::DepthFormat::D24:
464 surface_format = Format::D24;
465 break;
466
467 case Pica::Regs::DepthFormat::D24S8:
468 surface_format = Format::D24X8;
469 break;
470
471 default:
472 surface_format = Format::Unknown;
473 break;
474 }
475
476 break;
477 }
478
479 case Source::StencilBuffer: {
480 const auto& framebuffer = Pica::g_state.regs.framebuffer;
481
482 surface_address = framebuffer.GetDepthBufferPhysicalAddress();
483 surface_width = framebuffer.GetWidth();
484 surface_height = framebuffer.GetHeight();
485
486 switch (framebuffer.depth_format) {
487 case Pica::Regs::DepthFormat::D24S8:
488 surface_format = Format::X24S8;
489 break;
490
491 default:
492 surface_format = Format::Unknown;
493 break;
494 }
495
496 break;
497 }
498
499 case Source::Texture0:
500 case Source::Texture1:
501 case Source::Texture2: {
502 unsigned texture_index;
503 if (surface_source == Source::Texture0)
504 texture_index = 0;
505 else if (surface_source == Source::Texture1)
506 texture_index = 1;
507 else if (surface_source == Source::Texture2)
508 texture_index = 2;
509 else {
510 qDebug() << "Unknown texture source " << static_cast<int>(surface_source);
511 break;
512 }
513
514 const auto texture = Pica::g_state.regs.GetTextures()[texture_index];
515 auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(texture.config, texture.format);
516
517 surface_address = info.physical_address;
518 surface_width = info.width;
519 surface_height = info.height;
520 surface_format = static_cast<Format>(info.format);
521
522 if (surface_format > Format::MaxTextureFormat) {
523 qDebug() << "Unknown texture format " << static_cast<int>(info.format);
524 }
525 break;
526 }
527
528 case Source::Custom: {
529 // Keep user-specified values
530 break;
531 }
532
533 default:
534 qDebug() << "Unknown surface source " << static_cast<int>(surface_source);
535 break;
536 }
537
538 surface_address_control->SetValue(surface_address);
539 surface_width_control->setValue(surface_width);
540 surface_height_control->setValue(surface_height);
541 surface_format_control->setCurrentIndex(static_cast<int>(surface_format));
542
543 // TODO: Implement a good way to visualize alpha components!
544
545 QImage decoded_image(surface_width, surface_height, QImage::Format_ARGB32);
546 u8* buffer = Memory::GetPhysicalPointer(surface_address);
547
548 if (buffer == nullptr) {
549 surface_picture_label->hide();
550 surface_info_label->setText(tr("(invalid surface address)"));
551 surface_info_label->setAlignment(Qt::AlignCenter);
552 surface_picker_x_control->setEnabled(false);
553 surface_picker_y_control->setEnabled(false);
554 save_surface->setEnabled(false);
555 return;
556 }
557
558 if (surface_format == Format::Unknown) {
559 surface_picture_label->hide();
560 surface_info_label->setText(tr("(unknown surface format)"));
561 surface_info_label->setAlignment(Qt::AlignCenter);
562 surface_picker_x_control->setEnabled(false);
563 surface_picker_y_control->setEnabled(false);
564 save_surface->setEnabled(false);
565 return;
566 }
567
568 surface_picture_label->show();
569
570 unsigned nibbles_per_pixel = GraphicsSurfaceWidget::NibblesPerPixel(surface_format);
571 unsigned stride = nibbles_per_pixel * surface_width / 2;
572
573 // We handle depth formats here because DebugUtils only supports TextureFormats
574 if (surface_format <= Format::MaxTextureFormat) {
575
576 // Generate a virtual texture
577 Pica::DebugUtils::TextureInfo info;
578 info.physical_address = surface_address;
579 info.width = surface_width;
580 info.height = surface_height;
581 info.format = static_cast<Pica::Regs::TextureFormat>(surface_format);
582 info.stride = stride;
583
584 for (unsigned int y = 0; y < surface_height; ++y) {
585 for (unsigned int x = 0; x < surface_width; ++x) {
586 Math::Vec4<u8> color = Pica::DebugUtils::LookupTexture(buffer, x, y, info, true);
587 decoded_image.setPixel(x, y, qRgba(color.r(), color.g(), color.b(), color.a()));
588 }
589 }
590
591 } else {
592
593 ASSERT_MSG(nibbles_per_pixel >= 2,
594 "Depth decoder only supports formats with at least one byte per pixel");
595 unsigned bytes_per_pixel = nibbles_per_pixel / 2;
596
597 for (unsigned int y = 0; y < surface_height; ++y) {
598 for (unsigned int x = 0; x < surface_width; ++x) {
599 const u32 coarse_y = y & ~7;
600 u32 offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * stride;
601 const u8* pixel = buffer + offset;
602 Math::Vec4<u8> color = {0, 0, 0, 0};
603
604 switch (surface_format) {
605 case Format::D16: {
606 u32 data = Color::DecodeD16(pixel);
607 color.r() = data & 0xFF;
608 color.g() = (data >> 8) & 0xFF;
609 break;
610 }
611 case Format::D24: {
612 u32 data = Color::DecodeD24(pixel);
613 color.r() = data & 0xFF;
614 color.g() = (data >> 8) & 0xFF;
615 color.b() = (data >> 16) & 0xFF;
616 break;
617 }
618 case Format::D24X8: {
619 Math::Vec2<u32> data = Color::DecodeD24S8(pixel);
620 color.r() = data.x & 0xFF;
621 color.g() = (data.x >> 8) & 0xFF;
622 color.b() = (data.x >> 16) & 0xFF;
623 break;
624 }
625 case Format::X24S8: {
626 Math::Vec2<u32> data = Color::DecodeD24S8(pixel);
627 color.r() = color.g() = color.b() = data.y;
628 break;
629 }
630 default:
631 qDebug() << "Unknown surface format " << static_cast<int>(surface_format);
632 break;
633 }
634
635 decoded_image.setPixel(x, y, qRgba(color.r(), color.g(), color.b(), 255));
636 }
637 }
638 }
639
640 pixmap = QPixmap::fromImage(decoded_image);
641 surface_picture_label->setPixmap(pixmap);
642 surface_picture_label->resize(pixmap.size());
643
644 // Update the info with pixel data
645 surface_picker_x_control->setEnabled(true);
646 surface_picker_y_control->setEnabled(true);
647 Pick(surface_picker_x, surface_picker_y);
648
649 // Enable saving the converted pixmap to file
650 save_surface->setEnabled(true);
651}
652
653void GraphicsSurfaceWidget::SaveSurface() {
654 QString png_filter = tr("Portable Network Graphic (*.png)");
655 QString bin_filter = tr("Binary data (*.bin)");
656
657 QString selectedFilter;
658 QString filename = QFileDialog::getSaveFileName(
659 this, tr("Save Surface"),
660 QString("texture-0x%1.png").arg(QString::number(surface_address, 16)),
661 QString("%1;;%2").arg(png_filter, bin_filter), &selectedFilter);
662
663 if (filename.isEmpty()) {
664 // If the user canceled the dialog, don't save anything.
665 return;
666 }
667
668 if (selectedFilter == png_filter) {
669 const QPixmap* pixmap = surface_picture_label->pixmap();
670 ASSERT_MSG(pixmap != nullptr, "No pixmap set");
671
672 QFile file(filename);
673 file.open(QIODevice::WriteOnly);
674 if (pixmap)
675 pixmap->save(&file, "PNG");
676 } else if (selectedFilter == bin_filter) {
677 const u8* buffer = Memory::GetPhysicalPointer(surface_address);
678 ASSERT_MSG(buffer != nullptr, "Memory not accessible");
679
680 QFile file(filename);
681 file.open(QIODevice::WriteOnly);
682 int size = surface_width * surface_height * NibblesPerPixel(surface_format) / 2;
683 QByteArray data(reinterpret_cast<const char*>(buffer), size);
684 file.write(data);
685 } else {
686 UNREACHABLE_MSG("Unhandled filter selected");
687 }
688}
689
690unsigned int GraphicsSurfaceWidget::NibblesPerPixel(GraphicsSurfaceWidget::Format format) {
691 if (format <= Format::MaxTextureFormat) {
692 return Pica::Regs::NibblesPerPixel(static_cast<Pica::Regs::TextureFormat>(format));
693 }
694
695 switch (format) {
696 case Format::D24X8:
697 case Format::X24S8:
698 return 4 * 2;
699 case Format::D24:
700 return 3 * 2;
701 case Format::D16:
702 return 2 * 2;
703 default:
704 UNREACHABLE_MSG("GraphicsSurfaceWidget::BytesPerPixel: this should not be reached as this "
705 "function should be given a format which is in "
706 "GraphicsSurfaceWidget::Format. Instead got %i",
707 static_cast<int>(format));
708 return 0;
709 }
710}