Add more structure constructor tests.
[piglit/hramrach.git] / tests / texturing / s3tc-texsubimage.c
blob365baca50cb5eb29951802928a04d7c1241e764b
1 /*
2 * Copyright © 2009 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 * Chris Lord <chris@openedhand.com>
25 * Eric Anholt <eric@anholt.net>
29 /** @file s3tc-teximage.c
31 * Tests that a full S3TC-compressed mipmap tree can be created and
32 * used.
35 #include "piglit-util.h"
37 int piglit_width = 500, piglit_height = 700;
38 int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;
40 #define SIZE 128
42 const float red[4] = {1.0, 0.0, 0.0, 1.0};
43 const float green[4] = {0.0, 1.0, 0.0, 1.0};
44 const float blue[4] = {0.0, 0.0, 1.0, 1.0};
45 const float white[4] = {1.0, 1.0, 1.0, 1.0};
47 static void
48 display_mipmaps(int start_x, int start_y)
50 int i;
52 glEnable(GL_TEXTURE_2D);
54 /* Disply all the mipmap levels */
55 for (i = SIZE; i > 0; i /= 2) {
56 glBegin(GL_QUADS);
57 glTexCoord2f(0.0, 0.0); glVertex2f(start_x + 0, start_y + 0);
58 glTexCoord2f(1.0, 0.0); glVertex2f(start_x + i, start_y + 0);
59 glTexCoord2f(1.0, 1.0); glVertex2f(start_x + i, start_y + i);
60 glTexCoord2f(0.0, 1.0); glVertex2f(start_x + 0, start_y + i);
61 glEnd();
63 start_x += i + 5;
67 static GLuint
68 create_texture(GLenum format)
70 GLfloat *data;
71 int size, x, y, level;
72 GLuint tex;
74 glGenTextures(1, &tex);
75 glBindTexture(GL_TEXTURE_2D, tex);
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
77 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
80 GL_LINEAR_MIPMAP_NEAREST);
81 data = malloc(SIZE * SIZE * 4 * sizeof(GLfloat));
83 for (level = 0, size = SIZE; size > 0; level++, size >>= 1) {
84 for (y = 0; y < size; y++) {
85 for (x = 0; x < size; x++) {
86 const float *color;
88 if (size == 4)
89 color = red;
90 else if (size == 2)
91 color = green;
92 else if (size == 1)
93 color = blue;
94 else if (x < size / 2 && y < size / 2)
95 color = red;
96 else if (y < size / 2)
97 color = green;
98 else if (x < size / 2)
99 color = blue;
100 else
101 color = white;
103 memcpy(data + (y * size + x) * 4, color,
104 4 * sizeof(float));
108 /* Create empty texture */
109 glTexImage2D(GL_TEXTURE_2D, level, format,
110 size, size, 0,
111 GL_RGBA, GL_FLOAT, NULL);
113 glPixelStorei(GL_UNPACK_ROW_LENGTH, size);
115 /* Fill in sub regions of texture */
116 if (size <= 4) {
117 glTexSubImage2D(GL_TEXTURE_2D, level,
118 0, 0, size, size,
119 GL_RGBA, GL_FLOAT, data);
120 } else {
121 float *reds = data;
122 float *greens = data + size / 2 * 4;
123 float *blues = reds + (size / 2) * size * 4;
124 float *whites = blues + size / 2 * 4;
126 glTexSubImage2D(GL_TEXTURE_2D, level,
127 0, 0,
128 size / 2, size / 2,
129 GL_RGBA, GL_FLOAT, reds);
130 glTexSubImage2D(GL_TEXTURE_2D, level,
131 size / 2, 0,
132 size / 2, size / 2,
133 GL_RGBA, GL_FLOAT, greens);
134 glTexSubImage2D(GL_TEXTURE_2D, level,
135 0, size / 2,
136 size / 2, size / 2,
137 GL_RGBA, GL_FLOAT, blues);
138 glTexSubImage2D(GL_TEXTURE_2D, level,
139 size / 2, size / 2,
140 size / 2, size / 2,
141 GL_RGBA, GL_FLOAT, whites);
144 free(data);
145 return tex;
148 static GLboolean
149 check_resulting_mipmaps(int x, int y)
151 GLboolean pass = GL_TRUE;
152 int size;
154 for (size = SIZE; size > 0; size /= 2) {
155 if (size == 4)
156 pass = pass && piglit_probe_pixel_rgb(x + 2, y + 2,
157 red);
158 else if (size == 2)
159 pass = pass && piglit_probe_pixel_rgb(x + 1, y + 1,
160 green);
161 else if (size == 1)
162 pass = pass && piglit_probe_pixel_rgb(x, y,
163 blue);
164 else {
165 pass = pass && piglit_probe_pixel_rgb(x + size / 4,
166 y + size / 4,
167 red);
168 pass = pass && piglit_probe_pixel_rgb(x + size * 3 / 4,
169 y + size / 4,
170 green);
171 pass = pass && piglit_probe_pixel_rgb(x + size / 4,
172 y + size * 3 / 4,
173 blue);
174 pass = pass && piglit_probe_pixel_rgb(x + size * 3 / 4,
175 y + size * 3 / 4,
176 white);
178 x += size + 5;
181 return pass;
184 enum piglit_result
185 piglit_display(void)
187 GLuint tex;
188 GLboolean pass = GL_TRUE;
190 glClearColor(0, 0, 0, 0);
191 glClear(GL_COLOR_BUFFER_BIT);
193 tex = create_texture(GL_COMPRESSED_RGB_S3TC_DXT1_EXT);
194 display_mipmaps(10, 10 + (10 + SIZE) * 0);
195 glDeleteTextures(1, &tex);
196 tex = create_texture(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT);
197 display_mipmaps(10, 10 + (10 + SIZE) * 1);
198 glDeleteTextures(1, &tex);
199 tex = create_texture(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT);
200 display_mipmaps(10, 10 + (10 + SIZE) * 2);
201 glDeleteTextures(1, &tex);
202 tex = create_texture(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT);
203 display_mipmaps(10, 10 + (10 + SIZE) * 3);
204 glDeleteTextures(1, &tex);
206 pass = pass && check_resulting_mipmaps(10, 10 + (10 + SIZE) * 0);
207 pass = pass && check_resulting_mipmaps(10, 10 + (10 + SIZE) * 1);
208 pass = pass && check_resulting_mipmaps(10, 10 + (10 + SIZE) * 2);
209 pass = pass && check_resulting_mipmaps(10, 10 + (10 + SIZE) * 3);
211 glutSwapBuffers();
213 return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
216 void
217 piglit_init(int argc, char **argv)
219 piglit_require_extension("GL_EXT_texture_compression_s3tc");
221 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);