2 * Copyright © 2013 Intel Corporation
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:
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
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
25 * Test that transform feedback occurs before flatshading
27 * Section 2.16(Transform Feedback) of OpenGL 3.2 Core says:
28 * "In transform feedback mode, attributes of the vertices of transformed
29 * primitives processed by a vertex shader, or primitives generated by a
30 * geometry shader if one is active, are written out to one or more buffer
31 * objects. The vertices are fed back after vertex color clamping, but before
32 * flatshading and clipping."
36 #include "piglit-util-gl.h"
38 PIGLIT_GL_TEST_CONFIG_BEGIN
40 config
.supports_gl_compat_version
= 32;
41 config
.supports_gl_core_version
= 32;
43 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
;
44 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
46 PIGLIT_GL_TEST_CONFIG_END
48 static const char *vstext
=
52 "flat out vec3 color1;\n"
55 " gl_Position = vec4(vert1, 1.);\n"
60 static const char *fstext
=
63 "flat in vec3 color1;\n"
65 " gl_FragColor = vec4(color1, 1.);\n"
69 static GLuint vertBuff
;
70 static GLuint indexBuf
;
71 static GLuint vertColorBuf
;
73 static GLfloat vertices
[] = {
79 static GLsizei vertSize
= sizeof(vertices
);
81 static GLfloat vertColors
[] = {
87 static GLsizei vertColorSize
= sizeof(vertColors
);
89 static GLuint indices
[] = {
92 static GLsizei indSize
= sizeof(indices
);
94 static GLuint xfb_buf
;
97 /* Only capture the color data into the transform feedback buffer */
98 static const char *varyings
[] = { "color1" };
100 /* Calculate number of floats contained in the transform varyings */
101 static int numVaryingFloats
= ARRAY_SIZE(varyings
) * ARRAY_SIZE(indices
) * 3;
104 piglit_init(int argc
, char **argv
)
107 GLuint vs
= 0, fs
= 0;
109 GLuint vertColorIndex
;
111 prog
= glCreateProgram();
112 vs
= piglit_compile_shader_text(GL_VERTEX_SHADER
, vstext
);
113 fs
= piglit_compile_shader_text(GL_FRAGMENT_SHADER
, fstext
);
114 glAttachShader(prog
, vs
);
115 glAttachShader(prog
, fs
);
116 glTransformFeedbackVaryings(prog
, ARRAY_SIZE(varyings
), varyings
,
117 GL_INTERLEAVED_ATTRIBS
);
119 if(!piglit_link_check_status(prog
)){
120 glDeleteProgram(prog
);
121 piglit_report_result(PIGLIT_FAIL
);
126 glGenBuffers(1, &vertBuff
);
127 glBindBuffer(GL_ARRAY_BUFFER
, vertBuff
);
128 glBufferData(GL_ARRAY_BUFFER
, vertSize
, vertices
, GL_STATIC_DRAW
);
130 glGenBuffers(1, &indexBuf
);
131 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER
, indexBuf
);
132 glBufferData(GL_ELEMENT_ARRAY_BUFFER
, indSize
,
133 indices
, GL_STATIC_DRAW
);
135 glGenBuffers(1, &vertColorBuf
);
136 glBindBuffer(GL_ARRAY_BUFFER
, vertColorBuf
);
137 glBufferData(GL_ARRAY_BUFFER
, vertColorSize
,
138 vertColors
, GL_STATIC_DRAW
);
140 glGenVertexArrays(1, &vao
);
141 glBindVertexArray(vao
);
143 vertIndex
= glGetAttribLocation(prog
, "vert1");
144 vertColorIndex
= glGetAttribLocation(prog
, "color");
146 glBindBuffer(GL_ARRAY_BUFFER
, vertBuff
);
147 glEnableVertexAttribArray(vertIndex
);
148 glVertexAttribPointer(vertIndex
, 3, GL_FLOAT
, GL_FALSE
, 0, 0);
150 glBindBuffer(GL_ARRAY_BUFFER
, vertColorBuf
);
151 glEnableVertexAttribArray(vertColorIndex
);
152 glVertexAttribPointer(vertColorIndex
, 3, GL_FLOAT
, GL_FALSE
, 0, 0);
154 glGenBuffers(1, &xfb_buf
);
155 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER
, 0, xfb_buf
);
156 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER
,
157 numVaryingFloats
* sizeof(float),
158 NULL
, GL_STREAM_READ
);
160 pass
= piglit_check_gl_error(GL_NO_ERROR
) && pass
;
170 glClearColor(0.2, 0.2, 0.2, 1.0);
171 glClear(GL_COLOR_BUFFER_BIT
);
173 glBindVertexArray(vao
);
174 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER
, indexBuf
);
176 glBeginTransformFeedback(GL_TRIANGLES
);
177 glDrawElements(GL_TRIANGLES
, ARRAY_SIZE(indices
),
178 GL_UNSIGNED_INT
, NULL
);
179 glEndTransformFeedback();
181 xBuffer
= (float*)glMapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER
, GL_READ_ONLY
);
182 /* Compare transform feedback color data with original color data */
183 for(i
= 0; i
< numVaryingFloats
/3; i
++) {
184 bool unchanged
= true;
185 if(xBuffer
[i
*3+0] != vertColors
[0+3*indices
[i
]])
187 if(xBuffer
[i
*3+1] != vertColors
[1+3*indices
[i
]])
189 if(xBuffer
[i
*3+2] != vertColors
[2+3*indices
[i
]])
192 printf("Incorrect data for vertex %d:\n", i
);
193 printf("Expected (%f, %f, %f)\n",
194 vertColors
[0+3*indices
[i
]],
195 vertColors
[1+3*indices
[i
]],
196 vertColors
[2+3*indices
[i
]]);
197 printf("Actual (%f, %f, %f)\n",
204 glUnmapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER
);
206 piglit_present_results();
208 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;