GPU: Use CLOG to for debug output

This removes the escape color control caracters when the output
does not supports it (i.e: file output, windows cmd).
This commit is contained in:
Clément Foucault 2020-10-23 19:29:27 +02:00 committed by Howard Trickey
parent e856443c99
commit 0d1f65e516
Notes: blender-bot 2023-02-14 08:10:10 +01:00
Referenced by issue #95592, OpenGL Invalid Enum
7 changed files with 77 additions and 38 deletions

View File

@ -44,6 +44,7 @@ set(INC
../nodes
../nodes/intern
../../../intern/clog
../../../intern/ghost
../../../intern/glew-mx
../../../intern/guardedalloc

View File

@ -74,7 +74,7 @@ void GPU_debug_get_groups_names(int name_buf_len, char *r_name_buf)
for (StringRef &name : stack) {
sz += BLI_snprintf_rlen(r_name_buf + sz, name_buf_len - sz, "%s > ", name.data());
}
r_name_buf[sz - 2] = ':';
r_name_buf[sz - 3] = '\0';
}
/* Return true if inside a debug group with the same name. */

View File

@ -23,6 +23,7 @@
#include "MEM_guardedalloc.h"
#include "BLI_dynstr.h"
#include "BLI_math_base.h"
#include "BLI_math_vector.h"
#include "BLI_path_util.h"
@ -47,8 +48,12 @@
#include "gpu_context_private.hh"
#include "gpu_shader_private.hh"
#include "CLG_log.h"
extern "C" char datatoc_gpu_shader_colorspace_lib_glsl[];
static CLG_LogRef LOG = {"gpu.shader"};
using namespace blender;
using namespace blender::gpu;
@ -56,12 +61,21 @@ using namespace blender::gpu;
/** \name Debug functions
* \{ */
void Shader::print_errors(Span<const char *> sources, char *log, const char *stage)
void Shader::print_log(Span<const char *> sources, char *log, const char *stage, const bool error)
{
const char line_prefix[] = " | ";
char err_col[] = "\033[31;1m";
char warn_col[] = "\033[33;1m";
char info_col[] = "\033[0;2m";
char reset_col[] = "\033[0;0m";
char *sources_combined = BLI_string_join_arrayN((const char **)sources.data(), sources.size());
DynStr *dynstr = BLI_dynstr_new();
fprintf(stderr, "GPUShader: Compilation Log : %s : %s\n", this->name, stage);
if (!CLG_color_support_get(&LOG)) {
err_col[0] = warn_col[0] = info_col[0] = reset_col[0] = '\0';
}
BLI_dynstr_appendf(dynstr, "\n");
char *log_line = log, *line_end;
char *error_line_number_end;
@ -136,10 +150,10 @@ void Shader::print_errors(Span<const char *> sources, char *log, const char *sta
}
/* Separate from previous block. */
if (last_error_line != error_line) {
fprintf(stderr, "\033[90m%s\033[39m\n", line_prefix);
BLI_dynstr_appendf(dynstr, "%s%s%s\n", info_col, line_prefix, reset_col);
}
else if (error_char != last_error_char) {
fprintf(stderr, "%s\n", line_prefix);
BLI_dynstr_appendf(dynstr, "%s\n", line_prefix);
}
/* Print line from the source file that is producing the error. */
if ((error_line != -1) && (error_line != last_error_line || error_char != last_error_char)) {
@ -159,24 +173,24 @@ void Shader::print_errors(Span<const char *> sources, char *log, const char *sta
/* Print error source. */
if (found_line_id) {
if (error_line != last_error_line) {
fprintf(stderr, "%5d | ", src_line_index);
BLI_dynstr_appendf(dynstr, "%5d | ", src_line_index);
}
else {
fprintf(stderr, line_prefix);
BLI_dynstr_appendf(dynstr, line_prefix);
}
fwrite(src_line, (src_line_end + 1) - src_line, 1, stderr);
BLI_dynstr_nappend(dynstr, src_line, (src_line_end + 1) - src_line);
/* Print char offset. */
fprintf(stderr, line_prefix);
BLI_dynstr_appendf(dynstr, line_prefix);
if (error_char != -1) {
for (int i = 0; i < error_char; i++) {
fprintf(stderr, " ");
BLI_dynstr_appendf(dynstr, " ");
}
fprintf(stderr, "^");
BLI_dynstr_appendf(dynstr, "^");
}
fprintf(stderr, "\n");
BLI_dynstr_appendf(dynstr, "\n");
}
}
fprintf(stderr, line_prefix);
BLI_dynstr_appendf(dynstr, line_prefix);
/* Skip to message. Avoid redundant info. */
const char *keywords[] = {"error", "warning"};
for (int i = 0; i < ARRAY_SIZE(prefix); i++) {
@ -191,22 +205,32 @@ void Shader::print_errors(Span<const char *> sources, char *log, const char *sta
log_line++;
}
if (type == 0) {
fprintf(stderr, "\033[31;1mError\033[0;2m: ");
BLI_dynstr_appendf(dynstr, "%s%s%s: ", err_col, "Error", info_col);
}
else if (type == 1) {
fprintf(stderr, "\033[33;1mWarning\033[0;2m: ");
BLI_dynstr_appendf(dynstr, "%s%s%s: ", warn_col, "Warning", info_col);
}
/* Print the error itself. */
fprintf(stderr, "\033[2m");
fwrite(log_line, (line_end + 1) - log_line, 1, stderr);
fprintf(stderr, "\033[0m");
BLI_dynstr_appendf(dynstr, info_col);
BLI_dynstr_nappend(dynstr, log_line, (line_end + 1) - log_line);
BLI_dynstr_appendf(dynstr, reset_col);
/* Continue to next line. */
log_line = line_end + 1;
last_error_line = error_line;
last_error_char = error_char;
}
fprintf(stderr, "\n");
MEM_freeN(sources_combined);
CLG_Severity severity = error ? CLG_SEVERITY_ERROR : CLG_SEVERITY_WARN;
if (((LOG.type->flag & CLG_FLAG_USE) && (LOG.type->level >= 0)) ||
(severity >= CLG_SEVERITY_WARN)) {
const char *_str = BLI_dynstr_get_cstring(dynstr);
CLG_log_str(LOG.type, severity, this->name, stage, _str);
MEM_freeN((void *)_str);
}
BLI_dynstr_free(dynstr);
}
/** \} */

View File

@ -85,7 +85,6 @@ void ShaderInterface::debug_print(void)
char *name_buf = name_buffer_;
const char format[] = " | %.8x : %4d : %s\n";
printf(" \033[1mGPUShaderInterface : \033[0m\n");
if (attrs.size() > 0) {
printf("\n Attributes :\n");
}

View File

@ -70,7 +70,7 @@ class Shader {
};
protected:
void print_errors(Span<const char *> sources, char *log, const char *stage);
void print_log(Span<const char *> sources, char *log, const char *stage, const bool error);
};
/* Syntacting suggar. */

