ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / ext_framebuffer_multisample / sample-coverage.cpp
blob8cda26d7e413c9741bb2dec29998be4e4d6188fb
1 /*
2 * Copyright © 2012 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 #include "piglit-test-pattern.h"
25 #include "piglit-fbo.h"
26 using namespace piglit_util_fbo;
27 using namespace piglit_util_test_pattern;
29 /**
30 * \file sample-coverage.cpp
32 * Verify glSampleCoverage() with and without coverage mask invert.
34 * This test operates by drawing a test pattern to multisample_fbo with
35 * GL_SAMPLE_COVERAGE disabled.
37 * Blit the multisample_fbo to top half of window system framebuffer. This
38 * is used as reference image to visually compare the difference caused by
39 * sample coverage value.
41 * Compute the expected color values based on the coverage value used to
42 * draw the test pattern and status of coverage mask invert flag.
44 * Clear the multisample framebuffer to a unique color. Draw the
45 * same test pattern in multisample buffer with GL_SAMPLE_COVERAGE enabled.
46 * Resolve the multisample FBO by blitting it to a single sample FBO. Blit
47 * the resolve_fbo to bottom half of window system framebuffer. This is our
48 * test image.
50 * Probe the rectangles in bottom half of window system framebuffer and
51 * compare with expected color values. OpenGL 3.0 specification intends
52 * to allow (but not require) the implementation to produce a dithering
53 * effect when the coverage value is not a strict multiple of 1/num_samples.
54 * We will skip computing expected values and probing for such rectangles.
55 * They are drawn just to look for dithering by human inspection.
57 * This test can be executed in inverted / non-inverted modes using command
58 * line options.
60 * Note: glSampleCoverage() takes effect in the graphics pipeline before
61 * the point where the output of the fragment shader is split into the
62 * various buffers. So it's very likely that if glSampleCoverage() works
63 * properly for color buffers, it will work properly for depth and stencil
64 * buffers too.
66 * Author: Anuj Phogat <anuj.phogat@gmail.com>
69 PIGLIT_GL_TEST_CONFIG_BEGIN
71 config.supports_gl_compat_version = 10;
73 config.window_width = 512;
74 config.window_height = 256;
75 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
76 config.khr_no_error_support = PIGLIT_NO_ERRORS;
78 PIGLIT_GL_TEST_CONFIG_END
80 const int pattern_width = 512; const int pattern_height = 128;
81 static Fbo ms_fbo, resolve_fbo;
82 static GLbitfield buffer_to_test;
84 static bool coverage_invert = false;
85 static float *cov = NULL;
86 static float *color = NULL;
87 static float *expected = NULL;
89 static int num_samples;
90 static int num_rects;
91 static int prog;
92 static int color_loc;
93 static int depth_loc;
95 static const float bg_color[4] = { 0.4, 0.6, 0.0, 0.8 };
97 static const char *vert =
98 "#version 120\n"
99 "attribute vec2 pos;\n"
100 "uniform float depth;\n"
101 "void main()\n"
102 "{\n"
103 " vec4 eye_pos = gl_ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);\n"
104 " gl_Position = vec4(eye_pos.xy, depth, 1.0);\n"
105 "}\n";
107 static const char *frag =
108 "#version 120\n"
109 "uniform vec4 color;\n"
110 "void main()\n"
111 "{\n"
112 " gl_FragColor = color;\n"
113 "}\n";
115 void
116 shader_compile()
118 /* Compile program */
119 GLint vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vert);
120 GLint fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, frag);
121 prog = piglit_link_simple_program(vs, fs);
123 if (!piglit_link_check_status(prog)) {
124 piglit_report_result(PIGLIT_FAIL);
127 glBindAttribLocation(prog, 0, "pos");
128 glEnableVertexAttribArray(0);
130 /* Set up uniforms */
131 glUseProgram(prog);
132 color_loc = glGetUniformLocation(prog, "color");
133 depth_loc = glGetUniformLocation(prog, "depth");
136 void
137 draw_pattern(bool sample_coverage)
139 glUseProgram(prog);
140 glClearColor(bg_color[0], bg_color[1],
141 bg_color[2], bg_color[3]);
143 glClear(buffer_to_test);
144 if (sample_coverage)
145 glEnable (GL_SAMPLE_COVERAGE);
147 unsigned int indices[6] = {0, 1, 2, 0, 2, 3};
149 for (int i = 0; i < num_rects; ++i) {
151 float vertex_data[4][2] = {
152 { 0.0f + i * (pattern_width / num_rects), 0.0f },
153 { 0.0f + i * (pattern_width / num_rects), pattern_height },
154 { (i + 1.0f) * (pattern_width / num_rects), pattern_height },
155 { (i + 1.0f) * (pattern_width / num_rects), 0.0f } };
157 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,
158 sizeof(vertex_data[0]),
159 (void *) vertex_data);
161 if(sample_coverage) {
162 if(coverage_invert)
163 glSampleCoverage (cov[i], GL_TRUE);
164 else
165 glSampleCoverage (cov[i], GL_FALSE);
168 glUniform4fv(color_loc, 1, (color + i * 4));
169 glUniform1f(depth_loc, 0.0f);
170 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT,
171 (void *) indices);
173 if(sample_coverage)
174 glDisable (GL_SAMPLE_COVERAGE);
177 void
178 print_usage_and_exit(char *prog_name)
180 printf("Usage: %s <num_samples> <mode> \n"
181 " where <mode> is one of:\n"
182 " inverted\n"
183 " non-inverted\n",
184 prog_name);
186 piglit_report_result(PIGLIT_FAIL);
189 void
190 compute_expected(void)
192 int i, j;
194 /* Sample coverage doesn't affect single sample FBO */
195 if(num_samples == 0) {
196 for(i = 0; i < 4; i++)
197 expected[i] = color[i];
198 return;
201 float *coverage = new float[num_rects];
203 if(coverage_invert) {
204 for (i = 0; i < num_rects; i++)
205 coverage[i] = 1 - cov[i];
207 else
208 for (i = 0; i < num_rects; i++)
209 coverage[i] = cov[i];
211 /* Coverage value decides the number of samples in multisample buffer
212 * covered by an incoming fragment, which will then receive the fragment
213 * data. When the multisample buffer is resolved it will be blended
214 * with the background color which will be written to the remaining
215 * samples.
216 * Page 254 (page 270 of the PDF) of the OpenGL 3.0 spec says:
217 * "The method of combination is not specified, though a simple average
218 * computed independently for each color component is recommended."
220 if(buffer_to_test == GL_COLOR_BUFFER_BIT) {
221 for (i = 0; i < num_rects; i++) {
223 float samples_used = coverage[i] * num_samples;
224 /* Exepected color values are computed only for integer
225 * number of samples_used
227 if(samples_used == (int)samples_used) {
229 for(j =0; j < 4; j++)
230 expected[i * 4 + j] =
231 color[i * 4 + j] * coverage[i] +
232 bg_color[j] * (1 - coverage[i]);
237 delete [] coverage;
240 bool
241 probe_framebuffer_color(void)
243 bool result = true;
245 float *coverage = new float[num_rects];
247 if(coverage_invert) {
248 for (int i = 0; i < num_rects; i++)
249 coverage[i] = 1 - cov[i];
251 else
252 for (int i = 0; i < num_rects; i++)
253 coverage[i] = cov[i];
255 glBindFramebuffer(GL_READ_FRAMEBUFFER, piglit_winsys_fbo);
257 for (int i = 0; i < num_rects; i++) {
258 float samples_used = coverage[i] * num_samples;
260 /* Only probe rectangles with coverage value which is a strict
261 * multiple of 1 / num_samples.
263 if(samples_used == (int)samples_used) {
264 result = piglit_probe_rect_rgba(
265 i * (pattern_width / num_rects),
267 pattern_width / num_rects,
268 pattern_height,
269 expected + i * 4)
270 && result;
274 delete [] coverage;
276 return result;
279 bool
280 test_sample_coverage(void)
282 bool result = true;
283 compute_expected();
285 /* Now draw test pattern in multisample ms_fbo with GL_SAMPLE_COVERAGE
286 * enabled
288 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, ms_fbo.handle);
289 draw_pattern(true /* sample_coverage */);
291 /* Blit ms_fbo to resolve_fbo to resolve multisample buffer */
292 glBindFramebuffer(GL_READ_FRAMEBUFFER, ms_fbo.handle);
293 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resolve_fbo.handle);
294 glBlitFramebuffer(0, 0, pattern_width, pattern_height,
295 0, 0, pattern_width, pattern_height,
296 buffer_to_test, GL_NEAREST);
298 /* Blit resolve_fbo to the bottom half of window system framebuffer.
299 * This is the test image.
301 glBindFramebuffer(GL_READ_FRAMEBUFFER, resolve_fbo.handle);
302 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
303 glBlitFramebuffer(0, 0, pattern_width, pattern_height,
304 0, 0, pattern_width, pattern_height,
305 buffer_to_test, GL_NEAREST);
307 /* Probe the bottom half of default framebuffer and compare to the
308 * expected values */
309 if (buffer_to_test == GL_COLOR_BUFFER_BIT)
310 result = probe_framebuffer_color() && result;
312 result = piglit_check_gl_error(GL_NO_ERROR) && result;
313 return result;
316 void
317 allocate_data_arrays(void)
319 /* Draw 2N + 1 rectangles for N samples, each with a unique color
320 * and coverage value
322 num_rects = 2 * num_samples + 1;
324 /* Allocate data arrays based on number of samples used */
325 color = (float *) malloc(num_rects * 4 * sizeof(float));
326 cov = (float *) malloc(num_rects * sizeof(float));
327 expected = (float *) malloc(num_rects * 4 * sizeof(float));
329 for(int i = 0; i < num_rects; i++) {
330 color[i * 4 + 0] = (sin((float)(i * 4 + 0)) + 1) / 2;
331 color[i * 4 + 1] = (sin((float)(i * 4 + 1)) + 1) / 2;
332 color[i * 4 + 2] = (sin((float)(i * 4 + 2)) + 1) / 2;
333 color[i * 4 + 3] = (sin((float)(i * 4 + 3)) + 1) / 2;
335 cov[i] = i * (1.0 / (2 * num_samples));
339 void
340 free_data_arrays(void)
342 if(color != NULL) {
343 free(color);
344 color = NULL;
346 if(cov != NULL) {
347 free(cov);
348 cov = NULL;
350 if(expected != NULL) {
351 free(expected);
352 expected = NULL;
356 void
357 piglit_init(int argc, char **argv)
359 int samples;
360 if (argc < 3)
361 print_usage_and_exit(argv[0]);
363 char *endptr = NULL;
364 samples = strtol(argv[1], &endptr, 0);
365 if (endptr != argv[1] + strlen(argv[1]))
366 print_usage_and_exit(argv[0]);
369 for (int i = 2; i < argc; ++i) {
370 if (strcmp(argv[i], "inverted") == 0)
371 coverage_invert = true;
372 else if (strcmp(argv[i], "non-inverted") == 0)
373 coverage_invert = false;
376 piglit_require_gl_version(21);
377 piglit_require_extension("GL_ARB_framebuffer_object");
378 piglit_require_extension("GL_ARB_vertex_array_object");
380 piglit_ortho_projection(pattern_width, pattern_height, GL_TRUE);
382 /* Skip the test if samples > GL_MAX_SAMPLES */
383 GLint max_samples;
384 glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
386 if (samples > max_samples)
387 piglit_report_result(PIGLIT_SKIP);
389 /* Setup frame buffer objects with required configuration */
390 ms_fbo.setup(FboConfig(samples, pattern_width, pattern_height));
391 resolve_fbo.setup(FboConfig(0, pattern_width, pattern_height));
393 if (!piglit_check_gl_error(GL_NO_ERROR)) {
394 printf("Error setting up frame buffer objects\n");
395 piglit_report_result(PIGLIT_FAIL);
398 /* Query the number of samples used in ms_fbo. OpenGL implementation
399 * may create FBO with more samples per pixel than what is requested.
401 glBindRenderbuffer(GL_RENDERBUFFER, ms_fbo.color_rb[0]);
402 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
403 GL_RENDERBUFFER_SAMPLES,
404 &num_samples);
406 buffer_to_test = GL_COLOR_BUFFER_BIT;
407 shader_compile();
410 enum piglit_result
411 piglit_display()
413 bool pass = true;
414 allocate_data_arrays();
416 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
417 glClearColor(0.0, 0.0, 0.0, 1.0);
418 glClear(buffer_to_test);
420 /* Draw test pattern in multisample ms_fbo with GL_SAMPLE_COVERAGE
421 * disabled.
423 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, ms_fbo.handle);
424 ms_fbo.set_viewport();
425 draw_pattern(false /* sample_coverage */);
427 /* Blit ms_fbo to the top half of window system framebuffer. This
428 * is our reference image to visually compare the effect of MSAA with
429 * sample coverage.
431 glBindFramebuffer(GL_READ_FRAMEBUFFER, ms_fbo.handle);
432 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
433 glBlitFramebuffer(0, 0,
434 pattern_width, pattern_height,
435 0, pattern_height,
436 pattern_width, 2 * pattern_height,
437 buffer_to_test, GL_NEAREST);
439 pass = test_sample_coverage() && pass;
441 /* Free the memory allocated for data arrays */
442 free_data_arrays();
444 if (!piglit_automatic &&
445 buffer_to_test != GL_DEPTH_BUFFER_BIT)
446 piglit_present_results();
448 return pass ? PIGLIT_PASS : PIGLIT_FAIL;