1 /*
2  * Copyright © 2010 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27 
28 /** @file fbo-readpixels-small.c
29  *
30  * Tests that PBO blit readpixels on a 2x2 FBO works correctly.  Based
31  * on a description of a failure in clutter and figuring out the associated
32  * bug.
33  *
34  * https://bugs.freedesktop.org/show_bug.cgi?id=25921
35  */
36 
37 #include "piglit-util-gl.h"
38 
39 #define BUF_WIDTH 8
40 #define BUF_HEIGHT 8
41 
42 PIGLIT_GL_TEST_CONFIG_BEGIN
43 
44 	config.supports_gl_compat_version = 10;
45 
46 	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB;
47 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
48 
49 PIGLIT_GL_TEST_CONFIG_END
50 
51 static void
make_fbo(GLuint * fbo,GLuint * tex)52 make_fbo(GLuint *fbo, GLuint *tex)
53 {
54 	GLenum status;
55 
56 	glGenTextures(1, tex);
57 	glBindTexture(GL_TEXTURE_2D, *tex);
58 	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
59 		     2, 2, 0,
60 		     GL_RGBA, GL_UNSIGNED_BYTE, NULL);
61 
62 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
63 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
64 
65 	glGenFramebuffersEXT(1, fbo);
66 	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, *fbo);
67 	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
68 				  GL_COLOR_ATTACHMENT0_EXT,
69 				  GL_TEXTURE_2D,
70 				  *tex,
71 				  0);
72 	if (!piglit_check_gl_error(GL_NO_ERROR))
73 		piglit_report_result(PIGLIT_FAIL);
74 
75 	status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
76 	if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
77 		fprintf(stderr, "framebuffer incomplete (status = 0x%04x)\n",
78 			status);
79 		abort();
80 	}
81 }
82 
83 static GLboolean
probe(int x,int y,uint8_t * expected,uint8_t * observed)84 probe(int x, int y, uint8_t *expected, uint8_t *observed)
85 {
86 	if (expected[0] != observed[0] ||
87 	    expected[1] != observed[1] ||
88 	    expected[2] != observed[2]) {
89 		printf("Probe color at (%i,%i)\n", x, y);
90 		printf("  Expected: b = 0x%02x  g = 0x%02x  r = 0x%02x  a = 0x%02x\n",
91 				expected[0], expected[1], expected[2], expected[3]);
92 		printf("  Observed: b = 0x%02x  g = 0x%02x  r = 0x%02x  a = 0x%02x\n",
93 				observed[0], observed[1], observed[2], observed[3]);
94 
95 		return GL_FALSE;
96 	} else {
97 		return GL_TRUE;
98 	}
99 }
100 
101 enum piglit_result
piglit_display(void)102 piglit_display(void)
103 {
104 	GLboolean pass = GL_TRUE;
105 	GLuint fbo, tex, pbo;
106 	uint8_t *addr;
107 	static uint8_t green[] = {0x00, 0xFF, 0x00, 0x00};
108 	static uint8_t blue[]  = {0xFF, 0x00, 0x00, 0x00};
109 
110 	make_fbo(&fbo, &tex);
111 
112 	glClear(GL_COLOR_BUFFER_BIT);
113 
114 	glGenBuffersARB(1, &pbo);
115 	glBindBufferARB(GL_PIXEL_PACK_BUFFER, pbo);
116 	glBufferDataARB(GL_PIXEL_PACK_BUFFER, 4 * 4, NULL, GL_STREAM_DRAW_ARB);
117 	glPixelStorei(GL_PACK_ALIGNMENT, 1);
118 
119 	glViewport(0, 0, 2, 2);
120 	piglit_ortho_projection(2, 2, GL_FALSE);
121 
122 	/* bottom: green.  top: blue. */
123 	glColor4f(0.0, 1.0, 0.0, 0.0);
124 	piglit_draw_rect(0, 0, 2, 1);
125 	glColor4f(0.0, 0.0, 1.0, 0.0);
126 	piglit_draw_rect(0, 1, 2, 1);
127 
128 	/* Read the whole buffer. */
129 	glReadPixels(0, 0, 2, 2,
130 		     GL_BGRA, GL_UNSIGNED_BYTE, (void *)(uintptr_t)0);
131 	addr = glMapBufferARB(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY_ARB);
132 
133 	pass &= probe(0, 0, green, addr);
134 	pass &= probe(1, 0, green, addr + 4);
135 	pass &= probe(0, 1, blue,  addr + 8);
136 	pass &= probe(1, 1, blue,  addr + 12);
137 	glUnmapBufferARB(GL_PIXEL_PACK_BUFFER);
138 
139 	/* Read with an offset. */
140 	glReadPixels(1, 0, 1, 1,
141 		     GL_BGRA, GL_UNSIGNED_BYTE, (void *)(uintptr_t)4);
142 	addr = glMapBufferARB(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY_ARB);
143 	pass &= probe(1, 0, green, addr + 4);
144 	glUnmapBufferARB(GL_PIXEL_PACK_BUFFER);
145 
146 	/* Read with an offset. */
147 	glReadPixels(1, 1, 1, 1,
148 		     GL_BGRA, GL_UNSIGNED_BYTE, (void *)(uintptr_t)4);
149 	addr = glMapBufferARB(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY_ARB);
150 	pass &= probe(1, 1, blue, addr + 4);
151 	glUnmapBufferARB(GL_PIXEL_PACK_BUFFER);
152 
153 	glDeleteBuffersARB(1, &pbo);
154 
155 	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, piglit_winsys_fbo);
156 
157 	glViewport(0, 0, piglit_width, piglit_height);
158 	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
159 
160 	glBindTexture(GL_TEXTURE_2D, tex);
161 	glEnable(GL_TEXTURE_2D);
162 	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
163 
164 	piglit_draw_rect_tex(0, 0, piglit_width, piglit_height,
165 			     0, 0, 1, 1);
166 	glDisable(GL_TEXTURE_2D);
167 
168 	glDeleteFramebuffersEXT(1, &fbo);
169 	glDeleteTextures(1, &tex);
170 
171 	piglit_present_results();
172 
173 	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
174 }
175 
piglit_init(int argc,char ** argv)176 void piglit_init(int argc, char **argv)
177 {
178 	piglit_require_extension("GL_EXT_framebuffer_object");
179 	piglit_require_extension("GL_ARB_pixel_buffer_object");
180 
181 	glDisable(GL_DITHER);
182 }
183