summaryrefslogtreecommitdiff
path: root/tests/texturing/texsubimage.c
blob: 9f8df8accf16388f48bb1125438f9f4096f4f4ee (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * Copyright © 2011 VMware, Inc.
 *
 * 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
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 */


/**
 * This should expose any errors in texel addressing within a texture image
 * when calling glTexSubImage1D/2D/3D().
 *
 * Brian Paul
 * October 2011
 */


#include "piglit-util.h"
#include "../fbo/fbo-formats.h"

int piglit_width = 150, piglit_height = 150;
int piglit_window_mode = GLUT_RGBA | GLUT_ALPHA | GLUT_DOUBLE;


/**
 * This is a subset of the formats in fbo-formats.h
 * We don't test non-color, float, or int/uint textures at this time.
 */
static const struct test_desc texsubimage_test_sets[] = {
	{
		core,
		ARRAY_SIZE(core),
		"Core formats",
		GL_UNSIGNED_NORMALIZED,
	},
	{
		tdfx_texture_compression_fxt1,
		ARRAY_SIZE(tdfx_texture_compression_fxt1),
		"GL_3DFX_texture_compression_FXT1",
		GL_UNSIGNED_NORMALIZED,
		{"GL_ARB_texture_compression",
		 "GL_3DFX_texture_compression_FXT1"},
	},
	{
		ext_texture_compression_s3tc,
		ARRAY_SIZE(ext_texture_compression_s3tc),
		"GL_EXT_texture_compression_s3tc",
		GL_UNSIGNED_NORMALIZED,
		{"GL_ARB_texture_compression",
		 "GL_EXT_texture_compression_s3tc"},
	},
	{
		ext_texture_compression_rgtc,
		ARRAY_SIZE(ext_texture_compression_rgtc),
		"GL_EXT_texture_compression_rgtc",
		GL_UNSIGNED_NORMALIZED,
		{"GL_EXT_texture_compression_rgtc"}
	},
	{
		ext_texture_compression_latc,
		ARRAY_SIZE(ext_texture_compression_latc),
		"GL_EXT_texture_compression_latc",
		GL_UNSIGNED_NORMALIZED,
		{"GL_EXT_texture_compression_latc"}
	}
};


/**
 * XXX add this to piglit-util if useful elsewhere.
 */
static GLvoid
piglit_draw_rect_tex3d(float x, float y, float w, float h,
		       float tx, float ty, float tw, float th,
		       float tz0, float tz1)
{
	float verts[4][4];
	float tex[4][3];

	verts[0][0] = x;
	verts[0][1] = y;
	verts[0][2] = 0.0;
	verts[0][3] = 1.0;
	tex[0][0] = tx;
	tex[0][1] = ty;
	tex[0][2] = tz0;
	verts[1][0] = x + w;
	verts[1][1] = y;
	verts[1][2] = 0.0;
	verts[1][3] = 1.0;
	tex[1][0] = tx + tw;
	tex[1][1] = ty;
	tex[1][2] = tz1;
	verts[2][0] = x + w;
	verts[2][1] = y + h;
	verts[2][2] = 0.0;
	verts[2][3] = 1.0;
	tex[2][0] = tx + tw;
	tex[2][1] = ty + th;
	tex[2][2] = tz1;
	verts[3][0] = x;
	verts[3][1] = y + h;
	verts[3][2] = 0.0;
	verts[3][3] = 1.0;
	tex[3][0] = tx;
	tex[3][1] = ty + th;
	tex[3][2] = tz0;

	glVertexPointer(4, GL_FLOAT, 0, verts);
	glTexCoordPointer(3, GL_FLOAT, 0, tex);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glDrawArrays(GL_QUADS, 0, 4);

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}


static GLboolean
equal_images(const GLubyte *img1, const GLubyte *img2,
             GLuint w, GLuint h)
{
	return memcmp(img1, img2, w*h*4) == 0;
}


/**
 * Get block size for compressed format.
 * \return GL_TRUE if format is compressed, GL_FALSE otherwise
 * XXX this could be a piglit util function if useful elsewhere.
 */
static GLboolean
get_format_block_size(GLenum format, GLuint *bw, GLuint *bh)
{
	switch (format) {
	case GL_COMPRESSED_RGB_FXT1_3DFX:
	case GL_COMPRESSED_RGBA_FXT1_3DFX:
		*bw = 8;
		*bh = 4;
		return GL_TRUE;
	case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
	case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
	case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
	case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
	case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
	case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
	case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
	case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
		*bw = 4;
		*bh = 4;
		return GL_TRUE;
	case GL_COMPRESSED_RED:
	case GL_COMPRESSED_RED_RGTC1_EXT:
	case GL_COMPRESSED_RG:
	case GL_COMPRESSED_RED_GREEN_RGTC2_EXT:
		*bw = 4;
		*bh = 4;
		return GL_TRUE;
	case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
	case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
		*bw = 4;
		*bh = 4;
		return GL_TRUE;
	default:
		*bw = *bh = 1;
		return GL_FALSE;
	}
}


/**
 * Create a texture image with reference values.  Draw a textured quad.
 * Save reference image with glReadPixels().
 * Loop:
 *    replace a sub-region of the texture image with same values
 *    draw test textured quad
 *    read test image with glReadPixels
 *    compare reference image to test image
 * \param target  GL_TEXTURE_1D/2D/3D
 * \param intFormat  the internal texture format
 */
static GLboolean
test_format(GLenum target, GLenum intFormat)
{
	const GLenum srcFormat = GL_RGBA;
	GLuint w = 128, h = 64, d = 8;
	GLuint tex, i, j, k, n, t;
	GLubyte *img, *ref, *testImg;
	GLboolean pass = GL_TRUE;
	GLuint bw, bh, wMask, hMask, dMask;
	get_format_block_size(intFormat, &bw, &bh);
	wMask = ~(bw-1);
	hMask = ~(bh-1);
	dMask = ~0;

	if (target != GL_TEXTURE_3D)
		d = 1;
	if (target == GL_TEXTURE_1D)
		h = 1;

	img = (GLubyte *) malloc(w * h * d * 4);
	ref = (GLubyte *) malloc(w * h * d * 4);
	testImg = (GLubyte *) malloc(w * h * d * 4);

	/* fill source tex image */
	n = 0;
	for (i = 0; i < d; i++) {
		for (j = 0; j < h; j++) {
			for (k = 0; k < w; k++) {
				img[n++] = j * 4;
				img[n++] = k * 2;
				img[n++] = i * 16;
				img[n++] = 255;
			}
		}
	}

	glPixelStorei(GL_UNPACK_ROW_LENGTH, w);
	glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, h);

	glGenTextures(1, &tex);
	glBindTexture(target, tex);
	glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
	glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
	if (target == GL_TEXTURE_1D) {
		glTexImage1D(target, 0, intFormat, w, 0,
			     srcFormat, GL_UNSIGNED_BYTE, img);
	}
	else if (target == GL_TEXTURE_2D) {
		glTexImage2D(target, 0, intFormat, w, h, 0,
			     srcFormat, GL_UNSIGNED_BYTE, img);
	}
	else if (target == GL_TEXTURE_3D) {
		glTexImage3D(target, 0, intFormat, w, h, d, 0,
			     srcFormat, GL_UNSIGNED_BYTE, img);
	}

	glEnable(target);

	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

	/* draw reference image */
	glClear(GL_COLOR_BUFFER_BIT);
	piglit_draw_rect_tex3d(0, 0, w, h, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0);
	glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, ref);

	for (t = 0; t < 10; t++) {
		/* Choose random region of texture to update.
		 * Use sizes and positions that are multiples of
		 * the compressed block size.
		 */
		GLint tw = (rand() % w) & wMask;
		GLint th = (rand() % h) & hMask;
		GLint td = (rand() % d) & dMask;
		GLint tx = (rand() % (w - tw)) & wMask;
		GLint ty = (rand() % (h - th)) & hMask;
		GLint tz = (rand() % (d - td)) & dMask;

		assert(tx + tw <= w);
		assert(ty + th <= h);
		assert(tz + td <= d);

		/* replace texture region (with same data) */
		glPixelStorei(GL_UNPACK_SKIP_PIXELS, tx);
		glPixelStorei(GL_UNPACK_SKIP_ROWS, ty);
		glPixelStorei(GL_UNPACK_SKIP_IMAGES, tz);
		if (target == GL_TEXTURE_1D) {
			glTexSubImage1D(target, 0, tx, tw,
					srcFormat, GL_UNSIGNED_BYTE, img);
		}
		else if (target == GL_TEXTURE_2D) {
			glTexSubImage2D(target, 0, tx, ty, tw, th,
					srcFormat, GL_UNSIGNED_BYTE, img);
		}
		else if (target == GL_TEXTURE_2D) {
			glTexSubImage3D(target, 0, tx, ty, tz, tw, th, td,
					srcFormat, GL_UNSIGNED_BYTE, img);
		}

		/* draw test image */
		glClear(GL_COLOR_BUFFER_BIT);
		piglit_draw_rect_tex3d(0, 0, w, h, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0);
		glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, testImg);

		glutSwapBuffers();

		if (!equal_images(ref, testImg, w, h)) {
			printf("texsubimage failed\n");
			printf("  target: 0x%x\n", target);
			printf("  internal format: 0x%x\n", intFormat);
			printf("  region: %d, %d  %d x %d\n", tx, ty, tw, th);
			pass = GL_FALSE;
			break;
		}
	}

	glDisable(target);

	free(img);
	free(ref);
	free(testImg);

	glDeleteTextures(1, &tex);
	return pass;
}


