aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/amd_performance_monitor/api.c
blob: 81a26414b8eb431503dbaabc7092f3c496cdd57b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
/*
 * Copyright © 2013 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/**
 * \file api.c
 *
 * Basic AMD_performance_monitor infrastructure tests.  These test the
 * mechanism to retrieve counter and group information, string processing,
 * and various error conditions.  They do not actually activate monitoring.
 */

#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include "piglit-util-gl-common.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 10;
	config.window_visual = PIGLIT_GL_VISUAL_RGB;

PIGLIT_GL_TEST_CONFIG_END

/******************************************************************************/

/**
 * Get a list of group IDs.
 */
static void
get_groups(unsigned **groups, int *num_groups)
{
	glGetPerfMonitorGroupsAMD(num_groups, 0, NULL);
	*groups = calloc(*num_groups, sizeof(unsigned));
	glGetPerfMonitorGroupsAMD(NULL, *num_groups, *groups);
}

/**
 * Get a list of counter IDs in a given group.
 */
static void
get_counters(unsigned group, unsigned **counters, int *num_counters)
{
	glGetPerfMonitorCountersAMD(group, num_counters, NULL, 0, NULL);
	*counters = calloc(*num_counters, sizeof(unsigned));
	glGetPerfMonitorCountersAMD(group, NULL, NULL, *num_counters, *counters);
}

/**
 * Return true if x is in xs.
 */
static bool
in_list(int x, unsigned *xs, int elts)
{
	int i;
	for (i = 0; i < elts; i++) {
		if (x == xs[i])
			return true;
	}
	return false;
}

/**
 * Find an invalid group ID.
 */
static unsigned
find_invalid_group(unsigned *groups, int num_groups)
{
	unsigned invalid_group = ~0;

	/* Most implementations probably use small consecutive integers, so
	 * start at ~0 and work backwards.  Hopefully we shouldn't loop.
	 */
	while (in_list(invalid_group, groups, num_groups))
		--invalid_group;

	return invalid_group;
}

/**
 * Find an invalid counter ID.
 */
static unsigned
find_invalid_counter(unsigned *counters, int num_counters)
{
	unsigned invalid_counter = ~0;

	/* Most implementations probably use small consecutive integers, so
	 * start at ~0 and work backwards.  Hopefully we shouldn't loop.
	 */
	while (in_list(invalid_counter, counters, num_counters))
		--invalid_counter;

	return invalid_counter;
}

#define report(pass) \
    piglit_report_subtest_result((pass) ? PIGLIT_PASS : PIGLIT_FAIL, __func__); \
    return

/******************************************************************************/

/**
 * Call glGetPerfMonitorGroupsAMD() with a NULL numGroups pointer.
 *
 * Verify that it doesn't attempt to write the number of groups and crash.
 */
static void
test_number_of_groups_null_num_groups_pointer(void)
{
	glGetPerfMonitorGroupsAMD(NULL, 0, NULL);
	report(piglit_check_gl_error(GL_NO_ERROR));
}


/**
 * Call glGetPerfMonitorGroupsAMD() with NULL for groups but non-zero groupSize.
 *
 * Verify that it returns the number of groups but doesn't try to write any
 * group IDs and crash.
 */
static void
test_number_of_groups_null_groups_pointer(void)
{
	bool pass = true;
	int num_groups = -1;

	glGetPerfMonitorGroupsAMD(&num_groups, 777, NULL);
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	pass = num_groups >= 0 && pass;
	report(pass);
}

/**
 * Call glGetPerfMonitorGroupsAMD() with zero for groupSize.
 *
 * Verify that it doesn't write any group IDs.
 */
static void
test_number_of_groups_zero_size_array(void)
{
	bool pass = true;
	unsigned groups[2] = {0xd0d0d0d0, 0xd1d1d1d1};
	int num_groups = -1;

	glGetPerfMonitorGroupsAMD(&num_groups, 0, groups);
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

	/* num_groups must have changed */
	pass = num_groups >= 0 && pass;

	/* The groups array should not have changed. */
	pass = groups[0] == 0xd0d0d0d0 && pass;
	pass = groups[1] == 0xd1d1d1d1 && pass;
	report(pass);
}

