Blender V5.3
symbol_table.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "symbol_table.hh"
10#include "expression.hh"
11
12#include <bit>
13#include <cmath>
14
15namespace bsl {
16
17using namespace blender::gpu::shader::parser;
18using namespace blender::gpu::shader::parser::ast;
19using namespace std;
20using Token = blender::gpu::shader::parser::Token;
21
23{
24 LocalScope node = parser.root();
25 root = scp_arena.alloc(node);
26 register_builtins(node);
27}
28
30{
31 std::optional<AstNodeException> err;
32 Node node = decl.child_last();
33 AssignStmt assign(node);
34
35 if (Expr expr = assign.expr(); expr.is_valid()) {
36 return {expr_type_analysis(scope, expr.child_first()).unwrap(err).type, err};
37 }
38
40 if (!list.is_valid()) {
41 list = assign.initializer_list();
42 }
43
44 if (list.is_valid()) {
45 Initializer init(list.child_first());
46 if (init.child_first().type() == NodeType::InitializerList) {
47 err = {init.child_first(), Diag::AutoTypeNestedInitializer, string(decl.identifier().str())};
48 }
49 else if (init.next().is_valid()) {
50 err = {init.next(), Diag::AutoTypeMultipleExpressions, string(decl.identifier().str())};
51 }
52 else {
53 Expr expr(init.child_first());
54 return {expr_type_analysis(scope, expr.child_first()).unwrap(err).type, err};
55 }
56 }
57
59}
60
63 const SymbolScope &scope) const
64{
65 Expr param = list.child_first();
66
67 std::optional<AstNodeException> err;
68
69 StringPair result = {};
70 for (TemplateArg arg : args.children_range()) {
71 assert(arg.is_valid());
72 if (!param.is_valid()) {
73 err = AstNodeException(list, Diag::TemplateMissingArgument, string(arg.type().str()));
74 }
75 else if (arg.front() == Typename) {
76 if (param.child_count() != 1 || param.child_first().type() != NodeType::LocalVar) {
77 err = AstNodeException(param, Diag::TemplateInvalidArg, string(arg.type().str()));
78 }
79 else {
80 IdQualified type_id(LocalVar(param.child_first()).identifier());
81 auto *type = scope.lookup_class_base(*this, type_id);
82 if (type->template_data) {
83 TemplateParamList list_nested = type_id.template_params();
84 if (!list_nested.is_valid()) {
85 err = AstNodeException(
86 type_id, Diag::TemplateMissingParametersForType, string(type_id.str()));
87 }
88 else {
89 /* Recursive. */
90 auto [type_, _, err_] = type->template_data->lookup_inst(list_nested, scope, *this);
91 type = type_;
92 if (err_) {
93 err = err_;
94 }
95 }
96 }
97 if (type->is_error) {
98 err = AstNodeException(param, Diag::UnknownIdentifier, string(type_id.str()));
99 }
100 else {
101 result.str += "T" + type->identifier;
102 result.str_debug += ", " + type->identifier;
103 }
104 }
105 }
106 else {
107 auto expr_result = expr_type_analysis(scope, param.child_first()).unwrap(err);
108 if (!expr_result.is_constexpr()) {
110 }
111 int val = expr_result.value.comp_as<int>(0);
112 /* Replace minus sign by underscore. */
113 if (arg.type().str() == "bool") {
114 result.str += string("T") + (val ? "true" : "false");
115 }
116 else {
117 result.str += string("T") + (val < 0 ? "_" : "") + to_string(abs(val));
118 }
119 result.str_debug += ", " + to_string(val);
120 }
121 param = param.next();
122 }
123 return {result, err};
124}
125
127 Node start,
128 int &node_count,
129 bool allow_compilation_constant) const
130{
131 const char *expr_type = allow_compilation_constant ? "static branch condition" :
132 "constant expression";
133 std::optional<AstNodeException> error;
134 string expr_str;
135 for (Node child = start; child.is_valid(); child = child.next()) {
136 switch (child.type()) {
137 case NodeType::LocalVar: {
138 SymbolVariable *var = scope.lookup_variable(*this, LocalVar(child).identifier());
139 /* Resolve member access. */
140
141 for (Node next = child.next(); next.type() == NodeType::Op && next.front() == '.';
142 next = child.next())
143 {
144 child = next.next();
145
146 if (child.type() == NodeType::LocalVar) {
147 var = var->type->lookup_variable(*this, LocalVar(child).identifier());
148 }
149 else if (child.type() == NodeType::FuncCall) {
150 return {
151 "0",
152 AstNodeException(child, Diag::ExprCannotContainFunctionCalls, string(expr_type))};
153 }
154 else {
155 return {
156 "0",
158 child, Diag::UnsupportedSymbolInExpr, string(child.str()), string(expr_type))};
159 }
160 }
161
162 if (var->is_error) {
163 return {"0", AstNodeException(child, Diag::UnknownVariable, string(child.str()))};
164 }
165
166 if (allow_compilation_constant && var->is_compilation_const) {
167 /* Compilation constants are in global namespace. */
168 expr_str += "SRT_CONSTANT_" + var->identifier;
169 }
170 else if (!var->is_constexpr) {
171 return {"0", AstNodeException(child, Diag::NonConstVariableInExpr, string(expr_type))};
172 }
173 else {
174 expr_str += var->value_str();
175 }
176 node_count++;
177 break;
178 }
180 return {"0",
181 AstNodeException(child, Diag::ExprCannotContainFunctionCalls, string(expr_type))};
182 case NodeType::ExprSub: {
183 auto [str, err] = expr_to_string(scope, ExprSub(child).expr().child_first(), node_count);
184 expr_str += '(';
185 expr_str += str;
186 expr_str += ')';
187 node_count++;
188 if (err) {
189 return {"0", err};
190 }
191 break;
192 }
193 case NodeType::Op:
195 expr_str += child.str();
196 /* WORKAROUND: The runtime shader preprocessor has a bug which mangles operators that are
197 * next to each others. */
198 expr_str += " ";
199 node_count++;
200 break;
201 default:
202 return {"0",
204 child, Diag::UnsupportedSymbolInExpr, string(child.str()), string(expr_type))};
205 }
206 }
207 return {expr_str, error};
208}
209
210void SymbolTable::register_builtins(LocalScope node)
211{
212 Token tok = node.back();
213
214 struct BuiltinBasicType {
215 string id;
216 size_t size;
217 size_t align;
219 };
220
221 const vector<BuiltinBasicType> basic_types = {
223
224 {"char", 1, 1, builtin::char_t},
225 {"short", 2, 2, builtin::short_t},
226 {"int", 4, 4, builtin::int_t},
227 {"uchar", 1, 1, builtin::uchar_t},
228 {"ushort", 2, 2, builtin::ushort_t},
229 {"uint", 4, 4, builtin::uint_t},
230 {"half", 2, 2, builtin::half_t},
231 {"float", 4, 4, builtin::float_t},
232 {"bool", 1, 1, builtin::bool_t},
233 {"bool32_t", 4, 4, builtin::bool_t},
234
235 {"string_t", 4, 4, builtin::uint_t},
236 {"TextureWriteFormat", 4, 4, builtin::int_t},
237 };
238
239 for (const auto &t : basic_types) {
240 SymbolClass *cls = cls_arena.alloc(root, tok, t.id, t.size, t.align, t.cls);
241 cls->is_std140_compatible = t.align == 4;
242 cls->is_std430_compatible = t.align == 4;
243 cls->comp_len = 1;
244 cls->is_error = t.id == err_symbol;
245 root->classes.emplace(t.id, cls);
246 }
247
249
250 this->char_cls = root->lookup_class("char");
251 this->short_cls = root->lookup_class("short");
252 this->int_cls = root->lookup_class("int");
253 this->uchar_cls = root->lookup_class("uchar");
254 this->ushort_cls = root->lookup_class("ushort");
255 this->uint_cls = root->lookup_class("uint");
256 this->half_cls = root->lookup_class("half");
257 this->float_cls = root->lookup_class("float");
258 this->bool_cls = root->lookup_class("bool");
259 this->bool32_t_cls = root->lookup_class("bool32_t");
260
261 const vector<BuiltinBasicType> special_types = {
262 {err_symbol, 1, 1},
263
264 {"void", 1, 1}, /* Only for function return type. */
265
266 {"samplerBuffer", 0, 1},
267 {"sampler1D", 0, 1},
268 {"sampler2D", 0, 1},
269 {"sampler3D", 0, 1},
270 {"isamplerBuffer", 0, 1},
271 {"isampler1D", 0, 1},
272 {"isampler2D", 0, 1},
273 {"isampler3D", 0, 1},
274 {"usamplerBuffer", 0, 1},
275 {"usampler1D", 0, 1},
276 {"usampler2D", 0, 1},
277 {"usampler3D", 0, 1},
278 {"sampler1DArray", 0, 1},
279 {"sampler2DArray", 0, 1},
280 {"isampler1DArray", 0, 1},
281 {"isampler2DArray", 0, 1},
282 {"usampler1DArray", 0, 1},
283 {"usampler2DArray", 0, 1},
284 {"samplerCube", 0, 1},
285 {"isamplerCube", 0, 1},
286 {"usamplerCube", 0, 1},
287 {"samplerCubeArray", 0, 1},
288 {"isamplerCubeArray", 0, 1},
289 {"usamplerCubeArray", 0, 1},
290 {"usampler1DAtomic", 0, 1},
291 {"usampler2DAtomic", 0, 1},
292 {"usampler2DArrayAtomic", 0, 1},
293 {"usampler3DAtomic", 0, 1},
294 {"isampler1DAtomic", 0, 1},
295 {"isampler2DAtomic", 0, 1},
296 {"isampler2DArrayAtomic", 0, 1},
297 {"isampler3DAtomic", 0, 1},
298 {"sampler2DDepth", 0, 1},
299 {"sampler2DArrayDepth", 0, 1},
300 {"samplerCubeDepth", 0, 1},
301 {"samplerCubeArrayDepth", 0, 1},
302 {"image1D", 0, 1},
303 {"image2D", 0, 1},
304 {"image3D", 0, 1},
305 {"iimage1D", 0, 1},
306 {"iimage2D", 0, 1},
307 {"iimage3D", 0, 1},
308 {"uimage1D", 0, 1},
309 {"uimage2D", 0, 1},
310 {"uimage3D", 0, 1},
311 {"image1DArray", 0, 1},
312 {"image2DArray", 0, 1},
313 {"iimage1DArray", 0, 1},
314 {"iimage2DArray", 0, 1},
315 {"uimage1DArray", 0, 1},
316 {"uimage2DArray", 0, 1},
317 {"iimage2DAtomic", 0, 1},
318 {"iimage3DAtomic", 0, 1},
319 {"uimage2DAtomic", 0, 1},
320 {"uimage3DAtomic", 0, 1},
321 {"iimage2DArrayAtomic", 0, 1},
322 {"uimage2DArrayAtomic", 0, 1},
323
324 {"rayQuery", 0, 1},
325 {"accelerationStructure", 0, 1},
326
327 {"ShaderCreateInfo", 1, 1}, /* Only for compatibility. To be removed. */
328 };
329
330 for (const auto &t : special_types) {
331 SymbolClass *sym = cls_arena.alloc(root, tok, t.id, t.size, t.align);
332 sym->is_error = t.id == err_symbol;
333 sym->is_opaque = true;
334 root->classes.emplace(t.id, sym);
335 }
336
337 this->str_cls = root->lookup_class("string_t");
338 this->void_cls = root->lookup_class("void");
339
340 struct BuiltinVector {
341 string id;
342 SymbolClass *base;
343 int comp;
344 int align;
345 builtin::ClassId cls_id;
346 };
347
348 const char *swizzle_xyzw[4] = {"x", "y", "z", "w"};
349 const char *swizzle_rgba[4] = {"r", "g", "b", "a"};
350
351 auto generate_vector = [&](const BuiltinVector &t, const bool do_swizzle) {
352 SymbolClass *base = t.base;
353 SymbolClass *cls = cls_arena.alloc(root, tok, t.id, t.comp * base->size, t.align, t.cls_id);
354 root->classes.emplace(t.id, cls);
355 root->scopes.emplace(t.id, cls);
356
357 cls->is_std140_compatible = t.align >= 8 && t.id != "float3" && base->is_std140_compatible;
358 cls->is_std430_compatible = cls->is_std140_compatible;
359
360 SymbolFunction *sub_op = fun_arena.alloc(
362
363 cls->operator_subscript = sub_op;
364 cls->functions.emplace(subscript_operator_id, sub_op);
365 cls->comp_len = t.comp * base->comp_len;
366
367 for (int i = 0; i < t.comp && do_swizzle; i++) {
368 string id_xyzw(swizzle_xyzw[i]);
369 cls->variables.emplace(id_xyzw, var_arena.alloc(cls, base, tok, id_xyzw));
370 string id_rgba(swizzle_rgba[i]);
371 cls->variables.emplace(id_rgba, var_arena.alloc(cls, base, tok, id_rgba));
372 }
373 };
374
375 const std::vector<BuiltinVector> vectors = {
376 {"float2", float_cls, 2, 8, builtin::ClassId::float2_t},
377 {"float3", float_cls, 3, 16, builtin::ClassId::float3_t},
378 {"float4", float_cls, 4, 16, builtin::ClassId::float4_t},
379 {"half2", half_cls, 2, 4, builtin::ClassId::half2_t},
380 {"half3", half_cls, 3, 8, builtin::ClassId::half3_t},
381 {"half4", half_cls, 4, 8, builtin::ClassId::half4_t},
382 {"char2", char_cls, 2, 2, builtin::ClassId::char2_t},
383 {"char3", char_cls, 3, 3, builtin::ClassId::char3_t},
384 {"char4", char_cls, 4, 4, builtin::ClassId::char4_t},
385 {"uchar2", uchar_cls, 2, 2, builtin::ClassId::uchar2_t},
386 {"uchar3", uchar_cls, 3, 3, builtin::ClassId::uchar3_t},
387 {"uchar4", uchar_cls, 4, 4, builtin::ClassId::uchar4_t},
388 {"short2", short_cls, 2, 4, builtin::ClassId::short2_t},
389 {"short3", short_cls, 3, 6, builtin::ClassId::short3_t},
390 {"short4", short_cls, 4, 8, builtin::ClassId::short4_t},
391 {"ushort2", ushort_cls, 2, 4, builtin::ClassId::ushort2_t},
392 {"ushort3", ushort_cls, 3, 6, builtin::ClassId::ushort3_t},
393 {"ushort4", ushort_cls, 4, 8, builtin::ClassId::ushort4_t},
394 {"int2", int_cls, 2, 8, builtin::ClassId::int2_t},
395 {"int3", int_cls, 3, 16, builtin::ClassId::int3_t},
396 {"int4", int_cls, 4, 16, builtin::ClassId::int4_t},
397 {"uint2", uint_cls, 2, 8, builtin::ClassId::uint2_t},
398 {"uint3", uint_cls, 3, 16, builtin::ClassId::uint3_t},
399 {"uint4", uint_cls, 4, 16, builtin::ClassId::uint4_t},
400 {"bool2", bool_cls, 2, 2, builtin::ClassId::bool2_t},
401 {"bool3", bool_cls, 3, 3, builtin::ClassId::bool3_t},
402 {"bool4", bool_cls, 4, 4, builtin::ClassId::bool4_t},
403 {"packed_float2", float_cls, 2, 8, builtin::ClassId::float2_t},
404 {"packed_float3", float_cls, 3, 16, builtin::ClassId::float3_t},
405 {"packed_float4", float_cls, 4, 16, builtin::ClassId::float4_t},
406 {"packed_int2", int_cls, 2, 8, builtin::ClassId::int2_t},
407 {"packed_int3", int_cls, 3, 16, builtin::ClassId::int3_t},
408 {"packed_int4", int_cls, 4, 16, builtin::ClassId::int4_t},
409 };
410
411 for (const auto &t : vectors) {
412 generate_vector(t, true);
413 }
414
415 /* Fast lookup cache. */
416 this->char2_cls = root->lookup_class("char2");
417 this->short2_cls = root->lookup_class("short2");
418 this->int2_cls = root->lookup_class("int2");
419 this->uchar2_cls = root->lookup_class("uchar2");
420 this->ushort2_cls = root->lookup_class("ushort2");
421 this->uint2_cls = root->lookup_class("uint2");
422 this->half2_cls = root->lookup_class("half2");
423 this->char3_cls = root->lookup_class("char3");
424 this->short3_cls = root->lookup_class("short3");
425 this->int3_cls = root->lookup_class("int3");
426 this->uchar3_cls = root->lookup_class("uchar3");
427 this->ushort3_cls = root->lookup_class("ushort3");
428 this->uint3_cls = root->lookup_class("uint3");
429 this->half3_cls = root->lookup_class("half3");
430 this->char4_cls = root->lookup_class("char4");
431 this->short4_cls = root->lookup_class("short4");
432 this->int4_cls = root->lookup_class("int4");
433 this->uchar4_cls = root->lookup_class("uchar4");
434 this->ushort4_cls = root->lookup_class("ushort4");
435 this->uint4_cls = root->lookup_class("uint4");
436 this->half4_cls = root->lookup_class("half4");
437 this->float2_cls = root->lookup_class("float2");
438 this->float3_cls = root->lookup_class("float3");
439 this->float4_cls = root->lookup_class("float4");
440 this->packed_float2_cls = root->lookup_class("packed_float2");
441 this->packed_float3_cls = root->lookup_class("packed_float3");
442 this->packed_float4_cls = root->lookup_class("packed_float4");
443 this->bool2_cls = root->lookup_class("bool2");
444 this->bool3_cls = root->lookup_class("bool3");
445 this->bool4_cls = root->lookup_class("bool4");
446
447 const std::vector<BuiltinVector> matrices = {
448 {"float2x2", float2_cls, 2, 16, builtin::ClassId::float2x2_t},
449 {"float2x3", float3_cls, 2, 16, builtin::ClassId::float2x3_t},
450 {"float2x4", float4_cls, 2, 16, builtin::ClassId::float2x4_t},
451 {"float3x2", float2_cls, 3, 16, builtin::ClassId::float3x2_t},
452 {"float3x3", float3_cls, 3, 16, builtin::ClassId::float3x3_t},
453 {"float3x4", float4_cls, 3, 16, builtin::ClassId::float3x4_t},
454 {"float4x2", float2_cls, 4, 16, builtin::ClassId::float4x2_t},
455 {"float4x3", float3_cls, 4, 16, builtin::ClassId::float4x3_t},
456 {"float4x4", float4_cls, 4, 16, builtin::ClassId::float4x4_t},
457 };
458
459 for (const auto &t : matrices) {
460 generate_vector(t, false);
461 }
462
463 this->float2x2_cls = root->lookup_class("float2x2");
464 this->float2x3_cls = root->lookup_class("float2x3");
465 this->float2x4_cls = root->lookup_class("float2x4");
466 this->float3x2_cls = root->lookup_class("float3x2");
467 this->float3x3_cls = root->lookup_class("float3x3");
468 this->float3x4_cls = root->lookup_class("float3x4");
469 this->float4x2_cls = root->lookup_class("float4x2");
470 this->float4x3_cls = root->lookup_class("float4x3");
471 this->float4x4_cls = root->lookup_class("float4x4");
472
473 /* Aliases. */
474 root->classes.emplace(string("int32_t"), root->classes["int"]);
475 root->classes.emplace(string("uint32_t"), root->classes["uint"]);
476
477 /* Swizzle Generation */
478 /* We do this in a separate loop because a swizzle on a float2 (like `.xxxx`).
479 * might require float4 to already be registered in root->classes. */
480 for (const auto &[name, base, comp, __, ___] : vectors) {
481 SymbolClass *type = root->classes[name];
482 auto generate_swizzles =
483 [&](auto &self, string current, int target_length, auto swizzle) -> void {
484 if (current.length() == target_length) {
485 /* e.g., "float" + "3" = "float3" */
486 string return_type_id = base->identifier + to_string(target_length);
487
488 SymbolVariable var(type, root->classes[return_type_id], tok, current);
489 SymbolFunction fun(
490 type, tok, root->classes[return_type_id], current, SymbolFunction::MEMBER);
491
492 type->variables.emplace(current, var_arena.alloc(var));
493 type->functions.emplace(current, fun_arena.alloc(fun));
494 return;
495 }
496 /* Only allow swizzling components that actually exist on this vector type. */
497 for (int i = 0; i < comp; i++) {
498 self(self, current + swizzle[i], target_length, swizzle);
499 }
500 };
501 /* Generate combinations for vec2, vec3, and vec4 variants. */
502 for (int len = 2; len <= 4; len++) {
503 generate_swizzles(generate_swizzles, "", len, swizzle_xyzw);
504 generate_swizzles(generate_swizzles, "", len, swizzle_rgba);
505 }
506 }
507
508 auto builtin_function_decl =
509 [&](const BuiltinFunc &fn, bool allow_vector_promotion, bool is_constexpr) {
510 SymbolFunction *sym = fun_arena.alloc(
511 root, tok, fn.return_type, fn.id, SymbolFunction::GLOBAL);
512 sym->is_error = fn.id == err_symbol;
513 sym->allow_vector_promotion = allow_vector_promotion;
514 sym->is_constexpr = is_constexpr;
515 sym->is_builtin = true;
516 sym->reserve_arguments(fn.arg_types.size());
517 for (SymbolClass *arg : fn.arg_types) {
518 sym->add_argument(true, arg);
519 }
520 root->function_emplace(sym, true);
521 };
522
523 const vector<BuiltinFunc> functions = generate_all_builtin_functions();
524 /* Builtin functions. */
525 for (const auto &f : functions) {
526 builtin_function_decl(f, false, false);
527 }
528
529 const vector<BuiltinFunc> functions_constexpr = generate_all_builtin_constexpr_functions();
530 for (const auto &f : functions_constexpr) {
531 builtin_function_decl(f, false, true);
532 }
533
534 const vector<BuiltinType> builtin_types = generate_builtin_types();
535
536 const vector<BuiltinFunc> builtin_constructors = generate_all_constructors(builtin_types);
537 for (const auto &fn : builtin_constructors) {
538 builtin_function_decl(fn, true, true);
539 }
540
541 image_formats = {
542 {0, "SNORM_8"}, {1, "SNORM_8_8"}, {2, "SNORM_8_8_8_8"},
543 {3, "SNORM_16"}, {4, "SNORM_16_16"}, {5, "SNORM_16_16_16_16"},
544 {6, "UNORM_8"}, {7, "UNORM_8_8"}, {8, "UNORM_8_8_8_8"},
545 {9, "UNORM_16"}, {10, "UNORM_16_16"}, {11, "UNORM_16_16_16_16"},
546 {12, "SINT_8"}, {13, "SINT_8_8"}, {14, "SINT_8_8_8_8"},
547 {15, "SINT_16"}, {16, "SINT_16_16"}, {17, "SINT_16_16_16_16"},
548 {18, "SINT_32"}, {19, "SINT_32_32"}, {20, "SINT_32_32_32_32"},
549 {21, "UINT_8"}, {22, "UINT_8_8"}, {23, "UINT_8_8_8_8"},
550 {24, "UINT_16"}, {25, "UINT_16_16"}, {26, "UINT_16_16_16_16"},
551 {27, "UINT_32"}, {28, "UINT_32_32"}, {29, "UINT_32_32_32_32"},
552 {30, "SFLOAT_16"}, {31, "SFLOAT_16_16"}, {32, "SFLOAT_16_16_16_16"},
553 {33, "SFLOAT_32"}, {34, "SFLOAT_32_32"}, {35, "SFLOAT_32_32_32_32"},
554 {36, "UNORM_10_10_10_2"}, {37, "UINT_10_10_10_2"}, {38, "UFLOAT_11_11_10"},
555 };
556
557 struct BuiltinConst {
558 string id;
559 string type;
560 bool is_constexpr;
561 ConstexprValue value;
562 };
563
564 std::vector<BuiltinConst> consts = {
565 /* Error variable symbol. */
567
568 {"true", "bool", true, ConstexprValue(true)},
569 {"false", "bool", true, ConstexprValue(false)},
570
571 /* WORKAROUND: Should become an entry point argument. */
572 {"gl_FragStencilRefARB", "uint", false, ConstexprError(0)},
573
574 {"gpu_RayFlagsNone", "uint", false, ConstexprValue(0)},
575 {"gpu_RayFlagsOpaque", "uint", false, ConstexprValue(0)},
576 {"gpu_RayFlagsNoOpaque", "uint", false, ConstexprValue(0)},
577 {"gpu_RayFlagsTerminateOnFirstHit", "uint", false, ConstexprValue(0)},
578 {"gpu_RayFlagsSkipClosestHitShader", "uint", false, ConstexprValue(0)},
579 {"gpu_RayFlagsCullBackFacingTriangles", "uint", false, ConstexprValue(0)},
580 {"gpu_RayFlagsCullFrontFacingTriangles", "uint", false, ConstexprValue(0)},
581 {"gpu_RayFlagsCullOpaque", "uint", false, ConstexprValue(0)},
582 {"gpu_RayFlagsCullNoOpaque", "uint", false, ConstexprValue(0)},
583 {"gpu_RayQueryCommittedIntersectionNone", "uint", false, ConstexprValue(0)},
584 {"gpu_RayQueryCommittedIntersectionTriangle", "uint", false, ConstexprValue(0)},
585 {"gpu_RayQueryCommittedIntersectionGenerated", "uint", false, ConstexprValue(0)},
586 {"gpu_RayQueryCandidateIntersectionTriangle", "uint", false, ConstexprValue(0)},
587 {"gpu_RayQueryCandidateIntersectionAABB", "uint", false, ConstexprValue(0)},
588
589 {"FLT_MAX", "float", true, ConstexprValue(std::bit_cast<float>(0x7F7FFFFFu))},
590 {"FLT_MIN", "float", true, ConstexprValue(std::bit_cast<float>(0x00800000u))},
591 {"FLT_EPSILON", "float", true, ConstexprValue(1.192092896e-07f)},
592 {"SHRT_MAX", "int", true, ConstexprValue(0x00007FFF)},
593 {"INT_MAX", "int", true, ConstexprValue(0x7FFFFFFF)},
594 {"USHRT_MAX", "uint", true, ConstexprValue(0x0000FFFFu)},
595 {"UINT_MAX", "uint", true, ConstexprValue(0xFFFFFFFFu)},
596 {"NAN_FLT", "float", true, ConstexprValue(NAN)},
597 {"FLT_11_MAX", "float", false, ConstexprValue(0)},
598 {"FLT_10_MAX", "float", false, ConstexprValue(0)},
599 {"FLT_11_11_10_MAX", "float3", false, ConstexprValue(0)},
600
601 {"M_PI", "float", true, ConstexprValue(3.14159265358979323846f)},
602 {"M_TAU", "float", true, ConstexprValue(6.28318530717958647692f)},
603 {"M_PI_2", "float", true, ConstexprValue(1.57079632679489661923f)},
604 {"M_PI_4", "float", true, ConstexprValue(0.78539816339744830962f)},
605 {"M_SQRT2", "float", true, ConstexprValue(1.41421356237309504880f)},
606 {"M_SQRT1_2", "float", true, ConstexprValue(0.70710678118654752440f)},
607 {"M_SQRT3", "float", true, ConstexprValue(1.73205080756887729352f)},
608 {"M_SQRT1_3", "float", true, ConstexprValue(0.57735026918962576450f)},
609 {"M_1_PI", "float", true, ConstexprValue(0.318309886183790671538f)},
610 {"M_E", "float", true, ConstexprValue(2.7182818284590452354f)},
611 {"M_LOG2E", "float", true, ConstexprValue(1.4426950408889634074f)},
612 {"M_LOG10E", "float", true, ConstexprValue(0.43429448190325182765f)},
613 {"M_LN2", "float", true, ConstexprValue(0.69314718055994530942f)},
614 {"M_LN10", "float", true, ConstexprValue(2.30258509299404568402f)},
615 };
616
617 for (auto [value, name] : image_formats) {
618 consts.emplace_back(name, "int", true, ConstexprValue(value));
619 }
620
621 /* Boolean constants. */
622 for (auto c : consts) {
623 SymbolVariable *sym = var_arena.alloc(root, root->lookup_class(c.type), tok, string(c.id));
624 sym->is_error = c.id == err_symbol;
625 sym->is_static = c.is_constexpr;
626 sym->is_constexpr = c.is_constexpr;
627 sym->value = c.value;
628 root->variables.emplace(sym->identifier, sym);
629 }
630
633}
634
636{
637 if (lit == "true" || lit == "false") {
638 return bool_cls;
639 }
640
641 /* Convert to lowercase helper for easier suffix matching */
642 string str;
643 str.reserve(lit.size());
644 for (char c : lit) {
645 str.push_back(tolower(static_cast<unsigned char>(c)));
646 }
647
648 bool is_hex = str.starts_with("0x");
649
650 bool has_decimal = (str.find('.') != string::npos);
651 bool has_exponent = !is_hex && (str.find('e') != string::npos);
652 bool has_float_suffix = !is_hex && (str.back() == 'f');
653
654 if (has_decimal || has_exponent || has_float_suffix) {
655 return float_cls;
656 }
657 if (str.back() == 'u') {
658 return uint_cls;
659 }
660 return int_cls;
661}
662
664 const SymbolClass *to,
665 const bool ctor_conversion) const
666{
667 if (from == to) {
668 return MatchRank::Exact;
669 }
670
671 if (!from->is_builtin()) {
672 return MatchRank::None;
673 }
674
675 /* Casting enum or packed types of the underlying type matches. */
676 if (from->builtin_class == to->builtin_class) {
678 }
679
680 using namespace builtin;
681
682 const builtin::ClassId from_cls = from->builtin_class;
683 const builtin::ClassId to_cls = to->builtin_class;
684
685 const uint16_t from_raw = uint16_t(from_cls);
686 const uint16_t to_raw = uint16_t(to_cls);
687
688 /* Masking BASE_MASK strips component length, leaving only base type flags and rank. */
689 const uint16_t from_len = from_raw & LEN_MASK;
690 const uint16_t to_len = to_raw & LEN_MASK;
691 /* Check standard GLSL compatible convertible base types (int <-> uint <-> float). */
692 const bool both_numeric = (from_raw & FLAG_NUMERIC) && (to_raw & FLAG_NUMERIC);
693 /* Require both the family (Integer vs Float) and the signedness to match exactly */
694 const uint16_t PROMOTE_MASK = FLAG_INTEGER | FLAG_SIGN;
695 const bool is_promotion = (from_raw & PROMOTE_MASK) == (to_raw & PROMOTE_MASK);
696
697 if (from_len == to_len) {
698 if (from_len == 1) {
699 if (both_numeric || ctor_conversion) {
700 /* Scalar conversion. */
701 return is_promotion ? MatchRank::Promotion : MatchRank::Conversion;
702 }
703 }
704 if (ctor_conversion) {
705 /* Vector conversion. */
707 }
708 }
709
710 return MatchRank::None;
711}
712
713/* Generates every single legal BSL builtin function dynamically. */
714vector<SymbolTable::BuiltinFunc> SymbolTable::generate_all_builtin_functions()
715{
716 /* Start with the error symbol. */
717 vector<BuiltinFunc> functions = {{err_cls, err_symbol, {}}};
718
719 /* Special functions */
720 functions.push_back({uint_cls, "print_data", {uint_cls, str_cls}});
721 functions.push_back({uint_cls, "print_start", {uint_cls}});
722 functions.push_back({void_cls, "static_assert", {bool_cls}});
723
724 for (SymbolClass *type : {float_cls,
728 int_cls,
729 int2_cls,
730 int3_cls,
731 int4_cls,
732 uint_cls,
733 uint2_cls,
734 uint3_cls,
735 uint4_cls})
736 {
737 functions.push_back({bool_cls, "in_range_inclusive", {type, type, type}});
738 functions.push_back({bool_cls, "in_range_exclusive", {type, type, type}});
739 }
740
741 for (SymbolClass *sampler_t : {root->lookup_class("sampler2DDepth"),
742 root->lookup_class("sampler2D"),
743 root->lookup_class("isampler2D"),
744 root->lookup_class("usampler2D")})
745 {
746 functions.push_back({bool_cls, "in_texture_range", {int2_cls, sampler_t}});
747 }
748
749 for (SymbolClass *sampler_t : {root->lookup_class("sampler2DArray"),
750 root->lookup_class("isampler2DArray"),
751 root->lookup_class("usampler2DArray"),
752 root->lookup_class("sampler3D"),
753 root->lookup_class("isampler3D"),
754 root->lookup_class("usampler3D"),
755 root->lookup_class("isampler3DAtomic"),
756 root->lookup_class("usampler3DAtomic"),
757 root->lookup_class("isampler2DArrayAtomic"),
758 root->lookup_class("usampler2DArrayAtomic")})
759 {
760 functions.push_back({bool_cls, "in_texture_range", {int2_cls, sampler_t}});
761 }
762
763 for (SymbolClass *image_t : {root->lookup_class("iimage1DArray"),
764 root->lookup_class("uimage1DArray"),
765 root->lookup_class("image2D"),
766 root->lookup_class("iimage2D"),
767 root->lookup_class("uimage2D"),
768 root->lookup_class("iimage2DAtomic"),
769 root->lookup_class("uimage2DAtomic")})
770 {
771 functions.push_back({bool_cls, "in_image_range", {int2_cls, image_t}});
772 }
773
774 for (SymbolClass *image_t : {root->lookup_class("image2DArray"),
775 root->lookup_class("iimage2DArray"),
776 root->lookup_class("uimage2DArray"),
777 root->lookup_class("image3D"),
778 root->lookup_class("iimage3D"),
779 root->lookup_class("uimage3D"),
780 root->lookup_class("iimage3DAtomic"),
781 root->lookup_class("uimage3DAtomic"),
782 root->lookup_class("iimage2DArrayAtomic"),
783 root->lookup_class("uimage2DArrayAtomic")})
784 {
785 functions.push_back({bool_cls, "in_image_range", {int2_cls, image_t}});
786 }
787
788 /* There are many other overloads but we don't use them currently. */
789 functions.push_back({uint_cls, "simd_broadcast_first", {uint_cls}});
790 functions.push_back({uint_cls, "simd_min", {uint_cls}});
791 functions.push_back({uint_cls, "simd_max", {uint_cls}});
792 functions.push_back({uint_cls, "simd_or", {uint_cls}});
793
794 /* Type definition vectors for vectorized generation */
795 const vector<SymbolClass *> f_types = {float_cls, float2_cls, float3_cls, float4_cls};
796 const vector<SymbolClass *> i_types = {int_cls, int2_cls, int3_cls, int4_cls};
797 const vector<SymbolClass *> u_types = {uint_cls, uint2_cls, uint3_cls, uint4_cls};
798 const vector<SymbolClass *> b_types = {bool_cls, bool2_cls, bool3_cls, bool4_cls};
799
800 /* 1-argument component-wise functions (e.g. sin, cos, abs). */
801 auto add_unary = [&](const string &name, const vector<SymbolClass *> &types) {
802 for (const auto &t : types) {
803 functions.push_back({t, name, {t}});
804 }
805 };
806
807 /* 2-argument component-wise functions (e.g. pow, min, max). */
808 auto add_binary = [&](const string &name, const vector<SymbolClass *> &types) {
809 for (const auto &t : types) {
810 functions.push_back({t, name, {t, t}});
811 }
812 };
813
814 /* 3-argument component-wise functions (e.g. clamp, mix, fma). */
815 auto add_ternary = [&](const string &name, const vector<SymbolClass *> &types) {
816 for (const auto &t : types) {
817 functions.push_back({t, name, {t, t, t}});
818 }
819 };
820
821 /* Functions with vector-scalar variants (e.g. min(float3, float), clamp(float3, float, float)).
822 */
823 auto add_vec_scalar_binary = [&](const string &name, const vector<SymbolClass *> &types) {
824 SymbolClass *scalar = types[0];
825 for (size_t i = 1; i < types.size(); ++i) {
826 functions.push_back({types[i], name, {types[i], scalar}});
827 }
828 };
829
830 auto add_scalar_vec_binary = [&](const string &name, const vector<SymbolClass *> &types) {
831 SymbolClass *scalar = types[0];
832 for (size_t i = 1; i < types.size(); ++i) {
833 functions.push_back({types[i], name, {scalar, types[i]}});
834 }
835 };
836
837 auto add_vec_scalar_scalar_ternary = [&](const string &name,
838 const vector<SymbolClass *> &types) {
839 SymbolClass *scalar = types[0];
840 for (size_t i = 1; i < types.size(); ++i) {
841 functions.push_back({types[i], name, {types[i], scalar, scalar}});
842 }
843 };
844
845 auto add_vec_vec_scalar_ternary = [&](const string &name, const vector<SymbolClass *> &types) {
846 SymbolClass *scalar = types[0];
847 for (size_t i = 1; i < types.size(); ++i) {
848 functions.push_back({types[i], name, {types[i], types[i], scalar}});
849 }
850 };
851
852 auto add_scalar_scalar_vec_ternary = [&](const string &name,
853 const vector<SymbolClass *> &types) {
854 SymbolClass *scalar = types[0];
855 for (size_t i = 1; i < types.size(); ++i) {
856 functions.push_back({types[i], name, {types[i], scalar, scalar}});
857 }
858 };
859
860 const vector<string> trig_unary = {
861 "radians",
862 "degrees",
863 "sin",
864 "cos",
865 "tan",
866 "asin",
867 "acos",
868 "atan",
869 "sinh",
870 "cosh",
871 "tanh",
872 "asinh",
873 "acosh",
874 "atanh",
875 /* For C++ compat. Do something about it. Doesn't belong here. */
876 "sinf",
877 "cosf",
878 "tanf",
879 "atanf",
880 };
881 for (const auto &name : trig_unary) {
882 add_unary(name, f_types);
883 }
884 /* 2-argument atan(y, x). */
885 add_binary("atan", f_types);
886
887 for (string fn : {"EXPECT_EQ", "EXPECT_NE", "EXPECT_LE", "EXPECT_LT", "EXPECT_GE", "EXPECT_GT"})
888 {
889 add_binary(fn, f_types);
890 add_binary(fn, i_types);
891 add_binary(fn, u_types);
892 add_binary(fn, {char_cls, uchar_cls});
893 }
894 add_unary("EXPECT_TRUE", b_types);
895 add_unary("EXPECT_FALSE", b_types);
896 add_ternary("EXPECT_NEAR", f_types);
897 add_vec_vec_scalar_ternary("EXPECT_NEAR", f_types);
898
899 const vector<string> exp_unary = {
900 "exp",
901 "log",
902 "exp2",
903 "log2",
904 "sqrt",
905 "inversesqrt",
906 /* For C++ compat. Do something about it. Doesn't belong here. */
907 "expf",
908 "sqrtf",
909 };
910 for (const auto &name : exp_unary) {
911 add_unary(name, f_types);
912 }
913 add_binary("pow", f_types);
914
915 const vector<string> common_float_unary = {
916 "floor",
917 "trunc",
918 "round",
919 "roundEven",
920 "ceil",
921 "fract",
922 "saturate",
923 /* For C++ compat. Do something about it. Doesn't belong here. */
924 "floorf",
925 "ceilf",
926 };
927 for (const auto &name : common_float_unary) {
928 add_unary(name, f_types);
929 }
930
931 for (size_t i = 0; i < f_types.size(); ++i) {
932 functions.push_back({b_types[i], "isnan", {f_types[i]}});
933 functions.push_back({b_types[i], "isinf", {f_types[i]}});
934 }
935
936 add_unary("abs", f_types);
937 add_unary("abs", i_types);
938
939 add_unary("sign", f_types);
940 add_unary("sign", i_types);
941
942 add_binary("mod", f_types);
943 add_binary("modf", f_types);
944 add_vec_scalar_binary("mod", f_types);
945
946 for (const auto &tlist : {f_types, i_types, u_types}) {
947 add_binary("min", tlist);
948 add_vec_scalar_binary("min", tlist);
949 add_binary("max", tlist);
950 add_vec_scalar_binary("max", tlist);
951 add_ternary("clamp", tlist);
952 add_vec_scalar_scalar_ternary("clamp", tlist);
953 }
954
955 add_ternary("mix", f_types);
956 add_vec_vec_scalar_ternary("mix", f_types);
957 for (size_t i = 0; i < f_types.size(); ++i) {
958 functions.push_back({f_types[i], "mix", {f_types[i], f_types[i], b_types[i]}});
959 functions.push_back({f_types[i], "select", {f_types[i], f_types[i], b_types[i]}});
960 }
961
962 add_binary("step", f_types);
963 add_scalar_vec_binary("step", f_types);
964 add_ternary("smoothstep", f_types);
965 add_scalar_scalar_vec_ternary("smoothstep", f_types);
966
967 add_ternary("fma", f_types);
968
969 /* Matrix conversion functions. */
970 functions.push_back({float2x2_cls, "to_float2x2", {float3x3_cls}});
971 functions.push_back({float2x2_cls, "to_float2x2", {float4x4_cls}});
972 functions.push_back({float3x3_cls, "to_float3x3", {float4x4_cls}});
973 functions.push_back({float3x3_cls, "to_float3x3", {float2x2_cls}});
974 functions.push_back({float4x4_cls, "to_float4x4", {float2x2_cls}});
975 functions.push_back({float4x4_cls, "to_float4x4", {float3x3_cls}});
976 functions.push_back({float3x3_cls, "to_float3x3", {float3x4_cls}});
977
978 functions.push_back({uint_cls, "packUnorm2x16", {float2_cls}});
979 functions.push_back({uint_cls, "packSnorm2x16", {float2_cls}});
980 functions.push_back({uint_cls, "packUnorm4x8", {float4_cls}});
981 functions.push_back({uint_cls, "packSnorm4x8", {float4_cls}});
982 functions.push_back({uint_cls, "packHalf2x16", {float2_cls}});
983
984 functions.push_back({float2_cls, "unpackUnorm2x16", {uint_cls}});
985 functions.push_back({float2_cls, "unpackSnorm2x16", {uint_cls}});
986 functions.push_back({float4_cls, "unpackUnorm4x8", {uint_cls}});
987 functions.push_back({float4_cls, "unpackSnorm4x8", {uint_cls}});
988 functions.push_back({float2_cls, "unpackHalf2x16", {uint_cls}});
989
990 for (size_t i = 1; i < f_types.size(); ++i) {
991 functions.push_back({float_cls, "length", {f_types[i]}});
992 functions.push_back({float_cls, "distance", {f_types[i], f_types[i]}});
993 functions.push_back({float_cls, "dot", {f_types[i], f_types[i]}});
994 functions.push_back({f_types[i], "normalize", {f_types[i]}});
995 functions.push_back({f_types[i], "faceforward", {f_types[i], f_types[i], f_types[i]}});
996 functions.push_back({f_types[i], "reflect", {f_types[i], f_types[i]}});
997 functions.push_back({f_types[i], "refract", {f_types[i], f_types[i], float_cls}});
998 }
999 functions.push_back({float3_cls, "cross", {float3_cls, float3_cls}});
1000
1001 const vector<SymbolClass *> square_mats = {float2x2_cls, float3x3_cls, float4x4_cls};
1002 const vector<SymbolClass *> all_mats = {
1012 };
1013
1014 for (const auto &m : all_mats) {
1015 functions.push_back({m, "transpose", {m}});
1016 }
1017
1018 for (const auto &m : square_mats) {
1019 functions.push_back({float_cls, "determinant", {m}});
1020 functions.push_back({m, "inverse", {m}});
1021 }
1022
1023 for (size_t i = 1; i < b_types.size(); ++i) {
1024 functions.push_back({bool_cls, "any", {b_types[i]}});
1025 functions.push_back({bool_cls, "all", {b_types[i]}});
1026 functions.push_back({b_types[i], "not", {b_types[i]}});
1027 }
1028
1029 const vector<string> bit_unary = {"bitfieldReverse"};
1030 for (const auto &op : bit_unary) {
1031 add_unary(op, i_types);
1032 add_unary(op, u_types);
1033 }
1034
1035 for (int i : {0, 1, 2, 3}) {
1036 SymbolClass *u_type = u_types[i];
1037 SymbolClass *i_type = i_types[i];
1038
1039 for (SymbolClass *input : {u_type, i_type}) {
1040 functions.push_back({i_type, "bitCount", {input}});
1041 functions.push_back({i_type, "findLSB", {input}});
1042 functions.push_back({i_type, "findMSB", {input}});
1043 }
1044 }
1045
1046 for (const auto &tlist : {i_types, u_types}) {
1047 for (const auto &t : tlist) {
1048 functions.push_back({t, "bitfieldExtract", {t, int_cls, int_cls}});
1049 functions.push_back({t, "bitfieldInsert", {t, t, int_cls, int_cls}});
1050 }
1051 }
1052
1053 const vector<string> atomic_2arg = {"atomicAdd",
1054 "atomicMin",
1055 "atomicMax",
1056 "atomicAnd",
1057 "atomicOr",
1058 "atomicXor",
1059 "atomicExchange"};
1060
1061 for (const auto &op : atomic_2arg) {
1062 functions.push_back({int_cls, op, {int_cls, int_cls}});
1063 functions.push_back({uint_cls, op, {uint_cls, uint_cls}});
1064 }
1065 functions.push_back({int_cls, "atomicCompSwap", {int_cls, int_cls, int_cls}});
1066 functions.push_back({uint_cls, "atomicCompSwap", {uint_cls, uint_cls, uint_cls}});
1067
1068 const vector<string> deriv_funcs = {"gpu_dfdx", "gpu_dfdy", "gpu_fwidth"};
1069
1070 for (const auto &op : deriv_funcs) {
1071 add_unary(op, f_types);
1072 }
1073
1074 functions.push_back({void_cls, "gpu_discard_fragment", {}});
1075 functions.push_back({void_cls, "barrier", {}});
1076 functions.push_back({void_cls, "assert", {bool_cls}});
1077
1078 const vector<pair<SymbolClass *, string>> image_types = {
1079 {uint4_cls, "u"},
1080 {int4_cls, "i"},
1081 {float4_cls, ""},
1082 };
1083
1084 for (auto [type, prefix] : image_types) {
1085 SymbolClass *sampler = root->classes[prefix + "samplerBuffer"];
1086 functions.push_back({type, "texelFetch", {sampler, i_types[0]}});
1087 }
1088
1089 for (auto [type, prefix] : image_types) {
1090 vector<tuple<SymbolClass *, int, int>> samplers = {
1091 /* Name, Coordinate Dimensions. */
1092 {root->classes[prefix + "sampler1D"], 1, 1},
1093 {root->classes[prefix + "sampler2D"], 2, 2},
1094 {root->classes[prefix + "sampler3D"], 3, 3},
1095 {root->classes[prefix + "samplerCube"], 3, 3},
1096 {root->classes[prefix + "sampler1DArray"], 2, 1},
1097 {root->classes[prefix + "sampler2DArray"], 3, 2},
1098 {root->classes[prefix + "samplerCubeArray"], 4, 3},
1099 };
1100
1101 if (prefix.empty()) {
1102 samplers.emplace_back(root->classes["sampler2DDepth"], 2, 2);
1103 samplers.emplace_back(root->classes["samplerCubeDepth"], 3, 3);
1104 samplers.emplace_back(root->classes["sampler2DArrayDepth"], 3, 2);
1105 samplers.emplace_back(root->classes["samplerCubeArrayDepth"], 4, 3);
1106 }
1107 else {
1108 samplers.emplace_back(root->classes[prefix + "sampler2DAtomic"], 2, 2);
1109 samplers.emplace_back(root->classes[prefix + "sampler3DAtomic"], 3, 3);
1110 samplers.emplace_back(root->classes[prefix + "sampler2DArrayAtomic"], 3, 2);
1111 }
1112
1113 for (auto [sampler, coord_dim, deriv_dim] : samplers) {
1114 SymbolClass *coord_type = f_types[coord_dim - 1];
1115 SymbolClass *icoord_type = i_types[coord_dim - 1];
1116 SymbolClass *deriv_type = f_types[deriv_dim - 1];
1117
1118 functions.push_back({type, "texture", {sampler, coord_type}});
1119 functions.push_back({type, "texture", {sampler, coord_type, float_cls}}); /* Lod bias. */
1120 functions.push_back({type, "textureLod", {sampler, coord_type, float_cls}});
1121 functions.push_back({type, "textureGather", {sampler, coord_type}});
1122 functions.push_back({type, "textureGather", {sampler, coord_type, int_cls}});
1123 functions.push_back({type, "textureGrad", {sampler, coord_type, deriv_type, deriv_type}});
1124 functions.push_back({icoord_type, "textureSize", {sampler, int_cls}});
1125 functions.push_back({type, "texelFetch", {sampler, icoord_type, int_cls}});
1126 functions.push_back({type, "texelFetchExtend", {sampler, icoord_type, int_cls}});
1127 }
1128
1129 vector<tuple<SymbolClass *, int>> images = {
1130 {root->classes[prefix + "image2D"], 2},
1131 {root->classes[prefix + "image2DArray"], 3},
1132 {root->classes[prefix + "image3D"], 3},
1133 };
1134
1135 if (!prefix.empty()) {
1136 images.emplace_back(root->classes[prefix + "image2DAtomic"], 2);
1137 images.emplace_back(root->classes[prefix + "image3DAtomic"], 3);
1138 images.emplace_back(root->classes[prefix + "image2DArrayAtomic"], 3);
1139 }
1140
1141 for (auto [image, coord_dim] : images) {
1142 SymbolClass *coord_type = i_types[coord_dim - 1];
1143 functions.push_back({type, "imageLoad", {image, coord_type}});
1144 functions.push_back({type, "imageLoadFast", {image, coord_type}});
1145 functions.push_back({void_cls, "imageStore", {image, coord_type, type}});
1146 functions.push_back({void_cls, "imageStoreFast", {image, coord_type, type}});
1147 functions.push_back({coord_type, "imageSize", {image}});
1148 functions.push_back({void_cls, "imageFence", {image}});
1149
1150 if (image->identifier.ends_with("Atomic")) {
1151 SymbolClass *scalar = prefix == "u" ? i_types[0] : u_types[0];
1152 functions.push_back({scalar, "imageAtomicAdd", {image, coord_type, scalar}});
1153 functions.push_back({scalar, "imageAtomicMin", {image, coord_type, scalar}});
1154 functions.push_back({scalar, "imageAtomicMax", {image, coord_type, scalar}});
1155 functions.push_back({scalar, "imageAtomicAnd", {image, coord_type, scalar}});
1156 functions.push_back({scalar, "imageAtomicOr", {image, coord_type, scalar}});
1157 functions.push_back({scalar, "imageAtomicXor", {image, coord_type, scalar}});
1158 functions.push_back({scalar, "imageAtomicExchange", {image, coord_type, scalar}});
1159 functions.push_back({scalar, "imageAtomicCompSwap", {image, coord_type, scalar, scalar}});
1160 }
1161 }
1162 }
1163
1164 /* Ray queries. */
1165 SymbolClass *rayQuery_cls = root->lookup_class("rayQuery");
1166 SymbolClass *accelerationStructure_cls = root->lookup_class("accelerationStructure");
1167 functions.push_back({void_cls,
1168 "rayQueryInitialize",
1169 {rayQuery_cls,
1170 accelerationStructure_cls,
1171 uint_cls,
1172 uint_cls,
1173 float3_cls,
1174 float_cls,
1175 float3_cls,
1176 float_cls}});
1177 functions.push_back({bool_cls, "rayQueryProceed", {rayQuery_cls}});
1178 functions.push_back({void_cls, "rayQueryTerminate", {rayQuery_cls}});
1179 functions.push_back({void_cls, "rayQueryGenerateIntersection", {rayQuery_cls, float_cls}});
1180 functions.push_back({void_cls, "rayQueryConfirmIntersection", {rayQuery_cls}});
1181 functions.push_back({uint_cls, "rayQueryGetIntersectionType", {rayQuery_cls, bool_cls}});
1182 functions.push_back({float_cls, "rayQueryGetRayTMin", {rayQuery_cls}});
1183 functions.push_back({float3_cls, "rayQueryGetWorldRayOrigin", {rayQuery_cls}});
1184 functions.push_back({float3_cls, "rayQueryGetWorldRayDirection", {rayQuery_cls}});
1185 functions.push_back({float_cls, "rayQueryGetIntersectionT", {rayQuery_cls, bool_cls}});
1186 functions.push_back({uint_cls, "rayQueryGetIntersectionInstanceId", {rayQuery_cls, bool_cls}});
1187 functions.push_back(
1188 {uint_cls, "rayQueryGetIntersectionInstanceCustomIndex", {rayQuery_cls, bool_cls}});
1189 functions.push_back(
1190 {uint_cls, "rayQueryGetIntersectionGeometryIndex", {rayQuery_cls, bool_cls}});
1191 functions.push_back(
1192 {uint_cls, "rayQueryGetIntersectionPrimitiveIndex", {rayQuery_cls, bool_cls}});
1193 functions.push_back(
1194 {float2_cls, "rayQueryGetIntersectionBarycentrics", {rayQuery_cls, bool_cls}});
1195 functions.push_back({bool_cls, "rayQueryGetIntersectionFrontFace", {rayQuery_cls, bool_cls}});
1196 functions.push_back({bool_cls, "rayQueryGetIntersectionCandidateAABBOpaque", {rayQuery_cls}});
1197 functions.push_back(
1198 {float3_cls, "rayQueryGetIntersectionObjectRayDirection", {rayQuery_cls, bool_cls}});
1199 functions.push_back(
1200 {float3_cls, "rayQueryGetIntersectionObjectRayOrigin", {rayQuery_cls, bool_cls}});
1201 functions.push_back(
1202 {float4x3_cls, "rayQueryGetIntersectionObjectToWorld", {rayQuery_cls, bool_cls}});
1203 functions.push_back(
1204 {float4x3_cls, "rayQueryGetIntersectionWorldToObject", {rayQuery_cls, bool_cls}});
1205 /* Not mapped to Metal: no query-side ray-flags accessor, and MSL forbids caching the flags on
1206 * the query object. Gated off so a shader using it fails to compile rather than silently reading
1207 * 0. */
1208 // rayQueryGetRayFlagsEXT;
1209 /* Not mapped to Metal: no inline equivalent. Gated off so use is a compile error. */
1210 // rayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetEXT;
1211
1212 return functions;
1213}
1214
1215/* Generates every single legal BSL builtin function dynamically. */
1216vector<SymbolTable::BuiltinFunc> SymbolTable::generate_all_builtin_constexpr_functions()
1217{
1218 /* Start with the error symbol. */
1219 vector<BuiltinFunc> functions;
1220
1221 /* Type definition vectors for vectorized generation */
1222 const vector<SymbolClass *> f_types = {float_cls, float2_cls, float3_cls, float4_cls};
1223 const vector<SymbolClass *> i_types = {int_cls, int2_cls, int3_cls, int4_cls};
1224 const vector<SymbolClass *> u_types = {uint_cls, uint2_cls, uint3_cls, uint4_cls};
1225 const vector<SymbolClass *> b_types = {bool_cls, bool2_cls, bool3_cls, bool4_cls};
1226
1227 const vector<string> rel_ops = {
1228 "lessThan", "lessThanEqual", "greaterThan", "greaterThanEqual", "equal", "notEqual"};
1229
1230 for (const auto &op : rel_ops) {
1231 for (size_t i = 1; i < f_types.size(); ++i) {
1232 functions.push_back({b_types[i], op, {f_types[i], f_types[i]}});
1233 functions.push_back({b_types[i], op, {i_types[i], i_types[i]}});
1234 functions.push_back({b_types[i], op, {u_types[i], u_types[i]}});
1235 }
1236 }
1237
1238 /* Bit-reinterpretation conversions. */
1239 for (size_t i = 0; i < f_types.size(); ++i) {
1240 functions.push_back({i_types[i], "floatBitsToInt", {f_types[i]}});
1241 functions.push_back({u_types[i], "floatBitsToUint", {f_types[i]}});
1242 functions.push_back({f_types[i], "intBitsToFloat", {i_types[i]}});
1243 functions.push_back({f_types[i], "uintBitsToFloat", {u_types[i]}});
1244 }
1245
1246 return functions;
1247}
1248
1249} // namespace bsl
void init()
#define str(s)
#define image(slot, qualifiers, format)
#define sampler(slot)
#define abs
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
SymEdge< T > * sym(const SymEdge< T > *se)
unsigned short uint16_t
Definition BKE_text.h:80
Definition codegen.cc:18
ConstexprValue ConstexprError
@ TemplateParameterNotConstexpr
@ AutoTypeNestedInitializer
@ TemplateInvalidArg
@ NonConstVariableInExpr
@ UnknownIdentifier
@ TemplateMissingArgument
@ AutoTypeMultipleExpressions
@ ExprCannotContainFunctionCalls
@ TemplateMissingParametersForType
@ UnsupportedSymbolInExpr
PipelineCompute comp(comp_main)
@ Typename
Definition types.hh:84
string to_string(const char *str)
Definition string.cpp:202
InitializerList initializer_list() const
Definition ast.hh:791
TemplateParamList template_params() const
Definition ast.hh:503
ChildRange children_range() const
Definition ast.hh:334
std::string_view str() const
Definition ast.cc:113
AstNodeException(ast::Node node, Diag diag, const string &param1="", const string &param2="", const string &param3="")
ConstexprValue(int32_t s)
Definition constexpr.hh:139
builtin::ClassId builtin_class
bool is_builtin() const
unordered_map< string, SymbolScope * > scopes
SymbolVariable * lookup_variable(const SymbolTable &table, ast::IdQualified id) const
bool function_emplace(SymbolFunction *fn, bool builtin=false)
SymbolMap< SymbolVariable > variables
SymbolMap< SymbolClass > classes
Result< SymbolFunction * > lookup_function(const SymbolTable &table, ast::IdQualified id, optional< SourceLocation > loc=nullopt) const
SymbolClass * lookup_class_base(const SymbolTable &table, ast::IdQualified id) const
Result< SymbolClass * > lookup_class(const SymbolTable &table, ast::IdQualified id, optional< SourceLocation > loc=nullopt) const
SymbolClass * half4_cls
SymbolClass * float3x2_cls
SymbolClass * float4_cls
SymbolClass * ushort_cls
SymbolClass * void_cls
SymbolClass * bool3_cls
SymbolClass * short4_cls
SymbolClass * float4x3_cls
SymbolClass * uchar2_cls
SymbolClass * uint4_cls
SymbolClass * uint3_cls
static constexpr const char * err_symbol
SymbolClass * float3x3_cls
SymbolScope * root
SymbolClass * uchar3_cls
SymbolClass * int2_cls
SymbolClass * packed_float2_cls
SymbolClass * half3_cls
SymbolClass * short3_cls
SymbolClass * float2_cls
SymbolClass * float2x2_cls
SymbolClass * uchar4_cls
Result< StringPair > mangle_identifier(ast::TemplateArgList args, ast::TemplateParamList list, const SymbolScope &scope) const
SymbolClass * uint_cls
SymbolTable(ParserBase &builtin_parser)
Allocator< SymbolFunction > fun_arena
SymbolClass * uint2_cls
SymbolClass * bool2_cls
SymbolClass * get_literal_type(string_view lit) const
SymbolClass * float2x3_cls
SymbolClass * float_cls
Result< string > expr_to_string(const SymbolScope &scope, ast::Node start, int &node_count, bool allow_compilation_constant=false) const
SymbolClass * bool_cls
Result< SymbolClass * > resolve_auto_type(SymbolScope &scope, ast::Declarator decl) const
SymbolClass * short_cls
SymbolClass * half2_cls
SymbolClass * float2x4_cls
static constexpr const char * subscript_operator_id
Allocator< SymbolClass > cls_arena
SymbolClass * bool32_t_cls
SymbolClass * err_cls
Allocator< SymbolScope > scp_arena
SymbolClass * int3_cls
SymbolClass * packed_float3_cls
SymbolClass * float3x4_cls
SymbolClass * float4x2_cls
SymbolClass * str_cls
SymbolClass * float3_cls
SymbolClass * short2_cls
Result< ExpressionResult > expr_type_analysis(const SymbolScope &scope, ast::Node start_node, bool is_reference=false) const
SymbolClass * int4_cls
SymbolClass * char2_cls
SymbolVariable * err_var
SymbolClass * uchar_cls
SymbolClass * int_cls
SymbolClass * ushort2_cls
SymbolClass * packed_float4_cls
SymbolClass * char_cls
Allocator< SymbolVariable > var_arena
SymbolClass * bool4_cls
MatchRank get_conversion_rank(const SymbolClass *from, const SymbolClass *to, const bool ctor_conversion=false) const
SymbolClass * char3_cls
SymbolClass * half_cls
SymbolClass * ushort4_cls
SymbolClass * ushort3_cls
SymbolClass * float4x4_cls
SymbolClass * char4_cls
SymbolFunction * err_func
unordered_map< int, string > image_formats
string value_str() const