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 #ifndef SKSL_CONSTRUCTOR_SPLAT 9 #define SKSL_CONSTRUCTOR_SPLAT 10 11 #include "src/sksl/SkSLContext.h" 12 #include "src/sksl/ir/SkSLConstructor.h" 13 #include "src/sksl/ir/SkSLExpression.h" 14 15 #include <memory> 16 17 namespace SkSL { 18 19 /** 20 * Represents the construction of a vector splat, such as `half3(n)`. 21 * 22 * These always contain exactly 1 scalar. 23 */ 24 class ConstructorSplat final : public SingleArgumentConstructor { 25 public: 26 inline static constexpr Kind kExpressionKind = Kind::kConstructorSplat; 27 ConstructorSplat(int line,const Type & type,std::unique_ptr<Expression> arg)28 ConstructorSplat(int line, const Type& type, std::unique_ptr<Expression> arg) 29 : INHERITED(line, kExpressionKind, &type, std::move(arg)) {} 30 ConstructorSplat(const Expression & scalar,const Type & type)31 ConstructorSplat(const Expression& scalar, const Type& type) 32 : ConstructorSplat(scalar.fLine, type, scalar.clone()) { 33 SkASSERT(type.isVector()); 34 SkASSERT(type.componentType() == scalar.type()); 35 } 36 37 // The input argument must be scalar. A "splat" to a scalar type will be optimized into a no-op. 38 static std::unique_ptr<Expression> Make(const Context& context, 39 int line, 40 const Type& type, 41 std::unique_ptr<Expression> arg); 42 clone()43 std::unique_ptr<Expression> clone() const override { 44 return std::make_unique<ConstructorSplat>(fLine, this->type(), argument()->clone()); 45 } 46 supportsConstantValues()47 bool supportsConstantValues() const override { 48 return true; 49 } 50 getConstantValue(int n)51 skstd::optional<double> getConstantValue(int n) const override { 52 SkASSERT(n >= 0 && n < this->type().columns()); 53 return this->argument()->getConstantValue(0); 54 } 55 56 private: 57 using INHERITED = SingleArgumentConstructor; 58 }; 59 60 } // namespace SkSL 61 62 #endif 63