aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/arb_uniform_buffer_object/getactiveuniformblockname.c
blob: 6541a115653a20ea940911851b27686062e5a840 (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
/*
 * 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 getactiveuniformblockname.c
 *
 * From the GL_ARB_uniform_buffer_object spec:
 *
 *     "An active uniform block's name string can be queried from its
 *      uniform block index by calling
 *
 *          void GetActiveUniformBlockName(uint program,
 *                                         uint uniformBlockIndex,
 *                                         sizei bufSize,
 *                                         sizei* length,
 *                                         char* uniformBlockName);
 *
 *      The string name of the uniform block identified by
 *      <uniformBlockIndex> is returned into <uniformBlockName>. The
 *      name is null-terminated. The actual number of characters
 *      written into <uniformBlockName>, excluding the null
 *      terminator, is returned in <length>.  If <length> is NULL, no
 *      length is returned.
 *
 *      <bufSize> contains the maximum number of characters (including
 *      the null terminator) that will be written back to
 *      <uniformBlockName>.
 *
 *      If an error occurs, nothing will be written to
 *      <uniformBlockName> or <length>.
 *
 *      ...
 *
 *      The error INVALID_VALUE is generated by GetActiveUniformsiv,
 *      GetActiveUniformName, GetActiveUniformBlockiv,
 *      GetActiveUniformBlockName, and UniformBlockBinding if
 *      <program> is not a value generated by GL.
 *
 *      The error INVALID_VALUE is generated by GetActiveUniformName
 *      and GetActiveUniformBlockName if <bufSize> is less than zero.
 *
 *      The error INVALID_VALUE is generated by
 *      GetActiveUniformBlockiv, GetActiveUniformBlockName, and
 *      UniformBlockBinding if <uniformBlockIndex> is greater than or
 *      equal to ACTIVE_UNIFORM_BLOCKS."
 */

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

PIGLIT_GL_TEST_CONFIG_BEGIN

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

PIGLIT_GL_TEST_CONFIG_END

void
piglit_init(int argc, char **argv)
{
	int i;
	GLuint fs, prog;
	const char *source =
		"#extension GL_ARB_uniform_buffer_object : enable\n"
		"uniform a { float u1; };\n"
		"uniform bbb { float u2; };\n"
		"uniform cc { float u3; };\n"
		"void main() {\n"
		"	gl_FragColor = vec4(u1 + u2 + u3);\n"
		"}\n";
	int blocks;
	bool pass = true;
	const char *names[3] = {"a", "bbb", "cc"};
	bool found[3] = {false, false, false};
	char no_write;
	char fill_char = 0xd0;

	piglit_require_extension("GL_ARB_uniform_buffer_object");

	fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, source);
	prog = piglit_link_simple_program(fs, 0);
	if (!fs || !prog) {
		fprintf(stderr, "Failed to compile shader:\n%s", source);
		piglit_report_result(PIGLIT_FAIL);
	}

	glGetProgramiv(prog, GL_ACTIVE_UNIFORM_BLOCKS, &blocks);
	assert(blocks == 3);

	for (i = 0; i < blocks; i++) {
		GLint written_strlen = 0;
		GLint namelen = 9999;
		char name[1000];
		int name_index;

		/* This is the size including null terminator. */
		glGetActiveUniformBlockiv(prog, i, GL_UNIFORM_BLOCK_NAME_LENGTH,
					  &namelen);

		memset(name, 0xd0, sizeof(name));
		glGetActiveUniformBlockName(prog, i, sizeof(name),
					    &written_strlen, name);
		if (written_strlen >= sizeof(name) - 1) {
			fprintf(stderr,
				"return strlen %d, longer than the buffer size\n",
				written_strlen);
			pass = false;
			continue;
		} else if (name[written_strlen] != 0) {
			fprintf(stderr, "return name[%d] was %d, expected 0\n",
				written_strlen, name[written_strlen]);
			pass = false;
			continue;
		} else if (strlen(name) != written_strlen) {
			fprintf(stderr, "return strlen was %d, but \"%s\" "
				"has strlen %d\n", written_strlen, name,
				(int)strlen(name));
			pass = false;
			continue;
		}

		for (name_index = 0; name_index < ARRAY_SIZE(names); name_index++) {
			if (strcmp(names[name_index], name) == 0) {
				if (found[name_index]) {
					fprintf(stderr,
						"Uniform block \"%s\" "
						"returned twice.\n", name);
					pass = false;
				}
				found[name_index] = true;
				break;
			}
		}
		if (name_index == ARRAY_SIZE(names)) {
			fprintf(stderr,
				"block \"%s\" is not a known block name\n", name);
			pass = false;
			continue;
		}

		if (namelen != written_strlen + 1) {
			fprintf(stderr,
				"block \"%s\" had "
				"GL_UNIFORM_BLOCK_NAME_LENGTH %d, expected %d\n",
				name, namelen, written_strlen + 1);
			pass = false;
			continue;
		}

		/* Test for overflow by writing to a bufSize equal to
		 * strlen and checking if a null terminator or
		 * something landed past that.
		 */
		memset(name, fill_char, sizeof(name));
		glGetActiveUniformBlockName(prog, i, written_strlen,
					    NULL, name);
		if (name[written_strlen] != fill_char) {
			fprintf(stderr, "glGetActiveUniformName overflowed: "
				"name[%d] = 0x%02x instead of 0x%02x\n",
				written_strlen, name[written_strlen],
				fill_char);
			pass = false;
		}
	}

	no_write = fill_char;
	glGetActiveUniformBlockName(0xd0d0, 0, 1, NULL, &no_write);
	pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass;
	if (no_write != fill_char)
		pass = false;

	no_write = fill_char;
	glGetActiveUniformBlockName(prog, 0, -1, NULL, &no_write);
	pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass;
	if (no_write != fill_char)
		pass = false;

	no_write = fill_char;
	glGetActiveUniformBlockName(prog, blocks, 1, NULL, &no_write);
	pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass;
	if (no_write != fill_char)
		pass = false;

	glDeleteProgram(prog);
	glDeleteShader(fs);

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}

enum piglit_result piglit_display(void)
{
	/* UNREACHED */
	return PIGLIT_FAIL;
}