xref: /aosp_15_r20/external/skia/src/sksl/ir/SkSLModifierFlags.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include "src/sksl/ir/SkSLModifierFlags.h"
9 
10 #include "include/core/SkTypes.h"
11 #include "src/sksl/SkSLContext.h"
12 #include "src/sksl/SkSLErrorReporter.h"
13 #include "src/sksl/SkSLPosition.h"
14 
15 namespace SkSL {
16 
paddedDescription() const17 std::string ModifierFlags::paddedDescription() const {
18     // SkSL extensions
19     std::string result;
20     if (*this & ModifierFlag::kExport) {
21         result += "$export ";
22     }
23     if (*this & ModifierFlag::kES3) {
24         result += "$es3 ";
25     }
26     if (*this & ModifierFlag::kPure) {
27         result += "$pure ";
28     }
29     if (*this & ModifierFlag::kInline) {
30         result += "inline ";
31     }
32     if (*this & ModifierFlag::kNoInline) {
33         result += "noinline ";
34     }
35 
36     // Real GLSL qualifiers (must be specified in order in GLSL 4.1 and below)
37     if (*this & ModifierFlag::kFlat) {
38         result += "flat ";
39     }
40     if (*this & ModifierFlag::kNoPerspective) {
41         result += "noperspective ";
42     }
43     if (*this & ModifierFlag::kConst) {
44         result += "const ";
45     }
46     if (*this & ModifierFlag::kUniform) {
47         result += "uniform ";
48     }
49     if ((*this & ModifierFlag::kIn) && (*this & ModifierFlag::kOut)) {
50         result += "inout ";
51     } else if (*this & ModifierFlag::kIn) {
52         result += "in ";
53     } else if (*this & ModifierFlag::kOut) {
54         result += "out ";
55     }
56     if (*this & ModifierFlag::kHighp) {
57         result += "highp ";
58     }
59     if (*this & ModifierFlag::kMediump) {
60         result += "mediump ";
61     }
62     if (*this & ModifierFlag::kLowp) {
63         result += "lowp ";
64     }
65     if (*this & ModifierFlag::kReadOnly) {
66         result += "readonly ";
67     }
68     if (*this & ModifierFlag::kWriteOnly) {
69         result += "writeonly ";
70     }
71     if (*this & ModifierFlag::kBuffer) {
72         result += "buffer ";
73     }
74 
75     // We're using non-GLSL names for these.
76     if (*this & ModifierFlag::kPixelLocal) {
77         // Roughly equivalent to `__pixel_localEXT`.
78         result += "pixel_local ";
79     }
80     if (*this & ModifierFlag::kWorkgroup) {
81         // Equivalent to `shared`.
82         result += "workgroup ";
83     }
84 
85     return result;
86 }
87 
description() const88 std::string ModifierFlags::description() const {
89     std::string s = this->paddedDescription();
90     if (!s.empty()) {
91         s.pop_back();
92     }
93     return s;
94 }
95 
checkPermittedFlags(const Context & context,Position pos,ModifierFlags permittedModifierFlags) const96 bool ModifierFlags::checkPermittedFlags(const Context& context,
97                                         Position pos,
98                                         ModifierFlags permittedModifierFlags) const {
99     static constexpr struct { ModifierFlag flag; const char* name; } kModifierFlags[] = {
100         { ModifierFlag::kConst,          "const" },
101         { ModifierFlag::kIn,             "in" },
102         { ModifierFlag::kOut,            "out" },
103         { ModifierFlag::kUniform,        "uniform" },
104         { ModifierFlag::kFlat,           "flat" },
105         { ModifierFlag::kNoPerspective,  "noperspective" },
106         { ModifierFlag::kPure,           "$pure" },
107         { ModifierFlag::kInline,         "inline" },
108         { ModifierFlag::kNoInline,       "noinline" },
109         { ModifierFlag::kHighp,          "highp" },
110         { ModifierFlag::kMediump,        "mediump" },
111         { ModifierFlag::kLowp,           "lowp" },
112         { ModifierFlag::kExport,         "$export" },
113         { ModifierFlag::kES3,            "$es3" },
114         { ModifierFlag::kWorkgroup,      "workgroup" },
115         { ModifierFlag::kReadOnly,       "readonly" },
116         { ModifierFlag::kWriteOnly,      "writeonly" },
117         { ModifierFlag::kBuffer,         "buffer" },
118         { ModifierFlag::kPixelLocal,     "pixel_local" },
119     };
120 
121     bool success = true;
122     ModifierFlags modifierFlags = *this;
123     for (const auto& f : kModifierFlags) {
124         if (modifierFlags & f.flag) {
125             if (!(permittedModifierFlags & f.flag)) {
126                 context.fErrors->error(pos, "'" + std::string(f.name) + "' is not permitted here");
127                 success = false;
128             }
129             modifierFlags &= ~f.flag;
130         }
131     }
132     SkASSERT(modifierFlags == ModifierFlag::kNone);
133 
134     return success;
135 }
136 
137 } // namespace SkSL
138