ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_direct_state_access / transformfeedback-bufferrange.c
blob9ec3fb57947e80665e37eaf921a10d070dc303af
1 /*
2 * Copyright © 2015 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
21 * DEALINGS IN THE SOFTWARE.
24 /** @file transformfeedback-bufferrange.c
25 * Simple transform feedback test drawing GL_POINTS and making use of the
26 * transform-feedback-related direct state access entry points.
28 * Greatly inspired from ext_transform_feedback/points.c.
30 * From OpenGL 4.5, section 13.2.2 "Transform Feedback Primitive Capture",
31 * page 422:
33 * "void TransformFeedbackBufferRange(uint xfb, uint index, uint buffer,
34 * intptr offset, sizeiptr size);
36 * xfb must be zero, indicating the default transform feedback object, or the
37 * name of an existing transform feedback object. buffer must be zero or the
38 * name of an existing buffer object.
40 * TransformFeedbackBufferRange and TransformFeedbackBufferBase behave
41 * similarly to BindBufferRange and BindBufferBase, respectively, except
42 * that the target of the operation is xfb, and they do not affect any binding
43 * to the generic TRANSFORM_FEEDBACK_BUFFER target.
45 * Errors
46 * An INVALID_OPERATION error is generated if xfb is not zero or the name
47 * of an existing transform feedback object.
48 * An INVALID_VALUE error is generated if buffer is not zero or the name of
49 * an existing buffer object.
50 * An INVALID_VALUE error is generated if index is greater than or equal
51 * to the number of binding points for transform feedback, as described in
52 * section 6.7.1.
53 * An INVALID_VALUE error is generated by TransformFeedbackBufferRange
54 * if offset is negative.
55 * An INVALID_VALUE error is generated by TransformFeedbackBufferRange
56 * if size is less than or equal to zero.
57 * An INVALID_VALUE error is generated by TransformFeedbackBufferRange
58 * if offset or size do not satisfy the constraints described for those
59 * parameters for transform feedback array bindings, as described in
60 * section 6.7.1."
63 #include "piglit-util-gl.h"
64 #include "dsa-utils.h"
66 PIGLIT_GL_TEST_CONFIG_BEGIN
67 config.supports_gl_core_version = 31;
68 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
69 PIGLIT_GL_TEST_CONFIG_END
72 static GLuint prog;
73 static GLuint xfb_buf[3], input_buf, vao;
74 static const int xfb_buf_size = 500;
75 static const int offset = 16;
77 static const char *vstext = {
78 "#version 140\n"
79 "in float valIn;"
80 "out float valOut1;"
81 "out float valOut2;"
82 "void main() {"
83 " valOut1 = valIn + 1;"
84 " valOut2 = valIn * 2;"
85 "}"
88 #define NUM_INPUTS 4
89 static const GLfloat inputs[NUM_INPUTS] = {-1.0, 0.0, 1.0, 3.0};
90 static const GLfloat out1_ret[NUM_INPUTS] = { 0.0, 1.0, 2.0, 4.0};
91 static const GLfloat out2_ret[NUM_INPUTS] = {-2.0, 0.0, 2.0, 6.0};
93 void
94 piglit_init(int argc, char **argv)
96 static const char *varyings[] = { "valOut1", "valOut2" };
97 GLint inAttrib;
99 /* Check the driver. */
100 piglit_require_extension("GL_ARB_transform_feedback3");
101 piglit_require_extension("GL_ARB_direct_state_access");
103 /* Create shaders. */
104 prog = piglit_build_simple_program_unlinked(vstext, NULL);
105 glTransformFeedbackVaryings(prog, 2, varyings,
106 GL_SEPARATE_ATTRIBS);
107 glLinkProgram(prog);
108 if (!piglit_link_check_status(prog)) {
109 glDeleteProgram(prog);
110 piglit_report_result(PIGLIT_FAIL);
112 glUseProgram(prog);
114 /* Set up the Vertex Array Buffer */
115 glEnable(GL_VERTEX_ARRAY);
116 glGenVertexArrays(1, &vao);
117 glBindVertexArray(vao);
119 /* Set up the input data buffer */
120 glGenBuffers(1, &input_buf);
121 glBindBuffer(GL_ARRAY_BUFFER, input_buf);
122 glBufferData(GL_ARRAY_BUFFER, sizeof(inputs), inputs, GL_STATIC_DRAW);
123 inAttrib = glGetAttribLocation(prog, "valIn");
124 piglit_check_gl_error(GL_NO_ERROR);
125 glVertexAttribPointer(inAttrib, 1, GL_FLOAT, GL_FALSE, 0, 0);
126 glEnableVertexAttribArray(inAttrib);
129 static bool
130 equal(float a, float b)
132 return fabsf(a - b) < piglit_tolerance[0];
135 enum piglit_result
136 piglit_display(void)
138 GLint max_bind_points = 0;
139 GLuint q, num_prims;
140 bool pass = true, test = true;
141 GLfloat *v, *w;
142 int i;
144 /* init the transform feedback buffers */
145 glGenBuffers(3, xfb_buf);
146 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, xfb_buf[2]);
147 piglit_check_gl_error(GL_NO_ERROR);
149 /* Fetch the number of bind points */
150 glGetIntegerv(GL_MAX_TRANSFORM_FEEDBACK_BUFFERS, &max_bind_points);
151 PIGLIT_SUBTEST_ERROR(GL_NO_ERROR, pass,
152 "fetch maximum number of bind points");
154 if (piglit_khr_no_error)
155 goto valid_calls;
157 /* bind a non-existing transform feedback BO */
158 glTransformFeedbackBufferRange(1337, 0, 0, 0, 4096);
159 PIGLIT_SUBTEST_ERROR(GL_INVALID_OPERATION, pass,
160 "bind non-existing transform feedback BO");
162 /* bind a non-existing output BO */
163 glTransformFeedbackBufferRange(0, 0, 1337, 0, 4096);
164 PIGLIT_SUBTEST_ERROR(GL_INVALID_VALUE, pass,
165 "bind a non-existing output BO");
167 /* bind to a negative index */
168 glTransformFeedbackBufferRange(0, -1, xfb_buf[2], 0, 4096);
169 PIGLIT_SUBTEST_ERROR(GL_INVALID_VALUE, pass, "bind negative index");
171 /* bind to an index == max */
172 glTransformFeedbackBufferRange(0, max_bind_points, xfb_buf[2], 0,
173 4096);
174 PIGLIT_SUBTEST_ERROR(GL_INVALID_VALUE, pass, "bind to index == "
175 "max_bind_points (%i)", max_bind_points);
177 /* bind at a non-aligned offset */
178 glTransformFeedbackBufferRange(0, 0, xfb_buf[2], 3, 4096);
179 PIGLIT_SUBTEST_ERROR(GL_INVALID_VALUE, pass,
180 "bind at a non-aligned offset");
182 /* bind with a non-aligned size */
183 glTransformFeedbackBufferRange(0, 0, xfb_buf[2], 0, 4095);
184 PIGLIT_SUBTEST_ERROR(GL_INVALID_VALUE, pass,
185 "bind with a non-aligned size");
187 valid_calls:
188 /* Set up the transform feedback buffer */
189 for (i = 0; i < 2; i++) {
190 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, xfb_buf[i]);
191 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER,
192 xfb_buf_size, NULL, GL_STREAM_READ);
193 glTransformFeedbackBufferRange(0, i, xfb_buf[i], offset,
194 xfb_buf_size);
195 piglit_check_gl_error(GL_NO_ERROR);
198 /* Set up the query that checks the # of primitives handled */
199 glGenQueries(1, &q);
200 glBeginQuery(GL_PRIMITIVES_GENERATED, q);
201 piglit_check_gl_error(GL_NO_ERROR);
203 /* do the transform feedback */
204 glBeginTransformFeedback(GL_POINTS);
205 glBindBuffer(GL_ARRAY_BUFFER, input_buf);
206 glDrawArrays(GL_POINTS, 0, NUM_INPUTS);
207 glEndTransformFeedback();
209 glEndQuery(GL_PRIMITIVES_GENERATED);
211 /* check the number of primitives */
212 glGetQueryObjectuiv(q, GL_QUERY_RESULT, &num_prims);
213 glDeleteQueries(1, &q);
214 printf("%u primitives generated:\n", num_prims);
215 if (num_prims != NUM_INPUTS) {
216 printf("Incorrect number of prims generated.\n");
217 printf("Found %u, expected %u\n", num_prims, NUM_INPUTS);
218 pass = false;
219 test = false;
222 /* check the result */
223 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, xfb_buf[0]);
224 v = (GLfloat *)((GLbyte *)glMapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, GL_READ_ONLY) + offset);
225 piglit_check_gl_error(GL_NO_ERROR);
226 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, xfb_buf[1]);
227 w = (GLfloat *)((GLbyte *)glMapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, GL_READ_ONLY) + offset);
228 piglit_check_gl_error(GL_NO_ERROR);
230 for (i = 0; i < num_prims; i++) {
231 printf("%2d: (%2.0g, %2.0g) : ", i, v[i], w[i]);
232 if (!equal(v[i], out1_ret[i]) || !equal(w[i], out2_ret[i])) {
233 printf("NOK, expected (%2.0g, %2.0g)\n",
234 out1_ret[i], out2_ret[i]);
235 test = false;
236 } else
237 printf("OK\n");
239 PIGLIT_SUBTEST_CONDITION(test, pass, "general test");
241 piglit_present_results();
243 /* clean-up everything */
244 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, xfb_buf[0]);
245 glUnmapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER);
246 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, xfb_buf[1]);
247 glUnmapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER);
248 glDeleteBuffers(3, xfb_buf);
249 glDeleteBuffers(1, &input_buf);
250 glDeleteVertexArrays(1, &vao);
251 glDeleteProgram(prog);
253 return pass ? PIGLIT_PASS : PIGLIT_FAIL;