add pbobench: a benchmark for pbo functions
[piglit.git] / tests / texturing / tex3d.c
blob0006a6b1a764243bec70797820af2f956062ec38
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-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)
44 switch(format) {
45 case GL_RGBA: return 4;
46 case GL_RGB: return 3;
47 case GL_ALPHA: return 1;
48 default: abort();
52 static void
53 expected_rgba(GLenum format, const GLubyte *texdata, GLubyte *expected)
55 switch(format) {
56 case GL_RGBA:
57 expected[0] = texdata[0];
58 expected[1] = texdata[1];
59 expected[2] = texdata[2];
60 expected[3] = texdata[3];
61 return;
62 case GL_RGB:
63 expected[0] = texdata[0];
64 expected[1] = texdata[1];
65 expected[2] = texdata[2];
66 expected[3] = 255;
67 return;
68 case GL_ALPHA:
69 expected[0] = 255;
70 expected[1] = 255;
71 expected[2] = 255;
72 expected[3] = texdata[0];
73 return;
77 static bool
78 render_and_check(int w, int h, int d, GLenum format, float q,
79 const GLubyte *data, const char* test)
81 int x, y, z;
82 int layer;
83 GLubyte *readback;
84 const GLubyte *texp;
85 GLubyte *readp;
86 int ncomp = 0;
88 glClearColor(0.0, 0.0, 0.0, 0.0);
89 glClear(GL_COLOR_BUFFER_BIT);
90 glEnable(GL_TEXTURE_3D);
92 x = y = 0;
93 for(layer = 0; layer < d; ++layer) {
94 float r = (layer+0.5)/d;
95 glBegin(GL_QUADS);
96 glTexCoord4f(0, 0, r*q, q);
97 glVertex2f(x, y);
98 glTexCoord4f(q, 0, r*q, q);
99 glVertex2f(x+w, y);
100 glTexCoord4f(q, q, r*q, q);
101 glVertex2f(x+w, y+h);
102 glTexCoord4f(0, q, r*q, q);
103 glVertex2f(x, y+h);
104 glEnd();
105 x += w;
106 if (x + w >= piglit_width) {
107 y += h;
108 x = 0;
112 readback = (GLubyte*)malloc(w*h*d*4);
113 x = y = 0;
114 for(layer = 0; layer < d; ++layer) {
115 glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, readback+layer*w*h*4);
116 x += w;
117 if (x + w >= piglit_width) {
118 y += h;
119 x = 0;
123 texp = data;
124 readp = readback;
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) {
129 GLubyte expected[4];
130 int i;
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]);
139 free(readback);
140 return false;
146 free(readback);
148 piglit_present_results();
150 return true;
155 * Load non-mipmapped 3D texture of the given size
156 * and check whether it is rendered correctly.
158 static bool
159 test_simple(int w, int h, int d, GLenum format)
161 int size;
162 GLubyte *data;
163 int i;
164 bool success = true;
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)");
187 free(data);
189 if (!success) {
190 fprintf(stderr,
191 "Failure with texture size %ix%ix%i, format = %s\n",
192 w, h, d, piglit_get_gl_enum_name(format));
195 return success;
198 enum piglit_result
199 piglit_display(void)
201 static const GLenum formats[] = { GL_RGBA, GL_RGB, GL_ALPHA };
202 int w, h, d, fmt;
203 bool pass = true;
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]);
212 if (!pass)
213 goto end;
219 end:
220 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
223 void
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);