Blender V5.3
metadata.hh
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#pragma once
10
11#include <cctype>
12#include <cstdint>
13#include <sstream>
14#include <string>
15#include <vector>
16
17/* Metadata extracted from shader source file.
18 * These are then converted to their GPU module equivalent. */
19/* TODO(fclem): Make GPU enums standalone and directly use them instead of using separate enums
20 * and types. */
22
23/* Compile-time hashing function which converts string to a 64bit hash. */
24constexpr static uint64_t hash(const char *name)
25{
26 uint64_t hash = 2166136261u;
27 while (*name) {
28 hash = hash * 16777619u;
29 hash = hash ^ *name;
30 ++name;
31 }
32 return hash;
33}
34
35static inline uint64_t hash(const std::string &name)
36{
37 return hash(name.c_str());
38}
39
41 ClipDistance = hash("gl_ClipDistance"),
42 FragCoord = hash("gl_FragCoord"),
43 FragStencilRef = hash("gl_FragStencilRefARB"),
44 FrontFacing = hash("gl_FrontFacing"),
45 GlobalInvocationID = hash("gl_GlobalInvocationID"),
46 InstanceIndex = hash("gpu_InstanceIndex"),
47 BaseInstance = hash("gpu_BaseInstance"),
48 InstanceID = hash("gl_InstanceID"),
49 LocalInvocationID = hash("gl_LocalInvocationID"),
50 LocalInvocationIndex = hash("gl_LocalInvocationIndex"),
51 NumWorkGroup = hash("gl_NumWorkGroup"),
52 PointCoord = hash("gl_PointCoord"),
53 PointSize = hash("gl_PointSize"),
54 PrimitiveID = hash("gl_PrimitiveID"),
55 VertexID = hash("gl_VertexID"),
56 WorkGroupID = hash("gl_WorkGroupID"),
57 WorkGroupSize = hash("gl_WorkGroupSize"),
58 drw_debug = hash("drw_debug_"),
59 printf = hash("printf"),
60 assert = hash("assert"),
61 runtime_generated = hash("runtime_generated"),
62};
63
65 in = hash("in"),
66 out = hash("out"),
67 inout = hash("inout"),
68};
69
70enum Type : uint64_t {
71 float1 = hash("float"),
72 float2 = hash("float2"),
73 float3 = hash("float3"),
74 float4 = hash("float4"),
75 float3x3 = hash("float3x3"),
76 float4x4 = hash("float4x4"),
77 int1 = hash("int"),
78 int2 = hash("int2"),
79 int3 = hash("int3"),
80 int4 = hash("int4"),
81 bool1 = hash("bool"),
82 sampler1DArray = hash("sampler1DArray"),
83 sampler2DArray = hash("sampler2DArray"),
84 sampler2D = hash("sampler2D"),
85 sampler3D = hash("sampler3D"),
86 Closure = hash("Closure"),
87 KernelGlobals = hash("KernelGlobals"),
88 ShadingData = hash("ShadingData"),
89};
90
95
97 std::string name;
98 std::vector<ArgumentFormat> arguments;
99};
100
103 std::string format;
104};
105
107 std::string type;
108 std::string name;
109};
110
113 size_t line;
114
115 std::string var_type;
116 std::string var_name;
117 std::string var_array;
118
119 std::string res_type;
121 std::string res_slot;
123 std::string res_qualifier;
125 std::string res_value;
127 std::string res_format;
129 std::string res_condition;
131 std::string res_frequency = "PASS";
132
133 std::string serialize() const;
134};
135
136struct ResourceTable : std::vector<ParsedResource> {
137 std::string name;
138};
139
141 /* Line this resource was defined. */
142 size_t line;
143
144 std::string var_type;
145 std::string var_name;
146
148
149 std::string serialize() const;
150};
151
152struct StageInterface : std::vector<ParsedAttribute> {
153 std::string name;
154
155 std::string serialize() const;
156};
157
159 /* Line this resource was defined. */
160 size_t line;
161
162 std::string var_type;
163 std::string var_name;
164
165 std::string slot;
166 std::string dual_source;
168
169 std::string serialize() const;
170};
171
172struct FragmentOutputs : std::vector<ParsedFragOuput> {
173 std::string name;
174
175 std::string serialize() const;
176};
177
179 /* Line this resource was defined. */
180 size_t line;
181
182 std::string var_type;
183 std::string var_name;
184
185 std::string slot;
186 std::string image_type;
188
189 std::string serialize() const;
190};
191
192struct FragmentInputs : std::vector<ParsedFragInput> {
193 std::string name;
194
195 std::string serialize() const;
196};
197
199 /* Line this resource was defined. */
200 size_t line;
201
202 std::string var_type;
203 std::string var_name;
204
205 std::string slot;
206
208 std::string res_condition;
209
210 std::string serialize() const;
211};
212
213struct VertexInputs : std::vector<ParsedVertInput> {
214 std::string name;
215
216 std::string serialize() const;
217};
218
220 std::string identifier;
221 std::string name_space;
222 std::string definition;
223 std::string filepath;
228
229 bool operator==(const TemplateDefinition &other) const
230 {
231 return std::tie(identifier, name_space) == std::tie(other.identifier, other.name_space);
232 }
233};
234
235struct Symbol {
236 std::string identifier;
237 std::string name_space;
242 /* For structures only. */
243 std::vector<std::pair<std::string, std::string>> members;
244
245 bool operator<(const Symbol &other) const
246 {
247 if (is_static != other.is_static) {
248 return is_static > other.is_static;
249 }
250 if (is_method != other.is_method) {
251 /* Methods are supposed to have more precedence.
252 * So make them smaller than anything else. */
253 return is_method > other.is_method;
254 }
255 if (name_space != other.name_space) {
256 return name_space > other.name_space;
257 }
258 if (definition_line != other.definition_line) {
259 return definition_line < other.definition_line;
260 }
261 if (identifier != other.identifier) {
262 return identifier < other.identifier;
263 }
264 if (is_struct != other.is_struct) {
265 return is_struct < other.is_struct;
266 }
267 return false;
268 }
269};
270
271struct Source {
272 std::vector<Builtin> builtins;
273 /* Note: Could be a set, but for now the order matters. */
274 std::vector<std::string> dependencies;
275 std::vector<SharedVariable> shared_variables;
276 std::vector<PrintfFormat> printf_formats;
277 std::vector<FunctionFormat> functions;
278 std::vector<std::string> create_infos;
279 std::vector<std::string> create_infos_declarations;
280 std::vector<std::string> create_infos_dependencies;
281 std::vector<std::string> create_infos_defines;
282 std::vector<ResourceTable> resource_tables;
283 std::vector<StageInterface> stage_interfaces;
284 std::vector<FragmentOutputs> fragment_outputs;
285 std::vector<FragmentInputs> fragment_inputs;
286 std::vector<VertexInputs> vertex_inputs;
287 std::vector<Symbol> symbol_table;
288 std::vector<TemplateDefinition> template_definitions;
289 std::vector<std::string> visited_files;
290
291 /* Serialize Metadata for this source file. */
292 std::string serialize(const std::string &function_name) const;
293 /* Serialize Create Infos for this source file. */
294 std::string serialize_infos() const;
295};
296
297} // namespace blender::gpu::shader::metadata
static constexpr uint64_t hash(const char *name)
Definition metadata.hh:24
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint32_t
Definition BKE_text.h:81
std::vector< ArgumentFormat > arguments
Definition metadata.hh:98
std::vector< std::string > create_infos_defines
Definition metadata.hh:281
std::vector< ResourceTable > resource_tables
Definition metadata.hh:282
std::vector< FragmentOutputs > fragment_outputs
Definition metadata.hh:284
std::vector< StageInterface > stage_interfaces
Definition metadata.hh:283
std::string serialize(const std::string &function_name) const
std::vector< TemplateDefinition > template_definitions
Definition metadata.hh:288
std::vector< std::string > visited_files
Definition metadata.hh:289
std::vector< std::string > create_infos
Definition metadata.hh:278
std::vector< FragmentInputs > fragment_inputs
Definition metadata.hh:285
std::vector< Symbol > symbol_table
Definition metadata.hh:287
std::vector< FunctionFormat > functions
Definition metadata.hh:277
std::vector< std::string > dependencies
Definition metadata.hh:274
std::vector< PrintfFormat > printf_formats
Definition metadata.hh:276
std::vector< VertexInputs > vertex_inputs
Definition metadata.hh:286
std::vector< std::string > create_infos_declarations
Definition metadata.hh:279
std::vector< SharedVariable > shared_variables
Definition metadata.hh:275
std::vector< Builtin > builtins
Definition metadata.hh:272
std::vector< std::string > create_infos_dependencies
Definition metadata.hh:280
bool operator<(const Symbol &other) const
Definition metadata.hh:245
std::vector< std::pair< std::string, std::string > > members
Definition metadata.hh:243
bool operator==(const TemplateDefinition &other) const
Definition metadata.hh:229