• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SKSLDEBUGTRACEPRIV
9 #define SKSLDEBUGTRACEPRIV
10 
11 #include "include/core/SkPoint.h"
12 #include "include/sksl/SkSLDebugTrace.h"
13 #include "src/sksl/SkSLPosition.h"
14 #include "src/sksl/ir/SkSLType.h"
15 #include "src/sksl/tracing/SkSLTraceHook.h"
16 
17 #include <cstdint>
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 class SkWStream;
23 
24 namespace SkSL {
25 
26 struct TraceInfo {
27     enum class Op {
28         kLine,  /** data: line number, (unused) */
29         kVar,   /** data: slot, value */
30         kEnter, /** data: function index, (unused) */
31         kExit,  /** data: function index, (unused) */
32         kScope, /** data: scope delta, (unused) */
33     };
34     Op op;
35     int32_t data[2];
36 };
37 
38 struct SlotDebugInfo {
39     /** The full name of this variable (without component), e.g. `myArray[3].myStruct.myVector` */
40     std::string             name;
41     /** The dimensions of this variable: 1x1 is a scalar, Nx1 is a vector, NxM is a matrix. */
42     uint8_t                 columns = 1, rows = 1;
43     /** Which component of the variable is this slot? (e.g. `vec4.z` is component 2) */
44     uint8_t                 componentIndex = 0;
45     /** Complex types (arrays/structs) can be tracked as a "group" of adjacent slots. */
46     int                     groupIndex = 0;
47     /** What kind of numbers belong in this slot? */
48     SkSL::Type::NumberKind  numberKind = SkSL::Type::NumberKind::kNonnumeric;
49     /** Where is this variable located in the program? */
50     int                     line = 0;
51     Position                pos = {};
52     /** If this slot holds a function's return value, contains 1; if not, -1. */
53     int                     fnReturnValue = -1;
54 };
55 
56 struct FunctionDebugInfo {
57     /** Full function declaration: `float myFunction(half4 color)`) */
58     std::string             name;
59 };
60 
61 class DebugTracePriv : public DebugTrace {
62 public:
63     /**
64      * Sets the device-coordinate pixel to trace. If it's not set, the point at (0, 0) will be used.
65      */
66     void setTraceCoord(const SkIPoint& coord);
67 
68     /** Attaches the SkSL source to be debugged. */
69     void setSource(const std::string& source);
70 
71     /** Generates a human-readable dump of the debug trace. */
72     void dump(SkWStream* o) const override;
73 
74     /** Returns a slot's component as a variable-name suffix, e.g. ".x" or "[2][2]". */
75     std::string getSlotComponentSuffix(int slotIndex) const;
76 
77     /** Bit-casts a slot's value, then converts to text, e.g. "3.14" or "true" or "12345". */
78     std::string getSlotValue(int slotIndex, int32_t value) const;
79 
80     /** Bit-casts a value for a given slot into a double, honoring the slot's NumberKind. */
81     double interpretValueBits(int slotIndex, int32_t valueBits) const;
82 
83     /** Converts a numeric value into text, based on the slot's NumberKind. */
84     std::string slotValueToString(int slotIndex, double value) const;
85 
86     /** The device-coordinate pixel to trace (controlled by setTraceCoord) */
87     SkIPoint fTraceCoord = {};
88 
89     /** SkRP stores uniform slot info in fUniformInfo. (In SkVM, they were mixed into fSlotInfo.) */
90     std::vector<SlotDebugInfo> fUniformInfo;
91 
92     /** A 1:1 mapping of slot numbers to debug information. */
93     std::vector<SlotDebugInfo> fSlotInfo;
94     std::vector<FunctionDebugInfo> fFuncInfo;
95 
96     /** The SkSL debug trace. */
97     std::vector<TraceInfo> fTraceInfo;
98 
99     /** The SkSL code, split line-by-line. */
100     std::vector<std::string> fSource;
101 
102     /**
103      * A trace hook which populates fTraceInfo during shader evaluation. This will be created
104      * automatically during code generation.
105      */
106     std::unique_ptr<SkSL::TraceHook> fTraceHook;
107 };
108 
109 }  // namespace SkSL
110 
111 #endif
112