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
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
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
34 #include "piglit-fbo.h"
35 using namespace piglit_util_fbo
;
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
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
;
61 if (!piglit_check_gl_error(GL_NO_ERROR
))
62 piglit_report_result(PIGLIT_FAIL
);
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
78 const GLuint prog
= piglit_build_simple_program(
80 "in vec4 piglit_vertex;\n"
84 " gl_Position = piglit_vertex;\n"
87 "#extension GL_ARB_sample_shading : enable\n"
89 "out vec4 out_color[2];"
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"
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
);
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
123 check(const Fbo
&fbo
, unsigned i
)
125 const float expected
[] = { 1, 0, 1, 0 };
126 const GLuint prog
= piglit_build_simple_program(
128 "in vec4 piglit_vertex;\n"
132 " gl_Position = piglit_vertex;\n"
135 "#extension GL_ARB_texture_multisample : require\n"
137 "uniform sampler2DMS tex;\n"
138 "out vec4 out_color;\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"
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
))
166 if (!piglit_probe_rect_rgba(0, 0, piglit_width
, piglit_height
,
168 printf(" Attachment: %d\n", i
);
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
);
187 piglit_report_result(PIGLIT_FAIL
);