/**
 * Call glGetPerfMonitorGroupsAMD() with a groups array bigger than groupSize.
 *
 * Verify that it fills the correct number of array slots with group IDs.
 */
static void
test_number_of_groups_partial_array(void)
{
	bool pass = true;
	unsigned groups[] = {0xdddddddd, 0xdddddddd, 0xdddddddd, 0xdddddddd};

	/* Artificially low array size */
	const int groups_array_size = 2;
	int num_groups = -1;
	int i;

	/* This should return the number of groups.  It should not attempt to
	 * write any groups since the pointer is NULL.
	 */
	glGetPerfMonitorGroupsAMD(&num_groups, groups_array_size, groups);
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

	/* num_groups must have changed */
	pass = num_groups >= 0 && pass;

	/* The first few elements should have changed. */
	for (i = 0; i < MIN2(num_groups, groups_array_size); i++) {
		pass = groups[i] != 0xdddddddd && pass;
	}

	/* Catalyst 12.6 on a Radeon 3650 appears to have a bug where this
	 * returns 3 elements instead of 2.  According to the spec,
	 * "The number of entries that will be returned in <groups> is
	 *  determined by <groupSize>."
	 *
	 * Technically, it does not say that N elements will be returned if
	 * groupSize is N, but that's the only reasonable assumption.
	 */

	/* The rest should remain untouched. */
	for (; i < ARRAY_SIZE(groups); i++) {
		pass = groups[i] == 0xdddddddd && pass;
	}

	report(pass);
}

/******************************************************************************/

/**
 * Call glGetPerfMonitorCountersAMD() with an invalid group ID.
 *
 * Verify that it produces INVALID_VALUE.
 */
static void
test_get_counters_invalid_group(unsigned invalid_group)
{
	glGetPerfMonitorCountersAMD(invalid_group, NULL, NULL, 0, NULL);
	report(piglit_check_gl_error(GL_INVALID_VALUE));
}

/**
 * Call glGetPerfMonitorCountersAMD() with a bunch of NULL pointers.
 *
 * Verify that it doesn't crash attempting to write numCounters,
 * maxActiveCounters, or the counters list.
 */
static void
test_get_counters_null_pointers(unsigned valid_group)
{
	glGetPerfMonitorCountersAMD(valid_group, NULL, NULL, 0, NULL);
	report(piglit_check_gl_error(GL_NO_ERROR));
}

/**
 * Call glGetPerfMonitorCountersAMD() with NULL for the array but non-zero size.
 *
 * Verify that it returns the number of groups but doesn't try to write any
 * group IDs and crash.
 */
static void
test_get_counters_null_pointer_non_zero_size(unsigned valid_group)
{
	glGetPerfMonitorCountersAMD(valid_group, NULL, NULL, 777, NULL);
	report(piglit_check_gl_error(GL_NO_ERROR));
}

/**
 * Call glGetPerfMonitorCountersAMD() with zero for countersSize.
 *
 * Verify that it doesn't write any IDs, but does return other data.
 */
static void
test_get_counters_zero_size_array(unsigned valid_group)
{
	bool pass = true;
	unsigned counters[2] = {0xd0d0d0d0, 0xd1d1d1d1};
	int num_counters = -1;
	int max_active_counters = -1;

	glGetPerfMonitorCountersAMD(valid_group, &num_counters,
				    &max_active_counters,
				    0, counters);
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

	/* Expect a positive number of counters. */
	pass = num_counters >= 0 && pass;

	/* Expect a positive maximum active counters. */
	pass = max_active_counters >= 0 && pass;

	/* The counters array should not have changed. */
	pass = counters[0] == 0xd0d0d0d0 && pass;
	pass = counters[1] == 0xd1d1d1d1 && pass;
	report(pass);
}

/**
 * Call glGetPerfMonitorGroupsAMD() with a groups array bigger than groupSize.
 *
 * Verify that it fills the correct number of array slots with group IDs.
 */
