Blender V5.3
constructor.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
11namespace bsl {
12using namespace std;
13using namespace blender::gpu::shader::parser;
14using namespace blender::gpu::shader::parser::ast;
15
16/* Generates every single legal BSL constructor dynamically. */
17vector<SymbolTable::BuiltinFunc> SymbolTable::generate_all_constructors(
18 const vector<BuiltinType> &types)
19{
20 vector<BuiltinFunc> funcs;
21
22 funcs.push_back({this->str_cls, this->str_cls->identifier, {this->uint_cls}});
23
24 /* Vector and Scalar Constructors (restricted to matching component types). */
25 for (const auto [type, base] : types) {
26 SymbolClass *s = base;
27 SymbolClass *v2 = make_type(base, 2);
28 SymbolClass *v3 = make_type(base, 3);
29 SymbolClass *v4 = make_type(base, 4);
30
31 switch (type->comp_len) {
32 case 1:
33 /* Scalar Copy: float(float) */
34 funcs.push_back({s, s->identifier, {s}});
35 break;
36 case 2:
37 /* Copy: float2(float2) */
38 funcs.push_back({v2, v2->identifier, {v2}});
39 /* Broadcast: float2(float) */
40 funcs.push_back({v2, v2->identifier, {s}});
41 /* Component-wise (1+1): float2(float, float) */
42 funcs.push_back({v2, v2->identifier, {s, s}});
43 break;
44 case 3:
45 /* Copy: float3(float3) */
46 funcs.push_back({v3, v3->identifier, {v3}});
47 /* Broadcast: float3(float) */
48 funcs.push_back({v3, v3->identifier, {s}});
49 /* 1 + 1 + 1: float3(float, float, float) */
50 funcs.push_back({v3, v3->identifier, {s, s, s}});
51 /* 1 + 2: float3(float, float2) */
52 funcs.push_back({v3, v3->identifier, {s, v2}});
53 /* 2 + 1: float3(float2, float) */
54 funcs.push_back({v3, v3->identifier, {v2, s}});
55 break;
56 case 4:
57 /* Copy: float4(float4) */
58 funcs.push_back({v4, v4->identifier, {v4}});
59 /* Broadcast: float4(float) */
60 funcs.push_back({v4, v4->identifier, {s}});
61 /* 1 + 1 + 1 + 1: float4(float, float, float, float) */
62 funcs.push_back({v4, v4->identifier, {s, s, s, s}});
63 /* 1 + 3: float4(float, float3) */
64 funcs.push_back({v4, v4->identifier, {s, v3}});
65 /* 3 + 1: float4(float3, float) */
66 funcs.push_back({v4, v4->identifier, {v3, s}});
67 /* 2 + 2: float4(float2, float2) */
68 funcs.push_back({v4, v4->identifier, {v2, v2}});
69 /* 2 + 1 + 1: float4(float2, float, float) */
70 funcs.push_back({v4, v4->identifier, {v2, s, s}});
71 /* 1 + 2 + 1: float4(float, float2, float) */
72 funcs.push_back({v4, v4->identifier, {s, v2, s}});
73 /* 1 + 1 + 2: float4(float, float, float2) */
74 funcs.push_back({v4, v4->identifier, {s, s, v2}});
75 break;
76 }
77 }
78
79 /* Matrix Constructors (restricted to matching "float" component type). */
80 for (int n = 2; n <= 4; ++n) {
81 for (int m = 2; m <= 4; ++m) {
82 SymbolClass *row = make_type(float_cls, m);
83 SymbolClass *mat = make_type(row, n);
84 /* Broadcast: float3x3(float) */
85 funcs.push_back({mat, mat->identifier, {float_cls}});
86 /* All-Scalar: float3x3(float, float, ...) [N*N scalars] */
87 funcs.push_back({mat, mat->identifier, vector<SymbolClass *>(n * m, float_cls)});
88 /* All-Vector: float3x3(float3, float3, float3) [N vectors] */
89 funcs.push_back({mat, mat->identifier, vector<SymbolClass *>(n, row)});
90 }
91 }
92
93 return funcs;
94}
95
96} // namespace bsl
ATTR_WARN_UNUSED_RESULT const BMVert * v2
Definition codegen.cc:18
SymbolClass * uint_cls
SymbolClass * float_cls
SymbolClass * make_type(SymbolClass *base, int size) const
Definition operator.cc:336
SymbolClass * str_cls