Newer
Older
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
// Modified from waffle, last update: 1.7.0
// Changes are marked with SRT.../SRT
// SPDX-License-Identifier: BSD-2-Clause
//
// Copyright 2014 Intel Corporation
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/// @file
/// @brief Print OpenGL info using Waffle.
///
/// This program does the following:
/// 1. Dynamically choose the platform and OpenGL API according to
/// command line arguments.
/// 2. Create an OpenGL context.
/// 3. Print information about the context.
#define WAFFLE_API_VERSION 0x0106
#include <assert.h>
#include <ctype.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __APPLE__
# import <Foundation/NSAutoreleasePool.h>
# import <AppKit/NSApplication.h>
static void
removeXcodeArgs(int *argc, char **argv);
#endif
#include "waffle.h"
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
static const char *usage_message =
"Usage:\n"
" wflinfo <Required Parameters> [Options]\n"
"\n"
"Description:\n"
" Create an OpenGL or OpenGL ES context and print information about it.\n"
"\n"
"Required Parameters:\n"
" -p, --platform <platform>\n"
" One of: android, cgl, gbm, glx, surfaceless_egl (or short\n"
" alias 'sl'), wayland, wgl, or x11_egl.\n"
"\n"
" -a, --api <api>\n"
" One of: gl, gles1, gles2 or gles3\n"
"\n"
"Options:\n"
" -V, --version <version>\n"
" For example --api=gl --version=3.2 would request OpenGL 3.2.\n"
"\n"
" --profile <profile>\n"
" One of: core, compat or none\n"
"\n"
" -v, --verbose\n"
" Print more information.\n"
"\n"
" --forward-compatible\n"
" Create a forward-compatible context.\n"
"\n"
" --debug-context\n"
" Create a debug context.\n"
"\n"
" -f, --format <format>\n"
" One of: original (default) or json.\n"
"\n"
" -h, --help\n"
" Print wflinfo usage information.\n"
"\n"
"Examples:\n"
" wflinfo --platform=glx --api=gl\n"
" wflinfo --platform=x11_egl --api=gl --version=3.2 --profile=core\n"
" wflinfo --platform=wayland --api=gles3\n"
" wflinfo --platform=gbm --api=gl --version=3.2 --verbose\n"
" wflinfo -p gbm -a gl -V 3.2 -v\n"
;
enum {
OPT_PLATFORM = 'p',
OPT_API = 'a',
OPT_VERSION = 'V',
OPT_PROFILE,
OPT_VERBOSE = 'v',
OPT_DEBUG_CONTEXT,
OPT_FORWARD_COMPATIBLE,
OPT_FORMAT = 'f',
OPT_HELP = 'h',
};
static const struct option get_opts[] = {
{ .name = "platform", .has_arg = required_argument, .val = OPT_PLATFORM },
{ .name = "api", .has_arg = required_argument, .val = OPT_API },
{ .name = "version", .has_arg = required_argument, .val = OPT_VERSION },
{ .name = "profile", .has_arg = required_argument, .val = OPT_PROFILE },
{ .name = "verbose", .has_arg = no_argument, .val = OPT_VERBOSE },
{ .name = "debug-context", .has_arg = no_argument, .val = OPT_DEBUG_CONTEXT },
{ .name = "forward-compatible", .has_arg = no_argument, .val = OPT_FORWARD_COMPATIBLE },
{ .name = "format", .has_arg = required_argument, .val = OPT_FORMAT },
{ .name = "help", .has_arg = no_argument, .val = OPT_HELP },
{ 0 },
};
static bool
strneq(const char *a, const char *b, size_t n)
{
return strncmp(a, b, n) == 0;
}
#if defined(__GNUC__)
#define NORETURN __attribute__((noreturn))
#elif defined(_MSC_VER)
#define NORETURN __declspec(noreturn)
#else
#define NORETURN
#endif
// SRT: https://gitlab.freedesktop.org/mesa/waffle/-/merge_requests/100
// (Unmarked uses of PRINTFLIKE, below, are also changes)
#if defined(__GNUC__)
#define PRINTFLIKE(f, a) __attribute__((__format__(__printf__, f, a)))
#else
#define PRINTFLIKE(f, a)
#endif
// /SRT
static void NORETURN
error_oom(void)
{
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
static void NORETURN PRINTFLIKE(2, 3)
error_printf(const char *module, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s error: ", module);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(EXIT_FAILURE);
}
static void NORETURN
write_usage_and_exit(FILE *f, int exit_code)
{
fprintf(f, "%s", usage_message);
exit(exit_code);
}
static void NORETURN PRINTFLIKE(1, 2) usage_error_printf(const char *fmt, ...)
{
fprintf(stderr, "Wflinfo usage error: ");
if (fmt) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, " ");
}
fprintf(stderr, "(see wflinfo --help)\n");
exit(EXIT_FAILURE);
}
// SRT: https://gitlab.freedesktop.org/mesa/waffle/-/merge_requests/100
static void NORETURN
error_waffle(void)
{
const struct waffle_error_info *info = waffle_error_get_info();
const char *code = waffle_error_to_string(info->code);
if (info->message_length > 0)
error_printf("Waffle", "0x%x %s: %s", info->code, code, info->message);
else
error_printf("Waffle", "0x%x %s", info->code, code);
}
static void NORETURN
error_get_gl_symbol(const char *name)
{
error_printf("Wflinfo", "failed to get function pointer for %s", name);
}
// /SRT
typedef float GLclampf;
typedef unsigned int GLbitfield;
typedef unsigned int GLint;
typedef int GLsizei;
typedef unsigned int GLenum;
typedef void GLvoid;
typedef unsigned char GLubyte;
enum {
// Copied from <GL/gl*.h>.
GL_NO_ERROR = 0,
GL_CONTEXT_FLAGS = 0x821e,
GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT = 0x00000001,
GL_CONTEXT_FLAG_DEBUG_BIT = 0x00000002,
GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB = 0x00000004,
GL_VENDOR = 0x1F00,
GL_RENDERER = 0x1F01,
GL_VERSION = 0x1F02,
GL_EXTENSIONS = 0x1F03,
GL_NUM_EXTENSIONS = 0x821D,
GL_SHADING_LANGUAGE_VERSION = 0x8B8C,
};
#define WINDOW_WIDTH 320
#define WINDOW_HEIGHT 240
#define GL_MAJOR_VERSION 0x821B
#define GL_MINOR_VERSION 0x821C
#define GL_CONTEXT_PROFILE_MASK 0x9126
#define GL_CONTEXT_CORE_PROFILE_BIT 0x00000001
#define GL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002
#ifndef _WIN32
#define APIENTRY
#else
#ifndef APIENTRY
#define APIENTRY __stdcall
#endif
#endif
static GLenum (APIENTRY *glGetError)(void);
static void (APIENTRY *glGetIntegerv)(GLenum pname, GLint *params);
static const GLubyte * (APIENTRY *glGetString)(GLenum name);
static const GLubyte * (APIENTRY *glGetStringi)(GLenum name, GLint i);
/// @brief Command line options.
struct options {
/// @brief One of `WAFFLE_PLATFORM_*`.
int platform;
/// @brief One of `WAFFLE_CONTEXT_OPENGL_*`.
int context_api;
/// @brief One of `WAFFLE_CONTEXT_PROFILE_*` or `WAFFLE_NONE`.
int context_profile;
int context_major;
int context_minor;
bool verbose;
enum format {
FORMAT_ORIGINAL,
FORMAT_JSON,
} format;
bool context_forward_compatible;
bool context_debug;
/// @brief One of `WAFFLE_DL_*`.
int dl;
};
struct enum_map {
int i;
const char *s;
};
static const struct enum_map platform_map[] = {
{WAFFLE_PLATFORM_ANDROID, "android" },
{WAFFLE_PLATFORM_CGL, "cgl", },
{WAFFLE_PLATFORM_GBM, "gbm" },
{WAFFLE_PLATFORM_GLX, "glx" },
{WAFFLE_PLATFORM_WAYLAND, "wayland" },
{WAFFLE_PLATFORM_WGL, "wgl" },
{WAFFLE_PLATFORM_X11_EGL, "x11_egl" },
{WAFFLE_PLATFORM_SURFACELESS_EGL, "surfaceless_egl" },
{WAFFLE_PLATFORM_SURFACELESS_EGL, "sl" },
{0, 0 },
};
static const struct enum_map context_api_map[] = {
{WAFFLE_CONTEXT_OPENGL, "gl" },
{WAFFLE_CONTEXT_OPENGL_ES1, "gles1" },
{WAFFLE_CONTEXT_OPENGL_ES2, "gles2" },
{WAFFLE_CONTEXT_OPENGL_ES3, "gles3" },
{0, 0 },
};
/// @brief Translate string to `enum waffle_enum`.
///
/// @param self is a list of map items. The last item must be zero-filled.
/// @param result is altered only if @a s if found.
/// @return true if @a s was found in @a map.
static bool
enum_map_translate_str(
const struct enum_map *self,
const char *s,
int *result)
{
for (const struct enum_map *i = self; i->i != 0; ++i) {
if (!strncmp(s, i->s, strlen(i->s) + 1)) {
*result = i->i;
return true;
}
}
return false;
}
static const char *
enum_map_to_str(const struct enum_map *self,
int val)
{
for (const struct enum_map *i = self; i->i != 0; ++i) {
if (i->i == val) {
return i->s;
}
}
return NULL;
}
/// @return true on success.
static bool
parse_args(int argc, char *argv[], struct options *opts)
{
bool ok;
bool loop_get_opt = true;
#ifdef __APPLE__
removeXcodeArgs(&argc, argv);
#endif
// Set options to default values.
opts->context_profile = WAFFLE_NONE;
opts->context_major = WAFFLE_DONT_CARE;
opts->context_minor = WAFFLE_DONT_CARE;
opts->format = FORMAT_ORIGINAL;
// prevent getopt_long from printing an error message
opterr = 0;
while (loop_get_opt) {
int opt = getopt_long(argc, argv, "a:f:hp:vV:", get_opts, NULL);
switch (opt) {
case -1:
loop_get_opt = false;
break;
case '?':
goto error_unrecognized_arg;
case OPT_PLATFORM:
ok = enum_map_translate_str(platform_map, optarg,
&opts->platform);
if (!ok) {
usage_error_printf("'%s' is not a valid platform",
optarg);
}
break;
case OPT_API:
ok = enum_map_translate_str(context_api_map, optarg,
&opts->context_api);
if (!ok) {
usage_error_printf("'%s' is not a valid API for an OpenGL "
"context", optarg);
}
break;
case OPT_VERSION: {
int major;
int minor;
int match_count;
match_count = sscanf(optarg, "%d.%d", &major, &minor);
if (match_count != 2 || major < 0 || minor < 0) {
usage_error_printf("'%s' is not a valid OpenGL version",
optarg);
}
opts->context_major = major;
opts->context_minor = minor;
break;
}
case OPT_PROFILE:
if (strcmp(optarg, "none") == 0) {
opts->context_profile = WAFFLE_NONE;
} else if (strcmp(optarg, "core") == 0) {
opts->context_profile = WAFFLE_CONTEXT_CORE_PROFILE;
} else if (strcmp(optarg, "compat") == 0) {
opts->context_profile = WAFFLE_CONTEXT_COMPATIBILITY_PROFILE;
} else {
usage_error_printf("'%s' is not a valid OpenGL profile",
optarg);
}
break;
case OPT_FORMAT:
if (strcmp(optarg, "original") == 0) {
opts->format = FORMAT_ORIGINAL;
} else if (strcmp(optarg, "json") == 0) {
opts->format = FORMAT_JSON;
} else {
usage_error_printf("'%s' is not a valid format", optarg);
}
break;
case OPT_VERBOSE:
opts->verbose = true;
break;
case OPT_FORWARD_COMPATIBLE:
opts->context_forward_compatible = true;
break;
case OPT_DEBUG_CONTEXT:
opts->context_debug = true;
break;
case OPT_HELP:
write_usage_and_exit(stdout, EXIT_SUCCESS);
break;
default:
abort();
loop_get_opt = false;
break;
}
}
if (optind < argc) {
goto error_unrecognized_arg;
}
if (!opts->platform) {
usage_error_printf("--platform is required");
}
if (!opts->context_api) {
usage_error_printf("--api is required");
}
// Set dl.
switch (opts->context_api) {
case WAFFLE_CONTEXT_OPENGL: opts->dl = WAFFLE_DL_OPENGL; break;
case WAFFLE_CONTEXT_OPENGL_ES1: opts->dl = WAFFLE_DL_OPENGL_ES1; break;
case WAFFLE_CONTEXT_OPENGL_ES2: opts->dl = WAFFLE_DL_OPENGL_ES2; break;
case WAFFLE_CONTEXT_OPENGL_ES3: opts->dl = WAFFLE_DL_OPENGL_ES3; break;
default:
abort();
break;
}
return true;
error_unrecognized_arg:
if (optarg)
usage_error_printf("unrecognized option '%s'", optarg);
else if (optopt)
usage_error_printf("unrecognized option '-%c'", optopt);
else
usage_error_printf("unrecognized option");
}
static int
parse_version(const char *version)
{
int count, major, minor;
if (version == NULL)
return 0;
while (*version != '\0' && !isdigit(*version))
version++;
count = sscanf(version, "%d.%d", &major, &minor);
if (count != 2)
return 0;
if (minor > 9)
return 0;
return (major * 10) + minor;
}
static const char *
get_vendor(void)
{
const char *vendor = (const char *) glGetString(GL_VENDOR);
if (glGetError() != GL_NO_ERROR || vendor == NULL) {
vendor = "WFLINFO_GL_ERROR";
}
return vendor;
}
static const char *
get_renderer(void)
{
const char *renderer = (const char *) glGetString(GL_RENDERER);
if (glGetError() != GL_NO_ERROR || renderer == NULL) {
renderer = "WFLINFO_GL_ERROR";
}
return renderer;
}
static const char *
get_version(void)
{
const char *version_str = (const char *) glGetString(GL_VERSION);
if (glGetError() != GL_NO_ERROR || version_str == NULL) {
version_str = "WFLINFO_GL_ERROR";
}
return version_str;
}
static void
print_extensions(bool use_stringi)
{
GLint count = 0, i;
const char *ext;
printf("OpenGL extensions: ");
if (use_stringi) {
glGetIntegerv(GL_NUM_EXTENSIONS, &count);
if (glGetError() != GL_NO_ERROR) {
printf("WFLINFO_GL_ERROR");
} else {
for (i = 0; i < count; i++) {
ext = (const char *) glGetStringi(GL_EXTENSIONS, i);
if (glGetError() != GL_NO_ERROR)
ext = "WFLINFO_GL_ERROR";
printf("%s%s", ext, (i + 1) < count ? " " : "");
}
}
} else {
const char *extensions = (const char *) glGetString(GL_EXTENSIONS);
if (glGetError() != GL_NO_ERROR)
printf("WFLINFO_GL_ERROR");
else
printf("%s", extensions);
}
printf("\n");
}
// SRT: https://gitlab.freedesktop.org/mesa/waffle/-/merge_requests/102
static const struct {
GLint flag;
const char *str;
// /SRT
} context_flags[] = {
{ GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT, "FORWARD_COMPATIBLE" },
{ GL_CONTEXT_FLAG_DEBUG_BIT, "DEBUG" },
{ GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB, "ROBUST_ACCESS" },
};
static void
print_context_flags(void)
{
GLint gl_context_flags = 0;
printf("OpenGL context flags:");
glGetIntegerv(GL_CONTEXT_FLAGS, &gl_context_flags);
if (glGetError() != GL_NO_ERROR) {
printf(" WFLINFO_GL_ERROR\n");
return;
}
// SRT: https://gitlab.freedesktop.org/mesa/waffle/-/merge_requests/101
if (gl_context_flags == 0) {
printf(" 0x0\n");
return;
}
// /SRT
for (int i = 0; i < ARRAY_SIZE(context_flags); i++) {
if ((context_flags[i].flag & gl_context_flags) != 0) {
printf(" %s", context_flags[i].str);
gl_context_flags = gl_context_flags & ~context_flags[i].flag;
}
}
for (int i = 0; gl_context_flags != 0; gl_context_flags >>= 1, i++) {
if ((gl_context_flags & 1) != 0) {
printf(" 0x%x", 1 << i);
}
}
printf("\n");
}
static void
json_print_extensions(bool use_stringi)
{
// Print extensions in JSON format
printf(" \"extensions\": [\n");
if (use_stringi) {
GLint count = 0;
const char *ext;
glGetIntegerv(GL_NUM_EXTENSIONS, &count);
if (glGetError() != GL_NO_ERROR) {
printf(" \"WFLINFO_GL_ERROR\"");
} else {
for (int i = 0; i < count; i++) {
ext = (const char *) glGetStringi(GL_EXTENSIONS, i);
if (glGetError() != GL_NO_ERROR)
ext = "WFLINFO_GL_ERROR";
printf(" \"%s\"%s\n", ext, (i + 1) < count ? "," : "");
}
}
} else {
const char *extensions = (const char *) glGetString(GL_EXTENSIONS);
if (glGetError() != GL_NO_ERROR || !extensions) {
printf(" \"WFLINFO_GL_ERROR\"");
} else {
// Copy the string because strtok() is destructive.
char *splitter = strdup(extensions);
if (!splitter)
error_oom();
splitter = strtok(splitter, " ");
// The JSON doesn't strictly need to be newline seperated, but it
// makes it much easier to read. Split the lines and add commas
// correctly.
while (splitter) {
printf(" \"%s\"", splitter);
splitter = strtok(NULL, " ");
if (splitter) {
printf(",\n");
}
}
}
printf("\n");
}
printf(" ]\n");
}
/// @brief Print JSON formatted OpenGL (ES) information
static bool
print_json(const struct options *opts)
{
while (glGetError() != GL_NO_ERROR) {
/* Clear all errors */
}
const char *vendor = get_vendor();
const char *renderer = get_renderer();
const char *version_str = get_version();
const char *platform = enum_map_to_str(platform_map, opts->platform);
assert(platform != NULL);
const char *api = enum_map_to_str(context_api_map, opts->context_api);
assert(api != NULL);
// OpenGL and OpenGL ES >= 3.0 support glGetStringi(GL_EXTENSION, i).
const int version = parse_version(version_str);
const bool use_getstringi = version >= 30;
if (!glGetStringi && use_getstringi) {
error_get_gl_symbol("glGetStringi");
}
// See the equivalent section in print_wflinfo() for more info
const char *language_str = "None";
if ((opts->context_api == WAFFLE_CONTEXT_OPENGL && version >= 20)
|| opts->context_api == WAFFLE_CONTEXT_OPENGL_ES2
|| opts->context_api == WAFFLE_CONTEXT_OPENGL_ES3) {
language_str = (const char *) glGetString(GL_SHADING_LANGUAGE_VERSION);
if (glGetError() != GL_NO_ERROR || language_str == NULL) {
language_str = "WFLINFO_GL_ERROR";
}
}
printf("{\n");
printf(" \"waffle\": {\n");
printf(" \"platform\": \"%s\",\n", platform);
printf(" \"api\": \"%s\"\n", api);
printf(" },\n");
printf(" \"OpenGL\": {\n");
printf(" \"vendor string\": \"%s\",\n", vendor);
printf(" \"renderer string\": \"%s\",\n", renderer);
printf(" \"version string\": \"%s\",\n", version_str);
printf(" \"shading language version string\": \"%s\",\n", language_str);
json_print_extensions(use_getstringi);
printf(" }\n");
printf("}\n");
return true;
}
/// @brief Print out information about the context that was created.
static bool
print_wflinfo(const struct options *opts)
{
while (glGetError() != GL_NO_ERROR) {
/* Clear all errors */
}
const char *vendor = get_vendor();
const char *renderer = get_renderer();
const char *version_str = get_version();
const char *platform = enum_map_to_str(platform_map, opts->platform);
assert(platform != NULL);
printf("Waffle platform: %s\n", platform);
const char *api = enum_map_to_str(context_api_map, opts->context_api);
assert(api != NULL);
printf("Waffle api: %s\n", api);
printf("OpenGL vendor string: %s\n", vendor);
printf("OpenGL renderer string: %s\n", renderer);
printf("OpenGL version string: %s\n", version_str);
int version = parse_version(version_str);
if (opts->context_api == WAFFLE_CONTEXT_OPENGL && version >= 31) {
print_context_flags();
}
// OpenGL and OpenGL ES >= 3.0 support glGetStringi(GL_EXTENSION, i).
const bool use_getstringi = version >= 30;
if (!glGetStringi && use_getstringi)
error_get_gl_symbol("glGetStringi");
if (opts->verbose) {
// There are two exceptional cases where wflinfo may not get a
// version (or a valid version): one is in gles1 and the other
// is GL < 2.0. In these cases do not return WFLINFO_GL_ERROR,
// return None. This is preferable to returning WFLINFO_GL_ERROR
// because it creates a consistant interface for parsers
const char *language_str = "None";
if ((opts->context_api == WAFFLE_CONTEXT_OPENGL && version >= 20) ||
opts->context_api == WAFFLE_CONTEXT_OPENGL_ES2 ||
opts->context_api == WAFFLE_CONTEXT_OPENGL_ES3) {
language_str = (const char *) glGetString(GL_SHADING_LANGUAGE_VERSION);
if (glGetError() != GL_NO_ERROR || language_str == NULL) {
language_str = "WFLINFO_GL_ERROR";
}
}
printf("OpenGL shading language version string: %s\n", language_str);
print_extensions(use_getstringi);
}
return true;
}
#ifdef __APPLE__
static NSAutoreleasePool *pool;
static void
cocoa_init(void)
{
// From the NSApplication Class Reference:
// [...] if you do need to use Cocoa classes within the main()
// function itself (other than to load nib files or to instantiate
// NSApplication), you should create an autorelease pool before using
// the classes and then release the pool when you’re done.
pool = [[NSAutoreleasePool alloc] init];
// From the NSApplication Class Reference:
// The sharedApplication class method initializes the display
// environment and connects your program to the window server and the
// display server.
//
// It also creates the singleton NSApp if it does not yet exist.
[NSApplication sharedApplication];
}
static void
cocoa_finish(void)
{
[pool drain];
}
static void
removeArg(int index, int *argc, char **argv)
{
--*argc;
for (; index < *argc; ++index)
argv[index] = argv[index + 1];
}
static void
removeXcodeArgs(int *argc, char **argv)
{
// Xcode sometimes adds additional arguments.
for (int i = 1; i < *argc; )
{
if (strcmp(argv[i], "-NSDocumentRevisionsDebugMode") == 0 ||
strcmp(argv[i], "-ApplePersistenceIgnoreState" ) == 0)
{
removeArg(i, argc, argv);
removeArg(i, argc, argv);
} else
++i;
}
}
#endif // __APPLE__
/// @brief Attributes for waffle_choose_config().
struct wflinfo_config_attrs {
/// @brief One of `WAFFLE_CONTEXT_OPENGL_*`.
enum waffle_enum api;
/// @brief One of `WAFFLE_CONTEXT_PROFILE_*` or `WAFFLE_NONE`.
enum waffle_enum profile;
/// @brief The version major number.
int32_t major;
/// @brief The version minor number.
int32_t minor;
/// @brief Create a forward-compatible context.
bool forward_compat;
/// @brief Create a debug context.
bool debug;
};
static bool
wflinfo_try_create_context(struct waffle_display *dpy,
struct wflinfo_config_attrs attrs,
struct waffle_context **out_ctx,
struct waffle_config **out_config,
bool exit_on_fail)
{
int i;
int32_t config_attrib_list[64];
struct waffle_context *ctx = NULL;
struct waffle_config *config = NULL;
i = 0;
config_attrib_list[i++] = WAFFLE_CONTEXT_API;
config_attrib_list[i++] = attrs.api;
if (attrs.profile != WAFFLE_DONT_CARE) {
config_attrib_list[i++] = WAFFLE_CONTEXT_PROFILE;
config_attrib_list[i++] = attrs.profile;
}
if (attrs.major != WAFFLE_DONT_CARE && attrs.minor != WAFFLE_DONT_CARE) {
config_attrib_list[i++] = WAFFLE_CONTEXT_MAJOR_VERSION;
config_attrib_list[i++] = attrs.major;
config_attrib_list[i++] = WAFFLE_CONTEXT_MINOR_VERSION;
config_attrib_list[i++] = attrs.minor;
}
if (attrs.forward_compat) {
config_attrib_list[i++] = WAFFLE_CONTEXT_FORWARD_COMPATIBLE;
config_attrib_list[i++] = true;
}
if (attrs.debug) {
config_attrib_list[i++] = WAFFLE_CONTEXT_DEBUG;
config_attrib_list[i++] = true;
}
static int32_t dont_care_attribs[] = {
WAFFLE_RED_SIZE,
WAFFLE_GREEN_SIZE,
WAFFLE_BLUE_SIZE,
WAFFLE_ALPHA_SIZE,
WAFFLE_DEPTH_SIZE,
WAFFLE_STENCIL_SIZE,
WAFFLE_DOUBLE_BUFFERED,
};
int dont_care_attribs_count =
sizeof(dont_care_attribs) / sizeof(dont_care_attribs[0]);
for (int j = 0; j < dont_care_attribs_count; j++) {
config_attrib_list[i++] = dont_care_attribs[j];
config_attrib_list[i++] = WAFFLE_DONT_CARE;
}
config_attrib_list[i++] = 0;
config = waffle_config_choose(dpy, config_attrib_list);
if (!config) {
goto fail;
}
ctx = waffle_context_create(config, NULL);
if (!ctx) {
goto fail;
}
*out_ctx = ctx;
*out_config = config;
return true;
fail:
if (exit_on_fail) {
error_waffle();
}
if (ctx) {
waffle_context_destroy(ctx);
}
if (config) {
waffle_config_destroy(config);
}
return false;
}
/// @brief Return 10 * version of the current OpenGL context.
static int
gl_get_version(void)
{
GLint major_version = 0;
GLint minor_version = 0;
glGetIntegerv(GL_MAJOR_VERSION, &major_version);
if (glGetError()) {
error_printf("Wflinfo", "glGetIntegerv(GL_MAJOR_VERSION) failed");
}
glGetIntegerv(GL_MINOR_VERSION, &minor_version);
if (glGetError()) {
error_printf("Wflinfo", "glGetIntegerv(GL_MINOR_VERSION) failed");
}
return 10 * major_version + minor_version;
}
/// @brief Check if current context has an extension using glGetString().
static bool
gl_has_extension_GetString(const char *name)
{
#define BUF_LEN 4096
char exts[BUF_LEN];
const uint8_t *exts_orig = glGetString(GL_EXTENSIONS);
if (glGetError()) {
error_printf("Wflinfo", "glGetInteger(GL_EXTENSIONS) failed");
}
memcpy(exts, exts_orig, BUF_LEN);
exts[BUF_LEN - 1] = 0;
char *ext = strtok(exts, " ");
do {
if (strneq(ext, name, BUF_LEN)) {
return true;
}
ext = strtok(NULL, " ");
} while (ext);
return false;
#undef BUF_LEN
}
/// @brief Check if current context has an extension using glGetStringi().
static bool
gl_has_extension_GetStringi(const char *name)
{
const size_t max_ext_len = 128;
uint32_t num_exts = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &num_exts);
if (glGetError()) {
error_printf("Wflinfo", "glGetIntegerv(GL_NUM_EXTENSIONS) failed");
}
for (uint32_t i = 0; i < num_exts; i++) {
const uint8_t *ext = glGetStringi(GL_EXTENSIONS, i);