framework/replay: disable AA accounting when comparing with no tolerance
[piglit.git] / tests / spec / arb_sample_shading / execution / builtin-gl-sample-mask-mrt-alpha.cpp
blob47ad180852c1577c40e3d04cb64be974266054b1
1 /*
2 * Copyright (c) 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 DEALINGS
21 * IN THE SOFTWARE.
24 /** \file builtin-gl-sample-mask-mrt-alpha.cpp
26 * This test verifies that assigning gl_SampleMask[] from the
27 * fragment shader works as expected in cases where the
28 * implementation is required to supply an additional alpha component
29 * previously written to a different color attachment to render to a
30 * non-zero attachment of a multisample FBO (e.g. while using
31 * alpha-to-coverage).
34 #include "piglit-fbo.h"
35 using namespace piglit_util_fbo;
37 #define NUM_SAMPLES 4
39 PIGLIT_GL_TEST_CONFIG_BEGIN
41 config.supports_gl_compat_version = 21;
42 config.supports_gl_core_version = 31;
44 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
45 config.khr_no_error_support = PIGLIT_NO_ERRORS;
47 PIGLIT_GL_TEST_CONFIG_END
49 static Fbo
50 make_fbo()
52 FboConfig config(NUM_SAMPLES, piglit_width, piglit_height);
53 config.num_rb_attachments = 0;
54 config.num_tex_attachments = 2;
55 config.tex_attachment[0] = GL_COLOR_ATTACHMENT0;
56 config.tex_attachment[1] = GL_COLOR_ATTACHMENT1;
58 Fbo fbo;
59 fbo.setup(config);
61 if (!piglit_check_gl_error(GL_NO_ERROR))
62 piglit_report_result(PIGLIT_FAIL);
64 return fbo;
67 /**
68 * Render to both texture attachments of the multisample fbo enabling
69 * alpha-to-coverage to make the implementation pass the additional
70 * alpha component from the first attachment when rendering into the
71 * second. The resulting sample mask will still be 5 as specified in
72 * the fragment shader because an alpha value of 1.0 maps to the
73 * coverage mask ~0.
75 static bool
76 run_test(Fbo &fbo)
78 const GLuint prog = piglit_build_simple_program(
79 "#version 130\n"
80 "in vec4 piglit_vertex;\n"
81 "\n"
82 "void main()\n"
83 "{\n"
84 " gl_Position = piglit_vertex;\n"
85 "}\n",
86 "#version 130\n"
87 "#extension GL_ARB_sample_shading : enable\n"
88 "\n"
89 "out vec4 out_color[2];"
90 "\n"
91 "void main()\n"
92 "{\n"
93 " gl_SampleMask[0] = 5;\n"
94 " out_color[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
95 " out_color[1] = vec4(1.0, 0.0, 0.0, 0.0);\n"
96 "}\n");
98 glUseProgram(prog);
100 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo.handle);
101 glDrawBuffers(fbo.config.num_tex_attachments,
102 fbo.config.tex_attachment);
103 glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
104 fbo.set_viewport();
106 glClear(GL_COLOR_BUFFER_BIT);
107 piglit_draw_rect(-1, -1, 2, 2);
109 glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
110 glDeleteProgram(prog);
112 return piglit_check_gl_error(GL_NO_ERROR);
116 * Resolve the red component of each sample from the texture
117 * previously bound to color attachment i as the RGBA components of
118 * the actual framebuffer. Return true if only the first and third
119 * samples were written according to the coverage mask set by the
120 * shader.
122 static bool
123 check(const Fbo &fbo, unsigned i)
125 const float expected[] = { 1, 0, 1, 0 };
126 const GLuint prog = piglit_build_simple_program(
127 "#version 130\n"
128 "in vec4 piglit_vertex;\n"
129 "\n"
130 "void main()\n"
131 "{\n"
132 " gl_Position = piglit_vertex;\n"
133 "}\n",
134 "#version 130\n"
135 "#extension GL_ARB_texture_multisample : require\n"
136 "\n"
137 "uniform sampler2DMS tex;\n"
138 "out vec4 out_color;\n"
139 "\n"
140 "void main()\n"
141 "{\n"
142 " vec4 v;\n"
143 "\n"
144 " for (int i = 0; i < 4; i++)\n"
145 " v[i] = texelFetch(tex, ivec2(gl_FragCoord.x,\n"
146 " gl_FragCoord.y), i).x;\n"
147 "\n"
148 " out_color = v;\n"
149 "}\n");
151 glUseProgram(prog);
152 glUniform1i(glGetUniformLocation(prog, "tex"), 0);
154 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
155 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, fbo.color_tex[i]);
156 glViewport(0, 0, piglit_width, piglit_height);
158 glClear(GL_COLOR_BUFFER_BIT);
159 piglit_draw_rect(-1, -1, 2, 2);
161 glDeleteProgram(prog);
163 if (!piglit_check_gl_error(GL_NO_ERROR))
164 return false;
166 if (!piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height,
167 expected)) {
168 printf(" Attachment: %d\n", i);
169 return false;
172 return true;
175 void
176 piglit_init(int argc, char **argv)
178 piglit_require_extension("GL_ARB_texture_multisample");
179 piglit_require_extension("GL_ARB_sample_shading");
180 piglit_require_GLSL_version(130);
182 Fbo fbo = make_fbo();
184 if (run_test(fbo) && check(fbo, 0) && check(fbo, 1))
185 piglit_report_result(PIGLIT_PASS);
186 else
187 piglit_report_result(PIGLIT_FAIL);
190 piglit_result
191 piglit_display()
193 return PIGLIT_FAIL;