summaryrefslogtreecommitdiff
path: root/tests/spec/arb_program_interface_query/getprogramresourceindex.c
blob: 889b5e11234fb5cac6db121e91537bdeb658f65a (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright © 2015 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 getprogramresourceindex.c
 *
 * Tests the errors reported by the GetProgramResourceIndex interface while also
 * testing tricky naming cases. The real functional test is in resource-query.c.
 *
 * From the GL_ARB_program_interface_query spec:
 *      "The command
 *
 *      uint GetProgramResourceIndex(uint program, enum programInterface,
 *                                   const char *name);
 *
 *      returns the unsigned integer index assigned to a resource named <name>
 *      in the interface type <programInterface> of program object <program>.
 *      The error INVALID_ENUM is generated if <programInterface> is
 *      ATOMIC_COUNTER_BUFFER, since active atomic counter buffer resources are
 *      not assigned name strings.
 *
 *      If <name> exactly matches the name string of one of the active resources
 *      for <programInterface>, the index of the matched resource is returned.
 *      Additionally, if <name> would exactly match the name string of an active
 *      resource if "[0]" were appended to <name>, the index of the matched
 *      resource is returned.  Otherwise, <name> is considered not to be the
 *      name of an active resource, and INVALID_INDEX is returned.  Note that if
 *      an interface enumerates a single active resource list entry for an array
 *      variable (e.g., "a[0]"), a <name> identifying any array element other
 *      than the first (e.g., "a[1]") is not considered to match.
 *
 *      For the interface TRANSFORM_FEEDBACK_VARYING, the value INVALID_INDEX
 *      should be returned when querying the index assigned to the special names
 *      "gl_NextBuffer", "gl_SkipComponents1", "gl_SkipComponents2",
 *      "gl_SkipComponents3", and "gl_SkipComponents4".
 *
 *      [...]
 *
 *      An INVALID_VALUE error is generated by GetProgramInterfaceiv,
 *      GetProgramResourceIndex, GetProgramResourceName, GetProgramResourceiv,
 *      GetProgramResourceLocation, and GetProgramResourceLocationIndex if
 *      <program> is not the name of either a shader or program object.
 *
 *      An INVALID_OPERATION error is generated by GetProgramInterfaceiv,
 *      GetProgramResourceIndex, GetProgramResourceName, GetProgramResourceiv,
 *      GetProgramResourceLocation, and GetProgramResourceLocationIndex if
 *      <program> is the name of a shader object.
 *
 *      [...]
 *
 *      INVALID_ENUM is generated by GetProgramResourceIndex if
 *      <programInterface> is ATOMIC_COUNTER_BUFFER."
 */

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

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_core_version = 32;

PIGLIT_GL_TEST_CONFIG_END

/* Naming conventions, from the GL_ARB_program_interface_query extension:
 *
 * "When building a list of active variable or interface blocks, resources
 * with aggregate types (such as arrays or structures) may produce multiple
 * entries in the active resource list for the corresponding interface.
 * Additionally, each active variable, interface block, or subroutine in the
 * list is assigned an associated name string that can be used by
 * applications to refer to the resources.  For interfaces involving
 * variables, interface blocks, or subroutines, the entries of active
 * resource lists are generated as follows:
 *
 *  * For an active variable declared as a single instance of a basic type,
 *    a single entry will be generated, using the variable name from the
 *    shader source.
 *
 *  * For an active variable declared as an array of basic types, a single
 *    entry will be generated, with its name string formed by concatenating
 *    the name of the array and the string "[0]".
 *
 *  * For an active variable declared as a structure, a separate entry will
 *    be generated for each active structure member.  The name of each entry
 *    is formed by concatenating the name of the structure, the "."
 *    character, and the name of the structure member.  If a structure
 *    member to enumerate is itself a structure or array, these enumeration
 *    rules are applied recursively.
 *
 *  * For an active variable declared as an array of an aggregate data type
 *    (structures or arrays), a separate entry will be generated for each
 *    active array element, unless noted immediately below.  The name of
 *    each entry is formed by concatenating the name of the array, the "["
 *    character, an integer identifying the element number, and the "]"
 *    character.  These enumeration rules are applied recursively, treating
 *    each enumerated array element as a separate active variable.
 *
 *  * For an active shader storage block member declared as an array, an
 *    entry will be generated only for the first array element, regardless
 *    of its type.  For arrays of aggregate types, the enumeration rules are
 *    applied recursively for the single enumerated array element.
 *
 *  * For an active interface block not declared as an array of block
 *    instances, a single entry will be generated, using the block name from
 *    the shader source.
 *
 *  * For an active interface block declared as an array of instances,
 *    separate entries will be generated for each active instance.  The name
 *    of the instance is formed by concatenating the block name, the "["
 *    character, an integer identifying the instance number, and the "]"
 *    character.
 *
 *  * For an active subroutine, a single entry will be generated, using the
 *    subroutine name from the shader source.
 *
 * When an integer array element or block instance number is part of the name
 * string, it will be specified in decimal form without a "+" or "-" sign or
 * any extra leading zeroes.  Additionally, the name string will not include
 * white space anywhere in the string.
 */

struct subtest_index_t {
	const char *vs_text;

	GLenum programInterface;
	const char *name;
	GLint expect_value; /* 0 == any number >= 0, -1 == INVALID_INDEX */
	GLenum expected_error;

	const char *programInterface_str;
	const char *error_str;
};

#define ST(vs_text, programInterface, name, value, error) { \
	(vs_text), (programInterface), (name), (value), (error), \
	#programInterface, #error \
}

