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