From 97119e5b3c1e5b8636e8160af4f782a5387e1a17 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Wed, 8 Dec 2021 10:34:52 -0500 Subject: [PATCH] Add "Write Debug Trace/Dump" buttons to SkSL slide in Viewer. Click these buttons to trace execution of the current SkSL program. This demonstrates end-to-end debug trace functionality in SkRuntimeEffect. Change-Id: I684099e337d1d275e444eb33dfa3a9e99343bb17 Bug: skia:12708 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/481336 Auto-Submit: John Stiles Reviewed-by: Brian Osman Commit-Queue: John Stiles --- tools/viewer/SkSLSlide.cpp | 28 +++++++++++++++++++++++++++- tools/viewer/SkSLSlide.h | 1 + 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/tools/viewer/SkSLSlide.cpp b/tools/viewer/SkSLSlide.cpp index 96583450db..cfe14fb19a 100644 --- a/tools/viewer/SkSLSlide.cpp +++ b/tools/viewer/SkSLSlide.cpp @@ -11,6 +11,7 @@ #include "include/core/SkFont.h" #include "include/effects/SkGradientShader.h" #include "include/effects/SkPerlinNoiseShader.h" +#include "include/sksl/SkSLDebugTrace.h" #include "src/core/SkEnumerate.h" #include "tools/Resources.h" #include "tools/viewer/Viewer.h" @@ -140,6 +141,14 @@ void SkSLSlide::draw(SkCanvas* canvas) { return; } + bool writeTrace = false; + bool writeDump = false; + if (!canvas->recordingContext()) { + ImGui::InputInt2("Trace Coordinate (X/Y)", fTraceCoord); + writeTrace = ImGui::Button("Write Debug Trace (JSON)"); + writeDump = ImGui::Button("Write Debug Dump (Human-Readable)"); + } + // Update fMousePos ImVec2 mousePos = ImGui::GetMousePos(); if (ImGui::IsMouseDown(0)) { @@ -246,9 +255,17 @@ void SkSLSlide::draw(SkCanvas* canvas) { ImGui::End(); auto inputs = SkData::MakeWithoutCopy(fInputs.get(), fEffect->uniformSize()); + + sk_sp debugTrace; auto shader = fEffect->makeShader(std::move(inputs), fChildren.data(), fChildren.count(), nullptr, false); - + if (writeTrace || writeDump) { + SkIPoint traceCoord = {fTraceCoord[0], fTraceCoord[1]}; + SkRuntimeEffect::TracedShader traced = SkRuntimeEffect::MakeTraced(std::move(shader), + traceCoord); + shader = std::move(traced.shader); + debugTrace = std::move(traced.debugTrace); + } SkPaint p; p.setColor4f(gPaintColor); p.setShader(std::move(shader)); @@ -274,6 +291,15 @@ void SkSLSlide::draw(SkCanvas* canvas) { } break; default: break; } + + if (debugTrace && writeTrace) { + SkFILEWStream traceFile("SkVMDebugTrace.json"); + debugTrace->writeTrace(&traceFile); + } + if (debugTrace && writeDump) { + SkFILEWStream dumpFile("SkVMDebugTrace.dump.txt"); + debugTrace->dump(&dumpFile); + } } bool SkSLSlide::animate(double nanos) { diff --git a/tools/viewer/SkSLSlide.h b/tools/viewer/SkSLSlide.h index 75810952a7..bb6cdc5a45 100644 --- a/tools/viewer/SkSLSlide.h +++ b/tools/viewer/SkSLSlide.h @@ -51,6 +51,7 @@ private: int fGeometry = kFill; SkV3 fResolution = { 1, 1, 1 }; SkV4 fMousePos; + int fTraceCoord[2] = {64, 64}; // Named shaders that can be selected as inputs SkTArray>> fShaders;