2 * Copyright © 2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file performance_monitor.c
26 * Core Mesa support for the AMD_performance_monitor extension.
28 * In order to implement this extension, start by defining two enums:
29 * one for Groups, and one for Counters. These will be used as indexes into
30 * arrays, so they should start at 0 and increment from there.
32 * Counter IDs need to be globally unique. That is, you can't have counter 7
33 * in group A and counter 7 in group B. A global enum of all available
34 * counters is a convenient way to guarantee this.
44 #include "performance_monitor.h"
45 #include "util/bitset.h"
46 #include "util/ralloc.h"
49 _mesa_init_performance_monitors(struct gl_context
*ctx
)
51 ctx
->PerfMonitor
.Monitors
= _mesa_NewHashTable();
52 ctx
->PerfMonitor
.NumGroups
= 0;
53 ctx
->PerfMonitor
.Groups
= NULL
;
56 static struct gl_perf_monitor_object
*
57 new_performance_monitor(struct gl_context
*ctx
, GLuint index
)
60 struct gl_perf_monitor_object
*m
= ctx
->Driver
.NewPerfMonitor(ctx
);
70 rzalloc_array(NULL
, unsigned, ctx
->PerfMonitor
.NumGroups
);
73 ralloc_array(NULL
, BITSET_WORD
*, ctx
->PerfMonitor
.NumGroups
);
75 if (m
->ActiveGroups
== NULL
|| m
->ActiveCounters
== NULL
)
78 for (i
= 0; i
< ctx
->PerfMonitor
.NumGroups
; i
++) {
79 const struct gl_perf_monitor_group
*g
= &ctx
->PerfMonitor
.Groups
[i
];
81 m
->ActiveCounters
[i
] = rzalloc_array(m
->ActiveCounters
, BITSET_WORD
,
82 BITSET_WORDS(g
->NumCounters
));
83 if (m
->ActiveCounters
[i
] == NULL
)
90 ralloc_free(m
->ActiveGroups
);
91 ralloc_free(m
->ActiveCounters
);
92 ctx
->Driver
.DeletePerfMonitor(ctx
, m
);
97 free_performance_monitor(GLuint key
, void *data
, void *user
)
99 struct gl_perf_monitor_object
*m
= data
;
100 struct gl_context
*ctx
= user
;
102 ralloc_free(m
->ActiveGroups
);
103 ralloc_free(m
->ActiveCounters
);
104 ctx
->Driver
.DeletePerfMonitor(ctx
, m
);
108 _mesa_free_performance_monitors(struct gl_context
*ctx
)
110 _mesa_HashDeleteAll(ctx
->PerfMonitor
.Monitors
,
111 free_performance_monitor
, ctx
);
112 _mesa_DeleteHashTable(ctx
->PerfMonitor
.Monitors
);
115 static inline struct gl_perf_monitor_object
*
116 lookup_monitor(struct gl_context
*ctx
, GLuint id
)
118 return (struct gl_perf_monitor_object
*)
119 _mesa_HashLookup(ctx
->PerfMonitor
.Monitors
, id
);
122 static inline const struct gl_perf_monitor_group
*
123 get_group(const struct gl_context
*ctx
, GLuint id
)
125 if (id
>= ctx
->PerfMonitor
.NumGroups
)
128 return &ctx
->PerfMonitor
.Groups
[id
];
131 static inline const struct gl_perf_monitor_counter
*
132 get_counter(const struct gl_perf_monitor_group
*group_obj
, GLuint id
)
134 if (id
>= group_obj
->NumCounters
)
137 return &group_obj
->Counters
[id
];
140 /* For INTEL_performance_query, query id 0 is reserved to be invalid. We use
141 * index to Groups array + 1 as the query id. Same applies to counter id.
144 queryid_to_index(GLuint queryid
)
150 index_to_queryid(GLuint index
)
156 queryid_valid(const struct gl_context
*ctx
, GLuint queryid
)
158 return get_group(ctx
, queryid_to_index(queryid
)) != NULL
;
162 counterid_to_index(GLuint counterid
)
164 return counterid
- 1;
167 /*****************************************************************************/
170 _mesa_GetPerfMonitorGroupsAMD(GLint
*numGroups
, GLsizei groupsSize
,
173 GET_CURRENT_CONTEXT(ctx
);
175 if (numGroups
!= NULL
)
176 *numGroups
= ctx
->PerfMonitor
.NumGroups
;
178 if (groupsSize
> 0 && groups
!= NULL
) {
180 unsigned n
= MIN2((GLuint
) groupsSize
, ctx
->PerfMonitor
.NumGroups
);
182 /* We just use the index in the Groups array as the ID. */
183 for (i
= 0; i
< n
; i
++)
189 _mesa_GetPerfMonitorCountersAMD(GLuint group
, GLint
*numCounters
,
190 GLint
*maxActiveCounters
,
191 GLsizei countersSize
, GLuint
*counters
)
193 GET_CURRENT_CONTEXT(ctx
);
194 const struct gl_perf_monitor_group
*group_obj
= get_group(ctx
, group
);
195 if (group_obj
== NULL
) {
196 _mesa_error(ctx
, GL_INVALID_VALUE
,
197 "glGetPerfMonitorCountersAMD(invalid group)");
201 if (maxActiveCounters
!= NULL
)
202 *maxActiveCounters
= group_obj
->MaxActiveCounters
;
204 if (numCounters
!= NULL
)
205 *numCounters
= group_obj
->NumCounters
;
207 if (counters
!= NULL
) {
209 unsigned n
= MIN2(group_obj
->NumCounters
, (GLuint
) countersSize
);
210 for (i
= 0; i
< n
; i
++) {
211 /* We just use the index in the Counters array as the ID. */
218 _mesa_GetPerfMonitorGroupStringAMD(GLuint group
, GLsizei bufSize
,
219 GLsizei
*length
, GLchar
*groupString
)
221 GET_CURRENT_CONTEXT(ctx
);
223 const struct gl_perf_monitor_group
*group_obj
= get_group(ctx
, group
);
225 if (group_obj
== NULL
) {
226 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetPerfMonitorGroupStringAMD");
231 /* Return the number of characters that would be required to hold the
232 * group string, excluding the null terminator.
235 *length
= strlen(group_obj
->Name
);
238 *length
= MIN2(strlen(group_obj
->Name
), bufSize
);
239 if (groupString
!= NULL
)
240 strncpy(groupString
, group_obj
->Name
, bufSize
);
245 _mesa_GetPerfMonitorCounterStringAMD(GLuint group
, GLuint counter
,
246 GLsizei bufSize
, GLsizei
*length
,
247 GLchar
*counterString
)
249 GET_CURRENT_CONTEXT(ctx
);
251 const struct gl_perf_monitor_group
*group_obj
;
252 const struct gl_perf_monitor_counter
*counter_obj
;
254 group_obj
= get_group(ctx
, group
);
256 if (group_obj
== NULL
) {
257 _mesa_error(ctx
, GL_INVALID_VALUE
,
258 "glGetPerfMonitorCounterStringAMD(invalid group)");
262 counter_obj
= get_counter(group_obj
, counter
);
264 if (counter_obj
== NULL
) {
265 _mesa_error(ctx
, GL_INVALID_VALUE
,
266 "glGetPerfMonitorCounterStringAMD(invalid counter)");
271 /* Return the number of characters that would be required to hold the
272 * counter string, excluding the null terminator.
275 *length
= strlen(counter_obj
->Name
);
278 *length
= MIN2(strlen(counter_obj
->Name
), bufSize
);
279 if (counterString
!= NULL
)
280 strncpy(counterString
, counter_obj
->Name
, bufSize
);
285 _mesa_GetPerfMonitorCounterInfoAMD(GLuint group
, GLuint counter
, GLenum pname
,
288 GET_CURRENT_CONTEXT(ctx
);
290 const struct gl_perf_monitor_group
*group_obj
;
291 const struct gl_perf_monitor_counter
*counter_obj
;
293 group_obj
= get_group(ctx
, group
);
295 if (group_obj
== NULL
) {
296 _mesa_error(ctx
, GL_INVALID_VALUE
,
297 "glGetPerfMonitorCounterInfoAMD(invalid group)");
301 counter_obj
= get_counter(group_obj
, counter
);
303 if (counter_obj
== NULL
) {
304 _mesa_error(ctx
, GL_INVALID_VALUE
,
305 "glGetPerfMonitorCounterInfoAMD(invalid counter)");
310 case GL_COUNTER_TYPE_AMD
:
311 *((GLenum
*) data
) = counter_obj
->Type
;
314 case GL_COUNTER_RANGE_AMD
:
315 switch (counter_obj
->Type
) {
317 case GL_PERCENTAGE_AMD
: {
318 float *f_data
= data
;
319 f_data
[0] = counter_obj
->Minimum
.f
;
320 f_data
[1] = counter_obj
->Maximum
.f
;
323 case GL_UNSIGNED_INT
: {
324 uint32_t *u32_data
= data
;
325 u32_data
[0] = counter_obj
->Minimum
.u32
;
326 u32_data
[1] = counter_obj
->Maximum
.u32
;
329 case GL_UNSIGNED_INT64_AMD
: {
330 uint64_t *u64_data
= data
;
331 u64_data
[0] = counter_obj
->Minimum
.u64
;
332 u64_data
[1] = counter_obj
->Maximum
.u64
;
336 assert(!"Should not get here: invalid counter type");
341 _mesa_error(ctx
, GL_INVALID_ENUM
,
342 "glGetPerfMonitorCounterInfoAMD(pname)");
348 _mesa_GenPerfMonitorsAMD(GLsizei n
, GLuint
*monitors
)
351 GET_CURRENT_CONTEXT(ctx
);
353 if (MESA_VERBOSE
& VERBOSE_API
)
354 _mesa_debug(ctx
, "glGenPerfMonitorsAMD(%d)\n", n
);
357 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPerfMonitorsAMD(n < 0)");
361 if (monitors
== NULL
)
364 /* We don't actually need them to be contiguous, but this is what
365 * the rest of Mesa does, so we may as well.
367 first
= _mesa_HashFindFreeKeyBlock(ctx
->PerfMonitor
.Monitors
, n
);
370 for (i
= 0; i
< n
; i
++) {
371 struct gl_perf_monitor_object
*m
=
372 new_performance_monitor(ctx
, first
+ i
);
374 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenPerfMonitorsAMD");
377 monitors
[i
] = first
+ i
;
378 _mesa_HashInsert(ctx
->PerfMonitor
.Monitors
, first
+ i
, m
);
381 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenPerfMonitorsAMD");
387 _mesa_DeletePerfMonitorsAMD(GLsizei n
, GLuint
*monitors
)
390 GET_CURRENT_CONTEXT(ctx
);
392 if (MESA_VERBOSE
& VERBOSE_API
)
393 _mesa_debug(ctx
, "glDeletePerfMonitorsAMD(%d)\n", n
);
396 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeletePerfMonitorsAMD(n < 0)");
400 if (monitors
== NULL
)
403 for (i
= 0; i
< n
; i
++) {
404 struct gl_perf_monitor_object
*m
= lookup_monitor(ctx
, monitors
[i
]);
407 /* Give the driver a chance to stop the monitor if it's active. */
409 ctx
->Driver
.ResetPerfMonitor(ctx
, m
);
413 _mesa_HashRemove(ctx
->PerfMonitor
.Monitors
, monitors
[i
]);
414 ralloc_free(m
->ActiveGroups
);
415 ralloc_free(m
->ActiveCounters
);
416 ctx
->Driver
.DeletePerfMonitor(ctx
, m
);
418 /* "INVALID_VALUE error will be generated if any of the monitor IDs
419 * in the <monitors> parameter to DeletePerfMonitorsAMD do not
420 * reference a valid generated monitor ID."
422 _mesa_error(ctx
, GL_INVALID_VALUE
,
423 "glDeletePerfMonitorsAMD(invalid monitor)");
429 _mesa_SelectPerfMonitorCountersAMD(GLuint monitor
, GLboolean enable
,
430 GLuint group
, GLint numCounters
,
433 GET_CURRENT_CONTEXT(ctx
);
435 struct gl_perf_monitor_object
*m
;
436 const struct gl_perf_monitor_group
*group_obj
;
438 m
= lookup_monitor(ctx
, monitor
);
440 /* "INVALID_VALUE error will be generated if the <monitor> parameter to
441 * SelectPerfMonitorCountersAMD does not reference a monitor created by
442 * GenPerfMonitorsAMD."
445 _mesa_error(ctx
, GL_INVALID_VALUE
,
446 "glSelectPerfMonitorCountersAMD(invalid monitor)");
450 group_obj
= get_group(ctx
, group
);
452 /* "INVALID_VALUE error will be generated if the <group> parameter to
453 * GetPerfMonitorCountersAMD, GetPerfMonitorCounterStringAMD,
454 * GetPerfMonitorCounterStringAMD, GetPerfMonitorCounterInfoAMD, or
455 * SelectPerfMonitorCountersAMD does not reference a valid group ID."
457 if (group_obj
== NULL
) {
458 _mesa_error(ctx
, GL_INVALID_VALUE
,
459 "glSelectPerfMonitorCountersAMD(invalid group)");
463 /* "INVALID_VALUE error will be generated if the <numCounters> parameter to
464 * SelectPerfMonitorCountersAMD is less than 0."
466 if (numCounters
< 0) {
467 _mesa_error(ctx
, GL_INVALID_VALUE
,
468 "glSelectPerfMonitorCountersAMD(numCounters < 0)");
472 /* "When SelectPerfMonitorCountersAMD is called on a monitor, any outstanding
473 * results for that monitor become invalidated and the result queries
474 * PERFMON_RESULT_SIZE_AMD and PERFMON_RESULT_AVAILABLE_AMD are reset to 0."
476 ctx
->Driver
.ResetPerfMonitor(ctx
, m
);
478 /* Sanity check the counter ID list. */
479 for (i
= 0; i
< numCounters
; i
++) {
480 if (counterList
[i
] >= group_obj
->NumCounters
) {
481 _mesa_error(ctx
, GL_INVALID_VALUE
,
482 "glSelectPerfMonitorCountersAMD(invalid counter ID)");
488 /* Enable the counters */
489 for (i
= 0; i
< numCounters
; i
++) {
490 ++m
->ActiveGroups
[group
];
491 BITSET_SET(m
->ActiveCounters
[group
], counterList
[i
]);
494 /* Disable the counters */
495 for (i
= 0; i
< numCounters
; i
++) {
496 --m
->ActiveGroups
[group
];
497 BITSET_CLEAR(m
->ActiveCounters
[group
], counterList
[i
]);
503 _mesa_BeginPerfMonitorAMD(GLuint monitor
)
505 GET_CURRENT_CONTEXT(ctx
);
507 struct gl_perf_monitor_object
*m
= lookup_monitor(ctx
, monitor
);
510 _mesa_error(ctx
, GL_INVALID_VALUE
,
511 "glBeginPerfMonitorAMD(invalid monitor)");
515 /* "INVALID_OPERATION error will be generated if BeginPerfMonitorAMD is
516 * called when a performance monitor is already active."
519 _mesa_error(ctx
, GL_INVALID_OPERATION
,
520 "glBeginPerfMonitor(already active)");
524 /* The driver is free to return false if it can't begin monitoring for
525 * any reason. This translates into an INVALID_OPERATION error.
527 if (ctx
->Driver
.BeginPerfMonitor(ctx
, m
)) {
531 _mesa_error(ctx
, GL_INVALID_OPERATION
,
532 "glBeginPerfMonitor(driver unable to begin monitoring)");
537 _mesa_EndPerfMonitorAMD(GLuint monitor
)
539 GET_CURRENT_CONTEXT(ctx
);
541 struct gl_perf_monitor_object
*m
= lookup_monitor(ctx
, monitor
);
544 _mesa_error(ctx
, GL_INVALID_VALUE
, "glEndPerfMonitorAMD(invalid monitor)");
548 /* "INVALID_OPERATION error will be generated if EndPerfMonitorAMD is called
549 * when a performance monitor is not currently started."
552 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginPerfMonitor(not active)");
556 ctx
->Driver
.EndPerfMonitor(ctx
, m
);
563 * Return the number of bytes needed to store a monitor's result.
566 perf_monitor_result_size(const struct gl_context
*ctx
,
567 const struct gl_perf_monitor_object
*m
)
569 unsigned group
, counter
;
572 for (group
= 0; group
< ctx
->PerfMonitor
.NumGroups
; group
++) {
573 const struct gl_perf_monitor_group
*g
= &ctx
->PerfMonitor
.Groups
[group
];
574 for (counter
= 0; counter
< g
->NumCounters
; counter
++) {
575 const struct gl_perf_monitor_counter
*c
= &g
->Counters
[counter
];
577 if (!BITSET_TEST(m
->ActiveCounters
[group
], counter
))
580 size
+= sizeof(uint32_t); /* Group ID */
581 size
+= sizeof(uint32_t); /* Counter ID */
582 size
+= _mesa_perf_monitor_counter_size(c
);
589 _mesa_GetPerfMonitorCounterDataAMD(GLuint monitor
, GLenum pname
,
590 GLsizei dataSize
, GLuint
*data
,
593 GET_CURRENT_CONTEXT(ctx
);
595 struct gl_perf_monitor_object
*m
= lookup_monitor(ctx
, monitor
);
596 bool result_available
;
599 _mesa_error(ctx
, GL_INVALID_VALUE
,
600 "glGetPerfMonitorCounterDataAMD(invalid monitor)");
604 /* "It is an INVALID_OPERATION error for <data> to be NULL." */
606 _mesa_error(ctx
, GL_INVALID_OPERATION
,
607 "glGetPerfMonitorCounterDataAMD(data == NULL)");
611 /* We need at least enough room for a single value. */
612 if (dataSize
< sizeof(GLuint
)) {
613 if (bytesWritten
!= NULL
)
618 /* If the monitor has never ended, there is no result. */
619 result_available
= m
->Ended
&&
620 ctx
->Driver
.IsPerfMonitorResultAvailable(ctx
, m
);
622 /* AMD appears to return 0 for all queries unless a result is available. */
623 if (!result_available
) {
625 if (bytesWritten
!= NULL
)
626 *bytesWritten
= sizeof(GLuint
);
631 case GL_PERFMON_RESULT_AVAILABLE_AMD
:
633 if (bytesWritten
!= NULL
)
634 *bytesWritten
= sizeof(GLuint
);
636 case GL_PERFMON_RESULT_SIZE_AMD
:
637 *data
= perf_monitor_result_size(ctx
, m
);
638 if (bytesWritten
!= NULL
)
639 *bytesWritten
= sizeof(GLuint
);
641 case GL_PERFMON_RESULT_AMD
:
642 ctx
->Driver
.GetPerfMonitorResult(ctx
, m
, dataSize
, data
, bytesWritten
);
645 _mesa_error(ctx
, GL_INVALID_ENUM
,
646 "glGetPerfMonitorCounterDataAMD(pname)");
651 * Returns how many bytes a counter's value takes up.
654 _mesa_perf_monitor_counter_size(const struct gl_perf_monitor_counter
*c
)
658 case GL_PERCENTAGE_AMD
:
659 return sizeof(GLfloat
);
660 case GL_UNSIGNED_INT
:
661 return sizeof(GLuint
);
662 case GL_UNSIGNED_INT64_AMD
:
663 return sizeof(uint64_t);
665 assert(!"Should not get here: invalid counter type");
670 extern void GLAPIENTRY
671 _mesa_GetFirstPerfQueryIdINTEL(GLuint
*queryId
)
673 GET_CURRENT_CONTEXT(ctx
);
676 /* The GL_INTEL_performance_query spec says:
678 * "If queryId pointer is equal to 0, INVALID_VALUE error is generated."
681 _mesa_error(ctx
, GL_INVALID_VALUE
,
682 "glGetFirstPerfQueryIdINTEL(queryId == NULL)");
686 numGroups
= ctx
->PerfMonitor
.NumGroups
;
688 /* The GL_INTEL_performance_query spec says:
690 * "If the given hardware platform doesn't support any performance
691 * queries, then the value of 0 is returned and INVALID_OPERATION error
694 if (numGroups
== 0) {
696 _mesa_error(ctx
, GL_INVALID_OPERATION
,
697 "glGetFirstPerfQueryIdINTEL(no queries supported)");
701 *queryId
= index_to_queryid(0);
704 extern void GLAPIENTRY
705 _mesa_GetNextPerfQueryIdINTEL(GLuint queryId
, GLuint
*nextQueryId
)
707 GET_CURRENT_CONTEXT(ctx
);
709 /* The GL_INTEL_performance_query spec says:
711 * "The result is passed in location pointed by nextQueryId. If query
712 * identified by queryId is the last query available the value of 0 is
713 * returned. If the specified performance query identifier is invalid
714 * then INVALID_VALUE error is generated. If nextQueryId pointer is
715 * equal to 0, an INVALID_VALUE error is generated. Whenever error is
716 * generated, the value of 0 is returned."
720 _mesa_error(ctx
, GL_INVALID_VALUE
,
721 "glGetNextPerfQueryIdINTEL(nextQueryId == NULL)");
725 if (!queryid_valid(ctx
, queryId
)) {
727 _mesa_error(ctx
, GL_INVALID_VALUE
,
728 "glGetNextPerfQueryIdINTEL(invalid query)");
734 if (!queryid_valid(ctx
, queryId
)) {
737 *nextQueryId
= queryId
;
741 extern void GLAPIENTRY
742 _mesa_GetPerfQueryIdByNameINTEL(char *queryName
, GLuint
*queryId
)
744 GET_CURRENT_CONTEXT(ctx
);
747 /* The GL_INTEL_performance_query spec says:
749 * "If queryName does not reference a valid query name, an INVALID_VALUE
750 * error is generated."
753 _mesa_error(ctx
, GL_INVALID_VALUE
,
754 "glGetPerfQueryIdByNameINTEL(queryName == NULL)");
758 /* The specification does not state that this produces an error. */
760 _mesa_warning(ctx
, "glGetPerfQueryIdByNameINTEL(queryId == NULL)");
764 for (i
= 0; i
< ctx
->PerfMonitor
.NumGroups
; ++i
) {
765 const struct gl_perf_monitor_group
*group_obj
= get_group(ctx
, i
);
766 if (strcmp(group_obj
->Name
, queryName
) == 0) {
767 *queryId
= index_to_queryid(i
);
772 _mesa_error(ctx
, GL_INVALID_VALUE
,
773 "glGetPerfQueryIdByNameINTEL(invalid query name)");
776 extern void GLAPIENTRY
777 _mesa_GetPerfQueryInfoINTEL(GLuint queryId
,
778 GLuint queryNameLength
, char *queryName
,
779 GLuint
*dataSize
, GLuint
*noCounters
,
780 GLuint
*noActiveInstances
,
783 GET_CURRENT_CONTEXT(ctx
);
786 const struct gl_perf_monitor_group
*group_obj
=
787 get_group(ctx
, queryid_to_index(queryId
));
789 if (group_obj
== NULL
) {
790 /* The GL_INTEL_performance_query spec says:
792 * "If queryId does not reference a valid query type, an
793 * INVALID_VALUE error is generated."
795 _mesa_error(ctx
, GL_INVALID_VALUE
,
796 "glGetPerfQueryInfoINTEL(invalid query)");
801 strncpy(queryName
, group_obj
->Name
, queryNameLength
);
803 /* No specification given about whether the string needs to be
804 * zero-terminated. Zero-terminate the string always as we don't
805 * otherwise communicate the length of the returned string.
807 if (queryNameLength
> 0) {
808 queryName
[queryNameLength
- 1] = '\0';
814 for (i
= 0; i
< group_obj
->NumCounters
; ++i
) {
815 /* What we get from the driver is group id (uint32_t) + counter id
816 * (uint32_t) + value.
818 size
+= 2 * sizeof(uint32_t) + _mesa_perf_monitor_counter_size(&group_obj
->Counters
[i
]);
824 *noCounters
= group_obj
->NumCounters
;
827 /* The GL_INTEL_performance_query spec says:
829 * "-- the actual number of already created query instances in
830 * maxInstances location"
832 * 1) Typo in the specification, should be noActiveInstances.
833 * 2) Another typo in the specification, maxInstances parameter is not listed
834 * in the declaration of this function in the list of new functions.
836 if (noActiveInstances
) {
837 *noActiveInstances
= _mesa_HashNumEntries(ctx
->PerfMonitor
.Monitors
);
841 /* TODO: This information not yet available in the monitor structs. For
842 * now, we hardcode SINGLE_CONTEXT, since that's what the implementation
843 * currently tries very hard to do.
845 *capsMask
= GL_PERFQUERY_SINGLE_CONTEXT_INTEL
;
849 extern void GLAPIENTRY
850 _mesa_GetPerfCounterInfoINTEL(GLuint queryId
, GLuint counterId
,
851 GLuint counterNameLength
, char *counterName
,
852 GLuint counterDescLength
, char *counterDesc
,
853 GLuint
*counterOffset
, GLuint
*counterDataSize
, GLuint
*counterTypeEnum
,
854 GLuint
*counterDataTypeEnum
, GLuint64
*rawCounterMaxValue
)
856 GET_CURRENT_CONTEXT(ctx
);
858 const struct gl_perf_monitor_group
*group_obj
;
859 const struct gl_perf_monitor_counter
*counter_obj
;
860 unsigned counterIndex
;
863 group_obj
= get_group(ctx
, queryid_to_index(queryId
));
865 /* The GL_INTEL_performance_query spec says:
867 * "If the pair of queryId and counterId does not reference a valid
868 * counter, an INVALID_VALUE error is generated."
870 if (group_obj
== NULL
) {
871 _mesa_error(ctx
, GL_INVALID_VALUE
,
872 "glGetPerfCounterInfoINTEL(invalid queryId)");
876 counterIndex
= counterid_to_index(counterId
);
877 counter_obj
= get_counter(group_obj
, counterIndex
);
879 if (counter_obj
== NULL
) {
880 _mesa_error(ctx
, GL_INVALID_VALUE
,
881 "glGetPerfCounterInfoINTEL(invalid counterId)");
886 strncpy(counterName
, counter_obj
->Name
, counterNameLength
);
888 /* No specification given about whether the string needs to be
889 * zero-terminated. Zero-terminate the string always as we don't
890 * otherwise communicate the length of the returned string.
892 if (counterNameLength
> 0) {
893 counterName
[counterNameLength
- 1] = '\0';
898 /* TODO: No separate description text at the moment. We pass the name
899 * again for the moment.
901 strncpy(counterDesc
, counter_obj
->Name
, counterDescLength
);
903 /* No specification given about whether the string needs to be
904 * zero-terminated. Zero-terminate the string always as we don't
905 * otherwise communicate the length of the returned string.
907 if (counterDescLength
> 0) {
908 counterDesc
[counterDescLength
- 1] = '\0';
914 for (i
= 0; i
< counterIndex
; ++i
) {
915 /* What we get from the driver is group id (uint32_t) + counter id
916 * (uint32_t) + value.
918 offset
+= 2 * sizeof(uint32_t) + _mesa_perf_monitor_counter_size(&group_obj
->Counters
[i
]);
920 *counterOffset
= 2 * sizeof(uint32_t) + offset
;
923 if (counterDataSize
) {
924 *counterDataSize
= _mesa_perf_monitor_counter_size(counter_obj
);
927 if (counterTypeEnum
) {
928 /* TODO: Different counter types (semantic type, not data type) not
929 * supported as of yet.
931 *counterTypeEnum
= GL_PERFQUERY_COUNTER_RAW_INTEL
;
934 if (counterDataTypeEnum
) {
935 switch (counter_obj
->Type
) {
937 case GL_PERCENTAGE_AMD
:
938 *counterDataTypeEnum
= GL_PERFQUERY_COUNTER_DATA_FLOAT_INTEL
;
940 case GL_UNSIGNED_INT
:
941 *counterDataTypeEnum
= GL_PERFQUERY_COUNTER_DATA_UINT32_INTEL
;
943 case GL_UNSIGNED_INT64_AMD
:
944 *counterDataTypeEnum
= GL_PERFQUERY_COUNTER_DATA_UINT64_INTEL
;
947 assert(!"Should not get here: invalid counter type");
952 if (rawCounterMaxValue
) {
953 /* This value is (implicitly) specified to be used only with
954 * GL_PERFQUERY_COUNTER_RAW_INTEL counters. When semantic types for
955 * counters are added, that needs to be checked.
958 /* The GL_INTEL_performance_query spec says:
960 * "for some raw counters for which the maximal value is
961 * deterministic, the maximal value of the counter in 1 second is
962 * returned in the location pointed by rawCounterMaxValue, otherwise,
963 * the location is written with the value of 0."
965 * The maximum value reported by the driver at the moment is not with
966 * these semantics, so write 0 always to be safe.
968 *rawCounterMaxValue
= 0;
972 extern void GLAPIENTRY
973 _mesa_CreatePerfQueryINTEL(GLuint queryId
, GLuint
*queryHandle
)
975 GET_CURRENT_CONTEXT(ctx
);
978 const struct gl_perf_monitor_group
*group_obj
;
979 struct gl_perf_monitor_object
*m
;
982 /* This is not specified in the extension, but is the only sane thing to
985 if (queryHandle
== NULL
) {
986 _mesa_error(ctx
, GL_INVALID_VALUE
,
987 "glCreatePerfQueryINTEL(queryHandle == NULL)");
991 group
= queryid_to_index(queryId
);
992 group_obj
= get_group(ctx
, group
);
994 /* The GL_INTEL_performance_query spec says:
996 * "If queryId does not reference a valid query type, an INVALID_VALUE
997 * error is generated."
999 if (group_obj
== NULL
) {
1000 _mesa_error(ctx
, GL_INVALID_VALUE
,
1001 "glCreatePerfQueryINTEL(invalid queryId)");
1005 /* The query object created here is the counterpart of a `monitor' in
1006 * AMD_performance_monitor. This call is equivalent to calling
1007 * GenPerfMonitorsAMD and SelectPerfMonitorCountersAMD with a list of all
1008 * counters in a group.
1011 /* We keep the monitor ids contiguous */
1012 first
= _mesa_HashFindFreeKeyBlock(ctx
->PerfMonitor
.Monitors
, 1);
1014 /* The GL_INTEL_performance_query spec says:
1016 * "If the query instance cannot be created due to exceeding the
1017 * number of allowed instances or driver fails query creation due to
1018 * an insufficient memory reason, an OUT_OF_MEMORY error is
1019 * generated, and the location pointed by queryHandle returns NULL."
1021 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCreatePerfQueryINTEL");
1025 m
= new_performance_monitor(ctx
, first
);
1027 _mesa_error_no_memory(__func__
);
1031 _mesa_HashInsert(ctx
->PerfMonitor
.Monitors
, first
, m
);
1032 *queryHandle
= first
;
1034 ctx
->Driver
.ResetPerfMonitor(ctx
, m
);
1036 for (i
= 0; i
< group_obj
->NumCounters
; ++i
) {
1037 ++m
->ActiveGroups
[group
];
1038 /* Counters are a continuous range of integers, 0 to NumCounters (excl),
1039 * so i is the counter value to use here.
1041 BITSET_SET(m
->ActiveCounters
[group
], i
);
1045 extern void GLAPIENTRY
1046 _mesa_DeletePerfQueryINTEL(GLuint queryHandle
)
1048 GET_CURRENT_CONTEXT(ctx
);
1049 struct gl_perf_monitor_object
*m
;
1051 /* The queryHandle is the counterpart to AMD_performance_monitor's monitor
1054 m
= lookup_monitor(ctx
, queryHandle
);
1056 /* The GL_INTEL_performance_query spec says:
1058 * "If a query handle doesn't reference a previously created performance
1059 * query instance, an INVALID_VALUE error is generated."
1062 _mesa_error(ctx
, GL_INVALID_VALUE
,
1063 "glDeletePerfQueryINTEL(invalid queryHandle)");
1067 /* Let the driver stop the monitor if it's active. */
1069 ctx
->Driver
.ResetPerfMonitor(ctx
, m
);
1073 _mesa_HashRemove(ctx
->PerfMonitor
.Monitors
, queryHandle
);
1074 ralloc_free(m
->ActiveGroups
);
1075 ralloc_free(m
->ActiveCounters
);
1076 ctx
->Driver
.DeletePerfMonitor(ctx
, m
);
1079 extern void GLAPIENTRY
1080 _mesa_BeginPerfQueryINTEL(GLuint queryHandle
)
1082 GET_CURRENT_CONTEXT(ctx
);
1083 struct gl_perf_monitor_object
*m
;
1085 /* The queryHandle is the counterpart to AMD_performance_monitor's monitor
1089 m
= lookup_monitor(ctx
, queryHandle
);
1091 /* The GL_INTEL_performance_query spec says:
1093 * "If a query handle doesn't reference a previously created performance
1094 * query instance, an INVALID_VALUE error is generated."
1097 _mesa_error(ctx
, GL_INVALID_VALUE
,
1098 "glBeginPerfQueryINTEL(invalid queryHandle)");
1102 /* The GL_INTEL_performance_query spec says:
1104 * "Note that some query types, they cannot be collected in the same
1105 * time. Therefore calls of BeginPerfQueryINTEL() cannot be nested if
1106 * they refer to queries of such different types. In such case
1107 * INVALID_OPERATION error is generated."
1109 * We also generate an INVALID_OPERATION error if the driver can't begin
1110 * monitoring for its own reasons, and for nesting the same query.
1113 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1114 "glBeginPerfQueryINTEL(already active)");
1118 if (ctx
->Driver
.BeginPerfMonitor(ctx
, m
)) {
1122 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1123 "glBeginPerfQueryINTEL(driver unable to begin monitoring)");
1127 extern void GLAPIENTRY
1128 _mesa_EndPerfQueryINTEL(GLuint queryHandle
)
1130 GET_CURRENT_CONTEXT(ctx
);
1131 struct gl_perf_monitor_object
*m
;
1133 /* The queryHandle is the counterpart to AMD_performance_monitor's monitor
1137 m
= lookup_monitor(ctx
, queryHandle
);
1139 /* The GL_INTEL_performance_query spec says:
1141 * "If a performance query is not currently started, an
1142 * INVALID_OPERATION error will be generated."
1144 * The specification doesn't state that an invalid handle would be an
1145 * INVALID_VALUE error. Regardless, query for such a handle will not be
1146 * started, so we generate an INVALID_OPERATION in that case too.
1149 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1150 "glEndPerfQueryINTEL(invalid queryHandle)");
1155 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1156 "glEndPerfQueryINTEL(not active)");
1160 ctx
->Driver
.EndPerfMonitor(ctx
, m
);
1166 extern void GLAPIENTRY
1167 _mesa_GetPerfQueryDataINTEL(GLuint queryHandle
, GLuint flags
,
1168 GLsizei dataSize
, void *data
, GLuint
*bytesWritten
)
1170 GET_CURRENT_CONTEXT(ctx
);
1171 struct gl_perf_monitor_object
*m
;
1172 bool result_available
;
1174 /* The GL_INTEL_performance_query spec says:
1176 * "If bytesWritten or data pointers are NULL then an INVALID_VALUE
1177 * error is generated."
1179 if (!bytesWritten
|| !data
) {
1180 _mesa_error(ctx
, GL_INVALID_VALUE
,
1181 "glGetPerfQueryDataINTEL(bytesWritten or data is NULL)");
1185 /* The queryHandle is the counterpart to AMD_performance_monitor's monitor
1189 m
= lookup_monitor(ctx
, queryHandle
);
1191 /* The specification doesn't state that an invalid handle generates an
1192 * error. We could interpret that to mean the case should be handled as
1193 * "measurement not ready for this query", but what should be done if
1194 * `flags' equals PERFQUERY_WAIT_INTEL?
1196 * To resolve this, we just generate an INVALID_VALUE from an invalid query
1200 _mesa_error(ctx
, GL_INVALID_VALUE
,
1201 "glGetPerfQueryDataINTEL(invalid queryHandle)");
1205 /* We need at least enough room for a single value. */
1206 if (dataSize
< sizeof(GLuint
)) {
1211 /* The GL_INTEL_performance_query spec says:
1213 * "The call may end without returning any data if they are not ready
1214 * for reading as the measurement session is still pending (the
1215 * EndPerfQueryINTEL() command processing is not finished by
1216 * hardware). In this case location pointed by the bytesWritten
1217 * parameter will be set to 0."
1219 * If EndPerfQueryINTEL() is not called at all, we follow this.
1226 result_available
= ctx
->Driver
.IsPerfMonitorResultAvailable(ctx
, m
);
1228 if (!result_available
) {
1229 if (flags
== GL_PERFQUERY_FLUSH_INTEL
) {
1230 ctx
->Driver
.Flush(ctx
);
1231 } else if (flags
== GL_PERFQUERY_WAIT_INTEL
) {
1232 /* Assume Finish() is both enough and not too much to wait for
1233 * results. If results are still not available after Finish(), the
1234 * later code automatically bails out with 0 for bytesWritten.
1236 ctx
->Driver
.Finish(ctx
);
1238 ctx
->Driver
.IsPerfMonitorResultAvailable(ctx
, m
);
1242 if (result_available
) {
1243 ctx
->Driver
.GetPerfMonitorResult(ctx
, m
, dataSize
, data
, (GLint
*)bytesWritten
);