static void
test_get_counters_partial_array(unsigned valid_group)
{
	bool pass = true;
	unsigned counters[] = {0xdddddddd, 0xdddddddd, 0xdddddddd, 0xdddddddd};

	/* Artificially low array size */
	const int counters_array_size = 2;
	int num_counters = -1;
	int i;

	/* This should return the number of groups.  It should not attempt to
	 * write any groups since the pointer is NULL.
	 */
	glGetPerfMonitorCountersAMD(valid_group, &num_counters, NULL,
				    counters_array_size, counters);
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

	/* num_counters must have changed */
	pass = num_counters >= 0 && pass;

	/* The first few elements should have changed. */
	for (i = 0; i < MIN2(num_counters, counters_array_size); i++) {
		pass = counters[i] != 0xdddddddd && pass;
	}

	/* The rest should remain untouched. */
	for (; i < ARRAY_SIZE(counters); i++) {
		pass = counters[i] == 0xdddddddd && pass;
	}

	report(pass);
}

/******************************************************************************/

/**
 * Call glGetPerfMonitorGroupStringAMD() with an invalid group ID.
 *
 * Verify that it produces INVALID_VALUE.
 */
static void
test_group_string_invalid_group(unsigned invalid_group)
{
	glGetPerfMonitorGroupStringAMD(invalid_group, 0, NULL, NULL);
	report(piglit_check_gl_error(GL_INVALID_VALUE));
}

/**
 * Call glGetPerfMonitorGroupStringAMD() with a NULL length pointer.
 *
 * Verify that it doesn't crash.
 */
static void
test_group_string_null_length(unsigned valid_group)
{
	glGetPerfMonitorGroupStringAMD(valid_group, 0, NULL, NULL);
	report(piglit_check_gl_error(GL_NO_ERROR));
}

/**
 * Call glGetPerfMonitorGroupStringAMD() with a single character buffer.
 *
 * Verify that length is correct and no buffer overflows occur.
 */
static void
test_group_string_single_character_buffer(unsigned valid_group)
{
	bool pass = true;
	char name[3] = "```";
	GLsizei length = 0xd0d0d0d0;

	glGetPerfMonitorGroupStringAMD(valid_group, 1, &length, name);
	pass = piglit_check_gl_error(GL_NO_ERROR);

	/* Verify buffer contents: only the first character should change. */
	pass = name[0] != '`' && pass;
	pass = name[1] == '`' && pass;
	pass = name[2] == '`' && pass;

	/* length is the number of characters written excluding the null
	 * terminator.
	 */
	if (name[0] == '\0') {
		pass = length == 0 && pass;
	} else {
		/* AMD Catalyst 12.06 (Radeon 3650) does not write a null
		 * terminator.  Instead, it writes the first part of the name.
		 */
		pass = length == 1 && pass;
	}

	report(pass);
}

/**
 * Call glGetPerfMonitorGroupStringAMD() with a small buffer.
 *
 * Verify that a name is returned, length is valid, and no overflows occur.
 */
static void
test_group_string_small_buffer(unsigned valid_group)
{
	bool pass = true;
	char name[3] = "```";
	GLsizei length = 0xd0d0d0d0;
	int i;

	glGetPerfMonitorGroupStringAMD(valid_group, 3, &length, name);

	pass = length <= 3 && pass;

	/* Verify buffer contents: accept no null terminator. */
	for (i = 0; i < length; i++)
		pass = name[i] != '`' && pass;

	if (length < 3) {
		pass = name[length] == '\0';
		for (i = length + 1; i < 3; i++)
			pass = name[i] == '`' && pass;
	}

	report(pass);
}

/**
 * Call glGetPerfMonitorGroupStringAMD() with an appropriately sized buffer.
 *
 * Verify that a name is returned, length is valid, and no overflows occur.
 */
static void
test_group_string_normal_buffer(unsigned valid_group)
{
	bool pass = true;
	char *name;
	GLsizei length = 0xd0d0d0d0;
	int i;

	/* Get the length; bail if unwritten to avoid huge allocations. */
	glGetPerfMonitorGroupStringAMD(valid_group, 0, &length, NULL);
	pass = pass && piglit_check_gl_error(GL_NO_ERROR);
	if (length == 0xd0d0d0d0)
		report(false);

	name = malloc(length + 1);
	assert(name != NULL);

	/* Fill the buffer with a known character (` marks) */
	memset(name, '`', length + 1);

	/* Get the name; everything will fit. */
	glGetPerfMonitorGroupStringAMD(valid_group, length + 1, NULL, name);
	pass = pass && piglit_check_gl_error(GL_NO_ERROR);

	/* Indexes in the interval [0, length) must have been written, or
	 * else length is wrong.
	 */
	for (i = 0; i < length; i++)
		pass = name[i] != '`' && pass;

	/* The last character must be the null terminator. */
	pass = name[length] == '\0' && pass;

	report(pass);
}

