1 /*
2 * Copyright 2017 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 #include "src/sksl/ir/SkSLSetting.h"
9
10 #include "include/core/SkTypes.h"
11 #include "include/sksl/SkSLErrorReporter.h"
12 #include "src/core/SkTHash.h"
13 #include "src/sksl/SkSLBuiltinTypes.h"
14 #include "src/sksl/SkSLContext.h"
15 #include "src/sksl/SkSLProgramSettings.h"
16 #include "src/sksl/SkSLUtil.h"
17 #include "src/sksl/ir/SkSLLiteral.h"
18
19 #include <initializer_list>
20
21 namespace SkSL {
22
23 namespace {
24
25 using CapsLookupTable = SkTHashMap<std::string_view, Setting::CapsPtr>;
26
caps_lookup_table()27 static const CapsLookupTable& caps_lookup_table() {
28 // Create a lookup table that converts strings into the equivalent ShaderCaps member-pointers.
29 static CapsLookupTable* sCapsLookupTable = new CapsLookupTable({
30 CapsLookupTable::Pair("mustDoOpBetweenFloorAndAbs",
31 &ShaderCaps::fMustDoOpBetweenFloorAndAbs),
32 CapsLookupTable::Pair("mustGuardDivisionEvenAfterExplicitZeroCheck",
33 &ShaderCaps::fMustGuardDivisionEvenAfterExplicitZeroCheck),
34 CapsLookupTable::Pair("atan2ImplementedAsAtanYOverX",
35 &ShaderCaps::fAtan2ImplementedAsAtanYOverX),
36 CapsLookupTable::Pair("floatIs32Bits",
37 &ShaderCaps::fFloatIs32Bits),
38 CapsLookupTable::Pair("integerSupport",
39 &ShaderCaps::fIntegerSupport),
40 CapsLookupTable::Pair("builtinDeterminantSupport",
41 &ShaderCaps::fBuiltinDeterminantSupport),
42 CapsLookupTable::Pair("rewriteMatrixVectorMultiply",
43 &ShaderCaps::fRewriteMatrixVectorMultiply),
44 });
45 return *sCapsLookupTable;
46 }
47
48 } // namespace
49
name() const50 std::string_view Setting::name() const {
51 for (const auto& [name, capsPtr] : caps_lookup_table()) {
52 if (capsPtr == fCapsPtr) {
53 return name;
54 }
55 }
56 SkUNREACHABLE;
57 }
58
Convert(const Context & context,Position pos,const std::string_view & name)59 std::unique_ptr<Expression> Setting::Convert(const Context& context,
60 Position pos,
61 const std::string_view& name) {
62 SkASSERT(context.fConfig);
63
64 if (ProgramConfig::IsRuntimeEffect(context.fConfig->fKind)) {
65 context.fErrors->error(pos, "name 'sk_Caps' is reserved");
66 return nullptr;
67 }
68
69 const CapsPtr* capsPtr = caps_lookup_table().find(name);
70 if (!capsPtr) {
71 context.fErrors->error(pos, "unknown capability flag '" + std::string(name) + "'");
72 return nullptr;
73 }
74
75 return Setting::Make(context, pos, *capsPtr);
76 }
77
Make(const Context & context,Position pos,CapsPtr capsPtr)78 std::unique_ptr<Expression> Setting::Make(const Context& context, Position pos, CapsPtr capsPtr) {
79 if (context.fCaps) {
80 // We know the caps values--return a boolean literal.
81 return Literal::MakeBool(context, pos, context.fCaps->*capsPtr);
82 }
83
84 // We don't know the caps values yet--generate a Setting IRNode.
85 return std::make_unique<Setting>(pos, capsPtr, context.fTypes.fBool.get());
86 }
87
toLiteral(const Context & context) const88 std::unique_ptr<Expression> Setting::toLiteral(const Context& context) const {
89 SkASSERT(context.fCaps);
90 return Literal::MakeBool(fPosition, context.fCaps->*fCapsPtr, &this->type());
91 }
92
93
94 } // namespace SkSL
95