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/first_index_offset.h"
16
17 #include <memory>
18 #include <unordered_map>
19 #include <utility>
20
21 #include "src/ast/struct_block_decoration.h"
22 #include "src/program_builder.h"
23 #include "src/sem/function.h"
24 #include "src/sem/member_accessor_expression.h"
25 #include "src/sem/struct.h"
26 #include "src/sem/variable.h"
27
28 TINT_INSTANTIATE_TYPEINFO(tint::transform::FirstIndexOffset);
29 TINT_INSTANTIATE_TYPEINFO(tint::transform::FirstIndexOffset::BindingPoint);
30 TINT_INSTANTIATE_TYPEINFO(tint::transform::FirstIndexOffset::Data);
31
32 namespace tint {
33 namespace transform {
34 namespace {
35
36 // Uniform buffer member names
37 constexpr char kFirstVertexName[] = "first_vertex_index";
38 constexpr char kFirstInstanceName[] = "first_instance_index";
39
40 } // namespace
41
42 FirstIndexOffset::BindingPoint::BindingPoint() = default;
BindingPoint(uint32_t b,uint32_t g)43 FirstIndexOffset::BindingPoint::BindingPoint(uint32_t b, uint32_t g)
44 : binding(b), group(g) {}
45 FirstIndexOffset::BindingPoint::~BindingPoint() = default;
46
Data(bool has_vtx_index,bool has_inst_index,uint32_t first_vtx_offset,uint32_t first_inst_offset)47 FirstIndexOffset::Data::Data(bool has_vtx_index,
48 bool has_inst_index,
49 uint32_t first_vtx_offset,
50 uint32_t first_inst_offset)
51 : has_vertex_index(has_vtx_index),
52 has_instance_index(has_inst_index),
53 first_vertex_offset(first_vtx_offset),
54 first_instance_offset(first_inst_offset) {}
55 FirstIndexOffset::Data::Data(const Data&) = default;
56 FirstIndexOffset::Data::~Data() = default;
57
58 FirstIndexOffset::FirstIndexOffset() = default;
59 FirstIndexOffset::~FirstIndexOffset() = default;
60
Run(CloneContext & ctx,const DataMap & inputs,DataMap & outputs)61 void FirstIndexOffset::Run(CloneContext& ctx,
62 const DataMap& inputs,
63 DataMap& outputs) {
64 // Get the uniform buffer binding point
65 uint32_t ub_binding = binding_;
66 uint32_t ub_group = group_;
67 if (auto* binding_point = inputs.Get<BindingPoint>()) {
68 ub_binding = binding_point->binding;
69 ub_group = binding_point->group;
70 }
71
72 // Map of builtin usages
73 std::unordered_map<const sem::Variable*, const char*> builtin_vars;
74 std::unordered_map<const sem::StructMember*, const char*> builtin_members;
75
76 bool has_vertex_index = false;
77 bool has_instance_index = false;
78
79 // Traverse the AST scanning for builtin accesses via variables (includes
80 // parameters) or structure member accesses.
81 for (auto* node : ctx.src->ASTNodes().Objects()) {
82 if (auto* var = node->As<ast::Variable>()) {
83 for (auto* dec : var->decorations) {
84 if (auto* builtin_dec = dec->As<ast::BuiltinDecoration>()) {
85 ast::Builtin builtin = builtin_dec->builtin;
86 if (builtin == ast::Builtin::kVertexIndex) {
87 auto* sem_var = ctx.src->Sem().Get(var);
88 builtin_vars.emplace(sem_var, kFirstVertexName);
89 has_vertex_index = true;
90 }
91 if (builtin == ast::Builtin::kInstanceIndex) {
92 auto* sem_var = ctx.src->Sem().Get(var);
93 builtin_vars.emplace(sem_var, kFirstInstanceName);
94 has_instance_index = true;
95 }
96 }
97 }
98 }
99 if (auto* member = node->As<ast::StructMember>()) {
100 for (auto* dec : member->decorations) {
101 if (auto* builtin_dec = dec->As<ast::BuiltinDecoration>()) {
102 ast::Builtin builtin = builtin_dec->builtin;
103 if (builtin == ast::Builtin::kVertexIndex) {
104 auto* sem_mem = ctx.src->Sem().Get(member);
105 builtin_members.emplace(sem_mem, kFirstVertexName);
106 has_vertex_index = true;
107 }
108 if (builtin == ast::Builtin::kInstanceIndex) {
109 auto* sem_mem = ctx.src->Sem().Get(member);
110 builtin_members.emplace(sem_mem, kFirstInstanceName);
111 has_instance_index = true;
112 }
113 }
114 }
115 }
116 }
117
118 // Byte offsets on the uniform buffer
119 uint32_t vertex_index_offset = 0;
120 uint32_t instance_index_offset = 0;
121
122 if (has_vertex_index || has_instance_index) {
123 // Add uniform buffer members and calculate byte offsets
124 uint32_t offset = 0;
125 ast::StructMemberList members;
126 if (has_vertex_index) {
127 members.push_back(ctx.dst->Member(kFirstVertexName, ctx.dst->ty.u32()));
128 vertex_index_offset = offset;
129 offset += 4;
130 }
131 if (has_instance_index) {
132 members.push_back(ctx.dst->Member(kFirstInstanceName, ctx.dst->ty.u32()));
133 instance_index_offset = offset;
134 offset += 4;
135 }
136 auto* struct_ =
137 ctx.dst->Structure(ctx.dst->Sym(), std::move(members),
138 {ctx.dst->create<ast::StructBlockDecoration>()});
139
140 // Create a global to hold the uniform buffer
141 Symbol buffer_name = ctx.dst->Sym();
142 ctx.dst->Global(buffer_name, ctx.dst->ty.Of(struct_),
143 ast::StorageClass::kUniform, nullptr,
144 ast::DecorationList{
145 ctx.dst->create<ast::BindingDecoration>(ub_binding),
146 ctx.dst->create<ast::GroupDecoration>(ub_group),
147 });
148
149 // Fix up all references to the builtins with the offsets
150 ctx.ReplaceAll(
151 [=, &ctx](const ast::Expression* expr) -> const ast::Expression* {
152 if (auto* sem = ctx.src->Sem().Get(expr)) {
153 if (auto* user = sem->As<sem::VariableUser>()) {
154 auto it = builtin_vars.find(user->Variable());
155 if (it != builtin_vars.end()) {
156 return ctx.dst->Add(
157 ctx.CloneWithoutTransform(expr),
158 ctx.dst->MemberAccessor(buffer_name, it->second));
159 }
160 }
161 if (auto* access = sem->As<sem::StructMemberAccess>()) {
162 auto it = builtin_members.find(access->Member());
163 if (it != builtin_members.end()) {
164 return ctx.dst->Add(
165 ctx.CloneWithoutTransform(expr),
166 ctx.dst->MemberAccessor(buffer_name, it->second));
167 }
168 }
169 }
170 // Not interested in this experssion. Just clone.
171 return nullptr;
172 });
173 }
174
175 ctx.Clone();
176
177 outputs.Add<Data>(has_vertex_index, has_instance_index, vertex_index_offset,
178 instance_index_offset);
179 }
180
181 } // namespace transform
182 } // namespace tint
183