summaryrefslogtreecommitdiff
path: root/tests/fbo/fbo-incomplete.cpp
blob: e48ec4b31deedb9f4da2b323f98e346650f4429d (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * 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 fbo-incomplete.c
 * Collection of negative framebuffer completeness tests.
 */

#include "piglit-util-gl.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 10;

	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;

PIGLIT_GL_TEST_CONFIG_END

static const float green[] = { 0.f, 1.f, 0.f, 1.f };

class incomplete_fbo_test {
public:

	incomplete_fbo_test(const char *_name, GLenum _target)
		: name(_name), target(_target), tex(0), rb(0), fbo(0),
		  _pass(true)
	{
		if (target == GL_RENDERBUFFER) {
			glGenRenderbuffers(1, &rb);
			glBindRenderbuffer(target, rb);
		} else {
			glGenTextures(1, &tex);
			glBindTexture(target, tex);
			glTexParameteri(target,
					GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		}

		glGenFramebuffers(1, &fbo);
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	}


	~incomplete_fbo_test()
	{
		if (target == GL_RENDERBUFFER)
			glBindRenderbuffer(target, 0);
		else
			glBindTexture(target, 0);

		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
		glBindFramebuffer(GL_READ_FRAMEBUFFER, piglit_winsys_fbo);

		glDeleteTextures(1, &tex);
		glDeleteRenderbuffers(1, &rb);
		glDeleteFramebuffers(1, &fbo);

		piglit_report_subtest_result(_pass ? PIGLIT_PASS : PIGLIT_FAIL,
					     "%s", name);
	}

	bool check_fbo_status(GLenum expect)
	{
		GLenum status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
		if (status != expect) {
			fprintf(stderr,
				"status was %s (0x%04x), "
				"expected %s (0x%04x).\n",
				piglit_get_gl_enum_name(status),
				status,
				piglit_get_gl_enum_name(expect),
				expect);
			return false;
		}

		return true;
	}

	bool pass()
	{
		_pass = true;
		return true;
	}

	bool fail()
	{
		_pass = false;
		return false;
	}

	const char *name;
	GLenum target;
	GLuint tex;
	GLuint rb;
	GLuint fbo;

private:
	bool _pass;
};

/**
 * Verify that attaching a 0x0 texture results in incompleteness.
 */
bool
incomplete_0_by_0_texture(void)
{
	incomplete_fbo_test t("0x0 texture", GL_TEXTURE_2D);

	/* Attach a 0x0 texture to the framebuffer.  That should make it
	 * incomplete.
	 */
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 0, GL_RGBA,
		     GL_UNSIGNED_BYTE, NULL);
	glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
			       GL_TEXTURE_2D, t.tex, 0);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		return t.fail();

	if (!t.check_fbo_status(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT))
		return t.fail();

	/* Allocate some storage for the texture and verify that the FBO is
	 * now complete.
	 */
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA,
		     GL_UNSIGNED_BYTE, NULL);

	if (!t.check_fbo_status(GL_FRAMEBUFFER_COMPLETE))
		return t.fail();

	/* Verify that simple rendering can occur to the FBO.
	 */
	glClearColor(0.f, 1.f, 0.f, 1.f);
	glClear(GL_COLOR_BUFFER_BIT);

	glBindFramebuffer(GL_READ_FRAMEBUFFER, t.fbo);
	if (!piglit_probe_rect_rgba(0, 0, 4, 4, green))
		return t.fail();

	return t.pass();
}

/**
 * Verify that attaching a 0x0 renderbuffer results in incompleteness.
 */
