1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/transform/manager.h"
16
17 /// If set to 1 then the transform::Manager will dump the WGSL of the program
18 /// before and after each transform. Helpful for debugging bad output.
19 #define TINT_PRINT_PROGRAM_FOR_EACH_TRANSFORM 0
20
21 #if TINT_PRINT_PROGRAM_FOR_EACH_TRANSFORM
22 #define TINT_IF_PRINT_PROGRAM(x) x
23 #else // TINT_PRINT_PROGRAM_FOR_EACH_TRANSFORM
24 #define TINT_IF_PRINT_PROGRAM(x)
25 #endif // TINT_PRINT_PROGRAM_FOR_EACH_TRANSFORM
26
27 TINT_INSTANTIATE_TYPEINFO(tint::transform::Manager);
28
29 namespace tint {
30 namespace transform {
31
32 Manager::Manager() = default;
33 Manager::~Manager() = default;
34
Run(const Program * program,const DataMap & data)35 Output Manager::Run(const Program* program, const DataMap& data) {
36 #if TINT_PRINT_PROGRAM_FOR_EACH_TRANSFORM
37 auto print_program = [&](const char* msg, const Transform* transform) {
38 auto wgsl = Program::printer(program);
39 std::cout << "---------------------------------------------------------"
40 << std::endl;
41 std::cout << "-- " << msg << " " << transform->TypeInfo().name << ":"
42 << std::endl;
43 std::cout << "---------------------------------------------------------"
44 << std::endl;
45 std::cout << wgsl << std::endl;
46 std::cout << "---------------------------------------------------------"
47 << std::endl
48 << std::endl;
49 };
50 #endif
51
52 Output out;
53 if (!transforms_.empty()) {
54 for (const auto& transform : transforms_) {
55 TINT_IF_PRINT_PROGRAM(print_program("Input to", transform.get()));
56
57 auto res = transform->Run(program, data);
58 out.program = std::move(res.program);
59 out.data.Add(std::move(res.data));
60 program = &out.program;
61 if (!program->IsValid()) {
62 TINT_IF_PRINT_PROGRAM(
63 print_program("Invalid output of", transform.get()));
64 return out;
65 }
66
67 if (transform == transforms_.back()) {
68 TINT_IF_PRINT_PROGRAM(print_program("Output of", transform.get()));
69 }
70 }
71 } else {
72 out.program = program->Clone();
73 }
74
75 return out;
76 }
77
78 } // namespace transform
79 } // namespace tint
80