aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/ext_transform_feedback/get-buffer-state.c
blob: 40d73251ae1491c899eb3ec98c2d07cc20d2f215 (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
/*
 * 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 get-buffer-state.c
 *
 * Test that "Get" functions can be used to query the state of
 * transform feedback buffers.
 */

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

PIGLIT_GL_TEST_MAIN(
    16 /*window_width*/,
    16 /*window_height*/,
    PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB)

#define XFB_BUFFER_SIZE 12

enum test_mode {
	MAIN,
	INDEXED,
};

static struct test_desc
{
	const char *name;
	enum test_mode mode;
	GLenum param;
} tests[] = {
	/* name              mode     param */
	{ "main_binding",    MAIN,    GL_TRANSFORM_FEEDBACK_BUFFER_BINDING },
	{ "indexed_binding", INDEXED, GL_TRANSFORM_FEEDBACK_BUFFER_BINDING },
	{ "buffer_start",    INDEXED, GL_TRANSFORM_FEEDBACK_BUFFER_START },
	{ "buffer_size",     INDEXED, GL_TRANSFORM_FEEDBACK_BUFFER_SIZE },
};

static GLboolean
check_integer(const struct test_desc *test, GLenum param,
	      const char *param_string, GLint expected)
{
	GLint get_result;

	if (test->mode == MAIN && test->param == param) {
		glGetIntegerv(param, &get_result);
		if (get_result != expected) {
			printf("%s == %i, expected %i\n", param_string,
			       get_result, expected);
			return GL_FALSE;
		}
	}
	return GL_TRUE;
}

#define CHECK_INTEGER(param, expected) \
	pass = check_integer(test, param, #param, expected) && pass

static GLboolean
check_indexed(const struct test_desc *test, GLenum param,
	      const char *param_string, GLuint index, GLint expected)
{
	GLint get_result;

	if (test->mode == INDEXED && test->param == param) {
		glGetIntegeri_v(param, index, &get_result);
		if (get_result != expected) {
			printf("%s[%u] == %i, expected %i\n", param_string,
			       index, get_result, expected);
			return GL_FALSE;
		}
	}
	return GL_TRUE;
}

#define CHECK_INDEXED(param, index, expected) \
	pass = check_indexed(test, param, #param, index, expected) && pass

static GLboolean
do_test(const struct test_desc *test)
{
	GLuint *bufs;
	GLboolean pass = GL_TRUE;
	GLint max_separate_attribs;
	float initial_xfb_buffer_contents[XFB_BUFFER_SIZE];
	int i;

	glGetIntegerv(GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS,
		      &max_separate_attribs);
	printf("MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTIBS=%i\n",
	       max_separate_attribs);

	bufs = malloc(max_separate_attribs * sizeof(GLuint));
	glGenBuffers(max_separate_attribs, bufs);
	memset(initial_xfb_buffer_contents, 0,
	       sizeof(initial_xfb_buffer_contents));

	/* Main GL_TRANSFORM_FEEDBACK_BUFFER_BINDING should still be
	 * set to its default value.
	 */
	CHECK_INTEGER(GL_TRANSFORM_FEEDBACK_BUFFER_BINDING, 0);

	/* Set up buffers. */
	for (i = 0; i < max_separate_attribs; ++i) {
		printf("BindBuffer(TRANSFORM_FEEDBACK_BUFFER, %u)\n", bufs[i]);
		glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, bufs[i]);
		CHECK_INTEGER(GL_TRANSFORM_FEEDBACK_BUFFER_BINDING, bufs[i]);
		glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER,
			     sizeof(initial_xfb_buffer_contents),
			     initial_xfb_buffer_contents, GL_STREAM_READ);
	}

	/* Indexed bindings should still be set to their default values. */
	for (i = 0; i < max_separate_attribs; ++i) {
		CHECK_INDEXED(GL_TRANSFORM_FEEDBACK_BUFFER_BINDING, i, 0);
		CHECK_INDEXED(GL_TRANSFORM_FEEDBACK_BUFFER_START, i, 0);
		CHECK_INDEXED(GL_TRANSFORM_FEEDBACK_BUFFER_SIZE, i, 0);
	}

	/* Bind buffers, setting various offsets and sizes. */
	for (i = 0; i < max_separate_attribs; ++i) {
		int offset = 4 * (i % 4);
		int size = 4 * ((i % 3) + 1);
		printf("BindBufferRange(TRANSFORM_FEEDBACK_BUFFER, %i, %u, %i, %i)\n",
		       i, bufs[i], offset, size);
		glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, i,
				  bufs[i], offset, size);
		CHECK_INTEGER(GL_TRANSFORM_FEEDBACK_BUFFER_BINDING, bufs[i]);
	}

	/* Check indexed bindings. */
	for (i = 0; i < max_separate_attribs; ++i) {
		int offset = 4 * (i % 4);
		int size = 4 * ((i % 3) + 1);
		CHECK_INDEXED(GL_TRANSFORM_FEEDBACK_BUFFER_BINDING, i, bufs[i]);
		CHECK_INDEXED(GL_TRANSFORM_FEEDBACK_BUFFER_START, i, offset);
		CHECK_INDEXED(GL_TRANSFORM_FEEDBACK_BUFFER_SIZE, i, size);
	}

	return pass;
}

void
print_usage_and_exit(const char *prog_name)
{
	int i;

	printf("Usage: %s <test_name>\n"
	       "  where <test_name> is one of:\n", prog_name);
	for (i = 0; i < ARRAY_SIZE(tests); ++i)
		printf("    %s\n", tests[i].name);
	exit(0);
}

const struct test_desc *
find_matching_test(const char *prog_name, const char *test_name)
{
	int i;
	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
		if (strcmp(tests[i].name, test_name) == 0)
			return &tests[i];
	}
	print_usage_and_exit(prog_name);
	return NULL; /* won't actually be reached */
}

void
piglit_init(int argc, char **argv)
{
	const struct test_desc *test;

	/* Parse params. */
	if (argc != 2)
		print_usage_and_exit(argv[0]);
	test = find_matching_test(argv[0], argv[1]);

	piglit_require_GLSL();
	piglit_require_transform_feedback();

	piglit_report_result(do_test(test) ? PIGLIT_PASS : PIGLIT_FAIL);
}

enum piglit_result
piglit_display(void)
{
	/* Should never be reached */
	return PIGLIT_FAIL;
}