/******************************************************************************/

/**
 * Call glGetPerfMonitorCounterStringAMD() with an invalid group ID.
 *
 * Verify that it produces INVALID_VALUE.
 */
static void
test_counter_string_invalid_group(unsigned invalid_group)
{
	glGetPerfMonitorCounterStringAMD(invalid_group, 0, 0, NULL, NULL);
	report(piglit_check_gl_error(GL_INVALID_VALUE));
}

/**
 * Call glGetPerfMonitorCounterStringAMD() with an invalid counter ID.
 *
 * Verify that it produces INVALID_VALUE.
 */
static void
test_counter_string_invalid_counter(unsigned group, unsigned invalid_counter)
{
	glGetPerfMonitorCounterStringAMD(group, invalid_counter, 0, NULL, NULL);
	report(piglit_check_gl_error(GL_INVALID_VALUE));
}

/**
 * Call glGetPerfMonitorCounterStringAMD() with a NULL length pointer.
 *
 * Verify that it doesn't crash.
 */
static void
test_counter_string_null_length(unsigned group, unsigned counter)
{
	glGetPerfMonitorCounterStringAMD(group, counter, 0, NULL, NULL);
	report(piglit_check_gl_error(GL_NO_ERROR));
}

/**
 * Call glGetPerfMonitorCounterStringAMD() with a single character buffer.
 *
 * Verify that length is correct and no buffer overflows occur.
 */
static void
test_counter_string_single_character_buffer(unsigned group, unsigned counter)
{
	bool pass = true;
	char name[3] = "```";
	GLsizei length = 0xd0d0d0d0;

	glGetPerfMonitorCounterStringAMD(group, counter, 1, &length, name);
	pass = piglit_check_gl_error(GL_NO_ERROR);

	/* Verify buffer contents */
	pass = name[0] != '`' && pass;
	pass = name[1] == '`' && pass;
	pass = name[2] == '`' && pass;

	/* length is the number of characters written excluding the null
	 * terminator.
	 */
	if (name[0] == '\0') {
		pass = length == 0 && pass;
	} else {
		/* AMD Catalyst 12.06 (Radeon 3650) does not write a null
		 * terminator.  Instead, it writes the first part of the name.
		 */
		pass = length == 1 && pass;
	}

	report(pass);
}

/**
 * Call glGetPerfMonitorCounterStringAMD() with a small buffer.
 *
 * Verify that a name is returned, length is valid, and no overflows occur.
 */
static void
test_counter_string_small_buffer(unsigned group, unsigned counter)
{
	bool pass = true;
	char name[3] = "```";
	GLsizei length = 0xd0d0d0d0;
	int i;

	glGetPerfMonitorCounterStringAMD(group, counter, 3, &length, name);

	pass = length <= 3 && pass;

	/* Verify buffer contents: accept no null terminator. */
	for (i = 0; i < length; i++)
		pass = name[i] != '`' && pass;

	if (length < 3) {
		pass = name[length] == '\0';
		for (i = length + 1; i < 3; i++)
			pass = name[i] == '`' && pass;
	}

	report(pass);
}

/**
 * Call glGetPerfMonitorCounterStringAMD() with an appropriately sized buffer.
 *
 * Verify that a name is returned, length is valid, and no overflows occur.
 */
