1 /*
2  * Copyright © 2013 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /** @file vertices-in.c
25  *
26  * Check that the built-in geometry shader constant gl_VerticesIn has
27  * the correct value for all input primitive types.
28  *
29  * The test uses transform feedback to extract the value of
30  * gl_VerticesIn out of the shader.
31  */
32 
33 #include "piglit-util-gl.h"
34 
35 PIGLIT_GL_TEST_CONFIG_BEGIN
36 
37 	config.supports_gl_compat_version = 30;
38 	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB;
39 
40 PIGLIT_GL_TEST_CONFIG_END
41 
42 static const char *vs_text =
43 	"#version 130\n"
44 	"void main()\n"
45 	"{\n"
46 	"  gl_Position = vec4(0.0);\n"
47 	"}\n";
48 
49 static const char *gs_text =
50 	"#version 130\n"
51 	"#extension GL_ARB_geometry_shader4: require\n"
52 	"out int vertices_in;\n"
53 	"void main()\n"
54 	"{\n"
55 	"  vertices_in = gl_VerticesIn;\n"
56 	"  EmitVertex();\n"
57 	"}\n";
58 
59 static const char *varyings[] = { "vertices_in" };
60 
61 struct test_vector {
62 	GLenum prim_type;
63 	GLint vertices_in;
64 } test_vectors[] = {
65 	{ GL_POINTS,              1},
66 	{ GL_LINES,               2},
67 	{ GL_LINES_ADJACENCY,     4},
68 	{ GL_TRIANGLES,           3},
69 	{ GL_TRIANGLES_ADJACENCY, 6}
70 };
71 
72 void
piglit_init(int argc,char ** argv)73 piglit_init(int argc, char **argv)
74 {
75 	GLuint vs, gs, prog, buf;
76 	int i;
77 	GLint *ptr;
78 	bool pass = true;
79 
80 	/* Requirements */
81 	piglit_require_GLSL_version(130);
82 	piglit_require_extension("GL_ARB_geometry_shader4");
83 	piglit_require_extension("GL_EXT_transform_feedback");
84 
85 	/* Compile shaders, and prepare for linking.  We don't link
86 	 * yet because we're going to need to change the input
87 	 * primitive type inside the "for" loop below.
88 	 */
89 	prog = glCreateProgram();
90 	vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_text);
91 	glAttachShader(prog, vs);
92 	gs = piglit_compile_shader_text(GL_GEOMETRY_SHADER, gs_text);
93 	glAttachShader(prog, gs);
94 	glProgramParameteriARB(prog, GL_GEOMETRY_OUTPUT_TYPE_ARB, GL_POINTS);
95 	glProgramParameteriARB(prog, GL_GEOMETRY_VERTICES_OUT_ARB, 1);
96 	glTransformFeedbackVaryings(prog, 1, varyings,
97 				    GL_INTERLEAVED_ATTRIBS_EXT);
98 
99 	/* Set up the transform feedback buffer. */
100 	glGenBuffers(1, &buf);
101 	glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER_EXT, 0, buf);
102 	glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER_EXT, sizeof(GLint), NULL,
103 		     GL_STREAM_READ);
104 
105 	/* Use GL_RASTERIZER_DISCARD, since we are going to use
106 	 * transform feedback for this test.
107 	 */
108 	glEnable(GL_RASTERIZER_DISCARD);
109 
110 	if (!piglit_check_gl_error(GL_NO_ERROR))
111 		piglit_report_result(PIGLIT_FAIL);
112 
113 	for (i = 0; i < ARRAY_SIZE(test_vectors); i++) {
114 		printf("Testing %s:\n",
115 		       piglit_get_prim_name(test_vectors[i].prim_type));
116 		glProgramParameteriARB(prog, GL_GEOMETRY_INPUT_TYPE_ARB,
117 				       test_vectors[i].prim_type);
118 		glLinkProgram(prog);
119 		if (!piglit_link_check_status(prog))
120 			piglit_report_result(PIGLIT_FAIL);
121 		if (!piglit_check_gl_error(GL_NO_ERROR))
122 			piglit_report_result(PIGLIT_FAIL);
123 		glUseProgram(prog);
124 		glBeginTransformFeedback(GL_POINTS);
125 		glDrawArrays(test_vectors[i].prim_type, 0,
126 			     test_vectors[i].vertices_in);
127 		glEndTransformFeedback();
128 		if (!piglit_check_gl_error(GL_NO_ERROR))
129 			piglit_report_result(PIGLIT_FAIL);
130 		ptr = glMapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER_EXT,
131 				  GL_READ_ONLY);
132 		printf("  Expected gl_VerticesIn = %d, got %d\n",
133 		       test_vectors[i].vertices_in, *ptr);
134 		if (test_vectors[i].vertices_in != *ptr)
135 			pass = false;
136 		glUnmapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER_EXT);
137 		if (!piglit_check_gl_error(GL_NO_ERROR))
138 			piglit_report_result(PIGLIT_FAIL);
139 	}
140 
141 	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
142 }
143 
144 
145 enum piglit_result
piglit_display(void)146 piglit_display(void)
147 {
148 	/* Should never be reached */
149 	return PIGLIT_FAIL;
150 }
151