aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/gl-3.0/texture-integer.c
blob: 1612a36d999e46d2152fcb328ebf86286c2081c8 (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
/*
 * Copyright (c) 2010-2012 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
 * 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 VMWARE 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 OpenGL 3.0 integer textures
 */

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

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.window_width = 100;
	config.window_height = 100;
	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_ALPHA | PIGLIT_GL_VISUAL_DOUBLE;

PIGLIT_GL_TEST_CONFIG_END

static const char *TestName = "texture-integer";
static GLint TexWidth = 16, TexHeight = 16;
static GLint BiasUniform = -1, TexUniform = -1;

struct format_info
{
	const char *Name;
	GLenum IntFormat, BaseFormat;
	GLuint BitsPerChannel;
	GLboolean Signed;
};


static const struct format_info Formats[] = {
	/* { "GL_RGBA", GL_RGBA, GL_RGBA, 8, GL_FALSE },*/
	{ "GL_RGBA8I_EXT",   GL_RGBA8I_EXT,   GL_RGBA_INTEGER_EXT, 8,  GL_TRUE  },
	{ "GL_RGBA8UI_EXT",  GL_RGBA8UI_EXT,  GL_RGBA_INTEGER_EXT, 8,  GL_FALSE },
	{ "GL_RGBA16I_EXT",  GL_RGBA16I_EXT,  GL_RGBA_INTEGER_EXT, 16, GL_TRUE  },
	{ "GL_RGBA16UI_EXT", GL_RGBA16UI_EXT, GL_RGBA_INTEGER_EXT, 16, GL_FALSE },
	{ "GL_RGBA32I_EXT",  GL_RGBA32I_EXT,  GL_RGBA_INTEGER_EXT, 32, GL_TRUE  },
	{ "GL_RGBA32UI_EXT", GL_RGBA32UI_EXT, GL_RGBA_INTEGER_EXT, 32, GL_FALSE },

	{ "GL_RGB8I_EXT",   GL_RGB8I_EXT,   GL_RGB_INTEGER_EXT, 8,  GL_TRUE  },
	{ "GL_RGB8UI_EXT",  GL_RGB8UI_EXT,  GL_RGB_INTEGER_EXT, 8,  GL_FALSE },
	{ "GL_RGB16I_EXT",  GL_RGB16I_EXT,  GL_RGB_INTEGER_EXT, 16, GL_TRUE  },
	{ "GL_RGB16UI_EXT", GL_RGB16UI_EXT, GL_RGB_INTEGER_EXT, 16, GL_FALSE },
	{ "GL_RGB32I_EXT",  GL_RGB32I_EXT,  GL_RGB_INTEGER_EXT, 32, GL_TRUE  },
	{ "GL_RGB32UI_EXT", GL_RGB32UI_EXT, GL_RGB_INTEGER_EXT, 32, GL_FALSE },

	{ "GL_ALPHA8I_EXT",   GL_ALPHA8I_EXT,   GL_ALPHA_INTEGER_EXT, 8,  GL_TRUE  },
	{ "GL_ALPHA8UI_EXT",  GL_ALPHA8UI_EXT,  GL_ALPHA_INTEGER_EXT, 8,  GL_FALSE },
	{ "GL_ALPHA16I_EXT",  GL_ALPHA16I_EXT,  GL_ALPHA_INTEGER_EXT, 16, GL_TRUE  },
	{ "GL_ALPHA16UI_EXT", GL_ALPHA16UI_EXT, GL_ALPHA_INTEGER_EXT, 16, GL_FALSE },
	{ "GL_ALPHA32I_EXT",  GL_ALPHA32I_EXT,  GL_ALPHA_INTEGER_EXT, 32, GL_TRUE  },
	{ "GL_ALPHA32UI_EXT", GL_ALPHA32UI_EXT, GL_ALPHA_INTEGER_EXT, 32, GL_FALSE },

	{ "GL_LUMINANCE8I_EXT",   GL_LUMINANCE8I_EXT,   GL_LUMINANCE_INTEGER_EXT, 8,  GL_TRUE  },
	{ "GL_LUMINANCE8UI_EXT",  GL_LUMINANCE8UI_EXT,  GL_LUMINANCE_INTEGER_EXT, 8,  GL_FALSE },
	{ "GL_LUMINANCE16I_EXT",  GL_LUMINANCE16I_EXT,  GL_LUMINANCE_INTEGER_EXT, 16, GL_TRUE  },
	{ "GL_LUMINANCE16UI_EXT", GL_LUMINANCE16UI_EXT, GL_LUMINANCE_INTEGER_EXT, 16, GL_FALSE },
	{ "GL_LUMINANCE32I_EXT",  GL_LUMINANCE32I_EXT,  GL_LUMINANCE_INTEGER_EXT, 32, GL_TRUE  },
	{ "GL_LUMINANCE32UI_EXT", GL_LUMINANCE32UI_EXT, GL_LUMINANCE_INTEGER_EXT, 32, GL_FALSE },

	{ "GL_LUMINANCE_ALPHA8I_EXT",   GL_LUMINANCE_ALPHA8I_EXT,   GL_LUMINANCE_ALPHA_INTEGER_EXT, 8,  GL_TRUE  },
	{ "GL_LUMINANCE_ALPHA8UI_EXT",  GL_LUMINANCE_ALPHA8UI_EXT,  GL_LUMINANCE_ALPHA_INTEGER_EXT, 8,  GL_FALSE },
	{ "GL_LUMINANCE_ALPHA16I_EXT",  GL_LUMINANCE_ALPHA16I_EXT,  GL_LUMINANCE_ALPHA_INTEGER_EXT, 16, GL_TRUE  },
	{ "GL_LUMINANCE_ALPHA16UI_EXT", GL_LUMINANCE_ALPHA16UI_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT, 16, GL_FALSE },
	{ "GL_LUMINANCE_ALPHA32I_EXT",  GL_LUMINANCE_ALPHA32I_EXT,  GL_LUMINANCE_ALPHA_INTEGER_EXT, 32, GL_TRUE  },
	{ "GL_LUMINANCE_ALPHA32UI_EXT", GL_LUMINANCE_ALPHA32UI_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT, 32, GL_FALSE },

	{ "GL_INTENSITY8I_EXT",   GL_INTENSITY8I_EXT,   GL_RED_INTEGER_EXT, 8,  GL_TRUE  },
	{ "GL_INTENSITY8UI_EXT",  GL_INTENSITY8UI_EXT,  GL_RED_INTEGER_EXT, 8,  GL_FALSE },
	{ "GL_INTENSITY16I_EXT",  GL_INTENSITY16I_EXT,  GL_RED_INTEGER_EXT, 16, GL_TRUE  },
	{ "GL_INTENSITY16UI_EXT", GL_INTENSITY16UI_EXT, GL_RED_INTEGER_EXT, 16, GL_FALSE },
	{ "GL_INTENSITY32I_EXT",  GL_INTENSITY32I_EXT,  GL_RED_INTEGER_EXT, 32, GL_TRUE  },
	{ "GL_INTENSITY32UI_EXT", GL_INTENSITY32UI_EXT, GL_RED_INTEGER_EXT, 32, GL_FALSE },
};


/**
 * The basic idea here is to sample the integer texture and then apply
 * a bias vector to bring all the color components into the [0,1] range.
 * One minor issue is that we can't test the full range of 32-bit integer
 * textures since we cast the int values to floats.  But we test enough of
 * the range to be confident.
 */
static const char *FragShaderText =
	"#version 130 \n"
	"uniform vec4 bias; \n"
	"uniform isampler2D tex; \n"
	"void main() \n"
	"{ \n"
	"   ivec4 t = texture(tex, gl_TexCoord[0].xy); \n"
	"   gl_FragColor = vec4(t) + bias; \n"
	"} \n";


/**
 * Return the max texture value that can be represented with the given
 * integer format.
 */
static int
get_max_val(const struct format_info *info)
{
	int max;

	switch (info->BitsPerChannel) {
	case 8:
		if (info->Signed)
			max = 127;
		else
			max = 255;
		break;
	case 16:
		if (info->Signed)
			max = 32767;
		else
			max = 65535;
		break;
	case 32:
		if (info->Signed)
			max = 10*1000; /* don't use 0x8fffffff to avoid overflow issues */
		else
			max = 20*1000;
		break;
	default:
		assert(!"unexpected bpp");
		max = 0;
	}

	return max;
}


/**
 * Return the number of color components in the given format.
 */
static int
num_components(GLenum format)
{
	switch (format) {
	case GL_RGBA:
	case GL_RGBA_INTEGER_EXT:
		return 4;
	case GL_RGB_INTEGER_EXT:
		return 3;
	case GL_ALPHA_INTEGER_EXT:
		return 1;
	case GL_LUMINANCE_INTEGER_EXT:
		return 1;
	case GL_LUMINANCE_ALPHA_INTEGER_EXT:
		return 2;
	case GL_RED_INTEGER_EXT:
		return 1;
	default:
		assert(!"unexpected format");
		return 0;
	}
}


/**
 * Fill image buffer with given value.
 */
static void
fill_array(int comps, int texels, void *buf, int bpp, const int val[4])
{
	int i, j;

	switch (bpp) {
	case 8:
		{
			GLubyte *b = (GLubyte *) buf;
			for (i = 0; i < texels; i++) {
				for (j = 0; j < comps; j++) {
					b[i * comps + j] = val[j];
				}
			}
		}
		break;
	case 16:
		{
			GLushort *b = (GLushort *) buf;
			for (i = 0; i < texels; i++) {
				for (j = 0; j < comps; j++) {
					b[i * comps + j] = val[j];
				}
			}
		}
		break;
	case 32:
		{
			GLuint *b = (GLuint *) buf;
			for (i = 0; i < texels; i++) {
				for (j = 0; j < comps; j++) {
					b[i * comps + j] = val[j];
				}
			}
		}
		break;
	default:
		assert(!"unexpected bpp");
	}
}


static GLenum
get_datatype(const struct format_info *info)
{
	switch (info->BitsPerChannel) {
	case 8:
		return info->Signed ? GL_BYTE : GL_UNSIGNED_BYTE;
	case 16:
		return info->Signed ? GL_SHORT : GL_UNSIGNED_SHORT;
	case 32:
		return info->Signed ? GL_INT : GL_UNSIGNED_INT;
	default:
		assert(!"unexpected bpp");
		return 0;
	}
}


/** \return GL_TRUE for pass, GL_FALSE for fail */
static bool
test_format(const struct format_info *info)
{
	const int max = get_max_val(info);
	const int comps = num_components(info->BaseFormat);
	const int texels = TexWidth * TexHeight;
	const GLenum type = get_datatype(info);
	const int w = piglit_width / 10;
	const int h = piglit_height / 10;
	const float error = 2.0 / 255.0; /* XXX fix */
	GLfloat expected[4];
	void *buf;
	int value[4];
	GLfloat result[4], bias[4];
	GLint f;

	/* pick random texture color */
	value[0] = rand() % max;
	value[1] = rand() % max;
	value[2] = rand() % max;
	value[3] = rand() % max;

	/* alloc, fill texture image */
	buf = malloc(comps * texels * info->BitsPerChannel / 8);
	fill_array(comps, texels, buf, info->BitsPerChannel, value);

	glTexImage2D(GL_TEXTURE_2D, 0, info->IntFormat, TexWidth, TexHeight, 0,
					 info->BaseFormat, type, buf);

	/* make sure the teximage call worked */
	if (!piglit_check_gl_error(GL_NO_ERROR))
		return false;

	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &f);

	/* setup expected polygon color */
	expected[0] = 0.25;
	expected[1] = 0.50;
	expected[2] = 0.75;
	expected[3] = 1.00;

	/* need to swizzle things depending on texture format */
	switch (info->BaseFormat) {
	case GL_RGBA_INTEGER_EXT:
		/* nothing */
		break;
	case GL_RGB_INTEGER_EXT:
		expected[3] = 0.0;
		break;
	case GL_ALPHA_INTEGER_EXT:
		expected[0] = expected[1] = expected[2] = 0.0;
		expected[3] = 0.25;
		value[3] = value[0];
		break;
	case GL_LUMINANCE_INTEGER_EXT:
		expected[0] = expected[1] = expected[2] = 0.25;
		expected[3] = 1.0;
		value[1] = value[2] = value[0];
		value[3] = 1.0;
		break;
	case GL_LUMINANCE_ALPHA_INTEGER_EXT:
		expected[0] = expected[1] = expected[2] = 0.25;
		value[3] = value[1];
		value[1] = value[2] = value[0];
		break;
	case GL_RED_INTEGER_EXT:
		expected[0] = expected[1] = expected[2] = expected[3] = 0.25;
		value[1] = value[2] = value[3] = value[0];
		break;
	default:
		;
	}

	/* compute, set test bias */
	bias[0] = expected[0] - value[0];
	bias[1] = expected[1] - value[1];
	bias[2] = expected[2] - value[2];
	bias[3] = expected[3] - value[3];
	glUniform4fv(BiasUniform, 1, bias);

	/* draw */
	glClearColor(0, 1, 1, 0);
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_POLYGON);
	glTexCoord2f(0, 0);	glVertex2f(0, 0);
	glTexCoord2f(1, 0);	glVertex2f(w, 0);
	glTexCoord2f(1, 1);	glVertex2f(w, h);
	glTexCoord2f(0, 1);	glVertex2f(0, h);
	glEnd();

	/* test */
	glReadPixels(w/2, h/2, 1, 1, GL_RGBA, GL_FLOAT, result);

	if (fabsf(result[0] - expected[0]) > error ||
		 fabsf(result[1] - expected[1]) > error ||
		 fabsf(result[2] - expected[2]) > error ||
		 fabsf(result[3] - expected[3]) > error) {
		fprintf(stderr, "%s: failure with format %s:\n", TestName, info->Name);
		fprintf(stderr, "  texture color = %d, %d, %d, %d\n",
				  value[0], value[1], value[2], value[3]);
		fprintf(stderr, "  expected color = %g, %g, %g, %g\n",
				  expected[0], expected[1], expected[2], expected[3]);
		fprintf(stderr, "  result color = %g, %g, %g, %g\n",
				  result[0], result[1], result[2], result[3]);
		return false;
	}

	piglit_present_results();

	free(buf);

	return GL_TRUE;
}


enum piglit_result
piglit_display(void)
{
	int f, i;

	for (f = 0; f < ARRAY_SIZE(Formats); f++) {
		for (i = 0; i < 5; i++) {
			if (!test_format(&Formats[f]))
				return PIGLIT_FAIL;
		}
	}

	return PIGLIT_PASS;
}


void
piglit_init(int argc, char **argv)
{
	GLuint fragShader, program;
        static GLuint tex;

	piglit_require_gl_version(30);
	piglit_require_GLSL_version(130);

	fragShader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, FragShaderText);

	program = piglit_link_simple_program(0, fragShader);
	glUseProgram(program);

	BiasUniform = glGetUniformLocation(program, "bias");
	TexUniform = glGetUniformLocation(program, "tex");

	glUniform1i(TexUniform, 0);  /* tex unit zero */

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
}