/* Test for arrays of arrays */
static const struct subtest_index_t index_subtests[] = {
 ST(vs_empty,      GL_ATOMIC_COUNTER_BUFFER,              "dummy", -1, GL_INVALID_ENUM),
 ST(vs_empty,                    GL_UNIFORM,                 NULL, -1, GL_NO_ERROR),
 ST(vs_empty,                    GL_UNIFORM,              "dummy", -1, GL_NO_ERROR),
 ST(vs_empty,                       GL_TRUE,           "vs_input",  0, GL_INVALID_ENUM),
 ST(vs_array,              GL_PROGRAM_INPUT,           "vs_input",  0, GL_NO_ERROR),
 ST(vs_array,              GL_PROGRAM_INPUT,        "vs_input[0]",  0, GL_NO_ERROR),
 ST(vs_array,              GL_PROGRAM_INPUT,        "vs_input[1]", -1, GL_NO_ERROR),
 ST(vs_array,                    GL_UNIFORM,              "hello", -1, GL_NO_ERROR),
 ST(vs_array,                    GL_UNIFORM,        "sa[0].hello",  0, GL_NO_ERROR),
 ST(vs_array,                    GL_UNIFORM,        "sa[0].world",  0, GL_NO_ERROR),
 ST(vs_array,                    GL_UNIFORM,     "sa[0].world[0]",  0, GL_NO_ERROR),
 ST(vs_array,                    GL_UNIFORM,        "sa[1].hello", -1, GL_NO_ERROR),
 ST( vs_aofa,              GL_PROGRAM_INPUT,          "vs_input2", -1, GL_NO_ERROR),
 ST( vs_aofa,              GL_PROGRAM_INPUT,       "vs_input2[0]",  0, GL_NO_ERROR),
 ST( vs_aofa,              GL_PROGRAM_INPUT,    "vs_input2[0][0]",  0, GL_NO_ERROR),
 ST( vs_aofa,              GL_PROGRAM_INPUT,    "vs_input2[1][0]", -1, GL_NO_ERROR),
 ST( vs_aofa,              GL_PROGRAM_INPUT,    "vs_input2[0][1]", -1, GL_NO_ERROR),
 ST(  vs_sub,          GL_VERTEX_SUBROUTINE,                "vss",  0, GL_NO_ERROR),
 ST(vs_empty, GL_TRANSFORM_FEEDBACK_VARYING,      "gl_NextBuffer", -1, GL_NO_ERROR),
 ST(vs_empty, GL_TRANSFORM_FEEDBACK_VARYING, "gl_SkipComponents1", -1, GL_NO_ERROR),
 ST(vs_empty, GL_TRANSFORM_FEEDBACK_VARYING, "gl_SkipComponents2", -1, GL_NO_ERROR),
 ST(vs_empty, GL_TRANSFORM_FEEDBACK_VARYING, "gl_SkipComponents3", -1, GL_NO_ERROR),
 ST(vs_empty, GL_TRANSFORM_FEEDBACK_VARYING, "gl_SkipComponents4", -1, GL_NO_ERROR),
};

static bool
check_extensions(const struct subtest_index_t st)
{
	if (st.programInterface == GL_ATOMIC_COUNTER_BUFFER &&
	    !piglit_is_extension_supported("GL_ARB_shader_atomic_counters")) {
		return false;
	}

	if ((st.programInterface == GL_VERTEX_SUBROUTINE ||
	     st.programInterface == GL_GEOMETRY_SUBROUTINE ||
	     st.programInterface == GL_FRAGMENT_SUBROUTINE ||
	     st.programInterface == GL_COMPUTE_SUBROUTINE ||
	     st.programInterface == GL_VERTEX_SUBROUTINE_UNIFORM ||
	     st.programInterface == GL_GEOMETRY_SUBROUTINE_UNIFORM ||
	     st.programInterface == GL_FRAGMENT_SUBROUTINE_UNIFORM ||
	     st.programInterface == GL_COMPUTE_SUBROUTINE_UNIFORM ||
	     st.programInterface == GL_TESS_CONTROL_SUBROUTINE ||
	     st.programInterface == GL_TESS_EVALUATION_SUBROUTINE ||
	     st.programInterface == GL_TESS_CONTROL_SUBROUTINE_UNIFORM ||
	     st.programInterface == GL_TESS_EVALUATION_SUBROUTINE_UNIFORM ||
	     st.programInterface == GL_COMPUTE_SUBROUTINE_UNIFORM) &&
	     !piglit_is_extension_supported("GL_ARB_shader_subroutine")) {
		 return false;
	 }

	if (st.vs_text == vs_aofa &&
	    !piglit_is_extension_supported("GL_ARB_arrays_of_arrays")) {
		return false;
	}

	return true;
}

