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
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.cpp
25 * This test verifies that supplying a value to gl_SampleMask[]
26 * in fragment shader program works as per ARB_sample_shading
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
;
52 print_usage_and_exit(char *prog_name
)
54 printf("Usage: %s <num_samples>\n", prog_name
);
55 piglit_report_result(PIGLIT_FAIL
);
61 static const char *vert
=
63 "in vec4 piglit_vertex;\n"
66 " gl_Position = piglit_vertex;\n"
68 static const char *frag_0
=
70 "#extension GL_ARB_sample_shading : enable\n"
71 "out vec4 out_color;\n"
74 /* For 128x128 image size, below formula produces a bit
75 * pattern where no two bits of gl_SampleMask[0] are
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"
83 static const char *frag_template
=
87 "uniform int samples;\n"
88 "out vec4 out_color;\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. */
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"
104 " if ((is_sample_mask_set && sample_color != green) ||\n"
105 " (!is_sample_mask_set && sample_color != black)) {\n"
110 " } while (i < samples);\n"
113 " out_color = vec4(0.0, 1.0, 0.0, 1.0);\n"
115 " out_color = vec4(1.0, 0.0, 0.0, 1.0);\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
);
126 (void)!asprintf(&frag_1
, frag_template
,
127 "#extension GL_ARB_texture_multisample : require",
128 "sampler2DMS", ", i");
130 (void)!asprintf(&frag_1
, frag_template
, "", "sampler2DRect", "");
132 prog_1
= piglit_build_simple_program(vert
, frag_1
);
134 if (!piglit_link_check_status(prog_1
)) {
135 piglit_report_result(PIGLIT_FAIL
);
140 piglit_init(int argc
, char **argv
)
143 print_usage_and_exit(argv
[0]);
145 /* 1st arg: num_samples */
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 */
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
);
168 if (!piglit_check_gl_error(GL_NO_ERROR
)) {
169 piglit_report_result(PIGLIT_FAIL
);
174 test_builtin_sample_mask(const Fbo
& ms_fbo
)
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,
196 ms_fbo
.config
.height
,
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
)
216 piglit_present_results();
217 printf("FBO attachment = %s, result = %s\n",
218 ms_fbo
.config
.num_tex_attachments
> 0 ?
221 result
? "pass" : "fail");
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
;