summaryrefslogtreecommitdiff
path: root/tests/spec/glx_arb_create_context/no-error.c
blob: b85e30448f1042b76cadc5654333bd4e5bd00616 (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
/* Copyright 2017 Grigori Goronzy <greg@chown.ath.cx>
 *
 * 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-util.h"
#include "piglit-util-gl.h"
#include "piglit-glx-util.h"
#include "common.h"

#define BOOLSTR(x) ((x) ? "yes" : "no")

#ifndef GLX_CONTEXT_OPENGL_NO_ERROR_ARB
#define GLX_CONTEXT_OPENGL_NO_ERROR_ARB 0x31B3
#endif

static enum piglit_result check_no_error(bool debug, bool robust)
{
	int ctx_flags = 0;
	ctx_flags |= debug ? GLX_CONTEXT_DEBUG_BIT_ARB : 0;
	ctx_flags |= robust ? GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB : 0;
	const int attribs[] = {
		GLX_CONTEXT_MAJOR_VERSION_ARB, 2,
		GLX_CONTEXT_MINOR_VERSION_ARB, 0,
		GLX_CONTEXT_OPENGL_NO_ERROR_ARB, 1,
		GLX_CONTEXT_FLAGS_ARB, ctx_flags,
		None
	};
	static bool is_dispatch_init = false;
	GLXContext ctx;
	enum piglit_result result = PIGLIT_SKIP;

	printf("info: debug=%s, robustness=%s\n", BOOLSTR(debug), BOOLSTR(robust));

	if (robust && !piglit_is_glx_extension_supported(dpy, "GLX_ARB_create_context_robustness")) {
		printf("info: GLX_ARB_create_context_robustness not supported\n");
		result = PIGLIT_SKIP;
		goto done;
	}

	ctx = glXCreateContextAttribsARB(dpy, fbconfig, NULL, True, attribs);
	XSync(dpy, 0);

	if (glx_error_code != -1) {
		if ((debug || robust)) {
			/* KHR_no_error doesn't allow the no error mode to be enabled
			 * with KHR_debug or ARB_robustness, so context creation is
			 * expected to fail in these cases.
			 *
			 * From the ARB_create_context_no_error spec:
			 *
			 *    "BadMatch is generated if the GLX_CONTEXT_OPENGL_NO_ERROR_ARB is TRUE at
			 *    the same time as a debug or robustness context is specified."
			 */
			if (!validate_glx_error_code(BadMatch, -1)) {
				printf("error: incorrect context creation error code\n");
				result = PIGLIT_FAIL;
				goto done;
			}

			printf("info: context creation failed (expected)\n");
			result = PIGLIT_PASS;
			goto done;
		}

		/* Most likely the API/version is not supported. */
		result = PIGLIT_SKIP;
		goto done;
	}
	if (ctx == NULL) {
		printf("error: context creation failed\n");
		result = PIGLIT_FAIL;
		goto done;
	}

	if (!glXMakeContextCurrent(dpy, glxWin, glxWin, ctx)) {
		printf("error: created OpenGL context, but could not make it "
		       "current\n");
		result = PIGLIT_FAIL;
		goto done;
	}

	if (!is_dispatch_init) {
		/* We must postpone initialization of piglit-dispatch until
		 * a context is current.
		 */
		piglit_dispatch_default_init(PIGLIT_DISPATCH_GL);
		is_dispatch_init = true;
	}

	if (!piglit_is_extension_supported("GL_KHR_no_error")) {
		printf("warning: context does not report GL_KHR_no_error "
		       "availability\n");
		result = PIGLIT_WARN;
		goto done;
	}

	if (piglit_get_gl_version() >= 30) {
		GLint context_flags = 0;
		glGetIntegerv(GL_CONTEXT_FLAGS, &context_flags);
		if (!(context_flags & GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR)) {
			printf("error: context does not have "
			       "GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR set\n");
			result = PIGLIT_FAIL;
			goto done;
		}
	}

	result = PIGLIT_PASS;
done:
	printf("info: %s\n", piglit_result_to_string(result));
	glXMakeContextCurrent(dpy, None, None, NULL);
	glXDestroyContext(dpy, ctx);
	return result;
}

