glsl-max-varyings.c 6.62 KB
Newer Older
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
/*
 * Copyright © 2010 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.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *
 */

/** @file glsl-max-varyings.c
 *
 * Tests whether each varying can be used at all numbers of varyings up to
 * GL_MAX_VARYING_FLOATS / 4.
 */

#include "piglit-util.h"

36
37
#define MAX_VARYING 32

38
/* 10x10 rectangles with 2 pixels of pad.  Deal with up to 32 varyings. */
39
int piglit_width = (2 + MAX_VARYING * 12), piglit_height = (2 + MAX_VARYING * 12);
40
41
42
43
44
45
46
47
48
int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;

/* Generate a VS that writes to num_varyings vec4s, and put
 * interesting data in data_varying with 0.0 everywhere else.
 */
static GLint get_vs(int num_varyings, int data_varying)
{
	GLuint shader;
	unsigned i;
49
	char code[2048], temp[2048];
50
51
52
53
54
55
56
57
58

	code[0] = 0;
	for (i = 0; i < num_varyings; i++) {
		sprintf(temp, "varying vec4 v%d;\n", i);
		strcat(code, temp);
	}

	sprintf(temp,
		"attribute vec4 green;\n"
59
		"attribute vec4 red;\n"
60
61
62
63
64
65
66
67
68
69
70
		"void main()\n"
		"{\n"
		"	gl_Position = (gl_ModelViewProjectionMatrix * \n"
		"			gl_Vertex);\n"
		);
	strcat(code, temp);

	for (i = 0; i < num_varyings; i++) {
		if (i == data_varying)
			sprintf(temp, "	v%d = green;\n", i);
		else
71
			sprintf(temp, "	v%d = red;\n", i);
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
		strcat(code, temp);
	}

	sprintf(temp,
		"}\n"
		);
	strcat(code, temp);

	shader = piglit_compile_shader_text(GL_VERTEX_SHADER, code);
	/* printf("%s\n", code); */

	return shader;
}

/* Generate a FS that does operations on num_varyings, yet makes only
 * data_varying contribute to the output.
 *
 * We could make a single FS per num_varyings that did this by using a
 * uniform for data_varying and some multiplication by comparisons
 * (see glsl-routing for an example), but since we're linking a new
 * shader each time anyway, this produces a simpler FS to read and
 * verify.
 */
static GLint get_fs(int num_varyings, int data_varying)
{
	GLuint shader;
	unsigned i;
99
	char code[2048], temp[2048];
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

	code[0] = 0;
	for (i = 0; i < num_varyings; i++) {
		sprintf(temp, "varying vec4 v%d;\n", i);
		strcat(code, temp);
	}

	sprintf(temp,
		"uniform float zero;\n"
		"uniform float one;\n"
		"void main()\n"
		"{\n"
		"	vec4 val = vec4(0.0);\n"
		);
	strcat(code, temp);

	for (i = 0; i < num_varyings; i++) {
		if (i == data_varying)
			sprintf(temp, "	val += one * v%d;\n", i);
		else
			sprintf(temp, "	val += zero * v%d;\n", i);
		strcat(code, temp);
	}

	sprintf(temp,
		"	gl_FragColor = val;\n"
		"}\n"
		);
	strcat(code, temp);

	/* printf("%s\n", code); */
	shader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, code);

	return shader;
}

static int
coord_from_index(int index)
{
	return 2 + 12 * index;
}

static void
143
draw(int num_varyings)
144
145
146
147
148
149
{
	int data_varying;
	float green[4][4] = { {0.0, 1.0, 0.0, 0.0},
			      {0.0, 1.0, 0.0, 0.0},
			      {0.0, 1.0, 0.0, 0.0},
			      {0.0, 1.0, 0.0, 0.0} };
150
151
152
153
	float red[4][4] = { {1.0, 0.0, 0.0, 0.0},
			    {1.0, 0.0, 0.0, 0.0},
			    {1.0, 0.0, 0.0, 0.0},
			    {1.0, 0.0, 0.0, 0.0} };
154
155
156
157

	glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float),
			      green);
	glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float),
158
			      red);
159
160
161
	glEnableVertexAttribArray(1);
	glEnableVertexAttribArray(2);

162
	for (data_varying = 0; data_varying < num_varyings; data_varying++) {
163
164
165
		GLuint prog, vs, fs;
		GLint loc;

166
167
		vs = get_vs(num_varyings, data_varying);
		fs = get_fs(num_varyings, data_varying);
168
169
170
171
172
173

		prog = glCreateProgram();
		glAttachShader(prog, vs);
		glAttachShader(prog, fs);

		glBindAttribLocation(prog, 1, "green");
174
		glBindAttribLocation(prog, 2, "red");
175
176
177

		glLinkProgram(prog);
		if (!piglit_link_check_status(prog))
178
			piglit_report_result(PIGLIT_FAIL);
179
180
181
182

		glUseProgram(prog);

		loc = glGetUniformLocation(prog, "zero");
183
		if (loc != -1) /* not used for num_varyings == 1 */
184
185
186
187
188
189
190
			glUniform1f(loc, 0.0);

		loc = glGetUniformLocation(prog, "one");
		assert(loc != -1); /* should always be used */
		glUniform1f(loc, 1.0);

		piglit_draw_rect(coord_from_index(data_varying),
191
				 coord_from_index(num_varyings - 1),
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
				 10,
				 10);

		glDeleteShader(vs);
		glDeleteShader(fs);
		glDeleteProgram(prog);
	}
}

enum piglit_result
piglit_display(void)
{
	GLint max_components;
	int max_varyings, row, col;
	GLboolean pass = GL_TRUE, warned = GL_FALSE;

	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

	glGetIntegerv(GL_MAX_VARYING_FLOATS, &max_components);
	max_varyings = max_components / 4;

213
	printf("GL_MAX_VARYING_FLOATS = %i\n", max_components);
214
215
216

	if (max_varyings > MAX_VARYING) {
		printf("test not designed to handle >%d varying vec4s.\n"
217
		       "(implementation reports %d components)\n",
218
219
		       max_components, MAX_VARYING);
		max_varyings = MAX_VARYING;
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
		warned = GL_TRUE;
	}

	glClearColor(0.5, 0.5, 0.5, 0.5);
	glClear(GL_COLOR_BUFFER_BIT);

	for (row = 0; row < max_varyings; row++) {
		draw(row + 1);
	}

	for (row = 0; row < max_varyings; row++) {
		for (col = 0; col <= row; col++) {
			GLboolean ok;
			float green[3] = {0.0, 1.0, 0.0};

235
236
237
238
			ok = piglit_probe_rect_rgb(coord_from_index(col),
						   coord_from_index(row),
						   10, 10,
						   green);
239
240
241
242
243
244
245
246
247
248
249
250
251
			if (!ok) {
				printf("  Failure with %d vec4 varyings used "
				       "in varying index %d\n",
				       row + 1, col);
				pass = GL_FALSE;
				break;
			}
		}
	}

	glutSwapBuffers();

	if (!pass)
252
		return PIGLIT_FAIL;
253
254
255
	if (warned)
		return PIGLIT_WARN;
	else
256
		return PIGLIT_PASS;
257
258
259
260
261
262
263
264
265
266
267
268
269
}

void piglit_init(int argc, char **argv)
{
	if (!GLEW_VERSION_2_0) {
		printf("Requires OpenGL 2.0\n");
		piglit_report_result(PIGLIT_SKIP);
	}

	printf("Vertical axis: Increasing numbers of varyings.\n");
	printf("Horizontal axis: Which of the varyings contains the color.\n");
}