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"
36 #include "util/hash_table.h"
37 #include "util/simple_list.h"
40 static mtx_t DynamicIDMutex
= _MTX_INITIALIZER_NP
;
41 static GLuint NextDynamicID
= 1;
45 * A namespace element.
47 struct gl_debug_element
49 struct simple_node link
;
52 /* at which severity levels (mesa_debug_severity) is the message enabled */
57 struct gl_debug_namespace
59 struct simple_node 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 mtx_lock(&DynamicIDMutex
);
199 *id
= NextDynamicID
++;
200 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 make_empty_list(&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 struct simple_node
*node
, *tmp
;
267 foreach_s(node
, tmp
, &ns
->Elements
)
272 debug_namespace_copy(struct gl_debug_namespace
*dst
,
273 const struct gl_debug_namespace
*src
)
275 struct simple_node
*node
;
277 dst
->DefaultState
= src
->DefaultState
;
279 make_empty_list(&dst
->Elements
);
280 foreach(node
, &src
->Elements
) {
281 const struct gl_debug_element
*elem
=
282 (const struct gl_debug_element
*) node
;
283 struct gl_debug_element
*copy
;
285 copy
= malloc(sizeof(*copy
));
287 debug_namespace_clear(dst
);
292 copy
->State
= elem
->State
;
293 insert_at_tail(&dst
->Elements
, ©
->link
);
300 * Set the state of \p id in the namespace.
303 debug_namespace_set(struct gl_debug_namespace
*ns
,
304 GLuint id
, bool enabled
)
306 const uint32_t state
= (enabled
) ?
307 ((1 << MESA_DEBUG_SEVERITY_COUNT
) - 1) : 0;
308 struct gl_debug_element
*elem
= NULL
;
309 struct simple_node
*node
;
311 /* find the element */
312 foreach(node
, &ns
->Elements
) {
313 struct gl_debug_element
*tmp
= (struct gl_debug_element
*) node
;
320 /* we do not need the element if it has the default state */
321 if (ns
->DefaultState
== state
) {
323 remove_from_list(&elem
->link
);
330 elem
= malloc(sizeof(*elem
));
335 insert_at_tail(&ns
->Elements
, &elem
->link
);
344 * Set the default state of the namespace for \p severity. When \p severity
345 * is MESA_DEBUG_SEVERITY_COUNT, the default values for all severities are
349 debug_namespace_set_all(struct gl_debug_namespace
*ns
,
350 enum mesa_debug_severity severity
,
353 struct simple_node
*node
, *tmp
;
356 /* set all elements to the same state */
357 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
358 ns
->DefaultState
= (enabled
) ? ((1 << severity
) - 1) : 0;
359 debug_namespace_clear(ns
);
360 make_empty_list(&ns
->Elements
);
364 mask
= 1 << severity
;
365 val
= (enabled
) ? mask
: 0;
367 ns
->DefaultState
= (ns
->DefaultState
& ~mask
) | val
;
369 foreach_s(node
, tmp
, &ns
->Elements
) {
370 struct gl_debug_element
*elem
= (struct gl_debug_element
*) node
;
372 elem
->State
= (elem
->State
& ~mask
) | val
;
373 if (elem
->State
== ns
->DefaultState
) {
374 remove_from_list(node
);
381 * Get the state of \p id in the namespace.
384 debug_namespace_get(const struct gl_debug_namespace
*ns
, GLuint id
,
385 enum mesa_debug_severity severity
)
387 struct simple_node
*node
;
390 state
= ns
->DefaultState
;
391 foreach(node
, &ns
->Elements
) {
392 struct gl_debug_element
*elem
= (struct gl_debug_element
*) node
;
394 if (elem
->ID
== id
) {
400 return (state
& (1 << severity
));
404 * Allocate and initialize context debug state.
406 static struct gl_debug_state
*
409 struct gl_debug_state
*debug
;
412 debug
= CALLOC_STRUCT(gl_debug_state
);
416 debug
->Groups
[0] = malloc(sizeof(*debug
->Groups
[0]));
417 if (!debug
->Groups
[0]) {
422 /* Initialize state for filtering known debug messages. */
423 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
424 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
425 debug_namespace_init(&debug
->Groups
[0]->Namespaces
[s
][t
]);
432 * Return true if the top debug group points to the group below it.
435 debug_is_group_read_only(const struct gl_debug_state
*debug
)
437 const GLint gstack
= debug
->CurrentGroup
;
438 return (gstack
> 0 && debug
->Groups
[gstack
] == debug
->Groups
[gstack
- 1]);
442 * Make the top debug group writable.
445 debug_make_group_writable(struct gl_debug_state
*debug
)
447 const GLint gstack
= debug
->CurrentGroup
;
448 const struct gl_debug_group
*src
= debug
->Groups
[gstack
];
449 struct gl_debug_group
*dst
;
452 if (!debug_is_group_read_only(debug
))
455 dst
= malloc(sizeof(*dst
));
459 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
460 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
461 if (!debug_namespace_copy(&dst
->Namespaces
[s
][t
],
462 &src
->Namespaces
[s
][t
])) {
464 for (t
= t
- 1; t
>= 0; t
--)
465 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
466 for (s
= s
- 1; s
>= 0; s
--) {
467 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
468 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
476 debug
->Groups
[gstack
] = dst
;
482 * Free the top debug group.
485 debug_clear_group(struct gl_debug_state
*debug
)
487 const GLint gstack
= debug
->CurrentGroup
;
489 if (!debug_is_group_read_only(debug
)) {
490 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
493 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
494 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
495 debug_namespace_clear(&grp
->Namespaces
[s
][t
]);
501 debug
->Groups
[gstack
] = NULL
;
505 * Loop through debug group stack tearing down states for
506 * filtering debug messages. Then free debug output state.
509 debug_destroy(struct gl_debug_state
*debug
)
511 while (debug
->CurrentGroup
> 0) {
512 debug_clear_group(debug
);
513 debug
->CurrentGroup
--;
516 debug_clear_group(debug
);
521 * Sets the state of the given message source/type/ID tuple.
524 debug_set_message_enable(struct gl_debug_state
*debug
,
525 enum mesa_debug_source source
,
526 enum mesa_debug_type type
,
527 GLuint id
, GLboolean enabled
)
529 const GLint gstack
= debug
->CurrentGroup
;
530 struct gl_debug_namespace
*ns
;
532 debug_make_group_writable(debug
);
533 ns
= &debug
->Groups
[gstack
]->Namespaces
[source
][type
];
535 debug_namespace_set(ns
, id
, enabled
);
539 * Set the state of all message IDs found in the given intersection of
540 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
541 * GL_DONT_CARE (include all messages in the class).
543 * This requires both setting the state of all previously seen message
544 * IDs in the hash table, and setting the default state for all
545 * applicable combinations of source/type/severity, so that all the
546 * yet-unknown message IDs that may be used in the future will be
547 * impacted as if they were already known.
550 debug_set_message_enable_all(struct gl_debug_state
*debug
,
551 enum mesa_debug_source source
,
552 enum mesa_debug_type type
,
553 enum mesa_debug_severity severity
,
556 const GLint gstack
= debug
->CurrentGroup
;
557 int s
, t
, smax
, tmax
;
559 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
561 smax
= MESA_DEBUG_SOURCE_COUNT
;
566 if (type
== MESA_DEBUG_TYPE_COUNT
) {
568 tmax
= MESA_DEBUG_TYPE_COUNT
;
573 debug_make_group_writable(debug
);
575 for (s
= source
; s
< smax
; s
++) {
576 for (t
= type
; t
< tmax
; t
++) {
577 struct gl_debug_namespace
*nspace
=
578 &debug
->Groups
[gstack
]->Namespaces
[s
][t
];
579 debug_namespace_set_all(nspace
, severity
, enabled
);
585 * Returns if the given message source/type/ID tuple is enabled.
588 _mesa_debug_is_message_enabled(const struct gl_debug_state
*debug
,
589 enum mesa_debug_source source
,
590 enum mesa_debug_type type
,
592 enum mesa_debug_severity severity
)
594 const GLint gstack
= debug
->CurrentGroup
;
595 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
596 struct gl_debug_namespace
*nspace
= &grp
->Namespaces
[source
][type
];
598 if (!debug
->DebugOutput
)
601 return debug_namespace_get(nspace
, id
, severity
);
605 * 'buf' is not necessarily a null-terminated string. When logging, copy
606 * 'len' characters from it, store them in a new, null-terminated string,
607 * and remember the number of bytes used by that string, *including*
608 * the null terminator this time.
611 debug_log_message(struct gl_debug_state
*debug
,
612 enum mesa_debug_source source
,
613 enum mesa_debug_type type
, GLuint id
,
614 enum mesa_debug_severity severity
,
615 GLsizei len
, const char *buf
)
617 struct gl_debug_log
*log
= &debug
->Log
;
619 struct gl_debug_message
*emptySlot
;
621 if (debug
->LogToStderr
) {
622 _mesa_log("Mesa debug output: %.*s\n", len
, buf
);
625 assert(len
< MAX_DEBUG_MESSAGE_LENGTH
);
627 if (log
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
630 nextEmpty
= (log
->NextMessage
+ log
->NumMessages
)
631 % MAX_DEBUG_LOGGED_MESSAGES
;
632 emptySlot
= &log
->Messages
[nextEmpty
];
634 debug_message_store(emptySlot
, source
, type
,
635 id
, severity
, len
, buf
);
641 * Return the oldest debug message out of the log.
643 static const struct gl_debug_message
*
644 debug_fetch_message(const struct gl_debug_state
*debug
)
646 const struct gl_debug_log
*log
= &debug
->Log
;
648 return (log
->NumMessages
) ? &log
->Messages
[log
->NextMessage
] : NULL
;
652 * Delete the oldest debug messages out of the log.
655 debug_delete_messages(struct gl_debug_state
*debug
, int count
)
657 struct gl_debug_log
*log
= &debug
->Log
;
659 if (count
> log
->NumMessages
)
660 count
= log
->NumMessages
;
663 struct gl_debug_message
*msg
= &log
->Messages
[log
->NextMessage
];
665 debug_message_clear(msg
);
669 log
->NextMessage
%= MAX_DEBUG_LOGGED_MESSAGES
;
673 static struct gl_debug_message
*
674 debug_get_group_message(struct gl_debug_state
*debug
)
676 return &debug
->GroupMessages
[debug
->CurrentGroup
];
680 debug_push_group(struct gl_debug_state
*debug
)
682 const GLint gstack
= debug
->CurrentGroup
;
684 /* just point to the previous stack */
685 debug
->Groups
[gstack
+ 1] = debug
->Groups
[gstack
];
686 debug
->CurrentGroup
++;
690 debug_pop_group(struct gl_debug_state
*debug
)
692 debug_clear_group(debug
);
693 debug
->CurrentGroup
--;
698 * Lock and return debug state for the context. The debug state will be
699 * allocated and initialized upon the first call. When NULL is returned, the
700 * debug state is not locked.
702 static struct gl_debug_state
*
703 _mesa_lock_debug_state(struct gl_context
*ctx
)
705 mtx_lock(&ctx
->DebugMutex
);
708 ctx
->Debug
= debug_create();
710 GET_CURRENT_CONTEXT(cur
);
711 mtx_unlock(&ctx
->DebugMutex
);
714 * This function may be called from other threads. When that is the
715 * case, we cannot record this OOM error.
718 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
728 _mesa_unlock_debug_state(struct gl_context
*ctx
)
730 mtx_unlock(&ctx
->DebugMutex
);
734 * Set the integer debug state specified by \p pname. This can be called from
735 * _mesa_set_enable for example.
738 _mesa_set_debug_state_int(struct gl_context
*ctx
, GLenum pname
, GLint val
)
740 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
746 case GL_DEBUG_OUTPUT
:
747 debug
->DebugOutput
= (val
!= 0);
749 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
750 debug
->SyncOutput
= (val
!= 0);
753 assert(!"unknown debug output param");
757 _mesa_unlock_debug_state(ctx
);
763 * Query the integer debug state specified by \p pname. This can be called
764 * _mesa_GetIntegerv for example.
767 _mesa_get_debug_state_int(struct gl_context
*ctx
, GLenum pname
)
771 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
776 case GL_DEBUG_OUTPUT
:
777 val
= debug
->DebugOutput
;
779 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
780 val
= debug
->SyncOutput
;
782 case GL_DEBUG_LOGGED_MESSAGES
:
783 val
= debug
->Log
.NumMessages
;
785 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH
:
786 val
= (debug
->Log
.NumMessages
) ?
787 debug
->Log
.Messages
[debug
->Log
.NextMessage
].length
+ 1 : 0;
789 case GL_DEBUG_GROUP_STACK_DEPTH
:
790 val
= debug
->CurrentGroup
+ 1;
793 assert(!"unknown debug output param");
798 _mesa_unlock_debug_state(ctx
);
804 * Query the pointer debug state specified by \p pname. This can be called
805 * _mesa_GetPointerv for example.
808 _mesa_get_debug_state_ptr(struct gl_context
*ctx
, GLenum pname
)
811 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
817 case GL_DEBUG_CALLBACK_FUNCTION_ARB
:
818 val
= (void *) debug
->Callback
;
820 case GL_DEBUG_CALLBACK_USER_PARAM_ARB
:
821 val
= (void *) debug
->CallbackData
;
824 assert(!"unknown debug output param");
829 _mesa_unlock_debug_state(ctx
);
835 * Insert a debug message. The mutex is assumed to be locked, and will be
836 * unlocked by this call.
839 log_msg_locked_and_unlock(struct gl_context
*ctx
,
840 enum mesa_debug_source source
,
841 enum mesa_debug_type type
, GLuint id
,
842 enum mesa_debug_severity severity
,
843 GLint len
, const char *buf
)
845 struct gl_debug_state
*debug
= ctx
->Debug
;
847 if (!_mesa_debug_is_message_enabled(debug
, source
, type
, id
, severity
)) {
848 _mesa_unlock_debug_state(ctx
);
852 if (ctx
->Debug
->Callback
) {
853 /* Call the user's callback function */
854 GLenum gl_source
= debug_source_enums
[source
];
855 GLenum gl_type
= debug_type_enums
[type
];
856 GLenum gl_severity
= debug_severity_enums
[severity
];
857 GLDEBUGPROC callback
= ctx
->Debug
->Callback
;
858 const void *data
= ctx
->Debug
->CallbackData
;
861 * When ctx->Debug->SyncOutput is GL_FALSE, the client is prepared for
862 * unsynchronous calls. When it is GL_TRUE, we will not spawn threads.
863 * In either case, we can call the callback unlocked.
865 _mesa_unlock_debug_state(ctx
);
866 callback(gl_source
, gl_type
, id
, gl_severity
, len
, buf
, data
);
869 /* add debug message to queue */
870 debug_log_message(ctx
->Debug
, source
, type
, id
, severity
, len
, buf
);
871 _mesa_unlock_debug_state(ctx
);
876 * Log a client or driver debug message.
879 _mesa_log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
880 enum mesa_debug_type type
, GLuint id
,
881 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
883 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
888 log_msg_locked_and_unlock(ctx
, source
, type
, id
, severity
, len
, buf
);
893 * Verify that source, type, and severity are valid enums.
895 * The 'caller' param is used for handling values available
896 * only in glDebugMessageInsert or glDebugMessageControl
899 validate_params(struct gl_context
*ctx
, unsigned caller
,
900 const char *callerstr
, GLenum source
, GLenum type
,
906 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
907 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
909 case GL_DEBUG_SOURCE_API_ARB
:
910 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
911 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
912 case GL_DEBUG_SOURCE_OTHER_ARB
:
913 if (caller
!= INSERT
)
918 if (caller
== CONTROL
)
927 case GL_DEBUG_TYPE_ERROR_ARB
:
928 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
929 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
930 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
931 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
932 case GL_DEBUG_TYPE_OTHER_ARB
:
933 case GL_DEBUG_TYPE_MARKER
:
934 case GL_DEBUG_TYPE_PUSH_GROUP
:
935 case GL_DEBUG_TYPE_POP_GROUP
:
938 if (caller
== CONTROL
)
947 case GL_DEBUG_SEVERITY_HIGH_ARB
:
948 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
949 case GL_DEBUG_SEVERITY_LOW_ARB
:
950 case GL_DEBUG_SEVERITY_NOTIFICATION
:
953 if (caller
== CONTROL
)
963 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
964 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
965 source
, type
, severity
);
972 validate_length(struct gl_context
*ctx
, const char *callerstr
, GLsizei length
,
977 GLsizei len
= strlen(buf
);
979 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
980 _mesa_error(ctx
, GL_INVALID_VALUE
,
981 "%s(null terminated string length=%d, is not less than "
982 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, len
,
983 MAX_DEBUG_MESSAGE_LENGTH
);
988 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
989 _mesa_error(ctx
, GL_INVALID_VALUE
,
990 "%s(length=%d, which is not less than "
991 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
992 MAX_DEBUG_MESSAGE_LENGTH
);
1001 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
1002 GLenum severity
, GLint length
,
1005 GET_CURRENT_CONTEXT(ctx
);
1006 const char *callerstr
;
1008 if (_mesa_is_desktop_gl(ctx
))
1009 callerstr
= "glDebugMessageInsert";
1011 callerstr
= "glDebugMessageInsertKHR";
1013 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
1014 return; /* GL_INVALID_ENUM */
1016 if (!validate_length(ctx
, callerstr
, length
, buf
))
1017 return; /* GL_INVALID_VALUE */
1019 /* if length not specified, string will be null terminated: */
1021 length
= strlen(buf
);
1023 _mesa_log_msg(ctx
, gl_enum_to_debug_source(source
),
1024 gl_enum_to_debug_type(type
), id
,
1025 gl_enum_to_debug_severity(severity
),
1028 if (type
== GL_DEBUG_TYPE_MARKER
&& ctx
->Driver
.EmitStringMarker
) {
1029 ctx
->Driver
.EmitStringMarker(ctx
, buf
, length
);
1035 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
1036 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
1037 GLsizei
*lengths
, GLchar
*messageLog
)
1039 GET_CURRENT_CONTEXT(ctx
);
1040 struct gl_debug_state
*debug
;
1041 const char *callerstr
;
1044 if (_mesa_is_desktop_gl(ctx
))
1045 callerstr
= "glGetDebugMessageLog";
1047 callerstr
= "glGetDebugMessageLogKHR";
1053 _mesa_error(ctx
, GL_INVALID_VALUE
,
1054 "%s(logSize=%d : logSize must not be negative)",
1055 callerstr
, logSize
);
1059 debug
= _mesa_lock_debug_state(ctx
);
1063 for (ret
= 0; ret
< count
; ret
++) {
1064 const struct gl_debug_message
*msg
= debug_fetch_message(debug
);
1072 len
= strlen(msg
->message
);
1074 if (logSize
< len
+1 && messageLog
!= NULL
)
1078 assert(msg
->message
[len
] == '\0');
1079 (void) strncpy(messageLog
, msg
->message
, (size_t)len
+1);
1081 messageLog
+= len
+1;
1088 *severities
++ = debug_severity_enums
[msg
->severity
];
1090 *sources
++ = debug_source_enums
[msg
->source
];
1092 *types
++ = debug_type_enums
[msg
->type
];
1096 debug_delete_messages(debug
, 1);
1099 _mesa_unlock_debug_state(ctx
);
1106 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
1107 GLenum gl_severity
, GLsizei count
,
1108 const GLuint
*ids
, GLboolean enabled
)
1110 GET_CURRENT_CONTEXT(ctx
);
1111 enum mesa_debug_source source
= gl_enum_to_debug_source(gl_source
);
1112 enum mesa_debug_type type
= gl_enum_to_debug_type(gl_type
);
1113 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(gl_severity
);
1114 const char *callerstr
;
1115 struct gl_debug_state
*debug
;
1117 if (_mesa_is_desktop_gl(ctx
))
1118 callerstr
= "glDebugMessageControl";
1120 callerstr
= "glDebugMessageControlKHR";
1123 _mesa_error(ctx
, GL_INVALID_VALUE
,
1124 "%s(count=%d : count must not be negative)", callerstr
,
1129 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
1131 return; /* GL_INVALID_ENUM */
1133 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
1134 || gl_source
== GL_DONT_CARE
)) {
1135 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1136 "%s(When passing an array of ids, severity must be"
1137 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
1142 debug
= _mesa_lock_debug_state(ctx
);
1148 for (i
= 0; i
< count
; i
++)
1149 debug_set_message_enable(debug
, source
, type
, ids
[i
], enabled
);
1152 debug_set_message_enable_all(debug
, source
, type
, severity
, enabled
);
1155 _mesa_unlock_debug_state(ctx
);
1160 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
1162 GET_CURRENT_CONTEXT(ctx
);
1163 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
1165 debug
->Callback
= callback
;
1166 debug
->CallbackData
= userParam
;
1167 _mesa_unlock_debug_state(ctx
);
1173 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
1174 const GLchar
*message
)
1176 GET_CURRENT_CONTEXT(ctx
);
1177 const char *callerstr
;
1178 struct gl_debug_state
*debug
;
1179 struct gl_debug_message
*emptySlot
;
1181 if (_mesa_is_desktop_gl(ctx
))
1182 callerstr
= "glPushDebugGroup";
1184 callerstr
= "glPushDebugGroupKHR";
1187 case GL_DEBUG_SOURCE_APPLICATION
:
1188 case GL_DEBUG_SOURCE_THIRD_PARTY
:
1191 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
1192 "(source=0x%x)", callerstr
, source
);
1196 if (!validate_length(ctx
, callerstr
, length
, message
))
1197 return; /* GL_INVALID_VALUE */
1200 length
= strlen(message
);
1202 debug
= _mesa_lock_debug_state(ctx
);
1206 if (debug
->CurrentGroup
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
1207 _mesa_unlock_debug_state(ctx
);
1208 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
1212 /* pop reuses the message details from push so we store this */
1213 emptySlot
= debug_get_group_message(debug
);
1214 debug_message_store(emptySlot
,
1215 gl_enum_to_debug_source(source
),
1216 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
1218 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1221 debug_push_group(debug
);
1223 log_msg_locked_and_unlock(ctx
,
1224 gl_enum_to_debug_source(source
),
1225 MESA_DEBUG_TYPE_PUSH_GROUP
, id
,
1226 MESA_DEBUG_SEVERITY_NOTIFICATION
, length
,
1232 _mesa_PopDebugGroup(void)
1234 GET_CURRENT_CONTEXT(ctx
);
1235 const char *callerstr
;
1236 struct gl_debug_state
*debug
;
1237 struct gl_debug_message
*gdmessage
, msg
;
1239 if (_mesa_is_desktop_gl(ctx
))
1240 callerstr
= "glPopDebugGroup";
1242 callerstr
= "glPopDebugGroupKHR";
1244 debug
= _mesa_lock_debug_state(ctx
);
1248 if (debug
->CurrentGroup
<= 0) {
1249 _mesa_unlock_debug_state(ctx
);
1250 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
1254 debug_pop_group(debug
);
1256 /* make a shallow copy */
1257 gdmessage
= debug_get_group_message(debug
);
1259 gdmessage
->message
= NULL
;
1260 gdmessage
->length
= 0;
1262 log_msg_locked_and_unlock(ctx
,
1264 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
1266 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1267 msg
.length
, msg
.message
);
1269 debug_message_clear(&msg
);
1274 _mesa_init_debug_output(struct gl_context
*ctx
)
1276 mtx_init(&ctx
->DebugMutex
, mtx_plain
);
1278 if (MESA_DEBUG_FLAGS
& DEBUG_CONTEXT
) {
1279 /* If the MESA_DEBUG env is set to "context", we'll turn on the
1280 * GL_CONTEXT_FLAG_DEBUG_BIT context flag and log debug output
1281 * messages to stderr (or whatever MESA_LOG_FILE points at).
1283 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
1287 debug
->DebugOutput
= GL_TRUE
;
1288 debug
->LogToStderr
= GL_TRUE
;
1289 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
1290 _mesa_unlock_debug_state(ctx
);
1296 _mesa_free_errors_data(struct gl_context
*ctx
)
1299 debug_destroy(ctx
->Debug
);
1300 /* set to NULL just in case it is used before context is completely gone. */
1304 mtx_destroy(&ctx
->DebugMutex
);
1308 _mesa_StringMarkerGREMEDY(GLsizei len
, const GLvoid
*string
)
1310 GET_CURRENT_CONTEXT(ctx
);
1311 if (ctx
->Extensions
.GREMEDY_string_marker
) {
1312 /* if length not specified, string will be null terminated: */
1314 len
= strlen(string
);
1315 ctx
->Driver
.EmitStringMarker(ctx
, string
, len
);
1317 _mesa_error(ctx
, GL_INVALID_OPERATION
, "StringMarkerGREMEDY");