Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -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 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 8 | #include "include/sksl/DSLType.h" |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 9 | |
| 10 | #include "src/sksl/dsl/priv/DSLWriter.h" |
| 11 | #include "src/sksl/ir/SkSLConstructor.h" |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 12 | #include "src/sksl/ir/SkSLStructDefinition.h" |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 13 | |
| 14 | namespace SkSL { |
| 15 | |
| 16 | namespace dsl { |
| 17 | |
Ethan Nicholas | 80d2b3e | 2021-09-15 15:11:51 -0400 | [diff] [blame] | 18 | static const Type* find_type(skstd::string_view name, PositionInfo pos) { |
John Stiles | 4adb66f | 2021-08-05 10:15:16 -0400 | [diff] [blame] | 19 | const Symbol* symbol = (*DSLWriter::SymbolTable())[name]; |
| 20 | if (!symbol) { |
Ethan Nicholas | 3272412 | 2021-09-07 13:49:07 -0400 | [diff] [blame] | 21 | DSLWriter::ReportError(String::printf("no symbol named '%.*s'", (int)name.length(), |
Ethan Nicholas | 80d2b3e | 2021-09-15 15:11:51 -0400 | [diff] [blame] | 22 | name.data()), pos); |
John Stiles | 4adb66f | 2021-08-05 10:15:16 -0400 | [diff] [blame] | 23 | return nullptr; |
Ethan Nicholas | f07b4ce | 2021-06-08 08:57:37 -0400 | [diff] [blame] | 24 | } |
John Stiles | 4adb66f | 2021-08-05 10:15:16 -0400 | [diff] [blame] | 25 | if (!symbol->is<Type>()) { |
Ethan Nicholas | 3272412 | 2021-09-07 13:49:07 -0400 | [diff] [blame] | 26 | DSLWriter::ReportError(String::printf("symbol '%.*s' is not a type", (int)name.length(), |
Ethan Nicholas | 80d2b3e | 2021-09-15 15:11:51 -0400 | [diff] [blame] | 27 | name.data()), pos); |
John Stiles | 4adb66f | 2021-08-05 10:15:16 -0400 | [diff] [blame] | 28 | return nullptr; |
| 29 | } |
Ethan Nicholas | 517f4ff | 2021-09-02 10:57:45 -0400 | [diff] [blame] | 30 | const Type& result = symbol->as<Type>(); |
| 31 | if (!DSLWriter::IsModule()) { |
| 32 | if (result.containsPrivateFields()) { |
Ethan Nicholas | 80d2b3e | 2021-09-15 15:11:51 -0400 | [diff] [blame] | 33 | DSLWriter::ReportError("type '" + String(name) + "' is private", pos); |
Ethan Nicholas | 517f4ff | 2021-09-02 10:57:45 -0400 | [diff] [blame] | 34 | return nullptr; |
| 35 | } |
| 36 | if (DSLWriter::Context().fConfig->strictES2Mode() && !result.allowedInES2()) { |
Ethan Nicholas | 80d2b3e | 2021-09-15 15:11:51 -0400 | [diff] [blame] | 37 | DSLWriter::ReportError("type '" + String(name) + "' is not supported", pos); |
Ethan Nicholas | 517f4ff | 2021-09-02 10:57:45 -0400 | [diff] [blame] | 38 | return nullptr; |
| 39 | } |
| 40 | } |
| 41 | return &result; |
Ethan Nicholas | f07b4ce | 2021-06-08 08:57:37 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Ethan Nicholas | a248a9a | 2021-09-01 16:40:25 -0400 | [diff] [blame] | 44 | static const Type* find_type(skstd::string_view name, const Modifiers& modifiers, |
| 45 | PositionInfo pos) { |
Ethan Nicholas | 80d2b3e | 2021-09-15 15:11:51 -0400 | [diff] [blame] | 46 | const Type* type = find_type(name, pos); |
John Stiles | 4adb66f | 2021-08-05 10:15:16 -0400 | [diff] [blame] | 47 | if (!type) { |
| 48 | return nullptr; |
| 49 | } |
Ethan Nicholas | a248a9a | 2021-09-01 16:40:25 -0400 | [diff] [blame] | 50 | const Type* result = type->applyPrecisionQualifiers(DSLWriter::Context(), modifiers, |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 51 | DSLWriter::SymbolTable().get(), /*offset=*/-1); |
Ethan Nicholas | a248a9a | 2021-09-01 16:40:25 -0400 | [diff] [blame] | 52 | DSLWriter::ReportErrors(pos); |
| 53 | return result; |
John Stiles | 4adb66f | 2021-08-05 10:15:16 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | DSLType::DSLType(skstd::string_view name) |
Ethan Nicholas | 80d2b3e | 2021-09-15 15:11:51 -0400 | [diff] [blame] | 57 | : fSkSLType(find_type(name, PositionInfo())) {} |
John Stiles | 4adb66f | 2021-08-05 10:15:16 -0400 | [diff] [blame] | 58 | |
Ethan Nicholas | a248a9a | 2021-09-01 16:40:25 -0400 | [diff] [blame] | 59 | DSLType::DSLType(skstd::string_view name, const DSLModifiers& modifiers, PositionInfo position) |
| 60 | : fSkSLType(find_type(name, modifiers.fModifiers, position)) {} |
John Stiles | 4adb66f | 2021-08-05 10:15:16 -0400 | [diff] [blame] | 61 | |
Ethan Nicholas | b83199e | 2021-05-03 14:25:35 -0400 | [diff] [blame] | 62 | bool DSLType::isBoolean() const { |
| 63 | return this->skslType().isBoolean(); |
| 64 | } |
| 65 | |
| 66 | bool DSLType::isNumber() const { |
| 67 | return this->skslType().isNumber(); |
| 68 | } |
| 69 | |
| 70 | bool DSLType::isFloat() const { |
| 71 | return this->skslType().isFloat(); |
| 72 | } |
| 73 | |
| 74 | bool DSLType::isSigned() const { |
| 75 | return this->skslType().isSigned(); |
| 76 | } |
| 77 | |
| 78 | bool DSLType::isUnsigned() const { |
| 79 | return this->skslType().isUnsigned(); |
| 80 | } |
| 81 | |
| 82 | bool DSLType::isInteger() const { |
| 83 | return this->skslType().isInteger(); |
| 84 | } |
| 85 | |
| 86 | bool DSLType::isScalar() const { |
| 87 | return this->skslType().isScalar(); |
| 88 | } |
| 89 | |
| 90 | bool DSLType::isVector() const { |
| 91 | return this->skslType().isVector(); |
| 92 | } |
| 93 | |
| 94 | bool DSLType::isMatrix() const { |
| 95 | return this->skslType().isMatrix(); |
| 96 | } |
| 97 | |
| 98 | bool DSLType::isArray() const { |
| 99 | return this->skslType().isArray(); |
| 100 | } |
| 101 | |
| 102 | bool DSLType::isStruct() const { |
| 103 | return this->skslType().isStruct(); |
| 104 | } |
| 105 | |
Brian Osman | e76530d | 2021-09-09 14:31:31 -0400 | [diff] [blame] | 106 | bool DSLType::isEffectChild() const { |
| 107 | return this->skslType().isEffectChild(); |
| 108 | } |
| 109 | |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 110 | const SkSL::Type& DSLType::skslType() const { |
| 111 | if (fSkSLType) { |
| 112 | return *fSkSLType; |
| 113 | } |
| 114 | const SkSL::Context& context = DSLWriter::Context(); |
| 115 | switch (fTypeConstant) { |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 116 | case kBool_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 117 | return *context.fTypes.fBool; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 118 | case kBool2_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 119 | return *context.fTypes.fBool2; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 120 | case kBool3_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 121 | return *context.fTypes.fBool3; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 122 | case kBool4_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 123 | return *context.fTypes.fBool4; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 124 | case kHalf_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 125 | return *context.fTypes.fHalf; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 126 | case kHalf2_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 127 | return *context.fTypes.fHalf2; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 128 | case kHalf3_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 129 | return *context.fTypes.fHalf3; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 130 | case kHalf4_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 131 | return *context.fTypes.fHalf4; |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 132 | case kHalf2x2_Type: |
| 133 | return *context.fTypes.fHalf2x2; |
| 134 | case kHalf3x2_Type: |
| 135 | return *context.fTypes.fHalf3x2; |
| 136 | case kHalf4x2_Type: |
| 137 | return *context.fTypes.fHalf4x2; |
| 138 | case kHalf2x3_Type: |
| 139 | return *context.fTypes.fHalf2x3; |
| 140 | case kHalf3x3_Type: |
| 141 | return *context.fTypes.fHalf3x3; |
| 142 | case kHalf4x3_Type: |
| 143 | return *context.fTypes.fHalf4x3; |
| 144 | case kHalf2x4_Type: |
| 145 | return *context.fTypes.fHalf2x4; |
| 146 | case kHalf3x4_Type: |
| 147 | return *context.fTypes.fHalf3x4; |
| 148 | case kHalf4x4_Type: |
| 149 | return *context.fTypes.fHalf4x4; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 150 | case kFloat_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 151 | return *context.fTypes.fFloat; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 152 | case kFloat2_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 153 | return *context.fTypes.fFloat2; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 154 | case kFloat3_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 155 | return *context.fTypes.fFloat3; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 156 | case kFloat4_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 157 | return *context.fTypes.fFloat4; |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 158 | case kFloat2x2_Type: |
| 159 | return *context.fTypes.fFloat2x2; |
| 160 | case kFloat3x2_Type: |
| 161 | return *context.fTypes.fFloat3x2; |
| 162 | case kFloat4x2_Type: |
| 163 | return *context.fTypes.fFloat4x2; |
| 164 | case kFloat2x3_Type: |
| 165 | return *context.fTypes.fFloat2x3; |
| 166 | case kFloat3x3_Type: |
| 167 | return *context.fTypes.fFloat3x3; |
| 168 | case kFloat4x3_Type: |
| 169 | return *context.fTypes.fFloat4x3; |
| 170 | case kFloat2x4_Type: |
| 171 | return *context.fTypes.fFloat2x4; |
| 172 | case kFloat3x4_Type: |
| 173 | return *context.fTypes.fFloat3x4; |
| 174 | case kFloat4x4_Type: |
| 175 | return *context.fTypes.fFloat4x4; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 176 | case kInt_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 177 | return *context.fTypes.fInt; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 178 | case kInt2_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 179 | return *context.fTypes.fInt2; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 180 | case kInt3_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 181 | return *context.fTypes.fInt3; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 182 | case kInt4_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 183 | return *context.fTypes.fInt4; |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 184 | case kShader_Type: |
| 185 | return *context.fTypes.fShader; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 186 | case kShort_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 187 | return *context.fTypes.fShort; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 188 | case kShort2_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 189 | return *context.fTypes.fShort2; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 190 | case kShort3_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 191 | return *context.fTypes.fShort3; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 192 | case kShort4_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 193 | return *context.fTypes.fShort4; |
Ethan Nicholas | b83199e | 2021-05-03 14:25:35 -0400 | [diff] [blame] | 194 | case kUInt_Type: |
| 195 | return *context.fTypes.fUInt; |
| 196 | case kUInt2_Type: |
| 197 | return *context.fTypes.fUInt2; |
| 198 | case kUInt3_Type: |
| 199 | return *context.fTypes.fUInt3; |
| 200 | case kUInt4_Type: |
| 201 | return *context.fTypes.fUInt4; |
| 202 | case kUShort_Type: |
| 203 | return *context.fTypes.fUShort; |
| 204 | case kUShort2_Type: |
| 205 | return *context.fTypes.fUShort2; |
| 206 | case kUShort3_Type: |
| 207 | return *context.fTypes.fUShort3; |
| 208 | case kUShort4_Type: |
| 209 | return *context.fTypes.fUShort4; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 210 | case kVoid_Type: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 211 | return *context.fTypes.fVoid; |
Ethan Nicholas | 5c4463e | 2021-08-29 14:31:19 -0400 | [diff] [blame] | 212 | case kPoison_Type: |
| 213 | return *context.fTypes.fPoison; |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 214 | default: |
| 215 | SkUNREACHABLE; |
| 216 | } |
| 217 | } |
| 218 | |
John Stiles | d6c08c9 | 2021-08-11 11:37:51 -0400 | [diff] [blame] | 219 | DSLExpression DSLType::Construct(DSLType type, SkSpan<DSLExpression> argArray) { |
Ethan Nicholas | 0cb3b82 | 2021-05-03 15:12:14 -0400 | [diff] [blame] | 220 | return DSLWriter::Construct(type.skslType(), std::move(argArray)); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 223 | DSLType Array(const DSLType& base, int count, PositionInfo pos) { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 224 | count = base.skslType().convertArraySize(DSLWriter::Context(), |
| 225 | DSLExpression(count, pos).release()); |
Brian Osman | a909dd6 | 2021-09-24 19:00:35 +0000 | [diff] [blame] | 226 | DSLWriter::ReportErrors(pos); |
Ethan Nicholas | fd1ff92 | 2021-08-31 16:57:53 -0400 | [diff] [blame] | 227 | if (!count) { |
| 228 | return DSLType(kPoison_Type); |
| 229 | } |
Ethan Nicholas | 04be339 | 2021-01-26 10:07:01 -0500 | [diff] [blame] | 230 | return DSLWriter::SymbolTable()->addArrayDimension(&base.skslType(), count); |
| 231 | } |
| 232 | |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 233 | DSLType Struct(skstd::string_view name, SkSpan<DSLField> fields, PositionInfo pos) { |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 234 | std::vector<SkSL::Type::Field> skslFields; |
John Stiles | ed7b4f6 | 2021-08-11 11:42:57 -0400 | [diff] [blame] | 235 | skslFields.reserve(fields.size()); |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 236 | for (const DSLField& field : fields) { |
Ethan Nicholas | 0c8a598 | 2021-08-31 11:48:54 -0400 | [diff] [blame] | 237 | if (field.fModifiers.fModifiers.fFlags != Modifiers::kNo_Flag) { |
| 238 | String desc = field.fModifiers.fModifiers.description(); |
| 239 | desc.pop_back(); // remove trailing space |
Ethan Nicholas | 3272412 | 2021-09-07 13:49:07 -0400 | [diff] [blame] | 240 | DSLWriter::ReportError("modifier '" + desc + "' is not permitted on a struct field", |
| 241 | field.fPosition); |
Ethan Nicholas | 0c8a598 | 2021-08-31 11:48:54 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Ethan Nicholas | 772061e | 2021-08-29 12:42:38 -0400 | [diff] [blame] | 244 | const SkSL::Type& type = field.fType.skslType(); |
| 245 | if (type.isOpaque()) { |
Ethan Nicholas | 3272412 | 2021-09-07 13:49:07 -0400 | [diff] [blame] | 246 | DSLWriter::ReportError("opaque type '" + type.displayName() + |
| 247 | "' is not permitted in a struct", field.fPosition); |
Ethan Nicholas | 772061e | 2021-08-29 12:42:38 -0400 | [diff] [blame] | 248 | } |
| 249 | skslFields.emplace_back(field.fModifiers.fModifiers, field.fName, &type); |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 250 | } |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 251 | const SkSL::Type* result = DSLWriter::SymbolTable()->add(Type::MakeStructType(pos.offset(), |
Ethan Nicholas | 27f06eb | 2021-07-26 16:39:40 -0400 | [diff] [blame] | 252 | name, |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 253 | skslFields)); |
Ethan Nicholas | 833cd20 | 2021-09-02 14:46:41 -0400 | [diff] [blame] | 254 | if (result->isTooDeeplyNested()) { |
Ethan Nicholas | 3272412 | 2021-09-07 13:49:07 -0400 | [diff] [blame] | 255 | DSLWriter::ReportError("struct '" + String(name) + "' is too deeply nested", pos); |
Ethan Nicholas | 833cd20 | 2021-09-02 14:46:41 -0400 | [diff] [blame] | 256 | } |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 257 | DSLWriter::ProgramElements().push_back(std::make_unique<SkSL::StructDefinition>(/*offset=*/-1, |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 258 | *result)); |
| 259 | return result; |
| 260 | } |
| 261 | |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 262 | } // namespace dsl |
| 263 | |
| 264 | } // namespace SkSL |