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"
31 #include "util/imports.h"
35 #include "util/hash_table.h"
36 #include "util/list.h"
39 static simple_mtx_t DynamicIDMutex
= _SIMPLE_MTX_INITIALIZER_NP
;
40 static GLuint NextDynamicID
= 1;
44 * A namespace element.
46 struct gl_debug_element
48 struct list_head link
;
51 /* at which severity levels (mesa_debug_severity) is the message enabled */
56 struct gl_debug_namespace
58 struct list_head Elements
;
59 GLbitfield DefaultState
;
63 struct gl_debug_group
{
64 struct gl_debug_namespace Namespaces
[MESA_DEBUG_SOURCE_COUNT
][MESA_DEBUG_TYPE_COUNT
];
69 * An error, warning, or other piece of debug information for an application
70 * to consume via GL_ARB_debug_output/GL_KHR_debug.
72 struct gl_debug_message
74 enum mesa_debug_source source
;
75 enum mesa_debug_type type
;
77 enum mesa_debug_severity severity
;
78 /* length as given by the user - if message was explicitly null terminated,
79 * length can be negative */
86 * Debug message log. It works like a ring buffer.
89 struct gl_debug_message Messages
[MAX_DEBUG_LOGGED_MESSAGES
];
98 const void *CallbackData
;
100 GLboolean DebugOutput
;
101 GLboolean LogToStderr
;
103 struct gl_debug_group
*Groups
[MAX_DEBUG_GROUP_STACK_DEPTH
];
104 struct gl_debug_message GroupMessages
[MAX_DEBUG_GROUP_STACK_DEPTH
];
105 GLint CurrentGroup
; // GroupStackDepth - 1
107 struct gl_debug_log Log
;
111 static char out_of_memory
[] = "Debugging error: out of memory";
113 static const GLenum debug_source_enums
[] = {
115 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
116 GL_DEBUG_SOURCE_SHADER_COMPILER
,
117 GL_DEBUG_SOURCE_THIRD_PARTY
,
118 GL_DEBUG_SOURCE_APPLICATION
,
119 GL_DEBUG_SOURCE_OTHER
,
122 static const GLenum debug_type_enums
[] = {
124 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
125 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
126 GL_DEBUG_TYPE_PORTABILITY
,
127 GL_DEBUG_TYPE_PERFORMANCE
,
129 GL_DEBUG_TYPE_MARKER
,
130 GL_DEBUG_TYPE_PUSH_GROUP
,
131 GL_DEBUG_TYPE_POP_GROUP
,
134 static const GLenum debug_severity_enums
[] = {
135 GL_DEBUG_SEVERITY_LOW
,
136 GL_DEBUG_SEVERITY_MEDIUM
,
137 GL_DEBUG_SEVERITY_HIGH
,
138 GL_DEBUG_SEVERITY_NOTIFICATION
,
142 static enum mesa_debug_source
143 gl_enum_to_debug_source(GLenum e
)
147 for (i
= 0; i
< ARRAY_SIZE(debug_source_enums
); i
++) {
148 if (debug_source_enums
[i
] == e
)
154 static enum mesa_debug_type
155 gl_enum_to_debug_type(GLenum e
)
159 for (i
= 0; i
< ARRAY_SIZE(debug_type_enums
); i
++) {
160 if (debug_type_enums
[i
] == e
)
166 static enum mesa_debug_severity
167 gl_enum_to_debug_severity(GLenum e
)
171 for (i
= 0; i
< ARRAY_SIZE(debug_severity_enums
); i
++) {
172 if (debug_severity_enums
[i
] == e
)
180 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
183 * The GL API has this "ID" mechanism, where the intention is to allow a
184 * client to filter in/out messages based on source, type, and ID. Of course,
185 * building a giant enum list of all debug output messages that Mesa might
186 * generate is ridiculous, so instead we have our caller pass us a pointer to
187 * static storage where the ID should get stored. This ID will be shared
188 * across all contexts for that message (which seems like a desirable
189 * property, even if it's not expected by the spec), but note that it won't be
190 * the same between executions if messages aren't generated in the same order.
193 _mesa_debug_get_id(GLuint
*id
)
196 simple_mtx_lock(&DynamicIDMutex
);
198 *id
= NextDynamicID
++;
199 simple_mtx_unlock(&DynamicIDMutex
);
204 debug_message_clear(struct gl_debug_message
*msg
)
206 if (msg
->message
!= (char*)out_of_memory
)
213 debug_message_store(struct gl_debug_message
*msg
,
214 enum mesa_debug_source source
,
215 enum mesa_debug_type type
, GLuint id
,
216 enum mesa_debug_severity severity
,
217 GLsizei len
, const char *buf
)
219 GLsizei length
= len
;
221 assert(!msg
->message
&& !msg
->length
);
224 length
= strlen(buf
);
226 msg
->message
= malloc(length
+1);
228 (void) strncpy(msg
->message
, buf
, (size_t)length
);
229 msg
->message
[length
] = '\0';
232 msg
->source
= source
;
235 msg
->severity
= severity
;
237 static GLuint oom_msg_id
= 0;
238 _mesa_debug_get_id(&oom_msg_id
);
241 msg
->message
= out_of_memory
;
243 msg
->source
= MESA_DEBUG_SOURCE_OTHER
;
244 msg
->type
= MESA_DEBUG_TYPE_ERROR
;
245 msg
->id
= oom_msg_id
;
246 msg
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
251 debug_namespace_init(struct gl_debug_namespace
*ns
)
253 list_inithead(&ns
->Elements
);
255 /* Enable all the messages with severity HIGH or MEDIUM by default */
256 ns
->DefaultState
= (1 << MESA_DEBUG_SEVERITY_MEDIUM
) |
257 (1 << MESA_DEBUG_SEVERITY_HIGH
) |
258 (1 << MESA_DEBUG_SEVERITY_NOTIFICATION
);
262 debug_namespace_clear(struct gl_debug_namespace
*ns
)
264 list_for_each_entry_safe(struct gl_debug_element
, elem
, &ns
->Elements
, link
)
269 debug_namespace_copy(struct gl_debug_namespace
*dst
,
270 const struct gl_debug_namespace
*src
)
272 dst
->DefaultState
= src
->DefaultState
;
274 list_inithead(&dst
->Elements
);
275 list_for_each_entry(struct gl_debug_element
, elem
, &src
->Elements
, link
) {
276 struct gl_debug_element
*copy
;
278 copy
= malloc(sizeof(*copy
));
280 debug_namespace_clear(dst
);
285 copy
->State
= elem
->State
;
286 list_addtail(©
->link
, &dst
->Elements
);
293 * Set the state of \p id in the namespace.
296 debug_namespace_set(struct gl_debug_namespace
*ns
,
297 GLuint id
, bool enabled
)
299 const uint32_t state
= (enabled
) ?
300 ((1 << MESA_DEBUG_SEVERITY_COUNT
) - 1) : 0;
301 struct gl_debug_element
*elem
= NULL
;
303 /* find the element */
304 list_for_each_entry(struct gl_debug_element
, tmp
, &ns
->Elements
, link
) {
311 /* we do not need the element if it has the default state */
312 if (ns
->DefaultState
== state
) {
314 list_del(&elem
->link
);
321 elem
= malloc(sizeof(*elem
));
326 list_addtail(&elem
->link
, &ns
->Elements
);
335 * Set the default state of the namespace for \p severity. When \p severity
336 * is MESA_DEBUG_SEVERITY_COUNT, the default values for all severities are
340 debug_namespace_set_all(struct gl_debug_namespace
*ns
,
341 enum mesa_debug_severity severity
,
346 /* set all elements to the same state */
347 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
348 ns
->DefaultState
= (enabled
) ? ((1 << severity
) - 1) : 0;
349 debug_namespace_clear(ns
);
350 list_inithead(&ns
->Elements
);
354 mask
= 1 << severity
;
355 val
= (enabled
) ? mask
: 0;
357 ns
->DefaultState
= (ns
->DefaultState
& ~mask
) | val
;
359 list_for_each_entry_safe(struct gl_debug_element
, elem
, &ns
->Elements
,
361 elem
->State
= (elem
->State
& ~mask
) | val
;
362 if (elem
->State
== ns
->DefaultState
) {
363 list_del(&elem
->link
);
370 * Get the state of \p id in the namespace.
373 debug_namespace_get(const struct gl_debug_namespace
*ns
, GLuint id
,
374 enum mesa_debug_severity severity
)
378 state
= ns
->DefaultState
;
379 list_for_each_entry(struct gl_debug_element
, elem
, &ns
->Elements
, link
) {
380 if (elem
->ID
== id
) {
386 return (state
& (1 << severity
));
390 * Allocate and initialize context debug state.
392 static struct gl_debug_state
*
395 struct gl_debug_state
*debug
;
398 debug
= CALLOC_STRUCT(gl_debug_state
);
402 debug
->Groups
[0] = malloc(sizeof(*debug
->Groups
[0]));
403 if (!debug
->Groups
[0]) {
408 /* Initialize state for filtering known debug messages. */
409 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
410 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
411 debug_namespace_init(&debug
->Groups
[0]->Namespaces
[s
][t
]);
418 * Return true if the top debug group points to the group below it.
421 debug_is_group_read_only(const struct gl_debug_state
*debug
)
423 const GLint gstack
= debug
->CurrentGroup
;
424 return (gstack
> 0 && debug
->Groups
[gstack
] == debug
->Groups
[gstack
- 1]);
428 * Make the top debug group writable.
431 debug_make_group_writable(struct gl_debug_state
*debug
)
433 const GLint gstack
= debug
->CurrentGroup
;
434 const struct gl_debug_group
*src
= debug
->Groups
[gstack
];
435 struct gl_debug_group
*dst
;
438 if (!debug_is_group_read_only(debug
))
441 dst
= malloc(sizeof(*dst
));
445 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
446 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
447 if (!debug_namespace_copy(&dst
->Namespaces
[s
][t
],
448 &src
->Namespaces
[s
][t
])) {
450 for (t
= t
- 1; t
>= 0; t
--)
451 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
452 for (s
= s
- 1; s
>= 0; s
--) {
453 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
454 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
462 debug
->Groups
[gstack
] = dst
;
468 * Free the top debug group.
471 debug_clear_group(struct gl_debug_state
*debug
)
473 const GLint gstack
= debug
->CurrentGroup
;
475 if (!debug_is_group_read_only(debug
)) {
476 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
479 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
480 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
481 debug_namespace_clear(&grp
->Namespaces
[s
][t
]);
487 debug
->Groups
[gstack
] = NULL
;
491 * Delete the oldest debug messages out of the log.
494 debug_delete_messages(struct gl_debug_state
*debug
, int count
)
496 struct gl_debug_log
*log
= &debug
->Log
;
498 if (count
> log
->NumMessages
)
499 count
= log
->NumMessages
;
502 struct gl_debug_message
*msg
= &log
->Messages
[log
->NextMessage
];
504 debug_message_clear(msg
);
508 log
->NextMessage
%= MAX_DEBUG_LOGGED_MESSAGES
;
513 * Loop through debug group stack tearing down states for
514 * filtering debug messages. Then free debug output state.
517 debug_destroy(struct gl_debug_state
*debug
)
519 while (debug
->CurrentGroup
> 0) {
520 debug_clear_group(debug
);
521 debug
->CurrentGroup
--;
524 debug_clear_group(debug
);
525 debug_delete_messages(debug
, debug
->Log
.NumMessages
);
530 * Sets the state of the given message source/type/ID tuple.
533 debug_set_message_enable(struct gl_debug_state
*debug
,
534 enum mesa_debug_source source
,
535 enum mesa_debug_type type
,
536 GLuint id
, GLboolean enabled
)
538 const GLint gstack
= debug
->CurrentGroup
;
539 struct gl_debug_namespace
*ns
;
541 debug_make_group_writable(debug
);
542 ns
= &debug
->Groups
[gstack
]->Namespaces
[source
][type
];
544 debug_namespace_set(ns
, id
, enabled
);
548 * Set the state of all message IDs found in the given intersection of
549 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
550 * GL_DONT_CARE (include all messages in the class).
552 * This requires both setting the state of all previously seen message
553 * IDs in the hash table, and setting the default state for all
554 * applicable combinations of source/type/severity, so that all the
555 * yet-unknown message IDs that may be used in the future will be
556 * impacted as if they were already known.
559 debug_set_message_enable_all(struct gl_debug_state
*debug
,
560 enum mesa_debug_source source
,
561 enum mesa_debug_type type
,
562 enum mesa_debug_severity severity
,
565 const GLint gstack
= debug
->CurrentGroup
;
566 int s
, t
, smax
, tmax
;
568 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
570 smax
= MESA_DEBUG_SOURCE_COUNT
;
575 if (type
== MESA_DEBUG_TYPE_COUNT
) {
577 tmax
= MESA_DEBUG_TYPE_COUNT
;
582 debug_make_group_writable(debug
);
584 for (s
= source
; s
< smax
; s
++) {
585 for (t
= type
; t
< tmax
; t
++) {
586 struct gl_debug_namespace
*nspace
=
587 &debug
->Groups
[gstack
]->Namespaces
[s
][t
];
588 debug_namespace_set_all(nspace
, severity
, enabled
);
594 * Returns if the given message source/type/ID tuple is enabled.
597 _mesa_debug_is_message_enabled(const struct gl_debug_state
*debug
,
598 enum mesa_debug_source source
,
599 enum mesa_debug_type type
,
601 enum mesa_debug_severity severity
)
603 const GLint gstack
= debug
->CurrentGroup
;
604 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
605 struct gl_debug_namespace
*nspace
= &grp
->Namespaces
[source
][type
];
607 if (!debug
->DebugOutput
)
610 return debug_namespace_get(nspace
, id
, severity
);
614 * 'buf' is not necessarily a null-terminated string. When logging, copy
615 * 'len' characters from it, store them in a new, null-terminated string,
616 * and remember the number of bytes used by that string, *including*
617 * the null terminator this time.
620 debug_log_message(struct gl_debug_state
*debug
,
621 enum mesa_debug_source source
,
622 enum mesa_debug_type type
, GLuint id
,
623 enum mesa_debug_severity severity
,
624 GLsizei len
, const char *buf
)
626 struct gl_debug_log
*log
= &debug
->Log
;
628 struct gl_debug_message
*emptySlot
;
630 if (debug
->LogToStderr
) {
631 _mesa_log("Mesa debug output: %.*s\n", len
, buf
);
634 assert(len
< MAX_DEBUG_MESSAGE_LENGTH
);
636 if (log
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
639 nextEmpty
= (log
->NextMessage
+ log
->NumMessages
)
640 % MAX_DEBUG_LOGGED_MESSAGES
;
641 emptySlot
= &log
->Messages
[nextEmpty
];
643 debug_message_store(emptySlot
, source
, type
,
644 id
, severity
, len
, buf
);
650 * Return the oldest debug message out of the log.
652 static const struct gl_debug_message
*
653 debug_fetch_message(const struct gl_debug_state
*debug
)
655 const struct gl_debug_log
*log
= &debug
->Log
;
657 return (log
->NumMessages
) ? &log
->Messages
[log
->NextMessage
] : NULL
;
660 static struct gl_debug_message
*
661 debug_get_group_message(struct gl_debug_state
*debug
)
663 return &debug
->GroupMessages
[debug
->CurrentGroup
];
667 debug_push_group(struct gl_debug_state
*debug
)
669 const GLint gstack
= debug
->CurrentGroup
;
671 /* just point to the previous stack */
672 debug
->Groups
[gstack
+ 1] = debug
->Groups
[gstack
];
673 debug
->CurrentGroup
++;
677 debug_pop_group(struct gl_debug_state
*debug
)
679 debug_clear_group(debug
);
680 debug
->CurrentGroup
--;
685 * Lock and return debug state for the context. The debug state will be
686 * allocated and initialized upon the first call. When NULL is returned, the
687 * debug state is not locked.
689 static struct gl_debug_state
*
690 _mesa_lock_debug_state(struct gl_context
*ctx
)
692 simple_mtx_lock(&ctx
->DebugMutex
);
695 ctx
->Debug
= debug_create();
697 GET_CURRENT_CONTEXT(cur
);
698 simple_mtx_unlock(&ctx
->DebugMutex
);
701 * This function may be called from other threads. When that is the
702 * case, we cannot record this OOM error.
705 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
715 _mesa_unlock_debug_state(struct gl_context
*ctx
)
717 simple_mtx_unlock(&ctx
->DebugMutex
);
721 * Set the integer debug state specified by \p pname. This can be called from
722 * _mesa_set_enable for example.
725 _mesa_set_debug_state_int(struct gl_context
*ctx
, GLenum pname
, GLint val
)
727 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
733 case GL_DEBUG_OUTPUT
:
734 debug
->DebugOutput
= (val
!= 0);
736 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
737 debug
->SyncOutput
= (val
!= 0);
740 assert(!"unknown debug output param");
744 _mesa_unlock_debug_state(ctx
);
750 * Query the integer debug state specified by \p pname. This can be called
751 * _mesa_GetIntegerv for example.
754 _mesa_get_debug_state_int(struct gl_context
*ctx
, GLenum pname
)
758 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
763 case GL_DEBUG_OUTPUT
:
764 val
= debug
->DebugOutput
;
766 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
767 val
= debug
->SyncOutput
;
769 case GL_DEBUG_LOGGED_MESSAGES
:
770 val
= debug
->Log
.NumMessages
;
772 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH
:
773 val
= (debug
->Log
.NumMessages
) ?
774 debug
->Log
.Messages
[debug
->Log
.NextMessage
].length
+ 1 : 0;
776 case GL_DEBUG_GROUP_STACK_DEPTH
:
777 val
= debug
->CurrentGroup
+ 1;
780 assert(!"unknown debug output param");
785 _mesa_unlock_debug_state(ctx
);
791 * Query the pointer debug state specified by \p pname. This can be called
792 * _mesa_GetPointerv for example.
795 _mesa_get_debug_state_ptr(struct gl_context
*ctx
, GLenum pname
)
798 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
804 case GL_DEBUG_CALLBACK_FUNCTION_ARB
:
805 val
= (void *) debug
->Callback
;
807 case GL_DEBUG_CALLBACK_USER_PARAM_ARB
:
808 val
= (void *) debug
->CallbackData
;
811 assert(!"unknown debug output param");
816 _mesa_unlock_debug_state(ctx
);
822 * Insert a debug message. The mutex is assumed to be locked, and will be
823 * unlocked by this call.
826 log_msg_locked_and_unlock(struct gl_context
*ctx
,
827 enum mesa_debug_source source
,
828 enum mesa_debug_type type
, GLuint id
,
829 enum mesa_debug_severity severity
,
830 GLint len
, const char *buf
)
832 struct gl_debug_state
*debug
= ctx
->Debug
;
834 if (!_mesa_debug_is_message_enabled(debug
, source
, type
, id
, severity
)) {
835 _mesa_unlock_debug_state(ctx
);
839 if (ctx
->Debug
->Callback
) {
840 /* Call the user's callback function */
841 GLenum gl_source
= debug_source_enums
[source
];
842 GLenum gl_type
= debug_type_enums
[type
];
843 GLenum gl_severity
= debug_severity_enums
[severity
];
844 GLDEBUGPROC callback
= ctx
->Debug
->Callback
;
845 const void *data
= ctx
->Debug
->CallbackData
;
848 * When ctx->Debug->SyncOutput is GL_FALSE, the client is prepared for
849 * unsynchronous calls. When it is GL_TRUE, we will not spawn threads.
850 * In either case, we can call the callback unlocked.
852 _mesa_unlock_debug_state(ctx
);
853 callback(gl_source
, gl_type
, id
, gl_severity
, len
, buf
, data
);
856 /* add debug message to queue */
857 debug_log_message(ctx
->Debug
, source
, type
, id
, severity
, len
, buf
);
858 _mesa_unlock_debug_state(ctx
);
863 * Log a client or driver debug message.
866 _mesa_log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
867 enum mesa_debug_type type
, GLuint id
,
868 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
870 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
875 log_msg_locked_and_unlock(ctx
, source
, type
, id
, severity
, len
, buf
);
880 * Verify that source, type, and severity are valid enums.
882 * The 'caller' param is used for handling values available
883 * only in glDebugMessageInsert or glDebugMessageControl
886 validate_params(struct gl_context
*ctx
, unsigned caller
,
887 const char *callerstr
, GLenum source
, GLenum type
,
893 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
894 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
896 case GL_DEBUG_SOURCE_API_ARB
:
897 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
898 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
899 case GL_DEBUG_SOURCE_OTHER_ARB
:
900 if (caller
!= INSERT
)
905 if (caller
== CONTROL
)
914 case GL_DEBUG_TYPE_ERROR_ARB
:
915 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
916 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
917 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
918 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
919 case GL_DEBUG_TYPE_OTHER_ARB
:
920 case GL_DEBUG_TYPE_MARKER
:
921 case GL_DEBUG_TYPE_PUSH_GROUP
:
922 case GL_DEBUG_TYPE_POP_GROUP
:
925 if (caller
== CONTROL
)
934 case GL_DEBUG_SEVERITY_HIGH_ARB
:
935 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
936 case GL_DEBUG_SEVERITY_LOW_ARB
:
937 case GL_DEBUG_SEVERITY_NOTIFICATION
:
940 if (caller
== CONTROL
)
950 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
951 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
952 source
, type
, severity
);
959 validate_length(struct gl_context
*ctx
, const char *callerstr
, GLsizei length
,
964 GLsizei len
= strlen(buf
);
966 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
967 _mesa_error(ctx
, GL_INVALID_VALUE
,
968 "%s(null terminated string length=%d, is not less than "
969 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, len
,
970 MAX_DEBUG_MESSAGE_LENGTH
);
975 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
976 _mesa_error(ctx
, GL_INVALID_VALUE
,
977 "%s(length=%d, which is not less than "
978 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
979 MAX_DEBUG_MESSAGE_LENGTH
);
988 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
989 GLenum severity
, GLint length
,
992 GET_CURRENT_CONTEXT(ctx
);
993 const char *callerstr
;
995 if (_mesa_is_desktop_gl(ctx
))
996 callerstr
= "glDebugMessageInsert";
998 callerstr
= "glDebugMessageInsertKHR";
1000 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
1001 return; /* GL_INVALID_ENUM */
1003 if (!validate_length(ctx
, callerstr
, length
, buf
))
1004 return; /* GL_INVALID_VALUE */
1006 /* if length not specified, string will be null terminated: */
1008 length
= strlen(buf
);
1010 _mesa_log_msg(ctx
, gl_enum_to_debug_source(source
),
1011 gl_enum_to_debug_type(type
), id
,
1012 gl_enum_to_debug_severity(severity
),
1015 if (type
== GL_DEBUG_TYPE_MARKER
&& ctx
->Driver
.EmitStringMarker
) {
1016 ctx
->Driver
.EmitStringMarker(ctx
, buf
, length
);
1022 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
1023 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
1024 GLsizei
*lengths
, GLchar
*messageLog
)
1026 GET_CURRENT_CONTEXT(ctx
);
1027 struct gl_debug_state
*debug
;
1028 const char *callerstr
;
1031 if (_mesa_is_desktop_gl(ctx
))
1032 callerstr
= "glGetDebugMessageLog";
1034 callerstr
= "glGetDebugMessageLogKHR";
1040 _mesa_error(ctx
, GL_INVALID_VALUE
,
1041 "%s(logSize=%d : logSize must not be negative)",
1042 callerstr
, logSize
);
1046 debug
= _mesa_lock_debug_state(ctx
);
1050 for (ret
= 0; ret
< count
; ret
++) {
1051 const struct gl_debug_message
*msg
= debug_fetch_message(debug
);
1059 len
= strlen(msg
->message
);
1061 if (logSize
< len
+1 && messageLog
!= NULL
)
1065 assert(msg
->message
[len
] == '\0');
1066 (void) strncpy(messageLog
, msg
->message
, (size_t)len
+1);
1068 messageLog
+= len
+1;
1075 *severities
++ = debug_severity_enums
[msg
->severity
];
1077 *sources
++ = debug_source_enums
[msg
->source
];
1079 *types
++ = debug_type_enums
[msg
->type
];
1083 debug_delete_messages(debug
, 1);
1086 _mesa_unlock_debug_state(ctx
);
1093 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
1094 GLenum gl_severity
, GLsizei count
,
1095 const GLuint
*ids
, GLboolean enabled
)
1097 GET_CURRENT_CONTEXT(ctx
);
1098 enum mesa_debug_source source
= gl_enum_to_debug_source(gl_source
);
1099 enum mesa_debug_type type
= gl_enum_to_debug_type(gl_type
);
1100 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(gl_severity
);
1101 const char *callerstr
;
1102 struct gl_debug_state
*debug
;
1104 if (_mesa_is_desktop_gl(ctx
))
1105 callerstr
= "glDebugMessageControl";
1107 callerstr
= "glDebugMessageControlKHR";
1110 _mesa_error(ctx
, GL_INVALID_VALUE
,
1111 "%s(count=%d : count must not be negative)", callerstr
,
1116 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
1118 return; /* GL_INVALID_ENUM */
1120 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
1121 || gl_source
== GL_DONT_CARE
)) {
1122 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1123 "%s(When passing an array of ids, severity must be"
1124 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
1129 debug
= _mesa_lock_debug_state(ctx
);
1135 for (i
= 0; i
< count
; i
++)
1136 debug_set_message_enable(debug
, source
, type
, ids
[i
], enabled
);
1139 debug_set_message_enable_all(debug
, source
, type
, severity
, enabled
);
1142 _mesa_unlock_debug_state(ctx
);
1147 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
1149 GET_CURRENT_CONTEXT(ctx
);
1150 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
1152 debug
->Callback
= callback
;
1153 debug
->CallbackData
= userParam
;
1154 _mesa_unlock_debug_state(ctx
);
1160 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
1161 const GLchar
*message
)
1163 GET_CURRENT_CONTEXT(ctx
);
1164 const char *callerstr
;
1165 struct gl_debug_state
*debug
;
1166 struct gl_debug_message
*emptySlot
;
1168 if (_mesa_is_desktop_gl(ctx
))
1169 callerstr
= "glPushDebugGroup";
1171 callerstr
= "glPushDebugGroupKHR";
1174 case GL_DEBUG_SOURCE_APPLICATION
:
1175 case GL_DEBUG_SOURCE_THIRD_PARTY
:
1178 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
1179 "(source=0x%x)", callerstr
, source
);
1183 if (!validate_length(ctx
, callerstr
, length
, message
))
1184 return; /* GL_INVALID_VALUE */
1187 length
= strlen(message
);
1189 debug
= _mesa_lock_debug_state(ctx
);
1193 if (debug
->CurrentGroup
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
1194 _mesa_unlock_debug_state(ctx
);
1195 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
1199 /* pop reuses the message details from push so we store this */
1200 emptySlot
= debug_get_group_message(debug
);
1201 debug_message_store(emptySlot
,
1202 gl_enum_to_debug_source(source
),
1203 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
1205 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1208 debug_push_group(debug
);
1210 log_msg_locked_and_unlock(ctx
,
1211 gl_enum_to_debug_source(source
),
1212 MESA_DEBUG_TYPE_PUSH_GROUP
, id
,
1213 MESA_DEBUG_SEVERITY_NOTIFICATION
, length
,
1219 _mesa_PopDebugGroup(void)
1221 GET_CURRENT_CONTEXT(ctx
);
1222 const char *callerstr
;
1223 struct gl_debug_state
*debug
;
1224 struct gl_debug_message
*gdmessage
, msg
;
1226 if (_mesa_is_desktop_gl(ctx
))
1227 callerstr
= "glPopDebugGroup";
1229 callerstr
= "glPopDebugGroupKHR";
1231 debug
= _mesa_lock_debug_state(ctx
);
1235 if (debug
->CurrentGroup
<= 0) {
1236 _mesa_unlock_debug_state(ctx
);
1237 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
1241 debug_pop_group(debug
);
1243 /* make a shallow copy */
1244 gdmessage
= debug_get_group_message(debug
);
1246 gdmessage
->message
= NULL
;
1247 gdmessage
->length
= 0;
1249 log_msg_locked_and_unlock(ctx
,
1251 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
1253 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1254 msg
.length
, msg
.message
);
1256 debug_message_clear(&msg
);
1261 _mesa_init_debug_output(struct gl_context
*ctx
)
1263 simple_mtx_init(&ctx
->DebugMutex
, mtx_plain
);
1265 if (MESA_DEBUG_FLAGS
& DEBUG_CONTEXT
) {
1266 /* If the MESA_DEBUG env is set to "context", we'll turn on the
1267 * GL_CONTEXT_FLAG_DEBUG_BIT context flag and log debug output
1268 * messages to stderr (or whatever MESA_LOG_FILE points at).
1270 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
1274 debug
->DebugOutput
= GL_TRUE
;
1275 debug
->LogToStderr
= GL_TRUE
;
1276 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
1277 _mesa_unlock_debug_state(ctx
);
1283 _mesa_free_errors_data(struct gl_context
*ctx
)
1286 debug_destroy(ctx
->Debug
);
1287 /* set to NULL just in case it is used before context is completely gone. */
1291 simple_mtx_destroy(&ctx
->DebugMutex
);
1295 _mesa_StringMarkerGREMEDY(GLsizei len
, const GLvoid
*string
)
1297 GET_CURRENT_CONTEXT(ctx
);
1298 if (ctx
->Extensions
.GREMEDY_string_marker
) {
1299 /* if length not specified, string will be null terminated: */
1301 len
= strlen(string
);
1302 ctx
->Driver
.EmitStringMarker(ctx
, string
, len
);
1304 _mesa_error(ctx
, GL_INVALID_OPERATION
, "StringMarkerGREMEDY");