summaryrefslogtreecommitdiff
path: root/tests/fbo/fbo-blit.c
blob: efb5d48e6c717f0020fb152fbcc8193a869c3a56 (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
/*
 * Copyright © 2010 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.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *    Brian Paul
 */

/** @file fbo-blit.c
 *
 * Tests EXT_framebuffer_blit with various combinations of window system and
 * FBO objects.  Because FBOs are generally stored inverted relative to
 * window system frambuffers, this could catch flipping failures in blit paths.
 *
 * Brian added testing of glCopy/Read/DrawPixels().
 */

#include "piglit-util.h"

int piglit_width = 150;
int piglit_height = 150;
int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;
#define PAD 10
#define SIZE 20

/* size of texture/renderbuffer (power of two) */
#define FBO_SIZE 64


enum copy_method {
	COPY_PIXELS,
	READ_DRAW_PIXELS,
	BLIT_PIXELS
};


static const char *
method_name(enum copy_method method)
{
	switch (method) {
	case COPY_PIXELS:
		return "glCopyPixels";
	case READ_DRAW_PIXELS:
		return "glReadPixels + glDrawPixels";
	case BLIT_PIXELS:
		return "glBlitFramebuffer";
	default:
		assert(0 && "bad copy method");
		return NULL;
	}
}
			

static GLuint
make_fbo(int w, int h)
{
	GLuint tex;
	GLuint fb;
 	GLenum status;

	glGenFramebuffersEXT(1, &fb);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
		     w, h, 0,
		     GL_RGBA, GL_UNSIGNED_BYTE, NULL);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
				  GL_COLOR_ATTACHMENT0_EXT,
				  GL_TEXTURE_2D,
				  tex,
				  0);
	assert(glGetError() == 0);

	status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
	if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
		fprintf(stderr, "fbo incomplete (status = 0x%04x)\n", status);
		piglit_report_result(PIGLIT_SKIP);
	}

	return fb;
}

static void
draw_color_rect(int x, int y, int w, int h)
{
	int x1 = x;
	int x2 = x + w / 2;
	int y1 = y;
	int y2 = y + h / 2;

	glColor4f(1.0, 0.0, 0.0, 0.0);
	piglit_draw_rect(x1, y1, w / 2, h / 2);
	glColor4f(0.0, 1.0, 0.0, 0.0);
	piglit_draw_rect(x2, y1, w / 2, h / 2);
	glColor4f(0.0, 0.0, 1.0, 0.0);
	piglit_draw_rect(x1, y2, w / 2, h / 2);
	glColor4f(1.0, 1.0, 1.0, 0.0);
	piglit_draw_rect(x2, y2, w / 2, h / 2);
}

static GLboolean
verify_color_rect(int start_x, int start_y, int w, int h)
{
	float red[] =   {1, 0, 0, 0};
	float green[] = {0, 1, 0, 0};
	float blue[] =  {0, 0, 1, 0};
	float white[] = {1, 1, 1, 0};
	int x, y;

	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			float *expected;

			if ((y < h / 2) && (x < w / 2))
				expected = red;
			else if (y < h / 2)
				expected = green;
			else if (x < w / 2)
				expected = blue;
			else
				expected = white;

			if (!piglit_probe_pixel_rgb(start_x + x, start_y + y,
						    expected))
				return GL_FALSE;
		}
	}

	return GL_TRUE;
}