static enum piglit_result check_no_error_shared_context()
{
	const int attribs[] = {
		GLX_CONTEXT_MAJOR_VERSION_ARB, 2,
		GLX_CONTEXT_MINOR_VERSION_ARB, 0,
		GLX_CONTEXT_OPENGL_NO_ERROR_ARB, 1,
		GLX_CONTEXT_FLAGS_ARB, 0,
		None
	};

	const int attribs2[] = {
		GLX_CONTEXT_MAJOR_VERSION_ARB, 2,
		GLX_CONTEXT_MINOR_VERSION_ARB, 0,
		GLX_CONTEXT_OPENGL_NO_ERROR_ARB, 0,
		GLX_CONTEXT_FLAGS_ARB, 0,
		None
	};

	static bool is_dispatch_init = false;
	GLXContext ctx;
	enum piglit_result result = PIGLIT_SKIP;

	printf("info: no error attrib mismatch\n");

	ctx = glXCreateContextAttribsARB(dpy, fbconfig, NULL, True, attribs);
	XSync(dpy, 0);

	if (glx_error_code != -1) {
		/* Most likely the API/version is not supported. */
		result = PIGLIT_SKIP;
		goto done;
	}
	if (ctx == NULL) {
		printf("error: context creation failed\n");
		result = PIGLIT_FAIL;
		goto done;
	}

	if (!glXMakeContextCurrent(dpy, glxWin, glxWin, ctx)) {
		printf("error: created OpenGL context, but could not make it "
		       "current\n");
		result = PIGLIT_FAIL;
		goto done;
	}

	if (!is_dispatch_init) {
		/* We must postpone initialization of piglit-dispatch until
		 * a context is current.
		 */
		piglit_dispatch_default_init(PIGLIT_DISPATCH_GL);
		is_dispatch_init = true;
	}

	if (!piglit_is_extension_supported("GL_KHR_no_error")) {
		printf("warning: context does not report GL_KHR_no_error "
		       "availability\n");
		result = PIGLIT_WARN;
		goto done;
	}

	if (piglit_get_gl_version() >= 30) {
		GLint context_flags = 0;
		glGetIntegerv(GL_CONTEXT_FLAGS, &context_flags);
		if (!(context_flags & GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR)) {
			printf("error: context does not have "
			       "GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR set\n");
			result = PIGLIT_FAIL;
			goto done;
		}
	}

	glXCreateContextAttribsARB(dpy, fbconfig, ctx, True, attribs2);
	if (validate_glx_error_code(BadMatch, -1)) {
		/* From the ARB_create_context_no_error spec:
		 *
		 *    "BadMatch is generated if the value of GLX_CONTEXT_OPENGL_NO_ERROR_ARB
		 *    used to create <share_context> does not match the value of
		 *    GLX_CONTEXT_OPENGL_NO_ERROR_ARB for the context being created."
		 */
		printf("info: context creation failed (expected)\n");
		result = PIGLIT_PASS;
		goto done;
	}

	result = PIGLIT_FAIL;
done:
	printf("info: %s\n", piglit_result_to_string(result));
	glXMakeContextCurrent(dpy, None, None, NULL);
	glXDestroyContext(dpy, ctx);
	return result;
}

int main(int argc, char **argv)
{
	enum piglit_result result = PIGLIT_SKIP;

	for (int i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "-auto"))
			piglit_automatic = 1;
		else
			fprintf(stderr, "Unknown option: %s\n", argv[i]);
	}

	GLX_ARB_create_context_setup();
	piglit_require_glx_extension(dpy, "GLX_ARB_create_context_no_error");

	/* Check that KHR_no_error gets enabled and its interaction with debug and
	 * robustness context flags. */
	piglit_merge_result(&result, check_no_error(false, false));
	piglit_merge_result(&result, check_no_error(true, false));
	piglit_merge_result(&result, check_no_error(false, true));
	piglit_merge_result(&result, check_no_error(true, true));

	piglit_merge_result(&result, check_no_error_shared_context());

	GLX_ARB_create_context_teardown();

	piglit_report_result(result);
}