summaryrefslogtreecommitdiff
path: root/tests/util/piglit-dispatch.c
blob: 90a6dba276ed2cdfa7ffe70ccaaf49b18165f5e5 (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
/* Copyright 2012 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.
 */

#include "piglit-dispatch.h"
#include "piglit-util-gl.h"

/* Global state maintained by the Piglit dispatch mechanism: */

/**
 * Which function to call to get the address of a core function.
 */
static piglit_get_core_proc_address_function_ptr get_core_proc_address = NULL;

/**
 * Which function to call to get the address of a function defined in
 * an extension.
 */
static piglit_get_ext_proc_address_function_ptr get_ext_proc_address = NULL;

/**
 * Which function to call if the test attempts to call a function that
 * is not supported by the implementation.
 */
static piglit_error_function_ptr unsupported = NULL;

/**
 * Which function to call if get_core_proc_address or
 * get_ext_proc_address returns NULL.
 */
static piglit_error_function_ptr get_proc_address_failure = NULL;

/**
 * The GL version extracted from glGetString(GL_VERSION), times 10.
 * For example, if the GL version is 2.1, the value 21 is stored here.
 *
 * We cache this here because calling glGetString is prohibited
 * between glBegin and glEnd, and to avoid the inefficiency of
 * redundant glGetString queries.
 */
static int gl_version = 0;

/**
 * True if piglit_dispatch_init has been called.
 */
static bool is_initialized = false;

static piglit_dispatch_api dispatch_api;

/**
 * Generated code calls this function to verify that the dispatch
 * mechanism has been properly initialized.
 */
static void
check_initialized()
{
	if (is_initialized)
		return;

	printf("piglit_dispatch_init() must be called before GL functions\n");
	assert(false);
	exit(1);
}

/**
 * Generated code calls this function to retrieve the address of a
 * core function.
 */
static piglit_dispatch_function_ptr
get_core_proc(const char *name, int gl_10x_version)
{
	piglit_dispatch_function_ptr function_pointer = get_core_proc_address(name, gl_10x_version);
	if (function_pointer == NULL)
		get_proc_address_failure(name);
	return function_pointer;
}

/**
 * Generated code calls this function to retrieve the address of a
 * function defined in an extension.
 */
static piglit_dispatch_function_ptr
get_ext_proc(const char *name)
{
	piglit_dispatch_function_ptr function_pointer = get_ext_proc_address(name);
	if (function_pointer == NULL)
		get_proc_address_failure(name);
	return function_pointer;
}

/**
 * Generated code calls this function to determine whether a given GL
 * version is supported.
 */
static inline bool
check_version(int required_version)
{
	return gl_version >= required_version;
}

/**
 * Generated code calls this function to determine whether a given
 * extension is supported.
 */
static inline bool
check_extension(const char *name)
{
	return piglit_is_extension_supported(name);
}

#include "piglit-dispatch-gen.c"

/**
 * Initialize the dispatch mechanism.
 *
 * \param api is the API under test.  This determines whether
 * deprecated functionality is supported (since deprecated functions
 * cannot be used in forward compatible contexts).  It also affects
 * which GL version is queried for (since, for example a function
 * might be supported in GLES as of version 2.0, but in OpenGL only as
 * of version 2.1).  Not yet implemented.
 *
 * \param get_core_proc and get_ext_proc are the functions to call to
 * retrieve the address of a core GL function or an extension
 * function.  Note that for OpenGL, these can both map to the same
 * function (e.g. glXGetProcAddressARB).  However, in GLES, core
 * functions are not allowed to be queried using the GetProcAddress
 * mechanism, so get_core_proc will need to be implemented by looking
 * up a symbol in a shared library (e.g. using dlsym()).  When Waffle
 * is in use, these are ignored and replaced with the Waffle functions
 * that do the appropriate lookup according to the platform.  One day
 * we'll drop non-waffle support and remove this part of the
 * interface.
 *
 * \param unsupported_proc is the function to call if a test attempts
 * to use unsupported GL functionality.  It is passed the name of the
 * function that the test attempted to use.
 *
 * \param failure_proc is the function to call if a call to
 * get_core_proc() or get_ext_proc() unexpectedly returned NULL.  It
 * is passed the name of the function that was passed to
 * get_core_proc() or get_ext_proc().
 */
void
piglit_dispatch_init(piglit_dispatch_api api,
		     piglit_get_core_proc_address_function_ptr get_core_proc,
		     piglit_get_ext_proc_address_function_ptr get_ext_proc,
		     piglit_error_function_ptr unsupported_proc,
		     piglit_error_function_ptr failure_proc)
{
	dispatch_api = api;

	get_core_proc_address = get_core_proc;
	get_ext_proc_address = get_ext_proc;
	unsupported = unsupported_proc;
	get_proc_address_failure = failure_proc;

	/* No need to reset the dispatch pointers the first time */
	if (is_initialized) {
		reset_dispatch_pointers();
	}

	is_initialized = true;

	/* Store the GL version and extension string for use by
	 * check_version() and check_extension().  Note: the
	 * following two calls are safe because the only GL function
	 * they call is glGetString(), and the stub function for
	 * glGetString does not need to call check_version() or
	 * check_extension().
	 */
	gl_version = piglit_get_gl_version();
}

/**
 * Compare two strings in the function_names table.
 */
static int compare_function_names(const void *x, const void *y)
{
	const char *x_str = *(const char * const *) x;
	const char *y_str = *(const char * const *) y;
	return strcmp(x_str, y_str);
}

/**
 * Retrieve a GL function pointer given the function name.
 *
 * This function is similar to glXGetProcAddressARB(), except that:
 *
 * - It is platform-independent.
 *
 * - It may be called on any supported function, regardless of whether
 *   the function is defined in GL core or an extension, and
 *   regardless of whether desktop GL or GLES is in use.
 *
 * - Synonymous function names (e.g. glMapBuffer and glMapBufferARB)
 *   may be used interchangably; the correct function is automatically
 *   chosen based on the GL version and extension string.
 *
 * - If the requested function is not supported by the implementation,
 *   the unsupported_proc that was passed to piglit_dispatch_init() is
 *   called.
 */
piglit_dispatch_function_ptr
piglit_dispatch_resolve_function(const char *name)
{
	size_t item_size = sizeof(function_names[0]);
	size_t num_items = ARRAY_SIZE(function_names);
	const char * const *item = (const char * const *)
		bsearch(&name, function_names, num_items, item_size,
			compare_function_names);
	check_initialized();
	if (!item) {
		unsupported(name);
		return NULL;
	} else {
		size_t item_index = item - function_names;
		return function_resolvers[item_index]();
	}
}