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/simple_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 simple_node link
;
51 /* at which severity levels (mesa_debug_severity) is the message enabled */
56 struct gl_debug_namespace
58 struct simple_node 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 make_empty_list(&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 struct simple_node
*node
, *tmp
;
266 foreach_s(node
, tmp
, &ns
->Elements
)
271 debug_namespace_copy(struct gl_debug_namespace
*dst
,
272 const struct gl_debug_namespace
*src
)
274 struct simple_node
*node
;
276 dst
->DefaultState
= src
->DefaultState
;
278 make_empty_list(&dst
->Elements
);
279 foreach(node
, &src
->Elements
) {
280 const struct gl_debug_element
*elem
=
281 (const struct gl_debug_element
*) node
;
282 struct gl_debug_element
*copy
;
284 copy
= malloc(sizeof(*copy
));
286 debug_namespace_clear(dst
);
291 copy
->State
= elem
->State
;
292 insert_at_tail(&dst
->Elements
, ©
->link
);
299 * Set the state of \p id in the namespace.
302 debug_namespace_set(struct gl_debug_namespace
*ns
,
303 GLuint id
, bool enabled
)
305 const uint32_t state
= (enabled
) ?
306 ((1 << MESA_DEBUG_SEVERITY_COUNT
) - 1) : 0;
307 struct gl_debug_element
*elem
= NULL
;
308 struct simple_node
*node
;
310 /* find the element */
311 foreach(node
, &ns
->Elements
) {
312 struct gl_debug_element
*tmp
= (struct gl_debug_element
*) node
;
319 /* we do not need the element if it has the default state */
320 if (ns
->DefaultState
== state
) {
322 remove_from_list(&elem
->link
);
329 elem
= malloc(sizeof(*elem
));
334 insert_at_tail(&ns
->Elements
, &elem
->link
);
343 * Set the default state of the namespace for \p severity. When \p severity
344 * is MESA_DEBUG_SEVERITY_COUNT, the default values for all severities are
348 debug_namespace_set_all(struct gl_debug_namespace
*ns
,
349 enum mesa_debug_severity severity
,
352 struct simple_node
*node
, *tmp
;
355 /* set all elements to the same state */
356 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
357 ns
->DefaultState
= (enabled
) ? ((1 << severity
) - 1) : 0;
358 debug_namespace_clear(ns
);
359 make_empty_list(&ns
->Elements
);
363 mask
= 1 << severity
;
364 val
= (enabled
) ? mask
: 0;
366 ns
->DefaultState
= (ns
->DefaultState
& ~mask
) | val
;
368 foreach_s(node
, tmp
, &ns
->Elements
) {
369 struct gl_debug_element
*elem
= (struct gl_debug_element
*) node
;
371 elem
->State
= (elem
->State
& ~mask
) | val
;
372 if (elem
->State
== ns
->DefaultState
) {
373 remove_from_list(node
);
380 * Get the state of \p id in the namespace.
383 debug_namespace_get(const struct gl_debug_namespace
*ns
, GLuint id
,
384 enum mesa_debug_severity severity
)
386 struct simple_node
*node
;
389 state
= ns
->DefaultState
;
390 foreach(node
, &ns
->Elements
) {
391 struct gl_debug_element
*elem
= (struct gl_debug_element
*) node
;
393 if (elem
->ID
== id
) {
399 return (state
& (1 << severity
));
403 * Allocate and initialize context debug state.
405 static struct gl_debug_state
*
408 struct gl_debug_state
*debug
;
411 debug
= CALLOC_STRUCT(gl_debug_state
);
415 debug
->Groups
[0] = malloc(sizeof(*debug
->Groups
[0]));
416 if (!debug
->Groups
[0]) {
421 /* Initialize state for filtering known debug messages. */
422 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
423 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
424 debug_namespace_init(&debug
->Groups
[0]->Namespaces
[s
][t
]);
431 * Return true if the top debug group points to the group below it.
434 debug_is_group_read_only(const struct gl_debug_state
*debug
)
436 const GLint gstack
= debug
->CurrentGroup
;
437 return (gstack
> 0 && debug
->Groups
[gstack
] == debug
->Groups
[gstack
- 1]);
441 * Make the top debug group writable.
444 debug_make_group_writable(struct gl_debug_state
*debug
)
446 const GLint gstack
= debug
->CurrentGroup
;
447 const struct gl_debug_group
*src
= debug
->Groups
[gstack
];
448 struct gl_debug_group
*dst
;
451 if (!debug_is_group_read_only(debug
))
454 dst
= malloc(sizeof(*dst
));
458 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
459 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
460 if (!debug_namespace_copy(&dst
->Namespaces
[s
][t
],
461 &src
->Namespaces
[s
][t
])) {
463 for (t
= t
- 1; t
>= 0; t
--)
464 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
465 for (s
= s
- 1; s
>= 0; s
--) {
466 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
467 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
475 debug
->Groups
[gstack
] = dst
;
481 * Free the top debug group.
484 debug_clear_group(struct gl_debug_state
*debug
)
486 const GLint gstack
= debug
->CurrentGroup
;
488 if (!debug_is_group_read_only(debug
)) {
489 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
492 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
493 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
494 debug_namespace_clear(&grp
->Namespaces
[s
][t
]);
500 debug
->Groups
[gstack
] = NULL
;
504 * Loop through debug group stack tearing down states for
505 * filtering debug messages. Then free debug output state.
508 debug_destroy(struct gl_debug_state
*debug
)
510 while (debug
->CurrentGroup
> 0) {
511 debug_clear_group(debug
);
512 debug
->CurrentGroup
--;
515 debug_clear_group(debug
);
520 * Sets the state of the given message source/type/ID tuple.
523 debug_set_message_enable(struct gl_debug_state
*debug
,
524 enum mesa_debug_source source
,
525 enum mesa_debug_type type
,
526 GLuint id
, GLboolean enabled
)
528 const GLint gstack
= debug
->CurrentGroup
;
529 struct gl_debug_namespace
*ns
;
531 debug_make_group_writable(debug
);
532 ns
= &debug
->Groups
[gstack
]->Namespaces
[source
][type
];
534 debug_namespace_set(ns
, id
, enabled
);
538 * Set the state of all message IDs found in the given intersection of
539 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
540 * GL_DONT_CARE (include all messages in the class).
542 * This requires both setting the state of all previously seen message
543 * IDs in the hash table, and setting the default state for all
544 * applicable combinations of source/type/severity, so that all the
545 * yet-unknown message IDs that may be used in the future will be
546 * impacted as if they were already known.
549 debug_set_message_enable_all(struct gl_debug_state
*debug
,
550 enum mesa_debug_source source
,
551 enum mesa_debug_type type
,
552 enum mesa_debug_severity severity
,
555 const GLint gstack
= debug
->CurrentGroup
;
556 int s
, t
, smax
, tmax
;
558 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
560 smax
= MESA_DEBUG_SOURCE_COUNT
;
565 if (type
== MESA_DEBUG_TYPE_COUNT
) {
567 tmax
= MESA_DEBUG_TYPE_COUNT
;
572 debug_make_group_writable(debug
);
574 for (s
= source
; s
< smax
; s
++) {
575 for (t
= type
; t
< tmax
; t
++) {
576 struct gl_debug_namespace
*nspace
=
577 &debug
->Groups
[gstack
]->Namespaces
[s
][t
];
578 debug_namespace_set_all(nspace
, severity
, enabled
);
584 * Returns if the given message source/type/ID tuple is enabled.
587 _mesa_debug_is_message_enabled(const struct gl_debug_state
*debug
,
588 enum mesa_debug_source source
,
589 enum mesa_debug_type type
,
591 enum mesa_debug_severity severity
)
593 const GLint gstack
= debug
->CurrentGroup
;
594 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
595 struct gl_debug_namespace
*nspace
= &grp
->Namespaces
[source
][type
];
597 if (!debug
->DebugOutput
)
600 return debug_namespace_get(nspace
, id
, severity
);
604 * 'buf' is not necessarily a null-terminated string. When logging, copy
605 * 'len' characters from it, store them in a new, null-terminated string,
606 * and remember the number of bytes used by that string, *including*
607 * the null terminator this time.
610 debug_log_message(struct gl_debug_state
*debug
,
611 enum mesa_debug_source source
,
612 enum mesa_debug_type type
, GLuint id
,
613 enum mesa_debug_severity severity
,
614 GLsizei len
, const char *buf
)
616 struct gl_debug_log
*log
= &debug
->Log
;
618 struct gl_debug_message
*emptySlot
;
620 if (debug
->LogToStderr
) {
621 _mesa_log("Mesa debug output: %.*s\n", len
, buf
);
624 assert(len
< MAX_DEBUG_MESSAGE_LENGTH
);
626 if (log
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
629 nextEmpty
= (log
->NextMessage
+ log
->NumMessages
)
630 % MAX_DEBUG_LOGGED_MESSAGES
;
631 emptySlot
= &log
->Messages
[nextEmpty
];
633 debug_message_store(emptySlot
, source
, type
,
634 id
, severity
, len
, buf
);
640 * Return the oldest debug message out of the log.
642 static const struct gl_debug_message
*
643 debug_fetch_message(const struct gl_debug_state
*debug
)
645 const struct gl_debug_log
*log
= &debug
->Log
;
647 return (log
->NumMessages
) ? &log
->Messages
[log
->NextMessage
] : NULL
;
651 * Delete the oldest debug messages out of the log.
654 debug_delete_messages(struct gl_debug_state
*debug
, int count
)
656 struct gl_debug_log
*log
= &debug
->Log
;
658 if (count
> log
->NumMessages
)
659 count
= log
->NumMessages
;
662 struct gl_debug_message
*msg
= &log
->Messages
[log
->NextMessage
];
664 debug_message_clear(msg
);
668 log
->NextMessage
%= MAX_DEBUG_LOGGED_MESSAGES
;
672 static struct gl_debug_message
*
673 debug_get_group_message(struct gl_debug_state
*debug
)
675 return &debug
->GroupMessages
[debug
->CurrentGroup
];
679 debug_push_group(struct gl_debug_state
*debug
)
681 const GLint gstack
= debug
->CurrentGroup
;
683 /* just point to the previous stack */
684 debug
->Groups
[gstack
+ 1] = debug
->Groups
[gstack
];
685 debug
->CurrentGroup
++;
689 debug_pop_group(struct gl_debug_state
*debug
)
691 debug_clear_group(debug
);
692 debug
->CurrentGroup
--;
697 * Lock and return debug state for the context. The debug state will be
698 * allocated and initialized upon the first call. When NULL is returned, the
699 * debug state is not locked.
701 static struct gl_debug_state
*
702 _mesa_lock_debug_state(struct gl_context
*ctx
)
704 simple_mtx_lock(&ctx
->DebugMutex
);
707 ctx
->Debug
= debug_create();
709 GET_CURRENT_CONTEXT(cur
);
710 simple_mtx_unlock(&ctx
->DebugMutex
);
713 * This function may be called from other threads. When that is the
714 * case, we cannot record this OOM error.
717 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
727 _mesa_unlock_debug_state(struct gl_context
*ctx
)
729 simple_mtx_unlock(&ctx
->DebugMutex
);
733 * Set the integer debug state specified by \p pname. This can be called from
734 * _mesa_set_enable for example.
737 _mesa_set_debug_state_int(struct gl_context
*ctx
, GLenum pname
, GLint val
)
739 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
745 case GL_DEBUG_OUTPUT
:
746 debug
->DebugOutput
= (val
!= 0);
748 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
749 debug
->SyncOutput
= (val
!= 0);
752 assert(!"unknown debug output param");
756 _mesa_unlock_debug_state(ctx
);
762 * Query the integer debug state specified by \p pname. This can be called
763 * _mesa_GetIntegerv for example.
766 _mesa_get_debug_state_int(struct gl_context
*ctx
, GLenum pname
)
770 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
775 case GL_DEBUG_OUTPUT
:
776 val
= debug
->DebugOutput
;
778 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
779 val
= debug
->SyncOutput
;
781 case GL_DEBUG_LOGGED_MESSAGES
:
782 val
= debug
->Log
.NumMessages
;
784 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH
:
785 val
= (debug
->Log
.NumMessages
) ?
786 debug
->Log
.Messages
[debug
->Log
.NextMessage
].length
+ 1 : 0;
788 case GL_DEBUG_GROUP_STACK_DEPTH
:
789 val
= debug
->CurrentGroup
+ 1;
792 assert(!"unknown debug output param");
797 _mesa_unlock_debug_state(ctx
);
803 * Query the pointer debug state specified by \p pname. This can be called
804 * _mesa_GetPointerv for example.
807 _mesa_get_debug_state_ptr(struct gl_context
*ctx
, GLenum pname
)
810 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
816 case GL_DEBUG_CALLBACK_FUNCTION_ARB
:
817 val
= (void *) debug
->Callback
;
819 case GL_DEBUG_CALLBACK_USER_PARAM_ARB
:
820 val
= (void *) debug
->CallbackData
;
823 assert(!"unknown debug output param");
828 _mesa_unlock_debug_state(ctx
);
834 * Insert a debug message. The mutex is assumed to be locked, and will be
835 * unlocked by this call.
838 log_msg_locked_and_unlock(struct gl_context
*ctx
,
839 enum mesa_debug_source source
,
840 enum mesa_debug_type type
, GLuint id
,
841 enum mesa_debug_severity severity
,
842 GLint len
, const char *buf
)
844 struct gl_debug_state
*debug
= ctx
->Debug
;
846 if (!_mesa_debug_is_message_enabled(debug
, source
, type
, id
, severity
)) {
847 _mesa_unlock_debug_state(ctx
);
851 if (ctx
->Debug
->Callback
) {
852 /* Call the user's callback function */
853 GLenum gl_source
= debug_source_enums
[source
];
854 GLenum gl_type
= debug_type_enums
[type
];
855 GLenum gl_severity
= debug_severity_enums
[severity
];
856 GLDEBUGPROC callback
= ctx
->Debug
->Callback
;
857 const void *data
= ctx
->Debug
->CallbackData
;
860 * When ctx->Debug->SyncOutput is GL_FALSE, the client is prepared for
861 * unsynchronous calls. When it is GL_TRUE, we will not spawn threads.
862 * In either case, we can call the callback unlocked.
864 _mesa_unlock_debug_state(ctx
);
865 callback(gl_source
, gl_type
, id
, gl_severity
, len
, buf
, data
);
868 /* add debug message to queue */
869 debug_log_message(ctx
->Debug
, source
, type
, id
, severity
, len
, buf
);
870 _mesa_unlock_debug_state(ctx
);
875 * Log a client or driver debug message.
878 _mesa_log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
879 enum mesa_debug_type type
, GLuint id
,
880 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
882 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
887 log_msg_locked_and_unlock(ctx
, source
, type
, id
, severity
, len
, buf
);
892 * Verify that source, type, and severity are valid enums.
894 * The 'caller' param is used for handling values available
895 * only in glDebugMessageInsert or glDebugMessageControl
898 validate_params(struct gl_context
*ctx
, unsigned caller
,
899 const char *callerstr
, GLenum source
, GLenum type
,
905 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
906 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
908 case GL_DEBUG_SOURCE_API_ARB
:
909 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
910 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
911 case GL_DEBUG_SOURCE_OTHER_ARB
:
912 if (caller
!= INSERT
)
917 if (caller
== CONTROL
)
926 case GL_DEBUG_TYPE_ERROR_ARB
:
927 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
928 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
929 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
930 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
931 case GL_DEBUG_TYPE_OTHER_ARB
:
932 case GL_DEBUG_TYPE_MARKER
:
933 case GL_DEBUG_TYPE_PUSH_GROUP
:
934 case GL_DEBUG_TYPE_POP_GROUP
:
937 if (caller
== CONTROL
)
946 case GL_DEBUG_SEVERITY_HIGH_ARB
:
947 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
948 case GL_DEBUG_SEVERITY_LOW_ARB
:
949 case GL_DEBUG_SEVERITY_NOTIFICATION
:
952 if (caller
== CONTROL
)
962 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
963 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
964 source
, type
, severity
);
971 validate_length(struct gl_context
*ctx
, const char *callerstr
, GLsizei length
,
976 GLsizei len
= strlen(buf
);
978 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
979 _mesa_error(ctx
, GL_INVALID_VALUE
,
980 "%s(null terminated string length=%d, is not less than "
981 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, len
,
982 MAX_DEBUG_MESSAGE_LENGTH
);
987 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
988 _mesa_error(ctx
, GL_INVALID_VALUE
,
989 "%s(length=%d, which is not less than "
990 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
991 MAX_DEBUG_MESSAGE_LENGTH
);
1000 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
1001 GLenum severity
, GLint length
,
1004 GET_CURRENT_CONTEXT(ctx
);
1005 const char *callerstr
;
1007 if (_mesa_is_desktop_gl(ctx
))
1008 callerstr
= "glDebugMessageInsert";
1010 callerstr
= "glDebugMessageInsertKHR";
1012 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
1013 return; /* GL_INVALID_ENUM */
1015 if (!validate_length(ctx
, callerstr
, length
, buf
))
1016 return; /* GL_INVALID_VALUE */
1018 /* if length not specified, string will be null terminated: */
1020 length
= strlen(buf
);
1022 _mesa_log_msg(ctx
, gl_enum_to_debug_source(source
),
1023 gl_enum_to_debug_type(type
), id
,
1024 gl_enum_to_debug_severity(severity
),
1027 if (type
== GL_DEBUG_TYPE_MARKER
&& ctx
->Driver
.EmitStringMarker
) {
1028 ctx
->Driver
.EmitStringMarker(ctx
, buf
, length
);
1034 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
1035 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
1036 GLsizei
*lengths
, GLchar
*messageLog
)
1038 GET_CURRENT_CONTEXT(ctx
);
1039 struct gl_debug_state
*debug
;
1040 const char *callerstr
;
1043 if (_mesa_is_desktop_gl(ctx
))
1044 callerstr
= "glGetDebugMessageLog";
1046 callerstr
= "glGetDebugMessageLogKHR";
1052 _mesa_error(ctx
, GL_INVALID_VALUE
,
1053 "%s(logSize=%d : logSize must not be negative)",
1054 callerstr
, logSize
);
1058 debug
= _mesa_lock_debug_state(ctx
);
1062 for (ret
= 0; ret
< count
; ret
++) {
1063 const struct gl_debug_message
*msg
= debug_fetch_message(debug
);
1071 len
= strlen(msg
->message
);
1073 if (logSize
< len
+1 && messageLog
!= NULL
)
1077 assert(msg
->message
[len
] == '\0');
1078 (void) strncpy(messageLog
, msg
->message
, (size_t)len
+1);
1080 messageLog
+= len
+1;
1087 *severities
++ = debug_severity_enums
[msg
->severity
];
1089 *sources
++ = debug_source_enums
[msg
->source
];
1091 *types
++ = debug_type_enums
[msg
->type
];
1095 debug_delete_messages(debug
, 1);
1098 _mesa_unlock_debug_state(ctx
);
1105 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
1106 GLenum gl_severity
, GLsizei count
,
1107 const GLuint
*ids
, GLboolean enabled
)
1109 GET_CURRENT_CONTEXT(ctx
);
1110 enum mesa_debug_source source
= gl_enum_to_debug_source(gl_source
);
1111 enum mesa_debug_type type
= gl_enum_to_debug_type(gl_type
);
1112 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(gl_severity
);
1113 const char *callerstr
;
1114 struct gl_debug_state
*debug
;
1116 if (_mesa_is_desktop_gl(ctx
))
1117 callerstr
= "glDebugMessageControl";
1119 callerstr
= "glDebugMessageControlKHR";
1122 _mesa_error(ctx
, GL_INVALID_VALUE
,
1123 "%s(count=%d : count must not be negative)", callerstr
,
1128 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
1130 return; /* GL_INVALID_ENUM */
1132 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
1133 || gl_source
== GL_DONT_CARE
)) {
1134 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1135 "%s(When passing an array of ids, severity must be"
1136 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
1141 debug
= _mesa_lock_debug_state(ctx
);
1147 for (i
= 0; i
< count
; i
++)
1148 debug_set_message_enable(debug
, source
, type
, ids
[i
], enabled
);
1151 debug_set_message_enable_all(debug
, source
, type
, severity
, enabled
);
1154 _mesa_unlock_debug_state(ctx
);
1159 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
1161 GET_CURRENT_CONTEXT(ctx
);
1162 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
1164 debug
->Callback
= callback
;
1165 debug
->CallbackData
= userParam
;
1166 _mesa_unlock_debug_state(ctx
);
1172 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
1173 const GLchar
*message
)
1175 GET_CURRENT_CONTEXT(ctx
);
1176 const char *callerstr
;
1177 struct gl_debug_state
*debug
;
1178 struct gl_debug_message
*emptySlot
;
1180 if (_mesa_is_desktop_gl(ctx
))
1181 callerstr
= "glPushDebugGroup";
1183 callerstr
= "glPushDebugGroupKHR";
1186 case GL_DEBUG_SOURCE_APPLICATION
:
1187 case GL_DEBUG_SOURCE_THIRD_PARTY
:
1190 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
1191 "(source=0x%x)", callerstr
, source
);
1195 if (!validate_length(ctx
, callerstr
, length
, message
))
1196 return; /* GL_INVALID_VALUE */
1199 length
= strlen(message
);
1201 debug
= _mesa_lock_debug_state(ctx
);
1205 if (debug
->CurrentGroup
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
1206 _mesa_unlock_debug_state(ctx
);
1207 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
1211 /* pop reuses the message details from push so we store this */
1212 emptySlot
= debug_get_group_message(debug
);
1213 debug_message_store(emptySlot
,
1214 gl_enum_to_debug_source(source
),
1215 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
1217 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1220 debug_push_group(debug
);
1222 log_msg_locked_and_unlock(ctx
,
1223 gl_enum_to_debug_source(source
),
1224 MESA_DEBUG_TYPE_PUSH_GROUP
, id
,
1225 MESA_DEBUG_SEVERITY_NOTIFICATION
, length
,
1231 _mesa_PopDebugGroup(void)
1233 GET_CURRENT_CONTEXT(ctx
);
1234 const char *callerstr
;
1235 struct gl_debug_state
*debug
;
1236 struct gl_debug_message
*gdmessage
, msg
;
1238 if (_mesa_is_desktop_gl(ctx
))
1239 callerstr
= "glPopDebugGroup";
1241 callerstr
= "glPopDebugGroupKHR";
1243 debug
= _mesa_lock_debug_state(ctx
);
1247 if (debug
->CurrentGroup
<= 0) {
1248 _mesa_unlock_debug_state(ctx
);
1249 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
1253 debug_pop_group(debug
);
1255 /* make a shallow copy */
1256 gdmessage
= debug_get_group_message(debug
);
1258 gdmessage
->message
= NULL
;
1259 gdmessage
->length
= 0;
1261 log_msg_locked_and_unlock(ctx
,
1263 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
1265 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1266 msg
.length
, msg
.message
);
1268 debug_message_clear(&msg
);
1273 _mesa_init_debug_output(struct gl_context
*ctx
)
1275 simple_mtx_init(&ctx
->DebugMutex
, mtx_plain
);
1277 if (MESA_DEBUG_FLAGS
& DEBUG_CONTEXT
) {
1278 /* If the MESA_DEBUG env is set to "context", we'll turn on the
1279 * GL_CONTEXT_FLAG_DEBUG_BIT context flag and log debug output
1280 * messages to stderr (or whatever MESA_LOG_FILE points at).
1282 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
1286 debug
->DebugOutput
= GL_TRUE
;
1287 debug
->LogToStderr
= GL_TRUE
;
1288 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
1289 _mesa_unlock_debug_state(ctx
);
1295 _mesa_free_errors_data(struct gl_context
*ctx
)
1298 debug_destroy(ctx
->Debug
);
1299 /* set to NULL just in case it is used before context is completely gone. */
1303 simple_mtx_destroy(&ctx
->DebugMutex
);
1307 _mesa_StringMarkerGREMEDY(GLsizei len
, const GLvoid
*string
)
1309 GET_CURRENT_CONTEXT(ctx
);
1310 if (ctx
->Extensions
.GREMEDY_string_marker
) {
1311 /* if length not specified, string will be null terminated: */
1313 len
= strlen(string
);
1314 ctx
->Driver
.EmitStringMarker(ctx
, string
, len
);
1316 _mesa_error(ctx
, GL_INVALID_OPERATION
, "StringMarkerGREMEDY");