diff options
| -rw-r--r-- | src/citra_qt/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/citra_qt/debugger/graphics_breakpoint_observer.h | 2 | ||||
| -rw-r--r-- | src/citra_qt/debugger/graphics_framebuffer.cpp | 62 | ||||
| -rw-r--r-- | src/citra_qt/debugger/graphics_framebuffer.h | 4 | ||||
| -rw-r--r-- | src/citra_qt/debugger/graphics_tracing.cpp | 170 | ||||
| -rw-r--r-- | src/citra_qt/debugger/graphics_tracing.h | 32 | ||||
| -rw-r--r-- | src/citra_qt/main.cpp | 9 | ||||
| -rw-r--r-- | src/common/color.h | 27 | ||||
| -rw-r--r-- | src/common/file_util.h | 10 | ||||
| -rw-r--r-- | src/core/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/core/hle/service/gsp_gpu.cpp | 2 | ||||
| -rw-r--r-- | src/core/hw/gpu.cpp | 78 | ||||
| -rw-r--r-- | src/core/hw/hw.cpp | 30 | ||||
| -rw-r--r-- | src/core/hw/lcd.cpp | 10 | ||||
| -rw-r--r-- | src/core/tracer/citrace.h | 101 | ||||
| -rw-r--r-- | src/core/tracer/recorder.cpp | 187 | ||||
| -rw-r--r-- | src/core/tracer/recorder.h | 90 | ||||
| -rw-r--r-- | src/video_core/command_processor.cpp | 60 | ||||
| -rw-r--r-- | src/video_core/debug_utils/debug_utils.h | 4 | ||||
| -rw-r--r-- | src/video_core/pica.h | 43 | ||||
| -rw-r--r-- | src/video_core/rasterizer.cpp | 142 | ||||
| -rw-r--r-- | src/video_core/renderer_opengl/renderer_opengl.cpp | 6 |
22 files changed, 1023 insertions, 51 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 | */ |
| 16 | class BreakPointObserverDock : public QDockWidget, private Pica::DebugContext::BreakPointObserver { | 16 | class BreakPointObserverDock : public QDockWidget, protected Pica::DebugContext::BreakPointObserver { |
| 17 | Q_OBJECT | 17 | Q_OBJECT |
| 18 | 18 | ||
| 19 | public: | 19 | public: |
diff --git a/src/citra_qt/debugger/graphics_framebuffer.cpp b/src/citra_qt/debugger/graphics_framebuffer.cpp index 6bbe7572c..39eefbf75 100644 --- a/src/citra_qt/debugger/graphics_framebuffer.cpp +++ b/src/citra_qt/debugger/graphics_framebuffer.cpp | |||
| @@ -55,7 +55,9 @@ GraphicsFramebufferWidget::GraphicsFramebufferWidget(std::shared_ptr<Pica::Debug | |||
| 55 | framebuffer_format_control->addItem(tr("RGBA4")); | 55 | framebuffer_format_control->addItem(tr("RGBA4")); |
| 56 | framebuffer_format_control->addItem(tr("D16")); | 56 | framebuffer_format_control->addItem(tr("D16")); |
| 57 | framebuffer_format_control->addItem(tr("D24")); | 57 | framebuffer_format_control->addItem(tr("D24")); |
| 58 | framebuffer_format_control->addItem(tr("D24S8")); | 58 | framebuffer_format_control->addItem(tr("D24X8")); |
| 59 | framebuffer_format_control->addItem(tr("X24S8")); | ||
| 60 | framebuffer_format_control->addItem(tr("(unknown)")); | ||
| 59 | 61 | ||
| 60 | // TODO: This QLabel should shrink the image to the available space rather than just expanding... | 62 | // TODO: This QLabel should shrink the image to the available space rather than just expanding... |
| 61 | framebuffer_picture_label = new QLabel; | 63 | framebuffer_picture_label = new QLabel; |
| @@ -184,8 +186,32 @@ void GraphicsFramebufferWidget::OnUpdate() | |||
| 184 | framebuffer_address = framebuffer.GetColorBufferPhysicalAddress(); | 186 | framebuffer_address = framebuffer.GetColorBufferPhysicalAddress(); |
| 185 | framebuffer_width = framebuffer.GetWidth(); | 187 | framebuffer_width = framebuffer.GetWidth(); |
| 186 | framebuffer_height = framebuffer.GetHeight(); | 188 | framebuffer_height = framebuffer.GetHeight(); |
| 187 | // TODO: It's unknown how this format is actually specified | 189 | |
| 188 | framebuffer_format = Format::RGBA8; | 190 | switch (framebuffer.color_format) { |
| 191 | case Pica::Regs::ColorFormat::RGBA8: | ||
| 192 | framebuffer_format = Format::RGBA8; | ||
| 193 | break; | ||
| 194 | |||
| 195 | case Pica::Regs::ColorFormat::RGB8: | ||
| 196 | framebuffer_format = Format::RGB8; | ||
| 197 | break; | ||
| 198 | |||
| 199 | case Pica::Regs::ColorFormat::RGB5A1: | ||
| 200 | framebuffer_format = Format::RGB5A1; | ||
| 201 | break; | ||
| 202 | |||
| 203 | case Pica::Regs::ColorFormat::RGB565: | ||
| 204 | framebuffer_format = Format::RGB565; | ||
| 205 | break; | ||
| 206 | |||
| 207 | case Pica::Regs::ColorFormat::RGBA4: | ||
| 208 | framebuffer_format = Format::RGBA4; | ||
| 209 | break; | ||
| 210 | |||
| 211 | default: | ||
| 212 | framebuffer_format = Format::Unknown; | ||
| 213 | break; | ||
| 214 | } | ||
| 189 | 215 | ||
| 190 | break; | 216 | break; |
| 191 | } | 217 | } |
| @@ -197,7 +223,24 @@ void GraphicsFramebufferWidget::OnUpdate() | |||
| 197 | framebuffer_address = framebuffer.GetDepthBufferPhysicalAddress(); | 223 | framebuffer_address = framebuffer.GetDepthBufferPhysicalAddress(); |
| 198 | framebuffer_width = framebuffer.GetWidth(); | 224 | framebuffer_width = framebuffer.GetWidth(); |
| 199 | framebuffer_height = framebuffer.GetHeight(); | 225 | framebuffer_height = framebuffer.GetHeight(); |
| 200 | framebuffer_format = Format::D16; | 226 | |
| 227 | switch (framebuffer.depth_format) { | ||
| 228 | case Pica::Regs::DepthFormat::D16: | ||
| 229 | framebuffer_format = Format::D16; | ||
| 230 | break; | ||
| 231 | |||
| 232 | case Pica::Regs::DepthFormat::D24: | ||
| 233 | framebuffer_format = Format::D24; | ||
| 234 | break; | ||
| 235 | |||
| 236 | case Pica::Regs::DepthFormat::D24S8: | ||
| 237 | framebuffer_format = Format::D24X8; | ||
| 238 | break; | ||
| 239 | |||
| 240 | default: | ||
| 241 | framebuffer_format = Format::Unknown; | ||
| 242 | break; | ||
| 243 | } | ||
| 201 | 244 | ||
| 202 | break; | 245 | break; |
| 203 | } | 246 | } |
| @@ -258,7 +301,7 @@ void GraphicsFramebufferWidget::OnUpdate() | |||
| 258 | color.b() = (data >> 16) & 0xFF; | 301 | color.b() = (data >> 16) & 0xFF; |
| 259 | break; | 302 | break; |
| 260 | } | 303 | } |
| 261 | case Format::D24S8: | 304 | case Format::D24X8: |
| 262 | { | 305 | { |
| 263 | Math::Vec2<u32> data = Color::DecodeD24S8(pixel); | 306 | Math::Vec2<u32> data = Color::DecodeD24S8(pixel); |
| 264 | color.r() = data.x & 0xFF; | 307 | color.r() = data.x & 0xFF; |
| @@ -266,6 +309,12 @@ void GraphicsFramebufferWidget::OnUpdate() | |||
| 266 | color.b() = (data.x >> 16) & 0xFF; | 309 | color.b() = (data.x >> 16) & 0xFF; |
| 267 | break; | 310 | break; |
| 268 | } | 311 | } |
| 312 | case Format::X24S8: | ||
| 313 | { | ||
| 314 | Math::Vec2<u32> data = Color::DecodeD24S8(pixel); | ||
| 315 | color.r() = color.g() = color.b() = data.y; | ||
| 316 | break; | ||
| 317 | } | ||
| 269 | default: | 318 | default: |
| 270 | qDebug() << "Unknown fb color format " << static_cast<int>(framebuffer_format); | 319 | qDebug() << "Unknown fb color format " << static_cast<int>(framebuffer_format); |
| 271 | break; | 320 | break; |
| @@ -286,7 +335,8 @@ void GraphicsFramebufferWidget::OnUpdate() | |||
| 286 | u32 GraphicsFramebufferWidget::BytesPerPixel(GraphicsFramebufferWidget::Format format) { | 335 | u32 GraphicsFramebufferWidget::BytesPerPixel(GraphicsFramebufferWidget::Format format) { |
| 287 | switch (format) { | 336 | switch (format) { |
| 288 | case Format::RGBA8: | 337 | case Format::RGBA8: |
| 289 | case Format::D24S8: | 338 | case Format::D24X8: |
| 339 | case Format::X24S8: | ||
| 290 | return 4; | 340 | return 4; |
| 291 | case Format::RGB8: | 341 | case Format::RGB8: |
| 292 | case Format::D24: | 342 | case Format::D24: |
diff --git a/src/citra_qt/debugger/graphics_framebuffer.h b/src/citra_qt/debugger/graphics_framebuffer.h index 4cb396ffe..e9eae679f 100644 --- a/src/citra_qt/debugger/graphics_framebuffer.h +++ b/src/citra_qt/debugger/graphics_framebuffer.h | |||
| @@ -35,7 +35,9 @@ class GraphicsFramebufferWidget : public BreakPointObserverDock { | |||
| 35 | RGBA4 = 4, | 35 | RGBA4 = 4, |
| 36 | D16 = 5, | 36 | D16 = 5, |
| 37 | D24 = 6, | 37 | D24 = 6, |
| 38 | D24S8 = 7 | 38 | D24X8 = 7, |
| 39 | X24S8 = 8, | ||
| 40 | Unknown = 9 | ||
| 39 | }; | 41 | }; |
| 40 | 42 | ||
| 41 | static u32 BytesPerPixel(Format format); | 43 | static u32 BytesPerPixel(Format format); |
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 | |||
| 26 | GraphicsTracingWidget::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 | |||
| 59 | void 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 | |||
| 101 | void 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 | |||
| 122 | void 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 | |||
| 134 | void GraphicsTracingWidget::OnBreakPointHit(Pica::DebugContext::Event event, void* data) { | ||
| 135 | widget()->setEnabled(true); | ||
| 136 | } | ||
| 137 | |||
| 138 | void GraphicsTracingWidget::OnResumed() { | ||
| 139 | widget()->setEnabled(false); | ||
| 140 | } | ||
| 141 | |||
| 142 | void GraphicsTracingWidget::OnEmulationStarting(EmuThread* emu_thread) { | ||
| 143 | // Disable tracing starting/stopping until a GPU breakpoint is reached | ||
| 144 | widget()->setEnabled(false); | ||
| 145 | } | ||
| 146 | |||
| 147 | void 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 | } | ||
diff --git a/src/citra_qt/debugger/graphics_tracing.h b/src/citra_qt/debugger/graphics_tracing.h new file mode 100644 index 000000000..2a0e4819b --- /dev/null +++ b/src/citra_qt/debugger/graphics_tracing.h | |||
| @@ -0,0 +1,32 @@ | |||
| 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 | |||
| 9 | class EmuThread; | ||
| 10 | |||
| 11 | class GraphicsTracingWidget : public BreakPointObserverDock { | ||
| 12 | Q_OBJECT | ||
| 13 | |||
| 14 | public: | ||
| 15 | GraphicsTracingWidget(std::shared_ptr<Pica::DebugContext> debug_context, QWidget* parent = nullptr); | ||
| 16 | |||
| 17 | private slots: | ||
| 18 | void StartRecording(); | ||
| 19 | void StopRecording(); | ||
| 20 | void AbortRecording(); | ||
| 21 | |||
| 22 | void OnBreakPointHit(Pica::DebugContext::Event event, void* data) override; | ||
| 23 | void OnResumed() override; | ||
| 24 | |||
| 25 | void OnEmulationStarting(EmuThread* emu_thread); | ||
| 26 | void OnEmulationStopping(); | ||
| 27 | |||
| 28 | signals: | ||
| 29 | void SetStartTracingButtonEnabled(bool enable); | ||
| 30 | void SetStopTracingButtonEnabled(bool enable); | ||
| 31 | void SetAbortTracingButtonEnabled(bool enable); | ||
| 32 | }; | ||
diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp index d23bafafc..2746de779 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 |
| @@ -148,6 +154,9 @@ GMainWindow::GMainWindow() : emu_thread(nullptr) | |||
| 148 | connect(this, SIGNAL(EmulationStopping()), registersWidget, SLOT(OnEmulationStopping())); | 154 | connect(this, SIGNAL(EmulationStopping()), registersWidget, SLOT(OnEmulationStopping())); |
| 149 | connect(this, SIGNAL(EmulationStarting(EmuThread*)), render_window, SLOT(OnEmulationStarting(EmuThread*))); | 155 | connect(this, SIGNAL(EmulationStarting(EmuThread*)), render_window, SLOT(OnEmulationStarting(EmuThread*))); |
| 150 | connect(this, SIGNAL(EmulationStopping()), render_window, SLOT(OnEmulationStopping())); | 156 | connect(this, SIGNAL(EmulationStopping()), render_window, SLOT(OnEmulationStopping())); |
| 157 | connect(this, SIGNAL(EmulationStarting(EmuThread*)), graphicsTracingWidget, SLOT(OnEmulationStarting(EmuThread*))); | ||
| 158 | connect(this, SIGNAL(EmulationStopping()), graphicsTracingWidget, SLOT(OnEmulationStopping())); | ||
| 159 | |||
| 151 | 160 | ||
| 152 | // Setup hotkeys | 161 | // Setup hotkeys |
| 153 | RegisterHotkey("Main Window", "Load File", QKeySequence::Open); | 162 | RegisterHotkey("Main Window", "Load File", QKeySequence::Open); |
diff --git a/src/common/color.h b/src/common/color.h index 422fdc8af..9dafdca0c 100644 --- a/src/common/color.h +++ b/src/common/color.h | |||
| @@ -208,7 +208,32 @@ inline void EncodeD24(u32 value, u8* bytes) { | |||
| 208 | * @param bytes Pointer where to store the encoded value | 208 | * @param bytes Pointer where to store the encoded value |
| 209 | */ | 209 | */ |
| 210 | inline void EncodeD24S8(u32 depth, u8 stencil, u8* bytes) { | 210 | inline void EncodeD24S8(u32 depth, u8 stencil, u8* bytes) { |
| 211 | *reinterpret_cast<u32_le*>(bytes) = (stencil << 24) | depth; | 211 | bytes[0] = depth & 0xFF; |
| 212 | bytes[1] = (depth >> 8) & 0xFF; | ||
| 213 | bytes[2] = (depth >> 16) & 0xFF; | ||
| 214 | bytes[3] = stencil; | ||
| 215 | } | ||
| 216 | |||
| 217 | /** | ||
| 218 | * Encode a 24 bit depth value as D24X8 format (32 bits per pixel with 8 bits unused) | ||
| 219 | * @param depth 24 bit source depth value to encode | ||
| 220 | * @param bytes Pointer where to store the encoded value | ||
| 221 | * @note unused bits will not be modified | ||
| 222 | */ | ||
| 223 | inline void EncodeD24X8(u32 depth, u8* bytes) { | ||
| 224 | bytes[0] = depth & 0xFF; | ||
| 225 | bytes[1] = (depth >> 8) & 0xFF; | ||
| 226 | bytes[2] = (depth >> 16) & 0xFF; | ||
| 227 | } | ||
| 228 | |||
| 229 | /** | ||
| 230 | * Encode an 8 bit stencil value as X24S8 format (32 bits per pixel with 24 bits unused) | ||
| 231 | * @param stencil 8 bit source stencil value to encode | ||
| 232 | * @param bytes Pointer where to store the encoded value | ||
| 233 | * @note unused bits will not be modified | ||
| 234 | */ | ||
| 235 | inline void EncodeX24S8(u8 stencil, u8* bytes) { | ||
| 236 | bytes[3] = stencil; | ||
| 212 | } | 237 | } |
| 213 | 238 | ||
| 214 | } // namespace | 239 | } // namespace |
diff --git a/src/common/file_util.h b/src/common/file_util.h index 8fe772aee..9637d1b85 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -181,6 +181,10 @@ public: | |||
| 181 | template <typename T> | 181 | template <typename T> |
| 182 | size_t WriteArray(const T* data, size_t length) | 182 | size_t WriteArray(const T* data, size_t length) |
| 183 | { | 183 | { |
| 184 | static_assert(std::is_standard_layout<T>::value, "Given array does not consist of standard layout objects"); | ||
| 185 | // TODO: gcc 4.8 does not support is_trivially_copyable, but we really should check for it here. | ||
| 186 | //static_assert(std::is_trivially_copyable<T>::value, "Given array does not consist of trivially copyable objects"); | ||
| 187 | |||
| 184 | if (!IsOpen()) { | 188 | if (!IsOpen()) { |
| 185 | m_good = false; | 189 | m_good = false; |
| 186 | return -1; | 190 | return -1; |
| @@ -203,6 +207,12 @@ public: | |||
| 203 | return WriteArray(reinterpret_cast<const char*>(data), length); | 207 | return WriteArray(reinterpret_cast<const char*>(data), length); |
| 204 | } | 208 | } |
| 205 | 209 | ||
| 210 | template<typename T> | ||
| 211 | size_t WriteObject(const T& object) { | ||
| 212 | static_assert(!std::is_pointer<T>::value, "Given object is a pointer"); | ||
| 213 | return WriteArray(&object, 1); | ||
| 214 | } | ||
| 215 | |||
| 206 | bool IsOpen() { return nullptr != m_file; } | 216 | bool IsOpen() { return nullptr != m_file; } |
| 207 | 217 | ||
| 208 | // m_good is set to false when a read, write or other function fails | 218 | // m_good is set to false when a read, write or other function fails |
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 9b004440c..8267ee586 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/citrace.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 |
| 350 | static void ExecuteCommand(const Command& command, u32 thread_id) { | 350 | static 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 a1789f9c7..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 | |||
| 30 | namespace GPU { | 35 | namespace GPU { |
| 31 | 36 | ||
| 32 | Regs g_regs; | 37 | Regs g_regs; |
| @@ -101,39 +106,43 @@ inline void Write(u32 addr, const T data) { | |||
| 101 | const bool is_second_filler = (index != GPU_REG_INDEX(memory_fill_config[0].trigger)); | 106 | const bool is_second_filler = (index != GPU_REG_INDEX(memory_fill_config[0].trigger)); |
| 102 | auto& config = g_regs.memory_fill_config[is_second_filler]; | 107 | auto& config = g_regs.memory_fill_config[is_second_filler]; |
| 103 | 108 | ||
| 104 | if (config.address_start && config.trigger) { | 109 | if (config.trigger) { |
| 105 | u8* start = Memory::GetPhysicalPointer(config.GetStartAddress()); | 110 | if (config.address_start) { // Some games pass invalid values here |
| 106 | u8* end = Memory::GetPhysicalPointer(config.GetEndAddress()); | 111 | u8* start = Memory::GetPhysicalPointer(config.GetStartAddress()); |
| 107 | 112 | u8* end = Memory::GetPhysicalPointer(config.GetEndAddress()); | |
| 108 | if (config.fill_24bit) { | 113 | |
| 109 | // fill with 24-bit values | 114 | if (config.fill_24bit) { |
| 110 | for (u8* ptr = start; ptr < end; ptr += 3) { | 115 | // fill with 24-bit values |
| 111 | ptr[0] = config.value_24bit_r; | 116 | for (u8* ptr = start; ptr < end; ptr += 3) { |
| 112 | ptr[1] = config.value_24bit_g; | 117 | ptr[0] = config.value_24bit_r; |
| 113 | ptr[2] = config.value_24bit_b; | 118 | ptr[1] = config.value_24bit_g; |
| 119 | ptr[2] = config.value_24bit_b; | ||
| 120 | } | ||
| 121 | } else if (config.fill_32bit) { | ||
| 122 | // fill with 32-bit values | ||
| 123 | for (u32* ptr = (u32*)start; ptr < (u32*)end; ++ptr) | ||
| 124 | *ptr = config.value_32bit; | ||
| 125 | } else { | ||
| 126 | // fill with 16-bit values | ||
| 127 | for (u16* ptr = (u16*)start; ptr < (u16*)end; ++ptr) | ||
| 128 | *ptr = config.value_16bit; | ||
| 114 | } | 129 | } |
| 115 | } else if (config.fill_32bit) { | ||
| 116 | // fill with 32-bit values | ||
| 117 | for (u32* ptr = (u32*)start; ptr < (u32*)end; ++ptr) | ||
| 118 | *ptr = config.value_32bit; | ||
| 119 | } else { | ||
| 120 | // fill with 16-bit values | ||
| 121 | for (u16* ptr = (u16*)start; ptr < (u16*)end; ++ptr) | ||
| 122 | *ptr = config.value_16bit; | ||
| 123 | } | ||
| 124 | 130 | ||
| 125 | LOG_TRACE(HW_GPU, "MemoryFill from 0x%08x to 0x%08x", config.GetStartAddress(), config.GetEndAddress()); | 131 | LOG_TRACE(HW_GPU, "MemoryFill from 0x%08x to 0x%08x", config.GetStartAddress(), config.GetEndAddress()); |
| 126 | 132 | ||
| 127 | config.trigger = 0; | 133 | if (!is_second_filler) { |
| 128 | config.finished = 1; | 134 | GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PSC0); |
| 135 | } else { | ||
| 136 | GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PSC1); | ||
| 137 | } | ||
| 129 | 138 | ||
| 130 | if (!is_second_filler) { | 139 | VideoCore::g_renderer->hw_rasterizer->NotifyFlush(config.GetStartAddress(), config.GetEndAddress() - config.GetStartAddress()); |
| 131 | GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PSC0); | ||
| 132 | } else { | ||
| 133 | GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PSC1); | ||
| 134 | } | 140 | } |
| 135 | 141 | ||
| 136 | VideoCore::g_renderer->hw_rasterizer->NotifyFlush(config.GetStartAddress(), config.GetEndAddress() - config.GetStartAddress()); | 142 | // Reset "trigger" flag and set the "finish" flag |
| 143 | // NOTE: This was confirmed to happen on hardware even if "address_start" is zero. | ||
| 144 | config.trigger = 0; | ||
| 145 | config.finished = 1; | ||
| 137 | } | 146 | } |
| 138 | break; | 147 | break; |
| 139 | } | 148 | } |
| @@ -270,6 +279,7 @@ inline void Write(u32 addr, const T data) { | |||
| 270 | config.GetPhysicalOutputAddress(), output_width, output_height, | 279 | config.GetPhysicalOutputAddress(), output_width, output_height, |
| 271 | config.output_format.Value(), config.flags); | 280 | config.output_format.Value(), config.flags); |
| 272 | 281 | ||
| 282 | g_regs.display_transfer_config.trigger = 0; | ||
| 273 | GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PPF); | 283 | GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PPF); |
| 274 | 284 | ||
| 275 | VideoCore::g_renderer->hw_rasterizer->NotifyFlush(config.GetPhysicalOutputAddress(), output_size); | 285 | VideoCore::g_renderer->hw_rasterizer->NotifyFlush(config.GetPhysicalOutputAddress(), output_size); |
| @@ -284,7 +294,14 @@ inline void Write(u32 addr, const T data) { | |||
| 284 | if (config.trigger & 1) | 294 | if (config.trigger & 1) |
| 285 | { | 295 | { |
| 286 | 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 | |||
| 287 | Pica::CommandProcessor::ProcessCommandList(buffer, config.size); | 302 | Pica::CommandProcessor::ProcessCommandList(buffer, config.size); |
| 303 | |||
| 304 | g_regs.command_processor_config.trigger = 0; | ||
| 288 | } | 305 | } |
| 289 | break; | 306 | break; |
| 290 | } | 307 | } |
| @@ -292,6 +309,13 @@ inline void Write(u32 addr, const T data) { | |||
| 292 | default: | 309 | default: |
| 293 | break; | 310 | break; |
| 294 | } | 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 | } | ||
| 295 | } | 319 | } |
| 296 | 320 | ||
| 297 | // 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/hw.cpp b/src/core/hw/hw.cpp index c7006a498..b5fdbf9c1 100644 --- a/src/core/hw/hw.cpp +++ b/src/core/hw/hw.cpp | |||
| @@ -15,6 +15,21 @@ template <typename T> | |||
| 15 | inline void Read(T &var, const u32 addr) { | 15 | inline void Read(T &var, const u32 addr) { |
| 16 | switch (addr & 0xFFFFF000) { | 16 | switch (addr & 0xFFFFF000) { |
| 17 | case VADDR_GPU: | 17 | case VADDR_GPU: |
| 18 | case VADDR_GPU + 0x1000: | ||
| 19 | case VADDR_GPU + 0x2000: | ||
| 20 | case VADDR_GPU + 0x3000: | ||
| 21 | case VADDR_GPU + 0x4000: | ||
| 22 | case VADDR_GPU + 0x5000: | ||
| 23 | case VADDR_GPU + 0x6000: | ||
| 24 | case VADDR_GPU + 0x7000: | ||
| 25 | case VADDR_GPU + 0x8000: | ||
| 26 | case VADDR_GPU + 0x9000: | ||
| 27 | case VADDR_GPU + 0xA000: | ||
| 28 | case VADDR_GPU + 0xB000: | ||
| 29 | case VADDR_GPU + 0xC000: | ||
| 30 | case VADDR_GPU + 0xD000: | ||
| 31 | case VADDR_GPU + 0xE000: | ||
| 32 | case VADDR_GPU + 0xF000: | ||
| 18 | GPU::Read(var, addr); | 33 | GPU::Read(var, addr); |
| 19 | break; | 34 | break; |
| 20 | case VADDR_LCD: | 35 | case VADDR_LCD: |
| @@ -29,6 +44,21 @@ template <typename T> | |||
| 29 | inline void Write(u32 addr, const T data) { | 44 | inline void Write(u32 addr, const T data) { |
| 30 | switch (addr & 0xFFFFF000) { | 45 | switch (addr & 0xFFFFF000) { |
| 31 | case VADDR_GPU: | 46 | case VADDR_GPU: |
| 47 | case VADDR_GPU + 0x1000: | ||
| 48 | case VADDR_GPU + 0x2000: | ||
| 49 | case VADDR_GPU + 0x3000: | ||
| 50 | case VADDR_GPU + 0x4000: | ||
| 51 | case VADDR_GPU + 0x5000: | ||
| 52 | case VADDR_GPU + 0x6000: | ||
| 53 | case VADDR_GPU + 0x7000: | ||
| 54 | case VADDR_GPU + 0x8000: | ||
| 55 | case VADDR_GPU + 0x9000: | ||
| 56 | case VADDR_GPU + 0xA000: | ||
| 57 | case VADDR_GPU + 0xB000: | ||
| 58 | case VADDR_GPU + 0xC000: | ||
| 59 | case VADDR_GPU + 0xD000: | ||
| 60 | case VADDR_GPU + 0xE000: | ||
| 61 | case VADDR_GPU + 0xF000: | ||
| 32 | GPU::Write(addr, data); | 62 | GPU::Write(addr, data); |
| 33 | break; | 63 | break; |
| 34 | case VADDR_LCD: | 64 | case VADDR_LCD: |
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 | |||
| 13 | namespace LCD { | 16 | namespace LCD { |
| 14 | 17 | ||
| 15 | Regs g_regs; | 18 | Regs 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/citrace.h b/src/core/tracer/citrace.h new file mode 100644 index 000000000..5deb6ce9e --- /dev/null +++ b/src/core/tracer/citrace.h | |||
| @@ -0,0 +1,101 @@ | |||
| 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 | |||
| 9 | namespace CiTrace { | ||
| 10 | |||
| 11 | // NOTE: Things are stored in little-endian | ||
| 12 | |||
| 13 | #pragma pack(1) | ||
| 14 | |||
| 15 | struct 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 default_attributes; | ||
| 39 | uint32_t default_attributes_size; | ||
| 40 | uint32_t vs_program_binary; | ||
| 41 | uint32_t vs_program_binary_size; | ||
| 42 | uint32_t vs_swizzle_data; | ||
| 43 | uint32_t vs_swizzle_data_size; | ||
| 44 | uint32_t vs_float_uniforms; | ||
| 45 | uint32_t vs_float_uniforms_size; | ||
| 46 | uint32_t gs_program_binary; | ||
| 47 | uint32_t gs_program_binary_size; | ||
| 48 | uint32_t gs_swizzle_data; | ||
| 49 | uint32_t gs_swizzle_data_size; | ||
| 50 | uint32_t gs_float_uniforms; | ||
| 51 | uint32_t gs_float_uniforms_size; | ||
| 52 | |||
| 53 | // Other things we might want to store here: | ||
| 54 | // - Initial framebuffer data, maybe even a full copy of FCRAM/VRAM | ||
| 55 | // - Lookup tables for fragment lighting | ||
| 56 | // - Lookup tables for procedural textures | ||
| 57 | } initial_state_offsets; | ||
| 58 | |||
| 59 | uint32_t stream_offset; | ||
| 60 | uint32_t stream_size; | ||
| 61 | }; | ||
| 62 | |||
| 63 | enum CTStreamElementType : uint32_t { | ||
| 64 | FrameMarker = 0xE1, | ||
| 65 | MemoryLoad = 0xE2, | ||
| 66 | RegisterWrite = 0xE3, | ||
| 67 | }; | ||
| 68 | |||
| 69 | struct CTMemoryLoad { | ||
| 70 | uint32_t file_offset; | ||
| 71 | uint32_t size; | ||
| 72 | uint32_t physical_address; | ||
| 73 | uint32_t pad; | ||
| 74 | }; | ||
| 75 | |||
| 76 | struct CTRegisterWrite { | ||
| 77 | uint32_t physical_address; | ||
| 78 | |||
| 79 | enum : uint32_t { | ||
| 80 | SIZE_8 = 0xD1, | ||
| 81 | SIZE_16 = 0xD2, | ||
| 82 | SIZE_32 = 0xD3, | ||
| 83 | SIZE_64 = 0xD4 | ||
| 84 | } size; | ||
| 85 | |||
| 86 | // TODO: Make it clearer which bits of this member are used for sizes other than 32 bits | ||
| 87 | uint64_t value; | ||
| 88 | }; | ||
| 89 | |||
| 90 | struct CTStreamElement { | ||
| 91 | CTStreamElementType type; | ||
| 92 | |||
| 93 | union { | ||
| 94 | CTMemoryLoad memory_load; | ||
| 95 | CTRegisterWrite register_write; | ||
| 96 | }; | ||
| 97 | }; | ||
| 98 | |||
| 99 | #pragma pack() | ||
| 100 | |||
| 101 | } | ||
diff --git a/src/core/tracer/recorder.cpp b/src/core/tracer/recorder.cpp new file mode 100644 index 000000000..656706c0c --- /dev/null +++ b/src/core/tracer/recorder.cpp | |||
| @@ -0,0 +1,187 @@ | |||
| 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 | |||
| 13 | namespace CiTrace { | ||
| 14 | |||
| 15 | Recorder::Recorder(const InitialState& initial_state) : initial_state(initial_state) { | ||
| 16 | |||
| 17 | } | ||
| 18 | |||
| 19 | void Recorder::Finish(const std::string& filename) { | ||
| 20 | // Setup CiTrace header | ||
| 21 | CTHeader header; | ||
| 22 | std::memcpy(header.magic, CTHeader::ExpectedMagicWord(), 4); | ||
| 23 | header.version = CTHeader::ExpectedVersion(); | ||
| 24 | header.header_size = sizeof(CTHeader); | ||
| 25 | |||
| 26 | // Calculate file offsets | ||
| 27 | auto& initial = header.initial_state_offsets; | ||
| 28 | |||
| 29 | initial.gpu_registers_size = initial_state.gpu_registers.size(); | ||
| 30 | initial.lcd_registers_size = initial_state.lcd_registers.size(); | ||
| 31 | initial.pica_registers_size = initial_state.pica_registers.size(); | ||
| 32 | initial.default_attributes_size = initial_state.default_attributes.size(); | ||
| 33 | initial.vs_program_binary_size = initial_state.vs_program_binary.size(); | ||
| 34 | initial.vs_swizzle_data_size = initial_state.vs_swizzle_data.size(); | ||
| 35 | initial.vs_float_uniforms_size = initial_state.vs_float_uniforms.size(); | ||
| 36 | initial.gs_program_binary_size = initial_state.gs_program_binary.size(); | ||
| 37 | initial.gs_swizzle_data_size = initial_state.gs_swizzle_data.size(); | ||
| 38 | initial.gs_float_uniforms_size = initial_state.gs_float_uniforms.size(); | ||
| 39 | header.stream_size = stream.size(); | ||
| 40 | |||
| 41 | initial.gpu_registers = sizeof(header); | ||
| 42 | initial.lcd_registers = initial.gpu_registers + initial.gpu_registers_size * sizeof(u32); | ||
| 43 | initial.pica_registers = initial.lcd_registers + initial.lcd_registers_size * sizeof(u32);; | ||
| 44 | initial.default_attributes = initial.pica_registers + initial.pica_registers_size * sizeof(u32); | ||
| 45 | initial.vs_program_binary = initial.default_attributes + initial.default_attributes_size * sizeof(u32); | ||
| 46 | initial.vs_swizzle_data = initial.vs_program_binary + initial.vs_program_binary_size * sizeof(u32); | ||
| 47 | initial.vs_float_uniforms = initial.vs_swizzle_data + initial.vs_swizzle_data_size * sizeof(u32); | ||
| 48 | initial.gs_program_binary = initial.vs_float_uniforms + initial.vs_float_uniforms_size * sizeof(u32); | ||
| 49 | initial.gs_swizzle_data = initial.gs_program_binary + initial.gs_program_binary_size * sizeof(u32); | ||
| 50 | initial.gs_float_uniforms = initial.gs_swizzle_data + initial.gs_swizzle_data_size * sizeof(u32); | ||
| 51 | header.stream_offset = initial.gs_float_uniforms + initial.gs_float_uniforms_size * sizeof(u32); | ||
| 52 | |||
| 53 | // Iterate through stream elements, update relevant stream element data | ||
| 54 | for (auto& stream_element : stream) { | ||
| 55 | switch (stream_element.data.type) { | ||
| 56 | case MemoryLoad: | ||
| 57 | { | ||
| 58 | auto& file_offset = memory_regions[stream_element.hash]; | ||
| 59 | if (!stream_element.uses_existing_data) { | ||
| 60 | file_offset = header.stream_offset; | ||
| 61 | } | ||
| 62 | stream_element.data.memory_load.file_offset = file_offset; | ||
| 63 | break; | ||
| 64 | } | ||
| 65 | |||
| 66 | default: | ||
| 67 | // Other commands don't use any extra data | ||
| 68 | DEBUG_ASSERT(stream_element.extra_data.size() == 0); | ||
| 69 | break; | ||
| 70 | } | ||
| 71 | header.stream_offset += stream_element.extra_data.size(); | ||
| 72 | } | ||
| 73 | |||
| 74 | try { | ||
| 75 | // Open file and write header | ||
| 76 | FileUtil::IOFile file(filename, "wb"); | ||
| 77 | size_t written = file.WriteObject(header); | ||
| 78 | if (written != 1 || file.Tell() != initial.gpu_registers) | ||
| 79 | throw "Failed to write header"; | ||
| 80 | |||
| 81 | // Write initial state | ||
| 82 | written = file.WriteArray(initial_state.gpu_registers.data(), initial_state.gpu_registers.size()); | ||
| 83 | if (written != initial_state.gpu_registers.size() || file.Tell() != initial.lcd_registers) | ||
| 84 | throw "Failed to write GPU registers"; | ||
| 85 | |||
| 86 | written = file.WriteArray(initial_state.lcd_registers.data(), initial_state.lcd_registers.size()); | ||
| 87 | if (written != initial_state.lcd_registers.size() || file.Tell() != initial.pica_registers) | ||
| 88 | throw "Failed to write LCD registers"; | ||
| 89 | |||
| 90 | written = file.WriteArray(initial_state.pica_registers.data(), initial_state.pica_registers.size()); | ||
| 91 | if (written != initial_state.pica_registers.size() || file.Tell() != initial.default_attributes) | ||
| 92 | throw "Failed to write Pica registers"; | ||
| 93 | |||
| 94 | written = file.WriteArray(initial_state.default_attributes.data(), initial_state.default_attributes.size()); | ||
| 95 | if (written != initial_state.default_attributes.size() || file.Tell() != initial.vs_program_binary) | ||
| 96 | throw "Failed to write default vertex attributes"; | ||
| 97 | |||
| 98 | written = file.WriteArray(initial_state.vs_program_binary.data(), initial_state.vs_program_binary.size()); | ||
| 99 | if (written != initial_state.vs_program_binary.size() || file.Tell() != initial.vs_swizzle_data) | ||
| 100 | throw "Failed to write vertex shader program binary"; | ||
| 101 | |||
| 102 | written = file.WriteArray(initial_state.vs_swizzle_data.data(), initial_state.vs_swizzle_data.size()); | ||
| 103 | if (written != initial_state.vs_swizzle_data.size() || file.Tell() != initial.vs_float_uniforms) | ||
| 104 | throw "Failed to write vertex shader swizzle data"; | ||
| 105 | |||
| 106 | written = file.WriteArray(initial_state.vs_float_uniforms.data(), initial_state.vs_float_uniforms.size()); | ||
| 107 | if (written != initial_state.vs_float_uniforms.size() || file.Tell() != initial.gs_program_binary) | ||
| 108 | throw "Failed to write vertex shader float uniforms"; | ||
| 109 | |||
| 110 | written = file.WriteArray(initial_state.gs_program_binary.data(), initial_state.gs_program_binary.size()); | ||
| 111 | if (written != initial_state.gs_program_binary.size() || file.Tell() != initial.gs_swizzle_data) | ||
| 112 | throw "Failed to write geomtry shader program binary"; | ||
| 113 | |||
| 114 | written = file.WriteArray(initial_state.gs_swizzle_data.data(), initial_state.gs_swizzle_data.size()); | ||
| 115 | if (written != initial_state.gs_swizzle_data.size() || file.Tell() != initial.gs_float_uniforms) | ||
| 116 | throw "Failed to write geometry shader swizzle data"; | ||
| 117 | |||
| 118 | written = file.WriteArray(initial_state.gs_float_uniforms.data(), initial_state.gs_float_uniforms.size()); | ||
| 119 | if (written != initial_state.gs_float_uniforms.size() || file.Tell() != initial.gs_float_uniforms + sizeof(u32) * initial.gs_float_uniforms_size) | ||
| 120 | throw "Failed to write geometry shader float uniforms"; | ||
| 121 | |||
| 122 | // Iterate through stream elements, write "extra data" | ||
| 123 | for (const auto& stream_element : stream) { | ||
| 124 | if (stream_element.extra_data.size() == 0) | ||
| 125 | continue; | ||
| 126 | |||
| 127 | written = file.WriteBytes(stream_element.extra_data.data(), stream_element.extra_data.size()); | ||
| 128 | if (written != stream_element.extra_data.size()) | ||
| 129 | throw "Failed to write extra data"; | ||
| 130 | } | ||
| 131 | |||
| 132 | if (file.Tell() != header.stream_offset) | ||
| 133 | throw "Unexpected end of extra data"; | ||
| 134 | |||
| 135 | // Write actual stream elements | ||
| 136 | for (const auto& stream_element : stream) { | ||
| 137 | if (1 != file.WriteObject(stream_element.data)) | ||
| 138 | throw "Failed to write stream element"; | ||
| 139 | } | ||
| 140 | } catch(const char* str) { | ||
| 141 | LOG_ERROR(HW_GPU, "Writing CiTrace file failed: %s", str); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | void Recorder::FrameFinished() { | ||
| 146 | stream.push_back( { FrameMarker } ); | ||
| 147 | } | ||
| 148 | |||
| 149 | void Recorder::MemoryAccessed(const u8* data, u32 size, u32 physical_address) { | ||
| 150 | StreamElement element = { MemoryLoad }; | ||
| 151 | element.data.memory_load.size = size; | ||
| 152 | element.data.memory_load.physical_address = physical_address; | ||
| 153 | |||
| 154 | // Compute hash over given memory region to check if the contents are already stored internally | ||
| 155 | boost::crc_32_type result; | ||
| 156 | result.process_bytes(data, size); | ||
| 157 | element.hash = result.checksum(); | ||
| 158 | |||
| 159 | element.uses_existing_data = (memory_regions.find(element.hash) != memory_regions.end()); | ||
| 160 | if (!element.uses_existing_data) { | ||
| 161 | element.extra_data.resize(size); | ||
| 162 | memcpy(element.extra_data.data(), data, size); | ||
| 163 | memory_regions.insert({element.hash, 0}); // file offset will be initialized in Finish() | ||
| 164 | } | ||
| 165 | |||
| 166 | stream.push_back(element); | ||
| 167 | } | ||
| 168 | |||
| 169 | template<typename T> | ||
| 170 | void Recorder::RegisterWritten(u32 physical_address, T value) { | ||
| 171 | StreamElement element = { RegisterWrite }; | ||
| 172 | element.data.register_write.size = (sizeof(T) == 1) ? CTRegisterWrite::SIZE_8 | ||
| 173 | : (sizeof(T) == 2) ? CTRegisterWrite::SIZE_16 | ||
| 174 | : (sizeof(T) == 4) ? CTRegisterWrite::SIZE_32 | ||
| 175 | : CTRegisterWrite::SIZE_64; | ||
| 176 | element.data.register_write.physical_address = physical_address; | ||
| 177 | element.data.register_write.value = value; | ||
| 178 | |||
| 179 | stream.push_back(element); | ||
| 180 | } | ||
| 181 | |||
| 182 | template void Recorder::RegisterWritten(u32,u8); | ||
| 183 | template void Recorder::RegisterWritten(u32,u16); | ||
| 184 | template void Recorder::RegisterWritten(u32,u32); | ||
| 185 | template void Recorder::RegisterWritten(u32,u64); | ||
| 186 | |||
| 187 | } | ||
diff --git a/src/core/tracer/recorder.h b/src/core/tracer/recorder.h new file mode 100644 index 000000000..6e4b70015 --- /dev/null +++ b/src/core/tracer/recorder.h | |||
| @@ -0,0 +1,90 @@ | |||
| 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 "citrace.h" | ||
| 15 | |||
| 16 | namespace CiTrace { | ||
| 17 | |||
| 18 | class Recorder { | ||
| 19 | public: | ||
| 20 | struct InitialState { | ||
| 21 | std::vector<u32> gpu_registers; | ||
| 22 | std::vector<u32> lcd_registers; | ||
| 23 | std::vector<u32> pica_registers; | ||
| 24 | std::vector<u32> default_attributes; | ||
| 25 | std::vector<u32> vs_program_binary; | ||
| 26 | std::vector<u32> vs_swizzle_data; | ||
| 27 | std::vector<u32> vs_float_uniforms; | ||
| 28 | std::vector<u32> gs_program_binary; | ||
| 29 | std::vector<u32> gs_swizzle_data; | ||
| 30 | std::vector<u32> gs_float_uniforms; | ||
| 31 | }; | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Recorder constructor | ||
| 35 | * @param default_attributes Pointer to an array of 32-bit-aligned 24-bit floating point values. | ||
| 36 | * @param vs_float_uniforms Pointer to an array of 32-bit-aligned 24-bit floating point values. | ||
| 37 | */ | ||
| 38 | Recorder(const InitialState& initial_state); | ||
| 39 | |||
| 40 | /// Finish recording of this Citrace and save it using the given filename. | ||
| 41 | void Finish(const std::string& filename); | ||
| 42 | |||
| 43 | /// Mark end of a frame | ||
| 44 | void FrameFinished(); | ||
| 45 | |||
| 46 | /** | ||
| 47 | * Store a copy of the given memory range in the recording. | ||
| 48 | * @note Use this whenever the GPU is about to access a particular memory region. | ||
| 49 | * @note The implementation will make sure to minimize redundant memory updates. | ||
| 50 | */ | ||
| 51 | void MemoryAccessed(const u8* data, u32 size, u32 physical_address); | ||
| 52 | |||
| 53 | /** | ||
| 54 | * Record a register write. | ||
| 55 | * @note Use this whenever a GPU-related MMIO register has been written to. | ||
| 56 | */ | ||
| 57 | template<typename T> | ||
| 58 | void RegisterWritten(u32 physical_address, T value); | ||
| 59 | |||
| 60 | private: | ||
| 61 | // Initial state of recording start | ||
| 62 | InitialState initial_state; | ||
| 63 | |||
| 64 | // Command stream | ||
| 65 | struct StreamElement { | ||
| 66 | CTStreamElement data; | ||
| 67 | |||
| 68 | /** | ||
| 69 | * Extra data to store along "core" data. | ||
| 70 | * This is e.g. used for data used in MemoryUpdates. | ||
| 71 | */ | ||
| 72 | std::vector<u8> extra_data; | ||
| 73 | |||
| 74 | /// Optional CRC hash (e.g. for hashing memory regions) | ||
| 75 | boost::crc_32_type::value_type hash; | ||
| 76 | |||
| 77 | /// If true, refer to data already written to the output file instead of extra_data | ||
| 78 | bool uses_existing_data; | ||
| 79 | }; | ||
| 80 | |||
| 81 | std::vector<StreamElement> stream; | ||
| 82 | |||
| 83 | /** | ||
| 84 | * Internal cache which maps hashes of memory contents to file offsets at which those memory | ||
| 85 | * contents are stored. | ||
| 86 | */ | ||
| 87 | std::unordered_map<boost::crc_32_type::value_type /*hash*/, u32 /*file_offset*/> memory_regions; | ||
| 88 | }; | ||
| 89 | |||
| 90 | } // namespace | ||
diff --git a/src/video_core/command_processor.cpp b/src/video_core/command_processor.cpp index 110caec76..2a1c885a7 100644 --- a/src/video_core/command_processor.cpp +++ b/src/video_core/command_processor.cpp | |||
| @@ -123,12 +123,55 @@ 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 | class { | ||
| 139 | /// Combine overlapping and close ranges | ||
| 140 | void SimplifyRanges() { | ||
| 141 | for (auto it = ranges.begin(); it != ranges.end(); ++it) { | ||
| 142 | // NOTE: We add 32 to the range end address to make sure "close" ranges are combined, too | ||
| 143 | auto it2 = std::next(it); | ||
| 144 | while (it2 != ranges.end() && it->first + it->second + 32 >= it2->first) { | ||
| 145 | it->second = std::max(it->second, it2->first + it2->second - it->first); | ||
| 146 | it2 = ranges.erase(it2); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | public: | ||
| 152 | /// Record a particular memory access in the list | ||
| 153 | void AddAccess(u32 paddr, u32 size) { | ||
| 154 | // Create new range or extend existing one | ||
| 155 | ranges[paddr] = std::max(ranges[paddr], size); | ||
| 156 | |||
| 157 | // Simplify ranges... | ||
| 158 | SimplifyRanges(); | ||
| 159 | } | ||
| 160 | |||
| 161 | /// Map of accessed ranges (mapping start address to range size) | ||
| 162 | std::map<u32, u32> ranges; | ||
| 163 | } memory_accesses; | ||
| 164 | |||
| 126 | for (unsigned int index = 0; index < regs.num_vertices; ++index) | 165 | for (unsigned int index = 0; index < regs.num_vertices; ++index) |
| 127 | { | 166 | { |
| 128 | unsigned int vertex = is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index]) : index; | 167 | unsigned int vertex = is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index]) : index; |
| 129 | 168 | ||
| 130 | if (is_indexed) { | 169 | if (is_indexed) { |
| 131 | // TODO: Implement some sort of vertex cache! | 170 | // TODO: Implement some sort of vertex cache! |
| 171 | if (g_debug_context && Pica::g_debug_context->recorder) { | ||
| 172 | int size = index_u16 ? 2 : 1; | ||
| 173 | memory_accesses.AddAccess(base_address + index_info.offset + size * index, size); | ||
| 174 | } | ||
| 132 | } | 175 | } |
| 133 | 176 | ||
| 134 | // Initialize data for the current vertex | 177 | // Initialize data for the current vertex |
| @@ -151,7 +194,14 @@ static inline void WritePicaReg(u32 id, u32 value, u32 mask) { | |||
| 151 | 194 | ||
| 152 | // Load per-vertex data from the loader arrays | 195 | // Load per-vertex data from the loader arrays |
| 153 | for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) { | 196 | 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]); | 197 | u32 source_addr = vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i]; |
| 198 | const u8* srcdata = Memory::GetPhysicalPointer(source_addr); | ||
| 199 | |||
| 200 | if (g_debug_context && Pica::g_debug_context->recorder) { | ||
| 201 | memory_accesses.AddAccess(source_addr, | ||
| 202 | (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::FLOAT) ? 4 | ||
| 203 | : (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::SHORT) ? 2 : 1); | ||
| 204 | } | ||
| 155 | 205 | ||
| 156 | const float srcval = (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::BYTE) ? *(s8*)srcdata : | 206 | const float srcval = (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::BYTE) ? *(s8*)srcdata : |
| 157 | (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::UBYTE) ? *(u8*)srcdata : | 207 | (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::UBYTE) ? *(u8*)srcdata : |
| @@ -213,14 +263,20 @@ static inline void WritePicaReg(u32 id, u32 value, u32 mask) { | |||
| 213 | } | 263 | } |
| 214 | } | 264 | } |
| 215 | 265 | ||
| 266 | for (auto& range : memory_accesses.ranges) { | ||
| 267 | g_debug_context->recorder->MemoryAccessed(Memory::GetPhysicalPointer(range.first), | ||
| 268 | range.second, range.first); | ||
| 269 | } | ||
| 270 | |||
| 216 | if (Settings::values.use_hw_renderer) { | 271 | if (Settings::values.use_hw_renderer) { |
| 217 | VideoCore::g_renderer->hw_rasterizer->DrawTriangles(); | 272 | VideoCore::g_renderer->hw_rasterizer->DrawTriangles(); |
| 218 | } | 273 | } |
| 219 | 274 | ||
| 220 | geometry_dumper.Dump(); | 275 | geometry_dumper.Dump(); |
| 221 | 276 | ||
| 222 | if (g_debug_context) | 277 | if (g_debug_context) { |
| 223 | g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr); | 278 | g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr); |
| 279 | } | ||
| 224 | 280 | ||
| 225 | break; | 281 | break; |
| 226 | } | 282 | } |
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 | ||
| 19 | namespace Pica { | 21 | namespace 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 | |||
| 132 | private: | 136 | private: |
| 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/pica.h b/src/video_core/pica.h index feb20214a..46a7b21dc 100644 --- a/src/video_core/pica.h +++ b/src/video_core/pica.h | |||
| @@ -420,6 +420,11 @@ struct Regs { | |||
| 420 | GreaterThanOrEqual = 7, | 420 | GreaterThanOrEqual = 7, |
| 421 | }; | 421 | }; |
| 422 | 422 | ||
| 423 | enum class StencilAction : u32 { | ||
| 424 | Keep = 0, | ||
| 425 | Xor = 5, | ||
| 426 | }; | ||
| 427 | |||
| 423 | struct { | 428 | struct { |
| 424 | union { | 429 | union { |
| 425 | // If false, logic blending is used | 430 | // If false, logic blending is used |
| @@ -454,15 +459,35 @@ struct Regs { | |||
| 454 | BitField< 8, 8, u32> ref; | 459 | BitField< 8, 8, u32> ref; |
| 455 | } alpha_test; | 460 | } alpha_test; |
| 456 | 461 | ||
| 457 | union { | 462 | struct { |
| 458 | BitField< 0, 1, u32> stencil_test_enable; | 463 | union { |
| 459 | BitField< 4, 3, CompareFunc> stencil_test_func; | 464 | // If true, enable stencil testing |
| 460 | BitField< 8, 8, u32> stencil_replacement_value; | 465 | BitField< 0, 1, u32> enable; |
| 461 | BitField<16, 8, u32> stencil_reference_value; | ||
| 462 | BitField<24, 8, u32> stencil_mask; | ||
| 463 | } stencil_test; | ||
| 464 | 466 | ||
| 465 | INSERT_PADDING_WORDS(0x1); | 467 | // Comparison operation for stencil testing |
| 468 | BitField< 4, 3, CompareFunc> func; | ||
| 469 | |||
| 470 | // Value to calculate the new stencil value from | ||
| 471 | BitField< 8, 8, u32> replacement_value; | ||
| 472 | |||
| 473 | // Value to compare against for stencil testing | ||
| 474 | BitField<16, 8, u32> reference_value; | ||
| 475 | |||
| 476 | // Mask to apply on stencil test inputs | ||
| 477 | BitField<24, 8, u32> mask; | ||
| 478 | }; | ||
| 479 | |||
| 480 | union { | ||
| 481 | // Action to perform when the stencil test fails | ||
| 482 | BitField< 0, 3, StencilAction> action_stencil_fail; | ||
| 483 | |||
| 484 | // Action to perform when stencil testing passed but depth testing fails | ||
| 485 | BitField< 4, 3, StencilAction> action_depth_fail; | ||
| 486 | |||
| 487 | // Action to perform when both stencil and depth testing pass | ||
| 488 | BitField< 8, 3, StencilAction> action_depth_pass; | ||
| 489 | }; | ||
| 490 | } stencil_test; | ||
| 466 | 491 | ||
| 467 | union { | 492 | union { |
| 468 | BitField< 0, 1, u32> depth_test_enable; | 493 | BitField< 0, 1, u32> depth_test_enable; |
| @@ -512,7 +537,7 @@ struct Regs { | |||
| 512 | struct { | 537 | struct { |
| 513 | INSERT_PADDING_WORDS(0x6); | 538 | INSERT_PADDING_WORDS(0x6); |
| 514 | 539 | ||
| 515 | DepthFormat depth_format; | 540 | DepthFormat depth_format; // TODO: Should be a BitField! |
| 516 | BitField<16, 3, ColorFormat> color_format; | 541 | BitField<16, 3, ColorFormat> color_format; |
| 517 | 542 | ||
| 518 | INSERT_PADDING_WORDS(0x4); | 543 | INSERT_PADDING_WORDS(0x4); |
diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp index 70b115744..c381c2bd9 100644 --- a/src/video_core/rasterizer.cpp +++ b/src/video_core/rasterizer.cpp | |||
| @@ -126,6 +126,30 @@ static u32 GetDepth(int x, int y) { | |||
| 126 | } | 126 | } |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | static u8 GetStencil(int x, int y) { | ||
| 130 | const auto& framebuffer = g_state.regs.framebuffer; | ||
| 131 | const PAddr addr = framebuffer.GetDepthBufferPhysicalAddress(); | ||
| 132 | u8* depth_buffer = Memory::GetPhysicalPointer(addr); | ||
| 133 | |||
| 134 | y = framebuffer.height - y; | ||
| 135 | |||
| 136 | const u32 coarse_y = y & ~7; | ||
| 137 | u32 bytes_per_pixel = Pica::Regs::BytesPerDepthPixel(framebuffer.depth_format); | ||
| 138 | u32 stride = framebuffer.width * bytes_per_pixel; | ||
| 139 | |||
| 140 | u32 src_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * stride; | ||
| 141 | u8* src_pixel = depth_buffer + src_offset; | ||
| 142 | |||
| 143 | switch (framebuffer.depth_format) { | ||
| 144 | case Regs::DepthFormat::D24S8: | ||
| 145 | return Color::DecodeD24S8(src_pixel).y; | ||
| 146 | |||
| 147 | default: | ||
| 148 | LOG_WARNING(HW_GPU, "GetStencil called for function which doesn't have a stencil component (format %u)", framebuffer.depth_format); | ||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 129 | static void SetDepth(int x, int y, u32 value) { | 153 | static void SetDepth(int x, int y, u32 value) { |
| 130 | const auto& framebuffer = g_state.regs.framebuffer; | 154 | const auto& framebuffer = g_state.regs.framebuffer; |
| 131 | const PAddr addr = framebuffer.GetDepthBufferPhysicalAddress(); | 155 | const PAddr addr = framebuffer.GetDepthBufferPhysicalAddress(); |
| @@ -144,13 +168,46 @@ static void SetDepth(int x, int y, u32 value) { | |||
| 144 | case Regs::DepthFormat::D16: | 168 | case Regs::DepthFormat::D16: |
| 145 | Color::EncodeD16(value, dst_pixel); | 169 | Color::EncodeD16(value, dst_pixel); |
| 146 | break; | 170 | break; |
| 171 | |||
| 147 | case Regs::DepthFormat::D24: | 172 | case Regs::DepthFormat::D24: |
| 148 | Color::EncodeD24(value, dst_pixel); | 173 | Color::EncodeD24(value, dst_pixel); |
| 149 | break; | 174 | break; |
| 175 | |||
| 150 | case Regs::DepthFormat::D24S8: | 176 | case Regs::DepthFormat::D24S8: |
| 151 | // TODO(Subv): Implement the stencil buffer | 177 | Color::EncodeD24X8(value, dst_pixel); |
| 152 | Color::EncodeD24S8(value, 0, dst_pixel); | ||
| 153 | break; | 178 | break; |
| 179 | |||
| 180 | default: | ||
| 181 | LOG_CRITICAL(HW_GPU, "Unimplemented depth format %u", framebuffer.depth_format); | ||
| 182 | UNIMPLEMENTED(); | ||
| 183 | break; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | static void SetStencil(int x, int y, u8 value) { | ||
| 188 | const auto& framebuffer = g_state.regs.framebuffer; | ||
| 189 | const PAddr addr = framebuffer.GetDepthBufferPhysicalAddress(); | ||
| 190 | u8* depth_buffer = Memory::GetPhysicalPointer(addr); | ||
| 191 | |||
| 192 | y = framebuffer.height - y; | ||
| 193 | |||
| 194 | const u32 coarse_y = y & ~7; | ||
| 195 | u32 bytes_per_pixel = Pica::Regs::BytesPerDepthPixel(framebuffer.depth_format); | ||
| 196 | u32 stride = framebuffer.width * bytes_per_pixel; | ||
| 197 | |||
| 198 | u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * stride; | ||
| 199 | u8* dst_pixel = depth_buffer + dst_offset; | ||
| 200 | |||
| 201 | switch (framebuffer.depth_format) { | ||
| 202 | case Pica::Regs::DepthFormat::D16: | ||
| 203 | case Pica::Regs::DepthFormat::D24: | ||
| 204 | // Nothing to do | ||
| 205 | break; | ||
| 206 | |||
| 207 | case Pica::Regs::DepthFormat::D24S8: | ||
| 208 | Color::EncodeX24S8(value, dst_pixel); | ||
| 209 | break; | ||
| 210 | |||
| 154 | default: | 211 | default: |
| 155 | LOG_CRITICAL(HW_GPU, "Unimplemented depth format %u", framebuffer.depth_format); | 212 | LOG_CRITICAL(HW_GPU, "Unimplemented depth format %u", framebuffer.depth_format); |
| 156 | UNIMPLEMENTED(); | 213 | UNIMPLEMENTED(); |
| @@ -158,6 +215,22 @@ static void SetDepth(int x, int y, u32 value) { | |||
| 158 | } | 215 | } |
| 159 | } | 216 | } |
| 160 | 217 | ||
| 218 | // TODO: Should the stencil mask be applied to the "dest" or "ref" operands? Most likely not! | ||
| 219 | static u8 PerformStencilAction(Regs::StencilAction action, u8 dest, u8 ref) { | ||
| 220 | switch (action) { | ||
| 221 | case Regs::StencilAction::Keep: | ||
| 222 | return dest; | ||
| 223 | |||
| 224 | case Regs::StencilAction::Xor: | ||
| 225 | return dest ^ ref; | ||
| 226 | |||
| 227 | default: | ||
| 228 | LOG_CRITICAL(HW_GPU, "Unknown stencil action %x", (int)action); | ||
| 229 | UNIMPLEMENTED(); | ||
| 230 | return 0; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 161 | // NOTE: Assuming that rasterizer coordinates are 12.4 fixed-point values | 234 | // NOTE: Assuming that rasterizer coordinates are 12.4 fixed-point values |
| 162 | struct Fix12P4 { | 235 | struct Fix12P4 { |
| 163 | Fix12P4() {} | 236 | Fix12P4() {} |
| @@ -276,6 +349,9 @@ static void ProcessTriangleInternal(const VertexShader::OutputVertex& v0, | |||
| 276 | auto textures = regs.GetTextures(); | 349 | auto textures = regs.GetTextures(); |
| 277 | auto tev_stages = regs.GetTevStages(); | 350 | auto tev_stages = regs.GetTevStages(); |
| 278 | 351 | ||
| 352 | bool stencil_action_enable = g_state.regs.output_merger.stencil_test.enable && g_state.regs.framebuffer.depth_format == Regs::DepthFormat::D24S8; | ||
| 353 | const auto stencil_test = g_state.regs.output_merger.stencil_test; | ||
| 354 | |||
| 279 | // Enter rasterization loop, starting at the center of the topleft bounding box corner. | 355 | // Enter rasterization loop, starting at the center of the topleft bounding box corner. |
| 280 | // TODO: Not sure if looping through x first might be faster | 356 | // TODO: Not sure if looping through x first might be faster |
| 281 | for (u16 y = min_y + 8; y < max_y; y += 0x10) { | 357 | for (u16 y = min_y + 8; y < max_y; y += 0x10) { |
| @@ -647,6 +723,7 @@ static void ProcessTriangleInternal(const VertexShader::OutputVertex& v0, | |||
| 647 | } | 723 | } |
| 648 | 724 | ||
| 649 | const auto& output_merger = regs.output_merger; | 725 | const auto& output_merger = regs.output_merger; |
| 726 | // TODO: Does alpha testing happen before or after stencil? | ||
| 650 | if (output_merger.alpha_test.enable) { | 727 | if (output_merger.alpha_test.enable) { |
| 651 | bool pass = false; | 728 | bool pass = false; |
| 652 | 729 | ||
| @@ -688,6 +765,54 @@ static void ProcessTriangleInternal(const VertexShader::OutputVertex& v0, | |||
| 688 | continue; | 765 | continue; |
| 689 | } | 766 | } |
| 690 | 767 | ||
| 768 | u8 old_stencil = 0; | ||
| 769 | if (stencil_action_enable) { | ||
| 770 | old_stencil = GetStencil(x >> 4, y >> 4); | ||
| 771 | u8 dest = old_stencil & stencil_test.mask; | ||
| 772 | u8 ref = stencil_test.reference_value & stencil_test.mask; | ||
| 773 | |||
| 774 | bool pass = false; | ||
| 775 | switch (stencil_test.func) { | ||
| 776 | case Regs::CompareFunc::Never: | ||
| 777 | pass = false; | ||
| 778 | break; | ||
| 779 | |||
| 780 | case Regs::CompareFunc::Always: | ||
| 781 | pass = true; | ||
| 782 | break; | ||
| 783 | |||
| 784 | case Regs::CompareFunc::Equal: | ||
| 785 | pass = (ref == dest); | ||
| 786 | break; | ||
| 787 | |||
| 788 | case Regs::CompareFunc::NotEqual: | ||
| 789 | pass = (ref != dest); | ||
| 790 | break; | ||
| 791 | |||
| 792 | case Regs::CompareFunc::LessThan: | ||
| 793 | pass = (ref < dest); | ||
| 794 | break; | ||
| 795 | |||
| 796 | case Regs::CompareFunc::LessThanOrEqual: | ||
| 797 | pass = (ref <= dest); | ||
| 798 | break; | ||
| 799 | |||
| 800 | case Regs::CompareFunc::GreaterThan: | ||
| 801 | pass = (ref > dest); | ||
| 802 | break; | ||
| 803 | |||
| 804 | case Regs::CompareFunc::GreaterThanOrEqual: | ||
| 805 | pass = (ref >= dest); | ||
| 806 | break; | ||
| 807 | } | ||
| 808 | |||
| 809 | if (!pass) { | ||
| 810 | u8 new_stencil = PerformStencilAction(stencil_test.action_stencil_fail, old_stencil, stencil_test.replacement_value); | ||
| 811 | SetStencil(x >> 4, y >> 4, new_stencil); | ||
| 812 | continue; | ||
| 813 | } | ||
| 814 | } | ||
| 815 | |||
| 691 | // TODO: Does depth indeed only get written even if depth testing is enabled? | 816 | // TODO: Does depth indeed only get written even if depth testing is enabled? |
| 692 | if (output_merger.depth_test_enable) { | 817 | if (output_merger.depth_test_enable) { |
| 693 | unsigned num_bits = Regs::DepthBitsPerPixel(regs.framebuffer.depth_format); | 818 | unsigned num_bits = Regs::DepthBitsPerPixel(regs.framebuffer.depth_format); |
| @@ -732,11 +857,22 @@ static void ProcessTriangleInternal(const VertexShader::OutputVertex& v0, | |||
| 732 | break; | 857 | break; |
| 733 | } | 858 | } |
| 734 | 859 | ||
| 735 | if (!pass) | 860 | if (!pass) { |
| 861 | if (stencil_action_enable) { | ||
| 862 | u8 new_stencil = PerformStencilAction(stencil_test.action_depth_fail, old_stencil, stencil_test.replacement_value); | ||
| 863 | SetStencil(x >> 4, y >> 4, new_stencil); | ||
| 864 | } | ||
| 736 | continue; | 865 | continue; |
| 866 | } | ||
| 737 | 867 | ||
| 738 | if (output_merger.depth_write_enable) | 868 | if (output_merger.depth_write_enable) |
| 739 | SetDepth(x >> 4, y >> 4, z); | 869 | SetDepth(x >> 4, y >> 4, z); |
| 870 | |||
| 871 | if (stencil_action_enable) { | ||
| 872 | // TODO: What happens if stencil testing is enabled, but depth testing is not? Will stencil get updated anyway? | ||
| 873 | u8 new_stencil = PerformStencilAction(stencil_test.action_depth_pass, old_stencil, stencil_test.replacement_value); | ||
| 874 | SetStencil(x >> 4, y >> 4, new_stencil); | ||
| 875 | } | ||
| 740 | } | 876 | } |
| 741 | 877 | ||
| 742 | auto dest = GetPixel(x >> 4, y >> 4); | 878 | auto dest = GetPixel(x >> 4, y >> 4); |
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 | /** |