2 * Copyright (c) 2012 VMware, Inc.
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 * Test glViewport w/ FBOs.
26 * In Mesa, on-screen windows and user-created FBOs are stored differently
27 * (inverted). Make sure viewports are handled properly.
28 * Draw a test pattern (with many viewports) into the window, then draw the
29 * same thing into an FBO. Compare the images. They should be the same.
32 #include "piglit-util-gl.h"
34 PIGLIT_GL_TEST_CONFIG_BEGIN
36 config
.supports_gl_compat_version
= 10;
38 config
.window_width
= 500;
39 config
.window_height
= 500;
40 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
;
41 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
43 PIGLIT_GL_TEST_CONFIG_END
46 * Draw full-viewport quads in a bunch of viewports which tile the window.
47 * Note that viewports extend beyond the edges of the window too.
52 int vx
, vy
, vw
= 200, vh
= 200;
54 glClear(GL_COLOR_BUFFER_BIT
);
56 /* loop over viewports */
58 for (vy
= -50; vy
< piglit_height
; vy
+= vh
+10) {
59 for (vx
= -30; vx
< piglit_width
; vx
+= vw
+10, ++i
) {
60 glViewport(vx
, vy
, vw
, vh
);
64 glColor3f((i
% 4) / 3.0, ((i
/ 4) % 4) / 3.0, 0.0);
70 glColor3f((i
% 4) / 3.0, ((i
/ 4) % 4) / 3.0, 0.333);
76 glColor3f((i
% 4) / 3.0, ((i
/ 4) % 4) / 3.0, 0.666);
82 glColor3f((i
% 4) / 3.0, ((i
/ 4) % 4) / 3.0, 1.0);
97 GLubyte
*win_image
, *fbo_image
;
101 win_image
= (GLubyte
*) malloc(piglit_width
* piglit_height
* 3);
102 fbo_image
= (GLubyte
*) malloc(piglit_width
* piglit_height
* 3);
104 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
105 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
107 glGenFramebuffers(1, &fbo
);
108 glBindFramebuffer(GL_FRAMEBUFFER
, fbo
);
109 glGenRenderbuffers(1, &rb
);
110 glBindRenderbuffer(GL_RENDERBUFFER
, rb
);
111 glRenderbufferStorage(GL_RENDERBUFFER
, GL_RGBA
,
112 piglit_width
, piglit_height
);
113 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
114 GL_RENDERBUFFER
, rb
);
116 if (!piglit_check_gl_error(GL_NO_ERROR
))
117 piglit_report_result(PIGLIT_FAIL
);
119 assert(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
) ==
120 GL_FRAMEBUFFER_COMPLETE_EXT
);
122 /* draw reference image in the window */
123 glBindFramebuffer(GL_FRAMEBUFFER
, piglit_winsys_fbo
);
125 glReadPixels(0, 0, piglit_width
, piglit_height
,
126 GL_RGB
, GL_UNSIGNED_BYTE
, win_image
);
128 /* draw test image in fbo */
129 glBindFramebuffer(GL_FRAMEBUFFER
, fbo
);
130 glReadBuffer(GL_COLOR_ATTACHMENT0
);
132 glReadPixels(0, 0, piglit_width
, piglit_height
,
133 GL_RGB
, GL_UNSIGNED_BYTE
, fbo_image
);
136 if (memcmp(win_image
, fbo_image
, piglit_width
* piglit_height
* 3)) {
137 #if 0 /* helpful debug code */
139 for (i
= k
= 0; i
< piglit_width
* piglit_height
* 3; i
++) {
140 if (win_image
[i
] != fbo_image
[i
] && k
++ < 40)
141 printf("%d: %d vs. %d\n",
142 i
, win_image
[i
], fbo_image
[i
]);
145 printf("Image comparison failed!\n");
148 else if (!piglit_automatic
) {
149 printf("Image comparison passed.\n");
152 glBindFramebuffer(GL_FRAMEBUFFER
, piglit_winsys_fbo
);
154 #if 0 /* for debug/compare (alternate displaying Window vs. FBO image) */
158 for (i
= 0; i
< 10; i
++) {
159 GLubyte
*image
= (i
& 1) ? fbo_image
: win_image
;
160 printf("Showing %s image\n", (i
& 1) ? "FBO" : "window");
161 glDrawPixels(piglit_width
, piglit_height
,
162 GL_RGB
, GL_UNSIGNED_BYTE
, image
);
163 piglit_present_results();
169 piglit_present_results();
171 glDeleteRenderbuffers(1, &rb
);
172 glDeleteFramebuffers(1, &fbo
);
176 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
181 piglit_init(int argc
, char **argv
)
183 piglit_require_extension("GL_ARB_framebuffer_object");
184 glClearColor(0.2, 0.2, 0.2, 0.0);