static void
copy(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
     GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
     enum copy_method method)
{
	if (method == COPY_PIXELS) {
		GLsizei srcW = srcX1 - srcX0, srcH = srcY1 - srcY0;
		GLsizei dstW = dstX1 - dstX0, dstH = dstY1 - dstY0;
		glPixelZoom((float) dstW / (float) srcW,
			    (float) dstH / (float) srcH);
		glWindowPos2i(dstX0, dstY0);
		glCopyPixels(srcX0, srcY0, srcW, srcH, GL_COLOR);
	}
	else if (method == READ_DRAW_PIXELS) {
		GLsizei srcW = srcX1 - srcX0, srcH = srcY1 - srcY0;
		GLsizei dstW = dstX1 - dstX0, dstH = dstY1 - dstY0;
		void *buf = malloc(srcW * srcH * 4);
		glReadPixels(srcX0, srcY0, srcW, srcH,
			     GL_RGBA, GL_UNSIGNED_BYTE, buf);
		glPixelZoom((float) dstW / (float) srcW,
			    (float) dstH / (float) srcH);
		glWindowPos2i(dstX0, dstY0);
		glDrawPixels(srcW, srcH, GL_RGBA, GL_UNSIGNED_BYTE, buf);
		free(buf);
	}
	else if (method == BLIT_PIXELS) {
		glBlitFramebufferEXT(srcX0, srcY0, srcX1, srcY1,
				     dstX0, dstY0, dstX1, dstY1,
				     GL_COLOR_BUFFER_BIT, GL_NEAREST);
	}
	else {
		assert(0 && "invalid copy method");
	}
}


static GLboolean
run_test(enum copy_method method)
{
	GLboolean pass = GL_TRUE;
	GLuint fbo;
	int fbo_width = FBO_SIZE;
	int fbo_height = FBO_SIZE;
	int x0 = PAD;
	int y0 = PAD;
	int y1 = PAD * 2 + SIZE;
	int y2 = PAD * 3 + SIZE * 2;

	glViewport(0, 0, piglit_width, piglit_height);
	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

	glClearColor(0.5, 0.5, 0.5, 0.5);
	glClear(GL_COLOR_BUFFER_BIT);

	/* Draw the color rect in the window system window */
	draw_color_rect(x0, y0, SIZE, SIZE);

	fbo = make_fbo(fbo_width, fbo_height);

	glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fbo);
	glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
	glViewport(0, 0, fbo_width, fbo_height);
	piglit_ortho_projection(fbo_width, fbo_height, GL_FALSE);
	glClearColor(1.0, 0.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);

	/* Draw the color rect in the FBO */
	draw_color_rect(x0, y0, SIZE, SIZE);

	/* Now that we have correct samples, blit things around.
	 * FBO(bottom) -> WIN(middle)
	 */
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
	glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, fbo);
 	copy(x0, y0, x0 + SIZE, y0 + SIZE,
 	     x0, y1, x0 + SIZE, y1 + SIZE,
 	     method);

	/* WIN(bottom) -> FBO(middle) */
	glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fbo);
	glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
 	copy(x0, y0, x0 + SIZE, y0 + SIZE,
 	     x0, y1, x0 + SIZE, y1 + SIZE,
 	     method);

	/* FBO(middle) -> WIN(top) back to verify WIN -> FBO */
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
	glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, fbo);
 	copy(x0, y1, x0 + SIZE, y1 + SIZE,
 	     x0, y2, x0 + SIZE, y2 + SIZE,
 	     method);

	glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
	glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);

	pass = verify_color_rect(PAD, y0, SIZE, SIZE) && pass;
	pass = verify_color_rect(PAD, y1, SIZE, SIZE) && pass;
	pass = verify_color_rect(PAD, y2, SIZE, SIZE) && pass;

	if (!pass) {
		printf("fbo-blit: failure for %s\n", method_name(method));
	}

	glutSwapBuffers();

	return pass;
}


enum piglit_result
piglit_display(void)
{
	GLboolean pass = GL_TRUE;

	pass = pass && run_test(COPY_PIXELS);
	pass = pass && run_test(READ_DRAW_PIXELS);
	pass = pass && run_test(BLIT_PIXELS);

	return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
}


void
piglit_init(int argc, char **argv)
{
	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

	piglit_require_extension("GL_EXT_framebuffer_object");
	piglit_require_extension("GL_EXT_framebuffer_blit");
}