ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_provoking_vertex / xfb-before-flatshading.c
blob12470b45867a2d4c016d6755b7ed59296aa878dd
1 /**
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
13 * Software.
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.
24 /**
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 =
49 "#version 130\n"
50 "in vec3 vert1;\n"
51 "in vec3 color;\n"
52 "flat out vec3 color1;\n"
53 "out vec3 vert;\n"
54 "void main() {\n"
55 " gl_Position = vec4(vert1, 1.);\n"
56 " vert = vert1;\n"
57 " color1 = color;\n"
58 "}\n";
60 static const char *fstext =
61 "#version 130\n"
62 "in vec3 vert;\n"
63 "flat in vec3 color1;\n"
64 "void main() {\n"
65 " gl_FragColor = vec4(color1, 1.);\n"
66 "}\n";
68 static GLuint vao;
69 static GLuint vertBuff;
70 static GLuint indexBuf;
71 static GLuint vertColorBuf;
73 static GLfloat vertices[] = {
74 -1.0, 1.0, 0.0,
75 1.0, 1.0, 0.0,
76 1.0,-1.0, 0.0,
77 -1.0,-1.0, 0.0
79 static GLsizei vertSize = sizeof(vertices);
81 static GLfloat vertColors[] = {
82 1.0, 0.0, 0.0,
83 0.0, 1.0, 0.0,
84 0.0, 0.0, 1.0,
85 0.0, 1.0, 0.0
87 static GLsizei vertColorSize = sizeof(vertColors);
89 static GLuint indices[] = {
90 0, 1, 2, 0, 2, 3
92 static GLsizei indSize = sizeof(indices);
94 static GLuint xfb_buf;
95 static GLuint prog;
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;
103 void
104 piglit_init(int argc, char **argv)
106 bool pass = true;
107 GLuint vs = 0, fs = 0;
108 GLuint vertIndex;
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);
118 glLinkProgram(prog);
119 if(!piglit_link_check_status(prog)){
120 glDeleteProgram(prog);
121 piglit_report_result(PIGLIT_FAIL);
124 glUseProgram(prog);
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;
163 enum piglit_result
164 piglit_display(void)
166 float *xBuffer;
167 int i;
168 bool pass = true;
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]])
186 unchanged = false;
187 if(xBuffer[i*3+1] != vertColors[1+3*indices[i]])
188 unchanged = false;
189 if(xBuffer[i*3+2] != vertColors[2+3*indices[i]])
190 unchanged = false;
191 if(!unchanged) {
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",
198 xBuffer[i*3+0],
199 xBuffer[i*3+1],
200 xBuffer[i*3+2]);
201 pass = false;
204 glUnmapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER);
206 piglit_present_results();
208 return pass ? PIGLIT_PASS : PIGLIT_FAIL;