xref: /aosp_15_r20/external/skia/src/sksl/ir/SkSLSetting.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 Google Inc.
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_SETTING
9 #define SKSL_SETTING
10 
11 #include "src/sksl/SkSLPosition.h"
12 #include "src/sksl/SkSLUtil.h"
13 #include "src/sksl/ir/SkSLExpression.h"
14 #include "src/sksl/ir/SkSLIRNode.h"
15 
16 #include <cstdint>
17 #include <memory>
18 #include <string>
19 #include <string_view>
20 
21 
22 namespace SkSL {
23 
24 class Context;
25 class Type;
26 enum class OperatorPrecedence : uint8_t;
27 
28 /**
29  * Represents a compile-time constant setting, such as sk_Caps.integerSupport. These IRNodes are
30  * used when assembling a module. These nodes are replaced with the value of the setting during
31  * compilation when ShaderCaps are available.
32  */
33 class Setting final : public Expression {
34 public:
35     inline static constexpr Kind kIRNodeKind = Kind::kSetting;
36 
37     using CapsPtr = const bool ShaderCaps::*;
38 
Setting(Position pos,CapsPtr capsPtr,const Type * type)39     Setting(Position pos, CapsPtr capsPtr, const Type* type)
40         : INHERITED(pos, kIRNodeKind, type)
41         , fCapsPtr(capsPtr) {}
42 
43     // Creates the current value of the associated caps bit as a Literal if ShaderCaps are
44     // available, or a Setting IRNode when ShaderCaps are not known. Reports errors via the
45     // ErrorReporter.
46     static std::unique_ptr<Expression> Convert(const Context& context,
47                                                Position pos,
48                                                const std::string_view& name);
49 
50     // Creates the current value of the passed-in caps bit as a Literal if ShaderCaps are
51     // available, or a Setting IRNode when ShaderCaps are not known.
52     static std::unique_ptr<Expression> Make(const Context& context, Position pos, CapsPtr capsPtr);
53 
54     // Converts a Setting expression to its actual ShaderCaps value (boolean true/false).
55     std::unique_ptr<Expression> toLiteral(const ShaderCaps& caps) const;
56 
clone(Position pos)57     std::unique_ptr<Expression> clone(Position pos) const override {
58         return std::make_unique<Setting>(pos, fCapsPtr, &this->type());
59     }
60 
61     std::string_view name() const;
62 
capsPtr()63     CapsPtr capsPtr() const { return fCapsPtr; }
64 
description(OperatorPrecedence)65     std::string description(OperatorPrecedence) const override {
66         return "sk_Caps." + std::string(this->name());
67     }
68 
69 private:
70     CapsPtr fCapsPtr;
71 
72     using INHERITED = Expression;
73 };
74 
75 }  // namespace SkSL
76 
77 #endif
78