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_DSL_CORE |
| 9 | #define SKSL_DSL_CORE |
| 10 | |
Ethan Nicholas | ee49efc | 2021-04-09 15:33:53 -0400 | [diff] [blame] | 11 | #include "include/private/SkSLProgramKind.h" |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 12 | #include "include/private/SkTArray.h" |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 13 | #include "include/sksl/DSLBlock.h" |
| 14 | #include "include/sksl/DSLCase.h" |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 15 | #include "include/sksl/DSLExpression.h" |
| 16 | #include "include/sksl/DSLFunction.h" |
| 17 | #include "include/sksl/DSLStatement.h" |
| 18 | #include "include/sksl/DSLType.h" |
| 19 | #include "include/sksl/DSLVar.h" |
Ethan Nicholas | a1a0b92 | 2021-05-04 12:22:02 -0400 | [diff] [blame] | 20 | #include "include/sksl/DSLWrapper.h" |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 21 | #include "include/sksl/SkSLErrorReporter.h" |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 22 | |
Ethan Nicholas | 2280058 | 2021-09-16 10:42:24 -0400 | [diff] [blame^] | 23 | #define SKSL_DSL_PARSER 1 |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 24 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 25 | namespace SkSL { |
| 26 | |
| 27 | class Compiler; |
Ethan Nicholas | 4f3e6a2 | 2021-06-15 09:17:05 -0400 | [diff] [blame] | 28 | struct Program; |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 29 | struct ProgramSettings; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 30 | |
| 31 | namespace dsl { |
| 32 | |
John Stiles | 9b7ad1b | 2021-02-18 16:06:17 -0500 | [diff] [blame] | 33 | // When users import the DSL namespace via `using namespace SkSL::dsl`, we want the SwizzleComponent |
| 34 | // Type enum to come into scope as well, so `Swizzle(var, X, Y, ONE)` can work as expected. |
| 35 | // `namespace SkSL::SwizzleComponent` contains only an `enum Type`; this `using namespace` directive |
| 36 | // shouldn't pollute the SkSL::dsl namespace with anything else. |
| 37 | using namespace SkSL::SwizzleComponent; |
| 38 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 39 | /** |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 40 | * Starts DSL output on the current thread using the specified compiler. This must be called |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 41 | * prior to any other DSL functions. |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 42 | */ |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 43 | void Start(SkSL::Compiler* compiler, SkSL::ProgramKind kind = SkSL::ProgramKind::kFragment); |
| 44 | |
| 45 | void Start(SkSL::Compiler* compiler, SkSL::ProgramKind kind, const SkSL::ProgramSettings& settings); |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Signals the end of DSL output. This must be called sometime between a call to Start() and the |
| 49 | * termination of the thread. |
| 50 | */ |
| 51 | void End(); |
| 52 | |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 53 | /** |
Ethan Nicholas | b18c1e2 | 2021-07-16 09:53:53 -0400 | [diff] [blame] | 54 | * Returns all global elements (functions and global variables) as a self-contained Program. The |
| 55 | * optional source string is retained as the program's source. DSL programs do not normally have |
| 56 | * sources, but when a DSL program is produced from parsed program text (as in DSLParser), it may be |
| 57 | * important to retain it so that any skstd::string_views derived from it remain valid. |
Ethan Nicholas | 4f3e6a2 | 2021-06-15 09:17:05 -0400 | [diff] [blame] | 58 | */ |
Ethan Nicholas | b18c1e2 | 2021-07-16 09:53:53 -0400 | [diff] [blame] | 59 | std::unique_ptr<SkSL::Program> ReleaseProgram(std::unique_ptr<SkSL::String> source = nullptr); |
Ethan Nicholas | 4f3e6a2 | 2021-06-15 09:17:05 -0400 | [diff] [blame] | 60 | |
| 61 | /** |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 62 | * Returns the ErrorReporter which will be notified of any errors that occur during DSL calls. The |
| 63 | * default error reporter aborts on any error. |
Ethan Nicholas | 65e368b | 2021-08-05 14:53:02 -0400 | [diff] [blame] | 64 | */ |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 65 | ErrorReporter& GetErrorReporter(); |
Ethan Nicholas | 65e368b | 2021-08-05 14:53:02 -0400 | [diff] [blame] | 66 | |
| 67 | /** |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 68 | * Installs an ErrorReporter which will be notified of any errors that occur during DSL calls. |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 69 | */ |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 70 | void SetErrorReporter(ErrorReporter* errorReporter); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 71 | |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 72 | DSLGlobalVar sk_FragColor(); |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 73 | |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 74 | DSLGlobalVar sk_FragCoord(); |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 75 | |
Ethan Nicholas | ebc9fad | 2021-07-09 15:35:23 -0400 | [diff] [blame] | 76 | DSLExpression sk_Position(); |
| 77 | |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 78 | /** |
Ethan Nicholas | 642215e | 2021-09-03 11:10:54 -0400 | [diff] [blame] | 79 | * #extension <name> : enable |
| 80 | */ |
| 81 | void AddExtension(skstd::string_view name, PositionInfo pos = PositionInfo::Capture()); |
| 82 | |
| 83 | /** |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 84 | * break; |
| 85 | */ |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 86 | DSLStatement Break(PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * continue; |
| 90 | */ |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 91 | DSLStatement Continue(PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 92 | |
| 93 | /** |
Ethan Nicholas | 678ec71 | 2021-09-03 06:33:47 -0400 | [diff] [blame] | 94 | * Adds a modifiers declaration to the current program. |
| 95 | */ |
| 96 | void Declare(const DSLModifiers& modifiers, PositionInfo pos = PositionInfo::Capture()); |
| 97 | |
| 98 | /** |
Ethan Nicholas | 4f3e6a2 | 2021-06-15 09:17:05 -0400 | [diff] [blame] | 99 | * Creates a local variable declaration statement. |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 100 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 101 | DSLStatement Declare(DSLVar& var, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 102 | |
| 103 | /** |
Ethan Nicholas | da72978 | 2021-07-19 12:55:52 -0400 | [diff] [blame] | 104 | * Creates a local variable declaration statement containing multiple variables. |
| 105 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 106 | DSLStatement Declare(SkTArray<DSLVar>& vars, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | da72978 | 2021-07-19 12:55:52 -0400 | [diff] [blame] | 107 | |
| 108 | /** |
Ethan Nicholas | e9c2c5a | 2021-04-30 13:14:24 -0400 | [diff] [blame] | 109 | * Declares a global variable. |
| 110 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 111 | void Declare(DSLGlobalVar& var, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | e9c2c5a | 2021-04-30 13:14:24 -0400 | [diff] [blame] | 112 | |
| 113 | /** |
Ethan Nicholas | da72978 | 2021-07-19 12:55:52 -0400 | [diff] [blame] | 114 | * Declares a set of global variables. |
| 115 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 116 | void Declare(SkTArray<DSLGlobalVar>& vars, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | da72978 | 2021-07-19 12:55:52 -0400 | [diff] [blame] | 117 | |
| 118 | /** |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 119 | * default: statements |
| 120 | */ |
| 121 | template<class... Statements> |
| 122 | DSLCase Default(Statements... statements) { |
| 123 | return DSLCase(DSLExpression(), std::move(statements)...); |
| 124 | } |
| 125 | |
| 126 | /** |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 127 | * discard; |
| 128 | */ |
Ethan Nicholas | 360db87 | 2021-09-03 17:00:09 -0400 | [diff] [blame] | 129 | DSLStatement Discard(PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 130 | |
| 131 | /** |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 132 | * do stmt; while (test); |
| 133 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 134 | DSLStatement Do(DSLStatement stmt, DSLExpression test, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * for (initializer; test; next) stmt; |
| 138 | */ |
| 139 | DSLStatement For(DSLStatement initializer, DSLExpression test, DSLExpression next, |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 140 | DSLStatement stmt, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * if (test) ifTrue; [else ifFalse;] |
| 144 | */ |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 145 | DSLStatement If(DSLExpression test, DSLStatement ifTrue, DSLStatement ifFalse = DSLStatement(), |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 146 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 147 | |
John Stiles | e53c721 | 2021-08-05 10:19:11 -0400 | [diff] [blame] | 148 | DSLGlobalVar InterfaceBlock(const DSLModifiers& modifiers, skstd::string_view typeName, |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 149 | SkTArray<DSLField> fields, skstd::string_view varName = "", |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 150 | int arraySize = 0, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 151 | |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 152 | /** |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 153 | * return [value]; |
| 154 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 155 | DSLStatement Return(DSLExpression value = DSLExpression(), |
| 156 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * test ? ifTrue : ifFalse |
| 160 | */ |
| 161 | DSLExpression Select(DSLExpression test, DSLExpression ifTrue, DSLExpression ifFalse, |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 162 | PositionInfo info = PositionInfo::Capture()); |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 163 | |
Ethan Nicholas | 8a6537d | 2021-04-30 12:44:00 -0400 | [diff] [blame] | 164 | DSLStatement StaticIf(DSLExpression test, DSLStatement ifTrue, |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 165 | DSLStatement ifFalse = DSLStatement(), |
| 166 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 8a6537d | 2021-04-30 12:44:00 -0400 | [diff] [blame] | 167 | |
Ethan Nicholas | 360db87 | 2021-09-03 17:00:09 -0400 | [diff] [blame] | 168 | DSLPossibleStatement StaticSwitch(DSLExpression value, SkTArray<DSLCase> cases, |
| 169 | PositionInfo info = PositionInfo::Capture()); |
Ethan Nicholas | 8a6537d | 2021-04-30 12:44:00 -0400 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * @switch (value) { cases } |
| 173 | */ |
| 174 | template<class... Cases> |
| 175 | DSLPossibleStatement StaticSwitch(DSLExpression value, Cases... cases) { |
| 176 | SkTArray<DSLCase> caseArray; |
| 177 | caseArray.reserve_back(sizeof...(cases)); |
| 178 | (caseArray.push_back(std::move(cases)), ...); |
| 179 | return StaticSwitch(std::move(value), std::move(caseArray)); |
| 180 | } |
| 181 | |
Ethan Nicholas | 360db87 | 2021-09-03 17:00:09 -0400 | [diff] [blame] | 182 | DSLPossibleStatement Switch(DSLExpression value, SkTArray<DSLCase> cases, |
| 183 | PositionInfo info = PositionInfo::Capture()); |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 184 | |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 185 | /** |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 186 | * switch (value) { cases } |
| 187 | */ |
| 188 | template<class... Cases> |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 189 | DSLPossibleStatement Switch(DSLExpression value, Cases... cases) { |
Ethan Nicholas | 8a6537d | 2021-04-30 12:44:00 -0400 | [diff] [blame] | 190 | SkTArray<DSLCase> caseArray; |
| 191 | caseArray.reserve_back(sizeof...(cases)); |
| 192 | (caseArray.push_back(std::move(cases)), ...); |
| 193 | return Switch(std::move(value), std::move(caseArray)); |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 197 | * while (test) stmt; |
| 198 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 199 | DSLStatement While(DSLExpression test, DSLStatement stmt, |
| 200 | PositionInfo info = PositionInfo::Capture()); |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 201 | |
John Stiles | 9b7ad1b | 2021-02-18 16:06:17 -0500 | [diff] [blame] | 202 | /** |
| 203 | * expression.xyz1 |
| 204 | */ |
John Stiles | 9b7ad1b | 2021-02-18 16:06:17 -0500 | [diff] [blame] | 205 | DSLExpression Swizzle(DSLExpression base, |
| 206 | SkSL::SwizzleComponent::Type a, |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 207 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 68c77d4 | 2021-01-26 14:31:29 -0500 | [diff] [blame] | 208 | |
John Stiles | 9b7ad1b | 2021-02-18 16:06:17 -0500 | [diff] [blame] | 209 | DSLExpression Swizzle(DSLExpression base, |
| 210 | SkSL::SwizzleComponent::Type a, |
| 211 | SkSL::SwizzleComponent::Type b, |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 212 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 68c77d4 | 2021-01-26 14:31:29 -0500 | [diff] [blame] | 213 | |
John Stiles | 9b7ad1b | 2021-02-18 16:06:17 -0500 | [diff] [blame] | 214 | DSLExpression Swizzle(DSLExpression base, |
| 215 | SkSL::SwizzleComponent::Type a, |
| 216 | SkSL::SwizzleComponent::Type b, |
| 217 | SkSL::SwizzleComponent::Type c, |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 218 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 219 | |
| 220 | DSLExpression Swizzle(DSLExpression base, |
| 221 | SkSL::SwizzleComponent::Type a, |
| 222 | SkSL::SwizzleComponent::Type b, |
| 223 | SkSL::SwizzleComponent::Type c, |
| 224 | SkSL::SwizzleComponent::Type d, |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 225 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 68c77d4 | 2021-01-26 14:31:29 -0500 | [diff] [blame] | 226 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 227 | /** |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 228 | * Returns the absolute value of x. If x is a vector, operates componentwise. |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 229 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 230 | DSLExpression Abs(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * Returns true if all of the components of boolean vector x are true. |
| 234 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 235 | DSLExpression All(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * Returns true if any of the components of boolean vector x are true. |
| 239 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 240 | DSLExpression Any(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 241 | |
| 242 | /** |
John Stiles | e3fa745 | 2021-04-26 09:36:07 -0400 | [diff] [blame] | 243 | * Returns the arctangent of y over x. Operates componentwise on vectors. |
| 244 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 245 | DSLExpression Atan(DSLExpression y_over_x, PositionInfo pos = PositionInfo::Capture()); |
| 246 | DSLExpression Atan(DSLExpression y, DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
John Stiles | e3fa745 | 2021-04-26 09:36:07 -0400 | [diff] [blame] | 247 | |
| 248 | /** |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 249 | * Returns x rounded towards positive infinity. If x is a vector, operates componentwise. |
| 250 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 251 | DSLExpression Ceil(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 252 | |
| 253 | /** |
| 254 | * Returns x clamped to between min and max. If x is a vector, operates componentwise. |
| 255 | */ |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 256 | DSLExpression Clamp(DSLExpression x, DSLExpression min, DSLExpression max, |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 257 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * Returns the cosine of x. If x is a vector, operates componentwise. |
| 261 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 262 | DSLExpression Cos(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 263 | |
| 264 | /** |
| 265 | * Returns the cross product of x and y. |
| 266 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 267 | DSLExpression Cross(DSLExpression x, DSLExpression y, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 268 | |
| 269 | /** |
| 270 | * Returns x converted from radians to degrees. If x is a vector, operates componentwise. |
| 271 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 272 | DSLExpression Degrees(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * Returns the distance between x and y. |
| 276 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 277 | DSLExpression Distance(DSLExpression x, DSLExpression y, |
| 278 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 279 | |
| 280 | /** |
| 281 | * Returns the dot product of x and y. |
| 282 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 283 | DSLExpression Dot(DSLExpression x, DSLExpression y, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 284 | |
| 285 | /** |
| 286 | * Returns a boolean vector indicating whether components of x are equal to the corresponding |
| 287 | * components of y. |
| 288 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 289 | DSLExpression Equal(DSLExpression x, DSLExpression y, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 290 | |
| 291 | /** |
| 292 | * Returns e^x. If x is a vector, operates componentwise. |
| 293 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 294 | DSLExpression Exp(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 295 | |
| 296 | /** |
| 297 | * Returns 2^x. If x is a vector, operates componentwise. |
| 298 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 299 | DSLExpression Exp2(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 300 | |
| 301 | /** |
| 302 | * If dot(i, nref) >= 0, returns n, otherwise returns -n. |
| 303 | */ |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 304 | DSLExpression Faceforward(DSLExpression n, DSLExpression i, DSLExpression nref, |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 305 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 306 | |
| 307 | /** |
| 308 | * Returns x rounded towards negative infinity. If x is a vector, operates componentwise. |
| 309 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 310 | DSLExpression Floor(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 311 | |
| 312 | /** |
| 313 | * Returns the fractional part of x. If x is a vector, operates componentwise. |
| 314 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 315 | DSLExpression Fract(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 316 | |
| 317 | /** |
| 318 | * Returns a boolean vector indicating whether components of x are greater than the corresponding |
| 319 | * components of y. |
| 320 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 321 | DSLExpression GreaterThan(DSLExpression x, DSLExpression y, |
| 322 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 323 | |
| 324 | /** |
| 325 | * Returns a boolean vector indicating whether components of x are greater than or equal to the |
| 326 | * corresponding components of y. |
| 327 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 328 | DSLExpression GreaterThanEqual(DSLExpression x, DSLExpression y, |
| 329 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 330 | |
| 331 | /** |
| 332 | * Returns the 1/sqrt(x). If x is a vector, operates componentwise. |
| 333 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 334 | DSLExpression Inversesqrt(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * Returns the inverse of the matrix x. |
| 338 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 339 | DSLExpression Inverse(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 340 | |
| 341 | /** |
| 342 | * Returns the length of the vector x. |
| 343 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 344 | DSLExpression Length(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 345 | |
| 346 | /** |
| 347 | * Returns a boolean vector indicating whether components of x are less than the corresponding |
| 348 | * components of y. |
| 349 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 350 | DSLExpression LessThan(DSLExpression x, DSLExpression y, |
| 351 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 352 | |
| 353 | /** |
| 354 | * Returns a boolean vector indicating whether components of x are less than or equal to the |
| 355 | * corresponding components of y. |
| 356 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 357 | DSLExpression LessThanEqual(DSLExpression x, DSLExpression y, |
| 358 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 359 | |
| 360 | /** |
| 361 | * Returns the log base e of x. If x is a vector, operates componentwise. |
| 362 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 363 | DSLExpression Log(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 364 | |
| 365 | /** |
| 366 | * Returns the log base 2 of x. If x is a vector, operates componentwise. |
| 367 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 368 | DSLExpression Log2(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 369 | |
| 370 | /** |
| 371 | * Returns the larger (closer to positive infinity) of x and y. If x is a vector, operates |
| 372 | * componentwise. y may be either a vector of the same dimensions as x, or a scalar. |
| 373 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 374 | DSLExpression Max(DSLExpression x, DSLExpression y, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 375 | |
| 376 | /** |
| 377 | * Returns the smaller (closer to negative infinity) of x and y. If x is a vector, operates |
| 378 | * componentwise. y may be either a vector of the same dimensions as x, or a scalar. |
| 379 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 380 | DSLExpression Min(DSLExpression x, DSLExpression y, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 381 | |
| 382 | /** |
| 383 | * Returns a linear intepolation between x and y at position a, where a=0 results in x and a=1 |
| 384 | * results in y. If x and y are vectors, operates componentwise. a may be either a vector of the |
| 385 | * same dimensions as x and y, or a scalar. |
| 386 | */ |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 387 | DSLExpression Mix(DSLExpression x, DSLExpression y, DSLExpression a, |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 388 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 389 | |
| 390 | /** |
| 391 | * Returns x modulo y. If x is a vector, operates componentwise. y may be either a vector of the |
| 392 | * same dimensions as x, or a scalar. |
| 393 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 394 | DSLExpression Mod(DSLExpression x, DSLExpression y, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 395 | |
| 396 | /** |
| 397 | * Returns the vector x normalized to a length of 1. |
| 398 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 399 | DSLExpression Normalize(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 400 | |
| 401 | /** |
| 402 | * Returns a boolean vector indicating whether components of x are not equal to the corresponding |
| 403 | * components of y. |
| 404 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 405 | DSLExpression NotEqual(DSLExpression x, DSLExpression y, |
| 406 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 407 | |
| 408 | /** |
| 409 | * Returns x raised to the power y. If x is a vector, operates componentwise. y may be either a |
| 410 | * vector of the same dimensions as x, or a scalar. |
| 411 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 412 | DSLExpression Pow(DSLExpression x, DSLExpression y, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 413 | |
| 414 | /** |
| 415 | * Returns x converted from degrees to radians. If x is a vector, operates componentwise. |
| 416 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 417 | DSLExpression Radians(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 418 | |
| 419 | /** |
| 420 | * Returns i reflected from a surface with normal n. |
| 421 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 422 | DSLExpression Reflect(DSLExpression i, DSLExpression n, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 423 | |
| 424 | /** |
| 425 | * Returns i refracted across a surface with normal n and ratio of indices of refraction eta. |
| 426 | */ |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 427 | DSLExpression Refract(DSLExpression i, DSLExpression n, DSLExpression eta, |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 428 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 429 | |
| 430 | /** |
John Stiles | 2533953 | 2021-09-08 17:50:09 -0400 | [diff] [blame] | 431 | * Returns x, rounded to the nearest integer. If x is a vector, operates componentwise. |
| 432 | */ |
| 433 | DSLExpression Round(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
| 434 | |
| 435 | /** |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 436 | * Returns x clamped to the range [0, 1]. If x is a vector, operates componentwise. |
| 437 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 438 | DSLExpression Saturate(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 439 | |
| 440 | /** |
| 441 | * Returns -1, 0, or 1 depending on whether x is negative, zero, or positive, respectively. If x is |
| 442 | * a vector, operates componentwise. |
| 443 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 444 | DSLExpression Sign(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 445 | |
| 446 | /** |
| 447 | * Returns the sine of x. If x is a vector, operates componentwise. |
| 448 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 449 | DSLExpression Sin(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 450 | |
| 451 | /** |
| 452 | * Returns a smooth interpolation between 0 (at x=edge1) and 1 (at x=edge2). If x is a vector, |
| 453 | * operates componentwise. edge1 and edge2 may either be both vectors of the same dimensions as x or |
| 454 | * scalars. |
| 455 | */ |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 456 | DSLExpression Smoothstep(DSLExpression edge1, DSLExpression edge2, DSLExpression x, |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 457 | PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 458 | |
| 459 | /** |
| 460 | * Returns the square root of x. If x is a vector, operates componentwise. |
| 461 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 462 | DSLExpression Sqrt(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 463 | |
| 464 | /** |
| 465 | * Returns 0 if x < edge or 1 if x >= edge. If x is a vector, operates componentwise. edge may be |
| 466 | * either a vector of the same dimensions as x, or a scalar. |
| 467 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 468 | DSLExpression Step(DSLExpression edge, DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 469 | |
| 470 | /** |
| 471 | * Returns the tangent of x. If x is a vector, operates componentwise. |
| 472 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 473 | DSLExpression Tan(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 474 | |
| 475 | /** |
| 476 | * Returns x converted from premultipled to unpremultiplied alpha. |
| 477 | */ |
Ethan Nicholas | a2fd01c | 2021-08-12 11:41:59 -0400 | [diff] [blame] | 478 | DSLExpression Unpremul(DSLExpression x, PositionInfo pos = PositionInfo::Capture()); |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 479 | |
| 480 | } // namespace dsl |
| 481 | |
| 482 | } // namespace SkSL |
| 483 | |
| 484 | #endif |