Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 SkSLAnalysis_DEFINED |
| 9 | #define SkSLAnalysis_DEFINED |
| 10 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 11 | #include "include/private/SkSLSampleUsage.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 12 | #include "src/sksl/SkSLDefines.h" |
| 13 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 16 | namespace SkSL { |
| 17 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 18 | class ErrorReporter; |
Ethan Nicholas | 1e9f7f3 | 2020-10-08 05:28:32 -0400 | [diff] [blame] | 19 | class Expression; |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 20 | class ForStatement; |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 21 | class FunctionDeclaration; |
Ethan Nicholas | 6f87de7 | 2020-10-26 15:06:46 -0400 | [diff] [blame] | 22 | class FunctionDefinition; |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 23 | struct LoadedModule; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 24 | struct Program; |
Ethan Nicholas | 1e9f7f3 | 2020-10-08 05:28:32 -0400 | [diff] [blame] | 25 | class ProgramElement; |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 26 | class ProgramUsage; |
Ethan Nicholas | 1e9f7f3 | 2020-10-08 05:28:32 -0400 | [diff] [blame] | 27 | class Statement; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 28 | class Variable; |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 29 | class VariableReference; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Provides utilities for analyzing SkSL statically before it's composed into a full program. |
| 33 | */ |
| 34 | struct Analysis { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 35 | static SampleUsage GetSampleUsage(const Program& program, const Variable& fp); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 36 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 37 | static bool ReferencesBuiltin(const Program& program, int builtin); |
| 38 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 39 | static bool ReferencesSampleCoords(const Program& program); |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 40 | static bool ReferencesFragCoords(const Program& program); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 41 | |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 42 | static int NodeCountUpToLimit(const FunctionDefinition& function, int limit); |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 43 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 44 | static std::unique_ptr<ProgramUsage> GetUsage(const Program& program); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 45 | static std::unique_ptr<ProgramUsage> GetUsage(const LoadedModule& module); |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 46 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 47 | static bool StatementWritesToVariable(const Statement& stmt, const Variable& var); |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 48 | |
| 49 | struct AssignmentInfo { |
| 50 | VariableReference* fAssignedVar = nullptr; |
| 51 | bool fIsSwizzled = false; |
| 52 | }; |
| 53 | static bool IsAssignable(Expression& expr, AssignmentInfo* info, |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 54 | ErrorReporter* errors = nullptr); |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 55 | |
| 56 | // A "trivial" expression is one where we'd feel comfortable cloning it multiple times in |
| 57 | // the code, without worrying about incurring a performance penalty. Examples: |
| 58 | // - true |
| 59 | // - 3.14159265 |
| 60 | // - myIntVariable |
| 61 | // - myColor.rgb |
| 62 | // - myArray[123] |
| 63 | // - myStruct.myField |
| 64 | // - half4(0) |
| 65 | // |
| 66 | // Trivial-ness is stackable. Somewhat large expressions can occasionally make the cut: |
| 67 | // - half4(myColor.a) |
| 68 | // - myStruct.myArrayField[7].xyz |
| 69 | static bool IsTrivialExpression(const Expression& expr); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 70 | |
| 71 | struct UnrollableLoopInfo { |
| 72 | const Variable* fIndex; |
| 73 | double fStart; |
| 74 | double fDelta; |
| 75 | int fCount; |
| 76 | }; |
| 77 | |
| 78 | // Ensures that 'loop' meets the strict requirements of The OpenGL ES Shading Language 1.00, |
| 79 | // Appendix A, Section 4. |
| 80 | // Information about the loop's structure are placed in outLoopInfo (if not nullptr). |
| 81 | // If the function returns false, specific reasons are reported via errors (if not nullptr). |
| 82 | static bool ForLoopIsValidForES2(const ForStatement& loop, |
| 83 | UnrollableLoopInfo* outLoopInfo, |
| 84 | ErrorReporter* errors); |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 85 | |
| 86 | static void ValidateIndexingForES2(const ProgramElement& pe, ErrorReporter& errors); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * Utility class to visit every element, statement, and expression in an SkSL program IR. |
| 91 | * This is intended for simple analysis and accumulation, where custom visitation behavior is only |
| 92 | * needed for a limited set of expression kinds. |
| 93 | * |
| 94 | * Subclasses should override visitExpression/visitStatement/visitProgramElement as needed and |
| 95 | * intercept elements of interest. They can then invoke the base class's function to visit all |
| 96 | * sub expressions. They can also choose not to call the base function to arrest recursion, or |
| 97 | * implement custom recursion. |
| 98 | * |
| 99 | * The visit functions return a bool that determines how the default implementation recurses. Once |
| 100 | * any visit call returns true, the default behavior stops recursing and propagates true up the |
| 101 | * stack. |
| 102 | */ |
| 103 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 104 | template <typename PROG, typename EXPR, typename STMT, typename ELEM> |
| 105 | class TProgramVisitor { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 106 | public: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 107 | virtual ~TProgramVisitor() = default; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 108 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 109 | protected: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 110 | virtual bool visitExpression(EXPR expression); |
| 111 | virtual bool visitStatement(STMT statement); |
| 112 | virtual bool visitProgramElement(ELEM programElement); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 113 | }; |
| 114 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 115 | // Squelch bogus Clang warning about template vtables: https://bugs.llvm.org/show_bug.cgi?id=18733 |
| 116 | #if defined(__clang__) |
| 117 | #pragma clang diagnostic push |
| 118 | #pragma clang diagnostic ignored "-Wweak-template-vtables" |
| 119 | #endif |
| 120 | extern template class TProgramVisitor<const Program&, const Expression&, |
| 121 | const Statement&, const ProgramElement&>; |
| 122 | extern template class TProgramVisitor<Program&, Expression&, Statement&, ProgramElement&>; |
| 123 | #if defined(__clang__) |
| 124 | #pragma clang diagnostic pop |
| 125 | #endif |
| 126 | |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 127 | class ProgramVisitor : public TProgramVisitor<const Program&, |
| 128 | const Expression&, |
| 129 | const Statement&, |
| 130 | const ProgramElement&> { |
| 131 | public: |
| 132 | bool visit(const Program& program); |
| 133 | }; |
| 134 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 135 | using ProgramWriter = TProgramVisitor<Program&, Expression&, Statement&, ProgramElement&>; |
| 136 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 137 | } // namespace SkSL |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 138 | |
| 139 | #endif |