aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/ext_transform_feedback/order.c
blob: c2798a6f1c7f34fb159d4ebca3495549c510b513 (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
/*
 * Copyright © 2011 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 order.c
 *
 * Confirm that the order of vertices output by transform feedback
 * matches the order of vertices supplied to the GL pipeline.
 *
 * On OpenGL implementations that execute multiple vertex shader
 * threads in parallel, it's possible that the threads won't complete
 * in the same order that they were invoked.  When this happens, it's
 * critical that transform feedback records the vertices in the order
 * that they were inserted into the GL pipeline, not the order of
 * shader completion.
 *
 * This test verifies that transform feedback records vertices in the
 * correct order by using a vertex shader whose execution time is
 * dramatically different for different vertices.
 *
 * The test requries two command line arguments:
 *
 * - drawcall indicates which drawing function should be called.  A
 *   value of "arrays" causes DrawArrays() to be used.  A value of
 *   "elements" causes DrawElemeents() to be used.  When
 *   DrawElements() is used, we supply an indices array that scrambles
 *   the order in which vertices are sent to the shader, and verify
 *   that the scrambling is reflected in the transform feedback
 *   output.
 *
 * - mode indiceates which drawing mode should be used.  It may be
 *   "triangles", "lines", or "points".
 */

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

#define NUM_POINTS 10002
#define SHIFT_COUNT 64

PIGLIT_GL_TEST_CONFIG_BEGIN

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

PIGLIT_GL_TEST_CONFIG_END

static GLenum draw_mode;
static GLboolean use_draw_elements;
static GLuint prog;
static GLuint xfb_buf;
static unsigned *verts;
static unsigned *indices;

/* This vertex shader computes the hailstone sequence, which is
 * defined as:
 *
 * x[0] = starting_x
 * x[n+1] = x[n]/2 if x[n] is even
 *        = 3*x[n]+1 if x[n] is odd
 *
 * The shader measures, for different values of starting_x, the
 * minimum n such that x[n] = 1.  This value is output in
 * iteration_count.  The shader outputs a copy of starting_x in
 * starting_x_copy.
 *
 * To prevent an infinite loop, if starting_x is 0, it is changed to
 * 1.
 *
 * In addition, to consume more execution time, the shader maintains a
 * 31-bit shift register whose value starts at 1, and at each
 * iteration of the algorithm, shifts it left, in circular fashion,
 * shift_count times.  shift_count can be adjusted as necessary to
 * ensure that vertex shader threads complete out of order, but the
 * entire test doesn't take too long to finish.
 *
 * All of this pointless mathematics serves one purpose: to ensure
 * that different invocations of the vertex shader take dramatically
 * different amounts of time to execute.
 */
static const char *vstext =
	"#version 130\n"
	"in uint starting_x;\n"
	"flat out uint starting_x_copy;\n"
	"flat out uint iteration_count;\n"
	"flat out uint shift_reg_final;\n"
	"uniform uint shift_count;\n"
	"\n"
	"void main()\n"
	"{\n"
	"  gl_Position = vec4(0.0);\n"
	"  uint x = starting_x;\n"
	"  if (x == 0u)\n"
	"    x = 1u;\n"
	"  uint count = 0u;\n"
	"  uint shift_reg = 1u;\n"
	"  starting_x_copy = starting_x;\n"
	"  while (x != 1u) {\n"
	"    ++count;\n"
	"    if (x % 2u == 0u)\n"
	"      x /= 2u;\n"
	"    else\n"
	"      x = 3u * x + 1u;\n"
	"    uint i;\n"
	"    for (i = 0u; i < shift_count; ++i)\n"
	"      shift_reg = (shift_reg * 2u) % 0x7fffffffu;\n"
	"  }\n"
	"  iteration_count = count;\n"
	"  shift_reg_final = shift_reg;\n"
	"}\n";

static const char *varyings[] = {
	"starting_x_copy", "iteration_count", "shift_reg_final"
};

static void
initialize_shader_and_xfb()
{
	GLuint vs;

	piglit_require_gl_version(30);
	piglit_require_GLSL_version(130);
	piglit_require_transform_feedback();
	vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vstext);
	prog = piglit_CreateProgram();
	piglit_AttachShader(prog, vs);
	glTransformFeedbackVaryings(prog, 3, varyings, GL_INTERLEAVED_ATTRIBS);
	piglit_LinkProgram(prog);
	if (!piglit_link_check_status(prog)) {
		piglit_DeleteProgram(prog);
		piglit_report_result(PIGLIT_FAIL);
	}
	glGenBuffers(1, &xfb_buf);
	glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, xfb_buf);
	glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER,
		     3*NUM_POINTS*sizeof(unsigned), NULL, GL_STREAM_READ);
	glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, xfb_buf);
	piglit_UseProgram(prog);
	if (!piglit_check_gl_error(0))
		piglit_report_result(PIGLIT_FAIL);
}