static void
test_counter_string_normal_buffer(unsigned group, unsigned counter)
{
	bool pass = true;
	char *name;
	GLsizei length = 0xd0d0d0d0;
	int i;

	/* Get the length; bail if unwritten to avoid huge allocations. */
	glGetPerfMonitorCounterStringAMD(group, counter, 0, &length, NULL);
	pass = pass && piglit_check_gl_error(GL_NO_ERROR);
	if (length == 0xd0d0d0d0)
		report(false);

	name = malloc(length + 1);
	assert(name != NULL);

	/* Fill the buffer with a known character (` marks) */
	memset(name, '`', length + 1);

	/* Get the name; everything will fit. */
	glGetPerfMonitorCounterStringAMD(group, counter, length + 1, NULL, name);
	pass = pass && piglit_check_gl_error(GL_NO_ERROR);

	/* Indexes in the interval [0, length) must have been written, or
	 * else length is wrong.
	 */
	for (i = 0; i < length; i++)
		pass = name[i] != '`' && pass;

	/* The last character must be the null terminator. */
	pass = name[length] == '\0' && pass;

	report(pass);
}

/******************************************************************************/

/**
 * Call glGetPerfMonitorCounterInfoAMD() with an invalid group ID.
 *
 * Verify that it produces INVALID_VALUE.
 */
static void
test_counter_info_invalid_group(unsigned invalid_group)
{
	GLenum type;
	glGetPerfMonitorCounterInfoAMD(invalid_group, 0, GL_COUNTER_TYPE_AMD,
				       &type);
	report(piglit_check_gl_error(GL_INVALID_VALUE));
}

/**
 * Call glGetPerfMonitorCounterInfoAMD() with an invalid counter ID.
 *
 * Verify that it produces INVALID_VALUE.
 */
static void
test_counter_info_invalid_counter(unsigned group, unsigned invalid_counter)
{
	GLenum type;
	glGetPerfMonitorCounterInfoAMD(group, invalid_counter,
				       GL_COUNTER_TYPE_AMD, &type);
	report(piglit_check_gl_error(GL_INVALID_VALUE));
}

/**
 * Call glGetPerfMonitorCounterInfoAMD() on every group/counter and verify that:
 * - All counters must have a valid type.
 * - Percentage counters must have a range of [0.0f, 100.0f]
 * - Counter ranges should return a minimum strictly less than the maximum.
 * - The counter range query doesn't return too much data.
 */
static void
test_counter_info(unsigned *groups, int num_groups)
{
	int i;
	int j;

	for (i = 0; i < num_groups; i++) {
		unsigned *counters;
		int num_counters;
		get_counters(groups[i], &counters, &num_counters);

		for (j = 0; j < num_counters; j++) {
			GLenum type = GL_NONE;
			uint64_t data[3];
			uint64_t min_u, max_u;
			float min_f, max_f;
			uint32_t unchanged;
			bool is_unsigned = false;

			glGetPerfMonitorCounterInfoAMD(groups[i], counters[j],
						       GL_COUNTER_TYPE_AMD,
						       &type);

			/* Get the range */
			memset(data, 0xff, sizeof(uint64_t) * 3);
			glGetPerfMonitorCounterInfoAMD(groups[i], counters[j],
						       GL_COUNTER_RANGE_AMD,
						       data);

			/* Validate the type and use it to interpret the
			 * minimum/maximum information.
			 */
			switch (type) {
			case GL_UNSIGNED_INT:
				min_u = ((uint32_t *) data)[0];
				max_u = ((uint32_t *) data)[1];
				unchanged = ((uint32_t *) data)[2];
				is_unsigned = true;
				break;
			case GL_UNSIGNED_INT64_AMD:
				min_u = data[0];
				max_u = data[1];
				unchanged = ((uint32_t *) data)[4];
				is_unsigned = true;
				break;
			case GL_PERCENTAGE_AMD:
			case GL_FLOAT:
				min_f = ((float *) data)[0];
				max_f = ((float *) data)[1];
				unchanged = ((uint32_t *) data)[2];
				break;
			default:
				printf("Group %u/Counter %u has an invalid type: %x\n", groups[i], counters[j], type);
				report(false);
			}

			/* Make sure it didn't write too much data. */
			if (unchanged != 0xffffffff) {
				printf("COUNTER_RANGE_AMD query for group %u/Counter %u wrote too much data to the buffer.\n", groups[i], counters[j]);
				report(false);
			}

			/* "If type value returned is PERCENTAGE_AMD, then this
			 *  describes a float value that is in the range [0.0 ..
			 *  100.0]."  So we can check this.
			 */
			if (type == GL_PERCENTAGE_AMD) {
				if (min_f != 0.0f || max_f != 100.0f) {
					printf("Group %u/Counter %u's minimum (%f) and maximum (%f) must be 0.0f and 100.0f, respectively.\n", groups[i], counters[j], min_f, max_f);
					report(false);
				}
			} else if (is_unsigned) {
				/* The spec doesn't explicitly state it, but it
				 * makes sense for the minimum to be strictly
				 * less than the maximum.  Do a service to
				 * driver authors and validate that.
				 */
				if (min_u >= max_u) {
					printf("Group %u/Counter %u's minimum (%" PRIu64 ") is >= the maximum (%" PRIu64 ").\n", groups[i], counters[j], min_u, max_u);
					report(false);
				}
			} else if (type == GL_FLOAT) {
				if (min_f >= max_f) {
					printf("Group %u/Counter %u's minimum (%f) is >= the maximum (%f).\n", groups[i], counters[j], min_f, max_f);
					report(false);
				}
			}
		}

		free(counters);
	}
	report(true);
}

