summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/citra_qt/CMakeLists.txt2
-rw-r--r--src/citra_qt/debugger/graphics_breakpoint_observer.h2
-rw-r--r--src/citra_qt/debugger/graphics_tracing.cpp123
-rw-r--r--src/citra_qt/debugger/graphics_tracing.h27
-rw-r--r--src/citra_qt/main.cpp6
-rw-r--r--src/core/CMakeLists.txt3
-rw-r--r--src/core/hle/service/gsp_gpu.cpp2
-rw-r--r--src/core/hw/gpu.cpp17
-rw-r--r--src/core/hw/lcd.cpp10
-rw-r--r--src/core/tracer/recorder.cpp198
-rw-r--r--src/core/tracer/recorder.h92
-rw-r--r--src/core/tracer/tracer.h98
-rw-r--r--src/video_core/command_processor.cpp55
-rw-r--r--src/video_core/debug_utils/debug_utils.h4
-rw-r--r--src/video_core/renderer_opengl/renderer_opengl.cpp6
15 files changed, 641 insertions, 4 deletions
diff --git a/src/citra_qt/CMakeLists.txt b/src/citra_qt/CMakeLists.txt
index d4c0cecc3..47aaeca24 100644
--- a/src/citra_qt/CMakeLists.txt
+++ b/src/citra_qt/CMakeLists.txt
@@ -12,6 +12,7 @@ set(SRCS
12 debugger/graphics_breakpoints.cpp 12 debugger/graphics_breakpoints.cpp
13 debugger/graphics_cmdlists.cpp 13 debugger/graphics_cmdlists.cpp
14 debugger/graphics_framebuffer.cpp 14 debugger/graphics_framebuffer.cpp
15 debugger/graphics_tracing.cpp
15 debugger/graphics_vertex_shader.cpp 16 debugger/graphics_vertex_shader.cpp
16 debugger/profiler.cpp 17 debugger/profiler.cpp
17 debugger/ramview.cpp 18 debugger/ramview.cpp
@@ -35,6 +36,7 @@ set(HEADERS
35 debugger/graphics_breakpoints_p.h 36 debugger/graphics_breakpoints_p.h
36 debugger/graphics_cmdlists.h 37 debugger/graphics_cmdlists.h
37 debugger/graphics_framebuffer.h 38 debugger/graphics_framebuffer.h
39 debugger/graphics_tracing.h
38 debugger/graphics_vertex_shader.h 40 debugger/graphics_vertex_shader.h
39 debugger/profiler.h 41 debugger/profiler.h
40 debugger/ramview.h 42 debugger/ramview.h
diff --git a/src/citra_qt/debugger/graphics_breakpoint_observer.h b/src/citra_qt/debugger/graphics_breakpoint_observer.h
index f0d3361f8..02a0f4f4f 100644
--- a/src/citra_qt/debugger/graphics_breakpoint_observer.h
+++ b/src/citra_qt/debugger/graphics_breakpoint_observer.h
@@ -13,7 +13,7 @@
13 * This is because the Pica breakpoint callbacks are called from a non-GUI thread, while 13 * This is because the Pica breakpoint callbacks are called from a non-GUI thread, while
14 * the widget usually wants to perform reactions in the GUI thread. 14 * the widget usually wants to perform reactions in the GUI thread.
15 */ 15 */
16class BreakPointObserverDock : public QDockWidget, private Pica::DebugContext::BreakPointObserver { 16class BreakPointObserverDock : public QDockWidget, protected Pica::DebugContext::BreakPointObserver {
17 Q_OBJECT 17 Q_OBJECT
18 18
19public: 19public:
diff --git a/src/citra_qt/debugger/graphics_tracing.cpp b/src/citra_qt/debugger/graphics_tracing.cpp
new file mode 100644
index 000000000..2e74193f5
--- /dev/null
+++ b/src/citra_qt/debugger/graphics_tracing.cpp
@@ -0,0 +1,123 @@
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 <QPushButton>
12#include <QSpinBox>
13
14#include "core/hw/gpu.h"
15#include "video_core/pica.h"
16
17#include "nihstro/float24.h"
18
19#include "graphics_tracing.h"
20
21GraphicsTracingWidget::GraphicsTracingWidget(std::shared_ptr<Pica::DebugContext> debug_context,
22 QWidget* parent)
23 : BreakPointObserverDock(debug_context, tr("CiTrace Recorder"), parent) {
24
25 setObjectName("CiTracing");
26
27 QPushButton* start_recording = new QPushButton(tr("Start Recording"));
28 QPushButton* stop_recording = new QPushButton(QIcon::fromTheme("document-save"), tr("Stop and Save"));
29 QPushButton* abort_recording = new QPushButton(tr("Abort Recording"));
30
31 connect(this, SIGNAL(SetStartTracingButtonEnabled(bool)), start_recording, SLOT(setVisible(bool)));
32 connect(this, SIGNAL(SetStopTracingButtonEnabled(bool)), stop_recording, SLOT(setVisible(bool)));
33 connect(this, SIGNAL(SetAbortTracingButtonEnabled(bool)), abort_recording, SLOT(setVisible(bool)));
34 connect(start_recording, SIGNAL(clicked()), this, SLOT(StartRecording()));
35 connect(stop_recording, SIGNAL(clicked()), this, SLOT(StopRecording()));
36 connect(abort_recording, SIGNAL(clicked()), this, SLOT(AbortRecording()));
37
38 stop_recording->setVisible(false);
39 abort_recording->setVisible(false);
40
41 auto main_widget = new QWidget;
42 auto main_layout = new QVBoxLayout;
43 {
44 auto sub_layout = new QHBoxLayout;
45 sub_layout->addWidget(start_recording);
46 sub_layout->addWidget(stop_recording);
47 sub_layout->addWidget(abort_recording);
48 main_layout->addLayout(sub_layout);
49 }
50 main_widget->setLayout(main_layout);
51 setWidget(main_widget);
52
53 // TODO: Make sure to have this widget disabled as soon as emulation is started!
54}
55
56void GraphicsTracingWidget::StartRecording() {
57 auto context = context_weak.lock();
58 if (!context)
59 return;
60
61 auto shader_binary = Pica::g_state.vs.program_code;
62 auto swizzle_data = Pica::g_state.vs.swizzle_data;
63
64 // Encode floating point numbers to 24-bit values
65 // TODO: Drop this explicit conversion once we store float24 values bit-correctly internally.
66 std::array<Math::Vec4<uint32_t>, 96> vs_float_uniforms;
67 for (unsigned i = 0; i < 96; ++i)
68 for (unsigned comp = 0; comp < 3; ++comp)
69 vs_float_uniforms[i][comp] = nihstro::to_float24(Pica::g_state.vs.uniforms.f[i][comp].ToFloat32());
70
71 auto recorder = new CiTrace::Recorder((u32*)&GPU::g_regs, 0x700, nullptr, 0, (u32*)&Pica::g_state.regs, 0x300,
72 shader_binary.data(), shader_binary.size(),
73 swizzle_data.data(), swizzle_data.size(),
74 (u32*)vs_float_uniforms.data(), vs_float_uniforms.size() * 4,
75 nullptr, 0, nullptr, 0, nullptr, 0 // Geometry shader is not implemented yet, so submit dummy data for now
76 );
77 context->recorder = std::shared_ptr<CiTrace::Recorder>(recorder);
78
79 emit SetStartTracingButtonEnabled(false);
80 emit SetStopTracingButtonEnabled(true);
81 emit SetAbortTracingButtonEnabled(true);
82}
83
84void GraphicsTracingWidget::StopRecording() {
85 auto context = context_weak.lock();
86 if (!context)
87 return;
88
89 QString filename = QFileDialog::getSaveFileName(this, tr("Save CiTrace"), "citrace.ctf",
90 tr("CiTrace File (*.ctf)"));
91
92 if (filename.isEmpty()) {
93 // If the user canceled the dialog, keep recording
94 return;
95 }
96
97 context->recorder->Finish(filename.toStdString());
98 context->recorder = nullptr;
99
100 emit SetStopTracingButtonEnabled(false);
101 emit SetAbortTracingButtonEnabled(false);
102 emit SetStartTracingButtonEnabled(true);
103}
104
105void GraphicsTracingWidget::AbortRecording() {
106 auto context = context_weak.lock();
107 if (!context)
108 return;
109
110 context->recorder = nullptr;
111
112 emit SetStopTracingButtonEnabled(false);
113 emit SetAbortTracingButtonEnabled(false);
114 emit SetStartTracingButtonEnabled(true);
115}
116
117void GraphicsTracingWidget::OnBreakPointHit(Pica::DebugContext::Event event, void* data) {
118 widget()->setEnabled(true);
119}
120
121void GraphicsTracingWidget::OnResumed() {
122 widget()->setEnabled(false);
123}
diff --git a/src/citra_qt/debugger/graphics_tracing.h b/src/citra_qt/debugger/graphics_tracing.h
new file mode 100644
index 000000000..397a7173b
--- /dev/null
+++ b/src/citra_qt/debugger/graphics_tracing.h
@@ -0,0 +1,27 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "graphics_breakpoint_observer.h"
8
9class GraphicsTracingWidget : public BreakPointObserverDock {
10 Q_OBJECT
11
12public:
13 GraphicsTracingWidget(std::shared_ptr<Pica::DebugContext> debug_context, QWidget* parent = nullptr);
14
15private slots:
16 void StartRecording();
17 void StopRecording();
18 void AbortRecording();
19
20 void OnBreakPointHit(Pica::DebugContext::Event event, void* data) override;
21 void OnResumed() override;
22
23signals:
24 void SetStartTracingButtonEnabled(bool enable);
25 void SetStopTracingButtonEnabled(bool enable);
26 void SetAbortTracingButtonEnabled(bool enable);
27};
diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index d23bafafc..6bfb6e417 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -32,6 +32,7 @@
32#include "debugger/graphics_breakpoints.h" 32#include "debugger/graphics_breakpoints.h"
33#include "debugger/graphics_cmdlists.h" 33#include "debugger/graphics_cmdlists.h"
34#include "debugger/graphics_framebuffer.h" 34#include "debugger/graphics_framebuffer.h"
35#include "debugger/graphics_tracing.h"
35#include "debugger/graphics_vertex_shader.h" 36#include "debugger/graphics_vertex_shader.h"
36#include "debugger/profiler.h" 37#include "debugger/profiler.h"
37 38
@@ -94,6 +95,10 @@ GMainWindow::GMainWindow() : emu_thread(nullptr)
94 addDockWidget(Qt::RightDockWidgetArea, graphicsVertexShaderWidget); 95 addDockWidget(Qt::RightDockWidgetArea, graphicsVertexShaderWidget);
95 graphicsVertexShaderWidget->hide(); 96 graphicsVertexShaderWidget->hide();
96 97
98 auto graphicsTracingWidget = new GraphicsTracingWidget(Pica::g_debug_context, this);
99 addDockWidget(Qt::RightDockWidgetArea, graphicsTracingWidget);
100 graphicsTracingWidget->hide();
101
97 QMenu* debug_menu = ui.menu_View->addMenu(tr("Debugging")); 102 QMenu* debug_menu = ui.menu_View->addMenu(tr("Debugging"));
98 debug_menu->addAction(profilerWidget->toggleViewAction()); 103 debug_menu->addAction(profilerWidget->toggleViewAction());
99 debug_menu->addAction(disasmWidget->toggleViewAction()); 104 debug_menu->addAction(disasmWidget->toggleViewAction());
@@ -104,6 +109,7 @@ GMainWindow::GMainWindow() : emu_thread(nullptr)
104 debug_menu->addAction(graphicsBreakpointsWidget->toggleViewAction()); 109 debug_menu->addAction(graphicsBreakpointsWidget->toggleViewAction());
105 debug_menu->addAction(graphicsFramebufferWidget->toggleViewAction()); 110 debug_menu->addAction(graphicsFramebufferWidget->toggleViewAction());
106 debug_menu->addAction(graphicsVertexShaderWidget->toggleViewAction()); 111 debug_menu->addAction(graphicsVertexShaderWidget->toggleViewAction());
112 debug_menu->addAction(graphicsTracingWidget->toggleViewAction());
107 113
108 // Set default UI state 114 // Set default UI state
109 // geometry: 55% of the window contents are in the upper screen half, 45% in the lower half 115 // geometry: 55% of the window contents are in the upper screen half, 45% in the lower half
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 9b004440c..5066eb258 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -115,6 +115,7 @@ set(SRCS
115 loader/elf.cpp 115 loader/elf.cpp
116 loader/loader.cpp 116 loader/loader.cpp
117 loader/ncch.cpp 117 loader/ncch.cpp
118 tracer/recorder.cpp
118 mem_map.cpp 119 mem_map.cpp
119 memory.cpp 120 memory.cpp
120 settings.cpp 121 settings.cpp
@@ -243,6 +244,8 @@ set(HEADERS
243 loader/elf.h 244 loader/elf.h
244 loader/loader.h 245 loader/loader.h
245 loader/ncch.h 246 loader/ncch.h
247 tracer/recorder.h
248 tracer/tracer.h
246 mem_map.h 249 mem_map.h
247 memory.h 250 memory.h
248 memory_setup.h 251 memory_setup.h
diff --git a/src/core/hle/service/gsp_gpu.cpp b/src/core/hle/service/gsp_gpu.cpp
index f175085e8..3910d0227 100644
--- a/src/core/hle/service/gsp_gpu.cpp
+++ b/src/core/hle/service/gsp_gpu.cpp
@@ -349,7 +349,7 @@ void SignalInterrupt(InterruptId interrupt_id) {
349/// Executes the next GSP command 349/// Executes the next GSP command
350static void ExecuteCommand(const Command& command, u32 thread_id) { 350static void ExecuteCommand(const Command& command, u32 thread_id) {
351 // Utility function to convert register ID to address 351 // Utility function to convert register ID to address
352 auto WriteGPURegister = [](u32 id, u32 data) { 352 static auto WriteGPURegister = [](u32 id, u32 data) {
353 GPU::Write<u32>(0x1EF00000 + 4 * id, data); 353 GPU::Write<u32>(0x1EF00000 + 4 * id, data);
354 }; 354 };
355 355
diff --git a/src/core/hw/gpu.cpp b/src/core/hw/gpu.cpp
index 796493378..a3a7d128f 100644
--- a/src/core/hw/gpu.cpp
+++ b/src/core/hw/gpu.cpp
@@ -21,12 +21,17 @@
21#include "core/hw/hw.h" 21#include "core/hw/hw.h"
22#include "core/hw/gpu.h" 22#include "core/hw/gpu.h"
23 23
24#include "core/tracer/recorder.h"
25
24#include "video_core/command_processor.h" 26#include "video_core/command_processor.h"
25#include "video_core/hwrasterizer_base.h" 27#include "video_core/hwrasterizer_base.h"
26#include "video_core/renderer_base.h" 28#include "video_core/renderer_base.h"
27#include "video_core/utils.h" 29#include "video_core/utils.h"
28#include "video_core/video_core.h" 30#include "video_core/video_core.h"
29 31
32#include "video_core/debug_utils/debug_utils.h"
33
34
30namespace GPU { 35namespace GPU {
31 36
32Regs g_regs; 37Regs g_regs;
@@ -289,6 +294,11 @@ inline void Write(u32 addr, const T data) {
289 if (config.trigger & 1) 294 if (config.trigger & 1)
290 { 295 {
291 u32* buffer = (u32*)Memory::GetPhysicalPointer(config.GetPhysicalAddress()); 296 u32* buffer = (u32*)Memory::GetPhysicalPointer(config.GetPhysicalAddress());
297
298 if (Pica::g_debug_context && Pica::g_debug_context->recorder) {
299 Pica::g_debug_context->recorder->MemoryAccessed((u8*)buffer, config.size * sizeof(u32), config.GetPhysicalAddress());
300 }
301
292 Pica::CommandProcessor::ProcessCommandList(buffer, config.size); 302 Pica::CommandProcessor::ProcessCommandList(buffer, config.size);
293 303
294 g_regs.command_processor_config.trigger = 0; 304 g_regs.command_processor_config.trigger = 0;
@@ -299,6 +309,13 @@ inline void Write(u32 addr, const T data) {
299 default: 309 default:
300 break; 310 break;
301 } 311 }
312
313 // Notify tracer about the register write
314 // This is happening *after* handling the write to make sure we properly catch all memory reads.
315 if (Pica::g_debug_context && Pica::g_debug_context->recorder) {
316 // addr + GPU VBase - IO VBase + IO PBase
317 Pica::g_debug_context->recorder->RegisterWritten<T>(addr + 0x1EF00000 - 0x1EC00000 + 0x10100000, data);
318 }
302} 319}
303 320
304// Explicitly instantiate template functions because we aren't defining this in the header: 321// Explicitly instantiate template functions because we aren't defining this in the header:
diff --git a/src/core/hw/lcd.cpp b/src/core/hw/lcd.cpp
index cdb757a18..6f93709e3 100644
--- a/src/core/hw/lcd.cpp
+++ b/src/core/hw/lcd.cpp
@@ -10,6 +10,9 @@
10#include "core/hw/hw.h" 10#include "core/hw/hw.h"
11#include "core/hw/lcd.h" 11#include "core/hw/lcd.h"
12 12
13#include "core/tracer/recorder.h"
14#include "video_core/debug_utils/debug_utils.h"
15
13namespace LCD { 16namespace LCD {
14 17
15Regs g_regs; 18Regs g_regs;
@@ -40,6 +43,13 @@ inline void Write(u32 addr, const T data) {
40 } 43 }
41 44
42 g_regs[index] = static_cast<u32>(data); 45 g_regs[index] = static_cast<u32>(data);
46
47 // Notify tracer about the register write
48 // This is happening *after* handling the write to make sure we properly catch all memory reads.
49 if (Pica::g_debug_context && Pica::g_debug_context->recorder) {
50 // addr + GPU VBase - IO VBase + IO PBase
51 Pica::g_debug_context->recorder->RegisterWritten<T>(addr + HW::VADDR_LCD - 0x1EC00000 + 0x10100000, data);
52 }
43} 53}
44 54
45// Explicitly instantiate template functions because we aren't defining this in the header: 55// Explicitly instantiate template functions because we aren't defining this in the header:
diff --git a/src/core/tracer/recorder.cpp b/src/core/tracer/recorder.cpp
new file mode 100644
index 000000000..73ebdd388
--- /dev/null
+++ b/src/core/tracer/recorder.cpp
@@ -0,0 +1,198 @@
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 <cstring>
6
7#include "common/assert.h"
8#include "common/file_util.h"
9#include "common/logging/log.h"
10
11#include "recorder.h"
12
13namespace CiTrace {
14
15Recorder::Recorder(u32* gpu_registers, u32 gpu_registers_size,
16 u32* lcd_registers, u32 lcd_registers_size,
17 u32* pica_registers, u32 pica_registers_size,
18 u32* vs_program_binary, u32 vs_program_binary_size,
19 u32* vs_swizzle_data, u32 vs_swizzle_data_size,
20 u32* vs_float_uniforms, u32 vs_float_uniforms_size,
21 u32* gs_program_binary, u32 gs_program_binary_size,
22 u32* gs_swizzle_data, u32 gs_swizzle_data_size,
23 u32* gs_float_uniforms, u32 gs_float_uniforms_size)
24 : gpu_registers(gpu_registers, gpu_registers + gpu_registers_size),
25 lcd_registers(lcd_registers, lcd_registers + lcd_registers_size),
26 pica_registers(pica_registers, pica_registers + pica_registers_size),
27 vs_program_binary(vs_program_binary, vs_program_binary + vs_program_binary_size),
28 vs_swizzle_data(vs_swizzle_data, vs_swizzle_data + vs_swizzle_data_size),
29 vs_float_uniforms(vs_float_uniforms, vs_float_uniforms + vs_float_uniforms_size),
30 gs_program_binary(gs_program_binary, gs_program_binary + gs_program_binary_size),
31 gs_swizzle_data(gs_swizzle_data, gs_swizzle_data + gs_swizzle_data_size),
32 gs_float_uniforms(gs_float_uniforms, gs_float_uniforms + gs_float_uniforms_size) {
33
34}
35
36void Recorder::Finish(const std::string& filename) {
37 // Setup CiTrace header
38 CTHeader header;
39 std::memcpy(header.magic, CTHeader::ExpectedMagicWord(), 4);
40 header.version = CTHeader::ExpectedVersion();
41 header.header_size = sizeof(CTHeader);
42
43 // Calculate file offsets
44 auto& initial = header.initial_state_offsets;
45
46 initial.gpu_registers_size = gpu_registers.size();
47 initial.lcd_registers_size = lcd_registers.size();
48 initial.pica_registers_size = pica_registers.size();
49 initial.vs_program_binary_size = vs_program_binary.size();
50 initial.vs_swizzle_data_size = vs_swizzle_data.size();
51 initial.vs_float_uniforms_size = vs_float_uniforms.size();
52 initial.gs_program_binary_size = gs_program_binary.size();
53 initial.gs_swizzle_data_size = gs_swizzle_data.size();
54 initial.gs_float_uniforms_size = gs_float_uniforms.size();
55 header.stream_size = stream.size();
56
57 initial.gpu_registers = sizeof(header);
58 initial.lcd_registers = initial.gpu_registers + initial.gpu_registers_size * sizeof(u32);
59 initial.pica_registers = initial.lcd_registers + initial.lcd_registers_size * sizeof(u32);;
60 initial.vs_program_binary = initial.pica_registers + initial.pica_registers_size * sizeof(u32);
61 initial.vs_swizzle_data = initial.vs_program_binary + initial.vs_program_binary_size * sizeof(u32);
62 initial.vs_float_uniforms = initial.vs_swizzle_data + initial.vs_swizzle_data_size * sizeof(u32);
63 initial.gs_program_binary = initial.vs_float_uniforms + initial.vs_float_uniforms_size * sizeof(u32);
64 initial.gs_swizzle_data = initial.gs_program_binary + initial.gs_program_binary_size * sizeof(u32);
65 initial.gs_float_uniforms = initial.gs_swizzle_data + initial.gs_swizzle_data_size * sizeof(u32);
66 header.stream_offset = initial.gs_float_uniforms + initial.gs_float_uniforms_size * sizeof(u32);
67
68 // Iterate through stream elements, update relevant stream element data
69 for (auto& stream_element : stream) {
70 switch (stream_element.data.type) {
71 case MemoryLoad:
72 {
73 auto& file_offset = memory_regions[stream_element.hash];
74 if (!stream_element.uses_existing_data) {
75 file_offset = header.stream_offset;
76 }
77 stream_element.data.memory_load.file_offset = file_offset;
78 break;
79 }
80
81 default:
82 // Other commands don't use any extra data
83 DEBUG_ASSERT(stream_element.extra_data.size() == 0);
84 break;
85 }
86 header.stream_offset += stream_element.extra_data.size();
87 }
88
89 try {
90 // Open file and write header
91 FileUtil::IOFile file(filename, "wb");
92 size_t written = file.WriteObject(header);
93 if (written != 1 || file.Tell() != initial.gpu_registers)
94 throw "Failed to write header";
95
96 // Write initial state
97 written = file.WriteArray(gpu_registers.data(), gpu_registers.size());
98 if (written != gpu_registers.size() || file.Tell() != initial.lcd_registers)
99 throw "Failed to write GPU registers";
100
101 written = file.WriteArray(lcd_registers.data(), lcd_registers.size());
102 if (written != lcd_registers.size() || file.Tell() != initial.pica_registers)
103 throw "Failed to write LCD registers";
104
105 written = file.WriteArray(pica_registers.data(), pica_registers.size());
106 if (written != pica_registers.size() || file.Tell() != initial.vs_program_binary)
107 throw "Failed to write Pica registers";
108
109 written = file.WriteArray(vs_program_binary.data(), vs_program_binary.size());
110 if (written != vs_program_binary.size() || file.Tell() != initial.vs_swizzle_data)
111 throw "Failed to write vertex shader program binary";
112
113 written = file.WriteArray(vs_swizzle_data.data(), vs_swizzle_data.size());
114 if (written != vs_swizzle_data.size() || file.Tell() != initial.vs_float_uniforms)
115 throw "Failed to write vertex shader swizzle data";
116
117 written = file.WriteArray(vs_float_uniforms.data(), vs_float_uniforms.size());
118 if (written != vs_float_uniforms.size() || file.Tell() != initial.gs_program_binary)
119 throw "Failed to write vertex shader float uniforms";
120
121 written = file.WriteArray(gs_program_binary.data(), gs_program_binary.size());
122 if (written != gs_program_binary.size() || file.Tell() != initial.gs_swizzle_data)
123 throw "Failed to write geomtry shader program binary";
124
125 written = file.WriteArray(gs_swizzle_data.data(), gs_swizzle_data.size());
126 if (written != gs_swizzle_data.size() || file.Tell() != initial.gs_float_uniforms)
127 throw "Failed to write geometry shader swizzle data";
128
129 written = file.WriteArray(gs_float_uniforms.data(), gs_float_uniforms.size());
130 if (written != gs_float_uniforms.size() || file.Tell() != initial.gs_float_uniforms + sizeof(u32) * initial.gs_float_uniforms_size)
131 throw "Failed to write geometry shader float uniforms";
132
133 // Iterate through stream elements, write "extra data"
134 for (const auto& stream_element : stream) {
135 if (stream_element.extra_data.size() == 0)
136 continue;
137
138 written = file.WriteBytes(stream_element.extra_data.data(), stream_element.extra_data.size());
139 if (written != stream_element.extra_data.size())
140 throw "Failed to write extra data";
141 }
142
143 if (file.Tell() != header.stream_offset)
144 throw "Unexpected end of extra data";
145
146 // Write actual stream elements
147 for (const auto& stream_element : stream) {
148 if (1 != file.WriteObject(stream_element.data))
149 throw "Failed to write stream element";
150 }
151 } catch(const char* str) {
152 LOG_ERROR(HW_GPU, "Writing CiTrace file failed: %s", str);
153 }
154}
155
156void Recorder::FrameFinished() {
157 stream.push_back( { FrameMarker } );
158}
159
160void Recorder::MemoryAccessed(const u8* data, u32 size, u32 physical_address) {
161 StreamElement element = { MemoryLoad };
162 element.data.memory_load.size = size;
163 element.data.memory_load.physical_address = physical_address;
164
165 // Compute hash over given memory region to check if the contents are already stored internally
166 boost::crc_32_type result;
167 result.process_bytes(data, size);
168 element.hash = result.checksum();
169
170 element.uses_existing_data = (memory_regions.find(element.hash) != memory_regions.end());
171 if (!element.uses_existing_data) {
172 element.extra_data.resize(size);
173 memcpy(element.extra_data.data(), data, size);
174 memory_regions.insert({element.hash, 0}); // file offset will be initialized in Finish()
175 }
176
177 stream.push_back(element);
178}
179
180template<typename T>
181void Recorder::RegisterWritten(u32 physical_address, T value) {
182 StreamElement element = { RegisterWrite };
183 element.data.register_write.size = (sizeof(T) == 1) ? CTRegisterWrite::SIZE_8
184 : (sizeof(T) == 2) ? CTRegisterWrite::SIZE_16
185 : (sizeof(T) == 4) ? CTRegisterWrite::SIZE_32
186 : CTRegisterWrite::SIZE_64;
187 element.data.register_write.physical_address = physical_address;
188 element.data.register_write.value = value;
189
190 stream.push_back(element);
191}
192
193template void Recorder::RegisterWritten(u32,u8);
194template void Recorder::RegisterWritten(u32,u16);
195template void Recorder::RegisterWritten(u32,u32);
196template void Recorder::RegisterWritten(u32,u64);
197
198}
diff --git a/src/core/tracer/recorder.h b/src/core/tracer/recorder.h
new file mode 100644
index 000000000..8fec0a971
--- /dev/null
+++ b/src/core/tracer/recorder.h
@@ -0,0 +1,92 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <unordered_map>
8#include <vector>
9
10#include <boost/crc.hpp>
11
12#include "common/common_types.h"
13
14#include "tracer.h"
15
16namespace CiTrace {
17
18class Recorder {
19public:
20 /**
21 * Recorder constructor
22 * @param vs_float_uniforms Pointer to an array of 32-bit-aligned 24-bit floating point values.
23 */
24 Recorder(u32* gpu_registers, u32 gpu_registers_size,
25 u32* lcd_registers, u32 lcd_registers_size,
26 u32* pica_registers, u32 pica_registers_size,
27 u32* vs_program_binary, u32 vs_program_binary_size,
28 u32* vs_swizzle_data, u32 vs_swizzle_data_size,
29 u32* vs_float_uniforms, u32 vs_float_uniforms_size,
30 u32* gs_program_binary, u32 gs_program_binary_size,
31 u32* gs_swizzle_data, u32 gs_swizzle_data_size,
32 u32* gs_float_uniforms, u32 gs_float_uniforms_size);
33
34 /// Finish recording of this Citrace and save it using the given filename.
35 void Finish(const std::string& filename);
36
37 /// Mark end of a frame
38 void FrameFinished();
39
40 /**
41 * Store a copy of the given memory range in the recording.
42 * @note Use this whenever the GPU is about to access a particular memory region.
43 * @note The implementation will make sure to minimize redundant memory updates.
44 */
45 void MemoryAccessed(const u8* data, u32 size, u32 physical_address);
46
47 /**
48 * Record a register write.
49 * @note Use this whenever a GPU-related MMIO register has been written to.
50 */
51 template<typename T>
52 void RegisterWritten(u32 physical_address, T value);
53
54private:
55 // Initial state of recording start
56 std::vector<u32> gpu_registers;
57 std::vector<u32> lcd_registers;
58 std::vector<u32> pica_registers;
59 std::vector<u32> vs_program_binary;
60 std::vector<u32> vs_swizzle_data;
61 std::vector<u32> vs_float_uniforms;
62 std::vector<u32> gs_program_binary;
63 std::vector<u32> gs_swizzle_data;
64 std::vector<u32> gs_float_uniforms;
65
66 // Command stream
67 struct StreamElement {
68 CTStreamElement data;
69
70 /**
71 * Extra data to store along "core" data.
72 * This is e.g. used for data used in MemoryUpdates.
73 */
74 std::vector<u8> extra_data;
75
76 /// Optional CRC hash (e.g. for hashing memory regions)
77 boost::crc_32_type::value_type hash;
78
79 /// If true, refer to data already written to the output file instead of extra_data
80 bool uses_existing_data;
81 };
82
83 std::vector<StreamElement> stream;
84
85 /**
86 * Internal cache which maps hashes of memory contents to file offsets at which those memory
87 * contents are stored.
88 */
89 std::unordered_map<boost::crc_32_type::value_type /*hash*/, u32 /*file_offset*/> memory_regions;
90};
91
92} // namespace
diff --git a/src/core/tracer/tracer.h b/src/core/tracer/tracer.h
new file mode 100644
index 000000000..1545d7439
--- /dev/null
+++ b/src/core/tracer/tracer.h
@@ -0,0 +1,98 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <cstdint>
8
9namespace CiTrace {
10
11// NOTE: Things are stored in little-endian
12
13#pragma pack(1)
14
15struct CTHeader {
16 static const char* ExpectedMagicWord() {
17 return "CiTr";
18 }
19
20 static uint32_t ExpectedVersion() {
21 return 1;
22 }
23
24 char magic[4];
25 uint32_t version;
26 uint32_t header_size;
27
28 struct {
29 // NOTE: Register range sizes are technically hardware-constants, but the actual limits
30 // aren't known. Hence we store the presumed limits along the offsets.
31 // Sizes are given in uint32_t units.
32 uint32_t gpu_registers;
33 uint32_t gpu_registers_size;
34 uint32_t lcd_registers;
35 uint32_t lcd_registers_size;
36 uint32_t pica_registers;
37 uint32_t pica_registers_size;
38 uint32_t vs_program_binary;
39 uint32_t vs_program_binary_size;
40 uint32_t vs_swizzle_data;
41 uint32_t vs_swizzle_data_size;
42 uint32_t vs_float_uniforms;
43 uint32_t vs_float_uniforms_size;
44 uint32_t gs_program_binary;
45 uint32_t gs_program_binary_size;
46 uint32_t gs_swizzle_data;
47 uint32_t gs_swizzle_data_size;
48 uint32_t gs_float_uniforms;
49 uint32_t gs_float_uniforms_size;
50
51 // Other things we might want to store here:
52 // - Initial framebuffer data, maybe even a full copy of FCRAM/VRAM
53 // - Default vertex attributes
54 } initial_state_offsets;
55
56 uint32_t stream_offset;
57 uint32_t stream_size;
58};
59
60enum CTStreamElementType : uint32_t {
61 FrameMarker = 0xE1,
62 MemoryLoad = 0xE2,
63 RegisterWrite = 0xE3,
64};
65
66struct CTMemoryLoad {
67 uint32_t file_offset;
68 uint32_t size;
69 uint32_t physical_address;
70 uint32_t pad;
71};
72
73struct CTRegisterWrite {
74 uint32_t physical_address;
75
76 enum : uint32_t {
77 SIZE_8 = 0xD1,
78 SIZE_16 = 0xD2,
79 SIZE_32 = 0xD3,
80 SIZE_64 = 0xD4
81 } size;
82
83 // TODO: Make it clearer which bits of this member are used for sizes other than 32 bits
84 uint64_t value;
85};
86
87struct CTStreamElement {
88 CTStreamElementType type;
89
90 union {
91 CTMemoryLoad memory_load;
92 CTRegisterWrite register_write;
93 };
94};
95
96#pragma pack()
97
98}
diff --git a/src/video_core/command_processor.cpp b/src/video_core/command_processor.cpp
index 110caec76..2095e7b15 100644
--- a/src/video_core/command_processor.cpp
+++ b/src/video_core/command_processor.cpp
@@ -123,12 +123,50 @@ static inline void WritePicaReg(u32 id, u32 value, u32 mask) {
123 PrimitiveAssembler<VertexShader::OutputVertex> primitive_assembler(regs.triangle_topology.Value()); 123 PrimitiveAssembler<VertexShader::OutputVertex> primitive_assembler(regs.triangle_topology.Value());
124 PrimitiveAssembler<DebugUtils::GeometryDumper::Vertex> dumping_primitive_assembler(regs.triangle_topology.Value()); 124 PrimitiveAssembler<DebugUtils::GeometryDumper::Vertex> dumping_primitive_assembler(regs.triangle_topology.Value());
125 125
126 if (g_debug_context) {
127 for (int i = 0; i < 3; ++i) {
128 const auto texture = regs.GetTextures()[i];
129 if (!texture.enabled)
130 continue;
131
132 u8* texture_data = Memory::GetPhysicalPointer(texture.config.GetPhysicalAddress());
133 if (g_debug_context && Pica::g_debug_context->recorder)
134 g_debug_context->recorder->MemoryAccessed(texture_data, Pica::Regs::NibblesPerPixel(texture.format) * texture.config.width / 2 * texture.config.height, texture.config.GetPhysicalAddress());
135 }
136 }
137
138 // map physical start address to size
139 std::map<u32, u32> accessed_ranges;
140 static auto SimplifyRanges = [](std::map<u32, u32>& ranges) {
141 for (auto it = ranges.begin(); it != ranges.end(); ++it) {
142
143 // Combine overlapping ranges ... artificially extend first range by 32 bytes to merge "close" ranges
144 auto it2 = std::next(it);
145 while (it2 != ranges.end() && it->first + it->second + 32 >= it2->first) {
146 it->second = std::max(it->second, it2->first + it2->second - it->first);
147 it2 = ranges.erase(it2);
148 }
149 }
150 };
151
152 static auto AddMemoryAccess = [](std::map<u32, u32>& ranges, u32 paddr, u32 size) {
153 // Create new range or extend existing one
154 ranges[paddr] = std::max(ranges[paddr], size);
155
156 // Simplify ranges...
157 SimplifyRanges(ranges);
158 };
159
126 for (unsigned int index = 0; index < regs.num_vertices; ++index) 160 for (unsigned int index = 0; index < regs.num_vertices; ++index)
127 { 161 {
128 unsigned int vertex = is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index]) : index; 162 unsigned int vertex = is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index]) : index;
129 163
130 if (is_indexed) { 164 if (is_indexed) {
131 // TODO: Implement some sort of vertex cache! 165 // TODO: Implement some sort of vertex cache!
166 if (g_debug_context && Pica::g_debug_context->recorder) {
167 int size = index_u16 ? 2 : 1;
168 AddMemoryAccess(accessed_ranges, base_address + index_info.offset + size*index, size);
169 }
132 } 170 }
133 171
134 // Initialize data for the current vertex 172 // Initialize data for the current vertex
@@ -151,7 +189,14 @@ static inline void WritePicaReg(u32 id, u32 value, u32 mask) {
151 189
152 // Load per-vertex data from the loader arrays 190 // Load per-vertex data from the loader arrays
153 for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) { 191 for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
154 const u8* srcdata = Memory::GetPhysicalPointer(vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i]); 192 u32 source_addr = vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i];
193 const u8* srcdata = Memory::GetPhysicalPointer(source_addr);
194
195 if (g_debug_context && Pica::g_debug_context->recorder) {
196 AddMemoryAccess(accessed_ranges, source_addr,
197 (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::FLOAT) ? 4
198 : (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::SHORT) ? 2 : 1);
199 }
155 200
156 const float srcval = (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::BYTE) ? *(s8*)srcdata : 201 const float srcval = (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::BYTE) ? *(s8*)srcdata :
157 (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::UBYTE) ? *(u8*)srcdata : 202 (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::UBYTE) ? *(u8*)srcdata :
@@ -213,14 +258,20 @@ static inline void WritePicaReg(u32 id, u32 value, u32 mask) {
213 } 258 }
214 } 259 }
215 260
261 for (auto& range : accessed_ranges) {
262 g_debug_context->recorder->MemoryAccessed(Memory::GetPhysicalPointer(range.first),
263 range.second, range.first);
264 }
265
216 if (Settings::values.use_hw_renderer) { 266 if (Settings::values.use_hw_renderer) {
217 VideoCore::g_renderer->hw_rasterizer->DrawTriangles(); 267 VideoCore::g_renderer->hw_rasterizer->DrawTriangles();
218 } 268 }
219 269
220 geometry_dumper.Dump(); 270 geometry_dumper.Dump();
221 271
222 if (g_debug_context) 272 if (g_debug_context) {
223 g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr); 273 g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr);
274 }
224 275
225 break; 276 break;
226 } 277 }
diff --git a/src/video_core/debug_utils/debug_utils.h b/src/video_core/debug_utils/debug_utils.h
index 7926d64ec..2573292e2 100644
--- a/src/video_core/debug_utils/debug_utils.h
+++ b/src/video_core/debug_utils/debug_utils.h
@@ -14,6 +14,8 @@
14 14
15#include "common/vector_math.h" 15#include "common/vector_math.h"
16 16
17#include "core/tracer/recorder.h"
18
17#include "video_core/pica.h" 19#include "video_core/pica.h"
18 20
19namespace Pica { 21namespace Pica {
@@ -129,6 +131,8 @@ public:
129 Event active_breakpoint; 131 Event active_breakpoint;
130 bool at_breakpoint = false; 132 bool at_breakpoint = false;
131 133
134 std::shared_ptr<CiTrace::Recorder> recorder = nullptr;
135
132private: 136private:
133 /** 137 /**
134 * Private default constructor to make sure people always construct this through Construct() 138 * Private default constructor to make sure people always construct this through Construct()
diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp
index 9799f74fa..96e12839a 100644
--- a/src/video_core/renderer_opengl/renderer_opengl.cpp
+++ b/src/video_core/renderer_opengl/renderer_opengl.cpp
@@ -22,6 +22,8 @@
22#include "video_core/renderer_opengl/gl_shader_util.h" 22#include "video_core/renderer_opengl/gl_shader_util.h"
23#include "video_core/renderer_opengl/gl_shaders.h" 23#include "video_core/renderer_opengl/gl_shaders.h"
24 24
25#include "video_core/debug_utils/debug_utils.h"
26
25/** 27/**
26 * Vertex structure that the drawn screen rectangles are composed of. 28 * Vertex structure that the drawn screen rectangles are composed of.
27 */ 29 */
@@ -129,6 +131,10 @@ void RendererOpenGL::SwapBuffers() {
129 hw_rasterizer->Reset(); 131 hw_rasterizer->Reset();
130 } 132 }
131 } 133 }
134
135 if (Pica::g_debug_context && Pica::g_debug_context->recorder) {
136 Pica::g_debug_context->recorder->FrameFinished();
137 }
132} 138}
133 139
134/** 140/**