aboutsummaryrefslogtreecommitdiff
path: root/tests/util/piglit-framework-fbo.c
blob: 41333848e587914e84aa3d7bf8215915eca0799a (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
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
/*
 * Copyright © 2009-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.
 */

#if defined(USE_OPENGL_ES1)
#	define PIGLIT_FRAMEWORK_FBO_DISABLED
#elif defined(USE_WAFFLE)
#	define PIGLIT_FRAMEWORK_FBO_USE_WAFFLE
#elif defined(USE_GLX)
#	define PIGLIT_FRAMEWORK_FBO_USE_GLX
#else
#	define PIGLIT_FRAMEWORK_FBO_DISABLED
#endif

#ifdef USE_OPENGL_ES2
#	define GL_DEPTH_STENCIL GL_DEPTH_STENCIL_OES
#	define GL_UNSIGNED_INT_24_8 GL_UNSIGNED_INT_24_8_OES
#endif

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

#include "piglit-util-gl-common.h"
#include "piglit-framework.h"
#include "piglit-framework-fbo.h"

#ifdef PIGLIT_FRAMEWORK_FBO_USE_GLX
#include "piglit-glx-util.h"
#endif

#ifdef PIGLIT_FRAMEWORK_FBO_USE_WAFFLE
#include <waffle/waffle.h>
#endif

#ifdef PIGLIT_FRAMEWORK_FBO_USE_GLX
Display *piglit_glx_dpy;
Window piglit_glx_window;
XVisualInfo *piglit_glx_visinfo;
GLXContext piglit_glx_context;
#endif

#ifdef PIGLIT_FRAMEWORK_FBO_USE_WAFFLE
static struct waffle_display *piglit_waffle_display;
static struct waffle_window *piglit_waffle_window;
static struct waffle_context *piglit_waffle_context;
#endif

static void
piglit_framework_fbo_destroy(void);

#ifdef PIGLIT_FRAMEWORK_FBO_USE_GLX
static void
piglit_framework_fbo_glx_init()
{
	piglit_glx_dpy = piglit_get_glx_display();

	/* Unfortunately in GLX we need a drawable to bind our context
	 * to.  Make an unmapped window.
	 */
	piglit_glx_visinfo = piglit_get_glx_visual(piglit_glx_dpy);

	piglit_glx_context = piglit_get_glx_context(piglit_glx_dpy,
						    piglit_glx_visinfo);

	piglit_glx_window = piglit_get_glx_window_unmapped(piglit_glx_dpy,
							   piglit_glx_visinfo);

	glXMakeCurrent(piglit_glx_dpy, piglit_glx_window, piglit_glx_context);
}

static void
piglit_framework_fbo_glx_destroy()
{
	glXMakeCurrent(piglit_glx_dpy, None, None);
	glXDestroyContext(piglit_glx_dpy, piglit_glx_context);
	XFree(piglit_glx_visinfo);
	XCloseDisplay(piglit_glx_dpy);
}
#endif

#ifdef PIGLIT_FRAMEWORK_FBO_USE_WAFFLE
/**
 * \brief Print a description of the Waffle error and report test failure.
 *
 * The \a func_name is the name of the Waffle function that failed.
 */
static void
fatal_waffle_error(const char *func_name)
{
	const struct waffle_error_info *info = waffle_error_get_info();
	const char *error_name = waffle_error_to_string(info->code);

	fflush(stdout);
	fprintf(stderr, "%s failed with error: %s", func_name, error_name);
	if (info->message_length > 0)
		fprintf(stderr, ": %s", info->message);
	fprintf(stderr, "\n");

	piglit_report_result(PIGLIT_FAIL);
}

static void
piglit_framework_fbo_waffle_init(void)
{
	int i;
	bool ok = true;
	const char *env_platform;
	int32_t waffle_platform;
	int32_t waffle_context_api;
	int32_t init_attrib_list[64];
	int32_t config_attrib_list[64];
	struct waffle_config *config;

	env_platform = getenv("WAFFLE_PLATFORM");

	if (env_platform == NULL) {
		waffle_platform = WAFFLE_PLATFORM_GLX;
	} else if (!strcmp(env_platform, "glx")) {
		waffle_platform = WAFFLE_PLATFORM_GLX;
	} else if (!strcmp(env_platform, "x11_egl")) {
		waffle_platform = WAFFLE_PLATFORM_X11_EGL;
	} else if (!strcmp(env_platform, "wayland")) {
		waffle_platform = WAFFLE_PLATFORM_WAYLAND;
	} else {
		fprintf(stderr, "environment var WAFFLE_PLATFORM has bad "
			"value \"%s\"", env_platform);
	}

#if defined(USE_OPENGL)
	waffle_context_api = WAFFLE_CONTEXT_OPENGL;
#elif defined(USE_OPENGL_ES1)
	waffle_context_api = WAFFLE_CONTEXT_OPENGL_ES1;
#elif defined(USE_OPENGL_ES2)
	waffle_context_api = WAFFLE_CONTEXT_OPENGL_ES2;
#else
#	error
#endif
	i = 0;
	init_attrib_list[i++] = WAFFLE_PLATFORM;
	init_attrib_list[i++] = waffle_platform;
	init_attrib_list[i++] = WAFFLE_NONE;

	i = 0;
	config_attrib_list[i++] = WAFFLE_CONTEXT_API;
	config_attrib_list[i++] = waffle_context_api;
	config_attrib_list[i++] = WAFFLE_RED_SIZE;
	config_attrib_list[i++] = 1;
	config_attrib_list[i++] = WAFFLE_GREEN_SIZE;
	config_attrib_list[i++] = 1;
	config_attrib_list[i++] = WAFFLE_BLUE_SIZE;
	config_attrib_list[i++] = 1;
	config_attrib_list[i++] = WAFFLE_DOUBLE_BUFFERED;
	config_attrib_list[i++] = 1;
	config_attrib_list[i++] = WAFFLE_NONE;

	ok = waffle_init(init_attrib_list);
	if (!ok)
		fatal_waffle_error("waffle_init");

	piglit_waffle_display = waffle_display_connect(NULL);
	if (!piglit_waffle_display)
		fatal_waffle_error("waffle_display_connect");

	config = waffle_config_choose(piglit_waffle_display,
	                              config_attrib_list);
	if (!config)
		fatal_waffle_error("waffle_config_choose");

	piglit_waffle_context = waffle_context_create(config, NULL);
	if (!piglit_waffle_context)
		fatal_waffle_error("waffle_context_create");

	piglit_waffle_window = waffle_window_create(config,
	                                            piglit_width,
	                                            piglit_height);
	if (!piglit_waffle_window)
		fatal_waffle_error("waffle_window_create");

	ok = waffle_make_current(piglit_waffle_display,
			         piglit_waffle_window,
			         piglit_waffle_context);
	if (!ok)
		fatal_waffle_error("waffle_make_current");

	// Cleanup.
	ok = waffle_config_destroy(config);
	if (!ok)
		fatal_waffle_error("waffle_config_destroy");
}

static void
piglit_framework_fbo_waffle_destroy(void)
{
	bool ok = true;

	ok = waffle_make_current(piglit_waffle_display, NULL, NULL);
	if (!ok)
		fatal_waffle_error("waffle_make_current");

	ok = waffle_context_destroy(piglit_waffle_context);
	if (!ok)
		fatal_waffle_error("waffle_context_destroy");

	ok = waffle_display_disconnect(piglit_waffle_display);
	if (!ok)
		fatal_waffle_error("waffle_display_connect");

	piglit_waffle_display = NULL;
	piglit_waffle_context = NULL;
}
#endif

static bool
piglit_framework_fbo_gl_init(const struct piglit_gl_test_info *info)
{
#ifdef PIGLIT_FRAMEWORK_FBO_DISABLED
	return false;
#else
	GLuint tex, depth = 0;
	GLenum status;

#ifdef USE_OPENGL
	glewInit();

	if (piglit_get_gl_version() < 20)
		return false;
#endif

	glGenFramebuffers(1, &piglit_winsys_fbo);
	glBindFramebuffer(GL_FRAMEBUFFER, piglit_winsys_fbo);

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
		     piglit_width, piglit_height, 0,
		     GL_RGBA, GL_UNSIGNED_BYTE, NULL);
	glFramebufferTexture2D(GL_FRAMEBUFFER,
			       GL_COLOR_ATTACHMENT0,
			       GL_TEXTURE_2D,
			       tex,
			       0);

	if (info->window_visual & (GLUT_DEPTH | GLUT_STENCIL)) {
		/* Create a combined depth+stencil texture and attach it
		 * to the depth and stencil attachment points.
		 */
		glGenTextures(1, &depth);
		glBindTexture(GL_TEXTURE_2D, depth);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_STENCIL,
			     piglit_width, piglit_height, 0,
			     GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
		glFramebufferTexture2D(GL_FRAMEBUFFER,
				       GL_DEPTH_ATTACHMENT,
				       GL_TEXTURE_2D,
				       depth,
				       0);
		glFramebufferTexture2D(GL_FRAMEBUFFER,
				       GL_STENCIL_ATTACHMENT,
				       GL_TEXTURE_2D,
				       depth,
				       0);
	}

	glBindTexture(GL_TEXTURE_2D, 0);

	status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if (status != GL_FRAMEBUFFER_COMPLETE) {
		fprintf(stderr,
			"-fbo resulted in incomplete FBO, falling back\n");
		glBindFramebuffer(GL_FRAMEBUFFER, 0);

		glDeleteTextures(1, &depth);
		glDeleteTextures(1, &tex);

		piglit_framework_fbo_destroy();

		return false;
	}

	return true;
#endif /* PIGLIT_FRAMEWORK_FBO_DISABLED */
}

bool
piglit_framework_fbo_init(const struct piglit_gl_test_info *info)
{
#if defined(PIGLIT_FRAMEWORK_FBO_USE_GLX)
	piglit_framework_fbo_glx_init();
#elif defined(PIGLIT_FRAMEWORK_FBO_USE_WAFFLE)
	piglit_framework_fbo_waffle_init();
#endif

	return piglit_framework_fbo_gl_init(info);
}

static void
piglit_framework_fbo_destroy(void)
{
#ifdef USE_OPENGL
	glDeleteFramebuffers(1, &piglit_winsys_fbo);
#endif

	piglit_winsys_fbo = 0;

#if defined(PIGLIT_FRAMEWORK_FBO_USE_GLX)
	piglit_framework_fbo_glx_destroy();
#elif defined(PIGLIT_FRAMEWORK_FBO_USE_WAFFLE)
	piglit_framework_fbo_waffle_destroy();
#endif
}

void
piglit_framework_fbo_run(const struct piglit_gl_test_info *info)
{
	enum piglit_result result = info->display();
	piglit_framework_fbo_destroy();
	piglit_report_result(result);
}