aboutsummaryrefslogtreecommitdiff
path: root/tests/texturing/tex3d.c
blob: 62610736945e3c4c1fd41bb9d7551143e99d543f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Copyright (c) The Piglit project 2008
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * @file
 * Tests 3D textures.
 */

#include "piglit-util-gl-common.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 10;

	config.window_width = 128;
	config.window_height = 128;
	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_ALPHA;

PIGLIT_GL_TEST_CONFIG_END

static GLuint Texture;

static int nrcomponents(GLenum format)
{
	switch(format) {
	case GL_RGBA: return 4;
	case GL_RGB: return 3;
	case GL_ALPHA: return 1;
	default: abort();
	}
}

static const char* formatname(GLenum format)
{
	switch(format) {
	case GL_RGBA: return "GL_RGBA";
	case GL_RGB: return "GL_RGB";
	case GL_ALPHA: return "GL_ALPHA";
	default: abort();
	}
}

static void expected_rgba(GLenum format, const unsigned char* texdata, unsigned char* expected)
{
	switch(format) {
	case GL_RGBA:
		expected[0] = texdata[0];
		expected[1] = texdata[1];
		expected[2] = texdata[2];
		expected[3] = texdata[3];
		return;
	case GL_RGB:
		expected[0] = texdata[0];
		expected[1] = texdata[1];
		expected[2] = texdata[2];
		expected[3] = 255;
		return;
	case GL_ALPHA:
		expected[0] = 255;
		expected[1] = 255;
		expected[2] = 255;
		expected[3] = texdata[0];
		return;
	}
}

static int render_and_check(int w, int h, int d, GLenum format, float q, unsigned char* data, const char* test)
{
	int x, y, z;
	int layer;
	unsigned char* readback;
	unsigned char* texp;
	unsigned char* readp;
	int ncomp = 0;

	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glEnable(GL_TEXTURE_3D);

	x = y = 0;
	for(layer = 0; layer < d; ++layer) {
		float r = (layer+0.5)/d;
		glBegin(GL_QUADS);
			glTexCoord4f(0, 0, r*q, q);
			glVertex2f(x, y);
			glTexCoord4f(q, 0, r*q, q);
			glVertex2f(x+w, y);
			glTexCoord4f(q, q, r*q, q);
			glVertex2f(x+w, y+h);
			glTexCoord4f(0, q, r*q, q);
			glVertex2f(x, y+h);
		glEnd();
		x += w;
		if (x >= piglit_width) {
			y += h;
			x = 0;
		}
	}

	readback = (unsigned char*)malloc(w*h*d*4);
	x = y = 0;
	for(layer = 0; layer < d; ++layer) {
		glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, readback+layer*w*h*4);
		x += w;
		if (x >= piglit_width) {
			y += h;
			x = 0;
		}
	}

	texp = data;
	readp = readback;
	ncomp = nrcomponents(format);
	for(z = 0; z < d; ++z) {
		for(y = 0; y < h; ++y) {
			for(x = 0; x < w; ++x, readp += 4, texp += ncomp) {
				unsigned char expected[4];
				int i;
				expected_rgba(format, texp, expected);
				for(i = 0; i < 4; ++i) {
					if (expected[i] != readp[i]) {
						fprintf(stderr, "%s: Mismatch at %ix%ix%i\n", test, x, y, z);
						fprintf(stderr, " Expected: %i,%i,%i,%i\n",
							expected[0], expected[1], expected[2], expected[3]);
						fprintf(stderr, " Readback: %i,%i,%i,%i\n",
							readp[0], readp[1], readp[2], readp[3]);
						free(readback);
						return 0;
					}
				}
			}
		}
	}
	free(readback);

	piglit_present_results();

	return 1;
}


/**
 * Load non-mipmapped 3D texture of the given size
 * and check whether it is rendered correctly.
 */
static void test_simple(int w, int h, int d, GLenum format)
{
	int size;
	unsigned char *data;
	int i;
	int success = 1;

	assert(1 <= w && w <= 16);
	assert(1 <= h && h <= 16);
	assert(1 <= d && d <= 16);
	assert(format == GL_RGBA || format == GL_RGB || format == GL_ALPHA);

	size = w*h*d*nrcomponents(format);
	data = (unsigned char*)malloc(size);

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	srand(size); /* Generate reproducible image data */
	for(i = 0; i < size; ++i)
		data[i] = rand() & 255;

	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage3D(GL_TEXTURE_3D, 0, format, w, h, d, 0, format, GL_UNSIGNED_BYTE, data);

	success = success && render_and_check(w, h, d, format, 1.0, data, "Render 3D texture");
	success = success && render_and_check(w, h, d, format, 1.4, data, "Render 3D texture (q != 1.0)");

	free(data);

	if (!success) {
		fprintf(stderr, "Failure with texture size %ix%ix%i, format = %s\n",
			w, h, d, formatname(format));
		piglit_report_result(PIGLIT_FAIL);
	}
}

enum piglit_result
piglit_display(void)
{
	GLenum formats[] = { GL_RGBA, GL_RGB, GL_ALPHA };
	int w, h, d, fmt;

	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

	for(fmt = 0; fmt < sizeof(formats)/sizeof(formats[0]); ++fmt) {
		for(w = 1; w <= 16; w *= 2) {
			for(h = 1; h <= 16; h *= 2) {
				for(d = 1; d <= 16; d *= 2) {
					test_simple(w, h, d, formats[fmt]);
				}
			}
		}
	}

	return PIGLIT_PASS;
}

void
piglit_init(int argc, char **argv)
{
	piglit_require_gl_version(12);

	piglit_automatic = GL_TRUE;

	glDisable(GL_DITHER);

	glGenTextures(1, &Texture);
	glBindTexture(GL_TEXTURE_3D, Texture);
}