1 /* Copyright © 2011 Intel Corporation
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice (including the next
11  * paragraph) shall be included in all copies or substantial portions of the
12  * Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
23 /**
24  * \file getfragdatalocation.c
25  *
26  * \author Ian Romanick
27  */
28 #include "piglit-util-gl.h"
29 
30 PIGLIT_GL_TEST_CONFIG_BEGIN
31 
32 	config.supports_gl_compat_version = 10;
33 	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
34 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
35 
36 PIGLIT_GL_TEST_CONFIG_END
37 
38 static const char *vs_text =
39 	"#version 130\n"
40 	"in vec4 vertex;\n"
41 	"void main() { gl_Position = vertex; }\n"
42 	;
43 
44 static const char *fs_text =
45 	"#version 130\n"
46 	"out vec4 v;\n"
47 	"out vec4 a[2];\n"
48 	"void main() {\n"
49 	"    v = vec4(0.0);\n"
50 	"    a[0] = vec4(1.0);\n"
51 	"    a[1] = vec4(2.0);\n"
52 	"}\n"
53 	;
54 
55 enum piglit_result
piglit_display(void)56 piglit_display(void)
57 {
58 	return PIGLIT_FAIL;
59 }
60 
piglit_init(int argc,char ** argv)61 void piglit_init(int argc, char **argv)
62 {
63 	GLint max_draw_buffers;
64 	GLuint prog;
65 	GLuint vs;
66 	GLuint fs;
67 	GLint loc;
68 
69 	piglit_require_gl_version(30);
70 
71 	/* This test needs some number of draw buffers, so make sure the
72 	 * implementation isn't broken.  This enables the test to generate a
73 	 * useful failure message.
74 	 */
75 	glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
76 	if (max_draw_buffers < 8) {
77 		fprintf(stderr,
78 			"OpenGL 3.0 requires GL_MAX_DRAW_BUFFERS >= 8.  "
79 			"Only got %d!\n",
80 			max_draw_buffers);
81 		piglit_report_result(PIGLIT_FAIL);
82 	}
83 
84 	prog = glCreateProgram();
85 	vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_text);
86 	fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_text);
87 	glAttachShader(prog, vs);
88 	glAttachShader(prog, fs);
89 	if (!piglit_check_gl_error(GL_NO_ERROR))
90 		piglit_report_result(PIGLIT_FAIL);
91 
92 	if (!piglit_khr_no_error) {
93 		/* Page 237 (page 253 of the PDF) of the OpenGL 3.0 spec says:
94 		 *
95 		 *     "If program has not been successfully linked, the error
96 		 *     INVALID OPERATION is generated. If name is not a varying
97 		 *     out variable, or if an error occurs, -1 will be
98 		 *     returned."
99 		 */
100 		printf("Querying location before linking...\n");
101 		loc = glGetFragDataLocation(prog, "v");
102 		if (!piglit_check_gl_error(GL_INVALID_OPERATION))
103 			piglit_report_result(PIGLIT_FAIL);
104 
105 		if (loc != -1) {
106 			fprintf(stderr, "Expected location = -1, got %d\n", loc);
107 			piglit_report_result(PIGLIT_FAIL);
108 		}
109 	}
110 
111 	glLinkProgram(prog);
112 	if (!piglit_check_gl_error(GL_NO_ERROR))
113 		piglit_report_result(PIGLIT_FAIL);
114 
115 	if (!piglit_link_check_status(prog)) {
116 		piglit_report_result(PIGLIT_FAIL);
117 	}
118 
119 	printf("Querying location of nonexistent variable...\n");
120 	loc = glGetFragDataLocation(prog, "waldo");
121 	if (!piglit_check_gl_error(GL_NO_ERROR))
122 		piglit_report_result(PIGLIT_FAIL);
123 
124 	if (loc != -1) {
125 		fprintf(stderr, "Expected location = -1, got %d\n", loc);
126 		piglit_report_result(PIGLIT_FAIL);
127 	}
128 
129 	/* Page 236 (page 252 of the PDF) of the OpenGL 3.0 spec says:
130 	 *
131 	 *     "BindFragDataLocation has no effect until the program is
132 	 *     linked. In particular, it doesn’t modify the bindings of
133 	 *     varying out variables in a program that has already been
134 	 *     linked."
135 	 */
136 	glBindFragDataLocation(prog, 0, "v");
137 	glBindFragDataLocation(prog, 1, "a");
138 	glLinkProgram(prog);
139 	if (!piglit_check_gl_error(GL_NO_ERROR))
140 		piglit_report_result(PIGLIT_FAIL);
141 
142 	if (!piglit_link_check_status(prog)) {
143 		piglit_report_result(PIGLIT_FAIL);
144 	}
145 
146 	printf("Querying locations after binding and linking...\n");
147 	loc = glGetFragDataLocation(prog, "v");
148 	if (!piglit_check_gl_error(GL_NO_ERROR))
149 		piglit_report_result(PIGLIT_FAIL);
150 
151 	if (loc != 0) {
152 		fprintf(stderr, "Expected location = 0, got %d\n", loc);
153 		piglit_report_result(PIGLIT_FAIL);
154 	}
155 
156 	loc = glGetFragDataLocation(prog, "a");
157 	if (!piglit_check_gl_error(GL_NO_ERROR))
158 		piglit_report_result(PIGLIT_FAIL);
159 
160 	if (loc != 1) {
161 		fprintf(stderr, "Expected location = 1, got %d\n", loc);
162 		piglit_report_result(PIGLIT_FAIL);
163 	}
164 
165 	printf("Querying locations after just binding...\n");
166 	glBindFragDataLocation(prog, 2, "v");
167 	glBindFragDataLocation(prog, 0, "a");
168 	if (!piglit_check_gl_error(GL_NO_ERROR))
169 		piglit_report_result(PIGLIT_FAIL);
170 
171 	loc = glGetFragDataLocation(prog, "v");
172 	if (!piglit_check_gl_error(GL_NO_ERROR))
173 		piglit_report_result(PIGLIT_FAIL);
174 
175 	if (loc != 0) {
176 		fprintf(stderr, "Expected location = 0, got %d\n", loc);
177 		piglit_report_result(PIGLIT_FAIL);
178 	}
179 
180 	loc = glGetFragDataLocation(prog, "a");
181 	if (!piglit_check_gl_error(GL_NO_ERROR))
182 		piglit_report_result(PIGLIT_FAIL);
183 
184 	if (loc != 1) {
185 		fprintf(stderr, "Expected location = 1, got %d\n", loc);
186 		piglit_report_result(PIGLIT_FAIL);
187 	}
188 
189 	piglit_report_result(PIGLIT_PASS);
190 }
191