static bool
consistency_check(GLuint prog, const struct subtest_index_t st, GLint index)
{
	const GLchar *names[] = { st.name };
	GLuint old_index = 0xdeadcafe;

	/* Validate result against old API. */
	switch (st.programInterface) {
	case GL_UNIFORM:
		glGetUniformIndices(prog, 1, names, &old_index);
		piglit_check_gl_error(GL_NO_ERROR);
		break;
	case GL_UNIFORM_BLOCK:
		old_index = glGetUniformBlockIndex(prog, st.name);
		piglit_check_gl_error(GL_NO_ERROR);
		break;
	case GL_VERTEX_SUBROUTINE:
		old_index = glGetSubroutineIndex(prog, GL_VERTEX_SHADER,
						 st.name);
		piglit_check_gl_error(GL_NO_ERROR);
		break;
	default:
		/* There are no old APIs for this program interface */
		return true;
	}

	if (index != old_index) {
		printf("Index inconsistent with the old API: %i vs %i\n",
		       index, old_index);
		return false;
	} else
		return true;
}

static void
run_index_subtest(const struct subtest_index_t st, bool *pass)
{
	enum piglit_result result;
	bool local_pass = true;
	GLint index;
	GLuint prog;

	if (!check_extensions(st)) {
		result = PIGLIT_SKIP;
		goto report_result;
	}

	prog = piglit_build_simple_program(st.vs_text, NULL);
	if (!piglit_link_check_status(prog)) {
		glDeleteProgram(prog);
		result = PIGLIT_FAIL;
		*pass = false;
		goto report_result;
	}

	index = glGetProgramResourceIndex(prog, st.programInterface, st.name);
	if (!piglit_check_gl_error(st.expected_error)) {
		printf("Call was glGetProgramResourceIndex(prog, %s, "
		       "%s, ...) = %i\n", st.programInterface_str, st.name,
		       index);
		local_pass = false;
	} else if (st.expected_error == GL_NO_ERROR) {
		if (index >=0 && st.expect_value != 0) {
			printf("Invalid index for '%s': expected INVALID_INDEX"
			       "but got %i\n", st.name, index);
			local_pass = false;
		} else if (index == GL_INVALID_INDEX && st.expect_value != -1) {
			printf("Invalid index for '%s': expected a valid index "
			       "but got INVALID_INDEX\n", st.name);
			local_pass = false;
		} else if (!consistency_check(prog, st, index)) {
			local_pass = false;
		}
	}

	glDeleteProgram(prog);

	*pass = *pass && local_pass;
	result = local_pass ? PIGLIT_PASS : PIGLIT_FAIL;

report_result:
	piglit_report_subtest_result(result, "'%s' on %s", st.name,
				     st.programInterface_str);
}

void
piglit_init(int argc, char **argv)
{
	piglit_require_extension("GL_ARB_program_interface_query");
}

enum piglit_result
piglit_display(void)
{
	bool pass = true, prg_tst;
	GLuint shader, test_count;
	int i;

	/* test using an unexisting program ID */
	glGetProgramResourceIndex(1337, GL_UNIFORM, "resource");
	prg_tst = piglit_check_gl_error(GL_INVALID_VALUE);
	pass = pass && prg_tst;
	piglit_report_subtest_result(prg_tst ? PIGLIT_PASS : PIGLIT_FAIL,
				     "Invalid program (undefined ID)");

	/* test using a shader ID */
	shader = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_empty);
	glGetProgramResourceIndex(shader, GL_UNIFORM, "resource");
	prg_tst = piglit_check_gl_error(GL_INVALID_OPERATION);
	pass = pass && prg_tst;
	piglit_report_subtest_result(prg_tst ? PIGLIT_PASS : PIGLIT_FAIL,
				     "Invalid program (call on shader)");

	/* run all the getprograminterfaceiv tests */
	test_count = sizeof(index_subtests) / sizeof(struct subtest_index_t);
	for (i = 0; i < test_count; i++) {
		run_index_subtest(index_subtests[i], &pass);
	}

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}