summaryrefslogtreecommitdiff
path: root/tests/spec/glsl-1.10/execution/varying-packing/simple.c
blob: cfa288fa57c545e7d42d1f5515506715cd77ea45 (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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
 * Copyright © 2012 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 simple.c
 *
 * This file checks that simple cases of varying packing work
 * correctly.  Specifically, it tests that for each basic type allowed
 * in varyings, it is possible to create a shader with the maximum
 * possible number of that type of varying (determined by the
 * implementation's reported value of GL_MAX_VARYING_FLOATS).  If the
 * size of the basic type being tested does not evenly divide
 * GL_MAX_VARYING_FLOATS, the remaining varyings are taken up by
 * individual floats.
 *
 * The test may be run in two modes: "array" mode, in which the test
 * uses a single varying whose type is an array (e.g. mat3[7]), and
 * "separate" mode, in which the test uses separate individual
 * varyings of the given type.
 *
 * The test operates by first querying the implementation's value of
 * GL_MAX_VARYING_FLOATS, then creating a vertex and fragment shader
 * that use up all possible varying components.  The vertex shader
 * fills the varying components with consecutive integer values (where
 * the starting value is determined by a uniform), and the fragment
 * shader checks that all of the varying components were received
 * correctly.  The shaders are compiled and run, to ensure that the
 * implementation not only claims to be able to pack the varyings, but
 * actually packs them correctly too.
 *
 * For example, on an implementation where GL_MAX_VARYING_FLOATS is
 * 64, when testing the mat3 type in "array" mode, the vertex shader
 * looks like this:
 *
 *   #version 110
 *   uniform int i;
 *   varying mat3 var0[7];
 *   varying float var1;
 *
 *   void main()
 *   {
 *     gl_Position = gl_Vertex;
 *     var0[0][0][0] = float(i + 0);
 *     var0[0][0][1] = float(i + 1);
 *     var0[0][0][2] = float(i + 2);
 *     var0[0][1][0] = float(i + 3);
 *     ...
 *     var0[6][2][1] = float(i + 61);
 *     var0[6][2][2] = float(i + 62);
 *     var1 = float(i + 63);
 *   }
 *
 * And the fragment shader looks like this:
 *
 *   #version 110
 *   uniform int i;
 *   varying mat3 var0[7];
 *   varying float var1;
 *   
 *   void main()
 *   {
 *     bool failed = false;
 *     if (var0[0][0][0] != float(i + 0))
 *       failed = true;
 *     if (var0[0][0][1] != float(i + 1))
 *       failed = true;
 *     ...
 *     if (var0[6][2][2] != float(i + 62))
 *       failed = true;
 *     if (var1 != float(i + 63))
 *       failed = true;
 *     if (failed)
 *       gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
 *     else
 *       gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
 *   }
 */
#include "piglit-util-gl.h"

static void
parse_args(int argc, char *argv[], struct piglit_gl_test_config *config);

static const int outer_dim_size = 2;

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
	config.khr_no_error_support = PIGLIT_NO_ERRORS;
	parse_args(argc, argv, &config);

PIGLIT_GL_TEST_CONFIG_END

static GLuint prog;
static GLint i_location;

enum base_type
{
	BASE_TYPE_FLOAT,
	BASE_TYPE_UINT,
	BASE_TYPE_INT,
	BASE_TYPE_DOUBLE,
};

enum test_array_type
{
	SEPARATE,
	ARRAY,
	ARRAYS_OF_ARRAYS,
};

static const char *
get_base_type_name(enum base_type t)
{
	switch (t) {
	case BASE_TYPE_FLOAT: return "float";
	case BASE_TYPE_UINT:  return "uint";
	case BASE_TYPE_INT:   return "int";
	case BASE_TYPE_DOUBLE:return "double";
	default:              return "???";
	}
}

struct type_desc
{
	const char *name;
	enum base_type base;
	unsigned num_cols;
	unsigned num_rows;
	unsigned glsl_version_required;
};

struct type_desc int_type     = { "int",     BASE_TYPE_INT,    1, 1, 130 };
struct type_desc uint_type    = { "uint",    BASE_TYPE_UINT,   1, 1, 130 };
struct type_desc float_type   = { "float",   BASE_TYPE_FLOAT,  1, 1, 110 };
struct type_desc double_type  = { "double",  BASE_TYPE_DOUBLE, 1, 1, 150 };
struct type_desc vec2_type    = { "vec2",    BASE_TYPE_FLOAT,  1, 2, 110 };
struct type_desc vec3_type    = { "vec3",    BASE_TYPE_FLOAT,  1, 3, 110 };
struct type_desc vec4_type    = { "vec4",    BASE_TYPE_FLOAT,  1, 4, 110 };
struct type_desc ivec2_type   = { "ivec2",   BASE_TYPE_INT,    1, 2, 130 };
struct type_desc ivec3_type   = { "ivec3",   BASE_TYPE_INT,    1, 3, 130 };
struct type_desc ivec4_type   = { "ivec4",   BASE_TYPE_INT,    1, 4, 130 };
struct type_desc uvec2_type   = { "uvec2",   BASE_TYPE_UINT,   1, 2, 130 };
struct type_desc uvec3_type   = { "uvec3",   BASE_TYPE_UINT,   1, 3, 130 };
struct type_desc uvec4_type   = { "uvec4",   BASE_TYPE_UINT,   1, 4, 130 };
struct type_desc dvec2_type   = { "dvec2",   BASE_TYPE_DOUBLE, 1, 2, 150 };
struct type_desc dvec3_type   = { "dvec3",   BASE_TYPE_DOUBLE, 1, 3, 150 };
struct type_desc dvec4_type   = { "dvec4",   BASE_TYPE_DOUBLE, 1, 4, 150 };
struct type_desc mat2_type    = { "mat2",    BASE_TYPE_FLOAT,  2, 2, 110 };
struct type_desc mat3_type    = { "mat3",    BASE_TYPE_FLOAT,  3, 3, 110 };
struct type_desc mat4_type    = { "mat4",    BASE_TYPE_FLOAT,  4, 4, 110 };
struct type_desc mat2x3_type  = { "mat2x3",  BASE_TYPE_FLOAT,  2, 3, 120 };
struct type_desc mat2x4_type  = { "mat2x4",  BASE_TYPE_FLOAT,  2, 4, 120 };
struct type_desc mat3x2_type  = { "mat3x2",  BASE_TYPE_FLOAT,  3, 2, 120 };
struct type_desc mat3x4_type  = { "mat3x4",  BASE_TYPE_FLOAT,  3, 4, 120 };
struct type_desc mat4x2_type  = { "mat4x2",  BASE_TYPE_FLOAT,  4, 2, 120 };
struct type_desc mat4x3_type  = { "mat4x3",  BASE_TYPE_FLOAT,  4, 3, 120 };
struct type_desc dmat2_type   = { "dmat2",   BASE_TYPE_DOUBLE, 2, 2, 150 };
struct type_desc dmat3_type   = { "dmat3",   BASE_TYPE_DOUBLE, 3, 3, 150 };
struct type_desc dmat4_type   = { "dmat4",   BASE_TYPE_DOUBLE, 4, 4, 150 };
struct type_desc dmat2x3_type = { "dmat2x3", BASE_TYPE_DOUBLE, 2, 3, 150 };
struct type_desc dmat2x4_type = { "dmat2x4", BASE_TYPE_DOUBLE, 2, 4, 150 };
struct type_desc dmat3x2_type = { "dmat3x2", BASE_TYPE_DOUBLE, 3, 2, 150 };
struct type_desc dmat3x4_type = { "dmat3x4", BASE_TYPE_DOUBLE, 3, 4, 150 };
struct type_desc dmat4x2_type = { "dmat4x2", BASE_TYPE_DOUBLE, 4, 2, 150 };
struct type_desc dmat4x3_type = { "dmat4x3", BASE_TYPE_DOUBLE, 4, 3, 150 };

const struct type_desc *all_types[] = {
	&int_type,
	&uint_type,
	&float_type,
	&double_type,
	&vec2_type,
	&vec3_type,
	&vec4_type,
	&ivec2_type,
	&ivec3_type,
	&ivec4_type,
	&uvec2_type,
	&uvec3_type,
	&uvec4_type,
	&dvec2_type,
	&dvec3_type,
	&dvec4_type,
	&mat2_type,
	&mat3_type,
	&mat4_type,
	&mat2x3_type,
	&mat2x4_type,
	&mat3x2_type,
	&mat3x4_type,
	&mat4x2_type,
	&mat4x3_type,
	&dmat2_type,
	&dmat3_type,
	&dmat4_type,
	&dmat2x3_type,
	&dmat2x4_type,
	&dmat3x2_type,
	&dmat3x4_type,
	&dmat4x2_type,
	&dmat4x3_type,
	NULL,
};

/**
 * Type used to communicate to get_shader() the set of varyings to
 * test.
 */
struct varying_desc
{
	const struct type_desc *type;
	unsigned one_dim_array_elems;
	unsigned two_dim_array_elems;
};

/**
 * Generate a vertex or fragment shader to test the given set of
 * varyings.
 */
static GLint
get_shader(bool is_vs, unsigned glsl_version, int num_varyings,
	   struct varying_desc *varyings, enum test_array_type array_type)
{
	GLuint shader;
	char *full_text = malloc(1000 * 100 + num_varyings);
	char *text = full_text;
	unsigned i, j, k, l, m;
	const char *varying_keyword;
	unsigned offset = 0;
	GLenum shader_type = is_vs ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER;
	bool fp64 = false;

	if (glsl_version >= 130) {
		if (is_vs)
			varying_keyword = "out";
		else
			varying_keyword = "in";
	} else {
		varying_keyword = "varying";
	}

	text += sprintf(text, "#version %u\n", glsl_version);
	if (array_type == ARRAYS_OF_ARRAYS)
		text += sprintf(text, "#extension GL_ARB_arrays_of_arrays: enable\n");
	for (i = 0; i < num_varyings; ++i) {
		const char *opt_flat_keyword = "";
		if (!fp64 && varyings[i].type->base == BASE_TYPE_DOUBLE) {
			text += sprintf(text, "#extension GL_ARB_gpu_shader_fp64: enable\n");
			fp64 = true;
		}
		if (varyings[i].type->base != BASE_TYPE_FLOAT)
			opt_flat_keyword = "flat ";
		if (varyings[i].two_dim_array_elems != 0) {
			text += sprintf(text, "%s%s %s var%03u[%u][%u];\n",
					opt_flat_keyword, varying_keyword,
					varyings[i].type->name, i,
					outer_dim_size,
					varyings[i].two_dim_array_elems);
		} else if (varyings[i].one_dim_array_elems != 0) {
			text += sprintf(text, "%s%s %s var%03u[%u];\n",
					opt_flat_keyword, varying_keyword,
					varyings[i].type->name, i,
					varyings[i].one_dim_array_elems);
		} else {
			text += sprintf(text, "%s%s %s var%03u;\n",
					opt_flat_keyword, varying_keyword,
					varyings[i].type->name, i);
		}
	}
	if (glsl_version >= 150 && is_vs) {
		text += sprintf(text, "in vec4 piglit_vertex;\n");
		text += sprintf(text, "#define gl_Vertex piglit_vertex\n");
	}
	text += sprintf(text, "uniform int i;\n");
	text += sprintf(text,
			"\n"
			"void main()\n"
			"{\n");
	if (is_vs)
		text += sprintf(text, "  gl_Position = gl_Vertex;\n");
	else
		text += sprintf(text, "  bool failed = false;\n");
	for (i = 0; i < num_varyings; ++i) {
		unsigned array_loop_bound;
		unsigned outer_array_loop_bound = 1;
		const char *base_type_name
			= get_base_type_name(varyings[i].type->base);
		if (varyings[i].two_dim_array_elems != 0) {
			outer_array_loop_bound = outer_dim_size;
			array_loop_bound = varyings[i].two_dim_array_elems;
		} else {
			array_loop_bound = varyings[i].one_dim_array_elems;
		}
		if (array_loop_bound == 0)
			array_loop_bound = 1;
		for (j = 0; j < outer_array_loop_bound; ++j) {
			for (k = 0; k < array_loop_bound; ++k) {
				for (l = 0; l < varyings[i].type->num_cols; ++l) {
					for (m = 0; m < varyings[i].type->num_rows; ++m) {
						text += sprintf(text, "  ");
						if (!is_vs)
							text += sprintf(text, "failed = failed || ");
						text += sprintf(text, "var%03u", i);
						if (varyings[i].two_dim_array_elems)
							text += sprintf(text, "[%u]", j);
						if (varyings[i].one_dim_array_elems || varyings[i].two_dim_array_elems)
							text += sprintf(text, "[%u]", k);
						if (varyings[i].type->num_cols > 1)
							text += sprintf(text, "[%u]", l);
						if (varyings[i].type->num_rows > 1)
							text += sprintf(text, "[%u]", m);
						if (is_vs)
							text += sprintf(text, " = ");
						else
							text += sprintf(text, " != ");
						text += sprintf(text, "%s(i + %u);\n",
								base_type_name,
								offset++);
					}
				}
			}
		}
	}
	if (!is_vs) {
		text += sprintf(text,
				"  if (failed)\n"
				"    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
				"  else\n"
				"    gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n");
	}
	text += sprintf(text, "}\n");

	shader = piglit_compile_shader_text(shader_type, full_text);

	free(full_text);

	return shader;
}

/**
 * Choose the set of varyings necessary to properly run the given test
 * configuration, given the implementation's reported value of
 * max_varying_floats.
 */
static unsigned
choose_varyings(struct varying_desc *varyings,
		const struct type_desc *test_type,
		enum test_array_type array_type,
		unsigned max_varying_floats)
{
	unsigned num_varyings = 0;
	unsigned element_size = test_type->base == BASE_TYPE_DOUBLE ? 2 : 1;
	unsigned components_in_test_type
		= test_type->num_cols * test_type->num_rows * element_size;
	unsigned num_test_varyings
		= max_varying_floats / components_in_test_type;
	unsigned num_two_dim_test_varyings
		 = num_test_varyings / outer_dim_size;
	unsigned num_extra_arrays = 0;
	unsigned num_extra_varyings
		= (max_varying_floats -
		   num_test_varyings * components_in_test_type) / element_size;
	unsigned i;
	if (array_type == ARRAYS_OF_ARRAYS) {
		varyings[num_varyings].type = test_type;
		varyings[num_varyings].two_dim_array_elems = num_two_dim_test_varyings;
		varyings[num_varyings].one_dim_array_elems = 0;
		num_extra_arrays
			= num_test_varyings - (num_two_dim_test_varyings * outer_dim_size);
		++num_varyings;
		if (num_extra_arrays > 0) {
			varyings[num_varyings].type = test_type;
			varyings[num_varyings].two_dim_array_elems = 0;
			varyings[num_varyings].one_dim_array_elems = num_extra_arrays;
			++num_varyings;
		}
	} else if (array_type == ARRAY) {
		varyings[num_varyings].type = test_type;
		varyings[num_varyings].two_dim_array_elems = 0;
		varyings[num_varyings].one_dim_array_elems = num_test_varyings;
		++num_varyings;
	} else {
		for (i = 0; i < num_test_varyings; ++i) {
			varyings[num_varyings].type = test_type;
			varyings[num_varyings].two_dim_array_elems = 0;
			varyings[num_varyings].one_dim_array_elems = 0;
			++num_varyings;
		}
	}
	for (i = 0; i < num_extra_varyings; ++i) {
		switch(test_type->base) {
		case BASE_TYPE_UINT:
			varyings[num_varyings].type = &uint_type;
			break;
		case BASE_TYPE_INT:
			varyings[num_varyings].type = &int_type;
			break;
		case BASE_TYPE_FLOAT:
			varyings[num_varyings].type = &float_type;
			break;
		case BASE_TYPE_DOUBLE:
			varyings[num_varyings].type = &double_type;
			break;
		}
		varyings[num_varyings].two_dim_array_elems = 0;
		varyings[num_varyings].one_dim_array_elems = 0;
		++num_varyings;
	}

	return num_varyings;
}

void
NORETURN print_usage_and_exit(const char *prog_name)
{
	unsigned i;
	printf("Usage: %s <type> <arrayspec>\n"
	       "  where <type> is one of:\n", prog_name);
	for (i = 0; all_types[i]; ++i)
		printf("    %s\n", all_types[i]->name);
	printf("  and <arrayspec> is one of:\n"
	       "    array: test using an array of the above type\n"
	       "    arrays_of_arrays: test using a multidimensional array"
	       " of the above type\n"
	       "    separate: test using separately declared varyings\n");
	piglit_report_result(PIGLIT_FAIL);
}

static const struct type_desc *test_type;

static void
parse_args(int argc, char *argv[], struct piglit_gl_test_config *config)
{
	unsigned i;

	if (argc < 3)
		print_usage_and_exit(argv[0]);
	for (i = 0; all_types[i]; ++i) {
		if (strcmp(argv[1], all_types[i]->name) == 0)
			break;
	}
	if (all_types[i])
		test_type = all_types[i];
	else
		print_usage_and_exit(argv[0]);

	if (test_type->glsl_version_required <= 110)
		config->supports_gl_compat_version = 20;
	else if (test_type->glsl_version_required <= 120)
		config->supports_gl_compat_version = 21;
	else if (test_type->glsl_version_required <= 130)
		config->supports_gl_compat_version = 30;
	else if (test_type->glsl_version_required <= 150)
		config->supports_gl_core_version = 32;
	else
		piglit_report_result(PIGLIT_FAIL);
}

void
piglit_init(int argc, char **argv)
{
	enum test_array_type array_type;
	GLint max_varying_floats;
	struct varying_desc *varyings;
	unsigned num_varyings;
	unsigned glsl_version;
	GLuint vs, fs;

	if (argc != 3)
		print_usage_and_exit(argv[0]);

	glsl_version = test_type->glsl_version_required;

	if (strcmp(argv[2], "array") == 0)
		array_type = ARRAY;
	else if (strcmp(argv[2], "separate") == 0)
		array_type = SEPARATE;
	else if (strcmp(argv[2], "arrays_of_arrays") == 0) {
		array_type = ARRAYS_OF_ARRAYS;
		piglit_require_extension("GL_ARB_arrays_of_arrays");
		if (glsl_version < 120)
			glsl_version = 120;
	} else
		print_usage_and_exit(argv[0]);

	if (test_type->base == BASE_TYPE_DOUBLE)
		piglit_require_extension("GL_ARB_gpu_shader_fp64");
	glGetIntegerv(GL_MAX_VARYING_FLOATS, &max_varying_floats);

	varyings = malloc(sizeof(*varyings) * max_varying_floats);
	num_varyings = choose_varyings(varyings, test_type,
				       array_type, max_varying_floats);

	vs = get_shader(true, test_type->glsl_version_required,
			num_varyings, varyings, array_type);
	fs = get_shader(false, test_type->glsl_version_required,
			num_varyings, varyings, array_type);
	prog = piglit_link_simple_program(vs, fs);
	i_location = glGetUniformLocation(prog, "i");
	free(varyings);
}

enum piglit_result
piglit_display(void)
{
	GLboolean pass = GL_TRUE;
	GLuint vao;
	float green[] = { 0.0, 1.0, 0.0, 1.0 };

	glClear(GL_COLOR_BUFFER_BIT);
	glUseProgram(prog);
	glUniform1i(i_location, 0);
	if (piglit_is_core_profile) {
		glGenVertexArrays(1, &vao);
		glBindVertexArray(vao);
	}
	piglit_draw_rect(-1, -1, 2, 2);
	pass = piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height, green)
		&& pass;

	piglit_present_results();

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}