add pbobench: a benchmark for pbo functions
[piglit.git] / tests / texturing / texture-al.c
blob0d0d2ead845de2f6275162e96fca81fd5794c8c2
1 /*
2 * Copyright (c) 2011 Red Hat Inc.
3 * derived from texture-rg - Copyright (c) 2010 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NON-INFRINGEMENT. IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
26 /**
27 * @file
28 * Tests for a regression on r200 AL upload
29 * https://bugs.freedesktop.org/show_bug.cgi?id=34280
32 #include "piglit-util-gl.h"
34 PIGLIT_GL_TEST_CONFIG_BEGIN
36 config.supports_gl_compat_version = 10;
38 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
39 config.khr_no_error_support = PIGLIT_NO_ERRORS;
41 PIGLIT_GL_TEST_CONFIG_END
43 static const char *TestName = "texture-al";
45 static GLint TexWidth = 128, TexHeight = 128;
47 struct format_info
49 GLenum IntFormat, BaseFormat;
50 float expected0;
54 static const struct format_info IntFormats[] = {
55 { GL_ALPHA, GL_ALPHA, 1.0 },
56 { GL_ALPHA, GL_LUMINANCE_ALPHA, 1.0 },
57 { GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, 1.0 },
58 { GL_LUMINANCE_ALPHA, GL_ALPHA, 0.0 },
61 #define NUM_INT_FORMATS (sizeof(IntFormats) / sizeof(IntFormats[0]))
64 static GLboolean
65 check_error(const char *file, int line)
67 GLenum err = glGetError();
68 if (err) {
69 fprintf(stderr, "%s: error 0x%x at %s:%d\n", TestName, err, file, line);
70 return GL_TRUE;
72 return GL_FALSE;
76 static void
77 fill_texture_image(GLint w, GLint h, GLint comps, GLubyte *buf)
79 GLint i, j, k;
80 for (i = 0; i < h; i++) {
81 for (j = 0; j < w; j++) {
82 for (k = 0; k < comps; k++) {
83 GLfloat val;
84 if (k == 0) {
85 /* left/right = red gradient */
86 val = (int) (255 * j / (float) (w - 1));
88 else {
89 /* up/down = green gradient */
90 val = (int) (255 * i / (float) (h - 1));
92 *buf++ = val;
99 static GLboolean
100 test_teximage_formats(void)
102 GLint i;
103 GLubyte *image;
105 GLuint tex;
107 glGenTextures(1, &tex);
108 glBindTexture(GL_TEXTURE_2D, tex);
109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
111 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
112 glEnable(GL_TEXTURE_2D);
114 image = (GLubyte *) malloc(TexWidth * TexHeight * 2 * sizeof(GLubyte));
116 for (i = 0; i < NUM_INT_FORMATS; i++) {
117 const struct format_info *info = &IntFormats[i];
118 const GLuint comps = (info->BaseFormat == GL_ALPHA) ? 1 : 2;
119 GLfloat expected[4], result[4];
120 GLfloat error = 2.0 / 255.0; /* XXX fix */
122 fill_texture_image(TexWidth, TexHeight, comps, image);
124 glTexImage2D(GL_TEXTURE_2D, 0, info->IntFormat,
125 TexWidth, TexHeight, 0,
126 info->BaseFormat, GL_UNSIGNED_BYTE, image);
128 if (check_error(__FILE__, __LINE__)) {
129 fprintf(stderr, "%s: Error in glTexImage2D for "
130 "internalFormat = %s, baseFormat = %s\n", TestName,
131 piglit_get_gl_enum_name(info->IntFormat),
132 piglit_get_gl_enum_name(info->BaseFormat));
133 return GL_FALSE;
136 if (0) {
137 GLint f;
138 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0,
139 GL_TEXTURE_INTERNAL_FORMAT, &f);
140 assert(f == info->IntFormat);
143 glClear(GL_COLOR_BUFFER_BIT);
144 glBegin(GL_POLYGON);
145 glTexCoord2f(0, 0); glVertex2f(0, 0);
146 glTexCoord2f(1, 0); glVertex2f(TexWidth, 0);
147 glTexCoord2f(1, 1); glVertex2f(TexWidth, TexHeight);
148 glTexCoord2f(0, 1); glVertex2f(0, TexHeight);
149 glEnd();
151 /* setup expected polygon color */
152 expected[0] = info->expected0;
153 expected[1] = 0.5;
154 expected[2] = 0.0;
155 expected[3] = 1.0;
157 /* test center pixel */
158 result[0] = result[1] = result[2] = 0.0;
159 result[3] = 1.0;
160 glReadPixels(TexWidth/2, TexHeight/2, 1, 1, GL_LUMINANCE_ALPHA, GL_FLOAT, result);
162 if (check_error(__FILE__, __LINE__)) {
163 fprintf(stderr, "%s: Error in glReadPixels(format = GL_LA)\n",
164 TestName);
165 return GL_FALSE;
168 if (fabsf(result[0] - expected[0]) > error ||
169 fabsf(result[1] - expected[1]) > error ||
170 fabsf(result[2] - expected[2]) > error ||
171 fabsf(result[3] - expected[3]) > error) {
172 fprintf(stderr, "%s: failure with internalFormat %s, "
173 "baseFormat %s:\n", TestName,
174 piglit_get_gl_enum_name(info->IntFormat),
175 piglit_get_gl_enum_name(info->BaseFormat));
176 fprintf(stderr, " expected color = %g, %g, %g, %g\n",
177 expected[0], expected[1], expected[2], expected[3]);
178 fprintf(stderr, " result color = %g, %g, %g, %g\n",
179 result[0], result[1], result[2], result[3]);
180 return GL_FALSE;
183 piglit_present_results();
186 free(image);
188 glDisable(GL_TEXTURE_2D);
190 return GL_TRUE;
194 static GLboolean
195 test_drawpixels_formats(void)
197 GLubyte *image;
198 GLfloat result[4], expected[4];
199 GLfloat error = 2.0 / 255.0; /* XXX fix */
201 image = (GLubyte *) malloc(TexWidth * TexHeight * 2 * sizeof(GLubyte));
203 fill_texture_image(TexWidth, TexHeight, 2, image);
205 glWindowPos2iARB(0, 0);
206 glDrawPixels(TexWidth, TexHeight, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, image);
208 if (check_error(__FILE__, __LINE__)) {
209 fprintf(stderr, "%s: Error in glDrawPixels(format = GL_LA)\n",
210 TestName);
211 return GL_FALSE;
214 /* test center pixel */
215 result[0] = result[1] = result[2] = 0.0;
216 result[3] = 1.0;
217 glReadPixels(TexWidth/2, TexHeight/2, 1, 1, GL_LUMINANCE_ALPHA, GL_FLOAT, result);
219 expected[0] = 1.0;
220 expected[1] = 0.5;
221 expected[2] = 0.0;
222 expected[3] = 1.0;
224 if (fabsf(result[0] - expected[0]) > error ||
225 fabsf(result[1] - expected[1]) > error ||
226 fabsf(result[2] - expected[2]) > error ||
227 fabsf(result[3] - expected[3]) > error) {
228 fprintf(stderr, "%s: glDrawPixels failure with format GL_LA:\n", TestName);
229 fprintf(stderr, " expected color = %g, %g, %g, %g\n",
230 expected[0], expected[1], expected[2], expected[3]);
231 fprintf(stderr, " result color = %g, %g, %g, %g\n",
232 result[0], result[1], result[2], result[3]);
233 return GL_FALSE;
237 free(image);
239 return GL_TRUE;
243 enum piglit_result
244 piglit_display(void)
246 if (test_teximage_formats() && test_drawpixels_formats())
247 return PIGLIT_PASS;
248 else
249 return PIGLIT_FAIL;
253 void
254 piglit_init(int argc, char **argv)
256 piglit_require_extension("GL_ARB_window_pos");
257 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);