2 * Copyright © 2009 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.c
30 * Tests that various formats of color renderbuffer get correct results from
31 * glReadPixels() versus glClear and immediate mode rendering.
34 #include "piglit-util-gl.h"
39 PIGLIT_GL_TEST_CONFIG_BEGIN
41 config
.supports_gl_compat_version
= 10;
43 config
.window_visual
= PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_RGB
;
44 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
46 PIGLIT_GL_TEST_CONFIG_END
49 test_with_format(GLenum internal_format
, GLenum format
,
50 float results_x
, float results_y
)
54 GLboolean pass
= GL_TRUE
;
55 int subrect_w
= BUF_WIDTH
/ 5;
56 int subrect_h
= BUF_HEIGHT
/ 5;
58 int rbits
, gbits
, bbits
, abits
;
60 glGenTextures(1, &tex
);
61 glBindTexture(GL_TEXTURE_2D
, tex
);
62 glTexImage2D(GL_TEXTURE_2D
, 0, internal_format
,
63 BUF_WIDTH
, BUF_HEIGHT
, 0,
64 format
, GL_UNSIGNED_BYTE
, NULL
);
66 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
67 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
69 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_RED_SIZE
,
71 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_GREEN_SIZE
,
73 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_BLUE_SIZE
,
75 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_ALPHA_SIZE
,
78 printf("testing with format %s, %s "
79 "(%d,%d,%d,%d rgba)\n",
80 piglit_get_gl_enum_name(internal_format
),
81 piglit_get_gl_enum_name(format
),
82 rbits
, gbits
, bbits
, abits
);
84 glGenFramebuffersEXT(1, &fb
);
85 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fb
);
86 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
87 GL_COLOR_ATTACHMENT0_EXT
,
91 if (!piglit_check_gl_error(GL_NO_ERROR
))
92 piglit_report_result(PIGLIT_FAIL
);
94 status
= glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT
);
95 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
96 fprintf(stderr
, "texture for internalformat %s. "
97 "format %s is framebuffer "
98 "incomplete (status = %s)\n",
99 piglit_get_gl_enum_name(internal_format
),
100 piglit_get_gl_enum_name(format
),
101 piglit_get_gl_enum_name(status
));
106 glViewport(0, 0, BUF_WIDTH
, BUF_HEIGHT
);
107 piglit_ortho_projection(BUF_WIDTH
, BUF_HEIGHT
, GL_FALSE
);
109 /* clear background to purple */
110 glClearColor(1.0, 0.0, 1.0, 0.0);
111 glClear(GL_COLOR_BUFFER_BIT
);
113 /* lower-left square: red */
114 glColor4f(1.0, 0.0, 0.0, 0.0);
115 piglit_draw_rect(subrect_w
* 1, subrect_h
* 1,
116 subrect_w
, subrect_h
);
118 /* lower-right square: green */
119 glColor4f(0.0, 1.0, 0.0, 0.0);
120 piglit_draw_rect(subrect_w
* 3, subrect_h
* 1,
121 subrect_w
, subrect_h
);
123 /* upper-left square: blue */
124 glColor4f(0.0, 0.0, 1.0, 0.0);
125 piglit_draw_rect(subrect_w
* 1, subrect_h
* 3,
126 subrect_w
, subrect_h
);
128 /* upper-right square: black */
129 glColor4f(0.0, 0.0, 0.0, 1.0);
130 piglit_draw_rect(subrect_w
* 3, subrect_h
* 3,
131 subrect_w
, subrect_h
);
133 for (y
= 0; y
< BUF_HEIGHT
; y
++) {
134 for (x
= 0; x
< BUF_WIDTH
; x
++) {
137 if (x
>= subrect_w
* 1 && x
< subrect_w
* 2 &&
138 y
>= subrect_h
* 1 && y
< subrect_h
* 2) {
143 } else if (x
>= subrect_w
* 3 && x
< subrect_w
* 4 &&
144 y
>= subrect_h
* 1 && y
< subrect_h
* 2) {
149 } else if (x
>= subrect_w
* 1 && x
< subrect_w
* 2 &&
150 y
>= subrect_h
* 3 && y
< subrect_h
* 4) {
155 } else if (x
>= subrect_w
* 3 && x
< subrect_w
* 4 &&
156 y
>= subrect_h
* 3 && y
< subrect_h
* 4) {
167 pass
&= piglit_probe_pixel_rgb(x
, y
, expected
);
171 /* display the texture by drawing a quad */
172 glViewport(0, 0, piglit_width
, piglit_height
);
173 piglit_ortho_projection(piglit_width
, piglit_height
, GL_FALSE
);
175 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, piglit_winsys_fbo
);
176 glEnable(GL_TEXTURE_2D
);
177 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
179 piglit_draw_rect_tex(results_x
, results_y
, BUF_WIDTH
, BUF_HEIGHT
,
181 glDisable(GL_TEXTURE_2D
);
184 glDeleteFramebuffersEXT(1, &fb
);
185 glDeleteTextures(1, &tex
);
192 GLboolean pass
= GL_TRUE
;
194 glClearColor(1.0, 1.0, 1.0, 1.0);
195 glClear(GL_COLOR_BUFFER_BIT
);
197 pass
&= test_with_format(GL_RGBA8
, GL_BGRA
,
199 pass
&= test_with_format(GL_RGB5
, GL_RGB
,
201 pass
&= test_with_format(GL_RGBA4
, GL_BGRA
,
202 0, (BUF_HEIGHT
+ 1) * 2);
203 pass
&= test_with_format(GL_RGB5_A1
, GL_BGRA
,
204 0, (BUF_HEIGHT
+ 1) * 3);
205 piglit_present_results();
207 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
210 void piglit_init(int argc
, char **argv
)
212 piglit_require_extension("GL_EXT_framebuffer_object");