Add more structure constructor tests.
[piglit/hramrach.git] / tests / fbo / fbo-blit-d24s8.c
blobdca668fe7eba3dfa5e91809e3dafaede85f6c17a
1 /*
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
13 * Software.
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
21 * IN THE SOFTWARE.
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Brian Paul
26 * Marek Olšák <maraeo@gmail.com>
29 /** @file fbo-blit-d24s8.c
31 * Tests EXT_framebuffer_blit with various combinations of window system and
32 * FBO objects. Because FBOs are generally stored upside down relative to
33 * window system frambuffers, this could catch flipping failures in blit paths.
34 * The FBOs in this test are of the D24S8 format.
36 * See also fbo-blit.c
39 #include "piglit-util.h"
41 int piglit_width = 150;
42 int piglit_height = 150;
43 int piglit_window_mode = GLUT_RGBA | GLUT_DOUBLE | GLUT_STENCIL;
44 #define PAD 10
45 #define SIZE 20
47 /* size of texture/renderbuffer (power of two) */
48 #define FBO_SIZE 64
51 static GLuint
52 make_fbo(int w, int h)
54 GLuint tex;
55 GLuint fb;
56 GLenum status;
58 glGenFramebuffersEXT(1, &fb);
59 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
61 glGenTextures(1, &tex);
62 glBindTexture(GL_TEXTURE_2D, tex);
63 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8_EXT,
64 w, h, 0,
65 GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, NULL);
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
68 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
70 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
71 GL_DEPTH_ATTACHMENT_EXT,
72 GL_TEXTURE_2D,
73 tex,
74 0);
75 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
76 GL_STENCIL_ATTACHMENT_EXT,
77 GL_TEXTURE_2D,
78 tex,
79 0);
81 glDrawBuffer(GL_NONE);
82 glReadBuffer(GL_NONE);
83 assert(glGetError() == 0);
85 status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
86 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
87 fprintf(stderr, "fbo incomplete (status = 0x%04x)\n", status);
88 piglit_report_result(PIGLIT_SKIP);
91 return fb;
94 static void
95 draw_depth_rect(int x, int y, int w, int h)
97 int x1 = x;
98 int x2 = x + w / 2;
99 int y1 = y;
100 int y2 = y + h / 2;
102 glDepthRange(0, 0);
103 piglit_draw_rect(x1, y1, w / 2, h / 2);
104 glDepthRange(0.3, 0.3);
105 piglit_draw_rect(x2, y1, w / 2, h / 2);
106 glDepthRange(0.6, 0.6);
107 piglit_draw_rect(x1, y2, w / 2, h / 2);
108 glDepthRange(1, 1);
109 piglit_draw_rect(x2, y2, w / 2, h / 2);
110 glDepthRange(0, 1);
113 static GLboolean
114 verify_depth_rect(int start_x, int start_y, int w, int h)
116 float zero = 0;
117 float darkgrey = 0.3;
118 float grey = 0.6;
119 float one = 1;
120 int x, y;
122 for (y = 0; y < h; y++) {
123 for (x = 0; x < w; x++) {
124 float expected;
126 if ((y < h / 2) && (x < w / 2))
127 expected = zero;
128 else if (y < h / 2)
129 expected = darkgrey;
130 else if (x < w / 2)
131 expected = grey;
132 else
133 expected = one;
135 if (!piglit_probe_pixel_depth(start_x + x, start_y + y,
136 expected))
137 return GL_FALSE;
138 //fprintf(stderr, "Match: %f\n", expected);
142 return GL_TRUE;
146 static void
147 copy(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
148 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
149 GLenum mask)
151 glBlitFramebufferEXT(srcX0, srcY0, srcX1, srcY1,
152 dstX0, dstY0, dstX1, dstY1,
153 mask, GL_NEAREST);
157 static GLboolean
158 run_test(void)
160 GLboolean pass = GL_TRUE;
161 GLuint fbo;
162 int fbo_width = FBO_SIZE;
163 int fbo_height = FBO_SIZE;
164 int x0 = PAD;
165 int y0 = PAD;
166 int y1 = PAD * 2 + SIZE;
167 int y2 = PAD * 3 + SIZE * 2;
169 glViewport(0, 0, piglit_width, piglit_height);
170 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
172 glEnable(GL_DEPTH_TEST);
173 glDepthFunc(GL_ALWAYS);
174 glDepthMask(GL_TRUE);
176 glClearColor(0.5, 0.5, 0.5, 0.5);
177 glClearDepth(0.12345);
178 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
180 /* Draw the color rect in the window system window */
181 draw_depth_rect(x0, y0, SIZE, SIZE);
183 fbo = make_fbo(fbo_width, fbo_height);
185 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fbo);
186 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
187 glViewport(0, 0, fbo_width, fbo_height);
188 piglit_ortho_projection(fbo_width, fbo_height, GL_FALSE);
189 glClearDepth(0.54321);
190 glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
192 /* Draw the color rect in the FBO */
193 draw_depth_rect(x0, y0, SIZE, SIZE);
195 /* Now that we have correct samples, blit things around.
196 * FBO(bottom) -> WIN(middle)
198 * Also blit with stencil to exercise this path.
199 * Not that we need it for this test.
201 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
202 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fbo);
203 copy(x0, y0, x0 + SIZE, y0 + SIZE,
204 x0, y1, x0 + SIZE, y1 + SIZE,
205 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
207 /* WIN(bottom) -> FBO(middle) */
208 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fbo);
209 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
210 copy(x0, y0, x0 + SIZE, y0 + SIZE,
211 x0, y1, x0 + SIZE, y1 + SIZE,
212 GL_DEPTH_BUFFER_BIT);
214 /* FBO(middle) -> WIN(top) back to verify WIN -> FBO */
215 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
216 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fbo);
217 copy(x0, y1, x0 + SIZE, y1 + SIZE,
218 x0, y2, x0 + SIZE, y2 + SIZE,
219 GL_DEPTH_BUFFER_BIT);
221 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
222 assert(glGetError() == 0);
224 printf("Verify 1\n");
225 pass = verify_depth_rect(PAD, y0, SIZE, SIZE) && pass;
226 printf("Verify 2\n");
227 pass = verify_depth_rect(PAD, y1, SIZE, SIZE) && pass;
228 printf("Verify 3\n");
229 pass = verify_depth_rect(PAD, y2, SIZE, SIZE) && pass;
230 printf("Verify 4 (FBO)\n");
231 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
232 pass = verify_depth_rect(PAD, y0, SIZE, SIZE) && pass;
233 printf("Verify 5 (FBO)\n");
234 pass = verify_depth_rect(PAD, y1, SIZE, SIZE) && pass;
235 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
236 assert(glGetError() == 0);
238 glutSwapBuffers();
240 return pass;
244 enum piglit_result
245 piglit_display(void)
247 GLboolean pass = run_test();
249 return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
253 void
254 piglit_init(int argc, char **argv)
256 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
258 piglit_require_extension("GL_EXT_framebuffer_object");
259 piglit_require_extension("GL_EXT_framebuffer_blit");
260 piglit_require_extension("GL_EXT_packed_depth_stencil");