framework/replay: disable AA accounting when comparing with no tolerance
[piglit.git] / tests / spec / arb_sample_shading / execution / builtin-gl-sample-mask-simple.cpp
blobc91d8b94a0b2a12107859868c31ca9511eccc587
1 /*
2 * Copyright © 2013 Intel Corporation
3 * Copyright © 2014 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
25 /** \file builtin-gl-sample-mask-simple.cpp
26 * This test verifies that supplying a value to gl_SampleMask[]
27 * in fragment shader program works as per ARB_sample_shading
28 * specification.
29 **/
31 #include "piglit-fbo.h"
32 using namespace piglit_util_fbo;
34 PIGLIT_GL_TEST_CONFIG_BEGIN
36 config.supports_gl_compat_version = 21;
37 config.supports_gl_core_version = 31;
39 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
40 config.khr_no_error_support = PIGLIT_NO_ERRORS;
42 PIGLIT_GL_TEST_CONFIG_END
44 static int num_samples;
45 static unsigned prog_0, prog_1;
46 static Fbo multisampled_tex;
48 static void
49 print_usage_and_exit(char *prog_name)
51 printf("Usage: %s <num_samples>\n", prog_name);
52 piglit_report_result(PIGLIT_FAIL);
55 static void
56 compile_shader(void)
58 static const char *vert =
59 "#version 130\n"
60 "in vec4 piglit_vertex;\n"
61 "void main()\n"
62 "{\n"
63 " gl_Position = piglit_vertex;\n"
64 "}\n";
65 static const char *frag_0 =
66 "#version 130\n"
67 "#extension GL_ARB_sample_shading : enable\n"
68 "out vec4 out_color;\n"
69 "void main()\n"
70 "{\n"
71 " gl_SampleMask[0] = int(gl_FragCoord.x);\n"
72 " out_color = vec4(1.0, 0.0, 0.0, 0.0);\n"
73 "}\n";
75 static const char *frag_template =
76 "#version 130 \n"
77 "%s \n"
78 "uniform %s tex; \n"
79 "#define fetch(i) (texelFetch(tex, ivec2(int(gl_FragCoord.y/8) * 16 + int(gl_FragCoord.x/8) % 16, 0)%s)) \n"
80 "uniform int samples; \n"
81 "out vec4 out_color; \n"
82 "void main() \n"
83 "{ \n"
84 " vec4 outv = vec4(0.0, 0.0, 0.0, 0.0); \n"
85 /* encode the sample mask to RGBA */
86 " outv.x += fetch(0).x * 0.6; \n"
87 " if (1 < samples) outv.y += fetch(1).x * 0.6; \n"
88 " if (2 < samples) outv.z += fetch(2).x * 0.6; \n"
89 " if (3 < samples) outv.w += fetch(3).x * 0.6; \n"
90 " if (4 < samples) outv.x += fetch(4).x * 0.4; \n"
91 " if (5 < samples) outv.y += fetch(5).x * 0.4; \n"
92 " if (6 < samples) outv.z += fetch(6).x * 0.4; \n"
93 " if (7 < samples) outv.w += fetch(7).x * 0.4; \n"
94 " out_color = outv;\n"
95 "} \n";
97 /* Compile program */
98 prog_0 = piglit_build_simple_program(vert, frag_0);
99 if (!piglit_link_check_status(prog_0)) {
100 piglit_report_result(PIGLIT_FAIL);
103 char *frag_1;
104 if (num_samples)
105 (void)!asprintf(&frag_1, frag_template,
106 "#extension GL_ARB_texture_multisample : require",
107 "sampler2DMS", ", i");
108 else
109 (void)!asprintf(&frag_1, frag_template, "", "sampler2DRect", "");
111 prog_1 = piglit_build_simple_program(vert, frag_1);
112 free(frag_1);
113 if (!piglit_link_check_status(prog_1)) {
114 piglit_report_result(PIGLIT_FAIL);
118 void
119 piglit_init(int argc, char **argv)
121 if (argc != 2)
122 print_usage_and_exit(argv[0]);
124 /* 1st arg: num_samples */
125 char *endptr = NULL;
126 num_samples = strtol(argv[1], &endptr, 0);
127 if (endptr != argv[1] + strlen(argv[1]))
128 print_usage_and_exit(argv[0]);
130 if (num_samples > 8) {
131 puts("This test only supports 8 samples.");
132 piglit_report_result(PIGLIT_SKIP);
135 piglit_require_extension("GL_ARB_texture_multisample");
136 piglit_require_extension("GL_ARB_sample_shading");
137 piglit_require_GLSL_version(130);
139 /* Skip the test if num_samples > GL_MAX_SAMPLES */
140 GLint max_samples;
141 glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
142 if (num_samples > max_samples)
143 piglit_report_result(PIGLIT_SKIP);
145 FboConfig msConfig(num_samples, 1 << MAX2(num_samples, 1), 1);
146 msConfig.num_rb_attachments = 0;
147 msConfig.num_tex_attachments = 1;
148 multisampled_tex.setup(msConfig);
150 compile_shader();
151 if (!piglit_check_gl_error(GL_NO_ERROR)) {
152 piglit_report_result(PIGLIT_FAIL);
156 static bool
157 equal(float a, float b)
159 return fabs(a - b) < piglit_tolerance[0];
162 enum piglit_result
163 piglit_display()
165 bool pass = true;
166 int samples, i, j;
168 glUseProgram(prog_0);
169 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, multisampled_tex.handle);
170 multisampled_tex.set_viewport();
171 glGetIntegerv(GL_SAMPLES, &samples);
172 samples = MAX2(samples, 1);
174 glClear(GL_COLOR_BUFFER_BIT);
175 glUniform1i(glGetUniformLocation(prog_0, "samples"), samples);
176 piglit_draw_rect(-1, -1, 2, 2);
178 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
179 glViewport(0, 0, piglit_width, piglit_height);
180 glClear(GL_COLOR_BUFFER_BIT);
182 glUseProgram(prog_1);
183 glUniform1i(glGetUniformLocation(prog_1, "tex"), 0);
184 glUniform1i(glGetUniformLocation(prog_1, "samples"), samples);
185 if (samples <= 4)
186 piglit_draw_rect(-1, -1,
187 8*(1 << samples) * (2.0/piglit_width),
188 8 * (2.0/piglit_height));
189 else if (samples == 8)
190 piglit_draw_rect(-1, -1,
191 8*16 * (2.0/piglit_width),
192 8*16 * (2.0/piglit_height));
193 else
194 assert(0 && "Unimplemented");
196 for (i = 0; i < multisampled_tex.config.width; i++) {
197 float color[4];
198 unsigned full_mask = (1 << samples) - 1;
199 unsigned expected_mask = i & full_mask;
200 unsigned observed_mask = 0;
202 if (multisampled_tex.config.num_samples == 0)
203 expected_mask = full_mask;
205 glReadPixels((i % 16) * 8 + 4,
206 (i / 16) * 8 + 4,
207 1, 1, GL_RGBA, GL_FLOAT, color);
209 for (j = 0; j < 4; j++) {
210 if (equal(color[j], 1) || equal(color[j], 0.6))
211 observed_mask |= 1 << (j + 0);
212 if (equal(color[j], 1) || equal(color[j], 0.4))
213 observed_mask |= 1 << (j + 4);
216 if (expected_mask != observed_mask) {
217 printf("Test failed, samples = %u\n"
218 " Expected sample mask: 0x%x\n"
219 " Observed sample mask: 0x%x\n",
220 samples, expected_mask, observed_mask);
221 pass = false;
225 piglit_present_results();
226 return pass ? PIGLIT_PASS : PIGLIT_FAIL;