graphics_tracing.cpp 6.5 KB

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