summaryrefslogtreecommitdiff
path: root/tests/spec/glsl-1.50/execution/geometry/gs-max-output.cpp
blob: b29ac12f18b93d0ab98ec14de43caf55c59cec3d (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
545
546
547
548
549
550
551
/*
 * Copyright (c) 2018 Advanced Micro Devices
 *
 * 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 gs-max-output.cpp
 *
 * Stress the limits of what a geometry shader can output using a generic
 * geometry shader with points as input and output primitives, allowing
 * arbitrary:
 * - number of input instances (instanced draws)
 * - number of input points per instance
 * - number of invocations (GS instances)
 * - number of output vertices per invocation
 * - number of output components per vertex
 */

#include "piglit-util-gl.h"

#include <map>
#include <vector>

#define WINDOW_SIZE 256

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 32;
	config.supports_gl_core_version = 32;
	config.window_width = WINDOW_SIZE;
	config.window_height = WINDOW_SIZE;
	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
	config.khr_no_error_support = PIGLIT_NO_ERRORS;

PIGLIT_GL_TEST_CONFIG_END

#define PASTE(x) #x
#define STR(x) PASTE(x)

struct testcase {
	unsigned num_instances; /* draw instances */
	unsigned num_points; /* draw size / count */
	unsigned num_invocations; /* GS invocations / instances */
	unsigned num_outputs; /* # vertex ouput per GS invocation */
	unsigned num_components; /* # extra components per GS output vertex */


};

struct fragmentshaderkey {
	unsigned num_components;

	bool operator<(const fragmentshaderkey &o) const {
		return num_components < o.num_components;
	}
};

struct geometryshaderkey {
	unsigned num_invocations;
	unsigned num_outputs;
	unsigned num_components;

	bool operator<(const geometryshaderkey &o) const {
		if (num_invocations < o.num_invocations)
			return true;
		if (num_invocations > o.num_invocations)
			return false;
		if (num_outputs < o.num_outputs)
			return true;
		if (num_outputs > o.num_outputs)
			return false;
		return num_components < o.num_components;
	}
};

static std::map<fragmentshaderkey, GLuint> fragmentshaders;
static std::map<geometryshaderkey, GLuint> testprograms;

static const struct testcase default_testcase = {
	.num_instances = 1,
	.num_points = 1,
	.num_invocations = 1,
	.num_outputs = 1,
	.num_components = 0,
};

static GLuint vs_shader;
static GLuint vao;
static std::vector<testcase> testcases;
static GLuint max_gs_invocations;
static GLuint max_gs_out_vertices;
static GLuint max_gs_total_out_components;
static GLuint max_gs_out_components;
static unsigned max_gs_out_vertices_real;

static const char vs_text[] =
	"#version 150\n"
	"\n"
	"uniform int u_verts_per_instance;\n"
	"\n"
	"out int vs_gs_id;\n"
	"\n"
	"void main() {\n"
	"  vs_gs_id = gl_InstanceID * u_verts_per_instance + gl_VertexID;\n"
	"}\n";

/* Those numbers really don't matter much for what we're trying to do here. */
#define GEN_SEQUENCE \
	"int seq_next(int x) {\n" \
	"  x = (x + 1) * 709900053;\n" \
	"  x = x ^ (x >> 17);\n" \
	"  return x;\n" \
	"}\n"

static const char gs_text[] =
	"#version 150\n"
	"#extension GL_ARB_gpu_shader5 : require\n"
	"\n"
	"#define NUM_INVOCATIONS %d\n"
	"#define NUM_OUT_VERTICES %d\n"
	"#define NUM_PAYLOAD_COMPONENTS %d\n"
	"\n"
	"layout(points, invocations = NUM_INVOCATIONS) in;\n"
	"layout(points, max_vertices = NUM_OUT_VERTICES) out;\n"
	"\n"
	"in int vs_gs_id[];\n"
	"#if NUM_PAYLOAD_COMPONENTS\n"
	"flat out int gs_ps_data[NUM_PAYLOAD_COMPONENTS];\n"
	"#endif\n"
	"\n"
	GEN_SEQUENCE
	"\n"
	"void main() {\n"
	"  for (int i = 0; i < NUM_OUT_VERTICES; ++i) {\n"
	"    int id = (vs_gs_id[0] * NUM_INVOCATIONS + gl_InvocationID) * NUM_OUT_VERTICES + i;\n"
	"    int x = id %% " STR(WINDOW_SIZE) ";\n"
	"    int y = id / " STR(WINDOW_SIZE) ";\n"
	"    gl_Position.x = (float(x) + 0.5) / " STR(WINDOW_SIZE) " * 2.0 - 1.0;\n"
	"    gl_Position.y = (float(y) + 0.5) / " STR(WINDOW_SIZE) " * 2.0 - 1.0;\n"
	"    gl_Position.z = 0.0;\n"
	"    gl_Position.w = 1.0;\n"
	"\n"
	"#if NUM_PAYLOAD_COMPONENTS\n"
	"    int val = id;\n"
	"    for (int j = 0; j < NUM_PAYLOAD_COMPONENTS; ++j) {\n"
	"      gs_ps_data[j] = val;\n"
	"      val = seq_next(val);\n"
	"    }\n"
	"#endif\n"
	"\n"
	"    EmitVertex();\n"
	"  }\n"
	"}\n";

static const char fs_text[] =
	"#version 150\n"
	"\n"
	"#define NUM_PAYLOAD_COMPONENTS %d\n"
	"\n"
	"#if NUM_PAYLOAD_COMPONENTS\n"
	"flat in int gs_ps_data[NUM_PAYLOAD_COMPONENTS];\n"
	"#endif\n"
	"out vec4 out_color;\n"
	"\n"
	GEN_SEQUENCE
	"\n"
	"void main() {\n"
	"#if NUM_PAYLOAD_COMPONENTS\n"
	"  int id = int(gl_FragCoord.y) * " STR(WINDOW_SIZE) " + int(gl_FragCoord.x);\n"
	"  int val = id;\n"
	"  for (int j = 0; j < NUM_PAYLOAD_COMPONENTS; ++j) {\n"
	"    if (val != gs_ps_data[j]) {\n"
	"      out_color.x = 1.0;\n"
	"      out_color.y = float(j) / (NUM_PAYLOAD_COMPONENTS - 1);\n"
	"      out_color.z = float(val & 0xff) / 255;\n"
	"      out_color.w = float(gs_ps_data[j] & 0xff) / 255;\n"
	"      return;\n"
	"    }\n"
	"    val = seq_next(val);\n"
	"  }\n"
	"#endif\n"
	"  out_color = vec4(0, 1, 0, 1);\n"
	"}\n";

static void
print_testcase(const struct testcase *tc)
{
	printf("Case: instances = %u points = %u invocations = %u "
	       "outputs = %u components = %u\n",
	       tc->num_instances, tc->num_points, tc->num_invocations,
	       tc->num_outputs, tc->num_components);
}

static void
add_testcase(const struct testcase *tc)
{
	if (tc->num_instances > 64 * 1024 ||
	    tc->num_points > 64 * 1024 ||
	    tc->num_invocations > 64 * 1024 ||
	    tc->num_outputs > 64 * 1024 ||
	    tc->num_components > 64 * 1024) {
		fprintf(stderr, "Excessive test case size. Are you sure?\n");
		print_testcase(tc);
		exit(1);
	}

	/* Check against implementation-defined limits. */
	if (tc->num_outputs > (unsigned)max_gs_out_vertices) {
		fprintf(stderr, "Too many output vertices (max: %d)\n",
			max_gs_out_vertices);
		print_testcase(tc);
		exit(1);
	}
	if (tc->num_outputs * (4 + tc->num_components) > (unsigned)max_gs_total_out_components) {
		fprintf(stderr, "Too many output components (max: %d)\n",
			max_gs_total_out_components);
		print_testcase(tc);
		exit(1);
	}
	if (tc->num_invocations > (unsigned)max_gs_invocations) {
		fprintf(stderr, "Too many GS invocations (max: %d)\n",
			max_gs_invocations);
		print_testcase(tc);
		exit(1);
	}

	/* Compile GS shader and link program */
	geometryshaderkey gskey;
	gskey.num_invocations = tc->num_invocations;
	gskey.num_outputs = tc->num_outputs;
	gskey.num_components = tc->num_components;
	if (testprograms.find(gskey) == testprograms.end()) {
		char *text;

		fragmentshaderkey fskey;
		fskey.num_components = tc->num_components;
		std::map<fragmentshaderkey, GLuint>::const_iterator fsit =
			fragmentshaders.find(fskey);
		if (fsit == fragmentshaders.end()) {
			if (asprintf(&text, fs_text, tc->num_components) < 0)
				abort();
			GLuint fs_shader =
				piglit_compile_shader_text(GL_FRAGMENT_SHADER, text);
			free(text);

			fsit = fragmentshaders.insert(std::make_pair(fskey, fs_shader)).first;
		}

		if (asprintf(&text, gs_text, tc->num_invocations, tc->num_outputs,
			tc->num_components) < 0)
			abort();
		GLuint gs_shader =
			piglit_compile_shader_text(GL_GEOMETRY_SHADER, text);
		free(text);

		GLuint prog =  glCreateProgram();
		glAttachShader(prog, vs_shader);
		glAttachShader(prog, gs_shader);
		glAttachShader(prog, fsit->second);
		glLinkProgram(prog);
		if (!piglit_link_check_status(prog))
			piglit_report_result(PIGLIT_FAIL);

		glDeleteShader(gs_shader);

		testprograms.insert(std::make_pair(gskey, prog));
	}

	testcases.push_back(*tc);
}

static bool
run_testcase(const struct testcase *tc)
{
	print_testcase(tc);

	glClearColor(0, 0, 0, 1);
	glClear(GL_COLOR_BUFFER_BIT);

	geometryshaderkey gskey;
	gskey.num_invocations = tc->num_invocations;
	gskey.num_outputs = tc->num_outputs;
	gskey.num_components = tc->num_components;
	std::map<geometryshaderkey, GLuint>::const_iterator  progit =
		testprograms.find(gskey);
	assert(progit != testprograms.end());

	glUseProgram(progit->second);
	glUniform1i(glGetUniformLocation(progit->second, "u_verts_per_instance"),
		    tc->num_points);

	glDrawArraysInstanced(GL_POINTS, 0, tc->num_points, tc->num_instances);

	float expected[WINDOW_SIZE * WINDOW_SIZE * 4];
	unsigned num_total =
		tc->num_instances * tc->num_points * tc->num_invocations * tc->num_outputs;
	memset(expected, 0, sizeof(expected));

	for (unsigned i = 0; i < WINDOW_SIZE * WINDOW_SIZE; ++i) {
		if (i < num_total)
			expected[4 * i + 1] = 1.0;
		expected[4 * i + 3] = 1.0;
	}

	return piglit_probe_image_rgba(0, 0, WINDOW_SIZE, WINDOW_SIZE, expected);
}

static void
generate_testcases_max(const testcase &tc, bool explicit_instances, bool explicit_points)
{
	unsigned amplify = tc.num_invocations * tc.num_outputs;
	double target_in_points = double(WINDOW_SIZE * WINDOW_SIZE) / amplify;

	if (!explicit_instances) {
		testcase tc1 = tc;
		tc1.num_instances = MAX2(1, (unsigned)(target_in_points / tc1.num_points));
		add_testcase(&tc1);
	}

	if (!explicit_points) {
		testcase tc1 = tc;
		tc1.num_points = MAX2(1, (unsigned)(target_in_points / tc1.num_instances));
		add_testcase(&tc1);
	}

	if (!explicit_instances && !explicit_points) {
		testcase tc1 = tc;
		tc1.num_instances = MAX2(1, (unsigned)sqrt(target_in_points));
		tc1.num_points = MAX2(1, (unsigned)(target_in_points / tc1.num_instances));
		add_testcase(&tc1);
	}

	if (explicit_instances && explicit_points)
		add_testcase(&tc);
}

void
piglit_init(int argc, char **argv)
{
	bool explicit_instances = false;
	bool explicit_points = false;
	bool explicit_invocations = false;
	bool explicit_outputs = false;
	bool explicit_components = false;
	bool scan_mode = false;
	unsigned scan_seed = 0;
	unsigned scan_count = 0;
	struct testcase explicit_testcase;

	memcpy(&explicit_testcase, &default_testcase, sizeof(explicit_testcase));

	int i;
	for (i = 1; i + 1 < argc; ++i) {
		if (!strcmp(argv[i], "-instances")) {
			explicit_testcase.num_instances = atoi(argv[i + 1]);
			explicit_instances = true;
			i++;
		} else if (!strcmp(argv[i], "-points")) {
			explicit_testcase.num_points = atoi(argv[i + 1]);
			explicit_points = true;
			i++;
		} else if (!strcmp(argv[i], "-invocations")) {
			explicit_testcase.num_invocations = atoi(argv[i + 1]);
			explicit_invocations = true;
			i++;
		} else if (!strcmp(argv[i], "-outputs")) {
			explicit_testcase.num_outputs = atoi(argv[i + 1]);
			explicit_outputs = true;
			i++;
		} else if (!strcmp(argv[i], "-components")) {
			explicit_testcase.num_components = atoi(argv[i + 1]);
			explicit_components = true;
			i++;
		} else if (!strcmp(argv[i], "-scan")) {
			if (i + 2 >= argc) {
				fprintf(stderr, "-scan: too few arguments\n");
				exit(1);
			}
			scan_seed = atoi(argv[i + 1]);
			scan_count = atoi(argv[i + 2]);
			scan_mode = true;
			i += 2;
		} else
			break;
	}
	if (i < argc) {
		fprintf(stderr, "Unknown argument: %s\n", argv[i]);
		exit(1);
	}

	vs_shader = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_text);
	if (!piglit_check_gl_error(GL_NO_ERROR))
		piglit_report_result(PIGLIT_FAIL);

	/* Various other GL objects needed by the test */
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES, (GLint*)&max_gs_out_vertices);
	glGetIntegerv(GL_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS,
		      (GLint*)&max_gs_total_out_components);
	glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_COMPONENTS,
		      (GLint*)&max_gs_out_components);
	glGetIntegerv(GL_MAX_GEOMETRY_SHADER_INVOCATIONS, (GLint*)&max_gs_invocations);
	if (!piglit_check_gl_error(GL_NO_ERROR))
		piglit_report_result(PIGLIT_FAIL);

	max_gs_out_vertices_real = MIN2(max_gs_out_vertices,
					max_gs_total_out_components / 4);

	if (scan_mode) {
		srand(scan_seed);

		/* First, generate test cases that max out each of the dimensions */
		testcase tc0 = explicit_testcase;
		if (!explicit_invocations)
			tc0.num_invocations = max_gs_invocations;

		if (!explicit_outputs) {
			testcase tc1 = tc0;

			if (!explicit_components) {
				tc1.num_outputs = max_gs_out_vertices_real;
				tc1.num_components = MIN2(max_gs_total_out_components / tc1.num_outputs,
							  max_gs_out_components) - 4;
			} else {
				tc1.num_outputs = MIN2(max_gs_total_out_components / (4 + tc1.num_components),
						       max_gs_out_vertices_real);
			}

			generate_testcases_max(tc1, explicit_instances, explicit_points);
		}

		if (!explicit_components) {
			testcase tc1 = tc0;

			if (!explicit_outputs) {
				tc1.num_components = max_gs_out_components - 4;
				tc1.num_outputs = MIN2(max_gs_total_out_components / (4 + tc1.num_components),
						       max_gs_out_vertices_real);
			} else {
				tc1.num_components = MIN2(max_gs_total_out_components / tc1.num_outputs,
							  max_gs_out_components) - 4;
			}

			generate_testcases_max(tc1, explicit_instances, explicit_points);
		}

		if (explicit_outputs && explicit_components)
			generate_testcases_max(tc0, explicit_instances, explicit_points);

		/* Generate additional tests randomly */
		while (testcases.size() < scan_count) {
			testcase tc = explicit_testcase;

			if (!explicit_outputs || !explicit_components) {
				if (explicit_outputs || rand() & 1) {
					unsigned max_components =
						MIN2(max_gs_total_out_components / tc.num_outputs,
						     max_gs_out_components) - 4;
					tc.num_components = rand() % (max_components + 1);

					if (!explicit_outputs) {
						unsigned max_outputs =
							MIN2(max_gs_total_out_components / (4 + tc.num_components),
							     max_gs_out_vertices_real);
						tc.num_outputs = 1 + rand() % max_outputs;
					}
				} else {
					unsigned max_outputs =
						MIN2(max_gs_total_out_components / (4 + tc.num_components),
						     max_gs_out_vertices_real);
					tc.num_outputs = 1 + rand() % max_outputs;

					if (!explicit_components) {
						unsigned max_components =
							MIN2(max_gs_total_out_components / tc.num_outputs,
							      max_gs_out_components) - 4;
						tc.num_components = rand() % (max_components + 1);
					}
				}
			}

			if (!explicit_invocations)
				tc.num_invocations = 1 + rand() % max_gs_invocations;

			unsigned amplify = tc.num_invocations * tc.num_outputs;
			unsigned target_in_points =
				MAX2(1, (WINDOW_SIZE * WINDOW_SIZE + amplify - 1) / amplify);

			switch (rand() % 4) {
			case 0:
				tc.num_points = 1 + rand() % target_in_points;
				tc.num_instances = 1 + rand() % (1 + target_in_points / tc.num_points);
				break;
			case 1:
				tc.num_instances = 1 + rand() % target_in_points;
				tc.num_points = 1 + rand() % (1 + target_in_points / tc.num_instances);
				break;
			default: {
				unsigned min = MAX2(1, sqrt(target_in_points) / 2);
				unsigned max = MIN2(sqrt(target_in_points) * 3 / 2,
						    target_in_points);
				tc.num_instances = min + rand() % (max - min + 1);
				tc.num_points = 1 + rand() % (1 + target_in_points / tc.num_instances);
				break;
			}
			}

			add_testcase(&tc);
		}
	} else {
		add_testcase(&explicit_testcase);
	}
}

enum piglit_result
piglit_display(void)
{
	bool pass = true;

	for (unsigned i = 0; i < testcases.size(); ++i) {
		if (!run_testcase(&testcases[i]))
			pass = false;
	}

	if (!piglit_check_gl_error(GL_NO_ERROR))
		pass = false;

	piglit_present_results();

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}