framework/replay: disable AA accounting when comparing with no tolerance
[piglit.git] / tests / spec / arb_sample_shading / execution / builtin-gl-sample-mask.cpp
blobf80e6d59a45d1a83347c3f41e324294969cd1ee7
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 /** \file builtin-gl-sample-mask.cpp
25 * This test verifies that supplying a value to gl_SampleMask[]
26 * in fragment shader program works as per ARB_sample_shading
27 * specification.
28 **/
30 #include "piglit-fbo.h"
31 using namespace piglit_util_fbo;
33 const int pattern_width = 128; const int pattern_height = 128;
35 PIGLIT_GL_TEST_CONFIG_BEGIN
37 config.supports_gl_compat_version = 21;
38 config.supports_gl_core_version = 31;
40 config.window_width = pattern_width;
41 config.window_height = pattern_height;
42 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
43 config.khr_no_error_support = PIGLIT_NO_ERRORS;
45 PIGLIT_GL_TEST_CONFIG_END
47 static int num_samples;
48 static unsigned prog_0, prog_1;
49 static Fbo multisampled_fbo, multisampled_tex;
51 static void
52 print_usage_and_exit(char *prog_name)
54 printf("Usage: %s <num_samples>\n", prog_name);
55 piglit_report_result(PIGLIT_FAIL);
58 void
59 compile_shader(void)
61 static const char *vert =
62 "#version 130\n"
63 "in vec4 piglit_vertex;\n"
64 "void main()\n"
65 "{\n"
66 " gl_Position = piglit_vertex;\n"
67 "}\n";
68 static const char *frag_0 =
69 "#version 130\n"
70 "#extension GL_ARB_sample_shading : enable\n"
71 "out vec4 out_color;\n"
72 "void main()\n"
73 "{\n"
74 /* For 128x128 image size, below formula produces a bit
75 * pattern where no two bits of gl_SampleMask[0] are
76 * correlated.
78 " gl_SampleMask[0] = (int(gl_FragCoord.x) * 0x10204081) ^\n"
79 " (int(gl_FragCoord.y) * 0x01010101);\n"
80 " out_color = vec4(0.0, 1.0, 0.0, 1.0);\n"
81 "}\n";
83 static const char *frag_template =
84 "#version 130\n"
85 "%s\n"
86 "uniform %s tex;\n"
87 "uniform int samples;\n"
88 "out vec4 out_color;\n"
89 "void main()\n"
90 "{\n"
91 " int i = 0;\n"
92 " bool pass = true;\n"
93 " int mask = (int(gl_FragCoord.x) * 0x10204081) ^\n"
94 " (int(gl_FragCoord.y) * 0x01010101);\n"
95 " vec4 green = vec4(0.0, 1.0, 0.0, 1.0);\n"
96 " vec4 black = vec4(0.0, 0.0, 0.0, 0.0);\n"
97 " if (samples == 0) mask = 1;\n"
98 /* Use do-while to include 'samples = 0' case. */
99 " do {\n"
100 " bool is_sample_mask_set = ((mask >> i) & 0x1) == 0x1;\n"
101 " vec4 sample_color =\n"
102 " texelFetch(tex, ivec2(gl_FragCoord.xy)%s);\n"
103 "\n"
104 " if ((is_sample_mask_set && sample_color != green) ||\n"
105 " (!is_sample_mask_set && sample_color != black)) {\n"
106 " pass = false;\n"
107 " break;\n"
108 " }\n"
109 " i++;\n"
110 " } while (i < samples);\n"
111 "\n"
112 " if (pass)\n"
113 " out_color = vec4(0.0, 1.0, 0.0, 1.0);\n"
114 " else\n"
115 " out_color = vec4(1.0, 0.0, 0.0, 1.0);\n"
116 "}\n";
118 /* Compile program */
119 prog_0 = piglit_build_simple_program(vert, frag_0);
120 if (!piglit_link_check_status(prog_0)) {
121 piglit_report_result(PIGLIT_FAIL);
124 char *frag_1;
125 if (num_samples)
126 (void)!asprintf(&frag_1, frag_template,
127 "#extension GL_ARB_texture_multisample : require",
128 "sampler2DMS", ", i");
129 else
130 (void)!asprintf(&frag_1, frag_template, "", "sampler2DRect", "");
132 prog_1 = piglit_build_simple_program(vert, frag_1);
133 free(frag_1);
134 if (!piglit_link_check_status(prog_1)) {
135 piglit_report_result(PIGLIT_FAIL);
139 void
140 piglit_init(int argc, char **argv)
142 if (argc != 2)
143 print_usage_and_exit(argv[0]);
145 /* 1st arg: num_samples */
146 char *endptr = NULL;
147 num_samples = strtol(argv[1], &endptr, 0);
148 if (endptr != argv[1] + strlen(argv[1]))
149 print_usage_and_exit(argv[0]);
151 piglit_require_extension("GL_ARB_texture_multisample");
152 piglit_require_extension("GL_ARB_sample_shading");
153 piglit_require_GLSL_version(130);
155 /* Skip the test if num_samples > GL_MAX_SAMPLES */
156 GLint max_samples;
157 glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
158 if (num_samples > max_samples)
159 piglit_report_result(PIGLIT_SKIP);
161 FboConfig msConfig(num_samples, pattern_width, pattern_height);
162 multisampled_fbo.setup(msConfig);
163 msConfig.num_tex_attachments = 1;
164 msConfig.num_rb_attachments = 0;
165 multisampled_tex.setup(msConfig);
167 compile_shader();
168 if (!piglit_check_gl_error(GL_NO_ERROR)) {
169 piglit_report_result(PIGLIT_FAIL);
173 bool
174 test_builtin_sample_mask(const Fbo& ms_fbo)
176 int samples;
177 bool result = true;
178 float expected[4] = {0.0, 1.0, 0.0, 1.0};
180 glUseProgram(prog_0);
181 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, ms_fbo.handle);
182 glGetIntegerv(GL_SAMPLES, &samples);
183 glClear(GL_COLOR_BUFFER_BIT);
184 glUniform1i(glGetUniformLocation(prog_0, "samples"), samples);
185 piglit_draw_rect(-1, -1, 2, 2);
187 if(ms_fbo.config.num_tex_attachments == 0) {
188 /* Blit the framebuffer with multisample renderbuffer attachment
189 * into the framebuffer with multisample texture attachment.
191 glBindFramebuffer(GL_READ_FRAMEBUFFER, ms_fbo.handle);
192 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, multisampled_tex.handle);
193 glClear(GL_COLOR_BUFFER_BIT);
194 glBlitFramebuffer(0, 0,
195 ms_fbo.config.width,
196 ms_fbo.config.height,
197 0, 0,
198 ms_fbo.config.width,
199 ms_fbo.config.height,
200 GL_COLOR_BUFFER_BIT, GL_NEAREST);
203 glBindFramebuffer(GL_READ_FRAMEBUFFER, multisampled_tex.handle);
204 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
205 glClear(GL_COLOR_BUFFER_BIT);
207 glUseProgram(prog_1);
208 glUniform1i(glGetUniformLocation(prog_1, "tex"), 0);
209 glUniform1i(glGetUniformLocation(prog_1, "samples"), samples);
210 piglit_draw_rect(-1, -1, 2, 2);
212 glBindFramebuffer(GL_READ_FRAMEBUFFER, piglit_winsys_fbo);
213 result = piglit_probe_rect_rgba(0, 0, pattern_width,
214 pattern_width, expected)
215 && result;
216 piglit_present_results();
217 printf("FBO attachment = %s, result = %s\n",
218 ms_fbo.config.num_tex_attachments > 0 ?
219 "TEXTURE" :
220 "RENDERBUFFER",
221 result ? "pass" : "fail");
222 return result;
225 enum piglit_result
226 piglit_display()
228 bool pass = true;
229 pass = test_builtin_sample_mask(multisampled_tex) && pass;
230 pass = test_builtin_sample_mask(multisampled_fbo) && pass;
231 return pass ? PIGLIT_PASS : PIGLIT_FAIL;