/******************************************************************************/


/**
 * Call glBeginPerfMonitorAMD() on an invalid monitor ID.
 * (Should be run before any Gen tests to ensure this ID is invalid.)
 *
 * XXX: This isn't actually specified, but it seems like it ought to be.
 */
void
test_begin_invalid_monitor(void)
{
	glBeginPerfMonitorAMD(777);
	report(piglit_check_gl_error(GL_INVALID_VALUE));
}

/**
 * Call glEndPerfMonitorAMD() on an invalid monitor ID.
 * (Should be run before any Gen tests to ensure this ID is invalid.)
 *
 * XXX: This isn't actually specified, but it seems like it ought to be.
 *
 * AMD Catalyst 12.6 (Radeon 3650) instead produces INVALID_OPERATION,
 * presumably because the (invalid) monitor hasn't been started.  (See
 * test_end_without_begin.)  So we allow either here.
 */
void
test_end_invalid_monitor(void)
{
	GLenum error;
	glEndPerfMonitorAMD(777);
	error = glGetError();
	report(error == GL_INVALID_VALUE || error == GL_INVALID_OPERATION);
}

/**
 * Call glGetPerfMonitorCounterDataAMD() with an invalid monitor ID.
 *
 * XXX: This isn't actually specified, but it seems like it ought to be.
 */
static void
test_get_counter_data_invalid_monitor(void)
{
	unsigned value;
	glGetPerfMonitorCounterDataAMD(777, GL_PERFMON_RESULT_AVAILABLE_AMD,
				       0, &value, NULL);
	report(piglit_check_gl_error(GL_INVALID_VALUE));
}

/**
 * Call glSelectPerfMonitorCountersAMD() with an invalid monitor ID.
 *
 * "If <monitor> is not a valid monitor created by GenPerfMonitorsAMD, then
 *  INVALID_VALUE will be generated."
 */
static void
test_select_counters_invalid_monitor(void)
{
	unsigned junk;
	glSelectPerfMonitorCountersAMD(777, false, 0, 0, &junk);
	report(piglit_check_gl_error(GL_INVALID_VALUE));
}

/**
 * Call glDeletePerfMonitorsAMD() on an invalid monitor ID.
 * (Should be run before any Gen tests to ensure this ID is invalid.)
 *
 * "If a monitor ID in the list <monitors> does not reference a previously
 *  generated performance monitor, an INVALID_VALUE error is generated."
 *
 * AMD Catalyst 12.6 (Radeon 3650) fails this test, producing NO_ERROR.
 */
static void
test_delete_monitor_invalid(void)
{
	unsigned monitor = 777;
	glDeletePerfMonitorsAMD(1, &monitor);
	report(piglit_check_gl_error(GL_INVALID_VALUE));
}

/**
 * Mean tests for glGetPerfMonitorCounterDataAMD()'s data return mechanism.
 *
 * AMD Catalyst 12.6 (Radeon 3650) fails this test.  It does not set
 * bytes_written, yet writes 0 for each of these queries.  It apparently
 * interprets these fields as only relevant to the PERFMON_RESULT_AMD query.
 */
