Blender V5.3
union.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <unordered_map>
10
11#include "intermediate.hh"
12#include "metadata.hh"
13#include "processor.hh"
14
15namespace blender::gpu::shader {
16using namespace std;
17using namespace shader::parser;
18using namespace shader::parser::ast;
19using namespace metadata;
20
21void SourceProcessor::lower_unions(Parser &parser)
22{
23 struct Member {
24 string type, name;
25 size_t offset, size;
26 bool is_enum;
27
28 /* Return true for builtin trivial types (e.g. uint, float3). */
29 bool is_trivial() const
30 {
31 return type.empty();
32 }
33 };
34
35 auto is_struct_size_known = [&](Scope attributes) -> bool {
36 if (attributes.is_invalid()) {
37 return false;
38 }
39 bool is_shared = false;
40 attributes.foreach_attribute([&](Token attr, Scope) {
41 if (attr.str() == "host_shared") {
42 is_shared = true;
43 }
44 });
45 if (!is_shared) {
46 return false;
47 }
48 return true;
49 };
50
51 /* Description of each union types. */
52 unordered_map<string, vector<Member>> union_members;
53
54 /* First, lower anonymous unions into separate struct. */
55 parser().foreach_struct([&](Token struct_tok, Scope attrs, Token struct_name, Scope body) {
56 int union_index = 0;
57 body.foreach_match("o{..};", [&](const Tokens &t) {
58 Scope union_body = t[1].scope();
59
60 string union_name = "union" + to_string(union_index);
61 string union_type = string(struct_name.str()) + "_" + union_name;
62
63 /* Parse members of the union for later use. */
64 vector<Member> members;
65 union_body.foreach_declaration(
66 [&](Scope, Token, Token type, Scope, Token name, Scope array, Token) {
67 if (array.is_valid()) {
68 report_error(name, "Arrays are not supported inside unions.");
69 }
70 members.emplace_back(
71 Member{string(type.str()), string(name.str()), 0, 0, type.prev() == Enum});
72 });
73
74 if (members.empty()) {
75 report_error(t[0], "Empty union");
76 return;
77 }
78 union_members.emplace(union_type, members);
79
80 string union_member = union_type + " " + union_name + ";";
81 if (attrs.contains("host_shared")) {
82 union_member = "struct " + union_member;
83 }
84 parser.insert_before(t.front(), union_member);
85 parser.erase(t.front(), t.back());
86
87 string type_decl = "struct [[host_shared]] " + union_type + " {\n";
88 /* Temporary storage (use first member, still valid since all members should have the same
89 * size). The real storage can only be set once we know the size of the union, which we can
90 * only know after lowering them as outside types. */
91 type_decl += " " + members.front().type + " " + members.front().name + ";\n";
92 type_decl += "};\n";
93
94 parser.insert_line_number(struct_tok.str_index_start() - 1, t[0].line_number());
95 parser.insert_before(struct_tok, type_decl);
96 parser.insert_line_number(struct_tok.str_index_start() - 1, struct_tok.line_number());
97
98 union_index++;
99 });
100 });
101 parser.apply_mutations();
102
103 /* Map structure name to structure members. */
104 unordered_map<string, vector<Member>> struct_members = {
105 {"float", {{"", "", 0, 4}}},
106 {"float2", {{"", "", 0, 8}}},
107 {"float4", {{"", "", 0, 16}}},
108 {"bool32_t", {{"", "", 0, 4}}},
109 {"int", {{"", "", 0, 4}}},
110 {"int2", {{"", "", 0, 8}}},
111 {"int4", {{"", "", 0, 16}}},
112 {"uint", {{"", "", 0, 4}}},
113 {"uint2", {{"", "", 0, 8}}},
114 {"uint4", {{"", "", 0, 16}}},
115 {"string_t", {{"", "", 0, 4}}},
116 {"packed_float3", {{"", "", 0, 12}}},
117 {"packed_int3", {{"", "", 0, 12}}},
118 {"packed_uint3", {{"", "", 0, 12}}},
119 {"float2x4", {{"float4", "[0]", 0, 16}, {"float4", "[1]", 16, 16}}},
120 {"float3x4",
121 {{"float4", "[0]", 0, 16}, {"float4", "[1]", 16, 16}, {"float4", "[2]", 32, 16}}},
122 {"float4x4",
123 {{"float4", "[0]", 0, 16},
124 {"float4", "[1]", 16, 16},
125 {"float4", "[2]", 32, 16},
126 {"float4", "[3]", 48, 16}}},
127 };
128
129 auto type_size_get = [&](Token type) -> size_t {
130 auto value = struct_members.find(string(type.str()));
131 if (value == struct_members.end()) {
132 return 0;
133 }
134
135 int total_size = 0;
136 for (const Member &member : value->second) {
137 total_size += member.size;
138 }
139 return total_size;
140 };
141
142 /* Then populate struct members. */
143 parser().foreach_struct([&](Token, Scope attributes, Token struct_name, Scope body) {
144 if (!is_struct_size_known(attributes)) {
145 return;
146 }
147 vector<Member> members;
148 size_t offset = 0;
149 body.foreach_declaration([&](Scope, Token, Token type, Scope, Token name, Scope array, Token) {
150 size_t size = 4;
151
152 size_t array_size = 0;
153 if (array.is_valid()) {
154 /* Assume size to be zero by default. It will create invalid size error later on. */
155 array_size = static_array_size(array, 0);
156 }
157 else {
158 array_size = 1;
159 }
160
161 for (int i = 0; i < array_size; i++) {
162 string name_str(name.str());
163 if (array.is_valid()) {
164 name_str += "[" + to_string(i) + "]";
165 }
166 if (type.prev() != Enum) {
167 size = type_size_get(type);
168 if (size != 0) {
169 members.emplace_back(Member{string(type.str()), "." + name_str, offset, size});
170 }
171 }
172 else {
173 members.emplace_back(Member{string(type.str()), "." + name_str, offset, size, true});
174 }
175 offset += size;
176 }
177 });
178
179 struct_members.emplace(struct_name.str(), members);
180 });
181
182 /* Replace placeholder struct with a generic one. */
183 auto replace_placeholder_member = [&](Scope body) {
184 /* Replace placeholder struct with float members. */
185 size_t size = type_size_get(body.front().next());
186 if (size == 0) {
187 report_error(body.front().next(),
188 "Can't infer size of member. Type must be defined in this file and have "
189 "the [[host_shared]] attribute.");
190 }
191 for (int i = 0; i < size; i += 16) {
192 size_t member_size = size - i;
193 const char *data_type = "float4";
194 if (member_size == 4) {
195 data_type = "float";
196 }
197 else if (member_size == 8) {
198 data_type = "float2";
199 }
200 else if (member_size == 12) {
201 data_type = "float3";
202 }
203 parser.insert_after(body.front().str_index_last_no_whitespace(),
204 "\n " + string(data_type) + " data" + to_string(i / 16) + ";");
205 }
206 parser.erase(body.front().next(), body.back().prev());
207 };
208
209 auto member_from_float =
210 [&](const Member &union_member, const Member &struct_member, const string &access) {
211 /* Account for trivial types. */
212 const string &type = struct_member.is_trivial() ? union_member.type : struct_member.type;
213 bool is_enum = struct_member.is_trivial() ? union_member.is_enum : struct_member.is_enum;
214
215 if (is_enum) {
216 return struct_member.type + "(floatBitsToUint(" + access + "))";
217 }
218 if (type.starts_with("uint")) {
219 return "floatBitsToUint(" + access + ")";
220 }
221 if (type.starts_with("int")) {
222 return "floatBitsToInt(" + access + ")";
223 }
224 if (type == "bool") {
225 return "floatBitsToInt(" + access + ") != 0";
226 }
227 return access;
228 };
229
230 auto member_to_float =
231 [&](const Member &union_member, const Member &struct_member, const string &access) {
232 /* Account for trivial types. */
233 const string &type = struct_member.is_trivial() ? union_member.type : struct_member.type;
234 bool is_enum = struct_member.is_trivial() ? union_member.is_enum : struct_member.is_enum;
235
236 if (is_enum) {
237 return "uintBitsToFloat(uint(" + access + "))";
238 }
239 if (type.starts_with("uint")) {
240 return "uintBitsToFloat(" + access + ")";
241 }
242 if (type.starts_with("int")) {
243 return "intBitsToFloat(" + access + ")";
244 }
245 if (type == "bool") {
246 return "intBitsToFloat(int(" + access + "))";
247 }
248 return access;
249 };
250
251 auto union_data_access = [&](const Member &struct_member, size_t union_size) {
252 const size_t offset = struct_member.offset;
253 string access = ".data" + to_string(offset / 16);
254
255 if (struct_member.size == 12) {
256 access += ".xyz";
257 }
258 else if (struct_member.size == 8) {
259 access += ((offset % 16) == 0) ? ".xy" : ".zw";
260 }
261 else if (struct_member.size == 4) {
262 switch (offset % 16) {
263 case 0:
264 /* Special case if last member is a scalar. */
265 access += ((union_size - offset) == 4) ? "" : ".x";
266 break;
267 case 4:
268 access += ".y";
269 break;
270 case 8:
271 access += ".z";
272 break;
273 case 12:
274 access += ".w";
275 break;
276 }
277 }
278 return access;
279 };
280
281 auto member_data_access = [&](const Member &struct_member) -> string {
282 return struct_member.is_trivial() ? string() : struct_member.name;
283 };
284
285 auto create_getter = [&](/* Tokens of the union declaration inside the struct. */
286 const Token &union_type_tok,
287 const Token &union_var_tok,
288 /* Union member we are creating the accessor for. */
289 const Member &union_member,
290 /* Definition of the type of the accessed member. */
291 const vector<Member> &struct_members) -> string {
292 const size_t union_size = type_size_get(union_type_tok);
293 if (union_size == 0) {
294 report_error(union_type_tok,
295 "Can't infer size of member. Type must be defined in this file and have "
296 "the [[host_shared]] attribute.");
297 return "";
298 }
299 const Member &last_member = struct_members.back();
300 if (last_member.offset + last_member.size != union_size) {
301 report_error(union_type_tok, "union has members of different sizes");
302 return "";
303 }
304
305 string fn_body = "{\n";
306 /* Declare return variable of the same type as the accessed member. */
307 fn_body += " " + union_member.type + " val;\n";
308 for (const auto &member : struct_members) {
309 string to_var = "val" + member_data_access(member);
310 string access = "this_." + string(union_var_tok.str()) +
311 union_data_access(member, union_size);
312 fn_body += " " + to_var + " = " + member_from_float(union_member, member, access) + ";\n";
313 }
314 fn_body += " return val;\n";
315 fn_body += "}\n";
316
317 return "\n" + union_member.type + " " + union_member.name + "() const " + fn_body;
318 };
319
320 auto create_setter = [&](/* Tokens of the union declaration inside the struct. */
321 const Token &union_type_tok,
322 const Token &union_var_tok,
323 /* Union member we are creating the accessor for. */
324 const Member &union_member,
325 /* Definition of the type of the accessed member. */
326 const vector<Member> &struct_members) -> string {
327 const size_t union_size = type_size_get(union_type_tok);
328 if (union_size == 0) {
329 report_error(union_type_tok,
330 "Can't infer size of member. Type must be defined in this file and have "
331 "the [[host_shared]] attribute.");
332 return "";
333 }
334 const Member &last_member = struct_members.back();
335 if (last_member.offset + last_member.size != union_size) {
336 report_error(union_type_tok, "union has members of different sizes");
337 return "";
338 }
339
340 string fn_body = "{\n";
341 for (const auto &member : struct_members) {
342 string to_var = "this->" + string(union_var_tok.str()) +
343 union_data_access(member, union_size);
344 string access = "value" + member_data_access(member);
345 fn_body += " " + to_var + " = " + member_to_float(union_member, member, access) + ";\n";
346 }
347 fn_body += "}\n";
348
349 return "\nvoid " + union_member.name + "_set_(" + union_member.type + " value) " + fn_body;
350 };
351
352 auto flatten_members = [&](Token type, vector<Member> &members) {
353 vector<Member> dst;
354 dst.reserve(members.size());
355 bool expanded = false;
356 for (const auto &member : members) {
357 if (member.is_trivial() || member.is_enum) {
358 dst.emplace_back(member);
359 continue;
360 }
361 if (!struct_members.contains(member.type)) {
362 report_error(
363 type,
364 "Unknown type encountered while unwrapping union. Contained types must be defined "
365 "in this file and decorated with [[host_shared]] attribute.");
366 continue;
367 }
368
369 vector<Member> nested_structure = struct_members.find(member.type)->second;
370 for (Member nested_member : nested_structure) {
371 if (nested_member.is_trivial() || nested_member.is_enum) {
372 dst.emplace_back(member);
373 }
374 else {
375 expanded = true;
376 nested_member.name = member.name + nested_member.name;
377 nested_member.offset = member.offset + nested_member.offset;
378 dst.emplace_back(nested_member);
379 }
380 }
381 }
382 members = dst;
383 return expanded;
384 };
385
386 parser().foreach_struct([&](Token, Scope, Token struct_name, Scope body) {
387 if (union_members.contains(string(struct_name.str()))) {
388 replace_placeholder_member(body);
389 return;
390 }
391
392 body.foreach_declaration([&](Scope, Token, Token type, Scope, Token name, Scope, Token) {
393 if (!union_members.contains(string(type.str()))) {
394 return;
395 }
396
397 const vector<Member> &members = union_members.find(string(type.str()))->second;
398 for (const auto &member : members) {
399 if (!struct_members.contains(member.type)) {
400 report_error(
401 type,
402 "Unknown union member type. Type must be defined in this file and decorated "
403 "with [[host_shared]] attribute.");
404 return;
405 }
406 vector<Member> structure = struct_members.find(member.type)->second;
407 /* Flatten references to other structures, recursively. */
408 while (flatten_members(type, structure)) {
409 }
410
411 parser.insert_after(body.back().prev(), create_getter(type, name, member, structure));
412 parser.insert_after(body.back().prev(), create_setter(type, name, member, structure));
413 }
414 });
415 });
416
417 lower_union_setters(parser);
418}
419
420void SourceProcessor::lower_union_setters(Parser &parser)
421{
422 /* Replace assignment pattern.
423 * Example: `a.b() = c;` > `a.b_set_(c);`
424 * This pattern is currently only allowed for `union_t`. */
425 parser().foreach_match("A()=", [&](const Tokens &t) {
426 parser.insert_before(t[1], "_set_");
427 parser.erase(t[2], t[3]);
428 parser.insert_after(t[3].scope().back(), ")");
429 });
430
431 parser.apply_mutations();
432}
433
434void SourceProcessor::lower_bitfield_setters(Parser &parser)
435{
436 /* Replace assignment pattern.
437 * Example: `a.bitfieldExtract(1,2) = c;` > `a.bitfieldInsert(c,1,2);` */
438 parser().foreach_match("A(..)=", [&](const Tokens &t) {
439 if (t[0].str() != "bitfieldExtract") {
440 return;
441 }
442 Scope assign = t[5].scope();
443 string_view value = parser.substr(assign.front().next(), assign.back());
444 parser.replace(t[0], "bitfieldInsertAssign");
445 parser.insert_after(t[1], string(value) + ", ");
446 parser.replace(assign, "");
447 });
448
449 parser.apply_mutations();
450}
451
459void SourceProcessor::lower_union_accessor_templates(Parser &parser)
460{
461 parser().foreach_struct([&](Token, Scope, Token, Scope body) {
462 body.foreach_match("o{..};", [&](const Tokens &t) {
463 t[1].scope().foreach_declaration(
464 [&](Scope, Token, Token type, Scope template_scope, Token name, Scope, Token) {
465 if (type.str() != "union_t") {
466 report_error(
467 name,
468 "All union members must have their type wrapped using the union_t<T> template.");
469 parser.erase(type, type.find_next(SemiColon));
470 return;
471 }
472
473 /* Remove the template but not the wrapped type. */
474 parser.erase(type);
475 if (template_scope.is_valid()) {
476 parser.erase(template_scope.front());
477 parser.erase(template_scope.back());
478 }
479 });
480 });
481 });
482 parser.apply_mutations();
483}
484
492void SourceProcessor::lower_union_accessor_templates_ast(Parser &parser)
493{
494 for (ClassDecl decl : parser.root().descendants_of_type<ClassDecl>()) {
495 if (decl.is_union()) {
496 for (VarDecl var : decl.body().children_of_type<VarDecl>()) {
497 IdType type = var.type();
498 Id name = type.identifier().name();
499
500 if (type.identifier().name().str() != "union_t") {
501 continue;
502 }
503
504 /* Remove the template but not the wrapped type. */
505 parser.erase(name);
506 TemplateParamList param = type.identifier().template_params();
507 if (param.is_valid()) {
508 parser.erase(param.front());
509 parser.erase(param.back());
510 }
511 }
512 }
513 }
514
515 parser.apply_mutations();
516}
517
518} // namespace blender::gpu::shader
std::vector< parser::Token > Tokens
Definition processor.hh:48
#define str(s)
int size(const AssetLibraryReference *library_reference)
const char * to_string(ShaderStage stage)
Definition mtl_shader.mm:52
@ Enum
Definition types.hh:106
@ SemiColon
Definition types.hh:62