summaryrefslogtreecommitdiff
path: root/src/citra_qt/debugger/graphics_tracing.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/citra_qt/debugger/graphics_tracing.cpp')
-rw-r--r--src/citra_qt/debugger/graphics_tracing.cpp170
1 files changed, 170 insertions, 0 deletions
diff --git a/src/citra_qt/debugger/graphics_tracing.cpp b/src/citra_qt/debugger/graphics_tracing.cpp
new file mode 100644
index 000000000..3f20f149d
--- /dev/null
+++ b/src/citra_qt/debugger/graphics_tracing.cpp
@@ -0,0 +1,170 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <memory>
6
7#include <QBoxLayout>
8#include <QComboBox>
9#include <QFileDialog>
10#include <QLabel>
11#include <QMessageBox>
12#include <QPushButton>
13#include <QSpinBox>
14
15#include <boost/range/algorithm/copy.hpp>
16
17#include "core/hw/gpu.h"
18#include "core/hw/lcd.h"
19
20#include "video_core/pica.h"
21
22#include "nihstro/float24.h"
23
24#include "graphics_tracing.h"
25
26GraphicsTracingWidget::GraphicsTracingWidget(std::shared_ptr<Pica::DebugContext> debug_context,
27 QWidget* parent)
28 : BreakPointObserverDock(debug_context, tr("CiTrace Recorder"), parent) {
29
30 setObjectName("CiTracing");
31
32 QPushButton* start_recording = new QPushButton(tr("Start Recording"));
33 QPushButton* stop_recording = new QPushButton(QIcon::fromTheme("document-save"), tr("Stop and Save"));
34 QPushButton* abort_recording = new QPushButton(tr("Abort Recording"));
35
36 connect(this, SIGNAL(SetStartTracingButtonEnabled(bool)), start_recording, SLOT(setVisible(bool)));
37 connect(this, SIGNAL(SetStopTracingButtonEnabled(bool)), stop_recording, SLOT(setVisible(bool)));
38 connect(this, SIGNAL(SetAbortTracingButtonEnabled(bool)), abort_recording, SLOT(setVisible(bool)));
39 connect(start_recording, SIGNAL(clicked()), this, SLOT(StartRecording()));
40 connect(stop_recording, SIGNAL(clicked()), this, SLOT(StopRecording()));
41 connect(abort_recording, SIGNAL(clicked()), this, SLOT(AbortRecording()));
42
43 stop_recording->setVisible(false);
44 abort_recording->setVisible(false);
45
46 auto main_widget = new QWidget;
47 auto main_layout = new QVBoxLayout;
48 {
49 auto sub_layout = new QHBoxLayout;
50 sub_layout->addWidget(start_recording);
51 sub_layout->addWidget(stop_recording);
52 sub_layout->addWidget(abort_recording);
53 main_layout->addLayout(sub_layout);
54 }
55 main_widget->setLayout(main_layout);
56 setWidget(main_widget);
57}
58
59void GraphicsTracingWidget::StartRecording() {
60 auto context = context_weak.lock();
61 if (!context)
62 return;
63
64 auto shader_binary = Pica::g_state.vs.program_code;
65 auto swizzle_data = Pica::g_state.vs.swizzle_data;
66
67 // Encode floating point numbers to 24-bit values
68 // TODO: Drop this explicit conversion once we store float24 values bit-correctly internally.
69 std::array<uint32_t, 4 * 16> default_attributes;
70 for (unsigned i = 0; i < 16; ++i) {
71 for (unsigned comp = 0; comp < 3; ++comp) {
72 default_attributes[4 * i + comp] = nihstro::to_float24(Pica::g_state.vs.default_attributes[i][comp].ToFloat32());
73 }
74 }
75
76 std::array<uint32_t, 4 * 96> vs_float_uniforms;
77 for (unsigned i = 0; i < 96; ++i)
78 for (unsigned comp = 0; comp < 3; ++comp)
79 vs_float_uniforms[4 * i + comp] = nihstro::to_float24(Pica::g_state.vs.uniforms.f[i][comp].ToFloat32());
80
81 CiTrace::Recorder::InitialState state;
82 std::copy_n((u32*)&GPU::g_regs, sizeof(GPU::g_regs) / sizeof(u32), std::back_inserter(state.gpu_registers));
83 std::copy_n((u32*)&LCD::g_regs, sizeof(LCD::g_regs) / sizeof(u32), std::back_inserter(state.lcd_registers));
84 std::copy_n((u32*)&Pica::g_state.regs, sizeof(Pica::g_state.regs) / sizeof(u32), std::back_inserter(state.pica_registers));
85 boost::copy(default_attributes, std::back_inserter(state.default_attributes));
86 boost::copy(shader_binary, std::back_inserter(state.vs_program_binary));
87 boost::copy(swizzle_data, std::back_inserter(state.vs_swizzle_data));
88 boost::copy(vs_float_uniforms, std::back_inserter(state.vs_float_uniforms));
89 //boost::copy(TODO: Not implemented, std::back_inserter(state.gs_program_binary));
90 //boost::copy(TODO: Not implemented, std::back_inserter(state.gs_swizzle_data));
91 //boost::copy(TODO: Not implemented, std::back_inserter(state.gs_float_uniforms));
92
93 auto recorder = new CiTrace::Recorder(state);
94 context->recorder = std::shared_ptr<CiTrace::Recorder>(recorder);
95
96 emit SetStartTracingButtonEnabled(false);
97 emit SetStopTracingButtonEnabled(true);
98 emit SetAbortTracingButtonEnabled(true);
99}
100
101void GraphicsTracingWidget::StopRecording() {
102 auto context = context_weak.lock();
103 if (!context)
104 return;
105
106 QString filename = QFileDialog::getSaveFileName(this, tr("Save CiTrace"), "citrace.ctf",
107 tr("CiTrace File (*.ctf)"));
108
109 if (filename.isEmpty()) {
110 // If the user canceled the dialog, keep recording
111 return;
112 }
113
114 context->recorder->Finish(filename.toStdString());
115 context->recorder = nullptr;
116
117 emit SetStopTracingButtonEnabled(false);
118 emit SetAbortTracingButtonEnabled(false);
119 emit SetStartTracingButtonEnabled(true);
120}
121
122void GraphicsTracingWidget::AbortRecording() {
123 auto context = context_weak.lock();
124 if (!context)
125 return;
126
127 context->recorder = nullptr;
128
129 emit SetStopTracingButtonEnabled(false);
130 emit SetAbortTracingButtonEnabled(false);
131 emit SetStartTracingButtonEnabled(true);
132}
133
134void GraphicsTracingWidget::OnBreakPointHit(Pica::DebugContext::Event event, void* data) {
135 widget()->setEnabled(true);
136}
137
138void GraphicsTracingWidget::OnResumed() {
139 widget()->setEnabled(false);
140}
141
142void GraphicsTracingWidget::OnEmulationStarting(EmuThread* emu_thread) {
143 // Disable tracing starting/stopping until a GPU breakpoint is reached
144 widget()->setEnabled(false);
145}
146
147void GraphicsTracingWidget::OnEmulationStopping() {
148 // TODO: Is it safe to access the context here?
149
150 auto context = context_weak.lock();
151 if (!context)
152 return;
153
154
155 if (context->recorder) {
156 auto reply = QMessageBox::question(this, tr("CiTracing still active"),
157 tr("A CiTrace is still being recorded. Do you want to save it? If not, all recorded data will be discarded."),
158 QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
159
160 if (reply == QMessageBox::Yes) {
161 StopRecording();
162 } else {
163 AbortRecording();
164 }
165 }
166
167 // If the widget was disabled before, enable it now to allow starting
168 // tracing before starting the next emulation session
169 widget()->setEnabled(true);
170}