static void
test_get_counter_data_byte_size(void)
{
	bool pass = true;
	unsigned monitor;
	unsigned value;
	GLsizei bytes_written;

	glGenPerfMonitorsAMD(1, &monitor);
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

	/* "It is an INVALID_OPERATION error far <data> to be NULL." */
	glGetPerfMonitorCounterDataAMD(monitor, GL_PERFMON_RESULT_AVAILABLE_AMD,
				       0, NULL, NULL);
	pass = piglit_check_gl_error(GL_INVALID_OPERATION) && pass;

	/* "The argument <dataSize> specifies the number of bytes available in
	 *  the <data> buffer for writing."
	 *
	 * It would be easy to accidentally treat this as 4-byte units, so
	 * be mean and try < sizeof(int) sizes.
	 */

	/* dataSize = 0: Nothing should be written. */
	value = bytes_written = 0xd0d0d0d0;
	glGetPerfMonitorCounterDataAMD(monitor, GL_PERFMON_RESULT_AVAILABLE_AMD,
				       0, &value, &bytes_written);
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	pass = value == 0xd0d0d0d0 && pass;
	pass = bytes_written == 0 && pass;

	/* dataSize = 1: Unclear.  Accept nothing or 1 byte written. */
	value = bytes_written = 0xd0d0d0d0;
	glGetPerfMonitorCounterDataAMD(monitor, GL_PERFMON_RESULT_AVAILABLE_AMD,
				       1, &value, &bytes_written);
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	pass = value == 0xd0d0d0d0 && pass;
	if (bytes_written == 1) {
		pass = value == 0xd0d0d000 && pass;
	} else if (bytes_written == 0) {
		pass = value == 0xd0d0d0d0 && pass;
	} else {
		pass = false;
	}

	glDeletePerfMonitorsAMD(1, &monitor);
	report(pass);
}

static void
test_gen_initial_state(void)
{
	bool pass = true;
	unsigned monitor;
	unsigned value;

	glGenPerfMonitorsAMD(1, &monitor);
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

	/* "The value of the PERFMON_RESULT_AVAILABLE_AMD, PERMON_RESULT_AMD,
	 *  and PERFMON_RESULT_SIZE queries will all initially be 0."
	 */
	value = 0xd0d0d0d0;
	glGetPerfMonitorCounterDataAMD(monitor, GL_PERFMON_RESULT_AVAILABLE_AMD,
				       4, &value, NULL);
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	pass = value == 0 && pass;

	/* AMD Catalyst 12.6 (Radeon 3650) actually does write 0 for the
	 * PERFMON_RESULT query even though it isn't available.  This
	 * matches the spec, but is strange.
	 */
	value = 0xd0d0d0d0;
	glGetPerfMonitorCounterDataAMD(monitor, GL_PERFMON_RESULT_AMD,
				       4, &value, NULL);
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	pass = value == 0 && pass;

	value = 0xd0d0d0d0;
	glGetPerfMonitorCounterDataAMD(monitor, GL_PERFMON_RESULT_SIZE_AMD,
				       4, &value, NULL);
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	pass = value == 0 && pass;

	glDeletePerfMonitorsAMD(1, &monitor);
	report(pass);
}

/**
 * "INVALID_OPERATION error will be generated if EndPerfMonitorAMD is
 *  called when a performance monitor is not currently started."
 */
void
test_end_without_begin(void)
{
	unsigned monitor;
	glGenPerfMonitorsAMD(1, &monitor);
	glEndPerfMonitorAMD(monitor);
	glDeletePerfMonitorsAMD(1, &monitor);
	report(piglit_check_gl_error(GL_INVALID_OPERATION));
}

/**
 * "INVALID_OPERATION error will be generated if BeginPerfMonitorAMD is
 *  called when a performance monitor is already active."
 */
void
test_double_begin(void)
{
	GLenum error;
	bool pass;
	unsigned monitor;
	glGenPerfMonitorsAMD(1, &monitor);
	glBeginPerfMonitorAMD(monitor);

	error = glGetError();
	if (error != GL_NO_ERROR) {
		glDeletePerfMonitorsAMD(1, &monitor);
		/* Monitoring couldn't start for some reason; bail. */
		if (error == GL_INVALID_OPERATION)
			return;
		/* We weren't expecting this other error. */
		report(false);
	}

	/* Double begin */
	glBeginPerfMonitorAMD(monitor);
	pass = piglit_check_gl_error(GL_INVALID_OPERATION);

	glDeletePerfMonitorsAMD(1, &monitor);
	report(pass);
}

