Blender V5.3
resource.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 "resource.hh"
10
11#include <unordered_map>
12
13namespace bsl {
14
15using namespace blender::gpu::shader::parser::ast;
16
18{
19 struct AttributeDescriptor {
20 ResourceTableType srt_type;
21 ResourceType res_type;
27 /* TODO(fclem): Could be replaced by complete set of possible arguments once we support folding
28 * preprocessor directives. */
29 int args;
30 };
31
32 static unordered_map<string, AttributeDescriptor> attr_map = {
33 /* clang-format off */
34 /* Noop, to remove. */
35 {"host_shared", {ResourceTableType::NONE, ResourceType::NONE, 0}},
36 /* Vertex inputs. */
38 /* Vertex outputs. */
42 /* Fragment outputs. */
44 /* Fragment inputs. */
46 /* Entry point type. */
50 /* Entry point attrib. */
51 {"early_fragment_tests", {ResourceTableType::NONE, ResourceType::NONE, 0}},
52 {"texture_atomic", {ResourceTableType::NONE, ResourceType::NONE, 0}},
53 {"metal_max_total_threads_per_threadgroup", {ResourceTableType::NONE, ResourceType::NONE, 1}},
54 {"local_size", {ResourceTableType::NONE, ResourceType::NONE, -3}},
55 /* Resource table. */
68 /* Entry point argument. */
93 /* Misc. */
94 {"fallthrough", {ResourceTableType::NONE, ResourceType::NONE, 0}},
95 {"maybe_unused", {ResourceTableType::NONE, ResourceType::NONE, 0}},
98 {"static_branch", {ResourceTableType::NONE, ResourceType::NONE, 0}},
103 {"raster_order_group", {ResourceTableType::NONE, ResourceType::RASTER_ORDER_GROUP, 1}},
105 /* clang-format on */
106 };
107
109 for (Attr attr : list.children_of_type<Attr>()) {
110 string attr_str(attr.identifier().str());
111 if (auto it = attr_map.find(attr_str); it != attr_map.end()) {
112 auto [str_type, res_type, expected_param_count] = it->second;
113
114 FuncParamList params = attr.parameters();
115 int param_count = params.child_count();
116 if (expected_param_count >= 0) {
117 if (param_count != expected_param_count) {
118 result.err = AstNodeException(params,
120 to_string(expected_param_count),
121 attr_str);
122 continue;
123 }
124 }
125 else {
126 /* Negative value denote a range of parameter count. */
127 int max_param_count = abs(expected_param_count);
128 if (param_count < 1 || param_count > max_param_count) {
129 result.err = AstNodeException(params,
131 to_string(max_param_count),
132 attr_str);
133 continue;
134 }
135 }
136
137 if (res_type == ResourceType::CONDITION) {
138 result.value.condition = params.child_first();
139 }
140 else if (res_type == ResourceType::CAPACITY) {
141 result.value.capacity = params.child_first();
142 }
143 else if (res_type == ResourceType::FREQUENCY) {
144 result.value.frequency = params.child_first();
145 }
146 else if (res_type == ResourceType::DUAL_SOURCE_INDEX) {
147 result.value.dual_source_index = params.child_first();
148 }
149 else if (res_type == ResourceType::RASTER_ORDER_GROUP) {
150 result.value.raster_order_group = params.child_first();
151 }
152 else if (res_type != ResourceType::NONE) {
153 if (result.value.res_type == ResourceType::NONE) {
154 result.value.res_type = res_type;
155 result.value.srt_type = str_type;
156 result.value.attr = attr;
157
158 Expr param1 = params.child_first();
159 if (param1.is_valid()) {
160 result.value.param1 = param1;
161 }
162 Expr param2 = param1.next();
163 if (param2.is_valid()) {
164 result.value.param2 = param2;
165 }
166 Expr param3 = param2.next();
167 if (param3.is_valid()) {
168 result.value.param3 = param3;
169 }
170 }
171 else {
173 attr, Diag::ResourceTableDeclarationAlreadyOfType, to_str(result.value.res_type));
174 }
175 }
176 }
177 else {
178 result.err = AstNodeException(attr, Diag::UnknownAttribute, attr_str);
179 }
180 }
181 return result;
182}
183
184} // namespace bsl
#define abs
Definition codegen.cc:18
Result< ParsedAttribute > resource_type_from_attributes(AttrList list)
Definition resource.cc:17
@ ExpectedRangeParametersForAttribute
@ ResourceTableDeclarationAlreadyOfType
@ ExpectedNParametersForAttribute
ResourceTableType
Definition resource.hh:131
ResourceType
Definition resource.hh:20
string to_string(const char *str)
Definition string.cpp:202
AstNodeException(ast::Node node, Diag diag, const string &param1="", const string &param2="", const string &param3="")