summaryrefslogtreecommitdiff
path: root/tests/spec/arb_separate_shader_objects/active-sampler-conflict.c
blob: 0a91f49a9d8f5f207047db54bdbfe385e33615d0 (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
/*
 * 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 active-sampler-conflict.c
 * Verify results of program pipeline validation when a conflicting sampler
 * configuration is used.
 *
 * Section 2.11.11 (Shader Execution), subheading "Validation," of the OpenGL
 * 4.1 spec says:
 *
 *     "[INVALID_OPERATION] is generated by any command that transfers
 *     vertices to the GL if:
 *
 *         ...
 *
 *         - Any two active samplers in the current program object are of
 *           different types, but refer to the same texture image unit."
 *
 * This test verifies this behavior several ways.  First, an invalid
 * configuration is constructed.  When the pipeline is in the invalid
 * configuration, glValidateProgramPipeline is used to determine the status.
 *
 * The pipeline is then transitioned to a valid configuration, and
 * glValidateProgramPipeline is called again.
 *
 * The pipeline is then transitioned back to the invalid configuration.
 * Without calling glValidateProgramPipeline, glDrawArrays is called.  This
 * should generate the aforementioned error.  While still in the invalid state
 * glValidateProgramPipeline is called a third time.
 *
 * Finally, the pipeline is transitioned back to the valid configuration.
 * Without calling glValidateProgramPipeline, glDrawArrays is called.  This
 * should not generate any error.
 *
 * All of the state flip-flopping is done in an attempt to catch
 * implementations that latch state in glValidateProgramPipeline and do not
 * re-validate in glDrawArrays.
 */
#include "piglit-util-gl.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 30;
	config.supports_gl_core_version = 31;

PIGLIT_GL_TEST_CONFIG_END

static const char *fs_code =
	"#version 130\n"
	"#extension GL_ARB_separate_shader_objects: require\n"
	"\n"
	"out vec4 out_color;\n"
	"\n"
	"uniform sampler2D s2;\n"
	"uniform sampler3D s3;\n"
	"\n"
	"void main()\n"
	"{\n"
	"    out_color = texture(s2, vec2(0)) + texture(s3, vec3(0));\n"
	"}\n"
	;

static const char *fs_arrays_code =
	"#version 130\n"
	"#extension GL_ARB_separate_shader_objects: require\n"
	"\n"
	"out vec4 out_color;\n"
	"\n"
	"uniform sampler2D s2[2];\n"
	"uniform sampler3D s3[2];\n"
	"\n"
	"void main()\n"
	"{\n"
	"    out_color = texture(s2[1], vec2(0)) + texture(s3[1], vec3(0));\n"
	"}\n"
	;

static const char *fs_arrays_of_arrays_code =
	"#version 130\n"
	"#extension GL_ARB_separate_shader_objects: require\n"
	"#extension GL_ARB_arrays_of_arrays: require\n"
	"\n"
	"out vec4 out_color;\n"
	"\n"
	"uniform sampler2D s2[2][2];\n"
	"uniform sampler3D s3[2][2];\n"
	"\n"
	"void main()\n"
	"{\n"
	"    out_color = texture(s2[1][1], vec2(0)) + texture(s3[1][1], vec3(0));\n"
	"}\n"
	;

static const float vert[2] = {
	0.0, 0.0
};

static bool
setup_program(GLuint *prog, GLuint *pipe, GLuint *vao,
	      GLuint *bo, const char **fs_code)
{
	bool pass = true;

	*prog = glCreateShaderProgramv(GL_FRAGMENT_SHADER, 1,
				      (const GLchar *const *) fs_code);
	piglit_link_check_status(*prog);

	glGenProgramPipelines(1, pipe);
	glUseProgramStages(*pipe,
			   GL_FRAGMENT_SHADER_BIT,
			   *prog);
	glActiveShaderProgram(*pipe, *prog);
	glBindProgramPipeline(*pipe);

	glGenVertexArrays(1, vao);
	glBindVertexArray(*vao);

	/* Configure a vertex array object and buffer object that will
	 * be used for drawing later.
	 */
	glGenBuffers(1, bo);
	glBindBuffer(GL_ARRAY_BUFFER, *bo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vert), vert, GL_STATIC_DRAW);

	return piglit_check_gl_error(GL_NO_ERROR) && pass;
}