bool
incomplete_0_by_0_renderbuffer(void)
{
	incomplete_fbo_test t("0x0 renderbuffer", GL_RENDERBUFFER);

	/* Attach a 0x0 renderbuffer to the framebuffer.  That should make it
	 * incomplete.
	 */
	glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 0, 0);
	glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
				  GL_RENDERBUFFER, t.rb);
	if (!piglit_check_gl_error(GL_NO_ERROR))
		return t.fail();

	if (!t.check_fbo_status(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT))
		return t.fail();

	/* Allocate some storage for the renderbuffer and verify that
	 * the FBO is now complete.
	 */
	glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 4, 4);

	if (!t.check_fbo_status(GL_FRAMEBUFFER_COMPLETE))
		return t.fail();

	/* Verify that simple rendering can occur to the FBO.
	 */
	glClearColor(0.f, 1.f, 0.f, 1.f);
	glClear(GL_COLOR_BUFFER_BIT);

	glBindFramebuffer(GL_READ_FRAMEBUFFER, t.fbo);
	if (!piglit_probe_rect_rgba(0, 0, 4, 4, green))
		return t.fail();

	return t.pass();
}

/**
 * Verify that attaching an invalid slice of a 3D texture results in
 * incompleteness.
 */
bool
invalid_3d_slice(void)
{
	incomplete_fbo_test t("invalid slice of 3D texture", GL_TEXTURE_3D);

	/* Create a texture with only 8 slices (0 through 7), but try to
	 * attach slice 8 and slice 9 to the FBO.
	 */
	glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 8, 8, 8, 0, GL_RGBA,
		     GL_UNSIGNED_BYTE, NULL);
	glFramebufferTexture3D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
			       GL_TEXTURE_3D, t.tex, 0, 8);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		return t.fail();

	if (!t.check_fbo_status(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT))
		return t.fail();

	glFramebufferTexture3D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
			       GL_TEXTURE_3D, t.tex, 0, 9);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		return t.fail();

	if (!t.check_fbo_status(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT))
		return t.fail();

	/* Now try slice 7.  This should work.
	 */
	glFramebufferTexture3D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
			       GL_TEXTURE_3D, t.tex, 0, 7);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		return t.fail();

	if (!t.check_fbo_status(GL_FRAMEBUFFER_COMPLETE))
		return t.fail();

	return t.pass();
}

/**
 * Common code the verify attaching an invalid layer of an array texture
 * results in incompleteness.
 */
bool
invalid_array_layer_common(incomplete_fbo_test &t)
{
	const unsigned scale = (t.target == GL_TEXTURE_CUBE_MAP_ARRAY) ? 6 : 1;

	/* The texture has only 8 layers (0 through 7), but try to attach
	 * layer 8 and layer 9 to the FBO.
	 */
	glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
				  t.tex, 0, 8 * scale);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		return t.fail();

	if (!t.check_fbo_status(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT))
		return t.fail();

	glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
				  t.tex, 0, 9 * scale);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		return t.fail();

	if (!t.check_fbo_status(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT))
		return t.fail();

	/* Now try layer 7.  This should work.
	 */
	glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
				  t.tex, 0, 7 * scale);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		return t.fail();

	if (!t.check_fbo_status(GL_FRAMEBUFFER_COMPLETE))
		return t.fail();
	return t.pass();
}

/**
 * Verify that attaching an invalid layer of a 1D array texture results in
 * incompleteness.
 */
bool
invalid_1d_array_layer(void)
{
	static const char subtest_name[] =
		"invalid layer of a 1D-array texture";

	if (!piglit_is_extension_supported("GL_EXT_texture_array")
	    && piglit_get_gl_version() < 30) {
		piglit_report_subtest_result(PIGLIT_SKIP, "%s", subtest_name);
		return true;
	}

	incomplete_fbo_test t(subtest_name, GL_TEXTURE_1D_ARRAY);

	/* Create a texture with only 8 layers (0 through 7), but try to
	 * attach layer 8 and layer 9 to the FBO.
	 */
	glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA, 8, 8, 0, GL_RGBA,
		     GL_UNSIGNED_BYTE, NULL);

	return invalid_array_layer_common(t);
}

/**
 * Verify that attaching an invalid layer of a 2D array texture results in
 * incompleteness.
 */
