summaryrefslogtreecommitdiff
path: root/tests/testglxgetprocaddress.c
blob: 4c18bfc00d1b61b2655db09a87a1914d29a47632 (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
/*
 * Copyright (c) 2013, NVIDIA CORPORATION.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and/or associated documentation files (the
 * "Materials"), to deal in the Materials without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Materials, and to
 * permit persons to whom the Materials are furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * unaltered in all copies or substantial portions of the Materials.
 * Any additions, deletions, or changes to the original source files
 * must be clearly indicated in accompanying documentation.
 *
 * If only executable code is distributed, then the accompanying
 * documentation must state that "this software is based in part on the
 * work of the Khronos Group."
 *
 * THE MATERIALS ARE 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
 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
 */

#include <GL/glx.h>
#include <GL/gl.h>
#include <stdio.h>

#define PROC_DEFINES(ret, proc, params)  \
    typedef ret (*PTR_ ## proc) params ; \
    static PTR_ ## proc p_ ## proc

#define CHECK_PROC(proc, args) do {                             \
    printf("checking " #proc "\n");                             \
    if (!(p_ ## proc =                                          \
         (PTR_ ## proc)glXGetProcAddress((GLubyte *)#proc))) {  \
        printf("failed to get " #proc "!\n");                   \
        goto fail;                                              \
    }                                                           \
    (*p_ ## proc) args;                                         \
} while (0)

PROC_DEFINES(void *, glXGetProcAddress, (GLubyte *procName));
PROC_DEFINES(void, glXWaitGL, (void));
PROC_DEFINES(void, glVertex3fv, (GLfloat *v));
PROC_DEFINES(void, glXExampleExtensionFunction,
             (Display *dpy, int screen, int *retval));
PROC_DEFINES(void, glBogusFunc1, (int a, int b, int c));
PROC_DEFINES(void, glBogusFunc2, (int a, int b, int c));

int main(int argc, char **argv)
{
    Display *dpy = NULL;

    int retval = 0;
    /*
     * Try GetProcAddress on different classes of API functions, and bogus
     * functions. The API library should return entry point addresses for
     * any function beginning with gl*(), though bogus functions will resolve
     * to no-ops.
     */

    dpy = XOpenDisplay(NULL);
    if (dpy == NULL) {
        printf("Can't open display\n");
        goto fail;
    }

    /*
     * Test core GLX dispatch functions implemented by API library. This
     * simply returns the symbol exported by libGLX.
     */
    CHECK_PROC(glXGetProcAddress, ((GLubyte *)"glBogusFunc1"));
    CHECK_PROC(glXWaitGL, ());

    /*
     * Test a "GLX extension" function with a vendor-neutral dispatcher
     * implemented by the vendor library (in this case, libGLX_dummy).  If we
     * successfully used libGLX_dummy's dispatcher, retval should be non-zero
     * (a zero value indicates we might be calling into a no-op stub generated
     * by libGLdispatch).
     */
    CHECK_PROC(glXExampleExtensionFunction, (dpy, DefaultScreen(dpy), &retval));

    if (!retval) {
        printf("Unexpected glXExampleExtensionFunction() return value!\n");
        goto fail;
    }

    /*
     * Try getting the address of the core GL function glVertex3fv().
     * This retrieves a static stub from glapi.
     * Note calling this function with a NULL pointer is fine since this is a
     * no-op function while there is no context current.
     */
    CHECK_PROC(glVertex3fv, (NULL));

    /*
     * These are bogus functions, but will get a valid entry point since they
     * are prefixed with "gl". The first GetProcAddress() will early out since
     * there should be a cached copy from the explicit call made above, but
     * the second one will go through glapi's dynamic stub generation path.
     *
     * Again, calling these functions should be a no-op.
     */
    CHECK_PROC(glBogusFunc1, (0, 0, 0));
    CHECK_PROC(glBogusFunc2, (1, 1, 1));

    // Success!
    return 0;

fail:
    return -1;
}