2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2016 Brian Paul, et al All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
29 #include "debug_output.h"
35 #include "util/hash_table.h"
36 #include "util/list.h"
37 #include "util/u_memory.h"
40 static simple_mtx_t DynamicIDMutex
= _SIMPLE_MTX_INITIALIZER_NP
;
41 static GLuint NextDynamicID
= 1;
45 * A namespace element.
47 struct gl_debug_element
49 struct list_head link
;
52 /* at which severity levels (mesa_debug_severity) is the message enabled */
57 struct gl_debug_namespace
59 struct list_head Elements
;
60 GLbitfield DefaultState
;
64 struct gl_debug_group
{
65 struct gl_debug_namespace Namespaces
[MESA_DEBUG_SOURCE_COUNT
][MESA_DEBUG_TYPE_COUNT
];
70 * An error, warning, or other piece of debug information for an application
71 * to consume via GL_ARB_debug_output/GL_KHR_debug.
73 struct gl_debug_message
75 enum mesa_debug_source source
;
76 enum mesa_debug_type type
;
78 enum mesa_debug_severity severity
;
79 /* length as given by the user - if message was explicitly null terminated,
80 * length can be negative */
87 * Debug message log. It works like a ring buffer.
90 struct gl_debug_message Messages
[MAX_DEBUG_LOGGED_MESSAGES
];
99 const void *CallbackData
;
100 GLboolean SyncOutput
;
101 GLboolean DebugOutput
;
102 GLboolean LogToStderr
;
104 struct gl_debug_group
*Groups
[MAX_DEBUG_GROUP_STACK_DEPTH
];
105 struct gl_debug_message GroupMessages
[MAX_DEBUG_GROUP_STACK_DEPTH
];
106 GLint CurrentGroup
; // GroupStackDepth - 1
108 struct gl_debug_log Log
;
112 static char out_of_memory
[] = "Debugging error: out of memory";
114 static const GLenum debug_source_enums
[] = {
116 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
117 GL_DEBUG_SOURCE_SHADER_COMPILER
,
118 GL_DEBUG_SOURCE_THIRD_PARTY
,
119 GL_DEBUG_SOURCE_APPLICATION
,
120 GL_DEBUG_SOURCE_OTHER
,
123 static const GLenum debug_type_enums
[] = {
125 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
126 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
127 GL_DEBUG_TYPE_PORTABILITY
,
128 GL_DEBUG_TYPE_PERFORMANCE
,
130 GL_DEBUG_TYPE_MARKER
,
131 GL_DEBUG_TYPE_PUSH_GROUP
,
132 GL_DEBUG_TYPE_POP_GROUP
,
135 static const GLenum debug_severity_enums
[] = {
136 GL_DEBUG_SEVERITY_LOW
,
137 GL_DEBUG_SEVERITY_MEDIUM
,
138 GL_DEBUG_SEVERITY_HIGH
,
139 GL_DEBUG_SEVERITY_NOTIFICATION
,
143 static enum mesa_debug_source
144 gl_enum_to_debug_source(GLenum e
)
148 for (i
= 0; i
< ARRAY_SIZE(debug_source_enums
); i
++) {
149 if (debug_source_enums
[i
] == e
)
155 static enum mesa_debug_type
156 gl_enum_to_debug_type(GLenum e
)
160 for (i
= 0; i
< ARRAY_SIZE(debug_type_enums
); i
++) {
161 if (debug_type_enums
[i
] == e
)
167 static enum mesa_debug_severity
168 gl_enum_to_debug_severity(GLenum e
)
172 for (i
= 0; i
< ARRAY_SIZE(debug_severity_enums
); i
++) {
173 if (debug_severity_enums
[i
] == e
)
181 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
184 * The GL API has this "ID" mechanism, where the intention is to allow a
185 * client to filter in/out messages based on source, type, and ID. Of course,
186 * building a giant enum list of all debug output messages that Mesa might
187 * generate is ridiculous, so instead we have our caller pass us a pointer to
188 * static storage where the ID should get stored. This ID will be shared
189 * across all contexts for that message (which seems like a desirable
190 * property, even if it's not expected by the spec), but note that it won't be
191 * the same between executions if messages aren't generated in the same order.
194 _mesa_debug_get_id(GLuint
*id
)
197 simple_mtx_lock(&DynamicIDMutex
);
199 *id
= NextDynamicID
++;
200 simple_mtx_unlock(&DynamicIDMutex
);
205 debug_message_clear(struct gl_debug_message
*msg
)
207 if (msg
->message
!= (char*)out_of_memory
)
214 debug_message_store(struct gl_debug_message
*msg
,
215 enum mesa_debug_source source
,
216 enum mesa_debug_type type
, GLuint id
,
217 enum mesa_debug_severity severity
,
218 GLsizei len
, const char *buf
)
220 GLsizei length
= len
;
222 assert(!msg
->message
&& !msg
->length
);
225 length
= strlen(buf
);
227 msg
->message
= malloc(length
+1);
229 (void) strncpy(msg
->message
, buf
, (size_t)length
);
230 msg
->message
[length
] = '\0';
233 msg
->source
= source
;
236 msg
->severity
= severity
;
238 static GLuint oom_msg_id
= 0;
239 _mesa_debug_get_id(&oom_msg_id
);
242 msg
->message
= out_of_memory
;
244 msg
->source
= MESA_DEBUG_SOURCE_OTHER
;
245 msg
->type
= MESA_DEBUG_TYPE_ERROR
;
246 msg
->id
= oom_msg_id
;
247 msg
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
252 debug_namespace_init(struct gl_debug_namespace
*ns
)
254 list_inithead(&ns
->Elements
);
256 /* Enable all the messages with severity HIGH or MEDIUM by default */
257 ns
->DefaultState
= (1 << MESA_DEBUG_SEVERITY_MEDIUM
) |
258 (1 << MESA_DEBUG_SEVERITY_HIGH
) |
259 (1 << MESA_DEBUG_SEVERITY_NOTIFICATION
);
263 debug_namespace_clear(struct gl_debug_namespace
*ns
)
265 list_for_each_entry_safe(struct gl_debug_element
, elem
, &ns
->Elements
, link
)
270 debug_namespace_copy(struct gl_debug_namespace
*dst
,
271 const struct gl_debug_namespace
*src
)
273 dst
->DefaultState
= src
->DefaultState
;
275 list_inithead(&dst
->Elements
);
276 list_for_each_entry(struct gl_debug_element
, elem
, &src
->Elements
, link
) {
277 struct gl_debug_element
*copy
;
279 copy
= malloc(sizeof(*copy
));
281 debug_namespace_clear(dst
);
286 copy
->State
= elem
->State
;
287 list_addtail(©
->link
, &dst
->Elements
);
294 * Set the state of \p id in the namespace.
297 debug_namespace_set(struct gl_debug_namespace
*ns
,
298 GLuint id
, bool enabled
)
300 const uint32_t state
= (enabled
) ?
301 ((1 << MESA_DEBUG_SEVERITY_COUNT
) - 1) : 0;
302 struct gl_debug_element
*elem
= NULL
;
304 /* find the element */
305 list_for_each_entry(struct gl_debug_element
, tmp
, &ns
->Elements
, link
) {
312 /* we do not need the element if it has the default state */
313 if (ns
->DefaultState
== state
) {
315 list_del(&elem
->link
);
322 elem
= malloc(sizeof(*elem
));
327 list_addtail(&elem
->link
, &ns
->Elements
);
336 * Set the default state of the namespace for \p severity. When \p severity
337 * is MESA_DEBUG_SEVERITY_COUNT, the default values for all severities are
341 debug_namespace_set_all(struct gl_debug_namespace
*ns
,
342 enum mesa_debug_severity severity
,
347 /* set all elements to the same state */
348 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
349 ns
->DefaultState
= (enabled
) ? ((1 << severity
) - 1) : 0;
350 debug_namespace_clear(ns
);
351 list_inithead(&ns
->Elements
);
355 mask
= 1 << severity
;
356 val
= (enabled
) ? mask
: 0;
358 ns
->DefaultState
= (ns
->DefaultState
& ~mask
) | val
;
360 list_for_each_entry_safe(struct gl_debug_element
, elem
, &ns
->Elements
,
362 elem
->State
= (elem
->State
& ~mask
) | val
;
363 if (elem
->State
== ns
->DefaultState
) {
364 list_del(&elem
->link
);
371 * Get the state of \p id in the namespace.
374 debug_namespace_get(const struct gl_debug_namespace
*ns
, GLuint id
,
375 enum mesa_debug_severity severity
)
379 state
= ns
->DefaultState
;
380 list_for_each_entry(struct gl_debug_element
, elem
, &ns
->Elements
, link
) {
381 if (elem
->ID
== id
) {
387 return (state
& (1 << severity
));
391 * Allocate and initialize context debug state.
393 static struct gl_debug_state
*
396 struct gl_debug_state
*debug
;
399 debug
= CALLOC_STRUCT(gl_debug_state
);
403 debug
->Groups
[0] = malloc(sizeof(*debug
->Groups
[0]));
404 if (!debug
->Groups
[0]) {
409 /* Initialize state for filtering known debug messages. */
410 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
411 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
412 debug_namespace_init(&debug
->Groups
[0]->Namespaces
[s
][t
]);
419 * Return true if the top debug group points to the group below it.
422 debug_is_group_read_only(const struct gl_debug_state
*debug
)
424 const GLint gstack
= debug
->CurrentGroup
;
425 return (gstack
> 0 && debug
->Groups
[gstack
] == debug
->Groups
[gstack
- 1]);
429 * Make the top debug group writable.
432 debug_make_group_writable(struct gl_debug_state
*debug
)
434 const GLint gstack
= debug
->CurrentGroup
;
435 const struct gl_debug_group
*src
= debug
->Groups
[gstack
];
436 struct gl_debug_group
*dst
;
439 if (!debug_is_group_read_only(debug
))
442 dst
= malloc(sizeof(*dst
));
446 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
447 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
448 if (!debug_namespace_copy(&dst
->Namespaces
[s
][t
],
449 &src
->Namespaces
[s
][t
])) {
451 for (t
= t
- 1; t
>= 0; t
--)
452 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
453 for (s
= s
- 1; s
>= 0; s
--) {
454 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
455 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
463 debug
->Groups
[gstack
] = dst
;
469 * Free the top debug group.
472 debug_clear_group(struct gl_debug_state
*debug
)
474 const GLint gstack
= debug
->CurrentGroup
;
476 if (!debug_is_group_read_only(debug
)) {
477 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
480 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
481 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
482 debug_namespace_clear(&grp
->Namespaces
[s
][t
]);
488 debug
->Groups
[gstack
] = NULL
;
492 * Delete the oldest debug messages out of the log.
495 debug_delete_messages(struct gl_debug_state
*debug
, int count
)
497 struct gl_debug_log
*log
= &debug
->Log
;
499 if (count
> log
->NumMessages
)
500 count
= log
->NumMessages
;
503 struct gl_debug_message
*msg
= &log
->Messages
[log
->NextMessage
];
505 debug_message_clear(msg
);
509 log
->NextMessage
%= MAX_DEBUG_LOGGED_MESSAGES
;
514 * Loop through debug group stack tearing down states for
515 * filtering debug messages. Then free debug output state.
518 debug_destroy(struct gl_debug_state
*debug
)
520 while (debug
->CurrentGroup
> 0) {
521 debug_clear_group(debug
);
522 debug
->CurrentGroup
--;
525 debug_clear_group(debug
);
526 debug_delete_messages(debug
, debug
->Log
.NumMessages
);
531 * Sets the state of the given message source/type/ID tuple.
534 debug_set_message_enable(struct gl_debug_state
*debug
,
535 enum mesa_debug_source source
,
536 enum mesa_debug_type type
,
537 GLuint id
, GLboolean enabled
)
539 const GLint gstack
= debug
->CurrentGroup
;
540 struct gl_debug_namespace
*ns
;
542 debug_make_group_writable(debug
);
543 ns
= &debug
->Groups
[gstack
]->Namespaces
[source
][type
];
545 debug_namespace_set(ns
, id
, enabled
);
549 * Set the state of all message IDs found in the given intersection of
550 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
551 * GL_DONT_CARE (include all messages in the class).
553 * This requires both setting the state of all previously seen message
554 * IDs in the hash table, and setting the default state for all
555 * applicable combinations of source/type/severity, so that all the
556 * yet-unknown message IDs that may be used in the future will be
557 * impacted as if they were already known.
560 debug_set_message_enable_all(struct gl_debug_state
*debug
,
561 enum mesa_debug_source source
,
562 enum mesa_debug_type type
,
563 enum mesa_debug_severity severity
,
566 const GLint gstack
= debug
->CurrentGroup
;
567 int s
, t
, smax
, tmax
;
569 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
571 smax
= MESA_DEBUG_SOURCE_COUNT
;
576 if (type
== MESA_DEBUG_TYPE_COUNT
) {
578 tmax
= MESA_DEBUG_TYPE_COUNT
;
583 debug_make_group_writable(debug
);
585 for (s
= source
; s
< smax
; s
++) {
586 for (t
= type
; t
< tmax
; t
++) {
587 struct gl_debug_namespace
*nspace
=
588 &debug
->Groups
[gstack
]->Namespaces
[s
][t
];
589 debug_namespace_set_all(nspace
, severity
, enabled
);
595 * Returns if the given message source/type/ID tuple is enabled.
598 _mesa_debug_is_message_enabled(const struct gl_debug_state
*debug
,
599 enum mesa_debug_source source
,
600 enum mesa_debug_type type
,
602 enum mesa_debug_severity severity
)
604 const GLint gstack
= debug
->CurrentGroup
;
605 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
606 struct gl_debug_namespace
*nspace
= &grp
->Namespaces
[source
][type
];
608 if (!debug
->DebugOutput
)
611 return debug_namespace_get(nspace
, id
, severity
);
615 * 'buf' is not necessarily a null-terminated string. When logging, copy
616 * 'len' characters from it, store them in a new, null-terminated string,
617 * and remember the number of bytes used by that string, *including*
618 * the null terminator this time.
621 debug_log_message(struct gl_debug_state
*debug
,
622 enum mesa_debug_source source
,
623 enum mesa_debug_type type
, GLuint id
,
624 enum mesa_debug_severity severity
,
625 GLsizei len
, const char *buf
)
627 struct gl_debug_log
*log
= &debug
->Log
;
629 struct gl_debug_message
*emptySlot
;
631 if (debug
->LogToStderr
) {
632 _mesa_log("Mesa debug output: %.*s\n", len
, buf
);
635 assert(len
< MAX_DEBUG_MESSAGE_LENGTH
);
637 if (log
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
640 nextEmpty
= (log
->NextMessage
+ log
->NumMessages
)
641 % MAX_DEBUG_LOGGED_MESSAGES
;
642 emptySlot
= &log
->Messages
[nextEmpty
];
644 debug_message_store(emptySlot
, source
, type
,
645 id
, severity
, len
, buf
);
651 * Return the oldest debug message out of the log.
653 static const struct gl_debug_message
*
654 debug_fetch_message(const struct gl_debug_state
*debug
)
656 const struct gl_debug_log
*log
= &debug
->Log
;
658 return (log
->NumMessages
) ? &log
->Messages
[log
->NextMessage
] : NULL
;
661 static struct gl_debug_message
*
662 debug_get_group_message(struct gl_debug_state
*debug
)
664 return &debug
->GroupMessages
[debug
->CurrentGroup
];
668 debug_push_group(struct gl_debug_state
*debug
)
670 const GLint gstack
= debug
->CurrentGroup
;
672 /* just point to the previous stack */
673 debug
->Groups
[gstack
+ 1] = debug
->Groups
[gstack
];
674 debug
->CurrentGroup
++;
678 debug_pop_group(struct gl_debug_state
*debug
)
680 debug_clear_group(debug
);
681 debug
->CurrentGroup
--;
686 * Lock and return debug state for the context. The debug state will be
687 * allocated and initialized upon the first call. When NULL is returned, the
688 * debug state is not locked.
690 static struct gl_debug_state
*
691 _mesa_lock_debug_state(struct gl_context
*ctx
)
693 simple_mtx_lock(&ctx
->DebugMutex
);
696 ctx
->Debug
= debug_create();
698 GET_CURRENT_CONTEXT(cur
);
699 simple_mtx_unlock(&ctx
->DebugMutex
);
702 * This function may be called from other threads. When that is the
703 * case, we cannot record this OOM error.
706 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
716 _mesa_unlock_debug_state(struct gl_context
*ctx
)
718 simple_mtx_unlock(&ctx
->DebugMutex
);
722 * Set the integer debug state specified by \p pname. This can be called from
723 * _mesa_set_enable for example.
726 _mesa_set_debug_state_int(struct gl_context
*ctx
, GLenum pname
, GLint val
)
728 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
734 case GL_DEBUG_OUTPUT
:
735 debug
->DebugOutput
= (val
!= 0);
737 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
738 debug
->SyncOutput
= (val
!= 0);
741 assert(!"unknown debug output param");
745 _mesa_unlock_debug_state(ctx
);
751 * Query the integer debug state specified by \p pname. This can be called
752 * _mesa_GetIntegerv for example.
755 _mesa_get_debug_state_int(struct gl_context
*ctx
, GLenum pname
)
759 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
764 case GL_DEBUG_OUTPUT
:
765 val
= debug
->DebugOutput
;
767 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
768 val
= debug
->SyncOutput
;
770 case GL_DEBUG_LOGGED_MESSAGES
:
771 val
= debug
->Log
.NumMessages
;
773 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH
:
774 val
= (debug
->Log
.NumMessages
) ?
775 debug
->Log
.Messages
[debug
->Log
.NextMessage
].length
+ 1 : 0;
777 case GL_DEBUG_GROUP_STACK_DEPTH
:
778 val
= debug
->CurrentGroup
+ 1;
781 assert(!"unknown debug output param");
786 _mesa_unlock_debug_state(ctx
);
792 * Query the pointer debug state specified by \p pname. This can be called
793 * _mesa_GetPointerv for example.
796 _mesa_get_debug_state_ptr(struct gl_context
*ctx
, GLenum pname
)
799 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
805 case GL_DEBUG_CALLBACK_FUNCTION_ARB
:
806 val
= (void *) debug
->Callback
;
808 case GL_DEBUG_CALLBACK_USER_PARAM_ARB
:
809 val
= (void *) debug
->CallbackData
;
812 assert(!"unknown debug output param");
817 _mesa_unlock_debug_state(ctx
);
823 * Insert a debug message. The mutex is assumed to be locked, and will be
824 * unlocked by this call.
827 log_msg_locked_and_unlock(struct gl_context
*ctx
,
828 enum mesa_debug_source source
,
829 enum mesa_debug_type type
, GLuint id
,
830 enum mesa_debug_severity severity
,
831 GLint len
, const char *buf
)
833 struct gl_debug_state
*debug
= ctx
->Debug
;
835 if (!_mesa_debug_is_message_enabled(debug
, source
, type
, id
, severity
)) {
836 _mesa_unlock_debug_state(ctx
);
840 if (ctx
->Debug
->Callback
) {
841 /* Call the user's callback function */
842 GLenum gl_source
= debug_source_enums
[source
];
843 GLenum gl_type
= debug_type_enums
[type
];
844 GLenum gl_severity
= debug_severity_enums
[severity
];
845 GLDEBUGPROC callback
= ctx
->Debug
->Callback
;
846 const void *data
= ctx
->Debug
->CallbackData
;
849 * When ctx->Debug->SyncOutput is GL_FALSE, the client is prepared for
850 * unsynchronous calls. When it is GL_TRUE, we will not spawn threads.
851 * In either case, we can call the callback unlocked.
853 _mesa_unlock_debug_state(ctx
);
854 callback(gl_source
, gl_type
, id
, gl_severity
, len
, buf
, data
);
857 /* add debug message to queue */
858 debug_log_message(ctx
->Debug
, source
, type
, id
, severity
, len
, buf
);
859 _mesa_unlock_debug_state(ctx
);
864 * Log a client or driver debug message.
867 _mesa_log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
868 enum mesa_debug_type type
, GLuint id
,
869 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
871 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
876 log_msg_locked_and_unlock(ctx
, source
, type
, id
, severity
, len
, buf
);
881 * Verify that source, type, and severity are valid enums.
883 * The 'caller' param is used for handling values available
884 * only in glDebugMessageInsert or glDebugMessageControl
887 validate_params(struct gl_context
*ctx
, unsigned caller
,
888 const char *callerstr
, GLenum source
, GLenum type
,
894 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
895 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
897 case GL_DEBUG_SOURCE_API_ARB
:
898 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
899 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
900 case GL_DEBUG_SOURCE_OTHER_ARB
:
901 if (caller
!= INSERT
)
906 if (caller
== CONTROL
)
915 case GL_DEBUG_TYPE_ERROR_ARB
:
916 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
917 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
918 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
919 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
920 case GL_DEBUG_TYPE_OTHER_ARB
:
921 case GL_DEBUG_TYPE_MARKER
:
922 case GL_DEBUG_TYPE_PUSH_GROUP
:
923 case GL_DEBUG_TYPE_POP_GROUP
:
926 if (caller
== CONTROL
)
935 case GL_DEBUG_SEVERITY_HIGH_ARB
:
936 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
937 case GL_DEBUG_SEVERITY_LOW_ARB
:
938 case GL_DEBUG_SEVERITY_NOTIFICATION
:
941 if (caller
== CONTROL
)
951 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
952 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
953 source
, type
, severity
);
960 validate_length(struct gl_context
*ctx
, const char *callerstr
, GLsizei length
,
965 GLsizei len
= strlen(buf
);
967 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
968 _mesa_error(ctx
, GL_INVALID_VALUE
,
969 "%s(null terminated string length=%d, is not less than "
970 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, len
,
971 MAX_DEBUG_MESSAGE_LENGTH
);
976 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
977 _mesa_error(ctx
, GL_INVALID_VALUE
,
978 "%s(length=%d, which is not less than "
979 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
980 MAX_DEBUG_MESSAGE_LENGTH
);
989 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
990 GLenum severity
, GLint length
,
993 GET_CURRENT_CONTEXT(ctx
);
994 const char *callerstr
;
996 if (_mesa_is_desktop_gl(ctx
))
997 callerstr
= "glDebugMessageInsert";
999 callerstr
= "glDebugMessageInsertKHR";
1001 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
1002 return; /* GL_INVALID_ENUM */
1004 if (!validate_length(ctx
, callerstr
, length
, buf
))
1005 return; /* GL_INVALID_VALUE */
1007 /* if length not specified, string will be null terminated: */
1009 length
= strlen(buf
);
1011 _mesa_log_msg(ctx
, gl_enum_to_debug_source(source
),
1012 gl_enum_to_debug_type(type
), id
,
1013 gl_enum_to_debug_severity(severity
),
1016 if (type
== GL_DEBUG_TYPE_MARKER
&& ctx
->Driver
.EmitStringMarker
) {
1017 ctx
->Driver
.EmitStringMarker(ctx
, buf
, length
);
1023 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
1024 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
1025 GLsizei
*lengths
, GLchar
*messageLog
)
1027 GET_CURRENT_CONTEXT(ctx
);
1028 struct gl_debug_state
*debug
;
1029 const char *callerstr
;
1032 if (_mesa_is_desktop_gl(ctx
))
1033 callerstr
= "glGetDebugMessageLog";
1035 callerstr
= "glGetDebugMessageLogKHR";
1041 _mesa_error(ctx
, GL_INVALID_VALUE
,
1042 "%s(logSize=%d : logSize must not be negative)",
1043 callerstr
, logSize
);
1047 debug
= _mesa_lock_debug_state(ctx
);
1051 for (ret
= 0; ret
< count
; ret
++) {
1052 const struct gl_debug_message
*msg
= debug_fetch_message(debug
);
1060 len
= strlen(msg
->message
);
1062 if (logSize
< len
+1 && messageLog
!= NULL
)
1066 assert(msg
->message
[len
] == '\0');
1067 (void) strncpy(messageLog
, msg
->message
, (size_t)len
+1);
1069 messageLog
+= len
+1;
1076 *severities
++ = debug_severity_enums
[msg
->severity
];
1078 *sources
++ = debug_source_enums
[msg
->source
];
1080 *types
++ = debug_type_enums
[msg
->type
];
1084 debug_delete_messages(debug
, 1);
1087 _mesa_unlock_debug_state(ctx
);
1094 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
1095 GLenum gl_severity
, GLsizei count
,
1096 const GLuint
*ids
, GLboolean enabled
)
1098 GET_CURRENT_CONTEXT(ctx
);
1099 enum mesa_debug_source source
= gl_enum_to_debug_source(gl_source
);
1100 enum mesa_debug_type type
= gl_enum_to_debug_type(gl_type
);
1101 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(gl_severity
);
1102 const char *callerstr
;
1103 struct gl_debug_state
*debug
;
1105 if (_mesa_is_desktop_gl(ctx
))
1106 callerstr
= "glDebugMessageControl";
1108 callerstr
= "glDebugMessageControlKHR";
1111 _mesa_error(ctx
, GL_INVALID_VALUE
,
1112 "%s(count=%d : count must not be negative)", callerstr
,
1117 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
1119 return; /* GL_INVALID_ENUM */
1121 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
1122 || gl_source
== GL_DONT_CARE
)) {
1123 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1124 "%s(When passing an array of ids, severity must be"
1125 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
1130 debug
= _mesa_lock_debug_state(ctx
);
1136 for (i
= 0; i
< count
; i
++)
1137 debug_set_message_enable(debug
, source
, type
, ids
[i
], enabled
);
1140 debug_set_message_enable_all(debug
, source
, type
, severity
, enabled
);
1143 _mesa_unlock_debug_state(ctx
);
1148 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
1150 GET_CURRENT_CONTEXT(ctx
);
1151 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
1153 debug
->Callback
= callback
;
1154 debug
->CallbackData
= userParam
;
1155 _mesa_unlock_debug_state(ctx
);
1161 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
1162 const GLchar
*message
)
1164 GET_CURRENT_CONTEXT(ctx
);
1165 const char *callerstr
;
1166 struct gl_debug_state
*debug
;
1167 struct gl_debug_message
*emptySlot
;
1169 if (_mesa_is_desktop_gl(ctx
))
1170 callerstr
= "glPushDebugGroup";
1172 callerstr
= "glPushDebugGroupKHR";
1175 case GL_DEBUG_SOURCE_APPLICATION
:
1176 case GL_DEBUG_SOURCE_THIRD_PARTY
:
1179 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
1180 "(source=0x%x)", callerstr
, source
);
1184 if (!validate_length(ctx
, callerstr
, length
, message
))
1185 return; /* GL_INVALID_VALUE */
1188 length
= strlen(message
);
1190 debug
= _mesa_lock_debug_state(ctx
);
1194 if (debug
->CurrentGroup
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
1195 _mesa_unlock_debug_state(ctx
);
1196 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
1200 /* pop reuses the message details from push so we store this */
1201 emptySlot
= debug_get_group_message(debug
);
1202 debug_message_store(emptySlot
,
1203 gl_enum_to_debug_source(source
),
1204 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
1206 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1209 debug_push_group(debug
);
1211 log_msg_locked_and_unlock(ctx
,
1212 gl_enum_to_debug_source(source
),
1213 MESA_DEBUG_TYPE_PUSH_GROUP
, id
,
1214 MESA_DEBUG_SEVERITY_NOTIFICATION
, length
,
1220 _mesa_PopDebugGroup(void)
1222 GET_CURRENT_CONTEXT(ctx
);
1223 const char *callerstr
;
1224 struct gl_debug_state
*debug
;
1225 struct gl_debug_message
*gdmessage
, msg
;
1227 if (_mesa_is_desktop_gl(ctx
))
1228 callerstr
= "glPopDebugGroup";
1230 callerstr
= "glPopDebugGroupKHR";
1232 debug
= _mesa_lock_debug_state(ctx
);
1236 if (debug
->CurrentGroup
<= 0) {
1237 _mesa_unlock_debug_state(ctx
);
1238 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
1242 debug_pop_group(debug
);
1244 /* make a shallow copy */
1245 gdmessage
= debug_get_group_message(debug
);
1247 gdmessage
->message
= NULL
;
1248 gdmessage
->length
= 0;
1250 log_msg_locked_and_unlock(ctx
,
1252 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
1254 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1255 msg
.length
, msg
.message
);
1257 debug_message_clear(&msg
);
1262 _mesa_init_debug_output(struct gl_context
*ctx
)
1264 simple_mtx_init(&ctx
->DebugMutex
, mtx_plain
);
1266 if (MESA_DEBUG_FLAGS
& DEBUG_CONTEXT
) {
1267 /* If the MESA_DEBUG env is set to "context", we'll turn on the
1268 * GL_CONTEXT_FLAG_DEBUG_BIT context flag and log debug output
1269 * messages to stderr (or whatever MESA_LOG_FILE points at).
1271 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
1275 debug
->DebugOutput
= GL_TRUE
;
1276 debug
->LogToStderr
= GL_TRUE
;
1277 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
1278 _mesa_unlock_debug_state(ctx
);
1284 _mesa_destroy_debug_output(struct gl_context
*ctx
)
1287 debug_destroy(ctx
->Debug
);
1288 /* set to NULL just in case it is used before context is completely gone. */
1292 simple_mtx_destroy(&ctx
->DebugMutex
);
1296 _mesa_StringMarkerGREMEDY(GLsizei len
, const GLvoid
*string
)
1298 GET_CURRENT_CONTEXT(ctx
);
1299 if (ctx
->Extensions
.GREMEDY_string_marker
) {
1300 /* if length not specified, string will be null terminated: */
1302 len
= strlen(string
);
1303 ctx
->Driver
.EmitStringMarker(ctx
, string
, len
);
1305 _mesa_error(ctx
, GL_INVALID_OPERATION
, "StringMarkerGREMEDY");