bool
invalid_2d_array_layer(void)
{
	static const char subtest_name[] =
		"invalid layer of a 2D-array texture";

	if (!piglit_is_extension_supported("GL_EXT_texture_array")
	    && piglit_get_gl_version() < 30) {
		piglit_report_subtest_result(PIGLIT_SKIP, "%s", subtest_name);
		return true;
	}

	incomplete_fbo_test t(subtest_name, GL_TEXTURE_2D_ARRAY);

	/* Create a texture with only 8 layers (0 through 7), but try to
	 * attach layer 8 and layer 9 to the FBO.
	 */
	glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 8, 8, 8, 0, GL_RGBA,
		     GL_UNSIGNED_BYTE, NULL);

	return invalid_array_layer_common(t);
}

/**
 * Verify that attaching an invalid layer of a cube array texture results in
 * incompleteness.
 */
bool
invalid_cube_array_layer(void)
{
	static const char subtest_name[] =
		"invalid layer of a cube-array texture";

	if (!piglit_is_extension_supported("GL_ARB_texture_cube_map_array")
	    && piglit_get_gl_version() < 40) {
		piglit_report_subtest_result(PIGLIT_SKIP, "%s", subtest_name);
		return true;
	}

	incomplete_fbo_test t(subtest_name, GL_TEXTURE_CUBE_MAP_ARRAY);

	/* Create a texture with only 8 layers (0 through 7), but try to
	 * attach layer 8 and layer 9 to the FBO.
	 */
	glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA, 8, 8, 8 * 6, 0,
		     GL_RGBA, GL_UNSIGNED_BYTE, NULL);

	return invalid_array_layer_common(t);
}

/**
 * Verify that deleting the texture attached the currently bound FBO
 * results in incompleteness.
 */
bool
delete_texture_of_current_fbo(void)
{
	incomplete_fbo_test t("delete texture of bound FBO",
			      GL_TEXTURE_2D);

	/* Create a small color texture and attach it.  Everything should
	 * be fine at this point.
	 */
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA,
		     GL_UNSIGNED_BYTE, NULL);
	glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
			       GL_TEXTURE_2D, t.tex, 0);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		return t.fail();

	if (!t.check_fbo_status(GL_FRAMEBUFFER_COMPLETE))
		return t.fail();

	/* Now unbind the texture and delete it.  t.rb is set to zero so
	 * that the destructor won't try to delete it again.
	 */
	glBindTexture(GL_TEXTURE_2D, 0);
	glDeleteTextures(1, &t.tex);
	t.tex = 0;

	/* Now the deleted attachment is "missing."
	 */
	if (!t.check_fbo_status(GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT))
		return t.fail();

	return t.pass();
}

/**
 * Verify that deleting the renderbuffer attached the currently bound FBO
 * results in incompleteness.
 */
bool
delete_renderbuffer_of_current_fbo(void)
{
	incomplete_fbo_test t("delete renderbuffer of bound FBO",
			      GL_RENDERBUFFER);

	/* Create a small color renderbuffer and attach it.  Everything should
	 * be fine at this point.
	 */
	glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 4, 4);
	glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
				  GL_RENDERBUFFER, t.rb);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		return t.fail();

	if (!t.check_fbo_status(GL_FRAMEBUFFER_COMPLETE))
		return t.fail();

	/* Now unbind the renderbuffer and delete it.  t.rb is set to zero so
	 * that the destructor won't try to delete it again.
	 */
	glBindRenderbuffer(GL_RENDERBUFFER, 0);
	glDeleteRenderbuffers(1, &t.rb);
	t.rb = 0;

	/* Now the deleted attachment is "missing."
	 */
	if (!t.check_fbo_status(GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT))
		return t.fail();

	return t.pass();
}

enum piglit_result
piglit_display(void)
{
	return PIGLIT_FAIL;
}

void
piglit_init(int argc, char **argv)
{
	bool pass = true;

	piglit_require_extension("GL_ARB_framebuffer_object");

	pass = incomplete_0_by_0_texture() && pass;
	pass = incomplete_0_by_0_renderbuffer() && pass;
	pass = invalid_3d_slice() && pass;
	pass = invalid_1d_array_layer() && pass;
	pass = invalid_2d_array_layer() && pass;
	pass = invalid_cube_array_layer() && pass;
	pass = delete_texture_of_current_fbo() && pass;
	pass = delete_renderbuffer_of_current_fbo() && pass;

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}