Blender V5.3
gpu/shader_tool/string.cc
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#include "intermediate.hh"
10#include "metadata.hh"
11#include "processor.hh"
12
13namespace blender::gpu::shader {
14using namespace std;
15using namespace shader::parser;
16using namespace shader::parser::ast;
17using namespace metadata;
18
19void SourceProcessor::lower_strings_sequences(Parser &parser)
20{
21 do {
22 parser().foreach_match("\"\"", [&](const vector<Token> &tokens) {
23 string first(tokens[0].str());
24 string second(tokens[1].str());
25 string between = parser.substr_range_inclusive(tokens[0].str_index_last_no_whitespace() + 1,
26 tokens[1].str_index_start() - 1);
27 string trailing = parser.substr_range_inclusive(tokens[1].str_index_last_no_whitespace() + 1,
28 tokens[1].str_index_last());
29 string merged = first.substr(0, first.length() - 1) + second.substr(1) + between + trailing;
30 parser.replace_try(tokens[0], tokens[1], merged);
31 });
32 } while (parser.apply_mutations());
33}
34
35/* Turn assert into a printf. */
36void SourceProcessor::lower_assert(Parser &parser, [[maybe_unused]] const string &filename)
37{
38 /* Example: `assert(i < 0)` > `if (!(i < 0)) { printf(...); }` */
39 parser().foreach_match("A(..)", [&](const vector<Token> &tokens) {
40 if (tokens[0].str() != "assert") {
41 return;
42 }
43 string replacement;
44#ifdef WITH_GPU_SHADER_ASSERT
45 string condition = string(tokens[1].scope().str());
46
47 auto escape = [](string s) {
48 string result;
49 for (char c : s) {
50 if (c == '%') {
51 result += "%%";
52 }
53 else if (c == '\\') {
54 result += "\\\\";
55 }
56 else if (c == '\"') {
57 result += "\\\"";
58 }
59 else {
60 result += c;
61 }
62 }
63 return result;
64 };
65
66 replacement += "if (!" + condition + ") ";
67 replacement += "{";
68 replacement += " printf(\"";
69 replacement += "Assertion failed: " + escape(condition) + ", ";
70 replacement += "file " + filename + ", ";
71 replacement += "line " + to_string(tokens[1].line_number()) + ", ";
72 replacement += "thread (%u,%u,%u).\\n";
73 replacement += "\"";
74 replacement += ", GPU_THREAD.x, GPU_THREAD.y, GPU_THREAD.z); ";
75 replacement += "}";
76#endif
77 parser.replace(tokens[0], tokens[4], replacement);
78 });
79 parser.apply_mutations();
80}
81
82/* Turn assert into a printf. */
83void SourceProcessor::lower_assert_ast(Parser &parser, [[maybe_unused]] const string &filename)
84{
85 /* Example: `assert(i < 0)` > `if (!(i < 0)) { printf(...); }` */
86 for (FuncCall call : parser.root().descendants_of_type<FuncCall>()) {
87 if (call.identifier().str() != "assert") {
88 continue;
89 }
90
91 string replacement;
92#ifdef WITH_GPU_SHADER_ASSERT
93 string condition = string(call.parameters().str());
94
95 auto escape = [](string s) {
96 string result;
97 for (char c : s) {
98 if (c == '%') {
99 result += "%%";
100 }
101 else if (c == '\\') {
102 result += "\\\\";
103 }
104 else if (c == '\"') {
105 result += "\\\"";
106 }
107 else {
108 result += c;
109 }
110 }
111 return result;
112 };
113
114 replacement += "if (!" + condition + ") ";
115 replacement += "{";
116 replacement += " printf(\"";
117 replacement += "Assertion failed: " + escape(condition) + ", ";
118 replacement += "file " + filename + ", ";
119 replacement += "line " + to_string(call.parameters().front().line_number()) + ", ";
120 replacement += "thread (%u,%u,%u).\\n";
121 replacement += "\"";
122 replacement += ", GPU_THREAD.x, GPU_THREAD.y, GPU_THREAD.z); ";
123 replacement += "}";
124#endif
125 parser.replace(call, replacement);
126 }
127
128 parser.apply_mutations();
129}
130
131/* Replace string literals by their hash and store the original string in the file metadata. */
132void SourceProcessor::lower_strings(Parser &parser)
133{
134 parser().foreach_token(String, [&](const Token &token) {
135 if (token.scope().type() == ScopeType::Preprocessor) {
136 return;
137 }
138 uint32_t hash = hash_string(string(token.str()));
139 metadata::PrintfFormat format = {hash, string(token.str())};
140 metadata_.printf_formats.emplace_back(format);
141 parser.replace(token, "string_t(" + to_string(hash) + "u)", true);
142 });
143 parser.apply_mutations();
144}
145
146/* Change printf calls to "recursive" call to implementation functions.
147 * This allows to emulate the variadic arguments of printf. */
148void SourceProcessor::lower_printf(Parser &parser)
149{
150 bool has_printf = false;
151 parser().foreach_match("A(..)", [&](const vector<Token> &tokens) {
152 if (tokens[0].str() != "printf") {
153 return;
154 }
155 has_printf = true;
156
157 int arg_count = 0;
158 tokens[1].scope().foreach_scope(ScopeType::FunctionParam, [&](const Scope &) { arg_count++; });
159
160 string unrolled = "print_start(" + to_string(arg_count) + "u)";
161 tokens[1].scope().foreach_scope(ScopeType::FunctionParam, [&](const Scope &attribute) {
162 unrolled = "print_data(" + unrolled + ", " + string(attribute.str()) + ")";
163 });
164
165 parser.replace(tokens.front(), tokens.back(), unrolled);
166 });
167 parser.apply_mutations();
168
169 if (has_printf) {
170 metadata_.builtins.emplace_back(Builtin::printf);
171 }
172}
173
174} // namespace blender::gpu::shader
static uint32_t hash_string(const std::string &str)
Definition processor.hh:143
#define str(s)
#define attribute(slot)
#define condition(cond)
size_t line_number(const std::string_view &str, size_t pos)
std::string filename(const std::string_view &str, size_t pos)
const char * to_string(ShaderStage stage)
Definition mtl_shader.mm:52
unsigned int uint32_t
Definition BKE_text.h:81
std::string format(const std::string &fmt, Args &&...args)
Definition diagnostic.hh:43
@ String
Definition types.hh:154
#define hash
Definition noise_c.cc:156