2 * Copyright © 2010 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
28 /** @file fbo-readpixels-small.c
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
34 * https://bugs.freedesktop.org/show_bug.cgi?id=25921
37 #include "piglit-util-gl.h"
42 PIGLIT_GL_TEST_CONFIG_BEGIN
44 config
.supports_gl_compat_version
= 10;
46 config
.window_visual
= PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_RGB
;
47 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
49 PIGLIT_GL_TEST_CONFIG_END
52 make_fbo(GLuint
*fbo
, GLuint
*tex
)
56 glGenTextures(1, tex
);
57 glBindTexture(GL_TEXTURE_2D
, *tex
);
58 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
,
60 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
62 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
63 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
65 glGenFramebuffersEXT(1, fbo
);
66 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, *fbo
);
67 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
68 GL_COLOR_ATTACHMENT0_EXT
,
72 if (!piglit_check_gl_error(GL_NO_ERROR
))
73 piglit_report_result(PIGLIT_FAIL
);
75 status
= glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT
);
76 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
77 fprintf(stderr
, "framebuffer incomplete (status = 0x%04x)\n",
84 probe(int x
, int y
, uint8_t *expected
, uint8_t *observed
)
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]);
104 GLboolean pass
= GL_TRUE
;
105 GLuint fbo
, tex
, pbo
;
107 static uint8_t green
[] = {0x00, 0xFF, 0x00, 0x00};
108 static uint8_t blue
[] = {0xFF, 0x00, 0x00, 0x00};
110 make_fbo(&fbo
, &tex
);
112 glClear(GL_COLOR_BUFFER_BIT
);
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);
119 glViewport(0, 0, 2, 2);
120 piglit_ortho_projection(2, 2, GL_FALSE
);
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);
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
);
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
);
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
);
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
);
153 glDeleteBuffersARB(1, &pbo
);
155 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, piglit_winsys_fbo
);
157 glViewport(0, 0, piglit_width
, piglit_height
);
158 piglit_ortho_projection(piglit_width
, piglit_height
, GL_FALSE
);
160 glBindTexture(GL_TEXTURE_2D
, tex
);
161 glEnable(GL_TEXTURE_2D
);
162 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
164 piglit_draw_rect_tex(0, 0, piglit_width
, piglit_height
,
166 glDisable(GL_TEXTURE_2D
);
168 glDeleteFramebuffersEXT(1, &fbo
);
169 glDeleteTextures(1, &tex
);
171 piglit_present_results();
173 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
176 void piglit_init(int argc
, char **argv
)
178 piglit_require_extension("GL_EXT_framebuffer_object");
179 piglit_require_extension("GL_ARB_pixel_buffer_object");
181 glDisable(GL_DITHER
);