aboutsummaryrefslogtreecommitdiff
path: root/tests/texturing/mipmap-setup.c
blob: dee790211cb3a3332627f09cbce624381e7b507e (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
/*
 * Copyright 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
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 */

/*
 * Test building a mipmap in various orders:
 *   From largest to smallest
 *   From smallest to largest
 *   Random order.
 *
 * Author: Brian Paul
 */


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

PIGLIT_GL_TEST_MAIN(
    200 /*window_width*/,
    200 /*window_height*/,
    PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB)

#define MAX_LEVEL 7

enum order {
   NORMAL,
   REVERSE,
   RANDOM
};


static GLubyte
level_intensity(GLint level)
{
   return 100 + level * 20;
}


static void
setup_tex_image(GLint level)
{
   GLint size = 1 << (MAX_LEVEL - level);
   GLubyte *img = malloc(size * size * 4);
   GLubyte val = level_intensity(level);
   memset(img, val, size * size * 4);
   glTexImage2D(GL_TEXTURE_2D, level, GL_RGB, size, size, 0,
                GL_RGBA, GL_UNSIGNED_BYTE, img);
   free(img);
}


/*
 * Generate a mipmapped texture.
 * Define the mipmap levels/images in the order specified by 'ord'.
 */
static void
generate_mipmap(enum order ord)
{
   GLuint tex;
   GLint level;

   glGenTextures(1, &tex);
   glBindTexture(GL_TEXTURE_2D, tex);

   /* hint to the driver that there won't be a mipmap (but that's a lie) */
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

   /* Set each mipmap level to a different intensity, starting with the
    * smallest mipmap level and ending with the largest at level=0.
    */
   if (ord == NORMAL) {
      for (level = 0; level <= MAX_LEVEL; level++) {
         setup_tex_image(level);
      }
   }
   else if (ord == REVERSE) {
      for (level = MAX_LEVEL; level >= 0; level--) {
         setup_tex_image(level);
      }
   }
   else {
      static const GLuint rand[MAX_LEVEL + 1] = {3, 1, 2, 4, 7, 0, 6, 5};
      assert(ord == RANDOM);

      for (level = 0; level <= MAX_LEVEL; level++) {
         setup_tex_image(rand[level]);
      }
   }

   /* use mipmap filtering */
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}


static GLboolean
test(enum order ord, const char *order)
{
   const int px = piglit_width / 2, py = piglit_height / 2;
   GLboolean pass = GL_TRUE;
   GLuint level;

   generate_mipmap(ord);

   glEnable(GL_TEXTURE_2D);

   /* render a test polygon for each mipmap level */
   for (level = 0; level <= MAX_LEVEL; level++) {
      float expected[4];
      int p;

      expected[0] = expected[1] = expected[2] = level_intensity(level) / 255.0;

      /* force sampling from a specific mipmap level */
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, level);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, level);

      glClear(GL_COLOR_BUFFER_BIT);

      glBegin(GL_POLYGON);
      glTexCoord2f(0, 0);
      glVertex2f(-1, -1);
      glTexCoord2f(1, 0);
      glVertex2f(1, -1);
      glTexCoord2f(1, 1);
      glVertex2f(1, 1);
      glTexCoord2f(0, 1);
      glVertex2f(-1, 1);
      glEnd();

      p = piglit_probe_pixel_rgb(px, py, expected);
      if (!p) {
         printf("  At mipmap level %d, order = %s\n", level, order);
      }

      pass &= p;
      
      piglit_present_results();
   }

   return pass;
}


void
piglit_init(int argc, char **argv)
{
   /* nothing */
}


enum piglit_result
piglit_display(void)
{
   if (!test(NORMAL, "Normal"))
      return PIGLIT_FAIL;

   if (!test(REVERSE, "Reverse"))
      return PIGLIT_FAIL;

   if (!test(RANDOM, "Random"))
      return PIGLIT_FAIL;

   return PIGLIT_PASS;
}