summaryrefslogtreecommitdiff
path: root/tests/texturing/getcompressedteximage.c
blob: fee2eec3b55e5f3d1eb0dd6cfd38e64a8b52863b (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
/*
 * Copyright 2015 Intel Corporation
 *
 * 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.
 */

/** @file getcompressedteximage.c
 *
 * Tests to make sure that glGetCompressedTexImage works correctly.
 * The basic idea is to create some fake compressed data, upload it to the
 * driver, download it, and compare.
 */

#include "piglit-util-gl.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 20;

	config.window_visual = PIGLIT_GL_VISUAL_RGBA |
		PIGLIT_GL_VISUAL_DOUBLE;

PIGLIT_GL_TEST_CONFIG_END

/* Copied from Mesa src/mesa/main/formats.csv:
 * MESA_FORMAT_RGBA_DXT5, s3tc, 4, 4, x128
 */
#define FORMAT GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
#define WIDTH 32
#define HEIGHT 32
#define WIDTH_IN_BLOCKS 8
#define HEIGHT_IN_BLOCKS 8
#define BLOCK_BYTES 16
#define IMAGE_SIZE (WIDTH_IN_BLOCKS * HEIGHT_IN_BLOCKS * BLOCK_BYTES)
#define CHAR_LIMIT 100

static GLubyte *expected;
static bool global_pass = true;

static void
subtest(bool local_result, const char *test_name)
{
	global_pass = local_result && global_pass;
	piglit_report_subtest_result(local_result ? PIGLIT_PASS : PIGLIT_FAIL,
				     test_name);
}

static void
init_random_data(void)
{
	int i;

	expected = malloc(18 * IMAGE_SIZE);
	for (i = 0; i < 18 * IMAGE_SIZE; ++i) {
		expected[i] = (GLubyte) rand();
	}
}

void
piglit_init(int argc, char **argv)
{
	if (piglit_is_extension_supported("GL_EXT_texture_compression_s3tc"))
		piglit_require_extension("GL_EXT_texture_compression_s3tc");
	else
		piglit_require_extension("GL_ANGLE_texture_compression_dxt5");

	srand(0);
	init_random_data();
}

static bool
compare_to_expected(const GLubyte *data, int num_layers)
{
	return memcmp(expected, data, num_layers * IMAGE_SIZE) == 0;
}

static void
traditional_download(GLenum target, int num_layers, GLubyte *data)
{
	int i;

	/* Download it using traditional path */
	if (target == GL_TEXTURE_CUBE_MAP) {
		for (i = 0; i < num_layers; ++i) {
			glGetCompressedTexImage(
				GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0,
				data + i * IMAGE_SIZE);
		}
	}
	else
		glGetCompressedTexImage(target, 0, data);
}

static void
upload_subtest(GLenum target, bool use_pbo)
{
	GLuint tex, pbo_pack, pbo_unpack;
	int num_layers;
	char test_name [CHAR_LIMIT];
	int i;
	GLubyte *data = expected;
	bool pass = true;

	/* Prepare to read data from expected */
	if (use_pbo) {
		glGenBuffers(1, &pbo_unpack);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_unpack);
		glBufferData(GL_PIXEL_UNPACK_BUFFER, 18 * IMAGE_SIZE,
		             expected, GL_STATIC_DRAW);
		pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
		data = NULL;
	}

	/* Upload it using traditional path */
	glGenTextures(1, &tex);
	glBindTexture(target, tex);

	switch (target) {

	case GL_TEXTURE_2D:
		num_layers = 1;
		glCompressedTexImage2D(target, 0, FORMAT, WIDTH, HEIGHT, 0,
				       num_layers * IMAGE_SIZE, data);
		break;
	case GL_TEXTURE_2D_ARRAY:
		num_layers = 3;
		glCompressedTexImage3D(target, 0, FORMAT, WIDTH, HEIGHT,
				       num_layers, 0,
				       num_layers * IMAGE_SIZE, data);
		break;
	case GL_TEXTURE_CUBE_MAP:
		num_layers = 6;
		for (i = 0; i < num_layers; ++i) {
			glCompressedTexImage2D(
				GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, FORMAT,
				WIDTH, HEIGHT, 0, IMAGE_SIZE,
				data + i * IMAGE_SIZE);
		}
		break;
	case GL_TEXTURE_CUBE_MAP_ARRAY:
		num_layers = 18;
		glCompressedTexImage3D(target, 0, FORMAT, WIDTH, HEIGHT,
				       num_layers, 0,
				       num_layers * IMAGE_SIZE, data);
		break;
	default:
		printf("Bad texture target.\n");
		piglit_report_result(PIGLIT_FAIL);

	}
	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

	/* Download it */
	if (use_pbo) {
		/* Prepare PBO */
		GLubyte *junk = malloc(18 * IMAGE_SIZE);
		memset(junk, 123, 18 * IMAGE_SIZE);
		glGenBuffers(1, &pbo_pack);
		glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo_pack);
		glBufferData(GL_PIXEL_PACK_BUFFER, 18 * IMAGE_SIZE,
		             junk, GL_STATIC_READ);
		free(junk);
		pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

		/* Do the transfer */
		traditional_download(target, num_layers, (void *) 0);
		pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

		/* Map the data */
		data = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
		pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	}
	else {
		data = malloc(18 * IMAGE_SIZE);
		traditional_download(target, num_layers, data);
		pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	}

	/* Perform comparison and report the results. */
	snprintf(test_name, CHAR_LIMIT, "upload %s%s",
	         piglit_get_gl_enum_name(target), use_pbo ? " PBO" : "");
	pass = compare_to_expected(data, num_layers) && pass;
	subtest(pass, test_name);

	/* Clean up */
	glDeleteTextures(1, &tex);
	if (use_pbo) {
		glDeleteBuffers(1, &pbo_unpack);
		glDeleteBuffers(1, &pbo_pack);
	}
	else
		free(data);
}

enum piglit_result
piglit_display(void)
{
	/* Non-PBO tests */
	upload_subtest(GL_TEXTURE_2D, false);
	if (piglit_is_extension_supported("GL_EXT_texture_array"))
		upload_subtest(GL_TEXTURE_2D_ARRAY, false);
	upload_subtest(GL_TEXTURE_CUBE_MAP, false);
	if (piglit_is_extension_supported("GL_ARB_texture_cube_map_array"))
		upload_subtest(GL_TEXTURE_CUBE_MAP_ARRAY, false);

	/* PBO tests */
	upload_subtest(GL_TEXTURE_2D, true);
	if (piglit_is_extension_supported("GL_EXT_texture_array"))
		upload_subtest(GL_TEXTURE_2D_ARRAY, true);
	upload_subtest(GL_TEXTURE_CUBE_MAP, true);
	if (piglit_is_extension_supported("GL_ARB_texture_cube_map_array"))
		upload_subtest(GL_TEXTURE_CUBE_MAP_ARRAY, true);

	return global_pass ? PIGLIT_PASS : PIGLIT_FAIL;
}