View File

@ -33,6 +33,8 @@
#include "GPU_debug.h"
#include "GPU_platform.h"
#include "CLG_log.h"
#include "glew-mx.h"
#include "gl_context.hh"
@ -42,6 +44,8 @@
#include <stdio.h>
static CLG_LogRef LOG = {"gpu.debug"};
/* Avoid too much NVidia buffer info in the output log. */
#define TRIM_NVIDIA_BUFFER_INFO 1
@ -61,8 +65,6 @@ namespace blender::gpu::debug {
# define APIENTRY
#endif
#define VERBOSE 1
static void APIENTRY debug_callback(GLenum UNUSED(source),
GLenum type,
GLuint UNUSED(id),
@ -86,36 +88,47 @@ static void APIENTRY debug_callback(GLenum UNUSED(source),
const char format[] = "GPUDebug: %s%s%s\033[0m\n";
const bool use_color = CLG_color_support_get(&LOG);
if (ELEM(severity, GL_DEBUG_SEVERITY_LOW, GL_DEBUG_SEVERITY_NOTIFICATION)) {
if (VERBOSE) {
fprintf(stderr, format, "\033[2m", "", message);
if (((LOG.type->flag & CLG_FLAG_USE) && (LOG.type->level >= CLG_SEVERITY_INFO))) {
const char *format = use_color ? "\033[2m%s\033[0m" : "%s";
CLG_logf(LOG.type, CLG_SEVERITY_INFO, "Notification", "", format, message);
}
}
else {
char debug_groups[512] = "";
GPU_debug_get_groups_names(sizeof(debug_groups), debug_groups);
CLG_Severity clog_severity;
switch (type) {
case GL_DEBUG_TYPE_ERROR:
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
fprintf(stderr, format, "\033[31;1mError\033[39m: ", debug_groups, message);
clog_severity = CLG_SEVERITY_ERROR;
break;
case GL_DEBUG_TYPE_PORTABILITY:
case GL_DEBUG_TYPE_PERFORMANCE:
case GL_DEBUG_TYPE_OTHER:
case GL_DEBUG_TYPE_MARKER: /* KHR has this, ARB does not */
default:
fprintf(stderr, format, "\033[33;1mWarning\033[39m: ", debug_groups, message);
clog_severity = CLG_SEVERITY_WARN;
break;
}
if (VERBOSE && severity == GL_DEBUG_SEVERITY_HIGH) {
/* Focus on error message. */
fprintf(stderr, "\033[2m");
BLI_system_backtrace(stderr);
fprintf(stderr, "\033[0m\n");
fflush(stderr);
if (((LOG.type->flag & CLG_FLAG_USE) && (LOG.type->level >= clog_severity))) {
CLG_logf(LOG.type, clog_severity, debug_groups, "", message);
if (severity == GL_DEBUG_SEVERITY_HIGH) {
/* Focus on error message. */
if (use_color) {
fprintf(stderr, "\033[2m");
}
BLI_system_backtrace(stderr);
if (use_color) {
fprintf(stderr, "\033[0m\n");
}
fflush(stderr);
}
}
}
}
@ -125,6 +138,8 @@ static void APIENTRY debug_callback(GLenum UNUSED(source),
/* This function needs to be called once per context. */
void init_gl_callbacks(void)
{
CLOG_ENSURE(&LOG);
char msg[256] = "";
const char format[] = "Successfully hooked OpenGL debug callback using %s";
@ -154,7 +169,7 @@ void init_gl_callbacks(void)
msg);
}
else {
fprintf(stderr, "GPUDebug: Failed to hook OpenGL debug callback. Use fallback debug layer.\n");
CLOG_STR_WARN(&LOG, "Failed to hook OpenGL debug callback. Use fallback debug layer.");
init_debug_layer();
}
}

View File

@ -134,13 +134,13 @@ GLuint GLShader::create_shader_stage(GLenum gl_stage, MutableSpan<const char *>
if (log[0] != '\0') {
switch (gl_stage) {
case GL_VERTEX_SHADER:
this->print_errors(sources, log, "VertShader");
this->print_log(sources, log, "VertShader", !status);
break;
case GL_GEOMETRY_SHADER:
this->print_errors(sources, log, "GeomShader");
this->print_log(sources, log, "GeomShader", !status);
break;
case GL_FRAGMENT_SHADER:
this->print_errors(sources, log, "FragShader");
this->print_log(sources, log, "FragShader", !status);
break;
}
}
@ -186,7 +186,7 @@ bool GLShader::finalize(void)
char log[5000];
glGetProgramInfoLog(shader_program_, sizeof(log), NULL, log);
Span<const char *> sources;
this->print_errors(sources, log, "Linking");
this->print_log(sources, log, "Linking", true);
return false;
}