summaryrefslogtreecommitdiff
path: root/tests/spec/arb_separate_shader_objects/active-sampler-conflict.c
blob: e4c6800b8affd176b7ef256d853568b186233992 (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
/*
 * 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 float vert[2] = {
	0.0, 0.0
};

void piglit_init(int argc, char **argv)
{
	GLuint prog;
	GLint s2_loc;
	GLint s3_loc;
	GLuint pipe;
	GLuint vao;
	GLuint bo;
	bool pass = true;

	piglit_require_extension("GL_ARB_separate_shader_objects");

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

	s2_loc = glGetUniformLocation(prog, "s2");
	if (s2_loc == -1) {
		fprintf(stderr, "Failed to get uniform location for s2.\n");
		pass = false;
	}

	s3_loc = glGetUniformLocation(prog, "s3");
	if (s3_loc == -1) {
		fprintf(stderr, "Failed to get uniform location for s3.\n");
		pass = false;
	}

	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);

	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

	/* 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;
	}

	/* 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;

	/* Clean up.
	 */
	glBindProgramPipeline(0);
	glDeleteProgram(prog);
	glDeleteProgramPipelines(1, &pipe);
	
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);

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

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}

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