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