1 // Copyright 2021 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/glsl.h"
16
17 #include <utility>
18
19 #include "src/program_builder.h"
20 #include "src/transform/calculate_array_length.h"
21 #include "src/transform/canonicalize_entry_point_io.h"
22 #include "src/transform/decompose_memory_access.h"
23 #include "src/transform/external_texture_transform.h"
24 #include "src/transform/fold_trivial_single_use_lets.h"
25 #include "src/transform/loop_to_for_loop.h"
26 #include "src/transform/manager.h"
27 #include "src/transform/pad_array_elements.h"
28 #include "src/transform/promote_initializers_to_const_var.h"
29 #include "src/transform/remove_phonies.h"
30 #include "src/transform/simplify_pointers.h"
31 #include "src/transform/single_entry_point.h"
32 #include "src/transform/unshadow.h"
33 #include "src/transform/zero_init_workgroup_memory.h"
34
35 TINT_INSTANTIATE_TYPEINFO(tint::transform::Glsl);
36 TINT_INSTANTIATE_TYPEINFO(tint::transform::Glsl::Config);
37
38 namespace tint {
39 namespace transform {
40
41 Glsl::Glsl() = default;
42 Glsl::~Glsl() = default;
43
Run(const Program * in,const DataMap & inputs)44 Output Glsl::Run(const Program* in, const DataMap& inputs) {
45 Manager manager;
46 DataMap data;
47
48 auto* cfg = inputs.Get<Config>();
49
50 manager.Add<Unshadow>();
51
52 // Attempt to convert `loop`s into for-loops. This is to try and massage the
53 // output into something that will not cause FXC to choke or misbehave.
54 manager.Add<FoldTrivialSingleUseLets>();
55 manager.Add<LoopToForLoop>();
56
57 if (!cfg || !cfg->disable_workgroup_init) {
58 // ZeroInitWorkgroupMemory must come before CanonicalizeEntryPointIO as
59 // ZeroInitWorkgroupMemory may inject new builtin parameters.
60 manager.Add<ZeroInitWorkgroupMemory>();
61 }
62 manager.Add<CanonicalizeEntryPointIO>();
63 manager.Add<SimplifyPointers>();
64
65 // Running SingleEntryPoint before RemovePhonies prevents variables
66 // referenced only by phonies from being optimized out. Strictly
67 // speaking, that optimization isn't incorrect, but it prevents some
68 // tests (e.g., types/texture/*) from producing useful results.
69 if (cfg) {
70 manager.Add<SingleEntryPoint>();
71 data.Add<SingleEntryPoint::Config>(cfg->entry_point);
72 }
73 manager.Add<RemovePhonies>();
74 manager.Add<CalculateArrayLength>();
75 manager.Add<ExternalTextureTransform>();
76 manager.Add<PromoteInitializersToConstVar>();
77 manager.Add<PadArrayElements>();
78
79 // For now, canonicalize to structs for all IO, as in HLSL.
80 // TODO(senorblanco): we could skip this by accessing global entry point
81 // variables directly.
82 data.Add<CanonicalizeEntryPointIO::Config>(
83 CanonicalizeEntryPointIO::ShaderStyle::kHlsl);
84 auto out = manager.Run(in, data);
85 if (!out.program.IsValid()) {
86 return out;
87 }
88
89 ProgramBuilder builder;
90 CloneContext ctx(&builder, &out.program);
91 AddEmptyEntryPoint(ctx);
92 ctx.Clone();
93 builder.SetTransformApplied(this);
94 return Output{Program(std::move(builder))};
95 }
96
AddEmptyEntryPoint(CloneContext & ctx) const97 void Glsl::AddEmptyEntryPoint(CloneContext& ctx) const {
98 for (auto* func : ctx.src->AST().Functions()) {
99 if (func->IsEntryPoint()) {
100 return;
101 }
102 }
103 ctx.dst->Func(ctx.dst->Symbols().New("unused_entry_point"), {},
104 ctx.dst->ty.void_(), {},
105 {ctx.dst->Stage(ast::PipelineStage::kCompute),
106 ctx.dst->WorkgroupSize(1)});
107 }
108
Config(const std::string & ep,bool disable_wi)109 Glsl::Config::Config(const std::string& ep, bool disable_wi)
110 : entry_point(ep), disable_workgroup_init(disable_wi) {}
111 Glsl::Config::Config(const Config&) = default;
112 Glsl::Config::~Config() = default;
113
114 } // namespace transform
115 } // namespace tint
116