summaryrefslogtreecommitdiff
path: root/tests/spec/glsl-1.50/execution/geometry/transform-feedback-vertex-id.c
blob: fef1a173a35b7183e6f84d5df771664e80c54a20 (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
/*
 * Copyright © 2014 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 transform-feedback-vertex-id.c
 *
 * This test verifies that we get expected values of 'gl_VertexID' captured
 * using transform feedback.
 *
 * Test creates a vertex shader which captures the value of 'gl_VertexID'
 * in an output variable. Then it verifies that data captured by transform
 * feedback is as expected.
 */

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

PIGLIT_GL_TEST_CONFIG_BEGIN
	config.supports_gl_compat_version = 32;
	config.supports_gl_core_version = 32;
	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
PIGLIT_GL_TEST_CONFIG_END

static const char *vstext =
	"#version 150\n"
	"in vec4 vertex;\n"
	"out int vertex_id;\n"
	"void main()\n"
	"{\n"
	"  gl_Position = vertex;\n"
        "  vertex_id = gl_VertexID;\n"
	"}\n";

static const char *fstext =
	"#version 150\n"
	"out vec4 color;"
	"void main()\n"
	"{\n"
	"  color = vec4(1.0);\n"
	"}\n";

static const GLfloat vertices[] = {
	-0.6f, -0.2f, 0.0f, 1.0f,
	-0.6f,  0.2f, 0.0f, 1.0f,
	-0.4f, -0.4f, 0.0f, 1.0f,
	-0.4f,  0.4f, 0.0f, 1.0f,
	 0.0f, -0.6f, 0.0f, 1.0f,
	 0.0f,  0.6f, 0.0f, 1.0f,
	 0.4f, -0.4f, 0.0f, 1.0f,
	 0.4f,  0.4f, 0.0f, 1.0f,
	 0.6f, -0.2f, 0.0f, 1.0f,
	 0.6f,  0.2f, 0.0f, 1.0f,
	 0.0f,  0.0f, 0.0f, 1.0f};

static const GLuint indices_0[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
static const GLuint indices_1[] = {2, 3, 4, 1, 5, 8, 0, 9, 6, 10, 7};

/**
 * When below set of varyings are captured from the vertex shader
 * above, the output should be a sequence of integers defined in this
 * array.
 */
static const int expected_0[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
static const int expected_1[] = {2, 3, 4, 1, 5, 8, 0, 9, 6, 10, 7};
static const int expected_2[] = {4, 5, 6, 7};

static const char *varyings[] = {"vertex_id"};

bool
setup_xfb_and_compare(const GLuint* indices, int first,
		      int count, const int *expected) {
	int i, num_output_ints;
	GLuint xfb_buf, indexBuffer;
	const GLint *readback;
	bool result = true;
	num_output_ints = count;

	/* Gen indices array buffer if required */
        glGenBuffers(1, &indexBuffer);
	if (indices != NULL) {
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(GLuint),
			     indices, GL_STATIC_DRAW);
	} else {
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	}

	/* Setup transform feedback buffers */
	glGenBuffers(1, &xfb_buf);
	glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, xfb_buf);
	glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER,
		     num_output_ints * sizeof(GLint), NULL,
		     GL_STATIC_READ);

	printf("%s with Starting index = %d, Number of indices = %d\n",
	       indices ? "glDrawElements()" : "glDrawArrays()", first, count);

	/* Do drawing */
	glBeginTransformFeedback(GL_POINTS);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPointSize(5.0);

	if (indices != NULL) {
		glDrawElements(GL_POINTS, count, GL_UNSIGNED_INT, NULL);
	} else {
		glDrawArrays(GL_POINTS, first, count);
	}
	glEndTransformFeedback();
	piglit_present_results();

	/* Check if the correct data was written into the transform feedback
	 * buffers.
	 */
	glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, xfb_buf, 0,
			  num_output_ints * sizeof(GLint));
	readback = glMapBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0,
				    num_output_ints * sizeof(GLint),
				    GL_MAP_READ_BIT);
	result = piglit_check_gl_error(GL_NO_ERROR) && result;

	for (i = 0; i < num_output_ints; i++) {
		if (readback[i] != expected[i]) {
			printf("Incorrect data for '%s' output %d."
			       "  Expected %d, got %d.\n", varyings[0],
			       i, expected[i], readback[i]);
			result = false;
		}
	}
	glUnmapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER);
	glDeleteBuffers(1, &xfb_buf);
	return result;
}

void
piglit_init(int argc, char **argv)
{
	int vertex_pos;
	GLuint vao, vertexBuffer;
	GLuint prog = piglit_build_simple_program_unlinked_multiple_shaders(
		GL_VERTEX_SHADER, vstext,
		GL_FRAGMENT_SHADER, fstext,
		0, NULL);

	glTransformFeedbackVaryings(prog, 1, varyings, GL_SEPARATE_ATTRIBS);

	glLinkProgram(prog);
	if (!piglit_link_check_status(prog)) {
		glDeleteProgram(prog);
		piglit_report_result(PIGLIT_FAIL);
	}
	glUseProgram(prog);

	/* Gen VAO */
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	/* Gen vertex array buffer */
	glGenBuffers(1, &vertexBuffer);
	glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
	glBufferData(GL_ARRAY_BUFFER, ARRAY_SIZE(vertices) * sizeof(GLfloat),
		     &vertices, GL_STATIC_DRAW);

	/* Enable vertexAttribPointer */
	vertex_pos = glGetAttribLocation(prog, "vertex");
        glEnableVertexAttribArray(vertex_pos);
        glVertexAttribPointer(vertex_pos, 4, GL_FLOAT, GL_FALSE, 0, 0);
}

enum piglit_result
piglit_display(void)
{
	bool pass = true;
	int first = 0;
	int count = ARRAY_SIZE(vertices) / 4;
	/* Draw with different 'first' and 'count' values, capture the
	 * transform feedback data and compare with expected values.
	 */
	pass = setup_xfb_and_compare(NULL, first,
				     count,
				     expected_0) && pass;

	pass = setup_xfb_and_compare(NULL, 4 /*first*/,
				     4 /*count*/,
				     expected_2) && pass;

	pass = setup_xfb_and_compare(indices_0, first,
				     count,
				     expected_0) && pass;

	pass = setup_xfb_and_compare(indices_1, first,
				     count,
				     expected_1) && pass;

	return (pass ? PIGLIT_PASS : PIGLIT_FAIL);
}