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/SkSLConstantFolder.h" 9 #include "src/sksl/SkSLProgramSettings.h" 10 #include "src/sksl/ir/SkSLConstructorSplat.h" 11 12 namespace SkSL { 13 Make(const Context & context,int line,const Type & type,std::unique_ptr<Expression> arg)14std::unique_ptr<Expression> ConstructorSplat::Make(const Context& context, 15 int line, 16 const Type& type, 17 std::unique_ptr<Expression> arg) { 18 SkASSERT(type.isAllowedInES2(context)); 19 SkASSERT(type.isScalar() || type.isVector()); 20 SkASSERT(arg->type().scalarTypeForLiteral().matches( 21 type.componentType().scalarTypeForLiteral())); 22 SkASSERT(arg->type().isScalar()); 23 24 // A "splat" to a scalar type is a no-op and can be eliminated. 25 if (type.isScalar()) { 26 return arg; 27 } 28 29 // Replace constant variables with their corresponding values, so `float3(five)` can compile 30 // down to `float3(5.0)` (the latter is a compile-time constant). 31 arg = ConstantFolder::MakeConstantValueForVariable(std::move(arg)); 32 33 SkASSERT(type.isVector()); 34 return std::make_unique<ConstructorSplat>(line, type, std::move(arg)); 35 } 36 37 } // namespace SkSL 38