summaryrefslogtreecommitdiff
path: root/tests/spec/arb_separate_shader_objects/rendezvous_by_name.c
blob: 2137ee17fa65620ab15f814c4d15f8324968f744 (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
/*
 * Copyright © 2015 Gregory Hainaut <gregory.hainaut@gmail.com>
 *
 * 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 rendezvous_by_name.c
 * Simple test for separate shader objects that use rendezvous-by-name.
 *
 * Related to issue: https://bugs.freedesktop.org/show_bug.cgi?id=79783
 *
 * The test ensures deadcode optimization of input variables doesn't break
 * the rendezvous by name of the variables.
 */
#include "piglit-util-gl.h"
#include "sso-common.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 10;
	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;

PIGLIT_GL_TEST_CONFIG_END

static GLuint pipeline_3_out_1_in;
static GLuint pipeline_1_out_3_in;
static GLuint pipeline_inactive;

static const char *vs_code_3_out_template =
	"#version %d\n"
	"#extension GL_ARB_separate_shader_objects: require\n"
	"#extension GL_ARB_explicit_attrib_location: require\n"
	"\n"
	"layout(location = 0) in vec4 piglit_vertex;\n"
	"\n"
	"out vec4 blue;\n"
	"out vec4 green;\n"
	"out vec4 red;\n"
	"\n"
	"void main()\n"
	"{\n"
	"    gl_Position = piglit_vertex;\n"
	"    red   = vec4(1, 0, 0, 0);\n"
	"    green = vec4(0, 1, 0, 0);\n"
	"    blue  = vec4(0, 0, 1, 0);\n"
	"}\n"
	;

static const char *vs_code_1_out_template =
	"#version %d\n"
	"#extension GL_ARB_separate_shader_objects: require\n"
	"#extension GL_ARB_explicit_attrib_location: require\n"
	"\n"
	"layout(location = 0) in vec4 piglit_vertex;\n"
	"\n"
	"out vec4 blue;\n"
	"out vec4 green;\n"
	"out vec4 red;\n"
	"\n"
	"void main()\n"
	"{\n"
	"    gl_Position = piglit_vertex;\n"
	"    green = vec4(0, 1, 0, 0);\n"
	"}\n"
	;

static const char *vs_code_inactive_template =
	"#version %d\n"
	"#extension GL_ARB_separate_shader_objects: require\n"
	"#extension GL_ARB_explicit_attrib_location: require\n"
	"\n"
	"layout(location = 0) in vec4 piglit_vertex;\n"
	"\n"
	"#define MAX_VARYING %d\n"
	"out vec4 a_dummy[MAX_VARYING];\n"
	"out vec4 green;\n"
	"out vec4 z_dummy[MAX_VARYING];\n"
	"\n"
	"void main()\n"
	"{\n"
	"    gl_Position = piglit_vertex;\n"
	"    green = vec4(0, 1, 0, 0);\n"
	"    for(int i = 0; i < MAX_VARYING; i++) {\n"
	"        a_dummy[i] = vec4(1, 0, 0, 1);\n"
	"        z_dummy[i] = vec4(0, 0, 1, 1);\n"
	"    }\n"
	"}\n"
	;

static const char *fs_code_1_in_template =
	"#version %d\n"
	"#extension GL_ARB_separate_shader_objects: require\n"
	"#extension GL_ARB_explicit_attrib_location: enable\n"
	"\n"
	"#if __VERSION__ >= 130\n"
	"layout(location = 0) out vec4 out_color;\n"
	"#else\n"
	"#define out_color gl_FragColor\n"
	"#endif\n"
	"\n"
	"in vec4 blue;\n"
	"in vec4 green;\n"
	"in vec4 red;\n"
	"\n"
	"void main()\n"
	"{\n"
	"    out_color = vec4(green.xyz, 1);\n"
	"}\n"
	;

static const char *fs_code_3_in_template =
	"#version %d\n"
	"#extension GL_ARB_separate_shader_objects: require\n"
	"#extension GL_ARB_explicit_attrib_location: enable\n"
	"\n"
	"#if __VERSION__ >= 130\n"
	"layout(location = 0, index = 0) out vec4 out_color;\n"
	"layout(location = 0, index = 1) out vec4 avoid_opt;\n"
	"#else\n"
	"#define out_color gl_FragColor\n"
	"#endif\n"
	"\n"
	"in vec4 blue;\n"
	"in vec4 green;\n"
	"in vec4 red;\n"
	"\n"
	"void main()\n"
	"{\n"
	"    out_color = vec4(green.xyz, 1);\n"
	"    avoid_opt = vec4(blue + red);\n"
	"}\n"
	;

static const char *fs_code_inactive_template =
	"#version %d\n"
	"#extension GL_ARB_separate_shader_objects: require\n"
	"#extension GL_ARB_explicit_attrib_location: enable\n"
	"\n"
	"#if __VERSION__ >= 130\n"
	"layout(location = 0) out vec4 out_color;\n"
	"#else\n"
	"#define out_color gl_FragColor\n"
	"#endif\n"
	"\n"
	"#define MAX_VARYING %d\n"
	"in vec4 a_dummy[MAX_VARYING];\n"
	"in vec4 green;\n"
	"in vec4 z_dummy[MAX_VARYING];\n"
	"\n"
	"void main()\n"
	"{\n"
	"    out_color = vec4(green.xyz, 1);\n"
	"}\n"
	;

enum piglit_result
piglit_display(void)
{
	static const float expected[] = {
		0.0f, 1.0f, 0.0f, 1.0f
	};
	int h_width = piglit_width / 2;
	int h_height = piglit_height / 2;
	bool pass;
	bool pass1;
	bool pass2;
	bool pass3;

	glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
	glClear(GL_COLOR_BUFFER_BIT);

	/*
	 * Test 1: 3 active output in the VS + 1 active input in the FS.
	 * Screen location: bottom left
	 */
	glBindProgramPipeline(pipeline_3_out_1_in);
	piglit_draw_rect(-1, -1, 1, 1);

	/*
	 * Test 2: 1 active output in the VS + 3 active input in the FS.
	 * Screen location: top left
	 */
	glBindProgramPipeline(pipeline_1_out_3_in);
	piglit_draw_rect(-1, 0, 1, 1);

	/*
	 * Test 3: Link separate VS/FS together. Expect to optimize inactive variables
	 * Screen location: right
	 */
	if (pipeline_inactive) {
		glBindProgramPipeline(pipeline_inactive);
		piglit_draw_rect(0, -1, 1, 2);
	}

	/*
	 * Probe and report result
	 */
	pass1 = piglit_probe_rect_rgba(0, 0, h_width, h_height, expected);
	pass2 = piglit_probe_rect_rgba(0, h_height, h_width, h_height, expected);
	pass3 = !pipeline_inactive ||
		piglit_probe_rect_rgba(h_width, h_height, h_width, h_height, expected);

	piglit_present_results();

	pass = pass1 & pass2 & pass3;

	piglit_report_subtest_result(pass1 ? PIGLIT_PASS : PIGLIT_FAIL,
			"3 VS output => 1 FS input");
	piglit_report_subtest_result(pass2 ? PIGLIT_PASS : PIGLIT_FAIL,
			"1 VS output => 3 FS input");

	if (pipeline_inactive) {
		piglit_report_subtest_result(pass3 ? PIGLIT_PASS : PIGLIT_FAIL,
				"Unactive varying optimization in multi-shade separated program");
	}

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}

void piglit_init(int argc, char **argv)
{
	unsigned glsl_version;
	GLuint vs_prog_3_out;
	GLuint vs_prog_1_out;
	GLuint fs_prog_3_in;
	GLuint fs_prog_1_in;
	GLuint vs_fs_prog_separate_inactive;
	char *vs_source;
	char *fs_source;
	GLint max_varying;
	bool pass = true;

	piglit_require_GLSL_version(130); /* Support layout index on output color */
	piglit_require_extension("GL_ARB_separate_shader_objects");
	piglit_require_extension("GL_ARB_explicit_attrib_location");
	piglit_require_extension("GL_ARB_blend_func_extended");

	glsl_version = pick_a_glsl_version();

	glGetIntegerv(GL_MAX_VARYING_COMPONENTS, &max_varying);
	max_varying = (max_varying / 4u) - 1u;

	/*
	 * Program compilation and link
	 */
	printf("Compile vs_prog_3_out\n");
	vs_prog_3_out = format_and_link_program(GL_VERTEX_SHADER,
			vs_code_3_out_template, glsl_version);

	printf("Compile vs_prog_1_out\n");
	vs_prog_1_out = format_and_link_program(GL_VERTEX_SHADER,
			vs_code_1_out_template, glsl_version);

	printf("Compile fs_prog_3_in\n");
	fs_prog_3_in = format_and_link_program(GL_FRAGMENT_SHADER,
			fs_code_3_in_template, glsl_version);

	printf("Compile fs_prog_1_in\n");
	fs_prog_1_in = format_and_link_program(GL_FRAGMENT_SHADER,
			fs_code_1_in_template, glsl_version);

	(void)!asprintf(&vs_source, vs_code_inactive_template, glsl_version, max_varying);
	(void)!asprintf(&fs_source, fs_code_inactive_template, glsl_version, max_varying);

	pass &= piglit_check_gl_error(0);

	printf("Compile vs_fs_prog_separate_inactive\n");
	vs_fs_prog_separate_inactive = piglit_build_simple_program_unlinked(vs_source, fs_source);
	/* Manual linking so we can pack 2 separate-aware shaders into a single program */
	glProgramParameteri(vs_fs_prog_separate_inactive, GL_PROGRAM_SEPARABLE, GL_TRUE);
	glLinkProgram(vs_fs_prog_separate_inactive);

	if (!piglit_link_check_status(vs_fs_prog_separate_inactive)) {
		piglit_report_subtest_result(PIGLIT_SKIP,
				"Unactive varying optimization in multi-shade separated program");
		vs_fs_prog_separate_inactive = 0; // Skip program
		piglit_reset_gl_error(); // Clear pending error
	}

	free(vs_source);
	free(fs_source);

	/*
	 * Pipeline creation
	 */
	glGenProgramPipelines(1, &pipeline_3_out_1_in);
	glGenProgramPipelines(1, &pipeline_1_out_3_in);
	glBindProgramPipeline(pipeline_3_out_1_in);
	glUseProgramStages(pipeline_3_out_1_in,
			GL_VERTEX_SHADER_BIT, vs_prog_3_out);
	glUseProgramStages(pipeline_3_out_1_in,
			GL_FRAGMENT_SHADER_BIT, fs_prog_1_in);

	glBindProgramPipeline(pipeline_1_out_3_in);
	glUseProgramStages(pipeline_1_out_3_in,
			GL_VERTEX_SHADER_BIT, vs_prog_1_out);
	glUseProgramStages(pipeline_1_out_3_in,
			GL_FRAGMENT_SHADER_BIT, fs_prog_3_in);

	if (vs_fs_prog_separate_inactive) {
		glGenProgramPipelines(1, &pipeline_inactive);
		glBindProgramPipeline(pipeline_inactive);
		glUseProgramStages(pipeline_inactive,
				GL_VERTEX_SHADER_BIT | GL_FRAGMENT_SHADER_BIT,
				vs_fs_prog_separate_inactive);
	} else {
		pipeline_inactive = 0; // Skip the test
	}

	if (!piglit_check_gl_error(0) || !pass)
		piglit_report_result(PIGLIT_FAIL);
}