summaryrefslogtreecommitdiffstats
path: root/src/citra_qt/debugger/graphics_cmdlists.cpp
blob: 35a3140b22d58f0d5b95364e771f0cea4f003460 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <QApplication>
#include <QClipboard>
#include <QLabel>
#include <QListView>
#include <QMainWindow>
#include <QPushButton>
#include <QVBoxLayout>
#include <QTreeView>
#include <QHeaderView>
#include <QSpinBox>
#include <QComboBox>

#include "common/vector_math.h"

#include "video_core/debug_utils/debug_utils.h"
#include "video_core/pica.h"

#include "graphics_cmdlists.h"

#include "util/spinbox.h"

QImage LoadTexture(u8* src, const Pica::DebugUtils::TextureInfo& info) {
    QImage decoded_image(info.width, info.height, QImage::Format_ARGB32);
    for (int y = 0; y < info.height; ++y) {
        for (int x = 0; x < info.width; ++x) {
            Math::Vec4<u8> color = Pica::DebugUtils::LookupTexture(src, x, y, info, true);
            decoded_image.setPixel(x, y, qRgba(color.r(), color.g(), color.b(), color.a()));
        }
    }

    return decoded_image;
}

class TextureInfoWidget : public QWidget {
public:
    TextureInfoWidget(u8* src, const Pica::DebugUtils::TextureInfo& info, QWidget* parent = nullptr) : QWidget(parent) {
        QLabel* image_widget = new QLabel;
        QPixmap image_pixmap = QPixmap::fromImage(LoadTexture(src, info));
        image_pixmap = image_pixmap.scaled(200, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        image_widget->setPixmap(image_pixmap);

        QVBoxLayout* layout = new QVBoxLayout;
        layout->addWidget(image_widget);
        setLayout(layout);
    }
};

TextureInfoDockWidget::TextureInfoDockWidget(const Pica::DebugUtils::TextureInfo& info, QWidget* parent)
    : QDockWidget(tr("Texture 0x%1").arg(info.physical_address, 8, 16, QLatin1Char('0'))),
      info(info) {

    QWidget* main_widget = new QWidget;

    QLabel* image_widget = new QLabel;

    connect(this, SIGNAL(UpdatePixmap(const QPixmap&)), image_widget, SLOT(setPixmap(const QPixmap&)));

    CSpinBox* phys_address_spinbox = new CSpinBox;
    phys_address_spinbox->SetBase(16);
    phys_address_spinbox->SetRange(0, 0xFFFFFFFF);
    phys_address_spinbox->SetPrefix("0x");
    phys_address_spinbox->SetValue(info.physical_address);
    connect(phys_address_spinbox, SIGNAL(ValueChanged(qint64)), this, SLOT(OnAddressChanged(qint64)));

    QComboBox* format_choice = new QComboBox;
    format_choice->addItem(tr("RGBA8"));
    format_choice->addItem(tr("RGB8"));
    format_choice->addItem(tr("RGB5A1"));
    format_choice->addItem(tr("RGB565"));
    format_choice->addItem(tr("RGBA4"));
    format_choice->addItem(tr("IA8"));
    format_choice->addItem(tr("RG8"));
    format_choice->addItem(tr("I8"));
    format_choice->addItem(tr("A8"));
    format_choice->addItem(tr("IA4"));
    format_choice->addItem(tr("I4"));
    format_choice->addItem(tr("A4"));
    format_choice->addItem(tr("ETC1"));
    format_choice->addItem(tr("ETC1A4"));
    format_choice->setCurrentIndex(static_cast<int>(info.format));
    connect(format_choice, SIGNAL(currentIndexChanged(int)), this, SLOT(OnFormatChanged(int)));

    QSpinBox* width_spinbox = new QSpinBox;
    width_spinbox->setMaximum(65535);
    width_spinbox->setValue(info.width);
    connect(width_spinbox, SIGNAL(valueChanged(int)), this, SLOT(OnWidthChanged(int)));

    QSpinBox* height_spinbox = new QSpinBox;
    height_spinbox->setMaximum(65535);
    height_spinbox->setValue(info.height);
    connect(height_spinbox, SIGNAL(valueChanged(int)), this, SLOT(OnHeightChanged(int)));

    QSpinBox* stride_spinbox = new QSpinBox;
    stride_spinbox->setMaximum(65535 * 4);
    stride_spinbox->setValue(info.stride);
    connect(stride_spinbox, SIGNAL(valueChanged(int)), this, SLOT(OnStrideChanged(int)));

    QVBoxLayout* main_layout = new QVBoxLayout;
    main_layout->addWidget(image_widget);

    {
        QHBoxLayout* sub_layout = new QHBoxLayout;
        sub_layout->addWidget(new QLabel(tr("Source Address:")));
        sub_layout->addWidget(phys_address_spinbox);
        main_layout->addLayout(sub_layout);
    }

    {
        QHBoxLayout* sub_layout = new QHBoxLayout;
        sub_layout->addWidget(new QLabel(tr("Format")));
        sub_layout->addWidget(format_choice);
        main_layout->addLayout(sub_layout);
    }

    {
        QHBoxLayout* sub_layout = new QHBoxLayout;
        sub_layout->addWidget(new QLabel(tr("Width:")));
        sub_layout->addWidget(width_spinbox);
        sub_layout->addStretch();
        sub_layout->addWidget(new QLabel(tr("Height:")));
        sub_layout->addWidget(height_spinbox);
        sub_layout->addStretch();
        sub_layout->addWidget(new QLabel(tr("Stride:")));
        sub_layout->addWidget(stride_spinbox);
        main_layout->addLayout(sub_layout);
    }

    main_widget->setLayout(main_layout);

    emit UpdatePixmap(ReloadPixmap());

    setWidget(main_widget);
}

void TextureInfoDockWidget::OnAddressChanged(qint64 value) {
    info.physical_address = value;
    emit UpdatePixmap(ReloadPixmap());
}

void TextureInfoDockWidget::OnFormatChanged(int value) {
    info.format = static_cast<Pica::Regs::TextureFormat>(value);
    emit UpdatePixmap(ReloadPixmap());
}

void TextureInfoDockWidget::OnWidthChanged(int value) {
    info.width = value;
    emit UpdatePixmap(ReloadPixmap());
}

void TextureInfoDockWidget::OnHeightChanged(int value) {
    info.height = value;
    emit UpdatePixmap(ReloadPixmap());
}

void TextureInfoDockWidget::OnStrideChanged(int value) {
    info.stride = value;
    emit UpdatePixmap(ReloadPixmap());
}

QPixmap TextureInfoDockWidget::ReloadPixmap() const {
    u8* src = Memory::GetPhysicalPointer(info.physical_address);
    return QPixmap::fromImage(LoadTexture(src, info));
}

GPUCommandListModel::GPUCommandListModel(QObject* parent) : QAbstractListModel(parent) {

}

int GPUCommandListModel::rowCount(const QModelIndex& parent) const {
    return static_cast<int>(pica_trace.writes.size());
}

int GPUCommandListModel::columnCount(const QModelIndex& parent) const {
    return 4;
}

QVariant GPUCommandListModel::data(const QModelIndex& index, int role) const {
    if (!index.isValid())
        return QVariant();

    const auto& write = pica_trace.writes[index.row()];

    if (role == Qt::DisplayRole) {
        QString content;
        switch ( index.column() ) {
        case 0:
            return QString::fromLatin1(Pica::Regs::GetCommandName(write.cmd_id).c_str());
        case 1:
            return QString("%1").arg(write.cmd_id, 3, 16, QLatin1Char('0'));
        case 2:
            return QString("%1").arg(write.mask, 4, 2, QLatin1Char('0'));
        case 3:
            return QString("%1").arg(write.value, 8, 16, QLatin1Char('0'));
        }
    } else if (role == CommandIdRole) {
        return QVariant::fromValue<int>(write.cmd_id);
    }

    return QVariant();
}

QVariant GPUCommandListModel::headerData(int section, Qt::Orientation orientation, int role) const {
    switch(role) {
    case Qt::DisplayRole:
    {
        switch (section) {
        case 0:
            return tr("Command Name");
        case 1:
            return tr("Register");
        case 2:
            return tr("Mask");
        case 3:
            return tr("New Value");
        }

        break;
    }
    }

    return QVariant();
}

void GPUCommandListModel::OnPicaTraceFinished(const Pica::DebugUtils::PicaTrace& trace) {
    beginResetModel();

    pica_trace = trace;

    endResetModel();
}

#define COMMAND_IN_RANGE(cmd_id, reg_name)   \
    (cmd_id >= PICA_REG_INDEX(reg_name) &&   \
     cmd_id < PICA_REG_INDEX(reg_name) + sizeof(decltype(Pica::g_state.regs.reg_name)) / 4)

void GPUCommandListWidget::OnCommandDoubleClicked(const QModelIndex& index) {
    const unsigned int command_id = list_widget->model()->data(index, GPUCommandListModel::CommandIdRole).toUInt();
    if (COMMAND_IN_RANGE(command_id, texture0) ||
        COMMAND_IN_RANGE(command_id, texture1) ||
        COMMAND_IN_RANGE(command_id, texture2)) {

        unsigned index;
        if (COMMAND_IN_RANGE(command_id, texture0)) {
            index = 0;
        } else if (COMMAND_IN_RANGE(command_id, texture1)) {
            index = 1;
        } else {
            index = 2;
        }
        auto config = Pica::g_state.regs.GetTextures()[index].config;
        auto format = Pica::g_state.regs.GetTextures()[index].format;
        auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(config, format);

        // TODO: Instead, emit a signal here to be caught by the main window widget.
        auto main_window = static_cast<QMainWindow*>(parent());
        main_window->tabifyDockWidget(this, new TextureInfoDockWidget(info, main_window));
    }
}

void GPUCommandListWidget::SetCommandInfo(const QModelIndex& index) {
    QWidget* new_info_widget = nullptr;

    const unsigned int command_id = list_widget->model()->data(index, GPUCommandListModel::CommandIdRole).toUInt();
    if (COMMAND_IN_RANGE(command_id, texture0) ||
        COMMAND_IN_RANGE(command_id, texture1) ||
        COMMAND_IN_RANGE(command_id, texture2)) {

        unsigned index;
        if (COMMAND_IN_RANGE(command_id, texture0)) {
            index = 0;
        } else if (COMMAND_IN_RANGE(command_id, texture1)) {
            index = 1;
        } else {
            index = 2;
        }
        auto config = Pica::g_state.regs.GetTextures()[index].config;
        auto format = Pica::g_state.regs.GetTextures()[index].format;

        auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(config, format);
        u8* src = Memory::GetPhysicalPointer(config.GetPhysicalAddress());
        new_info_widget = new TextureInfoWidget(src, info);
    }
    if (command_info_widget) {
        delete command_info_widget;
        command_info_widget = nullptr;
    }
    if (new_info_widget) {
        widget()->layout()->addWidget(new_info_widget);
        command_info_widget = new_info_widget;
    }
}
#undef COMMAND_IN_RANGE

GPUCommandListWidget::GPUCommandListWidget(QWidget* parent) : QDockWidget(tr("Pica Command List"), parent) {
    setObjectName("Pica Command List");
    GPUCommandListModel* model = new GPUCommandListModel(this);

    QWidget* main_widget = new QWidget;

    list_widget = new QTreeView;
    list_widget->setModel(model);
    QFont font("monospace");
    font.setStyleHint(QFont::Monospace); // Automatic fallback to a monospace font on on platforms without a font called "monospace"
    list_widget->setFont(font);
    list_widget->setRootIsDecorated(false);
    list_widget->setUniformRowHeights(true);

#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
    list_widget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
#else
    list_widget->header()->setResizeMode(QHeaderView::ResizeToContents);
#endif

    connect(list_widget->selectionModel(), SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)),
            this, SLOT(SetCommandInfo(const QModelIndex&)));
    connect(list_widget, SIGNAL(doubleClicked(const QModelIndex&)),
            this, SLOT(OnCommandDoubleClicked(const QModelIndex&)));

    toggle_tracing = new QPushButton(tr("Start Tracing"));
    QPushButton* copy_all = new QPushButton(tr("Copy All"));

    connect(toggle_tracing, SIGNAL(clicked()), this, SLOT(OnToggleTracing()));
    connect(this, SIGNAL(TracingFinished(const Pica::DebugUtils::PicaTrace&)),
            model, SLOT(OnPicaTraceFinished(const Pica::DebugUtils::PicaTrace&)));

    connect(copy_all, SIGNAL(clicked()), this, SLOT(CopyAllToClipboard()));

    command_info_widget = nullptr;

    QVBoxLayout* main_layout = new QVBoxLayout;
    main_layout->addWidget(list_widget);
    {
        QHBoxLayout* sub_layout = new QHBoxLayout;
        sub_layout->addWidget(toggle_tracing);
        sub_layout->addWidget(copy_all);
        main_layout->addLayout(sub_layout);
    }
    main_widget->setLayout(main_layout);

    setWidget(main_widget);
}

void GPUCommandListWidget::OnToggleTracing() {
    if (!Pica::DebugUtils::IsPicaTracing()) {
        Pica::DebugUtils::StartPicaTracing();
        toggle_tracing->setText(tr("Finish Tracing"));
    } else {
        pica_trace = Pica::DebugUtils::FinishPicaTracing();
        emit TracingFinished(*pica_trace);
        toggle_tracing->setText(tr("Start Tracing"));
    }
}

void GPUCommandListWidget::CopyAllToClipboard() {
    QClipboard* clipboard = QApplication::clipboard();
    QString text;

    QAbstractItemModel* model = static_cast<QAbstractListModel*>(list_widget->model());

    for (int row = 0; row < model->rowCount({}); ++row) {
        for (int col = 0; col < model->columnCount({}); ++col) {
            QModelIndex index = model->index(row, col);
            text += model->data(index).value<QString>();
            text += '\t';
        }
        text += '\n';
    }

    clipboard->setText(text);
}