arb_copy_image: test copying of different mipmap levels of a texture
[piglit.git] / tests / fbo / fbo-array.c
blob635fd2b25edb326f5680779c306e437073a9b05f
1 /*
2 * Copyright © 2009 Intel Corporation
3 * Copyright (c) 2010 VMware, Inc.
4 * Copyright © 2011 Red Hat Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
25 * Authors:
26 * Dave Airlie
30 /** @file fbo-array.c
32 * Tests that drawing to each level of an array texture FBO and then drawing views * of those individual depths to the window system framebuffer succeeds.
33 * based on fbo-3d.c and array-texture.c
36 #include "piglit-util-gl.h"
38 #define BUF_WIDTH 32
39 #define BUF_HEIGHT 32
41 PIGLIT_GL_TEST_CONFIG_BEGIN
43 config.supports_gl_compat_version = 10;
45 config.window_width = 200;
46 config.window_height = 100;
47 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB;
48 config.khr_no_error_support = PIGLIT_NO_ERRORS;
50 PIGLIT_GL_TEST_CONFIG_END
52 #define NUM_LAYERS 6
54 float layer_color[NUM_LAYERS][4] = {
55 {1.0, 0.0, 0.0, 0.0},
56 {0.0, 1.0, 0.0, 0.0},
57 {0.0, 0.0, 1.0, 0.0},
58 {1.0, 0.0, 1.0, 0.0},
59 {1.0, 1.0, 0.0, 0.0},
60 {0.0, 1.0, 1.0, 0.0},
63 int num_layers = NUM_LAYERS;
65 static const char *prog = "fbo-array";
66 /* debug aid */
67 static void
68 check_error(int line)
70 GLenum err = glGetError();
71 if (err) {
72 printf("%s: GL error 0x%x at line %d\n", prog, err, line);
76 static const char *frag_shader_2d_array_text =
77 "#extension GL_EXT_texture_array : enable \n"
78 "uniform sampler2DArray tex; \n"
79 "void main() \n"
80 "{ \n"
81 " gl_FragColor = texture2DArray(tex, gl_TexCoord[0].xyz); \n"
82 "} \n";
84 static GLuint frag_shader_2d_array;
85 static GLuint program_2d_array;
88 static int
89 create_array_fbo(void)
91 GLuint tex, fb;
92 GLenum status;
93 int layer;
95 glGenTextures(1, &tex);
96 glBindTexture(GL_TEXTURE_2D_ARRAY_EXT, tex);
97 if (!piglit_check_gl_error(GL_NO_ERROR))
98 piglit_report_result(PIGLIT_FAIL);
100 /* allocate empty array texture */
101 glTexImage3D(GL_TEXTURE_2D_ARRAY_EXT, 0, GL_RGBA,
102 BUF_WIDTH, BUF_HEIGHT, num_layers,
104 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
105 if (!piglit_check_gl_error(GL_NO_ERROR))
106 piglit_report_result(PIGLIT_FAIL);
108 glGenFramebuffersEXT(1, &fb);
109 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
111 /* draw something into each layer of the array texture */
112 for (layer = 0; layer < NUM_LAYERS; layer++) {
113 glFramebufferTextureLayer(GL_FRAMEBUFFER_EXT,
114 GL_COLOR_ATTACHMENT0_EXT,
115 tex,
117 layer);
119 if (!piglit_check_gl_error(GL_NO_ERROR))
120 piglit_report_result(PIGLIT_FAIL);
122 status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
123 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
124 fprintf(stderr, "FBO incomplete\n");
125 goto done;
128 glViewport(0, 0, BUF_WIDTH, BUF_HEIGHT);
129 piglit_ortho_projection(BUF_WIDTH, BUF_HEIGHT, GL_FALSE);
131 /* solid color quad */
132 glColor4fv(layer_color[layer]);
133 piglit_draw_rect(-2, -2, BUF_WIDTH + 2, BUF_HEIGHT + 2);
137 done:
138 glDeleteFramebuffersEXT(1, &fb);
139 return tex;
142 /* Draw a textured quad, sampling only the given layer of the
143 * array texture.
145 static void
146 draw_layer(int x, int y, int depth)
148 GLfloat depth_coord = (GLfloat)depth;
149 GLint loc;
151 glUseProgram(program_2d_array);
152 loc = glGetUniformLocation(program_2d_array, "tex");
153 glUniform1i(loc, 0); /* texture unit p */
155 glViewport(0, 0, piglit_width, piglit_height);
156 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
158 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, piglit_winsys_fbo);
160 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
161 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
162 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
163 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
164 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
165 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
166 glBegin(GL_QUADS);
168 glTexCoord3f(0, 0, depth_coord);
169 glVertex2f(x, y);
171 glTexCoord3f(1, 0, depth_coord);
172 glVertex2f(x + BUF_WIDTH, y);
174 glTexCoord3f(1, 1, depth_coord);
175 glVertex2f(x + BUF_WIDTH, y + BUF_HEIGHT);
177 glTexCoord3f(0, 1, depth_coord);
178 glVertex2f(x, y + BUF_HEIGHT);
180 glEnd();
182 glUseProgram(0);
185 static GLboolean test_layer_drawing(int start_x, int start_y, float *expected)
187 return piglit_probe_rect_rgb(start_x, start_y, BUF_WIDTH, BUF_HEIGHT,
188 expected);
191 enum piglit_result
192 piglit_display(void)
194 GLboolean pass = GL_TRUE;
195 int layer;
196 GLuint tex;
198 glClearColor(1.0, 1.0, 1.0, 1.0);
199 glClear(GL_COLOR_BUFFER_BIT);
201 tex = create_array_fbo();
203 for (layer = 0; layer < NUM_LAYERS; layer++) {
204 int x = 1 + layer * (BUF_WIDTH + 1);
205 int y = 1;
206 draw_layer(x, y, layer);
209 for (layer = 0; layer < NUM_LAYERS; layer++) {
210 int x = 1 + layer * (BUF_WIDTH + 1);
211 int y = 1;
212 pass &= test_layer_drawing(x, y, layer_color[layer]);
215 glDeleteTextures(1, &tex);
217 piglit_present_results();
219 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
222 void piglit_init(int argc, char **argv)
224 piglit_require_extension("GL_EXT_framebuffer_object");
225 piglit_require_extension("GL_EXT_texture_array");
227 /* Make shader programs */
228 frag_shader_2d_array =
229 piglit_compile_shader_text(GL_FRAGMENT_SHADER,
230 frag_shader_2d_array_text);
231 check_error(__LINE__);
233 program_2d_array = piglit_link_simple_program(0, frag_shader_2d_array);
234 check_error(__LINE__);