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-num-samples.cpp
25 * This test verifies that using gl_NumSamples in fragment shader
26 * program works as per ARB_sample_shading specification.
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
;
49 static Fbo multisampled_fbo
, singlesampled_fbo
;
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
=
70 "#extension GL_ARB_sample_shading : require\n"
71 "uniform int samples;\n"
72 "out vec4 out_color;\n"
75 " if (gl_NumSamples == samples)\n"
76 " out_color = vec4(0.0, 1.0, 0.0, 1.0);\n"
78 " out_color = vec4(1.0, 0.0, 0.0, 1.0);\n"
81 prog
= piglit_build_simple_program(vert
, frag
);
82 if (!piglit_link_check_status(prog
)) {
83 piglit_report_result(PIGLIT_FAIL
);
88 piglit_init(int argc
, char **argv
)
91 print_usage_and_exit(argv
[0]);
93 /* 1st arg: num_samples */
95 num_samples
= strtol(argv
[1], &endptr
, 0);
96 if (endptr
!= argv
[1] + strlen(argv
[1]))
97 print_usage_and_exit(argv
[0]);
99 piglit_require_extension("GL_ARB_vertex_array_object");
100 piglit_require_extension("GL_ARB_sample_shading");
101 piglit_require_GLSL_version(130);
103 /* Skip the test if num_samples > GL_MAX_SAMPLES */
105 glGetIntegerv(GL_MAX_SAMPLES
, &max_samples
);
106 if (num_samples
> max_samples
)
107 piglit_report_result(PIGLIT_SKIP
);
109 singlesampled_fbo
.setup(FboConfig(0,
113 FboConfig
msConfig(num_samples
, pattern_width
, pattern_height
);
114 multisampled_fbo
.setup(msConfig
);
117 if (!piglit_check_gl_error(GL_NO_ERROR
)) {
118 piglit_report_result(PIGLIT_FAIL
);
122 bool test_builtin_num_samples(const Fbo
& ms_fbo
)
125 GLfloat expected
[4] = {0.0, 1.0, 0.0, 1.0};
129 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, ms_fbo
.handle
);
130 glGetIntegerv(GL_SAMPLES
, &samples
);
131 glUniform1i(glGetUniformLocation(prog
, "samples"), MAX2(1, samples
));
133 glClear(GL_COLOR_BUFFER_BIT
);
134 piglit_draw_rect(-1, -1, 2, 2);
136 glBindFramebuffer(GL_READ_FRAMEBUFFER
, ms_fbo
.handle
);
137 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, singlesampled_fbo
.handle
);
138 glClear(GL_COLOR_BUFFER_BIT
);
139 glBlitFramebuffer(0, 0,
140 pattern_width
, pattern_height
,
142 pattern_width
, pattern_height
,
146 glBindFramebuffer(GL_READ_FRAMEBUFFER
, singlesampled_fbo
.handle
);
147 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, piglit_winsys_fbo
);
149 pass
= piglit_probe_rect_rgba(0, 0, pattern_width
, pattern_height
,
151 glClear(GL_COLOR_BUFFER_BIT
);
152 glBlitFramebuffer(0, 0,
153 pattern_width
, pattern_height
,
155 pattern_width
, pattern_height
,
159 piglit_present_results();
167 pass
= test_builtin_num_samples(multisampled_fbo
)
169 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;