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
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.
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
)
39 case GL_RGBA
: return 4;
40 case GL_RGB
: return 3;
41 case GL_ALPHA
: return 1;
46 static const char* formatname(GLenum format
)
49 case GL_RGBA
: return "GL_RGBA";
50 case GL_RGB
: return "GL_RGB";
51 case GL_ALPHA
: return "GL_ALPHA";
56 static void expected_rgba(GLenum format
, const unsigned char* texdata
, unsigned char* expected
)
60 expected
[0] = texdata
[0];
61 expected
[1] = texdata
[1];
62 expected
[2] = texdata
[2];
63 expected
[3] = texdata
[3];
66 expected
[0] = texdata
[0];
67 expected
[1] = texdata
[1];
68 expected
[2] = texdata
[2];
75 expected
[3] = texdata
[0];
80 static int render_and_check(int w
, int h
, int d
, GLenum format
, float q
, unsigned char* data
, const char* test
)
84 unsigned char* readback
;
89 glClearColor(0.0, 0.0, 0.0, 0.0);
90 glClear(GL_COLOR_BUFFER_BIT
);
91 glEnable(GL_TEXTURE_3D
);
94 for(layer
= 0; layer
< d
; ++layer
) {
95 float r
= (layer
+0.5)/d
;
97 glTexCoord4f(0, 0, r
*q
, q
);
99 glTexCoord4f(q
, 0, r
*q
, q
);
101 glTexCoord4f(q
, q
, r
*q
, q
);
102 glVertex2f(x
+w
, y
+h
);
103 glTexCoord4f(0, q
, r
*q
, q
);
107 if (x
>= piglit_width
) {
114 readback
= (unsigned char*)malloc(w
*h
*d
*4);
116 for(layer
= 0; layer
< d
; ++layer
) {
117 glReadPixels(x
, y
, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, readback
+layer
*w
*h
*4);
119 if (x
>= piglit_width
) {
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];
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]);
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
)
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)");
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
);
198 GLenum formats
[] = { GL_RGBA
, GL_RGB
, GL_ALPHA
};
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
);
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
);