Blender V5.3
symbol_class.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_class.hh"
10
11namespace bsl {
12
13using namespace blender::gpu::shader::parser::ast;
14
17 decl.front(),
18 !decl.identifier().is_valid() ?
19 string("a") + std::to_string(parent->classes.size()) :
20 string(decl.identifier().name().str()) + suffix,
21 CLASS)
22
23{
24 this->is_anonymous = !decl.identifier().is_valid();
25 this->is_union = decl.front() == Union;
26 this->is_enum = decl.front() == Enum;
27}
28
30{
31 vector<MemberNode> flat_buffer;
32 auto err = get_flat_members_recursive(this, "", 0, flat_buffer);
33 return {flat_buffer, err};
34}
35
36/* Recursive helper to accumulate path and offsets */
37optional<TokenException> SymbolClass::get_flat_members_recursive(const SymbolClass *cls,
38 const string &current_path,
39 int current_offset,
40 vector<MemberNode> &out_buffer)
41{
42 /* Iterate through variables in their declaration order */
44 if (var->bit_offset != 0) {
45 continue;
46 }
47 /* Construct the full access path. */
48 /* If the identifier is empty (e.g., an anonymous union/struct), we skip adding the dot. */
49 std::string next_path = current_path;
50 if (!var->identifier.empty()) {
51 if (!next_path.empty()) {
52 next_path += ".";
53 }
54 next_path += var->original;
55 }
56 /* Accumulate the total offset from the root struct. */
57 int offset = current_offset + var->offset;
58
59 if (var->array_dimensions > 1) {
60 /* TODO */
62 }
63 int elements = (var->array_dimensions == 0) ? 1 : var->array_elements;
64 if (elements == -1) {
66 }
67
68 for (int i = 0; i < elements; ++i) {
69 string array_accessor = var->array_dimensions > 0 ? "[" + to_string(i) + "]" : "";
70 if (var->type->is_builtin()) {
71 /* Base case: It's a primitive/builtin type. Add to the flat buffer. */
72 out_buffer.emplace_back(var->type, next_path + array_accessor, offset);
73 offset += var->type->size;
74 }
75 else {
76 /* Recursive step: It's a nested user-defined structure. */
77 auto err = get_flat_members_recursive(
78 var->type, next_path + array_accessor, offset, out_buffer);
79 offset += var->type->size;
80 if (err) {
81 return err;
82 }
83 }
84 }
85 }
86 return {};
87}
88
89void SymbolClass::ensure_size_and_align(NodeErrorHandler *err_handler, bool check_st430)
90{
91 if (size != -1 && (err_handler == nullptr || is_builtin())) {
92 return;
93 }
94 size = 0;
95 align = 1;
99 if (var->bit_offset != 0) {
100 /* Aliased to the first member of the bitfield, which is the only one taking storage. */
101 continue;
102 }
103
104 SymbolClass &cls = *var->type;
105
106 if (err_handler && !cls.is_enum) {
107 auto scope = err_handler->note(
108 var->loc.tok, Diag::NoteInDeclarationOfVariable, var->identifier, cls.identifier);
109 cls.ensure_size_and_align(err_handler, check_st430);
110 }
111 else {
113 }
114
115 int member_size = cls.size;
116 if (var->array_elements == 0) {
117 /* Not an array. */
118 }
119 else if (var->array_elements == -1) {
120 /* Array size need to be known at compile time for shared structs. */
121 is_std140_compatible = false;
122 is_std430_compatible = false;
123 if (err_handler) {
124 err_handler->error(var->loc.tok, Diag::ArraySizeNotConstantExpression);
125 }
126 }
127 else {
128 if (cls.size == 12) {
129 /* Arrays of float3 padded to 16 bytes for each element for std430. */
130 is_std140_compatible = false;
131 if (err_handler) {
132 err_handler->error(var->loc.tok, Diag::ImplicitPadding12BytesArrayElement);
133 }
134 }
135 else if (cls.size % 16 != 0) {
136 /* Arrays are padded to 16 bytes for each element for std140. */
137 is_std140_compatible = false;
138 if (err_handler && !check_st430) {
139 err_handler->error(
140 var->loc.tok, Diag::ImplicitPaddingArrayElementStd140, 16 - (cls.size % 16));
141 }
142 }
143 member_size *= var->array_elements;
144 }
145
146 /* Inherit base compatibility from the member. */
149
150 if (err_handler) {
151 string hint;
152 if (cls.is_enum) {
153 hint = " because its underlying type is not 'uint' or 'int'";
154 }
155 else if (cls.identifier == "float3") {
156 hint = "; use 'packed_float3' instead";
157 }
158 else if (cls.identifier == "int3") {
159 hint = "; use 'packed_int3' instead";
160 }
161 else if (cls.identifier == "uint3") {
162 hint = "; use 'packed_uint3' instead";
163 }
164 else if (cls.identifier == "bool") {
165 hint = "; use 'bool32_t' instead";
166 }
167 else if (cls.identifier == "float3x3") {
168 hint = "; use 'float3x4' instead";
169 }
170
171 if (!is_std140_compatible && !check_st430) {
172 err_handler->error(var->loc.tok,
174 var->type->original,
175 hint);
176 }
177 else if (!is_std430_compatible) {
178 err_handler->error(
179 var->loc.tok, Diag::TypeNotAllowedInStorageBuffer, var->type->original, hint);
180 }
181 }
182
183 if (this->is_union) {
184 size = max(size, member_size);
185 }
186 else {
187 /* Padding to next member alignment.
188 * Force struct and arrays to all have alignment of float4. */
189 int align = (cls.is_builtin() || cls.is_enum) && var->array_dimensions == 0 ? cls.align : 16;
190 int padded = pad(size, align);
191 /* Consider class compatible if it has no implicit padding. */
192 is_std140_compatible &= padded == size;
193 is_std430_compatible &= padded == size;
194 if (err_handler && padded != size) {
195 err_handler->error(
196 var->loc.tok, Diag::ImplicitPaddingBeforeMember, padded - size, var->original);
197 }
198 size = padded;
199 /* Add member size. */
200 size += member_size;
201 }
202 /* Update the overall alignment of the current struct. */
203 align = max(align, cls.align);
204
205 if (!cls.is_builtin() && !cls.is_enum && cls.size % 16 != 0) {
206 is_std140_compatible = false;
207 if (err_handler && !check_st430) {
208 err_handler->error(
209 var->loc.tok, Diag::TypeNotAllowedInUniformBufferImplicitPadding, var->type->original);
210 }
211 }
212 }
213
214 /* The total size of the struct must be a multiple of its alignment. */
215 if (align > 0) {
216
217 int padded = pad(size, align);
218 if (padded != size) {
219 is_std140_compatible = false;
220 is_std430_compatible = false;
221 if (err_handler) {
222 err_handler->error(
223 loc.tok, Diag::ImplicitPaddingAtEndOfStruct, padded - size, this->original);
224 }
225 }
226 size = padded;
227 }
228
229#if 0 /* For debugging. */
230 std::cout << "size:" << size << " align:" << align << " id:" << identifier << std::endl;
231#endif
232
233 if (size == 0) {
234 is_std140_compatible = false;
235 is_std430_compatible = false;
236
237 if (err_handler) {
239 }
240 }
241 /* The overall size and alignment of a struct in std140 must be rounded up to the base alignment
242 * of a vec4 (16 bytes). */
243 if (size % 16 != 0) {
244 is_std140_compatible = false;
245
246 if (err_handler && !check_st430) {
247 err_handler->error(
248 loc.tok, Diag::ImplicitPaddingAtEndOfStruct, 16 - (size % 16), this->original);
249 }
250 }
251}
252
253} // namespace bsl
int pad[32 - sizeof(int)]
btScalar max
#define str(s)
Definition codegen.cc:18
@ ArrayMultidimensionalNotSupported
@ TypeNotAllowedInStorageBuffer
@ ImplicitPaddingArrayElementStd140
@ EmptyClassNotSupportedInBuffer
@ ImplicitPaddingAtEndOfStruct
@ TypeNotAllowedInUniformBufferImplicitPadding
@ ImplicitPaddingBeforeMember
@ ImplicitPadding12BytesArrayElement
@ TypeNotAllowedInUniformAndStorageBuffer
@ ArraySizeNotConstantExpression
@ NoteInDeclarationOfVariable
@ Enum
Definition types.hh:106
@ Union
Definition types.hh:109
string to_string(const char *str)
Definition string.cpp:202
ScopedNote note(Token tok, Diag code, Args &&...args)
void error(Token tok, const string &msg)
bool is_builtin() const
SymbolClass(SymbolScope *parent, ast::ClassDecl decl, const string &suffix="")
void ensure_size_and_align(NodeErrorHandler *err_handler=nullptr, bool check_st430=false)
Result< vector< MemberNode >, TokenException > get_all_members_flat() const
vector< SymbolVariable * > non_static_variables_in_declaration_order() const
enum bsl::SymbolScope::Type type
SymbolScope(ast::LocalScope decl)
SymbolMap< SymbolClass > classes
SymbolScope * parent
SourceLocation loc
TokenException(Token tok, Diag diag, const string &param1="", const string &param2="", const string &param3="")