Add more structure constructor tests.
[piglit/hramrach.git] / tests / texturing / tex3d.c
blob1647d8217cfc49d20f09497da49ced09610aa759
1 /*
2 * Copyright (c) The Piglit project 2008
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 /**
25 * @file
26 * Tests 3D textures.
29 #include "piglit-util.h"
31 int piglit_width = 128, piglit_height = 128;
32 int piglit_window_mode = GLUT_RGB | GLUT_ALPHA;
34 static GLuint Texture;
36 static int nrcomponents(GLenum format)
38 switch(format) {
39 case GL_RGBA: return 4;
40 case GL_RGB: return 3;
41 case GL_ALPHA: return 1;
42 default: abort();
46 static const char* formatname(GLenum format)
48 switch(format) {
49 case GL_RGBA: return "GL_RGBA";
50 case GL_RGB: return "GL_RGB";
51 case GL_ALPHA: return "GL_ALPHA";
52 default: abort();
56 static void expected_rgba(GLenum format, const unsigned char* texdata, unsigned char* expected)
58 switch(format) {
59 case GL_RGBA:
60 expected[0] = texdata[0];
61 expected[1] = texdata[1];
62 expected[2] = texdata[2];
63 expected[3] = texdata[3];
64 return;
65 case GL_RGB:
66 expected[0] = texdata[0];
67 expected[1] = texdata[1];
68 expected[2] = texdata[2];
69 expected[3] = 255;
70 return;
71 case GL_ALPHA:
72 expected[0] = 255;
73 expected[1] = 255;
74 expected[2] = 255;
75 expected[3] = texdata[0];
76 return;
80 static int render_and_check(int w, int h, int d, GLenum format, float q, unsigned char* data, const char* test)
82 int x, y, z;
83 int layer;
84 unsigned char* readback;
85 unsigned char* texp;
86 unsigned char* readp;
87 int ncomp = 0;
89 glClearColor(0.0, 0.0, 0.0, 0.0);
90 glClear(GL_COLOR_BUFFER_BIT);
91 glEnable(GL_TEXTURE_3D);
93 x = y = 0;
94 for(layer = 0; layer < d; ++layer) {
95 float r = (layer+0.5)/d;
96 glBegin(GL_QUADS);
97 glTexCoord4f(0, 0, r*q, q);
98 glVertex2f(x, y);
99 glTexCoord4f(q, 0, r*q, q);
100 glVertex2f(x+w, y);
101 glTexCoord4f(q, q, r*q, q);
102 glVertex2f(x+w, y+h);
103 glTexCoord4f(0, q, r*q, q);
104 glVertex2f(x, y+h);
105 glEnd();
106 x += w;
107 if (x >= piglit_width) {
108 y += h;
109 x = 0;
112 glutSwapBuffers();
114 readback = (unsigned char*)malloc(w*h*d*4);
115 x = y = 0;
116 for(layer = 0; layer < d; ++layer) {
117 glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, readback+layer*w*h*4);
118 x += w;
119 if (x >= piglit_width) {
120 y += h;
121 x = 0;
125 texp = data;
126 readp = readback;
127 ncomp = nrcomponents(format);
128 for(z = 0; z < d; ++z) {
129 for(y = 0; y < h; ++y) {
130 for(x = 0; x < w; ++x, readp += 4, texp += ncomp) {
131 unsigned char expected[4];
132 int i;
133 expected_rgba(format, texp, expected);
134 for(i = 0; i < 4; ++i) {
135 if (expected[i] != readp[i]) {
136 fprintf(stderr, "%s: Mismatch at %ix%ix%i\n", test, x, y, z);
137 fprintf(stderr, " Expected: %i,%i,%i,%i\n",
138 expected[0], expected[1], expected[2], expected[3]);
139 fprintf(stderr, " Readback: %i,%i,%i,%i\n",
140 readp[0], readp[1], readp[2], readp[3]);
141 free(readback);
142 return 0;
148 free(readback);
150 return 1;
155 * Load non-mipmapped 3D texture of the given size
156 * and check whether it is rendered correctly.
158 static void test_simple(int w, int h, int d, GLenum format)
160 int size;
161 unsigned char *data;
162 int i;
163 int success = 1;
165 assert(1 <= w && w <= 16);
166 assert(1 <= h && h <= 16);
167 assert(1 <= d && d <= 16);
168 assert(format == GL_RGBA || format == GL_RGB || format == GL_ALPHA);
170 size = w*h*d*nrcomponents(format);
171 data = (unsigned char*)malloc(size);
173 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
175 srand(size); /* Generate reproducible image data */
176 for(i = 0; i < size; ++i)
177 data[i] = rand() & 255;
179 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
180 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
181 glTexImage3D(GL_TEXTURE_3D, 0, format, w, h, d, 0, format, GL_UNSIGNED_BYTE, data);
183 success = success && render_and_check(w, h, d, format, 1.0, data, "Render 3D texture");
184 success = success && render_and_check(w, h, d, format, 1.4, data, "Render 3D texture (q != 1.0)");
186 free(data);
188 if (!success) {
189 fprintf(stderr, "Failure with texture size %ix%ix%i, format = %s\n",
190 w, h, d, formatname(format));
191 piglit_report_result(PIGLIT_FAILURE);
195 enum piglit_result
196 piglit_display(void)
198 GLenum formats[] = { GL_RGBA, GL_RGB, GL_ALPHA };
199 int w, h, d, fmt;
201 for(fmt = 0; fmt < sizeof(formats)/sizeof(formats[0]); ++fmt) {
202 for(w = 1; w <= 16; w *= 2) {
203 for(h = 1; h <= 16; h *= 2) {
204 for(d = 1; d <= 16; d *= 2) {
205 test_simple(w, h, d, formats[fmt]);
211 return PIGLIT_SUCCESS;
215 static void Reshape(int width, int height)
217 glViewport(0, 0, piglit_width, piglit_height);
218 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
222 void
223 piglit_init(int argc, char **argv)
225 piglit_automatic = GL_TRUE;
227 glutReshapeFunc(Reshape);
229 glDisable(GL_DITHER);
231 glGenTextures(1, &Texture);
232 glBindTexture(GL_TEXTURE_3D, Texture);
233 Reshape(piglit_width, piglit_height);