aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/ext_transform_feedback/overflow-edge-cases.c
blob: dd1a2ddf390c0d123209850bbfb7cbfb7598776d (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
/*
 * 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 overflow-edge-cases.c
 *
 * Verify edge cases of transform feedback overflow checking.
 *
 * This test exercises all possible combinations of the following four
 * variables:
 *
 * - Size passed to glBindBufferRange (1-6 floats)
 * - Number of transform feedback varying components (1 or 2)
 * - Number of primitives drawn (1-3)
 * - Primitive mode (GL_POINTS, GL_LINES, or GL_TRIANGLES)
 *
 * In all cases, it verifies that:
 *
 * - The proper values were written to the transform feedback buffer.
 * - GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN is set correctly.
 * - GL_PRIMITIVES_GENERATED is set correctly.
 */

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

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.window_width = 16;
	config.window_height = 16;
	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB;

PIGLIT_GL_TEST_CONFIG_END

#define XFB_BUFFER_SIZE 12
#define MAX_VERTICES 9

static const char *vstext =
	"attribute float vertex_num;\n"
	"varying float varying1;\n"
	"varying float varying2;\n"
	"\n"
	"void main()\n"
	"{\n"
	"  gl_Position = vec4(vertex_num);\n"
	"  varying1 = 100.0 + vertex_num;\n"
	"  varying2 = 200.0 + vertex_num;\n"
	"}\n";

static const char *varyings[] = { "varying1", "varying2" };

static GLuint xfb_buf;
static GLuint progs[2]; /* indexed by num_varyings - 1 */
static GLuint query_prims_generated;
static GLuint query_prims_written;

void
piglit_init(int argc, char **argv)
{
	GLuint vs;
	int num_varyings;

	piglit_require_GLSL();
	piglit_require_transform_feedback();

	vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vstext);
	for (num_varyings = 1; num_varyings <= 2; ++num_varyings) {
		GLuint prog = piglit_CreateProgram();
		piglit_AttachShader(prog, vs);
		piglit_BindAttribLocation(prog, 0, "vertex_num");
		glTransformFeedbackVaryings(prog, num_varyings, varyings,
					    GL_INTERLEAVED_ATTRIBS);
		piglit_LinkProgram(prog);
		if (!piglit_link_check_status(prog)) {
			piglit_DeleteProgram(prog);
			piglit_report_result(PIGLIT_FAIL);
		}
		progs[num_varyings - 1] = prog;
	}

	glGenBuffers(1, &xfb_buf);
	glGenQueries(1, &query_prims_generated);
	glGenQueries(1, &query_prims_written);
}

static GLenum modes[] = { GL_POINTS, GL_LINES, GL_TRIANGLES };
static const char *mode_names[] = {
	"GL_POINTS", "GL_LINES", "GL_TRIANGLES"
};
static int mode_vertices_per_prim[] = { 1, 2, 3 };

static GLboolean
test(int bind_size, int num_varyings, int num_primitives, int mode_index)
{
	float initial_xfb_buf[XFB_BUFFER_SIZE];
	float vertex_data[MAX_VERTICES];
	int i, j;
	int vertices_per_prim = mode_vertices_per_prim[mode_index];
	int expected_primitives_written =
		MIN2(num_primitives,
		     bind_size / num_varyings / vertices_per_prim);
	int expected_vertices_written =
		expected_primitives_written * vertices_per_prim;
	GLuint query_result;
	GLboolean pass = GL_TRUE;
	float expected_xfb_results[XFB_BUFFER_SIZE];
	float *readback;

	printf("size=%d, num_varyings=%d, num_primitives=%d, mode=%s: ",
	       bind_size, num_varyings, num_primitives,
	       mode_names[mode_index]);

	/* Setup program and initial buffer contents */
	piglit_UseProgram(progs[num_varyings - 1]);
	for (i = 0; i < MAX_VERTICES; ++i)
		vertex_data[i] = i;
	glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, sizeof(float),
			      &vertex_data);
	glEnableVertexAttribArray(0);
	for (i = 0; i < XFB_BUFFER_SIZE; ++i)
		initial_xfb_buf[i] = 0.0;
	glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, xfb_buf);
	glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(initial_xfb_buf),
		     initial_xfb_buf, GL_STREAM_READ);
	glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, xfb_buf, 0,
			  sizeof(float) * bind_size);

	/* Start queries and XFB */
	glBeginQuery(GL_PRIMITIVES_GENERATED, query_prims_generated);
	glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN,
		     query_prims_written);
	glBeginTransformFeedback(modes[mode_index]);

	/* Draw */
	glDrawArrays(modes[mode_index], 0, num_primitives * vertices_per_prim);

	/* Stop XFB and check queries */
	glEndTransformFeedback();
	glEndQuery(GL_PRIMITIVES_GENERATED);
	glGetQueryObjectuiv(query_prims_generated, GL_QUERY_RESULT,
			    &query_result);
	if (query_result != num_primitives) {
		printf("\n  Expected %u primitives generated, got %u\n",
		       (unsigned) num_primitives, query_result);
		pass = GL_FALSE;
	}
	glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
	glGetQueryObjectuiv(query_prims_written, GL_QUERY_RESULT,
			    &query_result);
	if (query_result != expected_primitives_written) {
		printf("\n  Expected %u primitives written, got %u",
		       (unsigned) expected_primitives_written, query_result);
		pass = GL_FALSE;
	}

	/* Check transform feedback buffer */
	memcpy(expected_xfb_results, initial_xfb_buf, sizeof(initial_xfb_buf));
	for (i = 0; i < expected_vertices_written; ++i) {
		for (j = 0; j < num_varyings; ++j) {
			expected_xfb_results[i * num_varyings + j] =
				100.0 * (j + 1) + i;
		}
	}
	readback = glMapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER,
			       GL_READ_ONLY);
	for (i = 0; i < XFB_BUFFER_SIZE; ++i) {
		if (expected_xfb_results[i] != readback[i]) {
			printf("\n  Expected buf[%i] = %f, got %f",
			       i, expected_xfb_results[i], readback[i]);
			pass = GL_FALSE;
		}
	}

	if (pass)
		printf("PASS\n");
	else
		printf("  FAIL\n");

	return pass;
}

enum piglit_result
piglit_display(void)
{
	GLboolean pass = GL_TRUE;
	int bind_size, num_varyings, num_primitives, mode_index;

	for (bind_size = 1; bind_size <= 6; ++bind_size) {
		for (num_varyings = 1; num_varyings <= 2; ++num_varyings) {
			for (num_primitives = 1; num_primitives <= 3; ++num_primitives) {
				for (mode_index = 0; mode_index < 3; ++mode_index) {
					pass = test(bind_size,
						    num_varyings,
						    num_primitives,
						    mode_index) && pass;
				}
			}
		}
	}

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}