summaryrefslogtreecommitdiff
path: root/test/wgl_per_context_funcptrs.c
blob: 2cf0dcbb505f054126c7b0afc92e7f82d38c15bf (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
/*
 * 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 wgl_per_context_funcptrs.c
 *
 * Tests that epoxy works correctly when wglGetProcAddress() returns
 * different function pointers for different contexts.
 *
 * wgl allows that to be the case when the device or pixel format are
 * different.  We don't know if the underlying implementation actually
 * *will* return different function pointers, so force the issue by
 * overriding wglGetProcAddress() to return our function pointers with
 * magic behavior.  This way we can test epoxy's implementation
 * regardless.
 */

#include <stdio.h>
#include <assert.h>

#include "wgl_common.h"
#include <epoxy/gl.h>

#define CREATESHADER_CTX1_VAL 1001
#define CREATESHADER_CTX2_VAL 1002

static HGLRC ctx1, ctx2, current_context;
static bool pass = true;

#define OVERRIDE_API(type) __declspec(dllexport) type __stdcall

OVERRIDE_API (GLuint) override_glCreateShader_ctx1(GLenum target);
OVERRIDE_API (GLuint) override_glCreateShader_ctx2(GLenum target);
OVERRIDE_API (PROC) override_wglGetProcAddress(LPCSTR name);

OVERRIDE_API (GLuint)
override_glCreateShader_ctx1(GLenum target)
{
    if (current_context != ctx1) {
        fputs("ctx1 called while other context current\n", stderr);
        pass = false;
    }
    return CREATESHADER_CTX1_VAL;
}

OVERRIDE_API (GLuint)
override_glCreateShader_ctx2(GLenum target)
{
    if (current_context != ctx2) {
        fputs("ctx2 called while other context current\n", stderr);
        pass = false;
    }
    return CREATESHADER_CTX2_VAL;
}

OVERRIDE_API (PROC)
override_wglGetProcAddress(LPCSTR name)
{
    assert(strcmp(name, "glCreateShader") == 0);

    if (current_context == ctx1) {
        return (PROC)override_glCreateShader_ctx1;
    } else {
        assert(current_context == ctx2);
        return (PROC)override_glCreateShader_ctx2;
    }
}

static void
test_createshader(HDC hdc, HGLRC ctx)
{
    GLuint shader, expected;
    int ctxnum;

    wglMakeCurrent(hdc, ctx);
    current_context = ctx;

    /* Install our GPA override so we can force per-context function
     * pointers.
     */
    wglGetProcAddress = override_wglGetProcAddress;

    if (ctx == ctx1) {
        expected = CREATESHADER_CTX1_VAL;
        ctxnum = 1;
    } else {
        assert(ctx == ctx2);
        expected = CREATESHADER_CTX2_VAL;
        ctxnum = 2;
    }

    shader = glCreateShader(GL_FRAGMENT_SHADER);
    printf("ctx%d: Returned %d\n", ctxnum, shader);
    if (shader != expected) {
        fprintf(stderr, "  expected %d\n", expected);
        pass = false;
    }
}

static int
test_function(HDC hdc)
{
    ctx1 = wglCreateContext(hdc);
    ctx2 = wglCreateContext(hdc);
    if (!ctx1 || !ctx2) {
        fputs("Failed to create wgl contexts\n", stderr);
        return 1;
    }

    if (!wglMakeCurrent(hdc, ctx1)) {
        fputs("Failed to make context current\n", stderr);
        return 1;
    }

    if (epoxy_gl_version() < 20) {
        /* We could possibly do a 1.3 entrypoint or something instead. */
        fputs("Test relies on overriding a GL 2.0 entrypoint\n", stderr);
        return 77;
    }

    /* Force resolving epoxy_wglGetProcAddress. */
    wglGetProcAddress("glCreateShader");

    test_createshader(hdc, ctx1);
    test_createshader(hdc, ctx1);
    test_createshader(hdc, ctx2);
    test_createshader(hdc, ctx2);
    test_createshader(hdc, ctx1);
    test_createshader(hdc, ctx2);

    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(ctx1);
    wglDeleteContext(ctx2);

    return !pass;
}

int
main(int argc, char **argv)
{
    make_window_and_test(test_function);

    /* UNREACHED */
    return 1;
}