summaryrefslogtreecommitdiff
path: root/src/citra_qt/debugger/graphics_vertex_shader.cpp
diff options
context:
space:
mode:
authorGravatar Tony Wasserka2015-08-16 13:18:56 +0200
committerGravatar Tony Wasserka2015-08-16 13:18:56 +0200
commit7312894a6a1a1600629f2eaec8843508a2fd3578 (patch)
tree4f67fb221da50ada7a62efc45788d16950ef16f6 /src/citra_qt/debugger/graphics_vertex_shader.cpp
parentMerge pull request #1033 from bbarenblat/master (diff)
parentcitra-qt/VertexShader: Minor UI improvements. (diff)
downloadyuzu-7312894a6a1a1600629f2eaec8843508a2fd3578.tar.gz
yuzu-7312894a6a1a1600629f2eaec8843508a2fd3578.tar.xz
yuzu-7312894a6a1a1600629f2eaec8843508a2fd3578.zip
Merge pull request #933 from neobrain/shader_debugger
Shader debugger improvements
Diffstat (limited to 'src/citra_qt/debugger/graphics_vertex_shader.cpp')
-rw-r--r--src/citra_qt/debugger/graphics_vertex_shader.cpp256
1 files changed, 227 insertions, 29 deletions
diff --git a/src/citra_qt/debugger/graphics_vertex_shader.cpp b/src/citra_qt/debugger/graphics_vertex_shader.cpp
index 302e22d7a..0c17edee0 100644
--- a/src/citra_qt/debugger/graphics_vertex_shader.cpp
+++ b/src/citra_qt/debugger/graphics_vertex_shader.cpp
@@ -6,9 +6,16 @@
6#include <sstream> 6#include <sstream>
7 7
8#include <QBoxLayout> 8#include <QBoxLayout>
9#include <QFileDialog>
10#include <QGroupBox>
11#include <QLabel>
12#include <QLineEdit>
13#include <QPushButton>
14#include <QSignalMapper>
15#include <QSpinBox>
9#include <QTreeView> 16#include <QTreeView>
10 17
11#include "video_core/shader/shader_interpreter.h" 18#include "video_core/shader/shader.h"
12 19
13#include "graphics_vertex_shader.h" 20#include "graphics_vertex_shader.h"
14 21
@@ -17,7 +24,7 @@ using nihstro::Instruction;
17using nihstro::SourceRegister; 24using nihstro::SourceRegister;
18using nihstro::SwizzlePattern; 25using nihstro::SwizzlePattern;
19 26
20GraphicsVertexShaderModel::GraphicsVertexShaderModel(QObject* parent): QAbstractItemModel(parent) { 27GraphicsVertexShaderModel::GraphicsVertexShaderModel(GraphicsVertexShaderWidget* parent): QAbstractItemModel(parent), par(parent) {
21 28
22} 29}
23 30
@@ -34,7 +41,7 @@ int GraphicsVertexShaderModel::columnCount(const QModelIndex& parent) const {
34} 41}
35 42
36int GraphicsVertexShaderModel::rowCount(const QModelIndex& parent) const { 43int GraphicsVertexShaderModel::rowCount(const QModelIndex& parent) const {
37 return static_cast<int>(info.code.size()); 44 return static_cast<int>(par->info.code.size());
38} 45}
39 46
40QVariant GraphicsVertexShaderModel::headerData(int section, Qt::Orientation orientation, int role) const { 47QVariant GraphicsVertexShaderModel::headerData(int section, Qt::Orientation orientation, int role) const {
@@ -62,21 +69,21 @@ QVariant GraphicsVertexShaderModel::data(const QModelIndex& index, int role) con
62 { 69 {
63 switch (index.column()) { 70 switch (index.column()) {
64 case 0: 71 case 0:
65 if (info.HasLabel(index.row())) 72 if (par->info.HasLabel(index.row()))
66 return QString::fromStdString(info.GetLabel(index.row())); 73 return QString::fromStdString(par->info.GetLabel(index.row()));
67 74
68 return QString("%1").arg(4*index.row(), 4, 16, QLatin1Char('0')); 75 return QString("%1").arg(4*index.row(), 4, 16, QLatin1Char('0'));
69 76
70 case 1: 77 case 1:
71 return QString("%1").arg(info.code[index.row()].hex, 8, 16, QLatin1Char('0')); 78 return QString("%1").arg(par->info.code[index.row()].hex, 8, 16, QLatin1Char('0'));
72 79
73 case 2: 80 case 2:
74 { 81 {
75 std::stringstream output; 82 std::stringstream output;
76 output.flags(std::ios::hex); 83 output.flags(std::ios::hex);
77 84
78 Instruction instr = info.code[index.row()]; 85 Instruction instr = par->info.code[index.row()];
79 const SwizzlePattern& swizzle = info.swizzle_info[instr.common.operand_desc_id].pattern; 86 const SwizzlePattern& swizzle = par->info.swizzle_info[instr.common.operand_desc_id].pattern;
80 87
81 // longest known instruction name: "setemit " 88 // longest known instruction name: "setemit "
82 output << std::setw(8) << std::left << instr.opcode.Value().GetInfo().name; 89 output << std::setw(8) << std::left << instr.opcode.Value().GetInfo().name;
@@ -130,13 +137,13 @@ QVariant GraphicsVertexShaderModel::data(const QModelIndex& index, int role) con
130 137
131 print_input_indexed_compact(output, src1, swizzle.negate_src1, swizzle.SelectorToString(false).substr(0,1), instr.common.AddressRegisterName()); 138 print_input_indexed_compact(output, src1, swizzle.negate_src1, swizzle.SelectorToString(false).substr(0,1), instr.common.AddressRegisterName());
132 output << " " << instr.common.compare_op.ToString(instr.common.compare_op.x) << " "; 139 output << " " << instr.common.compare_op.ToString(instr.common.compare_op.x) << " ";
133 print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(false).substr(0,1)); 140 print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(true).substr(0,1));
134 141
135 output << ", "; 142 output << ", ";
136 143
137 print_input_indexed_compact(output, src1, swizzle.negate_src1, swizzle.SelectorToString(false).substr(1,1), instr.common.AddressRegisterName()); 144 print_input_indexed_compact(output, src1, swizzle.negate_src1, swizzle.SelectorToString(false).substr(1,1), instr.common.AddressRegisterName());
138 output << " " << instr.common.compare_op.ToString(instr.common.compare_op.y) << " "; 145 output << " " << instr.common.compare_op.ToString(instr.common.compare_op.y) << " ";
139 print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(false).substr(1,1)); 146 print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(true).substr(1,1));
140 147
141 break; 148 break;
142 } 149 }
@@ -167,7 +174,7 @@ QVariant GraphicsVertexShaderModel::data(const QModelIndex& index, int role) con
167 // TODO: In some cases, the Address Register is used as an index for SRC2 instead of SRC1 174 // TODO: In some cases, the Address Register is used as an index for SRC2 instead of SRC1
168 if (instr.opcode.Value().GetInfo().subtype & OpCode::Info::Src2) { 175 if (instr.opcode.Value().GetInfo().subtype & OpCode::Info::Src2) {
169 SourceRegister src2 = instr.common.GetSrc2(src_is_inverted); 176 SourceRegister src2 = instr.common.GetSrc2(src_is_inverted);
170 print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(false)); 177 print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(true));
171 } 178 }
172 break; 179 break;
173 } 180 }
@@ -240,6 +247,18 @@ QVariant GraphicsVertexShaderModel::data(const QModelIndex& index, int role) con
240 case Qt::FontRole: 247 case Qt::FontRole:
241 return QFont("monospace"); 248 return QFont("monospace");
242 249
250 case Qt::BackgroundRole:
251 // Highlight instructions which have no debug data associated to them
252 for (const auto& record : par->debug_data.records)
253 if (index.row() == record.instruction_offset)
254 return QVariant();
255
256 return QBrush(QColor(255, 255, 127));
257
258
259 // TODO: Draw arrows for each "reachable" instruction to visualize control flow
260
261
243 default: 262 default:
244 break; 263 break;
245 } 264 }
@@ -247,53 +266,232 @@ QVariant GraphicsVertexShaderModel::data(const QModelIndex& index, int role) con
247 return QVariant(); 266 return QVariant();
248} 267}
249 268
250void GraphicsVertexShaderModel::OnUpdate() 269void GraphicsVertexShaderWidget::DumpShader() {
251{ 270 QString filename = QFileDialog::getSaveFileName(this, tr("Save Shader Dump"), "shader_dump.shbin",
252 beginResetModel(); 271 tr("Shader Binary (*.shbin)"));
253
254 info.Clear();
255
256 for (auto instr : Pica::g_state.vs.program_code)
257 info.code.push_back({instr});
258 272
259 for (auto pattern : Pica::g_state.vs.swizzle_data) 273 if (filename.isEmpty()) {
260 info.swizzle_info.push_back({pattern}); 274 // If the user canceled the dialog, don't dump anything.
275 return;
276 }
261 277
262 info.labels.insert({ Pica::g_state.regs.vs.main_offset, "main" }); 278 auto& setup = Pica::g_state.vs;
279 auto& config = Pica::g_state.regs.vs;
263 280
264 endResetModel(); 281 Pica::DebugUtils::DumpShader(filename.toStdString(), config, setup, Pica::g_state.regs.vs_output_attributes);
265} 282}
266 283
267
268GraphicsVertexShaderWidget::GraphicsVertexShaderWidget(std::shared_ptr< Pica::DebugContext > debug_context, 284GraphicsVertexShaderWidget::GraphicsVertexShaderWidget(std::shared_ptr< Pica::DebugContext > debug_context,
269 QWidget* parent) 285 QWidget* parent)
270 : BreakPointObserverDock(debug_context, "Pica Vertex Shader", parent) { 286 : BreakPointObserverDock(debug_context, "Pica Vertex Shader", parent) {
271 setObjectName("PicaVertexShader"); 287 setObjectName("PicaVertexShader");
272 288
273 auto binary_model = new GraphicsVertexShaderModel(this); 289 auto input_data_mapper = new QSignalMapper(this);
274 auto binary_list = new QTreeView; 290
275 binary_list->setModel(binary_model); 291 // TODO: Support inputting data in hexadecimal raw format
292 for (unsigned i = 0; i < ARRAY_SIZE(input_data); ++i) {
293 input_data[i] = new QLineEdit;
294 input_data[i]->setValidator(new QDoubleValidator(input_data[i]));
295 }
296
297 breakpoint_warning = new QLabel(tr("(data only available at VertexLoaded breakpoints)"));
298
299 // TODO: Add some button for jumping to the shader entry point
300
301 model = new GraphicsVertexShaderModel(this);
302 binary_list = new QTreeView;
303 binary_list->setModel(model);
276 binary_list->setRootIsDecorated(false); 304 binary_list->setRootIsDecorated(false);
277 binary_list->setAlternatingRowColors(true); 305 binary_list->setAlternatingRowColors(true);
278 306
279 connect(this, SIGNAL(Update()), binary_model, SLOT(OnUpdate())); 307 auto dump_shader = new QPushButton(QIcon::fromTheme("document-save"), tr("Dump"));
308
309 instruction_description = new QLabel;
310
311 cycle_index = new QSpinBox;
312
313 connect(this, SIGNAL(SelectCommand(const QModelIndex&, QItemSelectionModel::SelectionFlags)),
314 binary_list->selectionModel(), SLOT(select(const QModelIndex&, QItemSelectionModel::SelectionFlags)));
315
316 connect(dump_shader, SIGNAL(clicked()), this, SLOT(DumpShader()));
317
318 connect(cycle_index, SIGNAL(valueChanged(int)), this, SLOT(OnCycleIndexChanged(int)));
319
320 for (unsigned i = 0; i < ARRAY_SIZE(input_data); ++i) {
321 connect(input_data[i], SIGNAL(textEdited(const QString&)), input_data_mapper, SLOT(map()));
322 input_data_mapper->setMapping(input_data[i], i);
323 }
324 connect(input_data_mapper, SIGNAL(mapped(int)), this, SLOT(OnInputAttributeChanged(int)));
280 325
281 auto main_widget = new QWidget; 326 auto main_widget = new QWidget;
282 auto main_layout = new QVBoxLayout; 327 auto main_layout = new QVBoxLayout;
283 { 328 {
329 auto input_data_group = new QGroupBox(tr("Input Data"));
330
331 // For each vertex attribute, add a QHBoxLayout consisting of:
332 // - A QLabel denoting the source attribute index
333 // - Four QLineEdits for showing and manipulating attribute data
334 // - A QLabel denoting the shader input attribute index
335 auto sub_layout = new QVBoxLayout;
336 for (unsigned i = 0; i < 16; ++i) {
337 // Create an HBoxLayout to store the widgets used to specify a particular attribute
338 // and store it in a QWidget to allow for easy hiding and unhiding.
339 auto row_layout = new QHBoxLayout;
340 row_layout->addWidget(new QLabel(tr("Attribute %1").arg(i, 2)));
341 for (unsigned comp = 0; comp < 4; ++comp)
342 row_layout->addWidget(input_data[4 * i + comp]);
343
344 row_layout->addWidget(input_data_mapping[i] = new QLabel);
345
346 input_data_container[i] = new QWidget;
347 input_data_container[i]->setLayout(row_layout);
348 input_data_container[i]->hide();
349
350 sub_layout->addWidget(input_data_container[i]);
351 }
352
353 sub_layout->addWidget(breakpoint_warning);
354 breakpoint_warning->hide();
355
356 input_data_group->setLayout(sub_layout);
357 main_layout->addWidget(input_data_group);
358 }
359 {
284 auto sub_layout = new QHBoxLayout; 360 auto sub_layout = new QHBoxLayout;
285 sub_layout->addWidget(binary_list); 361 sub_layout->addWidget(binary_list);
286 main_layout->addLayout(sub_layout); 362 main_layout->addLayout(sub_layout);
287 } 363 }
364 main_layout->addWidget(dump_shader);
365 {
366 auto sub_layout = new QHBoxLayout;
367 sub_layout->addWidget(new QLabel(tr("Cycle Index:")));
368 sub_layout->addWidget(cycle_index);
369 main_layout->addLayout(sub_layout);
370 }
371 main_layout->addWidget(instruction_description);
372 main_layout->addStretch();
288 main_widget->setLayout(main_layout); 373 main_widget->setLayout(main_layout);
289 setWidget(main_widget); 374 setWidget(main_widget);
375
376 widget()->setEnabled(false);
290} 377}
291 378
292void GraphicsVertexShaderWidget::OnBreakPointHit(Pica::DebugContext::Event event, void* data) { 379void GraphicsVertexShaderWidget::OnBreakPointHit(Pica::DebugContext::Event event, void* data) {
293 emit Update(); 380 auto input = static_cast<Pica::Shader::InputVertex*>(data);
381 if (event == Pica::DebugContext::Event::VertexLoaded) {
382 Reload(true, data);
383 } else {
384 // No vertex data is retrievable => invalidate currently stored vertex data
385 Reload(true, nullptr);
386 }
294 widget()->setEnabled(true); 387 widget()->setEnabled(true);
295} 388}
296 389
390void GraphicsVertexShaderWidget::Reload(bool replace_vertex_data, void* vertex_data) {
391 model->beginResetModel();
392
393 if (replace_vertex_data) {
394 if (vertex_data) {
395 memcpy(&input_vertex, vertex_data, sizeof(input_vertex));
396 for (unsigned attr = 0; attr < 16; ++attr) {
397 for (unsigned comp = 0; comp < 4; ++comp) {
398 input_data[4 * attr + comp]->setText(QString("%1").arg(input_vertex.attr[attr][comp].ToFloat32()));
399 }
400 }
401 breakpoint_warning->hide();
402 } else {
403 for (unsigned attr = 0; attr < 16; ++attr) {
404 for (unsigned comp = 0; comp < 4; ++comp) {
405 input_data[4 * attr + comp]->setText(QString("???"));
406 }
407 }
408 breakpoint_warning->show();
409 }
410 }
411
412 // Reload shader code
413 info.Clear();
414
415 auto& shader_setup = Pica::g_state.vs;
416 auto& shader_config = Pica::g_state.regs.vs;
417 for (auto instr : shader_setup.program_code)
418 info.code.push_back({instr});
419
420 for (auto pattern : shader_setup.swizzle_data)
421 info.swizzle_info.push_back({pattern});
422
423 u32 entry_point = Pica::g_state.regs.vs.main_offset;
424 info.labels.insert({ entry_point, "main" });
425
426 // Generate debug information
427 debug_data = Pica::Shader::ProduceDebugInfo(input_vertex, 1, shader_config, shader_setup);
428
429 // Reload widget state
430
431 // Only show input attributes which are used as input to the shader
432 for (unsigned int attr = 0; attr < 16; ++attr) {
433 input_data_container[attr]->setVisible(false);
434 }
435 for (unsigned int attr = 0; attr < Pica::g_state.regs.vertex_attributes.GetNumTotalAttributes(); ++attr) {
436 unsigned source_attr = shader_config.input_register_map.GetRegisterForAttribute(attr);
437 input_data_mapping[source_attr]->setText(QString("-> v%1").arg(attr));
438 input_data_container[source_attr]->setVisible(true);
439 }
440
441 // Initialize debug info text for current cycle count
442 cycle_index->setMaximum(debug_data.records.size() - 1);
443 OnCycleIndexChanged(cycle_index->value());
444
445 model->endResetModel();
446}
447
297void GraphicsVertexShaderWidget::OnResumed() { 448void GraphicsVertexShaderWidget::OnResumed() {
298 widget()->setEnabled(false); 449 widget()->setEnabled(false);
299} 450}
451
452void GraphicsVertexShaderWidget::OnInputAttributeChanged(int index) {
453 float value = input_data[index]->text().toFloat();
454 Reload();
455}
456
457void GraphicsVertexShaderWidget::OnCycleIndexChanged(int index) {
458 QString text;
459
460 auto& record = debug_data.records[index];
461 if (record.mask & Pica::Shader::DebugDataRecord::SRC1)
462 text += tr("SRC1: %1, %2, %3, %4\n").arg(record.src1.x.ToFloat32()).arg(record.src1.y.ToFloat32()).arg(record.src1.z.ToFloat32()).arg(record.src1.w.ToFloat32());
463 if (record.mask & Pica::Shader::DebugDataRecord::SRC2)
464 text += tr("SRC2: %1, %2, %3, %4\n").arg(record.src2.x.ToFloat32()).arg(record.src2.y.ToFloat32()).arg(record.src2.z.ToFloat32()).arg(record.src2.w.ToFloat32());
465 if (record.mask & Pica::Shader::DebugDataRecord::SRC3)
466 text += tr("SRC3: %1, %2, %3, %4\n").arg(record.src3.x.ToFloat32()).arg(record.src3.y.ToFloat32()).arg(record.src3.z.ToFloat32()).arg(record.src3.w.ToFloat32());
467 if (record.mask & Pica::Shader::DebugDataRecord::DEST_IN)
468 text += tr("DEST_IN: %1, %2, %3, %4\n").arg(record.dest_in.x.ToFloat32()).arg(record.dest_in.y.ToFloat32()).arg(record.dest_in.z.ToFloat32()).arg(record.dest_in.w.ToFloat32());
469 if (record.mask & Pica::Shader::DebugDataRecord::DEST_OUT)
470 text += tr("DEST_OUT: %1, %2, %3, %4\n").arg(record.dest_out.x.ToFloat32()).arg(record.dest_out.y.ToFloat32()).arg(record.dest_out.z.ToFloat32()).arg(record.dest_out.w.ToFloat32());
471
472 if (record.mask & Pica::Shader::DebugDataRecord::ADDR_REG_OUT)
473 text += tr("Addres Registers: %1, %2\n").arg(record.address_registers[0]).arg(record.address_registers[1]);
474 if (record.mask & Pica::Shader::DebugDataRecord::CMP_RESULT)
475 text += tr("Compare Result: %1, %2\n").arg(record.conditional_code[0] ? "true" : "false").arg(record.conditional_code[1] ? "true" : "false");
476
477 if (record.mask & Pica::Shader::DebugDataRecord::COND_BOOL_IN)
478 text += tr("Static Condition: %1\n").arg(record.cond_bool ? "true" : "false");
479 if (record.mask & Pica::Shader::DebugDataRecord::COND_CMP_IN)
480 text += tr("Dynamic Conditions: %1, %2\n").arg(record.cond_cmp[0] ? "true" : "false").arg(record.cond_cmp[1] ? "true" : "false");
481 if (record.mask & Pica::Shader::DebugDataRecord::LOOP_INT_IN)
482 text += tr("Loop Parameters: %1 (repeats), %2 (initializer), %3 (increment), %4\n").arg(record.loop_int.x).arg(record.loop_int.y).arg(record.loop_int.z).arg(record.loop_int.w);
483
484 text += tr("Instruction offset: 0x%1").arg(4 * record.instruction_offset, 4, 16, QLatin1Char('0'));
485 if (record.mask & Pica::Shader::DebugDataRecord::NEXT_INSTR) {
486 text += tr(" -> 0x%2").arg(4 * record.next_instruction, 4, 16, QLatin1Char('0'));
487 } else {
488 text += tr(" (last instruction)");
489 }
490
491 instruction_description->setText(text);
492
493 // Scroll to current instruction
494 const QModelIndex& instr_index = model->index(record.instruction_offset, 0);
495 emit SelectCommand(instr_index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
496 binary_list->scrollTo(instr_index, QAbstractItemView::EnsureVisible);
497}