perf/pixel-rate: new pixel throughput microbenchmark
[piglit.git] / tests / spec / ext_transform_feedback / generatemipmap.c
blob37f0ac7499acfd87d4bb3dfbd438b06f9ced7346
1 /*
2 * Copyright © 2011 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 /**
25 * \file generatemipmap.c
27 * Tests that glGenerateMipmapEXT works correctly even when
28 * GL_RASTERIZER_DISCARD and/or transform feedback is enabled. This
29 * is important to test because on some implementations,
30 * glGenerateMipmapEXT works by temporarily reconfiguring the graphics
31 * pipeline to generate the mipmap, and then restoring the old
32 * configuration. We need to ensure that GL_RASTERIZER_DISCARD and
33 * transform feedback are appropriately disabled while the mipmap is
34 * being generated, and then properly restored later.
36 * This test can be run in the following modes (specified by a command
37 * line argument):
39 * - discard: in this mode the glGenerateMipmapEXT call is made while
40 * GL_RASTERIZER_DISCARD is enabled, and the test verifies that
41 * GL_RASTERIZER_DISCARD is still enabled after the call to
42 * glGenerateMipmapEXT completes.
44 * - buffer: in this mode the glGenerateMipmapEXT call is made while
45 * transform feedback is active, and the test verifies that no
46 * vertices were output to the transform feedback buffer while the
47 * glGenerateMipmapEXT call was in progress. The verification is
48 * performed by checking that the contents of the transform feedback
49 * buffer were not overwritten.
51 * - prims_written: in this mode the glGenerateMipmapEXT call is made
52 * while transform feedback is active and while performing a
53 * GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN query, and the test
54 * verifies that the query reports that 0 primitives were written.
56 * - prims_generated: in this mode the glGenerateMipmapEXT call is
57 * made while performing a GL_PRIMITIVES_GENERATED query, and the
58 * test verifies that the query reports that 0 primitives were
59 * generated.
61 * This file is largely copy-and-pasted from
62 * tests/fbo/fbo-generatemipmap.c.
65 #include "piglit-util-gl.h"
67 #define TEX_WIDTH 256
68 #define TEX_HEIGHT 256
70 PIGLIT_GL_TEST_CONFIG_BEGIN
72 config.supports_gl_compat_version = 10;
74 config.window_width = 700;
75 config.window_height = 300;
76 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB;
77 config.khr_no_error_support = PIGLIT_NO_ERRORS;
79 PIGLIT_GL_TEST_CONFIG_END
81 static const float red[] = {1, 0, 0, 0};
82 static const float green[] = {0, 1, 0, 0.25};
83 static const float blue[] = {0, 0, 1, 0.5};
84 static const float white[] = {1, 1, 1, 1};
85 static GLboolean discard = GL_FALSE;
86 static GLboolean buffer = GL_FALSE;
87 static GLboolean prims_written = GL_FALSE;
88 static GLboolean prims_generated = GL_FALSE;
89 static GLuint xfb_prog;
90 static GLuint xfb_buf;
91 static GLuint prims_written_query;
92 static GLuint prims_generated_query;
94 static const char *vstext =
95 "varying float xfb_out;\n"
96 "\n"
97 "void main()\n"
98 "{\n"
99 " gl_Position = gl_Vertex;\n"
100 " xfb_out = gl_Vertex.x;\n"
101 "}\n";
103 static const char *xfb_varyings[] = { "xfb_out" };
105 static int
106 create_texture(void)
108 GLuint tex;
109 float *ptr;
111 /* Generate mipmap level 0 */
112 tex = piglit_rgbw_texture(GL_RGBA, TEX_WIDTH, TEX_WIDTH, GL_FALSE,
113 GL_TRUE, GL_FLOAT);
114 glBindTexture(GL_TEXTURE_2D, tex);
116 if (!piglit_check_gl_error(GL_NO_ERROR))
117 piglit_report_result(PIGLIT_FAIL);
119 /* Go into appropriate transform feedback or discard state */
120 if (discard)
121 glEnable(GL_RASTERIZER_DISCARD);
122 if (buffer || prims_written) {
123 float buffer[4096];
124 buffer[0] = 12345.0;
125 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, xfb_buf);
126 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(buffer),
127 buffer, GL_STREAM_READ);
128 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, xfb_buf);
129 glUseProgram(xfb_prog);
130 glBeginTransformFeedback(GL_POINTS);
132 if (prims_written) {
133 glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN,
134 prims_written_query);
136 if (prims_generated) {
137 glBeginQuery(GL_PRIMITIVES_GENERATED, prims_generated_query);
140 /* Ask the implementation to generate the remaining mipmap levels. */
141 glGenerateMipmapEXT(GL_TEXTURE_2D);
142 if (!piglit_check_gl_error(0))
143 piglit_report_result(PIGLIT_FAIL);
145 /* Check state */
146 if (discard) {
147 if (!glIsEnabled(GL_RASTERIZER_DISCARD)) {
148 printf("GL_RASTERIZER_DISCARD state not restored "
149 "after glGenerateMipmapEXT\n");
150 piglit_report_result(PIGLIT_FAIL);
152 glDisable(GL_RASTERIZER_DISCARD);
154 if (buffer || prims_written) {
155 glEndTransformFeedback();
156 glUseProgram(0);
158 if (buffer) {
159 ptr = glMapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER_EXT,
160 GL_READ_ONLY);
161 if (ptr[0] != 12345.0) {
162 printf("Transform feedback buffer was overwritten "
163 "during glGenerateMipmapEXT\n");
164 piglit_report_result(PIGLIT_FAIL);
167 if (prims_written) {
168 GLuint result;
169 glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
170 glGetQueryObjectuiv(prims_written_query,
171 GL_QUERY_RESULT, &result);
172 if (result != 0) {
173 printf("PRIMITIVES_WRITTEN counter was incremented "
174 "during glGenerateMipmapEXT\n");
175 piglit_report_result(PIGLIT_FAIL);
178 if (prims_generated) {
179 GLuint result;
180 glEndQuery(GL_PRIMITIVES_GENERATED);
181 glGetQueryObjectuiv(prims_generated_query,
182 GL_QUERY_RESULT, &result);
183 if (result != 0) {
184 printf("PRIMITIVES_GENERATED counter was incremented "
185 "during glGenerateMipmapEXT\n");
186 piglit_report_result(PIGLIT_FAIL);
190 return tex;
193 static void
194 draw_mipmap(int x, int y, int dim)
196 glViewport(0, 0, piglit_width, piglit_height);
197 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
199 glEnable(GL_TEXTURE_2D);
200 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
202 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
204 piglit_draw_rect_tex(x, y, dim, dim,
205 0, 0, 1, 1);
206 glDisable(GL_TEXTURE_2D);
209 static GLboolean
210 test_mipmap_drawing(int start_x, int start_y, int dim)
212 GLboolean pass = GL_TRUE;
213 pass = pass && piglit_probe_rect_rgb(
214 start_x, start_y, dim/2, dim/2, red);
215 pass = pass && piglit_probe_rect_rgb(
216 start_x + dim/2, start_y, dim/2, dim/2, green);
217 pass = pass && piglit_probe_rect_rgb(
218 start_x, start_y + dim/2, dim/2, dim/2, blue);
219 pass = pass && piglit_probe_rect_rgb(
220 start_x + dim/2, start_y + dim/2, dim/2, dim/2, white);
222 return pass;
225 enum piglit_result
226 piglit_display(void)
228 GLboolean pass = GL_TRUE;
229 int dim;
230 GLuint tex;
231 int x;
233 glClearColor(0.5, 0.5, 0.5, 0.5);
234 glClear(GL_COLOR_BUFFER_BIT);
236 tex = create_texture();
238 x = 1;
239 for (dim = TEX_WIDTH; dim > 1; dim /= 2) {
240 draw_mipmap(x, 1, dim);
241 x += dim + 1;
244 x = 1;
245 for (dim = TEX_WIDTH; dim > 1; dim /= 2) {
246 pass &= test_mipmap_drawing(x, 1, dim);
247 x += dim + 1;
250 glDeleteTextures(1, &tex);
252 piglit_present_results();
254 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
257 static void
258 print_usage_and_exit(char *prog_name)
260 printf("Usage: %s <mode>\n"
261 " where <mode> is one of:\n"
262 " discard\n"
263 " buffer\n"
264 " prims_written\n"
265 " prims_generated\n", prog_name);
266 exit(1);
269 void piglit_init(int argc, char **argv)
271 if (argc != 2)
272 print_usage_and_exit(argv[0]);
273 if (strcmp(argv[1], "discard") == 0)
274 discard = GL_TRUE;
275 else if (strcmp(argv[1], "buffer") == 0)
276 buffer = GL_TRUE;
277 else if (strcmp(argv[1], "prims_written") == 0)
278 prims_written = GL_TRUE;
279 else if (strcmp(argv[1], "prims_generated") == 0)
280 prims_generated = GL_TRUE;
281 else
282 print_usage_and_exit(argv[0]);
284 piglit_require_transform_feedback();
286 if (buffer || prims_written) {
287 GLuint vs;
288 piglit_require_vertex_shader();
289 vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vstext);
290 xfb_prog = glCreateProgram();
291 glAttachShader(xfb_prog, vs);
292 glTransformFeedbackVaryings(xfb_prog, 1, xfb_varyings,
293 GL_INTERLEAVED_ATTRIBS);
294 glLinkProgram(xfb_prog);
295 if (!piglit_link_check_status(xfb_prog)) {
296 piglit_report_result(PIGLIT_FAIL);
298 glGenBuffers(1, &xfb_buf);
300 if (prims_written) {
301 glGenQueries(1, &prims_written_query);
303 if (prims_generated) {
304 glGenQueries(1, &prims_generated_query);