/******************************************************************************/

/**
 * Call glSelectPerfMonitorCountersAMD() with an invalid group ID.
 *
 * "If <group> is not a valid group, the INVALID_VALUE error will be generated."
 */
static void
test_select_counters_invalid_group(unsigned invalid_group)
{
	unsigned monitor;
	unsigned junk;
	bool pass;
	glGenPerfMonitorsAMD(1, &monitor);
	glSelectPerfMonitorCountersAMD(monitor, false, invalid_group, 0, &junk);
	pass = piglit_check_gl_error(GL_INVALID_VALUE);
	glDeletePerfMonitorsAMD(1, &monitor);
	report(pass);
}


/**
 * Call glSelectPerfMonitorCountersAMD() with numCounters < 0.
 *
 * "If <numCounters> is less than 0, an INVALID_VALUE error will be generated."
 */
static void
test_select_counters_invalid_num_counters(unsigned group)
{
	unsigned monitor;
	unsigned junk;
	bool pass;
	glGenPerfMonitorsAMD(1, &monitor);
	glSelectPerfMonitorCountersAMD(monitor, false, group, -1, &junk);
	pass = piglit_check_gl_error(GL_INVALID_VALUE);
	glDeletePerfMonitorsAMD(1, &monitor);
	report(pass);
}

/******************************************************************************/

enum piglit_result
piglit_display(void)
{
	return PIGLIT_FAIL;
}

/**
 * The main test program.
 */
void
piglit_init(int argc, char **argv)
{
	unsigned *groups;
	int num_groups;
	unsigned *g0_counters;
	int num_g0_counters;
	unsigned invalid_group;
	unsigned invalid_counter;

	piglit_require_extension("GL_AMD_performance_monitor");

	/* Basic glGetPerfMonitorGroupsAMD() tests */
	test_number_of_groups_null_num_groups_pointer();
	test_number_of_groups_null_groups_pointer();
	test_number_of_groups_zero_size_array();
	test_number_of_groups_partial_array();

	get_groups(&groups, &num_groups);
	invalid_group = find_invalid_group(groups, num_groups);

	test_get_counters_invalid_group(invalid_group);
	test_group_string_invalid_group(invalid_group);
	test_counter_string_invalid_group(invalid_group);
	test_counter_info_invalid_group(invalid_group);

	test_begin_invalid_monitor();
	test_end_invalid_monitor();
	test_delete_monitor_invalid();
	test_get_counter_data_invalid_monitor();
	test_select_counters_invalid_monitor();
	test_get_counter_data_byte_size();
	test_gen_initial_state();
	test_end_without_begin();
	test_double_begin();

	test_select_counters_invalid_group(invalid_group);

	/* If there are no groups, the rest of the tests can't run.  Bail. */
	if (num_groups == 0)
		exit(0);

	test_get_counters_null_pointers(groups[0]);
	test_get_counters_null_pointer_non_zero_size(groups[0]);
	test_get_counters_zero_size_array(groups[0]);
	test_get_counters_partial_array(groups[0]);
	test_group_string_null_length(groups[0]);
	test_group_string_single_character_buffer(groups[0]);
	test_group_string_small_buffer(groups[0]);
	test_group_string_normal_buffer(groups[0]);

	test_counter_info(groups, num_groups);

	test_select_counters_invalid_num_counters(groups[0]);

	get_counters(groups[0], &g0_counters, &num_g0_counters);
	invalid_counter = find_invalid_counter(g0_counters, num_g0_counters);

	test_counter_string_invalid_counter(groups[0], invalid_counter);
	test_counter_info_invalid_counter(groups[0], invalid_counter);

	/* If there are no counters, the rest of the tests can't run.  Bail. */
	if (num_g0_counters == 0)
		exit(0);

	test_counter_string_null_length(groups[0], g0_counters[0]);
	test_counter_string_single_character_buffer(groups[0], g0_counters[0]);
	test_counter_string_small_buffer(groups[0], g0_counters[0]);
	test_counter_string_normal_buffer(groups[0], g0_counters[0]);

	exit(0);
}