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-id.cpp
25 * This test verifies that using gl_SampleID in fragment shader program
26 * works as per ARB_sample_shading specification.
28 * Note that we do not have to enable GL_SAMPLE_SHADING_ARB.
29 * The GL_ARB_sample_shading spec (and the GLSL 4.0 spec) say:
31 * 9) Is per-sample shading ever triggered by properties of the fragment
34 * RESOLVED: Yes. The variables "gl_SampleID" and "gl_SamplePosition"
35 * can be used to read properties of the current sample, which wouldn't
36 * make much sense if the fragment shader were run at a lower frequency
41 #include "piglit-fbo.h"
42 using namespace piglit_util_fbo
;
44 const int pattern_width
= 128; const int pattern_height
= 128;
46 PIGLIT_GL_TEST_CONFIG_BEGIN
48 config
.supports_gl_compat_version
= 31;
49 config
.supports_gl_core_version
= 31;
51 config
.window_width
= pattern_width
;
52 config
.window_height
= pattern_height
;
53 config
.window_visual
= PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_RGBA
;
54 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
56 PIGLIT_GL_TEST_CONFIG_END
58 static int num_samples
;
59 static unsigned prog_0
, prog_1
;
60 static Fbo multisampled_fbo
, multisampled_tex
;
63 print_usage_and_exit(char *prog_name
)
65 printf("Usage: %s <num_samples>\n", prog_name
);
66 piglit_report_result(PIGLIT_FAIL
);
72 static const char *vert
=
74 "in vec4 piglit_vertex;\n"
77 " gl_Position = piglit_vertex;\n"
79 static const char *frag_0
=
81 "#extension GL_ARB_sample_shading : enable\n"
82 "uniform int samples;\n"
83 "out ivec4 out_color;\n"
86 " out_color = ivec4(0, gl_SampleID, 0, 1);\n"
89 static const char *frag_template
=
92 "uniform %s ms_tex;\n"
93 "uniform int samples;\n"
94 "out vec4 out_color;\n"
98 " bool pass = true;\n"
99 /* Use do-while to include 'samples = 0' case. */
101 " ivec4 sample_color =\n"
102 " texelFetch(ms_tex, ivec2(gl_FragCoord.xy)%s);\n"
103 " if (sample_color.g != i)\n"
106 " } while (i < samples);\n"
109 " out_color = vec4(0.0, 1.0, 0.0, 1.0);\n"
111 " out_color = vec4(1.0, 0.0, 0.0, 1.0);\n"
114 /* Compile program */
115 prog_0
= piglit_build_simple_program(vert
, frag_0
);
116 if (!piglit_link_check_status(prog_0
)) {
117 piglit_report_result(PIGLIT_FAIL
);
123 (void)!asprintf(&frag_1
, frag_template
,
124 "#extension GL_ARB_texture_multisample : require",
125 "isampler2DMS", ", i");
127 (void)!asprintf(&frag_1
, frag_template
, "", "isampler2DRect", "");
129 prog_1
= piglit_build_simple_program(vert
, frag_1
);
131 if (!piglit_link_check_status(prog_1
)) {
132 piglit_report_result(PIGLIT_FAIL
);
137 piglit_init(int argc
, char **argv
)
140 print_usage_and_exit(argv
[0]);
142 /* 1st arg: num_samples */
144 num_samples
= strtol(argv
[1], &endptr
, 0);
145 if (endptr
!= argv
[1] + strlen(argv
[1]))
146 print_usage_and_exit(argv
[0]);
148 piglit_require_extension("GL_ARB_texture_multisample");
149 piglit_require_extension("GL_ARB_sample_shading");
151 /* Skip the test if num_samples > GL_MAX_SAMPLES */
153 glGetIntegerv(GL_MAX_SAMPLES
, &max_samples
);
154 if (num_samples
> max_samples
)
155 piglit_report_result(PIGLIT_SKIP
);
157 FboConfig
msConfig(num_samples
, pattern_width
, pattern_height
);
158 msConfig
.color_format
= GL_RGBA_INTEGER
;
159 msConfig
.color_internalformat
= GL_RGBA8UI
;
160 multisampled_fbo
.setup(msConfig
);
162 msConfig
.num_tex_attachments
= 1;
163 msConfig
.num_rb_attachments
= 0; /* default value is 1 */
164 multisampled_tex
.setup(msConfig
);
167 if (!piglit_check_gl_error(GL_NO_ERROR
)) {
168 piglit_report_result(PIGLIT_FAIL
);
172 bool test_builtin_sample_id(const Fbo
& ms_fbo
)
176 float expected
[4] = {0.0, 1.0, 0.0, 1.0};
178 glUseProgram(prog_0
);
179 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, ms_fbo
.handle
);
180 glGetIntegerv(GL_SAMPLES
, &samples
);
181 glClear(GL_COLOR_BUFFER_BIT
);
182 glUniform1i(glGetUniformLocation(prog_0
, "samples"), samples
);
183 piglit_draw_rect(-1, -1, 2, 2);
185 if(ms_fbo
.config
.num_tex_attachments
== 0) {
186 /* Blit the framebuffer with multisample renderbuffer attachment
187 * into the framebuffer with multisample texture attachment.
189 glBindFramebuffer(GL_READ_FRAMEBUFFER
, ms_fbo
.handle
);
190 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, multisampled_tex
.handle
);
191 glClear(GL_COLOR_BUFFER_BIT
);
192 glBlitFramebuffer(0, 0,
194 ms_fbo
.config
.height
,
197 ms_fbo
.config
.height
,
198 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
201 glBindFramebuffer(GL_READ_FRAMEBUFFER
, multisampled_tex
.handle
);
202 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, piglit_winsys_fbo
);
203 glClear(GL_COLOR_BUFFER_BIT
);
205 glUseProgram(prog_1
);
206 glUniform1i(glGetUniformLocation(prog_1
, "ms_tex"), 0);
207 glUniform1i(glGetUniformLocation(prog_1
, "samples"), samples
);
208 piglit_draw_rect(-1, -1, 2, 2);
210 glBindFramebuffer(GL_READ_FRAMEBUFFER
, piglit_winsys_fbo
);
211 result
= piglit_probe_rect_rgba(0, 0, pattern_width
,
212 pattern_width
, expected
)
214 piglit_present_results();
215 printf("FBO attachment = %s, result = %s\n",
216 ms_fbo
.config
.num_tex_attachments
> 0 ?
219 result
? "pass" : "fail");
227 pass
= test_builtin_sample_id(multisampled_tex
) && pass
;
228 pass
= test_builtin_sample_id(multisampled_fbo
) && pass
;
229 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;