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
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
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
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
;
49 print_usage_and_exit(char *prog_name
)
51 printf("Usage: %s <num_samples>\n", prog_name
);
52 piglit_report_result(PIGLIT_FAIL
);
58 static const char *vert
=
60 "in vec4 piglit_vertex;\n"
63 " gl_Position = piglit_vertex;\n"
65 static const char *frag_0
=
67 "#extension GL_ARB_sample_shading : enable\n"
68 "out vec4 out_color;\n"
71 " gl_SampleMask[0] = int(gl_FragCoord.x);\n"
72 " out_color = vec4(1.0, 0.0, 0.0, 0.0);\n"
75 static const char *frag_template
=
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"
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"
98 prog_0
= piglit_build_simple_program(vert
, frag_0
);
99 if (!piglit_link_check_status(prog_0
)) {
100 piglit_report_result(PIGLIT_FAIL
);
105 (void)!asprintf(&frag_1
, frag_template
,
106 "#extension GL_ARB_texture_multisample : require",
107 "sampler2DMS", ", i");
109 (void)!asprintf(&frag_1
, frag_template
, "", "sampler2DRect", "");
111 prog_1
= piglit_build_simple_program(vert
, frag_1
);
113 if (!piglit_link_check_status(prog_1
)) {
114 piglit_report_result(PIGLIT_FAIL
);
119 piglit_init(int argc
, char **argv
)
122 print_usage_and_exit(argv
[0]);
124 /* 1st arg: num_samples */
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 */
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
);
151 if (!piglit_check_gl_error(GL_NO_ERROR
)) {
152 piglit_report_result(PIGLIT_FAIL
);
157 equal(float a
, float b
)
159 return fabs(a
- b
) < piglit_tolerance
[0];
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
);
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
));
194 assert(0 && "Unimplemented");
196 for (i
= 0; i
< multisampled_tex
.config
.width
; i
++) {
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,
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
);
225 piglit_present_results();
226 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;