• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkSLConstructorCompound.h"
11 
12 #include <algorithm>
13 #include <numeric>
14 
15 namespace SkSL {
16 
Make(const Context & context,int line,const Type & type,ExpressionArray args)17 std::unique_ptr<Expression> ConstructorCompound::Make(const Context& context,
18                                                       int line,
19                                                       const Type& type,
20                                                       ExpressionArray args) {
21     SkASSERT(type.isAllowedInES2(context));
22 
23     // A scalar "composite" type with a single scalar argument is a no-op and can be eliminated.
24     // (Pedantically, this isn't a composite at all, but it's harmless to allow and simplifies
25     // call sites which need to narrow a vector and may sometimes end up with a scalar.)
26     if (type.isScalar() && args.size() == 1 && args.front()->type() == type) {
27         return std::move(args.front());
28     }
29 
30     // The type must be a vector or matrix, and all the arguments must have matching component type.
31     SkASSERT(type.isVector() || type.isMatrix());
32     SkASSERT(std::all_of(args.begin(), args.end(), [&](const std::unique_ptr<Expression>& arg) {
33         const Type& argType = arg->type();
34         return (argType.isScalar() || argType.isVector() || argType.isMatrix()) &&
35                (argType.componentType() == type.componentType());
36     }));
37 
38     // The slot count of the combined argument list must match the composite type's slot count.
39     SkASSERT(type.slotCount() ==
40              std::accumulate(args.begin(), args.end(), /*initial value*/ (size_t)0,
41                              [](size_t n, const std::unique_ptr<Expression>& arg) {
42                                  return n + arg->type().slotCount();
43                              }));
44 
45     if (context.fConfig->fSettings.fOptimize) {
46         // Find ConstructorCompounds embedded inside other ConstructorCompounds and flatten them.
47         //   -  float4(float2(1, 2), 3, 4)                -->  float4(1, 2, 3, 4)
48         //   -  float4(w, float3(sin(x), cos(y), tan(z))) -->  float4(w, sin(x), cos(y), tan(z))
49         //   -  mat2(float2(a, b), float2(c, d))          -->  mat2(a, b, c, d)
50 
51         // See how many fields we would have if composite constructors were flattened out.
52         size_t fields = 0;
53         for (const std::unique_ptr<Expression>& arg : args) {
54             fields += arg->is<ConstructorCompound>()
55                               ? arg->as<ConstructorCompound>().arguments().size()
56                               : 1;
57         }
58 
59         // If we added up more fields than we're starting with, we found at least one input that can
60         // be flattened out.
61         if (fields > args.size()) {
62             ExpressionArray flattened;
63             flattened.reserve_back(fields);
64             for (std::unique_ptr<Expression>& arg : args) {
65                 // For non-ConstructorCompound fields, move them over as-is.
66                 if (!arg->is<ConstructorCompound>()) {
67                     flattened.push_back(std::move(arg));
68                     continue;
69                 }
70                 // For ConstructorCompound fields, move over their inner arguments individually.
71                 ConstructorCompound& compositeCtor = arg->as<ConstructorCompound>();
72                 for (std::unique_ptr<Expression>& innerArg : compositeCtor.arguments()) {
73                     flattened.push_back(std::move(innerArg));
74                 }
75             }
76             args = std::move(flattened);
77         }
78     }
79 
80     // Replace constant variables with their corresponding values, so `float2(one, two)` can
81     // compile down to `float2(1.0, 2.0)` (the latter is a compile-time constant).
82     for (std::unique_ptr<Expression>& arg : args) {
83         arg = ConstantFolder::MakeConstantValueForVariable(std::move(arg));
84     }
85 
86     return std::make_unique<ConstructorCompound>(line, type, std::move(args));
87 }
88 
89 }  // namespace SkSL
90