1 /*
2  * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
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 DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 /**
25  * EXT_transform_feedback test.
26  *
27  * Test writing separate vertex attribs into a buffer object.
28  */
29 
30 #include "piglit-util-gl.h"
31 
32 PIGLIT_GL_TEST_CONFIG_BEGIN
33 
34 	config.supports_gl_compat_version = 10;
35 	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
36 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
37 
38 PIGLIT_GL_TEST_CONFIG_END
39 
40 static const char *vstext = {
41 	"varying vec3 v3;"
42 	"varying vec2 v2;"
43 	"void main() {"
44 	"  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
45 	"  gl_FrontColor = vec4(1.0, 0.9, 0.8, 0.7);"
46 	"  gl_TexCoord[0] = vec4(0.5);"
47 	"  gl_TexCoord[1] = vec4(0.6, 0.0, 0.1, 0.6);"
48 	"  v2 = vec2(0.2, 0.7);"
49 	"  v3 = vec3(0.55, 0.66, 0.77);"
50 	"}"
51 };
52 
53 static const char *varyings[] = {"v3", "gl_FrontColor", "v2", "gl_TexCoord[1]"};
54 GLuint buf[4];
55 GLuint prog;
56 
57 #define NUM_OUT_VERTICES 6
58 
piglit_init(int argc,char ** argv)59 void piglit_init(int argc, char **argv)
60 {
61 	GLuint vs;
62 	GLint maxcomps, maxattrs;
63 	unsigned i;
64 
65 	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
66 
67 	/* Check the driver. */
68 	piglit_require_gl_version(15);
69 	piglit_require_GLSL();
70 	piglit_require_transform_feedback();
71 
72 	glGetIntegerv(GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS_EXT, &maxattrs);
73 	if (maxattrs < 4) {
74 		fprintf(stderr, "Not enough separate attribs supported by transform feedback.\n");
75 		piglit_report_result(PIGLIT_SKIP);
76 	}
77 	glGetIntegerv(GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT, &maxcomps);
78 	if (maxcomps < 4) {
79 		fprintf(stderr, "Not enough separate components supported by transform feedback.\n");
80 		piglit_report_result(PIGLIT_SKIP);
81 	}
82 
83 	/* Create shaders. */
84 	vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vstext);
85 	prog = glCreateProgram();
86 	glAttachShader(prog, vs);
87 	glTransformFeedbackVaryings(prog, sizeof(varyings)/sizeof(varyings[0]),
88 				    varyings, GL_SEPARATE_ATTRIBS_EXT);
89 	glLinkProgram(prog);
90 	if (!piglit_link_check_status(prog)) {
91 		glDeleteProgram(prog);
92 		piglit_report_result(PIGLIT_FAIL);
93 	}
94 
95 	/* Set up the transform feedback buffer. */
96 	glGenBuffers(4, buf);
97 	for (i = 0; i < 4; i++) {
98 		unsigned j;
99 		float *ptr;
100 		glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER_EXT, buf[i]);
101 		glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER_EXT,
102 			     NUM_OUT_VERTICES*4*sizeof(float), NULL, GL_STREAM_READ);
103 		ptr = glMapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER_EXT, GL_WRITE_ONLY);
104 		for (j = 0; j < NUM_OUT_VERTICES*4; j++) {
105 			ptr[j] = 0.123456;
106 		}
107 		glUnmapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER_EXT);
108 		glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER_EXT, i, buf[i]);
109 	}
110 
111 	if (!piglit_check_gl_error(GL_NO_ERROR))
112 		piglit_report_result(PIGLIT_FAIL);
113 
114 	glClearColor(0.2, 0.2, 0.2, 1.0);
115 	glEnableClientState(GL_VERTEX_ARRAY);
116 }
117 
piglit_display(void)118 enum piglit_result piglit_display(void)
119 {
120 	bool pass = true;
121 	static const float verts[] = {
122 		10, 10,
123 		10, 20,
124 		20, 20,
125 		20, 10
126 	};
127 	static const float v3[] = {0.55, 0.66, 0.77};
128 	static const float frontcolor[] = {1.0, 0.9, 0.8, 0.7};
129 	static const float v2[] = {0.2, 0.7};
130 	static const float texcoord1[] = {0.6, 0.0, 0.1, 0.6};
131 
132 	glClear(GL_COLOR_BUFFER_BIT);
133 
134 	/* Render into TFBO. */
135 	glLoadIdentity();
136 	glUseProgram(prog);
137 	glEnable(GL_RASTERIZER_DISCARD);
138 	glBeginTransformFeedback(GL_TRIANGLES);
139 	glVertexPointer(2, GL_FLOAT, 0, verts);
140 	glDrawArrays(GL_QUADS, 0, 4);
141 	glEndTransformFeedback();
142 	glDisable(GL_RASTERIZER_DISCARD);
143 
144 	if (!piglit_check_gl_error(GL_NO_ERROR))
145 		piglit_report_result(PIGLIT_FAIL);
146 
147 	pass = piglit_probe_buffer(buf[0], GL_TRANSFORM_FEEDBACK_BUFFER_EXT,
148 			"Buffer[0]", NUM_OUT_VERTICES, 3, v3) && pass;
149 	pass = piglit_probe_buffer(buf[1], GL_TRANSFORM_FEEDBACK_BUFFER_EXT,
150 			"Buffer[1]", NUM_OUT_VERTICES, 4, frontcolor)&& pass;
151 	pass = piglit_probe_buffer(buf[2], GL_TRANSFORM_FEEDBACK_BUFFER_EXT,
152 			"Buffer[2]", NUM_OUT_VERTICES, 2, v2) && pass;
153 	pass = piglit_probe_buffer(buf[3], GL_TRANSFORM_FEEDBACK_BUFFER_EXT,
154 			"Buffer[3]", NUM_OUT_VERTICES, 4, texcoord1) && pass;
155 
156 	if (!piglit_check_gl_error(GL_NO_ERROR))
157 		piglit_report_result(PIGLIT_FAIL);
158 
159 	piglit_present_results();
160 
161 	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
162 }
163