| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2005 Blender Foundation. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup gpu | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-20 14:01:03 +10:00
										 |  |  | #include "BLI_compiler_attrs.h"
 | 
					
						
							| 
									
										
										
										
											2016-08-16 17:14:42 -04:00
										 |  |  | #include "BLI_utildefines.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | #include "BLI_sys_types.h"
 | 
					
						
							| 
									
										
										
										
											2016-08-16 17:14:42 -04:00
										 |  |  | #include "BLI_system.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "BKE_global.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "GPU_glew.h"
 | 
					
						
							|  |  |  | #include "GPU_debug.h"
 | 
					
						
							|  |  |  | #include "intern/gpu_private.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2015-02-23 18:09:28 +05:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 04:38:30 -05:00
										 |  |  | #ifndef __APPLE__ /* only non-Apple systems implement OpenGL debug callbacks */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 04:05:53 -05:00
										 |  |  | /* control whether we use older AMD_debug_output extension
 | 
					
						
							|  |  |  |  * some supported GPU + OS combos do not have the newer extensions */ | 
					
						
							| 
									
										
										
										
											2017-03-12 03:00:06 +11:00
										 |  |  | #  define LEGACY_DEBUG 1
 | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 17:18:13 -06:00
										 |  |  | /* Debug callbacks need the same calling convention as OpenGL functions. */ | 
					
						
							| 
									
										
										
										
											2017-03-12 03:00:06 +11:00
										 |  |  | #  if defined(_WIN32)
 | 
					
						
							|  |  |  | #    define APIENTRY __stdcall
 | 
					
						
							|  |  |  | #  else
 | 
					
						
							|  |  |  | #    define APIENTRY
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 02:40:04 +11:00
										 |  |  | static const char *source_name(GLenum source) | 
					
						
							| 
									
										
										
										
											2016-08-09 01:29:58 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   switch (source) { | 
					
						
							|  |  |  |     case GL_DEBUG_SOURCE_API: | 
					
						
							|  |  |  |       return "API"; | 
					
						
							|  |  |  |     case GL_DEBUG_SOURCE_WINDOW_SYSTEM: | 
					
						
							|  |  |  |       return "window system"; | 
					
						
							|  |  |  |     case GL_DEBUG_SOURCE_SHADER_COMPILER: | 
					
						
							|  |  |  |       return "shader compiler"; | 
					
						
							|  |  |  |     case GL_DEBUG_SOURCE_THIRD_PARTY: | 
					
						
							|  |  |  |       return "3rd party"; | 
					
						
							|  |  |  |     case GL_DEBUG_SOURCE_APPLICATION: | 
					
						
							|  |  |  |       return "application"; | 
					
						
							|  |  |  |     case GL_DEBUG_SOURCE_OTHER: | 
					
						
							|  |  |  |       return "other"; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       return "???"; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-09 01:29:58 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 02:40:04 +11:00
										 |  |  | static const char *message_type_name(GLenum message) | 
					
						
							| 
									
										
										
										
											2016-08-09 01:29:58 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   switch (message) { | 
					
						
							|  |  |  |     case GL_DEBUG_TYPE_ERROR: | 
					
						
							|  |  |  |       return "error"; | 
					
						
							|  |  |  |     case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: | 
					
						
							|  |  |  |       return "deprecated behavior"; | 
					
						
							|  |  |  |     case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: | 
					
						
							|  |  |  |       return "undefined behavior"; | 
					
						
							|  |  |  |     case GL_DEBUG_TYPE_PORTABILITY: | 
					
						
							|  |  |  |       return "portability"; | 
					
						
							|  |  |  |     case GL_DEBUG_TYPE_PERFORMANCE: | 
					
						
							|  |  |  |       return "performance"; | 
					
						
							|  |  |  |     case GL_DEBUG_TYPE_OTHER: | 
					
						
							|  |  |  |       return "other"; | 
					
						
							|  |  |  |     case GL_DEBUG_TYPE_MARKER: | 
					
						
							|  |  |  |       return "marker"; /* KHR has this, ARB does not */ | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       return "???"; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-09 01:29:58 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | static void APIENTRY gpu_debug_proc(GLenum source, | 
					
						
							|  |  |  |                                     GLenum type, | 
					
						
							|  |  |  |                                     GLuint UNUSED(id), | 
					
						
							|  |  |  |                                     GLenum severity, | 
					
						
							|  |  |  |                                     GLsizei UNUSED(length), | 
					
						
							|  |  |  |                                     const GLchar *message, | 
					
						
							|  |  |  |                                     const GLvoid *UNUSED(userParm)) | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   bool backtrace = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   switch (severity) { | 
					
						
							|  |  |  |     case GL_DEBUG_SEVERITY_HIGH: | 
					
						
							|  |  |  |       backtrace = true; | 
					
						
							|  |  |  |       ATTR_FALLTHROUGH; | 
					
						
							|  |  |  |     case GL_DEBUG_SEVERITY_MEDIUM: | 
					
						
							|  |  |  |     case GL_DEBUG_SEVERITY_LOW: | 
					
						
							|  |  |  |     case GL_DEBUG_SEVERITY_NOTIFICATION: /* KHR has this, ARB does not */ | 
					
						
							|  |  |  |       fprintf(stderr, "GL %s %s: %s\n", source_name(source), message_type_name(type), message); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (backtrace) { | 
					
						
							|  |  |  |     BLI_system_backtrace(stderr); | 
					
						
							|  |  |  |     fflush(stderr); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 03:00:06 +11:00
										 |  |  | #  if LEGACY_DEBUG
 | 
					
						
							| 
									
										
										
										
											2016-11-12 17:18:13 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 03:00:06 +11:00
										 |  |  | static const char *category_name_amd(GLenum category) | 
					
						
							| 
									
										
										
										
											2016-11-12 17:18:13 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   switch (category) { | 
					
						
							|  |  |  |     case GL_DEBUG_CATEGORY_API_ERROR_AMD: | 
					
						
							|  |  |  |       return "API error"; | 
					
						
							|  |  |  |     case GL_DEBUG_CATEGORY_WINDOW_SYSTEM_AMD: | 
					
						
							|  |  |  |       return "window system"; | 
					
						
							|  |  |  |     case GL_DEBUG_CATEGORY_DEPRECATION_AMD: | 
					
						
							|  |  |  |       return "deprecated behavior"; | 
					
						
							|  |  |  |     case GL_DEBUG_CATEGORY_UNDEFINED_BEHAVIOR_AMD: | 
					
						
							|  |  |  |       return "undefined behavior"; | 
					
						
							|  |  |  |     case GL_DEBUG_CATEGORY_PERFORMANCE_AMD: | 
					
						
							|  |  |  |       return "performance"; | 
					
						
							|  |  |  |     case GL_DEBUG_CATEGORY_SHADER_COMPILER_AMD: | 
					
						
							|  |  |  |       return "shader compiler"; | 
					
						
							|  |  |  |     case GL_DEBUG_CATEGORY_APPLICATION_AMD: | 
					
						
							|  |  |  |       return "application"; | 
					
						
							|  |  |  |     case GL_DEBUG_CATEGORY_OTHER_AMD: | 
					
						
							|  |  |  |       return "other"; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       return "???"; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-11-12 17:18:13 -06:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | static void APIENTRY gpu_debug_proc_amd(GLuint UNUSED(id), | 
					
						
							|  |  |  |                                         GLenum category, | 
					
						
							|  |  |  |                                         GLenum severity, | 
					
						
							|  |  |  |                                         GLsizei UNUSED(length), | 
					
						
							|  |  |  |                                         const GLchar *message, | 
					
						
							|  |  |  |                                         GLvoid *UNUSED(userParm)) | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   bool backtrace = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   switch (severity) { | 
					
						
							|  |  |  |     case GL_DEBUG_SEVERITY_HIGH: | 
					
						
							|  |  |  |       backtrace = true; | 
					
						
							|  |  |  |       ATTR_FALLTHROUGH; | 
					
						
							|  |  |  |     case GL_DEBUG_SEVERITY_MEDIUM: | 
					
						
							|  |  |  |     case GL_DEBUG_SEVERITY_LOW: | 
					
						
							|  |  |  |       fprintf(stderr, "GL %s: %s\n", category_name_amd(category), message); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (backtrace) { | 
					
						
							|  |  |  |     BLI_system_backtrace(stderr); | 
					
						
							|  |  |  |     fflush(stderr); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-12 03:00:06 +11:00
										 |  |  | #  endif /* LEGACY_DEBUG */
 | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 03:00:06 +11:00
										 |  |  | #  undef APIENTRY
 | 
					
						
							| 
									
										
										
										
											2017-02-28 04:38:30 -05:00
										 |  |  | #endif /* not Apple */
 | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void gpu_debug_init(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-02-28 04:38:30 -05:00
										 |  |  | #ifdef __APPLE__
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   fprintf(stderr, "OpenGL debug callback is not available on Apple.\n"); | 
					
						
							| 
									
										
										
										
											2017-02-28 04:38:30 -05:00
										 |  |  | #else /* not Apple */
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   const char success[] = "Successfully hooked OpenGL debug callback."; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (GLEW_VERSION_4_3 || GLEW_KHR_debug) { | 
					
						
							|  |  |  |     fprintf(stderr, | 
					
						
							|  |  |  |             "Using %s\n", | 
					
						
							|  |  |  |             GLEW_VERSION_4_3 ? "OpenGL 4.3 debug facilities" : "KHR_debug extension"); | 
					
						
							|  |  |  |     glEnable(GL_DEBUG_OUTPUT); | 
					
						
							|  |  |  |     glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); | 
					
						
							|  |  |  |     glDebugMessageCallback((GLDEBUGPROC)gpu_debug_proc, NULL); | 
					
						
							|  |  |  |     glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE); | 
					
						
							|  |  |  |     GPU_string_marker(success); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else if (GLEW_ARB_debug_output) { | 
					
						
							|  |  |  |     fprintf(stderr, "Using ARB_debug_output extension\n"); | 
					
						
							|  |  |  |     glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); | 
					
						
							|  |  |  |     glDebugMessageCallbackARB((GLDEBUGPROCARB)gpu_debug_proc, NULL); | 
					
						
							|  |  |  |     glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE); | 
					
						
							|  |  |  |     GPU_string_marker(success); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-03-12 03:00:06 +11:00
										 |  |  | #  if LEGACY_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   else if (GLEW_AMD_debug_output) { | 
					
						
							|  |  |  |     fprintf(stderr, "Using AMD_debug_output extension\n"); | 
					
						
							|  |  |  |     glDebugMessageCallbackAMD(gpu_debug_proc_amd, NULL); | 
					
						
							|  |  |  |     glDebugMessageEnableAMD(GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE); | 
					
						
							|  |  |  |     GPU_string_marker(success); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-03-12 03:00:06 +11:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   else { | 
					
						
							|  |  |  |     fprintf(stderr, "Failed to hook OpenGL debug callback.\n"); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-02-28 04:38:30 -05:00
										 |  |  | #endif /* not Apple */
 | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void gpu_debug_exit(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-02-28 04:38:30 -05:00
										 |  |  | #ifndef __APPLE__
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   if (GLEW_VERSION_4_3 || GLEW_KHR_debug) { | 
					
						
							|  |  |  |     glDebugMessageCallback(NULL, NULL); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else if (GLEW_ARB_debug_output) { | 
					
						
							|  |  |  |     glDebugMessageCallbackARB(NULL, NULL); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-03-12 03:00:06 +11:00
										 |  |  | #  if LEGACY_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   else if (GLEW_AMD_debug_output) { | 
					
						
							|  |  |  |     glDebugMessageCallbackAMD(NULL, NULL); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-03-12 03:00:06 +11:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-09 01:29:58 -04:00
										 |  |  | void GPU_string_marker(const char *buf) | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-02-28 04:38:30 -05:00
										 |  |  | #ifdef __APPLE__
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   UNUSED_VARS(buf); | 
					
						
							| 
									
										
										
										
											2017-02-28 04:38:30 -05:00
										 |  |  | #else /* not Apple */
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   if (GLEW_VERSION_4_3 || GLEW_KHR_debug) { | 
					
						
							|  |  |  |     glDebugMessageInsert(GL_DEBUG_SOURCE_APPLICATION, | 
					
						
							|  |  |  |                          GL_DEBUG_TYPE_MARKER, | 
					
						
							|  |  |  |                          0, | 
					
						
							|  |  |  |                          GL_DEBUG_SEVERITY_NOTIFICATION, | 
					
						
							|  |  |  |                          -1, | 
					
						
							|  |  |  |                          buf); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else if (GLEW_ARB_debug_output) { | 
					
						
							|  |  |  |     glDebugMessageInsertARB(GL_DEBUG_SOURCE_APPLICATION_ARB, | 
					
						
							|  |  |  |                             GL_DEBUG_TYPE_OTHER_ARB, | 
					
						
							|  |  |  |                             0, | 
					
						
							|  |  |  |                             GL_DEBUG_SEVERITY_LOW_ARB, | 
					
						
							|  |  |  |                             -1, | 
					
						
							|  |  |  |                             buf); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-03-12 03:00:06 +11:00
										 |  |  | #  if LEGACY_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   else if (GLEW_AMD_debug_output) { | 
					
						
							|  |  |  |     glDebugMessageInsertAMD( | 
					
						
							|  |  |  |         GL_DEBUG_CATEGORY_APPLICATION_AMD, GL_DEBUG_SEVERITY_LOW_AMD, 0, 0, buf); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-03-12 03:00:06 +11:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2017-02-28 04:38:30 -05:00
										 |  |  | #endif /* not Apple */
 | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_print_error_debug(const char *str) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-22 09:32:37 +10:00
										 |  |  |   if (G.debug & G_DEBUG) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     fprintf(stderr, "GPU: %s\n", str); | 
					
						
							| 
									
										
										
										
											2019-04-22 09:32:37 +10:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-02-16 21:19:12 +01:00
										 |  |  | } |