2 * Copyright © 2013 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
25 /** @file readpixels.c
27 * Simple touch test of glReadPixels() using GL_PACK_INVERT_MESA, to a
28 * PBO or user memory, with format conversions or (hopefully) not.
31 #include "piglit-util-gl.h"
33 PIGLIT_GL_TEST_CONFIG_BEGIN
35 config
.supports_gl_compat_version
= 10;
37 config
.window_visual
= PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_RGBA
;
39 PIGLIT_GL_TEST_CONFIG_END
41 /* Size of the RGBW rect on the screen: at 6 pixels, the unorm failure
42 * result fits in an 80-column terminal.
48 check_unorm(const uint8_t *data
, const char *name
)
50 static const uint8_t colors
[4][4] = {
51 {0xff, 0x00, 0x00, 0x00},
52 {0xff, 0xff, 0xff, 0xff},
53 {0x00, 0x00, 0xff, 0x00},
54 {0x00, 0xff, 0x00, 0x00}
58 for (y
= 0; y
< H
; y
++) {
59 for (x
= 0; x
< W
; x
++) {
60 int i
= (y
>= H
/ 2) * 2 + (x
>= W
/ 2);
61 const uint8_t *expected
= &colors
[i
][0];
62 const uint8_t *observed
= &data
[(y
* W
+ x
) * 4];
64 if (expected
[0] != observed
[0] ||
65 expected
[1] != observed
[1] ||
66 expected
[2] != observed
[2] ||
67 expected
[3] != observed
[3]) {
69 "%s pixel value read at (%d, %d)\n", name
, x
, y
);
71 " Expected: b = 0x%02x g = 0x%02x r = 0x%02x a = 0x%02x\n",
72 expected
[0], expected
[1], expected
[2], expected
[3]);
74 " Observed: b = 0x%02x g = 0x%02x r = 0x%02x a = 0x%02x\n",
75 observed
[0], observed
[1], observed
[2], observed
[3]);
77 for (y
= 0; y
< H
; y
++) {
78 for (x
= 0; x
< W
; x
++) {
79 observed
= &data
[(y
* W
+ x
) * 4];
81 "b = 0x%02x g = 0x%02x r = 0x%02x a = 0x%02x\n",
82 observed
[0], observed
[1], observed
[2], observed
[3]);
86 piglit_report_subtest_result(PIGLIT_FAIL
,
93 piglit_report_subtest_result(PIGLIT_PASS
, "%s", name
);
98 check_float(const float *data
, const char *name
)
100 static const float colors
[4][4] = {
108 for (y
= 0; y
< H
; y
++) {
109 for (x
= 0; x
< W
; x
++) {
110 int i
= (y
>= H
/ 2) * 2 + (x
>= W
/ 2);
111 const float *expected
= colors
[i
];
112 const float *result
= &data
[(y
* W
+ x
) * 4];
114 if (memcmp(result
, expected
, sizeof(float[4])) != 0) {
116 "%s pixel value read at (%d, %d):\n"
117 " was %f, %f, %f, %f\n"
118 " expected %f, %f, %f, %f\n",
128 fprintf(stderr
, "\n");
130 piglit_report_subtest_result(PIGLIT_FAIL
,
137 piglit_report_subtest_result(PIGLIT_PASS
, "%s", name
);
146 uint8_t bgra_unorm
[W
* H
* 4];
147 float rgba_float
[W
* H
* 4];
150 glGenBuffers(1, &pbo
);
151 glBindBuffer(GL_PIXEL_PACK_BUFFER
, pbo
);
152 glBufferData(GL_PIXEL_PACK_BUFFER
, W
* H
* sizeof(float[4]),
153 NULL
, GL_STREAM_READ_ARB
);
154 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
156 glClearColor(0.5, 0.5, 0.5, 0.5);
157 glClear(GL_COLOR_BUFFER_BIT
);
159 piglit_ortho_projection(piglit_width
, piglit_height
, false);
161 glColor4f(1, 0, 0, 0);
162 piglit_draw_rect(5, 5,
164 glColor4f(0, 1, 0, 0);
165 piglit_draw_rect(5 + W
/ 2, 5,
167 glColor4f(0, 0, 1, 0);
168 piglit_draw_rect(5, 5 + H
/ 2,
170 glColor4f(1, 1, 1, 1);
171 piglit_draw_rect(5 + W
/ 2, 5 + H
/ 2,
174 glPixelStorei(GL_PACK_INVERT_MESA
, 1);
176 glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0);
177 glReadPixels(5, 5, W
, H
, GL_BGRA
, GL_UNSIGNED_BYTE
, bgra_unorm
);
178 if (!check_unorm(bgra_unorm
, "non-PBO unorm BGRA"))
181 glBindBuffer(GL_PIXEL_PACK_BUFFER
, pbo
);
182 glReadPixels(5, 5, W
, H
, GL_BGRA
, GL_UNSIGNED_BYTE
, NULL
);
183 map
= glMapBuffer(GL_PIXEL_PACK_BUFFER
, GL_READ_ONLY
);
184 if (!check_unorm(map
, "PBO unorm BGRA"))
186 glUnmapBuffer(GL_PIXEL_PACK_BUFFER
);
188 glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0);
189 glReadPixels(5, 5, W
, H
, GL_RGBA
, GL_FLOAT
, rgba_float
);
190 if (!check_float(rgba_float
, "non-PBO float RGBA"))
193 glBindBuffer(GL_PIXEL_PACK_BUFFER
, pbo
);
194 glReadPixels(5, 5, W
, H
, GL_RGBA
, GL_FLOAT
, NULL
);
195 map
= glMapBuffer(GL_PIXEL_PACK_BUFFER
, GL_READ_ONLY
);
196 if (!check_float(map
, "PBO float RGBA"))
198 glUnmapBuffer(GL_PIXEL_PACK_BUFFER
);
200 piglit_present_results();
202 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
205 void piglit_init(int argc
, char **argv
)
207 piglit_require_extension("GL_ARB_pixel_buffer_object");
208 piglit_require_extension("GL_MESA_pack_invert");
209 piglit_require_extension("GL_EXT_bgra");
211 const char * names
[] = {
212 "non-PBO unorm BGRA",
214 "non-PBO float RGBA",
218 piglit_register_subtests(names
);