static void
cleanup(GLuint prog, GLuint *pipe, GLuint *vao, GLuint *bo)
{
	glBindProgramPipeline(0);
	glDeleteProgram(prog);
	glDeleteProgramPipelines(1, pipe);

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);

	glDeleteBuffers(1, bo);
	glDeleteVertexArrays(1, vao);
}

static bool
get_uniform_location(GLuint prog, GLint *loc, char *uni_name)
{
	bool pass = true;
	*loc = glGetUniformLocation(prog, uni_name);
	if (*loc == -1) {
		fprintf(stderr, "Failed to get uniform location for %s.\n",
                        uni_name);
		pass = false;
	}
	return pass;
}

static bool
test_sampler_conflict(GLuint prog, GLuint pipe,
		      char *s2_uni_name, char *s3_uni_name)
{
	GLint s2_loc;
	GLint s3_loc;
	bool pass = true;

	pass = get_uniform_location(prog, &s2_loc, s2_uni_name);
	pass = get_uniform_location(prog, &s3_loc, s3_uni_name);

	/* First, try an invalid configuration.
	 */
	glUniform1i(s2_loc, 1);
	glUniform1i(s3_loc, 1);
	if (piglit_program_pipeline_check_status_quiet(pipe)) {
		fprintf(stderr,
			"Pipeline was validated with conflicting "
			"sampler configuration.\n");
		pass = false;
	}

	/* Now try a valid configuration.
	 */
	glUniform1i(s2_loc, 1);
	glUniform1i(s3_loc, 2);
	if (!piglit_program_pipeline_check_status_quiet(pipe)) {
		fprintf(stderr,
			"Pipeline was not validated with non-conflicting "
			"sampler configuration.\n");
		pass = false;
	}

	/* Switch back to the invalid configuration.  Without first calling
	 * glValidateProgramPipeline, try to draw something.  Verify that
	 * GL_INVALID_OPERATION is generated.
	 */
	glUniform1i(s2_loc, 1);
	glUniform1i(s3_loc, 1);

	glDrawArrays(GL_POINTS, 0, 1);
	
	pass = piglit_check_gl_error(GL_INVALID_OPERATION) && pass;

	/* Re-validate the program.
	 */
	if (piglit_program_pipeline_check_status_quiet(pipe)) {
		fprintf(stderr,
			"Pipeline was validated with conflicting "
			"sampler configuration (second attempt).\n");
		pass = false;
	}

	pass = piglit_check_gl_error(GL_INVALID_OPERATION) && pass;

	/* Switch back to the valid configuration.  Without first calling
	 * glValidateProgramPipeline, try to draw something.  Verify that
	 * no error is generated.
	 */
	glUniform1i(s2_loc, 1);
	glUniform1i(s3_loc, 2);

	glDrawArrays(GL_POINTS, 0, 1);

	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

	return pass;
}

void piglit_init(int argc, char **argv)
{
	GLuint prog;
	GLuint pipe;
	GLuint vao;
	GLuint bo;
	char *s2_uni_name = "s2";
	char *s3_uni_name = "s3";
	char *s2_uni_array_name = "s2[1]";
	char *s3_uni_array_name = "s3[1]";
	bool pass;

	piglit_require_extension("GL_ARB_separate_shader_objects");

	pass = setup_program(&prog, &pipe, &vao, &bo, &fs_code);
	pass = test_sampler_conflict(prog, pipe,
				     s2_uni_name, s3_uni_name) && pass;
	cleanup(prog, &pipe, &vao, &bo);

	pass = setup_program(&prog, &pipe, &vao, &bo, &fs_arrays_code);
	pass = test_sampler_conflict(prog, pipe, s2_uni_array_name,
				     s3_uni_array_name) && pass;
	cleanup(prog, &pipe, &vao, &bo);

	if (piglit_is_extension_supported("GL_ARB_arrays_of_arrays")) {
		char *s2_uni_aoa_name = "s2[1][1]";
		char *s3_uni_aoa_name = "s3[1][1]";

		pass = setup_program(&prog, &pipe, &vao, &bo,
				     &fs_arrays_of_arrays_code);
		pass = test_sampler_conflict(prog, pipe, s2_uni_aoa_name,
					     s3_uni_aoa_name) && pass;
		cleanup(prog, &pipe, &vao, &bo);
	}

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}

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