summaryrefslogtreecommitdiff
path: root/tests/texturing/texture-al.c
blob: 90ed002b938fd1cff7fc05670aaa09109d7dc589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Copyright (c) 2011 Red Hat Inc.
 * derived from texture-rg - Copyright (c) 2010 VMware, Inc. 
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT.  IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/**
 * @file
 * Tests for a regression on r200 AL upload
 * https://bugs.freedesktop.org/show_bug.cgi?id=34280
 */

#include "piglit-util-gl-common.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.window_width = 128;
	config.window_height = 128;
	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_ALPHA | PIGLIT_GL_VISUAL_DOUBLE;

PIGLIT_GL_TEST_CONFIG_END

static const char *TestName = "texture-al";

static GLint TexWidth = 128, TexHeight = 128;

struct format_info
{
   const char *Name;
   GLenum IntFormat, BaseFormat;
  float expected0;
};


static const struct format_info IntFormats[] = {
   { "GL_ALPHA", GL_ALPHA, GL_ALPHA, 1.0 },
   { "GL_ALPHA_2", GL_ALPHA, GL_LUMINANCE_ALPHA, 1.0 },
   { "GL_LUMINANCE_ALPHA", GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, 1.0 },
   { "GL_LUMINANCE_ALPHA_2", GL_LUMINANCE_ALPHA, GL_ALPHA, 0.0 },
};

#define NUM_INT_FORMATS  (sizeof(IntFormats) / sizeof(IntFormats[0]))


static GLboolean
check_error(const char *file, int line)
{
   GLenum err = glGetError();
   if (err) {
      fprintf(stderr, "%s: error 0x%x at %s:%d\n", TestName, err, file, line);
      return GL_TRUE;
   }
   return GL_FALSE;
}


static void
fill_texture_image(GLint w, GLint h, GLint comps, GLubyte *buf)
{
   GLint i, j, k;
   for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
         for (k = 0; k < comps; k++) {
            GLfloat val;
            if (k == 0) {
               /* left/right = red gradient */
               val = (int) (255 * j / (float) (w - 1));
            }
            else {
               /* up/down = green gradient */
               val = (int) (255 * i / (float) (h - 1));
            }
            *buf++ = val;
         }
      }
   }
}


static GLboolean
test_teximage_formats(void)
{
   GLint i;
   GLubyte *image;

   GLuint tex;

   glGenTextures(1, &tex);
   glBindTexture(GL_TEXTURE_2D, tex);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   glEnable(GL_TEXTURE_2D);

   image = (GLubyte *) malloc(TexWidth * TexHeight * 2 * sizeof(GLubyte));

   for (i = 0; i < NUM_INT_FORMATS; i++) {
      const struct format_info *info = &IntFormats[i];
      const GLuint comps = (info->BaseFormat == GL_ALPHA) ? 1 : 2;
      GLfloat expected[4], result[4];
      GLfloat error = 2.0 / 255.0; /* XXX fix */

      fill_texture_image(TexWidth, TexHeight, comps, image);

      glTexImage2D(GL_TEXTURE_2D, 0, info->IntFormat,
                   TexWidth, TexHeight, 0,
                   info->BaseFormat, GL_UNSIGNED_BYTE, image);

      if (check_error(__FILE__, __LINE__)) {
         fprintf(stderr, "%s: Error in glTexImage2D for "
                 "internalFormat = %s\n", TestName, info->Name);
         return GL_FALSE;
      }

      if (0) {
         GLint f;
         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0,
                                  GL_TEXTURE_INTERNAL_FORMAT, &f);
         assert(f == info->IntFormat);
      }

      glClear(GL_COLOR_BUFFER_BIT);
      glBegin(GL_POLYGON);
      glTexCoord2f(0, 0);   glVertex2f(0, 0);
      glTexCoord2f(1, 0);   glVertex2f(TexWidth, 0);
      glTexCoord2f(1, 1);   glVertex2f(TexWidth, TexHeight);
      glTexCoord2f(0, 1);   glVertex2f(0, TexHeight);
      glEnd();

      /* setup expected polygon color */
      expected[0] = info->expected0;
      expected[1] = 0.5;
      expected[2] = 0.0;
      expected[3] = 1.0;

      /* test center pixel */
      result[0] = result[1] = result[2] = 0.0;
      result[3] = 1.0;
      glReadPixels(TexWidth/2, TexHeight/2, 1, 1, GL_LUMINANCE_ALPHA, GL_FLOAT, result);

      if (check_error(__FILE__, __LINE__)) {
         fprintf(stderr, "%s: Error in glReadPixels(format = GL_LA)\n",
                 TestName);
         return GL_FALSE;
      }

      if (fabsf(result[0] - expected[0]) > error ||
          fabsf(result[1] - expected[1]) > error ||
          fabsf(result[2] - expected[2]) > error ||
          fabsf(result[3] - expected[3]) > error) {
         fprintf(stderr, "%s: failure with format %s:\n", TestName,
                 info->Name);
         fprintf(stderr, "  expected color = %g, %g, %g, %g\n",
                 expected[0], expected[1], expected[2], expected[3]);
         fprintf(stderr, "  result color = %g, %g, %g, %g\n",
                 result[0], result[1], result[2], result[3]);
         return GL_FALSE;
      }

      piglit_present_results();
   }

   free(image);

   glDisable(GL_TEXTURE_2D);

   return GL_TRUE;
}


static GLboolean
test_drawpixels_formats(void)
{
   GLubyte *image;
   GLfloat result[4], expected[4];
   GLfloat error = 2.0 / 255.0; /* XXX fix */

   image = (GLubyte *) malloc(TexWidth * TexHeight * 2 * sizeof(GLubyte));

   fill_texture_image(TexWidth, TexHeight, 2, image);

   glWindowPos2iARB(0, 0);
   glDrawPixels(TexWidth, TexHeight, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, image);

   if (check_error(__FILE__, __LINE__)) {
      fprintf(stderr, "%s: Error in glDrawPixels(format = GL_LA)\n",
              TestName);
      return GL_FALSE;
   }

   /* test center pixel */
   result[0] = result[1] = result[2] = 0.0;
   result[3] = 1.0;
   glReadPixels(TexWidth/2, TexHeight/2, 1, 1, GL_LUMINANCE_ALPHA, GL_FLOAT, result);
   
   expected[0] = 1.0;
   expected[1] = 0.5;
   expected[2] = 0.0;
   expected[3] = 1.0;

   if (fabsf(result[0] - expected[0]) > error ||
       fabsf(result[1] - expected[1]) > error ||
       fabsf(result[2] - expected[2]) > error ||
       fabsf(result[3] - expected[3]) > error) {
      fprintf(stderr, "%s: glDrawPixels failure with format GL_LA:\n", TestName);
      fprintf(stderr, "  expected color = %g, %g, %g, %g\n",
              expected[0], expected[1], expected[2], expected[3]);
      fprintf(stderr, "  result color = %g, %g, %g, %g\n",
              result[0], result[1], result[2], result[3]);
      return GL_FALSE;
   }


   free(image);

   return GL_TRUE;
}


enum piglit_result
piglit_display(void)
{
   if (test_teximage_formats() && test_drawpixels_formats())
      return PIGLIT_PASS;
   else
      return PIGLIT_FAIL;
}


void
piglit_init(int argc, char **argv)
{
   piglit_require_extension("GL_ARB_window_pos");
   piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
}