Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [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 SKSL_DSLWRITER |
| 9 | #define SKSL_DSLWRITER |
| 10 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 11 | #include "include/core/SkStringView.h" |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame^] | 12 | #include "include/core/SkTypes.h" |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 13 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
Brian Salomon | 4895946 | 2021-08-11 13:01:06 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrFragmentProcessor.h" |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame^] | 15 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 16 | #endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 17 | |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame^] | 18 | #include <memory> |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 19 | |
| 20 | namespace SkSL { |
| 21 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 22 | class Variable; |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame^] | 23 | class Statement; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 24 | |
| 25 | namespace dsl { |
| 26 | |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 27 | class DSLGlobalVar; |
| 28 | class DSLParameter; |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame^] | 29 | class DSLStatement; |
| 30 | class DSLVarBase; |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 31 | class DSLVar; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 32 | |
| 33 | /** |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame^] | 34 | * Various utility methods needed by DSL code. |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 35 | */ |
| 36 | class DSLWriter { |
| 37 | public: |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 38 | /** |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 39 | * Returns whether name mangling is enabled. Mangling is important for the DSL because its |
| 40 | * variables normally all go into the same symbol table; for instance if you were to translate |
| 41 | * this legal (albeit silly) GLSL code: |
| 42 | * int x; |
| 43 | * { |
| 44 | * int x; |
| 45 | * } |
| 46 | * |
| 47 | * into DSL, you'd end up with: |
| 48 | * DSLVar x1(kInt_Type, "x"); |
| 49 | * DSLVar x2(kInt_Type, "x"); |
| 50 | * Declare(x1); |
| 51 | * Block(Declare(x2)); |
| 52 | * |
| 53 | * with x1 and x2 ending up in the same symbol table. This is fine as long as their effective |
| 54 | * names are different, so mangling prevents this situation from causing problems. |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 55 | */ |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame^] | 56 | static bool ManglingEnabled(); |
| 57 | |
| 58 | static skstd::string_view Name(skstd::string_view name); |
| 59 | |
| 60 | /** |
| 61 | * Returns the SkSL variable corresponding to a DSL var. |
| 62 | */ |
| 63 | static const SkSL::Variable* Var(DSLVarBase& var); |
| 64 | |
| 65 | /** |
| 66 | * Creates an SkSL variable corresponding to a DSLParameter. |
| 67 | */ |
| 68 | static std::unique_ptr<SkSL::Variable> CreateParameterVar(DSLParameter& var); |
| 69 | |
| 70 | /** |
| 71 | * Returns the SkSL declaration corresponding to a DSLVar. |
| 72 | */ |
| 73 | static std::unique_ptr<SkSL::Statement> Declaration(DSLVarBase& var); |
| 74 | |
| 75 | /** |
| 76 | * For use in testing only: marks the variable as having been declared, so that it can be |
| 77 | * destroyed without generating errors. |
| 78 | */ |
| 79 | static void MarkDeclared(DSLVarBase& var); |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Returns whether DSLVars should automatically be marked declared upon creation. This is used |
| 83 | * to simplify testing. |
| 84 | */ |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame^] | 85 | static bool MarkVarsDeclared(); |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 86 | |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 87 | /** |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame^] | 88 | * Adds a new declaration into an existing declaration statement. This either turns the original |
| 89 | * declaration into an unscoped block or, if it already was, appends a new statement to the end |
| 90 | * of it. |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 91 | */ |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame^] | 92 | static void AddVarDeclaration(DSLStatement& existing, DSLVar& additional); |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 93 | |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame^] | 94 | /** |
| 95 | * Clears any elements or symbols which have been output. |
| 96 | */ |
| 97 | static void Reset(); |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 98 | |
Ethan Nicholas | c3bb9e3 | 2021-02-02 11:51:03 -0500 | [diff] [blame] | 99 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame^] | 100 | static GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLGlobalVar& var); |
| 101 | #endif |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 102 | |
| 103 | friend class DSLCore; |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 104 | friend class DSLVar; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace dsl |
| 108 | |
| 109 | } // namespace SkSL |
| 110 | |
| 111 | #endif |