Blender V5.3
gpu_shader_log.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "BLI_dynstr.hh"
12#include "BLI_vector.hh"
13
14#include "GPU_storage_buffer.hh"
15
18#include "gpu_shader_private.hh"
19
20#include "CLG_log.h"
21
22#include <fmt/format.h>
23#include <fmt/ranges.h>
24
25namespace blender {
26
27static CLG_LogRef LOG = {"gpu.shader"};
28
29namespace gpu {
30
31/* -------------------------------------------------------------------- */
34
35/* Number of lines before and after the error line to print for compilation errors. */
36#define DEBUG_CONTEXT_LINES 0
41#define DEBUG_DEPENDENCIES 0
42
44 const char *log,
45 const char *stage,
46 const bool error,
48{
49 const char line_prefix[] = " | ";
50 char err_col[] = "\033[31;1m";
51 char warn_col[] = "\033[33;1m";
52 char info_col[] = "\033[0;2m";
53 char reset_col[] = "\033[0;0m";
54 std::string sources_combined = fmt::to_string(fmt::join(sources, ""));
55 DynStr *dynstr = BLI_dynstr_new();
56
58 err_col[0] = warn_col[0] = info_col[0] = reset_col[0] = '\0';
59 }
60
61 BLI_dynstr_appendf(dynstr, "\n");
62
63#if DEBUG_DEPENDENCIES
65 dynstr, "%s%sIncluded files (in order):%s\n", info_col, line_prefix, reset_col);
66#endif
67
68 Vector<int64_t> sources_end_line;
69 for (StringRef src : sources) {
70 int64_t cursor = 0, line_count = 0;
71 while ((cursor = src.find('\n', cursor) + 1)) {
72 line_count++;
73 }
74 if (sources_end_line.is_empty() == false) {
75 line_count += sources_end_line.last();
76 }
77 sources_end_line.append(line_count);
78#if DEBUG_DEPENDENCIES
80 if (!filename.is_empty()) {
82 dynstr, "%s%s %s%s\n", info_col, line_prefix, filename.c_str(), reset_col);
83 }
84#endif
85 }
86 if (sources_end_line.is_empty()) {
87 sources_end_line.append(0);
88 }
89
90 const char *log_line = log, *line_end;
91
92 LogCursor previous_location;
93
94 bool found_line_id = false;
95 while ((line_end = strchr(log_line, '\n'))) {
96 /* Skip empty lines. */
97 if (line_end == log_line) {
98 log_line++;
99 continue;
100 }
101
102 /* Silence not useful lines. */
103 StringRef logref = StringRefNull(log_line).substr(0, size_t(line_end) - size_t(log_line));
104 if (logref.endswith(" shader failed to compile with the following errors:") ||
105 logref.endswith(" No code generated"))
106 {
107 log_line += size_t(line_end) - size_t(log_line);
108 continue;
109 }
110
111 GPULogItem log_item;
112 log_line = parser->parse_line(sources_combined.c_str(), log_line, log_item);
113
114 /* Empty line, skip. */
115 if ((log_item.cursor.row == -1) && ELEM(log_line[0], '\n', '\0')) {
116 continue;
117 }
118
119 /* Sanitize output. Really bad values can happen when the error line is buggy. */
120 if (log_item.cursor.source >= sources.size()) {
121 log_item.cursor.source = -1;
122 }
123 if (log_item.cursor.row >= sources_end_line.last()) {
124 log_item.cursor.source = -1;
125 log_item.cursor.row = -1;
126 }
127
128 if (log_item.cursor.row == -1) {
129 found_line_id = false;
130 }
131
132 const char *src_line = sources_combined.c_str();
133
134 std::string log_line_str(log_line, (line_end + 1) - log_line);
135 if (log_line_str.ends_with(" used uninitialized\n")) {
136 /* Mesa GLSL compiler warns about uninitialized variables are mostly false positive.
137 * Avoid noise. */
138 log_line = line_end + 1;
139 continue;
140 }
141
142 /* Separate from previous block. */
143 if (previous_location.source != log_item.cursor.source ||
144 previous_location.row != log_item.cursor.row)
145 {
146 BLI_dynstr_appendf(dynstr, "%s%s%s\n", info_col, line_prefix, reset_col);
147 }
148 else if (log_item.cursor.column != previous_location.column) {
149 BLI_dynstr_appendf(dynstr, "%s\n", line_prefix);
150 }
151 /* Print line from the source file that is producing the error. */
152 if ((log_item.cursor.row != -1) && (log_item.cursor.row != previous_location.row ||
153 log_item.cursor.column != previous_location.column))
154 {
155 const char *src_line_end;
156 found_line_id = false;
157 /* Lines are 1 based. */
158 int src_line_index = 1;
159 while ((src_line_end = strchr(src_line, '\n'))) {
160 if (src_line_index >= log_item.cursor.row) {
161 found_line_id = true;
162 break;
163 }
164 if (src_line_index >= log_item.cursor.row - DEBUG_CONTEXT_LINES) {
165 BLI_dynstr_appendf(dynstr, "%5d | ", src_line_index);
166 BLI_dynstr_nappend(dynstr, src_line, (src_line_end + 1) - src_line);
167 }
168 /* Continue to next line. */
169 src_line = src_line_end + 1;
170 src_line_index++;
171 }
172 /* Print error source. */
173 if (found_line_id) {
174 if (log_item.cursor.row != previous_location.row) {
175 BLI_dynstr_appendf(dynstr, "%5d | ", src_line_index);
176 }
177 else {
178 BLI_dynstr_appendf(dynstr, line_prefix);
179 }
180 BLI_dynstr_nappend(dynstr, src_line, (src_line_end + 1) - src_line);
181 /* Print char offset. */
182 BLI_dynstr_appendf(dynstr, line_prefix);
183 if (log_item.cursor.column != -1) {
184 for (int i = 0; i < log_item.cursor.column; i++) {
185 BLI_dynstr_appendf(dynstr, " ");
186 }
187 BLI_dynstr_appendf(dynstr, "^");
188 }
189 BLI_dynstr_appendf(dynstr, "\n");
190
191 /* Skip the error line. */
192 src_line = src_line_end + 1;
193 src_line_index++;
194 while ((src_line_end = strchr(src_line, '\n'))) {
195 if (src_line_index > log_item.cursor.row + DEBUG_CONTEXT_LINES) {
196 break;
197 }
198 BLI_dynstr_appendf(dynstr, "%5d | ", src_line_index);
199 BLI_dynstr_nappend(dynstr, src_line, (src_line_end + 1) - src_line);
200 /* Continue to next line. */
201 src_line = src_line_end + 1;
202 src_line_index++;
203 }
204 }
205 }
206 BLI_dynstr_appendf(dynstr, line_prefix);
207
208 /* Search the correct source index. */
209 int row_in_file = log_item.cursor.row;
210 int source_index = log_item.cursor.source;
211 if (source_index <= 0) {
212 for (auto i : sources_end_line.index_range()) {
213 if (log_item.cursor.row <= sources_end_line[i]) {
214 source_index = i;
215 break;
216 }
217 }
218 }
219 if (source_index > 0) {
220 row_in_file -= sources_end_line[source_index - 1];
221 }
222 /* Print the filename the error line is coming from. */
223 if (!log_item.cursor.file_name_and_error_line.empty()) {
224 char name_buf[256];
225 std::string string = log_item.cursor.file_name_and_error_line.substr(0,
226 sizeof(name_buf) - 1);
227 StringRefNull(string).copy_utf8_truncated(name_buf);
228 BLI_dynstr_appendf(dynstr, "%s%s: %s", info_col, name_buf, reset_col);
229 }
230 else if (source_index > 0) {
232 sources[source_index]);
233 if (!filename.is_empty()) {
234 BLI_dynstr_appendf(dynstr,
235 "%s%s:%d:%d: %s",
236 info_col,
237 filename.c_str(),
238 row_in_file,
239 log_item.cursor.column + 1,
240 reset_col);
241 }
242 }
243
244 if (log_item.severity == Severity::Error) {
245 BLI_dynstr_appendf(dynstr, "%s%s%s: ", err_col, "Error", info_col);
246 }
247 else if (log_item.severity == Severity::Warning) {
248 BLI_dynstr_appendf(dynstr, "%s%s%s: ", warn_col, "Warning", info_col);
249 }
250 else if (log_item.severity == Severity::Note) {
251 BLI_dynstr_appendf(dynstr, "%s%s%s: ", warn_col, "Note", info_col);
252 }
253 /* Print the error itself. */
254 BLI_dynstr_append(dynstr, info_col);
255 BLI_dynstr_nappend(dynstr, log_line, (line_end + 1) - log_line);
256 BLI_dynstr_append(dynstr, reset_col);
257 /* Continue to next line. */
258 log_line = line_end + 1;
259 previous_location = log_item.cursor;
260 }
261
262 CLG_Level level = error ? CLG_LEVEL_ERROR : CLG_LEVEL_WARN;
263
264 if (CLOG_CHECK(&LOG, level)) {
265 if (DEBUG_LOG_SHADER_SRC_ON_ERROR && error) {
266 CLG_log_raw(LOG.type, sources_combined.c_str());
267 }
268 const char *_str = BLI_dynstr_get_cstring(dynstr);
269 CLOG_AT_LEVEL(&LOG, level, "%s %s: %s", this->name, stage, _str);
270 MEM_delete(_str);
271 }
272
273 BLI_dynstr_free(dynstr);
274}
275
276const char *GPULogParser::skip_severity(const char *log_line,
277 GPULogItem &log_item,
278 const char *error_msg,
279 const char *warning_msg,
280 const char *note_msg) const
281{
282 if (STREQLEN(log_line, error_msg, strlen(error_msg))) {
283 log_line += strlen(error_msg);
284 log_item.severity = Severity::Error;
285 }
286 else if (STREQLEN(log_line, warning_msg, strlen(warning_msg))) {
287 log_line += strlen(warning_msg);
288 log_item.severity = Severity::Warning;
289 }
290 else if (STREQLEN(log_line, note_msg, strlen(note_msg))) {
291 log_line += strlen(note_msg);
292 log_item.severity = Severity::Note;
293 }
294 return log_line;
295}
296
297const char *GPULogParser::skip_separators(const char *log_line, const StringRef separators) const
298{
299 while (at_any(log_line, separators)) {
300 log_line++;
301 }
302 return log_line;
303}
304
305const char *GPULogParser::skip_until(const char *log_line, char stop_char) const
306{
307 const char *cursor = log_line;
308 while (!ELEM(cursor[0], '\n', '\0')) {
309 if (cursor[0] == stop_char) {
310 return cursor;
311 }
312 cursor++;
313 }
314 return log_line;
315}
316
317bool GPULogParser::at_number(const char *log_line) const
318{
319 return log_line[0] >= '0' && log_line[0] <= '9';
320}
321
322bool GPULogParser::at_any(const char *log_line, const StringRef chars) const
323{
324 return chars.find(log_line[0]) != StringRef::not_found;
325}
326
327int GPULogParser::parse_number(const char *log_line, const char **r_new_position) const
328{
329 return int(strtol(log_line, const_cast<char **>(r_new_position), 10));
330}
331
332size_t GPULogParser::line_start_get(StringRefNull source_combined, size_t target_line)
333{
334 size_t cursor = 0;
335 size_t current_line = 1;
336 for (char c : source_combined) {
337 if (current_line >= target_line) {
338 return cursor + 1;
339 }
340 if (c == '\n') {
341 current_line++;
342 }
343 cursor++;
344 }
345 return -1;
346}
347
349{
350 StringRef sub_str = source_combined.substr(0, pos);
351 StringRefNull directive = "#line 1 \"";
352 size_t nearest_line_directive = sub_str.rfind(directive);
353 if (nearest_line_directive != std::string::npos) {
354 size_t start_of_file_name = nearest_line_directive + directive.size();
355 size_t end_of_file_name = sub_str.find('\"', start_of_file_name);
356 if (end_of_file_name != std::string::npos) {
357 return sub_str.substr(start_of_file_name, end_of_file_name - start_of_file_name);
358 }
359 }
360 return {};
361}
362
363/* Original source file line. Found by looking up #line directives. */
364size_t GPULogParser::source_line_get(StringRefNull source_combined, size_t pos)
365{
366 StringRef sub_str = source_combined.substr(0, pos);
367 StringRefNull directive = "#line ";
368 size_t nearest_line_directive = sub_str.rfind(directive);
369 size_t line_count = 1;
370 if (nearest_line_directive != std::string::npos) {
371 sub_str = sub_str.substr(nearest_line_directive + directive.size());
372 line_count = std::stoll(sub_str) - 1;
373 }
374 return line_count + std::count(sub_str.begin(), sub_str.end(), '\n');
375}
376
377
378/* -------------------------------------------------------------------- */
381
383{
384#if GPU_SHADER_PRINTF_ENABLE == 0
385 return;
386#endif
387 if (ctx == nullptr) {
388 return;
389 }
391 return;
392 }
394 sizeof(uint32_t));
396 ctx->printf_buf.append(printf_buf);
397}
398
400{
401#if GPU_SHADER_PRINTF_ENABLE == 0
402 return;
403#endif
404 if (ctx == nullptr) {
405 return;
406 }
407 if (ctx->printf_buf.is_empty()) {
408 return;
409 }
410 StorageBuf *printf_buf = ctx->printf_buf.pop_last();
411
413 GPU_storagebuf_read(printf_buf, data.data());
414 GPU_storagebuf_free(printf_buf);
415
416 uint32_t data_len = data[0];
417 if (data_len == 0) {
418 return;
419 }
420
421 /* data[0] contains the length of the data. */
422 int cursor = 1;
423 while (cursor < data_len + 1) {
424 uint32_t format_hash = data[cursor++];
425
427 format_hash);
428
429 if (cursor + format.format_blocks.size() >= GPU_SHADER_PRINTF_MAX_CAPACITY) {
430 printf("Printf buffer overflow.\n");
431 break;
432 }
433
434 for (const shader::PrintfFormat::Block &block : format.format_blocks) {
435 switch (block.type) {
437 printf("%s", block.fmt.c_str());
438 break;
440 /* This is actually just the string that is referenced, not a format. */
442 *reinterpret_cast<uint32_t *>(&data[cursor++]));
443 printf(block.fmt.c_str(), str.format_str.c_str());
444 break;
445 }
447 printf(block.fmt.c_str(), *reinterpret_cast<uint32_t *>(&data[cursor++]));
448 break;
450 printf(block.fmt.c_str(), *reinterpret_cast<int32_t *>(&data[cursor++]));
451 break;
453 printf(block.fmt.c_str(), *reinterpret_cast<float *>(&data[cursor++]));
454 break;
455 default:
457 break;
458 }
459 }
460 }
461}
462
464
465} // namespace gpu
466} // namespace blender
A dynamically sized string ADT.
void CLG_log_raw(const CLG_LogType *lg, const char *message)
Definition clog.cc:697
#define CLOG_CHECK(clg_ref, verbose_level,...)
Definition CLG_log.h:147
CLG_Level
Definition CLG_log.h:52
@ CLG_LEVEL_ERROR
Definition CLG_log.h:56
@ CLG_LEVEL_WARN
Definition CLG_log.h:58
#define CLOG_AT_LEVEL(clg_ref, verbose_level,...)
Definition CLG_log.h:150
int CLG_color_support_get(CLG_LogRef *clg_ref)
Definition clog.cc:1008
#define GPU_storagebuf_create(size)
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define STREQLEN(a, b, n)
#define ELEM(...)
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr StringRefNull()
constexpr int64_t rfind(char c, int64_t pos=INT64_MAX) const
constexpr const char * end() const
constexpr const char * begin() const
static constexpr int64_t not_found
constexpr int64_t size() const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr bool endswith(StringRef suffix) const
Vector< StorageBuf * > printf_buf
static size_t line_start_get(StringRefNull source_combined, size_t target_line)
static StringRef filename_get(StringRefNull source_combined, size_t pos)
int parse_number(const char *log_line, const char **r_new_position) const
const char * skip_separators(const char *log_line, const StringRef separators) const
bool at_number(const char *log_line) const
static size_t source_line_get(StringRefNull source_combined, size_t pos)
bool at_any(const char *log_line, const StringRef chars) const
const char * skip_until(const char *log_line, char stop_char) const
const char * skip_severity(const char *log_line, GPULogItem &log_item, const char *error_msg, const char *warning_msg, const char *note_msg) const
void print_log(Span< StringRefNull > sources, const char *log, const char *stage, bool error, GPULogParser *parser)
#define str(s)
#define GPU_SHADER_PRINTF_MAX_CAPACITY
#define log
#define DEBUG_CONTEXT_LINES
#define DEBUG_LOG_SHADER_SRC_ON_ERROR
std::string filename(const std::string_view &str, size_t pos)
const PrintfFormat & gpu_shader_dependency_get_printf_format(uint32_t format_hash)
StringRefNull gpu_shader_dependency_get_filename_from_source_string(const StringRef source_string)
Find the name of the file from which the given string was generated.
void printf_begin(Context *ctx)
void printf_end(Context *ctx)
BMesh Iterators.
void BLI_dynstr_appendf(DynStr *__restrict ds, const char *__restrict format,...) ATTR_PRINTF_FORMAT(2
__int64 int64_t
Definition BKE_text.h:90
void GPU_storagebuf_clear_to_zero(gpu::StorageBuf *ssbo)
void BLI_dynstr_nappend(DynStr *__restrict ds, const char *cstr, int len) ATTR_NONNULL()
Definition BLI_dynstr.cc:77
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
unsigned int uint32_t
Definition BKE_text.h:81
void BLI_dynstr_append(DynStr *__restrict ds, const char *cstr) ATTR_NONNULL()
Definition BLI_dynstr.cc:58
BMesh const char void * data
char * BLI_dynstr_get_cstring(const DynStr *ds) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
signed int int32_t
Definition BKE_text.h:78
static CLG_LogRef LOG
Definition logging.cpp:12
void GPU_storagebuf_free(gpu::StorageBuf *ssbo)
DynStr * BLI_dynstr_new() ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_dynstr.cc:39
void GPU_storagebuf_read(gpu::StorageBuf *ssbo, void *data)
enum blender::gpu::shader::PrintfFormat::Block::ArgumentType type