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-gl.h"
31 PIGLIT_GL_TEST_CONFIG_BEGIN
33 config
.supports_gl_compat_version
= 10;
35 config
.window_visual
= PIGLIT_GL_VISUAL_RGBA
| PIGLIT_GL_VISUAL_DOUBLE
;
36 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
38 PIGLIT_GL_TEST_CONFIG_END
40 static GLuint Texture
;
42 static int nrcomponents(GLenum format
)
45 case GL_RGBA
: return 4;
46 case GL_RGB
: return 3;
47 case GL_ALPHA
: return 1;
53 expected_rgba(GLenum format
, const GLubyte
*texdata
, GLubyte
*expected
)
57 expected
[0] = texdata
[0];
58 expected
[1] = texdata
[1];
59 expected
[2] = texdata
[2];
60 expected
[3] = texdata
[3];
63 expected
[0] = texdata
[0];
64 expected
[1] = texdata
[1];
65 expected
[2] = texdata
[2];
72 expected
[3] = texdata
[0];
78 render_and_check(int w
, int h
, int d
, GLenum format
, float q
,
79 const GLubyte
*data
, const char* test
)
88 glClearColor(0.0, 0.0, 0.0, 0.0);
89 glClear(GL_COLOR_BUFFER_BIT
);
90 glEnable(GL_TEXTURE_3D
);
93 for(layer
= 0; layer
< d
; ++layer
) {
94 float r
= (layer
+0.5)/d
;
96 glTexCoord4f(0, 0, r
*q
, q
);
98 glTexCoord4f(q
, 0, r
*q
, q
);
100 glTexCoord4f(q
, q
, r
*q
, q
);
101 glVertex2f(x
+w
, y
+h
);
102 glTexCoord4f(0, q
, r
*q
, q
);
106 if (x
+ w
>= piglit_width
) {
112 readback
= (GLubyte
*)malloc(w
*h
*d
*4);
114 for(layer
= 0; layer
< d
; ++layer
) {
115 glReadPixels(x
, y
, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, readback
+layer
*w
*h
*4);
117 if (x
+ w
>= piglit_width
) {
125 ncomp
= nrcomponents(format
);
126 for(z
= 0; z
< d
; ++z
) {
127 for(y
= 0; y
< h
; ++y
) {
128 for(x
= 0; x
< w
; ++x
, readp
+= 4, texp
+= ncomp
) {
131 expected_rgba(format
, texp
, expected
);
132 for(i
= 0; i
< 4; ++i
) {
133 if (expected
[i
] != readp
[i
]) {
134 fprintf(stderr
, "%s: Mismatch at %ix%ix%i\n", test
, x
, y
, z
);
135 fprintf(stderr
, " Expected: %i,%i,%i,%i\n",
136 expected
[0], expected
[1], expected
[2], expected
[3]);
137 fprintf(stderr
, " Readback: %i,%i,%i,%i\n",
138 readp
[0], readp
[1], readp
[2], readp
[3]);
148 piglit_present_results();
155 * Load non-mipmapped 3D texture of the given size
156 * and check whether it is rendered correctly.
159 test_simple(int w
, int h
, int d
, GLenum format
)
166 assert(1 <= w
&& w
<= 16);
167 assert(1 <= h
&& h
<= 16);
168 assert(1 <= d
&& d
<= 16);
169 assert(format
== GL_RGBA
|| format
== GL_RGB
|| format
== GL_ALPHA
);
171 size
= w
*h
*d
*nrcomponents(format
);
172 data
= (GLubyte
*)malloc(size
);
174 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
176 srand(size
); /* Generate reproducible image data */
177 for(i
= 0; i
< size
; ++i
)
178 data
[i
] = rand() & 255;
180 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
181 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
182 glTexImage3D(GL_TEXTURE_3D
, 0, format
, w
, h
, d
, 0, format
, GL_UNSIGNED_BYTE
, data
);
184 success
= success
&& render_and_check(w
, h
, d
, format
, 1.0, data
, "Render 3D texture");
185 success
= success
&& render_and_check(w
, h
, d
, format
, 1.4, data
, "Render 3D texture (q != 1.0)");
191 "Failure with texture size %ix%ix%i, format = %s\n",
192 w
, h
, d
, piglit_get_gl_enum_name(format
));
201 static const GLenum formats
[] = { GL_RGBA
, GL_RGB
, GL_ALPHA
};
205 piglit_ortho_projection(piglit_width
, piglit_height
, GL_FALSE
);
207 for(fmt
= 0; fmt
< sizeof(formats
)/sizeof(formats
[0]); ++fmt
) {
208 for(w
= 1; w
<= 16; w
*= 2) {
209 for(h
= 1; h
<= 16; h
*= 2) {
210 for(d
= 1; d
<= 16; d
*= 2) {
211 pass
= test_simple(w
, h
, d
, formats
[fmt
]);
220 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
224 piglit_init(int argc
, char **argv
)
226 piglit_require_gl_version(12);
228 glDisable(GL_DITHER
);
230 glGenTextures(1, &Texture
);
231 glBindTexture(GL_TEXTURE_3D
, Texture
);
232 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);