static void
initialize_vertex_shader_inputs()
{
	GLint starting_x_index = glGetAttribLocation(prog, "starting_x");
	unsigned i;

	verts = malloc(NUM_POINTS*sizeof(unsigned));
	indices = malloc(NUM_POINTS*sizeof(unsigned));

	for (i = 0; i < NUM_POINTS; ++i)
		verts[i] = i;
	for (i = 0; i < NUM_POINTS/2; ++i) {
		indices[i] = 2*i;
		indices[i + NUM_POINTS/2] = 2*i+1;
	}

	piglit_Uniform1ui(piglit_GetUniformLocation(prog, "shift_count"),
			  SHIFT_COUNT);
	glVertexAttribIPointer(starting_x_index, 1, GL_UNSIGNED_INT, sizeof(unsigned),
			       verts);
	glEnableVertexAttribArray(starting_x_index);
	if (!piglit_check_gl_error(0))
		piglit_report_result(PIGLIT_FAIL);
}

/**
 * Compute the value of iteration_count that we expect the vertex
 * shader to output for the given starting_x.
 */
static unsigned
compute_iteration_count(unsigned starting_x)
{
	unsigned count = 0;
	unsigned x = starting_x;
	if (x == 0)
		x = 1;
	while (x != 1) {
		++count;
		if (x % 2 == 0)
			x /= 2;
		else
			x = 3 * x + 1;
	}
	return count;
}

/**
 * Compute the value of shift_reg_final that we expect the vertex
 * shader to output for the given iteration_count.
 */
static unsigned
compute_shift_reg_final(unsigned iteration_count)
{
	/* shift_reg starts at 1 and is shifted left
	 * SHIFT_COUNT*iteration_count times.  After every 31 shifts,
	 * it resets to 1.
	 */
	return ((unsigned) 1) << ((SHIFT_COUNT * iteration_count) % 31);
}

static void
draw()
{
	glEnable(GL_RASTERIZER_DISCARD);
	glBeginTransformFeedback(draw_mode);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	if (use_draw_elements) {
		glDrawElements(draw_mode, NUM_POINTS, GL_UNSIGNED_INT,
			       indices);
	} else {
		glDrawArrays(draw_mode, 0, NUM_POINTS);
	}
	glEndTransformFeedback();
	if (!piglit_check_gl_error(0))
		piglit_report_result(PIGLIT_FAIL);
}

static void
check_results_and_exit()
{
	unsigned *readback;
	unsigned i;
	GLboolean pass = GL_TRUE;

	readback = glMapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, GL_READ_ONLY);
	pass = piglit_check_gl_error(0) && pass;

	for (i = 0; i < NUM_POINTS; ++i) {
		unsigned expected_starting_x =
			use_draw_elements ? indices[i] : i;
		unsigned expected_iteration_count =
			compute_iteration_count(expected_starting_x);
		unsigned expected_shift_reg_final =
			compute_shift_reg_final(expected_iteration_count);
		if (readback[3*i] != expected_starting_x) {
			printf("Order changed at vertex %u\n", i);
			pass = GL_FALSE;
			break;
		}
		if (readback[3*i+1] != expected_iteration_count) {
			printf("Incorrect iteration_count at vertex %u\n", i);
			pass = GL_FALSE;
			break;
		}
		if (readback[3*i+2] != expected_shift_reg_final) {
			printf("Incorrect shift_reg_final at vertex %u\n", i);
			pass = GL_FALSE;
			break;
		}
	}
	glUnmapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER);

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}

static void
print_usage_and_exit(char *prog_name)
{
	printf("Usage: %s <drawcall> <mode>\n"
	       "  where <drawcall is one of:\n"
	       "    arrays\n"
	       "    elements\n"
	       "  and <mode> is one of:\n"
	       "    triangles\n"
	       "    lines\n"
	       "    points\n", prog_name);
	exit(1);
}

void
piglit_init(int argc, char **argv)
{
	/* Interpret command line args */
	if (argc != 3)
		print_usage_and_exit(argv[0]);
	if (strcmp(argv[1], "arrays") == 0)
		use_draw_elements = GL_FALSE;
	else if (strcmp(argv[1], "elements") == 0)
		use_draw_elements = GL_TRUE;
	else
		print_usage_and_exit(argv[0]);
	if (strcmp(argv[2], "triangles") == 0)
		draw_mode = GL_TRIANGLES;
	else if (strcmp(argv[2], "lines") == 0)
		draw_mode = GL_LINES;
	else if (strcmp(argv[2], "points") == 0)
		draw_mode = GL_POINTS;
	else
		print_usage_and_exit(argv[0]);

	initialize_shader_and_xfb();
	initialize_vertex_shader_inputs();
	draw();
	check_results_and_exit();
}

enum piglit_result piglit_display(void)
{
	/* Test should finish before we reach here. */
	return PIGLIT_FAIL;
}