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.h"
38 int piglit_width
= 100;
39 int piglit_height
= 200;
40 int piglit_window_mode
= GLUT_DOUBLE
| GLUT_RGB
;
43 test_with_format(GLenum internal_format
, GLenum format
,
44 float results_x
, float results_y
)
48 GLboolean pass
= GL_TRUE
;
49 int subrect_w
= BUF_WIDTH
/ 5;
50 int subrect_h
= BUF_HEIGHT
/ 5;
52 int rbits
, gbits
, bbits
, abits
;
54 glGenTextures(1, &tex
);
55 glBindTexture(GL_TEXTURE_2D
, tex
);
56 glTexImage2D(GL_TEXTURE_2D
, 0, internal_format
,
57 BUF_WIDTH
, BUF_HEIGHT
, 0,
58 format
, GL_UNSIGNED_BYTE
, NULL
);
60 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
61 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
63 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_RED_SIZE
,
65 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_GREEN_SIZE
,
67 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_BLUE_SIZE
,
69 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_ALPHA_SIZE
,
72 printf("testing with format 0x%04x, 0x%04x "
73 "(%d,%d,%d,%d rgba)\n",
74 internal_format
, format
,
75 rbits
, gbits
, bbits
, abits
);
77 glGenFramebuffersEXT(1, &fb
);
78 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fb
);
79 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
80 GL_COLOR_ATTACHMENT0_EXT
,
84 assert(glGetError() == 0);
86 status
= glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT
);
87 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
88 fprintf(stderr
, "texture for internalformat 0x%04x. "
89 "format 0x%04x is framebuffer "
90 "incomplete (status = 0x%04x)\n",
91 internal_format
, format
, status
);
96 glViewport(0, 0, BUF_WIDTH
, BUF_HEIGHT
);
97 piglit_ortho_projection(BUF_WIDTH
, BUF_HEIGHT
, GL_FALSE
);
99 /* clear background to purple */
100 glClearColor(1.0, 0.0, 1.0, 0.0);
101 glClear(GL_COLOR_BUFFER_BIT
);
103 /* lower-left square: red */
104 glColor4f(1.0, 0.0, 0.0, 0.0);
105 piglit_draw_rect(subrect_w
* 1, subrect_h
* 1,
106 subrect_w
, subrect_h
);
108 /* lower-right square: green */
109 glColor4f(0.0, 1.0, 0.0, 0.0);
110 piglit_draw_rect(subrect_w
* 3, subrect_h
* 1,
111 subrect_w
, subrect_h
);
113 /* upper-left square: blue */
114 glColor4f(0.0, 0.0, 1.0, 0.0);
115 piglit_draw_rect(subrect_w
* 1, subrect_h
* 3,
116 subrect_w
, subrect_h
);
118 /* upper-right square: black */
119 glColor4f(0.0, 0.0, 0.0, 1.0);
120 piglit_draw_rect(subrect_w
* 3, subrect_h
* 3,
121 subrect_w
, subrect_h
);
123 for (y
= 0; y
< BUF_HEIGHT
; y
++) {
124 for (x
= 0; x
< BUF_WIDTH
; x
++) {
127 if (x
>= subrect_w
* 1 && x
< subrect_w
* 2 &&
128 y
>= subrect_h
* 1 && y
< subrect_h
* 2) {
133 } else if (x
>= subrect_w
* 3 && x
< subrect_w
* 4 &&
134 y
>= subrect_h
* 1 && y
< subrect_h
* 2) {
139 } else if (x
>= subrect_w
* 1 && x
< subrect_w
* 2 &&
140 y
>= subrect_h
* 3 && y
< subrect_h
* 4) {
145 } else if (x
>= subrect_w
* 3 && x
< subrect_w
* 4 &&
146 y
>= subrect_h
* 3 && y
< subrect_h
* 4) {
157 pass
&= piglit_probe_pixel_rgb(x
, y
, expected
);
161 /* display the texture by drawing a quad */
162 glViewport(0, 0, piglit_width
, piglit_height
);
163 piglit_ortho_projection(piglit_width
, piglit_height
, GL_FALSE
);
165 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
166 glEnable(GL_TEXTURE_2D
);
167 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
169 piglit_draw_rect_tex(results_x
, results_y
, BUF_WIDTH
, BUF_HEIGHT
,
171 glDisable(GL_TEXTURE_2D
);
174 glDeleteFramebuffersEXT(1, &fb
);
175 glDeleteTextures(1, &tex
);
182 GLboolean pass
= GL_TRUE
;
184 glClearColor(1.0, 1.0, 1.0, 1.0);
185 glClear(GL_COLOR_BUFFER_BIT
);
187 pass
&= test_with_format(GL_RGBA8
, GL_BGRA
,
189 pass
&= test_with_format(GL_RGB5
, GL_RGB
,
191 pass
&= test_with_format(GL_RGBA4
, GL_BGRA
,
192 0, (BUF_HEIGHT
+ 1) * 2);
193 pass
&= test_with_format(GL_RGB5_A1
, GL_BGRA
,
194 0, (BUF_HEIGHT
+ 1) * 3);
197 return pass
? PIGLIT_SUCCESS
: PIGLIT_FAILURE
;
200 void piglit_init(int argc
, char **argv
)
202 piglit_require_extension("GL_EXT_framebuffer_object");