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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
|
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2019-2022 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "ShaderMessageViewer.h"
#include <QAction>
#include <QMenu>
#include <QMouseEvent>
#include <QScrollBar>
#include "Code/QRDUtils.h"
#include "Code/Resources.h"
#include "Widgets/Extended/RDHeaderView.h"
#include "Widgets/Extended/RDLineEdit.h"
#include "toolwindowmanager/ToolWindowManager.h"
#include "ui_ShaderMessageViewer.h"
static const int debuggableRole = Qt::UserRole + 1;
static const int gotoableRole = Qt::UserRole + 2;
ButtonDelegate::ButtonDelegate(const QIcon &icon, int enableRole, QWidget *parent)
: m_Icon(icon), m_EnableRole(enableRole), QStyledItemDelegate(parent)
{
}
void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
// draw the background to get selection etc
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &option, painter);
QStyleOptionButton button;
QSize sz = sizeHint(option, index);
button.rect = option.rect;
button.rect.setLeft(button.rect.center().x() - sz.width() / 2);
button.rect.setTop(button.rect.center().y() - sz.height() / 2);
button.rect.setSize(sz);
button.icon = m_Icon;
button.iconSize = sz;
if(m_EnableRole == 0 || index.data(m_EnableRole).toBool())
button.state = QStyle::State_Enabled;
if(m_ClickedIndex == index)
button.state |= QStyle::State_Sunken;
QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
}
QSize ButtonDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionButton button;
button.icon = m_Icon;
button.state = QStyle::State_Enabled;
return QApplication::style()->sizeFromContents(QStyle::CT_PushButton, &button,
option.decorationSize);
}
bool ButtonDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option, const QModelIndex &index)
{
if(event->type() == QEvent::MouseButtonPress)
{
if(m_EnableRole == 0 || index.data(m_EnableRole).toBool())
m_ClickedIndex = index;
}
else if(event->type() == QEvent::MouseMove)
{
QMouseEvent *e = (QMouseEvent *)event;
if(m_ClickedIndex != index || (e->buttons() & Qt::LeftButton) == 0)
{
m_ClickedIndex = QModelIndex();
}
else
{
QPoint p = e->pos();
QSize sz = option.decorationSize;
QRect rect = option.rect;
rect.setLeft(rect.center().x() - sz.width() / 2);
rect.setTop(rect.center().y() - sz.height() / 2);
rect.setSize(sz);
if(!rect.contains(p))
{
m_ClickedIndex = QModelIndex();
}
}
}
else if(event->type() == QEvent::MouseButtonRelease)
{
if(m_ClickedIndex == index && index != QModelIndex())
{
m_ClickedIndex = QModelIndex();
QMouseEvent *e = (QMouseEvent *)event;
QPoint p = e->pos();
QSize sz = option.decorationSize;
QRect rect = option.rect;
rect.setLeft(rect.center().x() - sz.width() / 2);
rect.setTop(rect.center().y() - sz.height() / 2);
rect.setSize(sz);
if(rect.contains(p))
emit messageClicked(index);
}
}
return true;
}
ShaderMessageViewer::ShaderMessageViewer(ICaptureContext &ctx, ShaderStageMask stages, QWidget *parent)
: QFrame(parent), ui(new Ui::ShaderMessageViewer), m_Ctx(ctx)
{
ui->setupUi(this);
ui->messages->setFont(Formatter::PreferredFont());
ui->messages->setMouseTracking(true);
m_API = m_Ctx.APIProps().pipelineType;
QObject::connect(ui->vertex, &QToolButton::toggled, [this](bool) { refreshMessages(); });
QObject::connect(ui->hull, &QToolButton::toggled, [this](bool) { refreshMessages(); });
QObject::connect(ui->domain, &QToolButton::toggled, [this](bool) { refreshMessages(); });
QObject::connect(ui->geometry, &QToolButton::toggled, [this](bool) { refreshMessages(); });
QObject::connect(ui->pixel, &QToolButton::toggled, [this](bool) { refreshMessages(); });
QObject::connect(ui->filterButton, &QToolButton::clicked, [this]() { refreshMessages(); });
QObject::connect(ui->filter, &RDLineEdit::returnPressed, [this]() { refreshMessages(); });
QMenu *menu = new QMenu(this);
QAction *action = new QAction(tr("Export to &Text"));
action->setIcon(Icons::save());
QObject::connect(action, &QAction::triggered, this, &ShaderMessageViewer::exportText);
menu->addAction(action);
action = new QAction(tr("Export to &CSV"));
action->setIcon(Icons::save());
QObject::connect(action, &QAction::triggered, this, &ShaderMessageViewer::exportCSV);
menu->addAction(action);
ui->exportButton->setMenu(menu);
QObject::connect(ui->exportButton, &QToolButton::clicked, this, &ShaderMessageViewer::exportText);
ui->vertex->setText(ToQStr(ShaderStage::Vertex, m_API));
ui->hull->setText(ToQStr(ShaderStage::Hull, m_API));
ui->domain->setText(ToQStr(ShaderStage::Domain, m_API));
ui->geometry->setText(ToQStr(ShaderStage::Geometry, m_API));
ui->pixel->setText(ToQStr(ShaderStage::Pixel, m_API));
m_EID = m_Ctx.CurEvent();
m_Action = m_Ctx.GetAction(m_EID);
const PipeState &pipe = m_Ctx.CurPipelineState();
// check if we have multiview enabled
m_Multiview = pipe.MultiviewBroadcastCount() > 1;
// only display sample information if one of the targets is multisampled
m_Multisampled = false;
rdcarray<BoundResource> outs = pipe.GetOutputTargets();
outs.push_back(pipe.GetDepthTarget());
for(const BoundResource &o : outs)
{
if(o.resourceId == ResourceId())
continue;
const TextureDescription *tex = m_Ctx.GetTexture(o.resourceId);
if(tex->msSamp > 1)
{
m_Multisampled = true;
break;
}
}
RDHeaderView *header = new RDHeaderView(Qt::Horizontal, this);
ui->messages->setHeader(header);
header->setStretchLastSection(true);
header->setMinimumSectionSize(40);
int sortColumn = 0;
m_debugDelegate = new ButtonDelegate(Icons::wrench(), debuggableRole, this);
if(m_Action && (m_Action->flags & ActionFlags::Dispatch))
{
ui->stageFilters->hide();
ui->messages->setColumns({lit("Debug"), tr("Workgroup"), lit("Thread"), lit("Message")});
sortColumn = 1;
ui->messages->setItemDelegateForColumn(0, m_debugDelegate);
m_OrigShaders[5] = pipe.GetShader(ShaderStage::Compute);
}
else
{
ui->messages->setColumns({lit("Debug"), lit("Go to"), tr("Location"), lit("Message")});
sortColumn = 2;
m_gotoDelegate = new ButtonDelegate(Icons::find(), gotoableRole, this);
ui->messages->setItemDelegateForColumn(0, m_debugDelegate);
ui->messages->setItemDelegateForColumn(1, m_gotoDelegate);
QCheckBox *boxes[] = {
ui->vertex, ui->hull, ui->domain, ui->geometry, ui->pixel,
};
for(ShaderStage s : values<ShaderStage>())
{
if(s == ShaderStage::Compute)
continue;
uint32_t idx = (uint32_t)s;
m_OrigShaders[idx] = pipe.GetShader(s);
boxes[idx]->setChecked(bool(stages & MaskForStage(s)));
// if there's no shader bound, we currently don't support adding stages at runtime so just
// hide this box as no messages can come from the unbound stage
if(m_OrigShaders[idx] == ResourceId())
boxes[idx]->hide();
}
}
ui->messages->setContextMenuPolicy(Qt::CustomContextMenu);
QObject::connect(ui->messages, &RDTreeWidget::customContextMenuRequested, [this](const QPoint &pos) {
QModelIndex idx = ui->messages->indexAt(pos);
RDTreeWidgetItem *item = ui->messages->itemForIndex(idx);
QMenu contextMenu(this);
QAction copy(tr("&Copy"), this);
contextMenu.addAction(©);
copy.setIcon(Icons::copy());
QObject::connect(©, &QAction::triggered,
[this, pos, item]() { ui->messages->copyItem(pos, item); });
QAction debugAction(tr("&Debug"), this);
debugAction.setIcon(Icons::wrench());
QAction gotoAction(tr("&Go to"), this);
gotoAction.setIcon(Icons::find());
QObject::connect(&debugAction, &QAction::triggered,
[this, idx]() { m_debugDelegate->messageClicked(idx); });
QObject::connect(&gotoAction, &QAction::triggered,
[this, idx]() { m_gotoDelegate->messageClicked(idx); });
contextMenu.addAction(&debugAction);
if(m_gotoDelegate)
contextMenu.addAction(&gotoAction);
RDDialog::show(&contextMenu, ui->messages->viewport()->mapToGlobal(pos));
});
QObject::connect(m_debugDelegate, &ButtonDelegate::messageClicked, [this](const QModelIndex &idx) {
RDTreeWidgetItem *item = ui->messages->itemForIndex(idx);
int msgIdx = 0;
if(item)
msgIdx = item->tag().toInt();
else
return;
const ShaderMessage &msg = m_Messages[msgIdx];
const ShaderReflection *refl = m_Ctx.CurPipelineState().GetShaderReflection(msg.stage);
if(refl->debugInfo.debuggable)
{
bool done = false;
ShaderDebugTrace *trace = NULL;
m_Ctx.Replay().AsyncInvoke([&trace, &done, msg](IReplayController *r) {
if(msg.stage == ShaderStage::Compute)
trace = r->DebugThread(msg.location.compute.workgroup, msg.location.compute.thread);
else if(msg.stage == ShaderStage::Vertex)
trace = r->DebugVertex(msg.location.vertex.vertexIndex, msg.location.vertex.instance,
msg.location.vertex.vertexIndex, msg.location.vertex.view);
else if(msg.stage == ShaderStage::Pixel)
trace = r->DebugPixel(msg.location.pixel.x, msg.location.pixel.y,
msg.location.pixel.sample, msg.location.pixel.primitive);
if(trace->debugger == NULL)
{
r->FreeTrace(trace);
trace = NULL;
}
done = true;
});
QString debugContext;
if(msg.stage == ShaderStage::Compute)
debugContext = lit("Group [%1,%2,%3] Thread [%4,%5,%6]")
.arg(msg.location.compute.workgroup[0])
.arg(msg.location.compute.workgroup[1])
.arg(msg.location.compute.workgroup[2])
.arg(msg.location.compute.thread[0])
.arg(msg.location.compute.thread[1])
.arg(msg.location.compute.thread[2]);
else if(msg.stage == ShaderStage::Vertex)
debugContext = tr("Vertex %1").arg(msg.location.vertex.vertexIndex);
else if(msg.stage == ShaderStage::Pixel)
debugContext = tr("Pixel %1,%2").arg(msg.location.pixel.x).arg(msg.location.pixel.y);
// wait a short while before displaying the progress dialog (which won't show if we're already
// done by the time we reach it)
for(int i = 0; !done && i < 100; i++)
QThread::msleep(5);
ShowProgressDialog(this, tr("Debugging %1").arg(debugContext), [&done]() { return done; });
if(!trace)
{
RDDialog::critical(this, tr("Debug Error"), tr("Error debugging pixel."));
return;
}
const ShaderBindpointMapping &bindMapping =
m_Ctx.CurPipelineState().GetBindpointMapping(msg.stage);
ResourceId pipeline = msg.stage == ShaderStage::Compute
? m_Ctx.CurPipelineState().GetComputePipelineObject()
: m_Ctx.CurPipelineState().GetGraphicsPipelineObject();
// viewer takes ownership of the trace
IShaderViewer *s = m_Ctx.DebugShader(&bindMapping, refl, pipeline, trace, debugContext);
if(msg.disassemblyLine >= 0)
{
s->ToggleBreakpointOnDisassemblyLine(msg.disassemblyLine);
s->RunForward();
}
m_Ctx.AddDockWindow(s->Widget(), DockReference::AddTo, this);
}
else
{
RDDialog::critical(
this, tr("Shader can't be debugged"),
tr("The shader does not support debugging: %1").arg(refl->debugInfo.debugStatus));
}
});
if(m_gotoDelegate)
{
QObject::connect(m_gotoDelegate, &ButtonDelegate::messageClicked, [this](const QModelIndex &idx) {
RDTreeWidgetItem *item = ui->messages->itemForIndex(idx);
int msgIdx = 0;
if(item)
msgIdx = item->tag().toInt();
else
return;
const ShaderMessage &msg = m_Messages[msgIdx];
m_Ctx.SetEventID({}, m_EID, m_EID);
if(msg.stage == ShaderStage::Vertex)
{
m_Ctx.ShowMeshPreview();
m_Ctx.GetMeshPreview()->SetCurrentInstance(msg.location.vertex.instance);
m_Ctx.GetMeshPreview()->SetCurrentView(msg.location.vertex.view);
m_Ctx.GetMeshPreview()->ShowMeshData(MeshDataStage::VSOut);
m_Ctx.GetMeshPreview()->ScrollToRow(msg.location.vertex.vertexIndex, MeshDataStage::VSOut);
m_Ctx.GetMeshPreview()->ShowMeshData(MeshDataStage::VSIn);
// TODO, not accurate for indices
m_Ctx.GetMeshPreview()->ScrollToRow(msg.location.vertex.vertexIndex, MeshDataStage::VSIn);
}
else if(msg.stage == ShaderStage::Pixel)
{
m_Ctx.ShowTextureViewer();
Subresource sub = m_Ctx.GetTextureViewer()->GetSelectedSubresource();
sub.sample = msg.location.pixel.sample;
sub.slice = msg.location.pixel.view;
m_Ctx.GetTextureViewer()->SetSelectedSubresource(sub);
// select an actual output. Prefer the first colour output, but if there's no colour output
// pick depth.
rdcarray<BoundResource> cols = m_Ctx.CurPipelineState().GetOutputTargets();
bool hascol = false;
for(size_t i = 0; i < cols.size(); i++)
hascol |= cols[i].resourceId != ResourceId();
if(hascol)
m_Ctx.GetTextureViewer()->ViewFollowedResource(FollowType::OutputColor,
ShaderStage::Pixel, 0, 0);
else
m_Ctx.GetTextureViewer()->ViewFollowedResource(FollowType::OutputDepth,
ShaderStage::Pixel, 0, 0);
m_Ctx.GetTextureViewer()->GotoLocation(msg.location.pixel.x, msg.location.pixel.y);
}
else if(msg.stage == ShaderStage::Geometry)
{
m_Ctx.ShowMeshPreview();
m_Ctx.GetMeshPreview()->SetCurrentView(msg.location.geometry.view);
m_Ctx.GetMeshPreview()->ShowMeshData(MeshDataStage::GSOut);
// TODO, instances not supported
m_Ctx.GetMeshPreview()->ScrollToRow(
RENDERDOC_VertexOffset(m_Ctx.CurPipelineState().GetPrimitiveTopology(),
msg.location.geometry.primitive),
MeshDataStage::GSOut);
}
else
{
qCritical() << "Can't go to a compute thread";
}
});
}
// deliberately copy m_OrigShaders to m_ReplacedShaders. This is impossible because we should
// either see a ResourceId() for unedited, or a new resource for edited. This means when we first
// get OnEventChanged() called we will definitely detect the situation as 'stale' and refresh the
// messages.
memcpy(m_ReplacedShaders, m_OrigShaders, sizeof(m_ReplacedShaders));
header->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui->staleStatus->hide();
ui->label->setText(tr("Shader messages from @%1 - %2")
.arg(m_EID)
.arg(m_Action ? m_Ctx.GetEventBrowser()->GetEventName(m_Action->eventId)
: rdcstr("Unknown action")));
setWindowTitle(tr("Shader messages at @%1").arg(m_EID));
m_Ctx.AddCaptureViewer(this);
OnEventChanged(m_Ctx.CurEvent());
ui->messages->setSortComparison(
[this](int col, Qt::SortOrder order, const RDTreeWidgetItem *a, const RDTreeWidgetItem *b) {
if(order == Qt::DescendingOrder)
std::swap(a, b);
const ShaderMessage &am = m_Messages[a->tag().toInt()];
const ShaderMessage &bm = m_Messages[b->tag().toInt()];
if(col == 3)
{
return am.message < bm.message;
}
else if(col == 2 || m_OrigShaders[5] == ResourceId())
{
// sort by location either if it's selected, or if it's not dispatch in which case we
// default to location sorting (don't try to sort by the button-only columns that have no
// data)
// sort by stage first
if(am.stage != bm.stage)
return am.stage < bm.stage;
if(am.stage == ShaderStage::Vertex)
{
const ShaderVertexMessageLocation &aloc = am.location.vertex;
const ShaderVertexMessageLocation &bloc = bm.location.vertex;
if(aloc.view != bloc.view)
return aloc.view < bloc.view;
if(aloc.instance != bloc.instance)
return aloc.instance < bloc.instance;
return aloc.vertexIndex < bloc.vertexIndex;
}
else if(am.stage == ShaderStage::Pixel)
{
const ShaderPixelMessageLocation &aloc = am.location.pixel;
const ShaderPixelMessageLocation &bloc = bm.location.pixel;
if(aloc.x != bloc.x)
return aloc.x < bloc.x;
if(aloc.y != bloc.y)
return aloc.y < bloc.y;
if(aloc.primitive != bloc.primitive)
return aloc.primitive < bloc.primitive;
if(aloc.view != bloc.view)
return aloc.view < bloc.view;
return aloc.sample < bloc.sample;
}
else if(am.stage == ShaderStage::Compute)
{
// column 2 is the thread column for compute
return am.location.compute.thread < bm.location.compute.thread;
}
else if(am.stage == ShaderStage::Geometry)
{
const ShaderGeometryMessageLocation &aloc = am.location.geometry;
const ShaderGeometryMessageLocation &bloc = bm.location.geometry;
if(aloc.view != bloc.view)
return aloc.view < bloc.view;
return am.location.geometry.primitive < bm.location.geometry.primitive;
}
else
{
// can't sort these, pretend they're all equal
return false;
}
}
else if(col == 1)
{
return am.location.compute.workgroup < bm.location.compute.workgroup;
}
return false;
});
ui->messages->sortByColumn(sortColumn, Qt::SortOrder::AscendingOrder);
for(int i = 0; i < 4; i++)
{
header->setSectionResizeMode(i, QHeaderView::Interactive);
ui->messages->resizeColumnToContents(i);
}
}
ShaderMessageViewer::~ShaderMessageViewer()
{
m_Ctx.RemoveCaptureViewer(this);
delete ui;
}
bool ShaderMessageViewer::IsOutOfDate()
{
return ui->staleStatus->isVisible();
}
void ShaderMessageViewer::OnCaptureLoaded()
{
}
void ShaderMessageViewer::OnCaptureClosed()
{
ToolWindowManager::closeToolWindow(this);
}
void ShaderMessageViewer::OnEventChanged(uint32_t eventId)
{
ResourceId shaders[6];
bool editsChanged = false;
QString staleReason;
for(ShaderStage s : values<ShaderStage>())
{
uint32_t idx = (uint32_t)s;
shaders[idx] = m_Ctx.GetResourceReplacement(m_OrigShaders[idx]);
// either an edit has been applied, updated, or removed if these don't match
if(shaders[idx] != m_ReplacedShaders[idx])
{
editsChanged = true;
staleReason += QFormatStr(", %1").arg(ToQStr(s, m_API));
}
}
// if the edits haven't changed, just skip
if(!editsChanged)
return;
// if it's the current event we can update with the latest
if(m_EID == eventId)
{
m_Messages = m_Ctx.CurPipelineState().GetShaderMessages();
// not stale anymore
ui->staleStatus->hide();
// update current set of replaced shaders
memcpy(m_ReplacedShaders, shaders, sizeof(m_ReplacedShaders));
refreshMessages();
}
else
{
staleReason.remove(0, 2);
// otherwise we can't - just update the stale status
ui->staleStatus->show();
ui->staleStatus->setText(
tr("Messages are stale because edits to %1 shaders have changed since they were fetched.\n"
"Select the event @%2 to refresh.")
.arg(staleReason)
.arg(m_EID));
ui->messages->beginUpdate();
for(int i = 0; i < ui->messages->topLevelItemCount(); i++)
ui->messages->topLevelItem(i)->setItalic(true);
ui->messages->endUpdate();
}
}
void ShaderMessageViewer::exportText()
{
exportData(false);
}
void ShaderMessageViewer::exportCSV()
{
exportData(true);
}
void ShaderMessageViewer::exportData(bool csv)
{
QString filter;
QString title;
if(csv)
{
filter = tr("CSV Files (*.csv)");
title = tr("Export buffer to CSV");
}
else
{
filter = tr("Text Files (*.txt)");
title = tr("Export buffer to text");
}
QString filename =
RDDialog::getSaveFileName(this, title, QString(), tr("%1;;All files (*)").arg(filter));
if(filename.isEmpty())
return;
QFile *f = new QFile(filename);
QIODevice::OpenMode flags = QIODevice::WriteOnly | QFile::Truncate | QIODevice::Text;
if(!f->open(flags))
{
delete f;
RDDialog::critical(this, tr("Error exporting file"),
tr("Couldn't open file '%1' for writing").arg(filename));
return;
}
LambdaThread *exportThread = new LambdaThread([this, csv, f]() {
QTextStream s(f);
bool compute = (m_OrigShaders[5] != ResourceId());
if(csv)
{
if(compute)
s << tr("Workgroup,Thread,Message\n");
else
s << tr("Location,Message\n");
}
const int start = compute ? 1 : 2;
const int end = 3;
int locationWidth = 0;
for(int i = 0; i < ui->messages->topLevelItemCount(); i++)
{
RDTreeWidgetItem *node = ui->messages->topLevelItem(i);
locationWidth = qMax(locationWidth, node->text(start).length());
if(compute)
locationWidth = qMax(locationWidth, node->text(start + 1).length());
}
for(int i = 0; i < ui->messages->topLevelItemCount(); i++)
{
RDTreeWidgetItem *node = ui->messages->topLevelItem(i);
if(csv)
{
int col = start;
for(; col <= end - 1; col++)
s << "\"" << node->text(col) << "\",";
s << "\"" << node->text(col).replace(QLatin1Char('"'), lit("\"\"")) << "\"\n";
}
else
{
int col = start;
for(; col <= end - 1; col++)
s << QFormatStr("%1").arg(node->text(col), -locationWidth) << "\t";
s << node->text(col) << "\n";
}
}
f->close();
delete f;
});
exportThread->start();
// wait a short while before displaying the progress dialog (which won't show if we're already
// done by the time we reach it)
for(int i = 0; exportThread->isRunning() && i < 100; i++)
QThread::msleep(5);
ShowProgressDialog(this, tr("Exporting messages"),
[exportThread]() { return !exportThread->isRunning(); });
exportThread->deleteLater();
}
void ShaderMessageViewer::refreshMessages()
{
ShaderStageMask mask = ShaderStageMask::Compute;
if(!m_Action || !(m_Action->flags & ActionFlags::Dispatch))
{
mask = ShaderStageMask::Unknown;
if(ui->vertex->isChecked())
mask |= ShaderStageMask::Vertex;
if(ui->hull->isChecked())
mask |= ShaderStageMask::Hull;
if(ui->domain->isChecked())
mask |= ShaderStageMask::Domain;
if(ui->geometry->isChecked())
mask |= ShaderStageMask::Geometry;
if(ui->pixel->isChecked())
mask |= ShaderStageMask::Pixel;
}
int vs = ui->messages->verticalScrollBar()->value();
int curMsg = -1;
{
RDTreeWidgetItem *item = ui->messages->currentItem();
if(item)
curMsg = item->tag().toInt();
}
RDTreeWidgetItem *newCurrentItem = NULL;
ui->messages->beginUpdate();
ui->messages->clear();
QString filter = ui->filter->text().trimmed();
const ShaderReflection *vsrefl = m_Ctx.CurPipelineState().GetShaderReflection(ShaderStage::Vertex);
const ShaderReflection *psrefl = m_Ctx.CurPipelineState().GetShaderReflection(ShaderStage::Pixel);
const ShaderReflection *csrefl = m_Ctx.CurPipelineState().GetShaderReflection(ShaderStage::Compute);
for(int i = 0; i < m_Messages.count(); i++)
{
const ShaderMessage &msg = m_Messages[i];
// filter by stages
if(!(MaskForStage(msg.stage) & mask))
continue;
QString text(msg.message);
const ShaderReflection *refl = NULL;
QString location;
if(msg.stage == ShaderStage::Vertex)
{
refl = vsrefl;
// only show the view if the draw has multiview enabled
if(m_Multiview)
{
location += lit("View %1, ").arg(msg.location.vertex.view);
}
// only show the instance if the draw is actually instanced
if(m_Action && (m_Action->flags & ActionFlags::Instanced) && m_Action->numInstances > 1)
{
location += lit("Inst %1, ").arg(msg.location.vertex.instance);
}
if(m_Action && (m_Action->flags & ActionFlags::Indexed))
{
location += lit("Idx %1").arg(msg.location.vertex.vertexIndex);
}
else
{
location += lit("Vert %1").arg(msg.location.vertex.vertexIndex);
}
}
else if(msg.stage == ShaderStage::Pixel)
{
refl = psrefl;
location = QFormatStr("%1 %2,%3")
.arg(IsD3D(m_API) ? lit("Pixel") : lit("Frag"))
.arg(msg.location.pixel.x)
.arg(msg.location.pixel.y);
if(msg.location.pixel.primitive == ~0U)
location += lit(", Prim ?");
else
location += lit(", Prim %1").arg(msg.location.pixel.primitive);
// only show the view if the draw has multiview enabled
if(m_Multiview)
{
location += lit(", View %1").arg(msg.location.pixel.view);
}
if(m_Multisampled && msg.location.pixel.sample != ~0U)
{
location += lit(", Samp %1").arg(msg.location.pixel.sample);
}
}
else if(msg.stage == ShaderStage::Compute)
{
refl = csrefl;
}
else if(msg.stage == ShaderStage::Geometry)
{
location = lit("Geometry Prim %1").arg(msg.location.geometry.primitive);
// only show the view if the draw has multiview enabled
if(m_Multiview)
{
location += lit(", View %1").arg(msg.location.geometry.view);
}
}
else
{
// no location info for other stages
location = tr("Unknown %1").arg(ToQStr(msg.stage, m_Ctx.APIProps().pipelineType));
}
// filter by text on location and messag
if(!filter.isEmpty() && !text.contains(filter, Qt::CaseInsensitive) &&
!location.contains(filter, Qt::CaseInsensitive))
continue;
RDTreeWidgetItem *node = NULL;
if(msg.stage == ShaderStage::Compute)
{
node = new RDTreeWidgetItem({
QString(), QFormatStr("%1, %2, %3")
.arg(msg.location.compute.workgroup[0])
.arg(msg.location.compute.workgroup[1])
.arg(msg.location.compute.workgroup[2]),
QFormatStr("%1, %2, %3")
.arg(msg.location.compute.thread[0])
.arg(msg.location.compute.thread[1])
.arg(msg.location.compute.thread[2]),
text,
});
node->setData(0, debuggableRole, refl && refl->debugInfo.debuggable);
}
else
{
node = new RDTreeWidgetItem({QString(), QString(), location, text});
node->setData(0, debuggableRole, refl && refl->debugInfo.debuggable);
node->setData(1, gotoableRole, msg.stage == ShaderStage::Vertex ||
msg.stage == ShaderStage::Pixel ||
msg.stage == ShaderStage::Geometry);
}
if(node)
{
if(i == curMsg)
newCurrentItem = node;
node->setItalic(ui->staleStatus->isVisible());
node->setTag(i);
ui->messages->addTopLevelItem(node);
}
}
ui->messages->clearSelection();
ui->messages->endUpdate();
ui->messages->verticalScrollBar()->setValue(vs);
if(newCurrentItem)
{
ui->messages->setCurrentItem(newCurrentItem);
ui->messages->scrollToItem(newCurrentItem);
}
}
|