Blender V5.3
gl_debug.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "BLI_compiler_attrs.hh"
12#include "BLI_string.hh"
13#include "BLI_system.hh"
14#include "BLI_utildefines.hh"
15
16#include "BKE_global.hh"
17
18#include "GPU_debug.hh"
19#include "GPU_platform.hh"
20#include "gpu_profile_report.hh"
21
22#include "CLG_log.h"
23
24#include "gl_backend.hh"
25#include "gl_context.hh"
26#include "gl_uniform_buffer.hh"
27
28#include "gl_debug.hh"
29
30namespace blender {
31
32static CLG_LogRef LOG = {"gpu.debug"};
33
34/* Avoid too much NVidia buffer info in the output log. */
35#define TRIM_NVIDIA_BUFFER_INFO 1
36/* Avoid unneeded shader statistics. */
37#define TRIM_SHADER_STATS_INFO 1
38
39namespace gpu::debug {
40
41/* -------------------------------------------------------------------- */
47
48/* Debug callbacks need the same calling convention as OpenGL functions. */
49#if defined(_WIN32)
50# define APIENTRY __stdcall
51#else
52# define APIENTRY
53#endif
54
55static void APIENTRY debug_callback(GLenum /*source*/,
56 GLenum type,
57 GLuint /*id*/,
58 GLenum severity,
59 GLsizei /*length*/,
60 const GLchar *message,
61 const GLvoid * /*userParm*/)
62{
63 if (ELEM(type, GL_DEBUG_TYPE_PUSH_GROUP, GL_DEBUG_TYPE_POP_GROUP)) {
64 /* The debug layer will emit a message each time a debug group is pushed or popped.
65 * We use that for easy command grouping inside frame analyzer tools. */
66 return;
67 }
68
69 /* NOTE: callback function can be triggered during before the platform is initialized.
70 * In this case invoking `GPU_type_matches` would fail and
71 * therefore the message is checked before the platform matching. */
72 if (TRIM_NVIDIA_BUFFER_INFO && STRPREFIX(message, "Buffer detailed info") &&
74 {
75 /* Suppress buffer information flooding the output. */
76 return;
77 }
78
79 if (TRIM_SHADER_STATS_INFO && STRPREFIX(message, "Shader Stats")) {
80 /* Suppress buffer information flooding the output. */
81 return;
82 }
83
84 const bool use_color = CLG_color_support_get(&LOG);
85
86 if (ELEM(severity, GL_DEBUG_SEVERITY_LOW, GL_DEBUG_SEVERITY_NOTIFICATION)) {
88 const char *format = use_color ? "\033[2m%s\033[0m" : "%s";
89 CLG_logf(LOG.type, CLG_LEVEL_INFO, "Notification", "", format, message);
90 }
91 }
92 else {
93 char debug_groups[512] = "";
94 GPU_debug_get_groups_names(sizeof(debug_groups), debug_groups);
95 CLG_Level clog_level;
96
99 {
100 /* Do not duplicate shader compilation error/warnings. */
101 return;
102 }
103
104 switch (type) {
105 case GL_DEBUG_TYPE_ERROR:
106 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
107 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
108 clog_level = CLG_LEVEL_ERROR;
109 break;
110 case GL_DEBUG_TYPE_PORTABILITY:
111 case GL_DEBUG_TYPE_PERFORMANCE:
112 case GL_DEBUG_TYPE_OTHER:
113 case GL_DEBUG_TYPE_MARKER: /* KHR has this, ARB does not */
114 default:
115 clog_level = CLG_LEVEL_WARN;
116 break;
117 }
118
119 if (CLOG_CHECK(&LOG, clog_level)) {
120 CLG_logf(LOG.type, clog_level, debug_groups, "", "%s", message);
121 if (severity == GL_DEBUG_SEVERITY_HIGH) {
122 /* Focus on error message. */
123 if (use_color) {
124 fprintf(stderr, "\033[2m");
125 }
126 BLI_system_backtrace(stderr);
127 if (use_color) {
128 fprintf(stderr, "\033[0m\n");
129 }
130 fflush(stderr);
131 }
132 }
133 }
134}
135
136#undef APIENTRY
137
139{
140 glEnable(GL_DEBUG_OUTPUT);
141 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
142 glDebugMessageCallback(static_cast<GLDEBUGPROC>(debug_callback), nullptr);
143 glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
144 glDebugMessageInsert(GL_DEBUG_SOURCE_APPLICATION,
145 GL_DEBUG_TYPE_MARKER,
146 0,
147 GL_DEBUG_SEVERITY_NOTIFICATION,
148 -1,
149 "Successfully hooked OpenGL debug callback");
150}
151
153
154/* -------------------------------------------------------------------- */
160
161void check_gl_error(const char *info)
162{
163 if (!(G.debug & G_DEBUG_GPU)) {
164 return;
165 }
166 GLenum error = glGetError();
167
168#define ERROR_CASE(err) \
169 case err: { \
170 char msg[256]; \
171 SNPRINTF(msg, "%s : %s", #err, info); \
172 debug_callback(0, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, 0, msg, nullptr); \
173 break; \
174 }
175
176 switch (error) {
177 ERROR_CASE(GL_INVALID_ENUM)
178 ERROR_CASE(GL_INVALID_VALUE)
179 ERROR_CASE(GL_INVALID_OPERATION)
180 ERROR_CASE(GL_INVALID_FRAMEBUFFER_OPERATION)
181 ERROR_CASE(GL_OUT_OF_MEMORY)
182 ERROR_CASE(GL_STACK_UNDERFLOW)
183 ERROR_CASE(GL_STACK_OVERFLOW)
184 case GL_NO_ERROR:
185 break;
186 default:
187 char msg[256];
188 SNPRINTF(msg, "Unknown GL error: %x : %s", error, info);
189 debug_callback(0, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, 0, msg, nullptr);
190 break;
191 }
192}
193
194void check_gl_resources(const char *info)
195{
196 if (!(G.debug & G_DEBUG_GPU)) {
197 return;
198 }
199
200 GLContext *ctx = GLContext::get();
201 ShaderInterface *interface = ctx->shader->interface;
202 /* NOTE: This only check binding. To be valid, the bound ubo needs to
203 * be big enough to feed the data range the shader awaits. */
204 uint16_t ubo_needed = interface->enabled_ubo_mask_;
205 ubo_needed &= ~ctx->bound_ubo_slots;
206 /* NOTE: This only check binding. To be valid, the bound ssbo needs to
207 * be big enough to feed the data range the shader awaits. */
208 uint16_t ssbo_needed = interface->enabled_ssbo_mask_;
209 ssbo_needed &= ~ctx->bound_ssbo_slots;
210 /* NOTE: This only check binding. To be valid, the bound texture needs to
211 * be the same format/target the shader expects. */
212 uint64_t tex_needed = interface->enabled_tex_mask_;
213 tex_needed &= ~GLContext::state_manager_active_get()->bound_texture_slots();
214 /* NOTE: This only check binding. To be valid, the bound image needs to
215 * be the same format/target the shader expects. */
216 uint8_t ima_needed = interface->enabled_ima_mask_;
217 ima_needed &= ~GLContext::state_manager_active_get()->bound_image_slots();
218
219 if (ubo_needed == 0 && tex_needed == 0 && ima_needed == 0 && ssbo_needed == 0) {
220 return;
221 }
222
223 for (int i = 0; ubo_needed != 0; i++, ubo_needed >>= 1) {
224 if ((ubo_needed & 1) != 0) {
225 const ShaderInput *ubo_input = interface->ubo_get(i);
226 const char *ubo_name = interface->input_name_get(ubo_input);
227 const StringRefNull sh_name = ctx->shader->name_get();
228 char msg[256];
229 SNPRINTF(
230 msg, "Missing UBO bind at slot %d : %s > %s : %s", i, sh_name.c_str(), ubo_name, info);
231 debug_callback(0, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, 0, msg, nullptr);
232 }
233 }
234
235 for (int i = 0; ssbo_needed != 0; i++, ssbo_needed >>= 1) {
236 if ((ssbo_needed & 1) != 0) {
237 const ShaderInput *ssbo_input = interface->ssbo_get(i);
238 const char *ssbo_name = interface->input_name_get(ssbo_input);
239 const StringRefNull sh_name = ctx->shader->name_get();
240 char msg[256];
241 SNPRINTF(
242 msg, "Missing SSBO bind at slot %d : %s > %s : %s", i, sh_name.c_str(), ssbo_name, info);
243 debug_callback(0, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, 0, msg, nullptr);
244 }
245 }
246
247 for (int i = 0; tex_needed != 0; i++, tex_needed >>= 1) {
248 if ((tex_needed & 1) != 0) {
249 /* FIXME: texture_get might return an image input instead. */
250 const ShaderInput *tex_input = interface->texture_get(i);
251 const char *tex_name = interface->input_name_get(tex_input);
252 const StringRefNull sh_name = ctx->shader->name_get();
253 char msg[256];
254 SNPRINTF(msg,
255 "Missing Texture bind at slot %d : %s > %s : %s",
256 i,
257 sh_name.c_str(),
258 tex_name,
259 info);
260 debug_callback(0, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, 0, msg, nullptr);
261 }
262 }
263
264 for (int i = 0; ima_needed != 0; i++, ima_needed >>= 1) {
265 if ((ima_needed & 1) != 0) {
266 /* FIXME: texture_get might return a texture input instead. */
267 const ShaderInput *tex_input = interface->texture_get(i);
268 const char *tex_name = interface->input_name_get(tex_input);
269 const StringRefNull sh_name = ctx->shader->name_get();
270 char msg[256];
271 SNPRINTF(
272 msg, "Missing Image bind at slot %d : %s > %s : %s", i, sh_name.c_str(), tex_name, info);
273 debug_callback(0, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, 0, msg, nullptr);
274 }
275 }
276}
277
278void raise_gl_error(const char *info)
279{
280 debug_callback(0, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, 0, info, nullptr);
281}
282
284
285/* -------------------------------------------------------------------- */
291
292static const char *to_str_prefix(GLenum type)
293{
294 switch (type) {
295 case GL_FRAGMENT_SHADER:
296 case GL_GEOMETRY_SHADER:
297 case GL_VERTEX_SHADER:
298 case GL_SHADER:
299 case GL_PROGRAM:
300 return "SHD-";
301 case GL_SAMPLER:
302 return "SAM-";
303 case GL_TEXTURE:
304 return "TEX-";
305 case GL_FRAMEBUFFER:
306 return "FBO-";
307 case GL_VERTEX_ARRAY:
308 return "VAO-";
309 case GL_UNIFORM_BUFFER:
310 return "UBO-";
311 case GL_BUFFER:
312 return "BUF-";
313 default:
314 return "";
315 }
316}
317static const char *to_str_suffix(GLenum type)
318{
319 switch (type) {
320 case GL_FRAGMENT_SHADER:
321 return "-Frag";
322 case GL_GEOMETRY_SHADER:
323 return "-Geom";
324 case GL_VERTEX_SHADER:
325 return "-Vert";
326 default:
327 return "";
328 }
329}
330
331void object_label(GLenum type, GLuint object, const char *name)
332{
333 if ((G.debug & G_DEBUG_GPU) &&
334 (epoxy_gl_version() >= 43 || epoxy_has_gl_extension("GL_KHR_debug")))
335 {
336 char label[64];
337 SNPRINTF(label, "%s%s%s", to_str_prefix(type), name, to_str_suffix(type));
338 /* Small convenience for caller. */
339 switch (type) {
340 case GL_FRAGMENT_SHADER:
341 case GL_GEOMETRY_SHADER:
342 case GL_VERTEX_SHADER:
343 case GL_COMPUTE_SHADER:
344 type = GL_SHADER;
345 break;
346 case GL_UNIFORM_BUFFER:
347 case GL_SHADER_STORAGE_BUFFER:
348 case GL_ARRAY_BUFFER:
349 case GL_ELEMENT_ARRAY_BUFFER:
350 type = GL_BUFFER;
351 break;
352 default:
353 break;
354 }
355 glObjectLabel(type, object, -1, label);
356 }
357}
358
360
361} // namespace gpu::debug
362
363namespace gpu {
364
365/* -------------------------------------------------------------------- */
370
371void GLContext::debug_group_begin(const char *name, int index)
372{
373 if ((G.debug & G_DEBUG_GPU) &&
374 (epoxy_gl_version() >= 43 || epoxy_has_gl_extension("GL_KHR_debug")))
375 {
376 /* Add 10 to avoid collision with other indices from other possible callback layers. */
377 index += 10;
378 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, index, -1, name);
379 }
380
381 if (!G.profile_gpu) {
382 return;
383 }
384
385 TimeQuery query = {};
386 query.name = name;
387 query.finished = false;
388
389 glGetInteger64v(GL_TIMESTAMP, &query.cpu_start);
390 /* Use GL_TIMESTAMP instead of GL_ELAPSED_TIME to support nested debug groups */
391 glGenQueries(2, query.handles);
392 glQueryCounter(query.handle_start, GL_TIMESTAMP);
393
394 if (frame_timings.is_empty()) {
395 frame_timings.append({});
396 }
397 frame_timings.last().queries.append(query);
398}
399
401{
402 if ((G.debug & G_DEBUG_GPU) &&
403 (epoxy_gl_version() >= 43 || epoxy_has_gl_extension("GL_KHR_debug")))
404 {
405 glPopDebugGroup();
406 }
407
408 if (!G.profile_gpu) {
409 return;
410 }
411
412 Vector<TimeQuery> &queries = frame_timings.last().queries;
413 for (int i = queries.size() - 1; i >= 0; i--) {
414 TimeQuery &query = queries[i];
415 if (!query.finished) {
416 query.finished = true;
417 glQueryCounter(query.handle_end, GL_TIMESTAMP);
418 glGetInteger64v(GL_TIMESTAMP, &query.cpu_end);
419 break;
420 }
421 if (i == 0) {
422 CLOG_ERROR(&LOG, "Profile GPU error: Extra GPU_debug_group_end() call.");
423 }
424 }
425}
426
427void GLContext::process_frame_timings()
428{
429 if (!G.profile_gpu) {
430 return;
431 }
432
433 for (int frame_i = 0; frame_i < frame_timings.size(); frame_i++) {
434 Vector<TimeQuery> &queries = frame_timings[frame_i].queries;
435
436 GLint frame_is_ready = 0;
437 bool frame_is_valid = !queries.is_empty();
438
439 for (int i = queries.size() - 1; i >= 0; i--) {
440 if (!queries[i].finished) {
441 frame_is_valid = false;
442 CLOG_ERROR(&LOG, "Profile GPU error: Missing GPU_debug_group_end() call");
443 }
444 else {
445 glGetQueryObjectiv(queries.last().handle_end, GL_QUERY_RESULT_AVAILABLE, &frame_is_ready);
446 }
447 break;
448 }
449
450 if (!frame_is_valid) {
451 /* Cleanup. */
452 for (TimeQuery &query : queries) {
453 glDeleteQueries(2, query.handles);
454 }
455 frame_timings.remove(frame_i--);
456 continue;
457 }
458
459 if (!frame_is_ready) {
460 break;
461 }
462
463 for (TimeQuery &query : queries) {
464 GLuint64 gpu_start = 0;
465 GLuint64 gpu_end = 0;
466 glGetQueryObjectui64v(query.handle_start, GL_QUERY_RESULT, &gpu_start);
467 glGetQueryObjectui64v(query.handle_end, GL_QUERY_RESULT, &gpu_end);
468 glDeleteQueries(2, query.handles);
469
471 query.name, gpu_start, gpu_end, query.cpu_start, query.cpu_end);
472 }
473
474 frame_timings.remove(frame_i--);
475 }
476
477 frame_timings.append({});
478}
479
480bool GLContext::debug_capture_begin(const char *title)
481{
482 return GLBackend::get()->debug_capture_begin(title);
483}
484
485bool GLBackend::debug_capture_begin(const char *title)
486{
487#ifdef WITH_RENDERDOC
488 if (G.debug & G_DEBUG_GPU_RENDERDOC) {
489 bool result = renderdoc_.start_frame_capture(nullptr, nullptr);
490 if (result && title) {
491 renderdoc_.set_frame_capture_title(title);
492 }
493 return result;
494 }
495#endif
496 UNUSED_VARS(title);
497 return false;
498}
499
504
506{
507#ifdef WITH_RENDERDOC
508 if (G.debug & G_DEBUG_GPU_RENDERDOC) {
509 renderdoc_.end_frame_capture(nullptr, nullptr);
510 }
511#endif
512}
513
515{
516 return (void *)name;
517}
518
520{
521#ifdef WITH_RENDERDOC
522 const char *title = (const char *)scope;
523 if (StringRefNull(title) != StringRefNull(G.gpu_debug_scope_name)) {
524 return false;
525 }
527#else
528 UNUSED_VARS(scope);
529#endif
530 return false;
531}
532
534{
535#ifdef WITH_RENDERDOC
536 const char *title = (const char *)scope;
537 if (StringRefNull(title) == StringRefNull(G.gpu_debug_scope_name)) {
539 }
540#else
541 UNUSED_VARS(scope);
542#endif
543}
544
546{
547 this->bound_ubo_slots = 0u;
548}
549
551{
552 this->bound_ssbo_slots = 0u;
553}
554
556
557} // namespace gpu
558} // namespace blender
#define SNPRINTF(dst, format,...)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
void void CLG_logf(const CLG_LogType *lg, enum CLG_Level level, const char *file_line, const char *fn, const char *format,...) _CLOG_ATTR_NONNULL(1
#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_INFO
Definition CLG_log.h:60
@ CLG_LEVEL_WARN
Definition CLG_log.h:58
int CLG_color_support_get(CLG_LogRef *clg_ref)
Definition clog.cc:1008
#define GPU_DEBUG_SHADER_SPECIALIZATION_GROUP
Definition GPU_debug.hh:71
#define GPU_DEBUG_SHADER_COMPILATION_GROUP
Definition GPU_debug.hh:70
#define STRPREFIX(a, b)
#define UNUSED_VARS(...)
#define ELEM(...)
constexpr const char * c_str() const
constexpr StringRefNull()
int64_t size() const
const T & last(const int64_t n=0) const
bool is_empty() const
static GLBackend * get()
Definition gl_backend.hh:86
bool debug_capture_begin(const char *title)
Definition gl_debug.cc:485
void * debug_capture_scope_create(const char *name) override
Definition gl_debug.cc:514
void debug_unbind_all_ubo() override
Definition gl_debug.cc:545
void debug_capture_end() override
Definition gl_debug.cc:500
bool debug_capture_begin(const char *title) override
Definition gl_debug.cc:480
void debug_group_begin(const char *name, int index) override
Definition gl_debug.cc:371
void debug_capture_scope_end(void *scope) override
Definition gl_debug.cc:533
void debug_group_end() override
Definition gl_debug.cc:400
static GLContext * get()
bool debug_capture_scope_begin(void *scope) override
Definition gl_debug.cc:519
void debug_unbind_all_ssbo() override
Definition gl_debug.cc:550
static ProfileReport & get()
void add_group(StringRefNull name, uint64_t gpu_start, uint64_t gpu_end, uint64_t cpu_start, uint64_t cpu_end)
StringRefNull name_get() const
#define ERROR_CASE(err)
#define APIENTRY
Definition gl_debug.cc:52
#define TRIM_NVIDIA_BUFFER_INFO
Definition gl_debug.cc:35
#define TRIM_SHADER_STATS_INFO
Definition gl_debug.cc:37
void check_gl_resources(const char *info)
Definition gl_debug.cc:194
static void APIENTRY debug_callback(GLenum, GLenum type, GLuint, GLenum severity, GLsizei, const GLchar *message, const GLvoid *)
Definition gl_debug.cc:55
void raise_gl_error(const char *info)
Definition gl_debug.cc:278
static const char * to_str_prefix(GLenum type)
Definition gl_debug.cc:292
void check_gl_error(const char *info)
Definition gl_debug.cc:161
static const char * to_str_suffix(GLenum type)
Definition gl_debug.cc:317
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:331
BMesh Iterators.
unsigned __int64 uint64_t
Definition BKE_text.h:91
@ GPU_DRIVER_OFFICIAL
@ G_DEBUG_GPU_RENDERDOC
Global G
Definition blender.cc:57
unsigned short uint16_t
Definition BKE_text.h:80
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
unsigned char uint8_t
Definition BKE_text.h:79
bool GPU_debug_group_match(const char *ref)
Definition gpu_debug.cc:84
void BLI_system_backtrace(FILE *fp)
Definition system.cc:112
static CLG_LogRef LOG
Definition logging.cpp:12
void GPU_debug_get_groups_names(int name_buf_maxncpy, char *r_name_buf)
Definition gpu_debug.cc:44
@ GPU_DEVICE_NVIDIA
int size() const
Definition vector.hh:130