/**
 * Test all formats in texsubimage_test_sets[] for the given
 * texture target.
 */
static GLboolean
test_formats(GLenum target)
{
	GLboolean pass = GL_TRUE;
	int i, j;

	/* loop over the format groups */
	for (i = 0; i < ARRAY_SIZE(texsubimage_test_sets); i++) {
		const struct test_desc *set = &texsubimage_test_sets[i];
		GLboolean skip = GL_FALSE;

		/* only test compressed formats with 2D textures */
		if (i > 0 && target != GL_TEXTURE_2D)
			continue;

		/* skip formats for unsupported extensions */
		for (j = 0; j < ARRAY_SIZE(set->ext); j++) {
			if (set->ext[j] &&
			    !piglit_is_extension_supported(set->ext[j])) {
				/* req'd extension not supported */
				skip = GL_TRUE;
				break;
			}
		}
		if (skip)
			continue;

		/* loop over formats in the set */
		for (j = 0; j < set->num_formats; j++) {
			if (!test_format(target,
					 set->format[j].internalformat)) {
				pass = GL_FALSE;
			}
		}
	}

	return pass;
}


enum piglit_result
piglit_display(void)
{
	static const GLenum targets[] = {
		GL_TEXTURE_1D,
		GL_TEXTURE_2D,
		GL_TEXTURE_3D
	};
	GLboolean pass = GL_TRUE;
	int i;

	/* Loop over 1/2/3D texture targets */
	for (i = 0; i < ARRAY_SIZE(targets); i++) {
		pass = test_formats(targets[i]) && pass;
	}

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}


void
piglit_init(int argc, char **argv)
{
	fbo_formats_init(argc, argv, 0);
	(void) fbo_formats_display;

	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
}